summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2020-02-17 21:22:17 (GMT)
committerJunio C Hamano <gitster@pobox.com>2020-02-17 21:22:17 (GMT)
commitcbecc168d4264d93295cd1e0a48dc19f0ed1880c (patch)
tree930d8f95060899f4abe7176b6d7437677c62ec14
parent5af345a4388630b422a53fecfcc48b33e2439380 (diff)
parent7a9f8ca805045f5d6d59227c8cce5a3e6790b0ef (diff)
downloadgit-cbecc168d4264d93295cd1e0a48dc19f0ed1880c.zip
git-cbecc168d4264d93295cd1e0a48dc19f0ed1880c.tar.gz
git-cbecc168d4264d93295cd1e0a48dc19f0ed1880c.tar.bz2
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()
-rw-r--r--parse-options-cb.c42
-rw-r--r--parse-options.h2
2 files changed, 18 insertions, 26 deletions
diff --git a/parse-options-cb.c b/parse-options-cb.c
index c2062ae..a28b55b 100644
--- a/parse-options-cb.c
+++ b/parse-options-cb.c
@@ -159,40 +159,32 @@ int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
return 0;
}
-struct option *parse_options_dup(const struct option *o)
+static size_t parse_options_count(const struct option *opt)
{
- const struct option *orig = o;
- struct option *opts;
- int nr = 0;
+ size_t n = 0;
- while (o && o->type != OPTION_END) {
- nr++;
- o++;
- }
+ for (; opt && opt->type != OPTION_END; opt++)
+ n++;
+ return n;
+}
- ALLOC_ARRAY(opts, nr + 1);
- COPY_ARRAY(opts, orig, nr);
- memset(opts + nr, 0, sizeof(*opts));
- opts[nr].type = OPTION_END;
- return opts;
+struct option *parse_options_dup(const struct option *o)
+{
+ struct option no_options[] = { OPT_END() };
+
+ return parse_options_concat(o, no_options);
}
-struct option *parse_options_concat(struct option *a, struct option *b)
+struct option *parse_options_concat(const struct option *a,
+ const struct option *b)
{
struct option *ret;
- size_t i, a_len = 0, b_len = 0;
-
- for (i = 0; a[i].type != OPTION_END; i++)
- a_len++;
- for (i = 0; b[i].type != OPTION_END; i++)
- b_len++;
+ size_t a_len = parse_options_count(a);
+ size_t b_len = parse_options_count(b);
ALLOC_ARRAY(ret, st_add3(a_len, b_len, 1));
- for (i = 0; i < a_len; i++)
- ret[i] = a[i];
- for (i = 0; i < b_len; i++)
- ret[a_len + i] = b[i];
- ret[a_len + b_len] = b[b_len]; /* final OPTION_END */
+ COPY_ARRAY(ret, a, a_len);
+ COPY_ARRAY(ret + a_len, b, b_len + 1); /* + 1 for final OPTION_END */
return ret;
}
diff --git a/parse-options.h b/parse-options.h
index fdc0c1c..1d60205 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -281,7 +281,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
int parse_options_end(struct parse_opt_ctx_t *ctx);
struct option *parse_options_dup(const struct option *a);
-struct option *parse_options_concat(struct option *a, struct option *b);
+struct option *parse_options_concat(const struct option *a, const struct option *b);
/*----- some often used options -----*/
int parse_opt_abbrev_cb(const struct option *, const char *, int);