Re: [PATCH 2/4] parse-options: factor out parse_options_count()
- From
Eric Sunshine <sunshine@sunshineco.com>
- Date
- Feb 9, 2020, 17:56 UTC
- Message-ID
- <CAPig+cQpzc6eZyOo9N=4sR3pBFza299rRn_wP0w2W7Zf5CWThg@mail.gmail.com>
- In-Reply-To
- <c729aaab-68d7-9cfa-8981-97eaa72a5ebe@web.de>
On Sun, Feb 9, 2020 at 10:56 AM René Scharfe <l.s.r@web.de> wrote:
Show 22 quoted lines
> 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>
> ---
> diff --git a/parse-options-cb.c b/parse-options-cb.c
> @@ -159,16 +159,20 @@ int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
> 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);This could use a little more cleanup. After this change, 'o' is never again consulted or changed, and 'orig' points at the original value of 'o', which means 'o' and 'orig' have the same value now always. Therefore, the additional cleanup would be to drop the declaration and assignment of 'orig' and reference 'o' in COPY_ARRAY() rather than 'orig'.