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>maint
parent
3c3d0c4242
commit
44ccb337f1
|
@ -366,17 +366,8 @@ static const char *quote_literal_for_format(const char *s)
|
||||||
static struct strbuf buf = STRBUF_INIT;
|
static struct strbuf buf = STRBUF_INIT;
|
||||||
|
|
||||||
strbuf_reset(&buf);
|
strbuf_reset(&buf);
|
||||||
while (*s) {
|
while (strbuf_expand_step(&buf, &s))
|
||||||
const char *ep = strchrnul(s, '%');
|
strbuf_addstr(&buf, "%%");
|
||||||
if (s < ep)
|
|
||||||
strbuf_add(&buf, s, ep - s);
|
|
||||||
if (*ep == '%') {
|
|
||||||
strbuf_addstr(&buf, "%%");
|
|
||||||
s = ep + 1;
|
|
||||||
} else {
|
|
||||||
s = ep;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return buf.buf;
|
return buf.buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
28
strbuf.c
28
strbuf.c
|
@ -415,19 +415,24 @@ void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
|
||||||
strbuf_setlen(sb, sb->len + len);
|
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 strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
|
||||||
void *context)
|
void *context)
|
||||||
{
|
{
|
||||||
for (;;) {
|
while (strbuf_expand_step(sb, &format)) {
|
||||||
const char *percent;
|
|
||||||
size_t consumed;
|
size_t consumed;
|
||||||
|
|
||||||
percent = strchrnul(format, '%');
|
|
||||||
strbuf_add(sb, format, percent - format);
|
|
||||||
if (!*percent)
|
|
||||||
break;
|
|
||||||
format = percent + 1;
|
|
||||||
|
|
||||||
if (*format == '%') {
|
if (*format == '%') {
|
||||||
strbuf_addch(sb, '%');
|
strbuf_addch(sb, '%');
|
||||||
format++;
|
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
|
* we want for %z, but the computation for %s has to convert to number
|
||||||
* of seconds.
|
* of seconds.
|
||||||
*/
|
*/
|
||||||
for (;;) {
|
while (strbuf_expand_step(&munged_fmt, &fmt)) {
|
||||||
const char *percent = strchrnul(fmt, '%');
|
|
||||||
strbuf_add(&munged_fmt, fmt, percent - fmt);
|
|
||||||
if (!*percent)
|
|
||||||
break;
|
|
||||||
fmt = percent + 1;
|
|
||||||
switch (*fmt) {
|
switch (*fmt) {
|
||||||
case '%':
|
case '%':
|
||||||
strbuf_addstr(&munged_fmt, "%%");
|
strbuf_addstr(&munged_fmt, "%%");
|
||||||
|
|
8
strbuf.h
8
strbuf.h
|
@ -371,6 +371,14 @@ size_t strbuf_expand_dict_cb(struct strbuf *sb,
|
||||||
const char *placeholder,
|
const char *placeholder,
|
||||||
void *context);
|
void *context);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If the string pointed to by `formatp` contains a percent sign ("%"),
|
||||||
|
* advance it to point to the character following the next one and
|
||||||
|
* return 1, otherwise return 0. Append the substring before that
|
||||||
|
* percent sign to `sb`, or the whole string if there is none.
|
||||||
|
*/
|
||||||
|
int strbuf_expand_step(struct strbuf *sb, const char **formatp);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Append the contents of one strbuf to another, quoting any
|
* Append the contents of one strbuf to another, quoting any
|
||||||
* percent signs ("%") into double-percents ("%%") in the
|
* percent signs ("%") into double-percents ("%%") in the
|
||||||
|
|
Loading…
Reference in New Issue