Re: [PATCH] parse-options: use COPY_ARRAY in parse_options_concat()
- From
- Martin Ågren <martin.agren@gmail.com>
- Date
- Nov 13, 2019, 18:25 UTC
- Message-ID
- <20191113182548.1707-1-martin.agren@gmail.com>
- In-Reply-To
- <df941c25-22ea-3523-9607-8e892dea320c@web.de>
On Wed, 13 Nov 2019 at 18:19, René Scharfe <l.s.r@web.de> wrote:
Show 8 quoted lines
> ALLOC_ARRAY(ret, st_add3(a_len, b_len, 1)); > - for (i = 0; i < a_len; i++) > - ret[i] = a[i]; > - for (i = 0; i < b_len; i++) > - ret[a_len + i] = b[i]; > + COPY_ARRAY(ret, a, a_len); > + COPY_ARRAY(ret + a_len, b, b_len); > ret[a_len + b_len] = b[b_len]; /* final OPTION_END */
Maybe include that last one in the COPY_ARRAY with something like this on top?
- COPY_ARRAY(ret + a_len, b, b_len); - ret[a_len + b_len] = b[b_len]; /* final OPTION_END */ + /* 1 more to include the final OPTION_END */ + COPY_ARRAY(ret + a_len, b, st_add(b_len, 1));
Or maybe even something like the below (directly on top of your patch)?
(Plus, you could drop `i` entirely, but I'm not sure that's worth golfing on.)
Martin
diff --git a/parse-options-cb.c b/parse-options-cb.c index 2bde78b726..11196cfb96 100644 --- a/parse-options-cb.c +++ b/parse-options-cb.c @@ -185,11 +185,11 @@ struct option *parse_options_concat(struct option *a, struct option *b) a_len++; for (i = 0; b[i].type != OPTION_END; i++) b_len++; + b_len++; /* final OPTION_END */ - ALLOC_ARRAY(ret, st_add3(a_len, b_len, 1)); + ALLOC_ARRAY(ret, st_add(a_len, b_len)); COPY_ARRAY(ret, a, a_len); COPY_ARRAY(ret + a_len, b, b_len); - ret[a_len + b_len] = b[b_len]; /* final OPTION_END */ return ret; }
-- 2.24.0