From: "René Scharfe" <l.s.r@web.de>
To: Git List <git@vger.kernel.org>
Subject: [PATCH 2/5] strbuf: factor out strbuf_expand_step()
Date: Sat, 17 Jun 2023 22:41:44 +0200 [thread overview]
Message-ID: <caf8e100-1308-cb4e-d61a-4e15ee3f47f7@web.de> (raw)
In-Reply-To: <767baa64-20a6-daf2-d34b-d81f72363749@web.de>
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>
---
builtin/branch.c | 13 ++-----------
strbuf.c | 28 ++++++++++++++--------------
strbuf.h | 8 ++++++++
3 files changed, 24 insertions(+), 25 deletions(-)
diff --git a/builtin/branch.c b/builtin/branch.c
index e6c2655af6..7c20e049a2 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -366,17 +366,8 @@ static const char *quote_literal_for_format(const char *s)
static struct strbuf buf = STRBUF_INIT;
strbuf_reset(&buf);
- while (*s) {
- const char *ep = strchrnul(s, '%');
- if (s < ep)
- strbuf_add(&buf, s, ep - s);
- if (*ep == '%') {
- strbuf_addstr(&buf, "%%");
- s = ep + 1;
- } else {
- s = ep;
- }
- }
+ while (strbuf_expand_step(&buf, &s))
+ strbuf_addstr(&buf, "%%");
return buf.buf;
}
diff --git a/strbuf.c b/strbuf.c
index 08eec8f1d8..a90b597da1 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -415,19 +415,24 @@ void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
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 *context)
{
- for (;;) {
- const char *percent;
+ while (strbuf_expand_step(sb, &format)) {
size_t consumed;
- percent = strchrnul(format, '%');
- strbuf_add(sb, format, percent - format);
- if (!*percent)
- break;
- format = percent + 1;
-
if (*format == '%') {
strbuf_addch(sb, '%');
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
* 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, "%%");
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
--
2.41.0
next prev parent reply other threads:[~2023-06-17 20:42 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-17 20:37 [PATCH 0/5] replace strbuf_expand() René Scharfe
2023-06-17 20:40 ` [PATCH 1/5] pretty: factor out expand_separator() René Scharfe
2023-06-17 20:41 ` René Scharfe [this message]
2023-06-19 16:10 ` [PATCH 2/5] strbuf: factor out strbuf_expand_step() Taylor Blau
2023-06-20 16:25 ` René Scharfe
2023-06-21 12:26 ` Taylor Blau
2023-06-27 8:26 ` Jeff King
2023-06-27 16:21 ` René Scharfe
2023-06-17 20:42 ` [PATCH 3/5] replace strbuf_expand_dict_cb() with strbuf_expand_step() René Scharfe
2023-06-27 8:29 ` Jeff King
2023-06-27 8:35 ` Jeff King
2023-06-27 16:24 ` René Scharfe
2023-06-17 20:43 ` [PATCH 4/5] replace strbuf_expand() " René Scharfe
2023-06-27 8:54 ` Jeff King
2023-06-27 16:31 ` René Scharfe
2023-06-27 20:19 ` Jeff King
2023-06-17 20:44 ` [PATCH 5/5] strbuf: simplify strbuf_expand_literal_cb() René Scharfe
2023-06-27 8:57 ` Jeff King
2023-06-27 16:32 ` René Scharfe
2023-06-27 20:21 ` Jeff King
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=caf8e100-1308-cb4e-d61a-4e15ee3f47f7@web.de \
--to=l.s.r@web.de \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).