From 44ccb337f10a08bb265b911f86deaf5f3347d967 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 17 Jun 2023 22:41:44 +0200 Subject: strbuf: factor out strbuf_expand_step() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Junio C Hamano --- strbuf.h | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'strbuf.h') diff --git a/strbuf.h b/strbuf.h index 3dfeadb44c..a189f12b84 100644 --- a/strbuf.h +++ b/strbuf.h @@ -371,6 +371,14 @@ size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder, 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 * percent signs ("%") into double-percents ("%%") in the -- cgit v1.2.3 From 39dbd49b4138b6cdc9fb73e317d4e9f06df0c5c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= 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 --- strbuf.h | 14 -------------- 1 file changed, 14 deletions(-) (limited to 'strbuf.h') diff --git a/strbuf.h b/strbuf.h index a189f12b84..e293117f07 100644 --- a/strbuf.h +++ b/strbuf.h @@ -357,20 +357,6 @@ size_t strbuf_expand_literal_cb(struct strbuf *sb, const char *placeholder, void *context); -/** - * Used as callback for `strbuf_expand()`, expects an array of - * struct strbuf_expand_dict_entry as context, i.e. pairs of - * placeholder and replacement string. The array needs to be - * terminated by an entry with placeholder set to NULL. - */ -struct strbuf_expand_dict_entry { - const char *placeholder; - const char *value; -}; -size_t strbuf_expand_dict_cb(struct strbuf *sb, - const char *placeholder, - 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 -- cgit v1.2.3 From 6f1e2d52797161c7effe4d1388765de3dbd80bbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 17 Jun 2023 22:43:17 +0200 Subject: replace strbuf_expand() with strbuf_expand_step() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Junio C Hamano --- strbuf.h | 37 +++---------------------------------- 1 file changed, 3 insertions(+), 34 deletions(-) (limited to 'strbuf.h') diff --git a/strbuf.h b/strbuf.h index e293117f07..95e50e243e 100644 --- a/strbuf.h +++ b/strbuf.h @@ -318,40 +318,9 @@ const char *strbuf_join_argv(struct strbuf *buf, int argc, const char **argv, char delim); /** - * This function can be used to expand a format string containing - * placeholders. To that end, it parses the string and calls the specified - * function for every percent sign found. - * - * The callback function is given a pointer to the character after the `%` - * and a pointer to the struct strbuf. It is expected to add the expanded - * version of the placeholder to the strbuf, e.g. to add a newline - * character if the letter `n` appears after a `%`. The function returns - * the length of the placeholder recognized and `strbuf_expand()` skips - * over it. - * - * The format `%%` is automatically expanded to a single `%` as a quoting - * mechanism; callers do not need to handle the `%` placeholder themselves, - * and the callback function will not be invoked for this placeholder. - * - * All other characters (non-percent and not skipped ones) are copied - * verbatim to the strbuf. If the callback returned zero, meaning that the - * placeholder is unknown, then the percent sign is copied, too. - * - * In order to facilitate caching and to make it possible to give - * parameters to the callback, `strbuf_expand()` passes a context - * pointer with any kind of data. - */ -typedef size_t (*expand_fn_t) (struct strbuf *sb, - const char *placeholder, - void *context); -void strbuf_expand(struct strbuf *sb, - const char *format, - expand_fn_t fn, - void *context); - -/** - * Used as callback for `strbuf_expand` to only expand literals - * (i.e. %n and %xNN). The context argument is ignored. + * Used with `strbuf_expand_step` to expand the literals %n and %x + * followed by two hexadecimal digits. Returns the number of recognized + * characters. The context argument is ignored. */ size_t strbuf_expand_literal_cb(struct strbuf *sb, const char *placeholder, -- cgit v1.2.3 From 4416b86c6b34dad64b556bb1eb6711d5e6595a48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 17 Jun 2023 22:44:00 +0200 Subject: strbuf: simplify strbuf_expand_literal_cb() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now that strbuf_expand_literal_cb() is no longer used as a callback, drop its "_cb" name suffix and unused context parameter. Signed-off-by: René Scharfe Signed-off-by: Junio C Hamano --- strbuf.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'strbuf.h') diff --git a/strbuf.h b/strbuf.h index 95e50e243e..b1eab015f0 100644 --- a/strbuf.h +++ b/strbuf.h @@ -320,11 +320,9 @@ const char *strbuf_join_argv(struct strbuf *buf, int argc, /** * Used with `strbuf_expand_step` to expand the literals %n and %x * followed by two hexadecimal digits. Returns the number of recognized - * characters. The context argument is ignored. + * characters. */ -size_t strbuf_expand_literal_cb(struct strbuf *sb, - const char *placeholder, - void *context); +size_t strbuf_expand_literal(struct strbuf *sb, const char *placeholder); /** * If the string pointed to by `formatp` contains a percent sign ("%"), -- cgit v1.2.3