Re: [PATCH 2/4] parse-options: factor out parse_options_count()
- From
René Scharfe <l.s.r@web.de>
- Date
- Feb 9, 2020, 18:36 UTC
- Message-ID
- <4577baea-0cc6-5fd9-a1c4-699b15dae380@web.de>
- In-Reply-To
- <CAPig+cQpzc6eZyOo9N=4sR3pBFza299rRn_wP0w2W7Zf5CWThg@mail.gmail.com>
Am 09.02.20 um 18:56 schrieb Eric Sunshine:
Show 30 quoted lines
> On Sun, Feb 9, 2020 at 10:56 AM René Scharfe <l.s.r@web.de> wrote:
>> 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'.True, but that would go beyond the purpose of this patch, which is to extract a count function. While it enables the cleanup you mentioned, the latter should go into its own patch. The last one in the series takes care of it.
René