strbuf: factor out strbuf_expand_step()

Extract the part of strbuf_expand that finds the next placeholder into a new function. It allows to build parsers without callback functions and the overhead imposed by them. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jun 17, 2023 at 22:41 UTC 44ccb337f10a08bb265b911f86deaf5f3347d967
3 files changed +24 -25
builtin/branch.c
+2 -11
@@ -366,17 +366,8 @@ static const char *quote_literal_for_format(const char *s)
366 static struct strbuf buf = STRBUF_INIT;
367
368 strbuf_reset(&buf);
369 - while (*s) {
370 - const char *ep = strchrnul(s, '%');
371 - if (s < ep)
372 - strbuf_add(&buf, s, ep - s);
373 - if (*ep == '%') {
374 - strbuf_addstr(&buf, "%%");
375 - s = ep + 1;
376 - } else {
377 - s = ep;
378 - }
379 - }
369 + while (strbuf_expand_step(&buf, &s))
370 + strbuf_addstr(&buf, "%%");
371 return buf.buf;
372 }
373
strbuf.c
+14 -14
@@ -415,19 +415,24 @@ void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
415 strbuf_setlen(sb, sb->len + len);
416 }
417
418 +int strbuf_expand_step(struct strbuf *sb, const char **formatp)
419 +{
420 + const char *format = *formatp;
421 + const char *percent = strchrnul(format, '%');
422 +
423 + strbuf_add(sb, format, percent - format);
424 + if (!*percent)
425 + return 0;
426 + *formatp = percent + 1;
427 + return 1;
428 +}
429 +
430 void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
431 void *context)
432 {
421 - for (;;) {
422 - const char *percent;
433 + while (strbuf_expand_step(sb, &format)) {
434 size_t consumed;
435
425 - percent = strchrnul(format, '%');
426 - strbuf_add(sb, format, percent - format);
427 - if (!*percent)
428 - break;
429 - format = percent + 1;
430 -
436 if (*format == '%') {
437 strbuf_addch(sb, '%');
438 format++;
@@ -1022,12 +1027,7 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm,
1027 * we want for %z, but the computation for %s has to convert to number
1028 * of seconds.
1029 */
1025 - for (;;) {
1026 - const char *percent = strchrnul(fmt, '%');
1027 - strbuf_add(&munged_fmt, fmt, percent - fmt);
1028 - if (!*percent)
1029 - break;
1030 - fmt = percent + 1;
1030 + while (strbuf_expand_step(&munged_fmt, &fmt)) {
1031 switch (*fmt) {
1032 case '%':
1033 strbuf_addstr(&munged_fmt, "%%");
strbuf.h
+8
@@ -371,6 +371,14 @@ size_t strbuf_expand_dict_cb(struct strbuf *sb,
371 const char *placeholder,
372 void *context);
373
374 +/**
375 + * If the string pointed to by `formatp` contains a percent sign ("%"),
376 + * advance it to point to the character following the next one and
377 + * return 1, otherwise return 0. Append the substring before that
378 + * percent sign to `sb`, or the whole string if there is none.
379 + */
380 +int strbuf_expand_step(struct strbuf *sb, const char **formatp);
381 +
382 /**
383 * Append the contents of one strbuf to another, quoting any
384 * percent signs ("%") into double-percents ("%%") in the