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
Junio C Hamano <gitster@pobox.com>
Date
Feb 26, 2024, 16:39 UTC
Message-ID
<xmqqil2bdvsy.fsf@gitster.g>
In-Reply-To
<20240226143350.3596-1-ach.lumap@gmail.com>
Achu Luma <ach.lumap@gmail.com> writes:
Show 8 quoted lines
> 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 ;-).

Show 11 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.

> +	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.
> +	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? Repeated manual memcpy() that involves manual offset computation makes me nervous.

Show 24 quoted lines
> +	strbuf_setlen(sb, sb->len + len * n);
> +}
> +
>  void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)
>  {
>  	strbuf_grow(sb, sb2->len);
> diff --git a/strbuf.h b/strbuf.h
> index e959caca87..0fb1b5e81e 100644
> --- a/strbuf.h
> +++ b/strbuf.h
> @@ -310,6 +310,11 @@ static inline void strbuf_addstr(struct strbuf *sb, const char *s)
>  	strbuf_add(sb, s, strlen(s));
>  }
>
> +/**
> + * Add a NUL-terminated string the specified number of times to the buffer.
> + */
> +void strbuf_addstrings(struct strbuf *sb, const char *s, size_t n);
> +
>  /**
>   * Copy the contents of another buffer at the end of the current one.
>   */
> --
> 2.43.0.windows.1
Previous: Achu LumaNext: Christian Couder
Message 3 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.