diff options
author | René Scharfe <l.s.r@web.de> | 2023-06-17 22:41:44 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2023-06-18 21:55:30 +0200 |
commit | 44ccb337f10a08bb265b911f86deaf5f3347d967 (patch) | |
tree | 07762d77de4581021436c997e0f8099219f043b0 /strbuf.c | |
parent | pretty: factor out expand_separator() (diff) | |
download | git-44ccb337f10a08bb265b911f86deaf5f3347d967.tar.xz git-44ccb337f10a08bb265b911f86deaf5f3347d967.zip |
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>
Diffstat (limited to 'strbuf.c')
-rw-r--r-- | strbuf.c | 28 |
1 files changed, 14 insertions, 14 deletions
@@ -415,19 +415,24 @@ void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap) strbuf_setlen(sb, sb->len + len); } +int strbuf_expand_step(struct strbuf *sb, const char **formatp) +{ + const char *format = *formatp; + const char *percent = strchrnul(format, '%'); + + strbuf_add(sb, format, percent - format); + if (!*percent) + return 0; + *formatp = percent + 1; + return 1; +} + void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn, void *context) { - for (;;) { - const char *percent; + while (strbuf_expand_step(sb, &format)) { size_t consumed; - percent = strchrnul(format, '%'); - strbuf_add(sb, format, percent - format); - if (!*percent) - break; - format = percent + 1; - if (*format == '%') { strbuf_addch(sb, '%'); format++; @@ -1022,12 +1027,7 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm, * we want for %z, but the computation for %s has to convert to number * of seconds. */ - for (;;) { - const char *percent = strchrnul(fmt, '%'); - strbuf_add(&munged_fmt, fmt, percent - fmt); - if (!*percent) - break; - fmt = percent + 1; + while (strbuf_expand_step(&munged_fmt, &fmt)) { switch (*fmt) { case '%': strbuf_addstr(&munged_fmt, "%%"); |