replace strbuf_expand() with strbuf_expand_step()
Avoid the overhead of passing context to a callback function of strbuf_expand() by using strbuf_expand_step() in a loop instead. It requires explicit handling of %% and unrecognized placeholders, but is simpler, more direct and avoids void pointers. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
39dbd49b41
commit
6f1e2d5279
72
pretty.c
72
pretty.c
@ -1254,9 +1254,19 @@ static struct strbuf *expand_separator(struct strbuf *sb,
|
||||
const char *argval, size_t arglen)
|
||||
{
|
||||
char *fmt = xstrndup(argval, arglen);
|
||||
const char *format = fmt;
|
||||
|
||||
strbuf_reset(sb);
|
||||
strbuf_expand(sb, fmt, strbuf_expand_literal_cb, NULL);
|
||||
while (strbuf_expand_step(sb, &format)) {
|
||||
size_t len;
|
||||
|
||||
if (skip_prefix(format, "%", &format))
|
||||
strbuf_addch(sb, '%');
|
||||
else if ((len = strbuf_expand_literal_cb(sb, format, NULL)))
|
||||
format += len;
|
||||
else
|
||||
strbuf_addch(sb, '%');
|
||||
}
|
||||
free(fmt);
|
||||
return sb;
|
||||
}
|
||||
@ -1803,7 +1813,7 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
|
||||
|
||||
static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
|
||||
const char *placeholder,
|
||||
void *context)
|
||||
struct format_commit_context *context)
|
||||
{
|
||||
size_t consumed, orig_len;
|
||||
enum {
|
||||
@ -1842,7 +1852,7 @@ static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
|
||||
}
|
||||
|
||||
orig_len = sb->len;
|
||||
if (((struct format_commit_context *)context)->flush_type != no_flush)
|
||||
if ((context)->flush_type != no_flush)
|
||||
consumed = format_and_pad_commit(sb, placeholder, context);
|
||||
else
|
||||
consumed = format_commit_one(sb, placeholder, context);
|
||||
@ -1861,30 +1871,6 @@ static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
|
||||
return consumed + 1;
|
||||
}
|
||||
|
||||
static size_t userformat_want_item(struct strbuf *sb UNUSED,
|
||||
const char *placeholder,
|
||||
void *context)
|
||||
{
|
||||
struct userformat_want *w = context;
|
||||
|
||||
if (*placeholder == '+' || *placeholder == '-' || *placeholder == ' ')
|
||||
placeholder++;
|
||||
|
||||
switch (*placeholder) {
|
||||
case 'N':
|
||||
w->notes = 1;
|
||||
break;
|
||||
case 'S':
|
||||
w->source = 1;
|
||||
break;
|
||||
case 'd':
|
||||
case 'D':
|
||||
w->decorate = 1;
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void userformat_find_requirements(const char *fmt, struct userformat_want *w)
|
||||
{
|
||||
struct strbuf dummy = STRBUF_INIT;
|
||||
@ -1894,7 +1880,26 @@ void userformat_find_requirements(const char *fmt, struct userformat_want *w)
|
||||
return;
|
||||
fmt = user_format;
|
||||
}
|
||||
strbuf_expand(&dummy, fmt, userformat_want_item, w);
|
||||
while (strbuf_expand_step(&dummy, &fmt)) {
|
||||
if (skip_prefix(fmt, "%", &fmt))
|
||||
continue;
|
||||
|
||||
if (*fmt == '+' || *fmt == '-' || *fmt == ' ')
|
||||
fmt++;
|
||||
|
||||
switch (*fmt) {
|
||||
case 'N':
|
||||
w->notes = 1;
|
||||
break;
|
||||
case 'S':
|
||||
w->source = 1;
|
||||
break;
|
||||
case 'd':
|
||||
case 'D':
|
||||
w->decorate = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
strbuf_release(&dummy);
|
||||
}
|
||||
|
||||
@ -1912,7 +1917,16 @@ void repo_format_commit_message(struct repository *r,
|
||||
const char *output_enc = pretty_ctx->output_encoding;
|
||||
const char *utf8 = "UTF-8";
|
||||
|
||||
strbuf_expand(sb, format, format_commit_item, &context);
|
||||
while (strbuf_expand_step(sb, &format)) {
|
||||
size_t len;
|
||||
|
||||
if (skip_prefix(format, "%", &format))
|
||||
strbuf_addch(sb, '%');
|
||||
else if ((len = format_commit_item(sb, format, &context)))
|
||||
format += len;
|
||||
else
|
||||
strbuf_addch(sb, '%');
|
||||
}
|
||||
rewrap_message_tail(sb, &context, 0, 0, 0);
|
||||
|
||||
/*
|
||||
|
Reference in New Issue
Block a user