From 39dbd49b4138b6cdc9fb73e317d4e9f06df0c5c5 Mon Sep 17 00:00:00 2001 From: René Scharfe Date: Sat, 17 Jun 2023 22:42:26 +0200 Subject: replace strbuf_expand_dict_cb() with strbuf_expand_step() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Avoid the overhead of setting up a dictionary and passing it via strbuf_expand() to strbuf_expand_dict_cb() by using strbuf_expand_step() in a loop instead. It requires explicit handling of %% and unrecognized placeholders, but is more direct and simpler overall, and expands only on demand. Signed-off-by: René Scharfe Signed-off-by: Junio C Hamano --- convert.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'convert.c') diff --git a/convert.c b/convert.c index 9ee79fe469..455d05cf6b 100644 --- a/convert.c +++ b/convert.c @@ -633,23 +633,21 @@ static int filter_buffer_or_fd(int in UNUSED, int out, void *data) */ struct child_process child_process = CHILD_PROCESS_INIT; struct filter_params *params = (struct filter_params *)data; + const char *format = params->cmd; int write_err, status; /* apply % substitution to cmd */ struct strbuf cmd = STRBUF_INIT; - struct strbuf path = STRBUF_INIT; - struct strbuf_expand_dict_entry dict[] = { - { "f", NULL, }, - { NULL, NULL, }, - }; - - /* quote the path to preserve spaces, etc. */ - sq_quote_buf(&path, params->path); - dict[0].value = path.buf; - /* expand all %f with the quoted path */ - strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict); - strbuf_release(&path); + /* expand all %f with the quoted path; quote to preserve space, etc. */ + while (strbuf_expand_step(&cmd, &format)) { + if (skip_prefix(format, "%", &format)) + strbuf_addch(&cmd, '%'); + else if (skip_prefix(format, "f", &format)) + sq_quote_buf(&cmd, params->path); + else + strbuf_addch(&cmd, '%'); + } strvec_push(&child_process.args, cmd.buf); child_process.use_shell = 1; -- cgit v1.2.3