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

Re: [Outreachy][PATCH 1/2] strbuf: introduce strbuf_addstrings() to repeatedly add a string

From
Christian Couder <christian.couder@gmail.com>
Date
Feb 26, 2024, 17:15 UTC
Message-ID
<CAP8UFD0Qhy78=v9+hCekMJPkcH2KmeZeQ0xUx8kqrByQ4PO3Xg@mail.gmail.com>
In-Reply-To
<xmqqil2bdvsy.fsf@gitster.g>
On Mon, Feb 26, 2024 at 5:39 PM Junio C Hamano <gitster@pobox.com> wrote:
Show 27 quoted lines
>
> Achu Luma <ach.lumap@gmail.com> writes:
>
> > In a following commit we are going to port code from
> > "t/helper/test-sha256.c", t/helper/test-hash.c and "t/t0015-hash.sh" to
> > a new "t/unit-tests/t-hash.c" file using the recently added unit test
> > framework.
> >
> > To port code like: perl -e "$| = 1; print q{aaaaaaaaaa} for 1..100000;"
> > we are going to need a new strbuf_addstrings() function that repeatedly
> > adds the same string a number of times to a buffer.
>
> We do not need to call such a function "addstrings", though.  The
> name on the subject line made me expect a varargs function:
>
>  (bad)  strbuf_addstrings(&sb, "foo", "bar", "baz", NULL);
>
> It would have been clearer if the name hinted what it does, clearer
> than just a single "s" that says it is talking about plural.  What
> would be a good name that hints "n times add a single same string"?
> I dunno.
>
> I also would have expected that the order of parameters are
> repeat-count followed by what gets repeated.
>
> Having said all of the above, we already have "addchars" that is
> equally strange, so let's let it pass ;-).

Yeah, we thought about naming it strbuf_repeatstr() first, but then seeing addchars() I thought it would be better to imitate it.

Show 14 quoted lines
> > diff --git a/strbuf.c b/strbuf.c
> > index 7827178d8e..eb2b3299ce 100644
> > --- a/strbuf.c
> > +++ b/strbuf.c
> > @@ -302,6 +302,17 @@ void strbuf_add(struct strbuf *sb, const void *data, size_t len)
> >       strbuf_setlen(sb, sb->len + len);
> >  }
> >
> > +void strbuf_addstrings(struct strbuf *sb, const char *s, size_t n)
> > +{
> > +     size_t len = strlen(s);
>
> Let's have a blank line here to separate decls from the first
> statement.
Yeah, right.
Show 9 quoted lines
> > +     if (unsigned_mult_overflows(len, n))
> > +             die("you want to use way too much memory");
> > +     strbuf_grow(sb, len * n);
>
> The error message given by
>
>         strbuf_grow(sb, st_mult(len, n));
>
> would be equally informative and takes only a single line.

It seems that the pattern in strbuf.c, for example in strbuf_splice() and strbuf_vinsertf(), is to do a check first using an *_overflows() function, die if the check fails, and only then call strbuf_grow(). So we did the same. I am fine with using your suggestion though.

Show 6 quoted lines
> > +     for (size_t i = 0; i < n; i++)
> > +             memcpy(sb->buf + sb->len + len * i, s, len);
>
> Wouldn't it be sufficient to run strbuf_add() n times at this point,
> as we have already called strbuf_grow() to avoid repeated
> reallocation?

Unfortunately strbuf_add() calls strbuf_grow() itself which is not needed as we have already called strbuf_grow() to avoid repeated reallocation.

>  Repeated manual memcpy() that involves manual offset
> computation makes me nervous.
I would have prefered to avoid it too, but didn't find a good way to do it.
> > +     strbuf_setlen(sb, sb->len + len * n);
> > +}
Previous: Junio C HamanoNext: Junio C Hamano
Message 4 of 35 in “strbuf: introduce strbuf_addstrings() to repeatedly add a string”
  1. Achu LumaFeb 26, 2024
  2. [Outreachy][PATCH 2/2] Port helper/test-sha256.c and helper/test-sha1.c to unit-tests/t-hash.cAchu Luma, Feb 26, 2024
  3. Junio C HamanoFeb 26, 2024
  4. Christian CouderFeb 26, 2024
  5. Junio C HamanoFeb 26, 2024
  6. Christian CouderFeb 27, 2024
  7. [Outreachy][PATCH v2 1/2] strbuf: introduce strbuf_addstrings() to repeatedly add a stringAchu Luma, Feb 29, 2024
  8. [Outreachy][PATCH v2 2/2] Port helper/test-sha256.c and helper/test-sha1.c to unit-tests/t-hash.cAchu Luma, Feb 29, 2024
  9. Christian CouderMar 6, 2024
  10. Patrick SteinhardtMar 26, 2024
  11. Christian CouderMar 26, 2024
  12. Ghanshyam ThakkarMay 16, 2024
  13. 0/3 Port t0015-hash to the unit testing frameworkGhanshyam Thakkar, May 23, 2024
  14. 1/3 strbuf: introduce strbuf_addstrings() to repeatedly add a stringGhanshyam Thakkar, May 23, 2024
  15. 2/3 t/: port helper/test-sha1.c to unit-tests/t-hash.cGhanshyam Thakkar, May 23, 2024
  16. Patrick SteinhardtMay 24, 2024
  17. Christian CouderMay 24, 2024
  18. Junio C HamanoMay 24, 2024
  19. Ghanshyam ThakkarJun 15, 2024
  20. Jeff KingJun 16, 2024
  21. Junio C HamanoJun 17, 2024
  22. Ghanshyam ThakkarJun 21, 2024
  23. 3/3 t/: port helper/test-sha256.c to unit-tests/t-hash.cGhanshyam Thakkar, May 23, 2024
  24. Patrick SteinhardtMay 24, 2024
  25. Ghanshyam ThakkarMay 25, 2024
  26. 0/2 t/: port helper/test-{sha1, sha256} to unit-tests/t-hashGhanshyam Thakkar, May 26, 2024
  27. 1/2 strbuf: introduce strbuf_addstrings() to repeatedly add a stringGhanshyam Thakkar, May 26, 2024
  28. 2/2 t/: migrate helper/test-{sha1, sha256} to unit-tests/t-hashGhanshyam Thakkar, May 26, 2024
  29. Patrick SteinhardtMay 29, 2024
  30. Junio C HamanoMay 29, 2024
  31. [GSoC][PATCH v5 0/2] t/: migrate helper/test-{sha1, sha256} to unit-tests/t-hashGhanshyam Thakkar, May 29, 2024
  32. 1/2 strbuf: introduce strbuf_addstrings() to repeatedly add a stringGhanshyam Thakkar, May 29, 2024
  33. 2/2 t/: migrate helper/test-{sha1, sha256} to unit-tests/t-hashGhanshyam Thakkar, May 29, 2024
  34. Patrick SteinhardtMay 29, 2024
  35. Junio C HamanoMay 29, 2024

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.