Skip to content

Commit f904f90

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a277d0a commit f904f90

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

parse-options-cb.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,20 @@ int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
159159
return 0;
160160
}
161161

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+
162171
struct option *parse_options_dup(const struct option *o)
163172
{
164173
const struct option *orig = o;
165174
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);
172176

173177
ALLOC_ARRAY(opts, nr + 1);
174178
COPY_ARRAY(opts, orig, nr);
@@ -180,12 +184,8 @@ struct option *parse_options_dup(const struct option *o)
180184
struct option *parse_options_concat(struct option *a, struct option *b)
181185
{
182186
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);
189189

190190
ALLOC_ARRAY(ret, st_add3(a_len, b_len, 1));
191191
COPY_ARRAY(ret, a, a_len);

0 commit comments

Comments
 (0)