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

[PATCH v2 3/4] strbuf: make add_lines() public

From
JTJonathan Tan <jonathantanmy@google.com>
Date
Oct 31, 2023, 22:28 UTC
Message-ID
<283f502acb68910cb43d6077eef99d6345aaea4b.1698791220.git.jonathantanmy@google.com>
In-Reply-To
<cover.1698791220.git.jonathantanmy@google.com>

A subsequent patch will require the ability to add different prefixes to different lines (depending on their contents), so make this functionality available from outside strbuf.c. The function name is chosen to avoid a conflict with the existing function named strbuf_add_lines().

Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
---
 strbuf.c | 22 +++++++++++-----------
 strbuf.h |  4 ++++
 2 files changed, 15 insertions(+), 11 deletions(-)
diff --git a/strbuf.c b/strbuf.c
index 2088f7800a..d5ee8874f8 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -340,24 +340,24 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
 	va_end(ap);
 }
 
-static void add_lines(struct strbuf *out,
-			const char *prefix1,
-			const char *prefix2,
-			const char *buf, size_t size)
+void strbuf_add_lines_varied_prefix(struct strbuf *sb,
+				    const char *default_prefix,
+				    const char *tab_nl_prefix,
+				    const char *buf, size_t size)
 {
 	while (size) {
 		const char *prefix;
 		const char *next = memchr(buf, '\n', size);
 		next = next ? (next + 1) : (buf + size);
 
-		prefix = ((prefix2 && (buf[0] == '\n' || buf[0] == '\t'))
-			  ? prefix2 : prefix1);
-		strbuf_addstr(out, prefix);
-		strbuf_add(out, buf, next - buf);
+		prefix = (buf[0] == '\n' || buf[0] == '\t')
+			  ? tab_nl_prefix : default_prefix;
+		strbuf_addstr(sb, prefix);
+		strbuf_add(sb, buf, next - buf);
 		size -= next - buf;
 		buf = next;
 	}
-	strbuf_complete_line(out);
+	strbuf_complete_line(sb);
 }
 
 void strbuf_add_commented_lines(struct strbuf *out,
@@ -370,7 +370,7 @@ void strbuf_add_commented_lines(struct strbuf *out,
 		xsnprintf(prefix1, sizeof(prefix1), "%c ", comment_line_char);
 		xsnprintf(prefix2, sizeof(prefix2), "%c", comment_line_char);
 	}
-	add_lines(out, prefix1, prefix2, buf, size);
+	strbuf_add_lines_varied_prefix(out, prefix1, prefix2, buf, size);
 }
 
 void strbuf_commented_addf(struct strbuf *sb,
@@ -751,7 +751,7 @@ ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
 void strbuf_add_lines(struct strbuf *out, const char *prefix,
 		      const char *buf, size_t size)
 {
-	add_lines(out, prefix, NULL, buf, size);
+	strbuf_add_lines_varied_prefix(out, prefix, prefix, buf, size);
 }
 
 void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
diff --git a/strbuf.h b/strbuf.h
index 4547efa62e..a9333ac1ad 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -601,6 +601,10 @@ void strbuf_add_lines(struct strbuf *sb,
 		      const char *prefix,
 		      const char *buf,
 		      size_t size);
+void strbuf_add_lines_varied_prefix(struct strbuf *sb,
+				    const char *default_prefix,
+				    const char *tab_nl_prefix,
+				    const char *buf, size_t size);
 
 /**
  * Append s to sb, with the characters '<', '>', '&' and '"' converted
-- 
2.42.0.820.g83a721a137-goog
Previous: Jonathan TanNext: Junio C Hamano
Message 18 of 21 in “Avoid passing global comment_line_char repeatedly”
  1. 0/2 Avoid passing global comment_line_char repeatedlyJunio C Hamano, Oct 30, 2023
  2. 1/2 strbuf_commented_addf(): drop the comment_line_char parameterJunio C Hamano, Oct 30, 2023
  3. 2/2 strbuf_add_commented_lines(): drop the comment_line_char parameterJunio C Hamano, Oct 30, 2023
  4. Dragan SimicOct 30, 2023
  5. Phillip WoodOct 30, 2023
  6. 0/3 Avoid passing global comment_line_char repeatedlyJonathan Tan, Oct 30, 2023
  7. 1/3 strbuf: make add_lines() publicJonathan Tan, Oct 30, 2023
  8. Junio C HamanoOct 30, 2023
  9. Junio C HamanoOct 31, 2023
  10. 2/3 strbuf_commented_addf(): drop the comment_line_char parameterJonathan Tan, Oct 30, 2023
  11. Junio C HamanoOct 31, 2023
  12. Jonathan TanOct 31, 2023
  13. Junio C HamanoOct 31, 2023
  14. 3/3 strbuf_add_commented_lines(): drop the comment_line_char parameterJonathan Tan, Oct 30, 2023
  15. 0/4 Avoid passing global comment_line_char repeatedlyJonathan Tan, Oct 31, 2023
  16. 1/4 strbuf_commented_addf(): drop the comment_line_char parameterJonathan Tan, Oct 31, 2023
  17. 2/4 strbuf_add_commented_lines(): drop the comment_line_char parameterJonathan Tan, Oct 31, 2023
  18. 3/4 strbuf: make add_lines() publicJonathan Tan, Oct 31, 2023
  19. Junio C HamanoNov 1, 2023
  20. 4/4 strbuf: move env-using functions to environment.cJonathan Tan, Oct 31, 2023
  21. Junio C HamanoNov 1, 2023

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.