git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Tan <jonathantanmy@google.com>
To: git@vger.kernel.org
Cc: Jonathan Tan <jonathantanmy@google.com>,
	Junio C Hamano <gitster@pobox.com>,
	 Phillip Wood <phillip.wood123@gmail.com>,
	Dragan Simic <dsimic@manjaro.org>
Subject: [RFC PATCH 1/3] strbuf: make add_lines() public
Date: Mon, 30 Oct 2023 13:22:46 -0700	[thread overview]
Message-ID: <d96633a2919ac619ccf29e87abc6f25314a8bfb1.1698696798.git.jonathantanmy@google.com> (raw)
In-Reply-To: <cover.1698696798.git.jonathantanmy@google.com>

Subsequent patches will require the ability to add different prefixes
to different lines (depending on their contents), so make this
functionality available from outside strbuf.c.

Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
---
 branch.c |  3 ++-
 commit.c |  2 +-
 strbuf.c | 39 ++++++++++++++++-----------------------
 strbuf.h |  3 ++-
 4 files changed, 21 insertions(+), 26 deletions(-)

diff --git a/branch.c b/branch.c
index 06f7af9dd4..04a8b90b6a 100644
--- a/branch.c
+++ b/branch.c
@@ -721,7 +721,8 @@ static int submodule_create_branch(struct repository *r,
 		return ret;
 	ret = finish_command(&child);
 	strbuf_read(&child_err, child.err, 0);
-	strbuf_add_lines(&out_buf, out_prefix, child_err.buf, child_err.len);
+	strbuf_add_lines(&out_buf, out_prefix, out_prefix,
+			 child_err.buf, child_err.len);
 
 	if (ret)
 		fprintf(stderr, "%s", out_buf.buf);
diff --git a/commit.c b/commit.c
index b3223478bc..7caafcde01 100644
--- a/commit.c
+++ b/commit.c
@@ -1361,7 +1361,7 @@ static void add_extra_header(struct strbuf *buffer,
 {
 	strbuf_addstr(buffer, extra->key);
 	if (extra->len)
-		strbuf_add_lines(buffer, " ", extra->value, extra->len);
+		strbuf_add_lines(buffer, " ", " ", extra->value, extra->len);
 	else
 		strbuf_addch(buffer, '\n');
 }
diff --git a/strbuf.c b/strbuf.c
index 7827178d8e..9ee639519a 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -339,26 +339,6 @@ 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)
-{
-	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);
-		size -= next - buf;
-		buf = next;
-	}
-	strbuf_complete_line(out);
-}
-
 void strbuf_add_commented_lines(struct strbuf *out, const char *buf,
 				size_t size, char comment_line_char)
 {
@@ -369,7 +349,7 @@ void strbuf_add_commented_lines(struct strbuf *out, const char *buf,
 		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(out, prefix1, prefix2, buf, size);
 }
 
 void strbuf_commented_addf(struct strbuf *sb, char comment_line_char,
@@ -747,10 +727,23 @@ ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
 	return len;
 }
 
-void strbuf_add_lines(struct strbuf *out, const char *prefix,
+void strbuf_add_lines(struct strbuf *out, const char *default_prefix,
+		      const char *tab_or_nl_prefix,
 		      const char *buf, size_t size)
 {
-	add_lines(out, prefix, NULL, buf, size);
+	while (size) {
+		const char *prefix;
+		const char *next = memchr(buf, '\n', size);
+		next = next ? (next + 1) : (buf + size);
+
+		prefix = (buf[0] == '\n' || buf[0] == '\t')
+			  ? tab_or_nl_prefix : default_prefix;
+		strbuf_addstr(out, prefix);
+		strbuf_add(out, buf, next - buf);
+		size -= next - buf;
+		buf = next;
+	}
+	strbuf_complete_line(out);
 }
 
 void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
diff --git a/strbuf.h b/strbuf.h
index e959caca87..3559e73dd8 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -599,7 +599,8 @@ void strbuf_list_free(struct strbuf **list);
 void strbuf_strip_file_from_path(struct strbuf *sb);
 
 void strbuf_add_lines(struct strbuf *sb,
-		      const char *prefix,
+		      const char *default_prefix,
+		      const char *tab_or_nl_prefix,
 		      const char *buf,
 		      size_t size);
 
-- 
2.42.0.820.g83a721a137-goog


  reply	other threads:[~2023-10-30 20:23 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-30  5:10 [PATCH 0/2] Avoid passing global comment_line_char repeatedly Junio C Hamano
2023-10-30  5:10 ` [PATCH 1/2] strbuf_commented_addf(): drop the comment_line_char parameter Junio C Hamano
2023-10-30  5:10 ` [PATCH 2/2] strbuf_add_commented_lines(): " Junio C Hamano
2023-10-30  5:36 ` [PATCH 0/2] Avoid passing global comment_line_char repeatedly Dragan Simic
2023-10-30  9:59 ` Phillip Wood
2023-10-30 20:22   ` [RFC PATCH 0/3] " Jonathan Tan
2023-10-30 20:22     ` Jonathan Tan [this message]
2023-10-30 23:53       ` [RFC PATCH 1/3] strbuf: make add_lines() public Junio C Hamano
2023-10-31  6:01         ` Junio C Hamano
2023-10-30 20:22     ` [RFC PATCH 2/3] strbuf_commented_addf(): drop the comment_line_char parameter Jonathan Tan
2023-10-31  5:19       ` Junio C Hamano
2023-10-31 22:24         ` Jonathan Tan
2023-10-31 23:54           ` Junio C Hamano
2023-10-30 20:22     ` [RFC PATCH 3/3] strbuf_add_commented_lines(): " Jonathan Tan
2023-10-31 22:28   ` [PATCH v2 0/4] Avoid passing global comment_line_char repeatedly Jonathan Tan
2023-10-31 22:28     ` [PATCH v2 1/4] strbuf_commented_addf(): drop the comment_line_char parameter Jonathan Tan
2023-10-31 22:28     ` [PATCH v2 2/4] strbuf_add_commented_lines(): " Jonathan Tan
2023-10-31 22:28     ` [PATCH v2 3/4] strbuf: make add_lines() public Jonathan Tan
2023-11-01  4:14       ` Junio C Hamano
2023-10-31 22:28     ` [PATCH v2 4/4] strbuf: move env-using functions to environment.c Jonathan Tan
2023-11-01  4:37       ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d96633a2919ac619ccf29e87abc6f25314a8bfb1.1698696798.git.jonathantanmy@google.com \
    --to=jonathantanmy@google.com \
    --cc=dsimic@manjaro.org \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=phillip.wood123@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).