diff options
author | Junio C Hamano <gitster@pobox.com> | 2023-07-06 21:54:45 +0300 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2023-07-06 21:54:45 +0300 |
commit | da269af9207e38e820daad8aa993caa7d2fad76c (patch) | |
tree | 28567788ed00e6c037cb93684e95a473e3d2f412 /strbuf.c | |
parent | a646b86cd10282de2ceb64ef33b5412e4fb2a54c (diff) | |
parent | 4416b86c6b34dad64b556bb1eb6711d5e6595a48 (diff) |
Merge branch 'rs/strbuf-expand-step'
Code clean-up around strbuf_expand() API.
* rs/strbuf-expand-step:
strbuf: simplify strbuf_expand_literal_cb()
replace strbuf_expand() with strbuf_expand_step()
replace strbuf_expand_dict_cb() with strbuf_expand_step()
strbuf: factor out strbuf_expand_step()
pretty: factor out expand_separator()
Diffstat (limited to 'strbuf.c')
-rw-r--r-- | strbuf.c | 58 |
1 files changed, 10 insertions, 48 deletions
@@ -416,36 +416,19 @@ void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap) strbuf_setlen(sb, sb->len + len); } -void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn, - void *context) +int strbuf_expand_step(struct strbuf *sb, const char **formatp) { - for (;;) { - const char *percent; - size_t consumed; - - percent = strchrnul(format, '%'); - strbuf_add(sb, format, percent - format); - if (!*percent) - break; - format = percent + 1; - - if (*format == '%') { - strbuf_addch(sb, '%'); - format++; - continue; - } + const char *format = *formatp; + const char *percent = strchrnul(format, '%'); - consumed = fn(sb, format, context); - if (consumed) - format += consumed; - else - strbuf_addch(sb, '%'); - } + strbuf_add(sb, format, percent - format); + if (!*percent) + return 0; + *formatp = percent + 1; + return 1; } -size_t strbuf_expand_literal_cb(struct strbuf *sb, - const char *placeholder, - void *context UNUSED) +size_t strbuf_expand_literal(struct strbuf *sb, const char *placeholder) { int ch; @@ -464,22 +447,6 @@ size_t strbuf_expand_literal_cb(struct strbuf *sb, return 0; } -size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder, - void *context) -{ - struct strbuf_expand_dict_entry *e = context; - size_t len; - - for (; e->placeholder && (len = strlen(e->placeholder)); e++) { - if (!strncmp(placeholder, e->placeholder, len)) { - if (e->value) - strbuf_addstr(sb, e->value); - return len; - } - } - return 0; -} - void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src) { size_t i, len = src->len; @@ -1028,12 +995,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, "%%"); |