]> git.ipfire.org Git - thirdparty/git.git/commitdiff
trailer: change strbuf in-place in unfold_value()
authorRené Scharfe <l.s.r@web.de>
Fri, 15 May 2026 07:33:53 +0000 (09:33 +0200)
committerJunio C Hamano <gitster@pobox.com>
Sat, 16 May 2026 13:11:07 +0000 (22:11 +0900)
Avoid an allocation by doing s/\n\s*/ /g (replacing NL and any following
whitespace with a SP) right in the strbuf instead of copying the result
to a temporary one and swapping them in the end.  We can safely do that
because the replacement is never longer than the original string.

Signed-off-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
trailer.c

index 470f86a4a2f83eb30b104b506025334aa1f03b0a..6d8ec7fa8d88b51dbc624345c73227c74b749e36 100644 (file)
--- a/trailer.c
+++ b/trailer.c
@@ -988,10 +988,9 @@ static int ends_with_blank_line(const char *buf, size_t len)
 
 static void unfold_value(struct strbuf *val)
 {
-       struct strbuf out = STRBUF_INIT;
        size_t i;
+       size_t pos = 0;
 
-       strbuf_grow(&out, val->len);
        i = 0;
        while (i < val->len) {
                char c = val->buf[i++];
@@ -999,18 +998,14 @@ static void unfold_value(struct strbuf *val)
                        /* Collapse continuation down to a single space. */
                        while (i < val->len && isspace(val->buf[i]))
                                i++;
-                       strbuf_addch(&out, ' ');
-               } else {
-                       strbuf_addch(&out, c);
+                       c = ' ';
                }
+               val->buf[pos++] = c;
        }
+       strbuf_setlen(val, pos);
 
        /* Empty lines may have left us with whitespace cruft at the edges */
-       strbuf_trim(&out);
-
-       /* output goes back to val as if we modified it in-place */
-       strbuf_swap(&out, val);
-       strbuf_release(&out);
+       strbuf_trim(val);
 }
 
 static struct trailer_block *trailer_block_new(void)