Skip to content

Commit 44ccb33

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3c3d0c4 commit 44ccb33

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

builtin/branch.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -366,17 +366,8 @@ static const char *quote_literal_for_format(const char *s)
366366
static struct strbuf buf = STRBUF_INIT;
367367

368368
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, "%%");
380371
return buf.buf;
381372
}
382373

strbuf.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -415,19 +415,24 @@ void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
415415
strbuf_setlen(sb, sb->len + len);
416416
}
417417

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+
418430
void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
419431
void *context)
420432
{
421-
for (;;) {
422-
const char *percent;
433+
while (strbuf_expand_step(sb, &format)) {
423434
size_t consumed;
424435

425-
percent = strchrnul(format, '%');
426-
strbuf_add(sb, format, percent - format);
427-
if (!*percent)
428-
break;
429-
format = percent + 1;
430-
431436
if (*format == '%') {
432437
strbuf_addch(sb, '%');
433438
format++;
@@ -1022,12 +1027,7 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm,
10221027
* we want for %z, but the computation for %s has to convert to number
10231028
* of seconds.
10241029
*/
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)) {
10311031
switch (*fmt) {
10321032
case '%':
10331033
strbuf_addstr(&munged_fmt, "%%");

strbuf.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,14 @@ size_t strbuf_expand_dict_cb(struct strbuf *sb,
371371
const char *placeholder,
372372
void *context);
373373

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+
374382
/**
375383
* Append the contents of one strbuf to another, quoting any
376384
* percent signs ("%") into double-percents ("%%") in the

0 commit comments

Comments
 (0)