parse-options: factor out parse_options_count()
Add a helper function to count the number of options (excluding the final OPT_END()) and use it to simplify parse_options_dup() and parse_options_concat(). Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Feb 9, 2020 at 16:56 UTC
f904f9025f070b17a440d019b53e1d70fca3a269
1 file changed
+12
-12
parse-options-cb.c
+12
-12
@@ -159,16 +159,20 @@ int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
159
return 0;
160
}
161
162
+static size_t parse_options_count(const struct option *opt)
163
+{
164
+ size_t n = 0;
165
+
166
+ for (; opt && opt->type != OPTION_END; opt++)
167
+ n++;
168
+ return n;
169
+}
170
+
171
struct option *parse_options_dup(const struct option *o)
172
{
173
const struct option *orig = o;
174
struct option *opts;
166
- int nr = 0;
167
-
168
- while (o && o->type != OPTION_END) {
169
- nr++;
170
- o++;
171
- }
175
+ size_t nr = parse_options_count(o);
176
177
ALLOC_ARRAY(opts, nr + 1);
178
COPY_ARRAY(opts, orig, nr);
@@ -180,12 +184,8 @@ struct option *parse_options_dup(const struct option *o)
184
struct option *parse_options_concat(struct option *a, struct option *b)
185
{
186
struct option *ret;
183
- size_t i, a_len = 0, b_len = 0;
184
-
185
- for (i = 0; a[i].type != OPTION_END; i++)
186
- a_len++;
187
- for (i = 0; b[i].type != OPTION_END; i++)
188
- b_len++;
187
+ size_t a_len = parse_options_count(a);
188
+ size_t b_len = parse_options_count(b);
189
190
ALLOC_ARRAY(ret, st_add3(a_len, b_len, 1));
191
COPY_ARRAY(ret, a, a_len);