git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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é
Previous: Eric SunshineNext: René Scharfe
Message 5 of 9 in “parse-options: simplify parse_options_concat() and parse_options_dup()”
  1. 0/4 parse-options: simplify parse_options_concat() and parse_options_dup()René Scharfe, Feb 9, 2020
  2. 1/4 parse-options: use COPY_ARRAY in parse_options_concat()René Scharfe, Feb 9, 2020
  3. 2/4 parse-options: factor out parse_options_count()René Scharfe, Feb 9, 2020
  4. Eric SunshineFeb 9, 2020
  5. René ScharfeFeb 9, 2020
  6. 3/4 parse-options: const parse_options_concat() parametersRené Scharfe, Feb 9, 2020
  7. 4/4 parse-options: simplify parse_options_dup()René Scharfe, Feb 9, 2020
  8. Junio C HamanoFeb 10, 2020
  9. Jeff KingFeb 10, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.