diff mbox series

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

Message ID 20240229054004.3807-1-ach.lumap@gmail.com (mailing list archive)
State New, archived
Headers show
Series [Outreachy,v2,1/2] strbuf: introduce strbuf_addstrings() to repeatedly add a string | expand

Commit Message

Achu Luma Feb. 29, 2024, 5:40 a.m. UTC
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.

Such a strbuf_addstrings() function would already be useful in
"json-writer.c" and "builtin/submodule-helper.c" as both of these files
already have code that repeatedly adds the same string. So let's
introduce such a strbuf_addstrings() function in "strbuf.{c,h}" and use
it in both "json-writer.c" and "builtin/submodule-helper.c".

We use the "strbuf_addstrings" name as this way strbuf_addstr() and
strbuf_addstrings() would be similar for strings as strbuf_addch() and
strbuf_addchars() for characters.

Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Achu Luma <ach.lumap@gmail.com>
---
 The changes between version 1 and version 2 are the following:
 - Remove memcpy() that involves manual offset computation
 - Use strbuf_add() repeatedly after calling strbuf_grow() once to avoid
   repeated allocation.

 Thanks to Junio for pointing this out.
 Here is a diff between v1 and v2:

 -       size_t len = strlen(s);
 -       if (unsigned_mult_overflows(len, n))
 -               die("you want to use way too much memory");
 -       strbuf_grow(sb, len * n);
 -       for (size_t i = 0; i < n; i++)
 -               memcpy(sb->buf + sb->len + len * i, s, len);
 -       strbuf_setlen(sb, sb->len + len * n);
 +       size_t len = strlen(s);
 +
 +       if (unsigned_mult_overflows(len, n))
 +               die("you want to use way too much memory");
 +       strbuf_grow(sb, len * n);
 +       for (size_t i = 0; i < n; i++)
 +               strbuf_add(sb, s, len);

 builtin/submodule--helper.c |  4 +---
 json-writer.c               |  5 +----
 strbuf.c                    | 10 ++++++++++
 strbuf.h                    |  5 +++++
 4 files changed, 17 insertions(+), 7 deletions(-)

--
2.43.0.windows.1
diff mbox series

Patch

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index fda50f2af1..bed08af410 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -257,11 +257,9 @@  static void module_list_active(struct module_list *list)

 static char *get_up_path(const char *path)
 {
-	int i;
 	struct strbuf sb = STRBUF_INIT;

-	for (i = count_slashes(path); i; i--)
-		strbuf_addstr(&sb, "../");
+	strbuf_addstrings(&sb, "../", count_slashes(path));

 	/*
 	 * Check if 'path' ends with slash or not
diff --git a/json-writer.c b/json-writer.c
index 005c820aa4..25b9201f9c 100644
--- a/json-writer.c
+++ b/json-writer.c
@@ -46,10 +46,7 @@  static void append_quoted_string(struct strbuf *out, const char *in)

 static void indent_pretty(struct json_writer *jw)
 {
-	int k;
-
-	for (k = 0; k < jw->open_stack.len; k++)
-		strbuf_addstr(&jw->json, "  ");
+	strbuf_addstrings(&jw->json, "  ", jw->open_stack.len);
 }

 /*
diff --git a/strbuf.c b/strbuf.c
index 7827178d8e..f4282b70ad 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -302,6 +302,16 @@  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);
+
+       if (unsigned_mult_overflows(len, n))
+               die("you want to use way too much memory");
+       strbuf_grow(sb, len * n);
+       for (size_t i = 0; i < n; i++)
+               strbuf_add(sb, s, len);
+}
+
 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.
  */