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
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'.

Previous: René ScharfeNext: René Scharfe
Message 4 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.