Skip to content

Commit cbecc16

Browse files
committed
Merge branch 'rs/parse-options-concat-dup'
Code clean-up. * rs/parse-options-concat-dup: parse-options: simplify parse_options_dup() parse-options: const parse_options_concat() parameters parse-options: factor out parse_options_count() parse-options: use COPY_ARRAY in parse_options_concat()
2 parents 5af345a + 7a9f8ca commit cbecc16

File tree

2 files changed

+18
-26
lines changed

2 files changed

+18
-26
lines changed

parse-options-cb.c

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

162-
struct option *parse_options_dup(const struct option *o)
162+
static size_t parse_options_count(const struct option *opt)
163163
{
164-
const struct option *orig = o;
165-
struct option *opts;
166-
int nr = 0;
164+
size_t n = 0;
167165

168-
while (o && o->type != OPTION_END) {
169-
nr++;
170-
o++;
171-
}
166+
for (; opt && opt->type != OPTION_END; opt++)
167+
n++;
168+
return n;
169+
}
172170

173-
ALLOC_ARRAY(opts, nr + 1);
174-
COPY_ARRAY(opts, orig, nr);
175-
memset(opts + nr, 0, sizeof(*opts));
176-
opts[nr].type = OPTION_END;
177-
return opts;
171+
struct option *parse_options_dup(const struct option *o)
172+
{
173+
struct option no_options[] = { OPT_END() };
174+
175+
return parse_options_concat(o, no_options);
178176
}
179177

180-
struct option *parse_options_concat(struct option *a, struct option *b)
178+
struct option *parse_options_concat(const struct option *a,
179+
const struct option *b)
181180
{
182181
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++;
182+
size_t a_len = parse_options_count(a);
183+
size_t b_len = parse_options_count(b);
189184

190185
ALLOC_ARRAY(ret, st_add3(a_len, b_len, 1));
191-
for (i = 0; i < a_len; i++)
192-
ret[i] = a[i];
193-
for (i = 0; i < b_len; i++)
194-
ret[a_len + i] = b[i];
195-
ret[a_len + b_len] = b[b_len]; /* final OPTION_END */
186+
COPY_ARRAY(ret, a, a_len);
187+
COPY_ARRAY(ret + a_len, b, b_len + 1); /* + 1 for final OPTION_END */
196188

197189
return ret;
198190
}

parse-options.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
281281
int parse_options_end(struct parse_opt_ctx_t *ctx);
282282

283283
struct option *parse_options_dup(const struct option *a);
284-
struct option *parse_options_concat(struct option *a, struct option *b);
284+
struct option *parse_options_concat(const struct option *a, const struct option *b);
285285

286286
/*----- some often used options -----*/
287287
int parse_opt_abbrev_cb(const struct option *, const char *, int);

0 commit comments

Comments
 (0)