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

[PATCH 1/4] strbuf: add strbuf_add_uint()

From
René Scharfe <l.s.r@web.de>
Date
May 12, 2026, 11:56 UTC
Message-ID
<20260512115603.80780-2-l.s.r@web.de>
In-Reply-To
<20260512115603.80780-1-l.s.r@web.de>

strbuf_addf() calls vsnprintf(3) underneath, which supports a plethora of formatting options. We can avoid its overhead in basic cases by providing specialized functions like strbuf_addstr() for strings. Add another one, strbuf_add_uint(), for unsigned integers.

Prepare the number string in a temporary buffer. Make it big enough for any unsigned integer value: A decimal digit can represent ln(10)/ln(2) ≈ 3.32 bits; dividing the number of bits of uintmax_t by 3.3 and rounding up gives a sufficiently close conservative size estimate.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 strbuf.c | 12 ++++++++++++
 strbuf.h |  6 ++++++
 2 files changed, 18 insertions(+)
diff --git a/strbuf.c b/strbuf.c
index 3e04addc22..9731ecdc1f 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -361,6 +361,18 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
 	va_end(ap);
 }
 
+void strbuf_add_uint(struct strbuf *sb, uintmax_t value)
+{
+	char buf[DIV_ROUND_UP(bitsizeof(value) * 10, 33)];
+	char *end = buf + sizeof(buf);
+	char *p = end;
+
+	do
+		*--p = "0123456789"[value % 10];
+	while (value /= 10);
+	strbuf_add(sb, p, end - p);
+}
+
 static void add_lines(struct strbuf *out,
 			const char *prefix,
 			const char *buf, size_t size,
diff --git a/strbuf.h b/strbuf.h
index 06e284f9cc..1089ae687b 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -410,6 +410,12 @@ void strbuf_humanise_rate(struct strbuf *buf, off_t bytes);
 __attribute__((format (printf,2,3)))
 void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
 
+
+/**
+ * Add an unsigned decimal number.
+ */
+void strbuf_add_uint(struct strbuf *sb, uintmax_t value);
+
 /**
  * Add a formatted string prepended by a comment character and a
  * blank to the buffer.
-- 
2.54.0
Previous: Jeff KingNext: Jeff King
Message 4 of 18 in “strbuf: add and use strbuf_add_uint()”
  1. 0/4 strbuf: add and use strbuf_add_uint()René Scharfe, May 12, 2026
  2. 2/4 cat-file: use strbuf_add_uint()René Scharfe, May 12, 2026
  3. Jeff KingMay 12, 2026
  4. 1/4 strbuf: add strbuf_add_uint()René Scharfe, May 12, 2026
  5. Jeff KingMay 12, 2026
  6. René ScharfeMay 12, 2026
  7. Jeff KingMay 13, 2026
  8. Jeff KingMay 13, 2026
  9. Jeff KingMay 13, 2026
  10. René ScharfeMay 14, 2026
  11. Junio C HamanoMay 14, 2026
  12. Jeff KingMay 15, 2026
  13. René ScharfeMay 13, 2026
  14. 4/4 ls-tree: use strbuf_add_uint()René Scharfe, May 12, 2026
  15. 3/4 ls-files: use strbuf_add_uint()René Scharfe, May 12, 2026
  16. Jeff KingMay 12, 2026
  17. René ScharfeMay 12, 2026
  18. Jeff KingMay 13, 2026

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.