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@xxxxxxxxxx> --- 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