trailer: change strbuf in-place in unfold_value()

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>

René Scharfe committed May 15, 2026 at 09:33 UTC 34a891a2d30865316be2628949d4f1b005f65662
1 file changed +5 -10
trailer.c
+5 -10
@@ -988,10 +988,9 @@ static int ends_with_blank_line(const char *buf, size_t len)
988
989 static void unfold_value(struct strbuf *val)
990 {
991 - struct strbuf out = STRBUF_INIT;
991 size_t i;
992 + size_t pos = 0;
993
994 - strbuf_grow(&out, val->len);
994 i = 0;
995 while (i < val->len) {
996 char c = val->buf[i++];
@@ -999,18 +998,14 @@ static void unfold_value(struct strbuf *val)
998 /* Collapse continuation down to a single space. */
999 while (i < val->len && isspace(val->buf[i]))
1000 i++;
1002 - strbuf_addch(&out, ' ');
1003 - } else {
1004 - strbuf_addch(&out, c);
1001 + c = ' ';
1002 }
1003 + val->buf[pos++] = c;
1004 }
1005 + strbuf_setlen(val, pos);
1006
1007 /* Empty lines may have left us with whitespace cruft at the edges */
1009 - strbuf_trim(&out);
1010 -
1011 - /* output goes back to val as if we modified it in-place */
1012 - strbuf_swap(&out, val);
1013 - strbuf_release(&out);
1008 + strbuf_trim(val);
1009 }
1010
1011 static struct trailer_block *trailer_block_new(void)