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

[PATCH 2/4] utf8.c: remove print_spaces()

From
René Scharfe <rene.scharfe@lsrfire.ath.cx>
Date
Feb 19, 2010, 22:15 UTC
Message-ID
<4B7F0D9B.2060904@lsrfire.ath.cx>
In-Reply-To
<4B7F0D08.6040608@lsrfire.ath.cx>

The previous patch made sure that strbuf_add_wrapped_text() (and thus strbuf_add_indented_text(), too) always get a strbuf. Make use of this fact by adding strbuf_addchars(), a small helper that adds a char the specified number of times to a strbuf, and use it to replace print_spaces().

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
---
 utf8.c |   15 ++++++---------
 1 files changed, 6 insertions(+), 9 deletions(-)
diff --git a/utf8.c b/utf8.c
index 831cad6..5146d30 100644
--- a/utf8.c
+++ b/utf8.c
@@ -288,14 +288,11 @@ static inline void strbuf_write(struct strbuf *sb, const char *buf, int len)
 		fwrite(buf, len, 1, stdout);
 }
 
-static void print_spaces(struct strbuf *buf, int count)
+static void strbuf_addchars(struct strbuf *sb, int c, size_t n)
 {
-	static const char s[] = "                    ";
-	while (count >= sizeof(s)) {
-		strbuf_write(buf, s, sizeof(s) - 1);
-		count -= sizeof(s) - 1;
-	}
-	strbuf_write(buf, s, count);
+	strbuf_grow(sb, n);
+	memset(sb->buf + sb->len, c, n);
+	strbuf_setlen(sb, sb->len + n);
 }
 
 static void strbuf_add_indented_text(struct strbuf *buf, const char *text,
@@ -307,7 +304,7 @@ static void strbuf_add_indented_text(struct strbuf *buf, const char *text,
 		const char *eol = strchrnul(text, '\n');
 		if (*eol == '\n')
 			eol++;
-		print_spaces(buf, indent);
+		strbuf_addchars(buf, ' ', indent);
 		strbuf_write(buf, text, eol - text);
 		text = eol;
 		indent = indent2;
@@ -366,7 +363,7 @@ int strbuf_add_wrapped_text(struct strbuf *buf,
 				if (space)
 					start = space;
 				else
-					print_spaces(buf, indent);
+					strbuf_addchars(buf, ' ', indent);
 				strbuf_write(buf, start, text - start);
 				if (!c)
 					return w;
-- 
1.7.0
Previous: René ScharfeNext: René Scharfe
Message 3 of 8 in “utf8.c: strbuf'ify strbuf_add_wrapped_text()”
  1. 0/4 utf8.c: strbuf'ify strbuf_add_wrapped_text()René Scharfe, Feb 19, 2010
  2. 1/4 utf8.c: remove print_wrapped_text()René Scharfe, Feb 19, 2010
  3. 2/4 utf8.c: remove print_spaces()René Scharfe, Feb 19, 2010
  4. 3/4 utf8.c: remove strbuf_write()René Scharfe, Feb 19, 2010
  5. 4/4 utf8.c: speculatively assume utf-8 in strbuf_add_wrapped_text()René Scharfe, Feb 19, 2010
  6. Johannes SchindelinFeb 20, 2010
  7. Junio C HamanoFeb 20, 2010
  8. Johannes SchindelinFeb 20, 2010

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.