]> git.ipfire.org Git - thirdparty/git.git/commitdiff
parse-options: factor out parse_options_count()
authorRené Scharfe <l.s.r@web.de>
Sun, 9 Feb 2020 15:56:47 +0000 (16:56 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 10 Feb 2020 17:43:25 +0000 (09:43 -0800)
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>
parse-options-cb.c

index 012e048856db51083b7950295711c102a778d52a..db6f666ef75baa3d6584eae3c979d94808c00f98 100644 (file)
@@ -159,16 +159,20 @@ int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
        return 0;
 }
 
+static size_t parse_options_count(const struct option *opt)
+{
+       size_t n = 0;
+
+       for (; opt && opt->type != OPTION_END; opt++)
+               n++;
+       return n;
+}
+
 struct option *parse_options_dup(const struct option *o)
 {
        const struct option *orig = o;
        struct option *opts;
-       int nr = 0;
-
-       while (o && o->type != OPTION_END) {
-               nr++;
-               o++;
-       }
+       size_t nr = parse_options_count(o);
 
        ALLOC_ARRAY(opts, nr + 1);
        COPY_ARRAY(opts, orig, nr);
@@ -180,12 +184,8 @@ struct option *parse_options_dup(const struct option *o)
 struct option *parse_options_concat(struct option *a, 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));
        COPY_ARRAY(ret, a, a_len);