git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 5/7] quote: add sq_quote_argv_pretty_ltrim

From
René Scharfe <l.s.r@web.de>
Date
Aug 8, 2019, 22:49 UTC
Message-ID
<60c7b26c-e598-182b-421e-bdf5c0a8ac9f@web.de>
In-Reply-To
<7dbee619-2495-6426-b02b-49fd59e4f028@jeffhostetler.com>
Am 08.08.19 um 21:04 schrieb Jeff Hostetler:
Show 39 quoted lines
> On 8/8/2019 2:05 PM, Junio C Hamano wrote:
>> Having made the primary purpose of the helper clearer leads me to
>> wonder if "do not add SP before the first element, i.e. argv[0]", is
>> really what we want.  If we always clear the *dst strbuf before
>> starting to serialize argv[] into it, then the behaviour would make
>> sense, but we do not---we are "appending".
>>
>> As long as we are appending, would we be better off doing something
>> sillily magical like this instead, I have to wonder?
>>
>>     void sq_append_strings_quoted(struct strbuf *buf, const char **av)
>>     {
>>         int i;
>>
>>         for (i = 0; av[i]; i++) {
>>             if (buf->len)
>>                 strbuf_addch(buf, ' ');
>>             sq_quote_buf_pretty(buf, argv[0]);
>>         }
>>     }
>>
>> That is, "if we are appending to an existing string, have SP to
>> separate the first element from that existing string; treat the
>> remaining elements the same way (if the buffer is empty, there is no
>> point adding SP at the beginning)".
>
> I don't think that would do what we want.  We don't know what the
> caller's expectations are.  In my uses in commits 6/7 and 7/7 I
> already added the leading chars I wanted in the strbuf before calling
> sq_quote_argv_pretty_ltrim() and assumed the output would be a true
> append.  For example:
>
> +    strbuf_addf(&buf_payload, "alias:%s argv:[", alias);
> +    sq_quote_argv_pretty_ltrim(&buf_payload, argv);
> +    strbuf_addch(&buf_payload, ']');
>
> I like your suggestion of putting my new function in the _append_
> category.  I think I'll add the 3rd arg to this and then it will
> be completely specified and I can get rid of the _ltrim suffix.
Two observations:

If callers want to add something before a joined delimited list, they already can with a strbuf_add* call. No need to add that feature to a function that joins lists.

And repetitions of repetitions (loops) are boring.

Apologies in advance for any coffee stains on your monitor, but here's how I would start, probably followed by attempts to inline the functions that become trivial wrappers:

---
 quote.c  | 18 ++++--------------
 strbuf.c | 20 +++++++++++++-------
 strbuf.h |  8 ++++++++
 3 files changed, 25 insertions(+), 21 deletions(-)
diff --git a/quote.c b/quote.c
index 7f2aa6faa4..f422188852 100644
--- a/quote.c
+++ b/quote.c
@@ -74,24 +74,14 @@ void sq_quotef(struct strbuf *dst, const char *fmt, ...)

 void sq_quote_argv(struct strbuf *dst, const char **argv)
 {
-	int i;
-
-	/* Copy into destination buffer. */
-	strbuf_grow(dst, 255);
-	for (i = 0; argv[i]; ++i) {
-		strbuf_addch(dst, ' ');
-		sq_quote_buf(dst, argv[i]);
-	}
+	strbuf_addch(dst, ' ');
+	strbuf_map_join_argv(dst, argv, sq_quote_buf, " ");
 }

 void sq_quote_argv_pretty(struct strbuf *dst, const char **argv)
 {
-	int i;
-
-	for (i = 0; argv[i]; i++) {
-		strbuf_addch(dst, ' ');
-		sq_quote_buf_pretty(dst, argv[i]);
-	}
+	strbuf_addch(dst, ' ');
+	strbuf_map_join_argv(dst, argv, sq_quote_buf_pretty, " ");
 }

 static char *sq_dequote_step(char *arg, char **next)
diff --git a/strbuf.c b/strbuf.c
index d30f916858..d337853b53 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -304,17 +304,23 @@ void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)
 	strbuf_setlen(sb, sb->len + sb2->len);
 }

+void strbuf_map_join_argv(struct strbuf *sb, const char **argv,
+			  void (*fn)(struct strbuf *, const char *),
+			  const char *separator)
+{
+	while (*argv) {
+		fn(sb, *argv++);
+		if (*argv)
+			strbuf_addstr(sb, separator);
+	}
+}
+
 const char *strbuf_join_argv(struct strbuf *buf,
 			     int argc, const char **argv, char delim)
 {
-	if (!argc)
-		return buf->buf;
+	char separator[] = { delim, '\0' };

-	strbuf_addstr(buf, *argv);
-	while (--argc) {
-		strbuf_addch(buf, delim);
-		strbuf_addstr(buf, *(++argv));
-	}
+	strbuf_map_join_argv(buf, argv, strbuf_addstr, separator);

 	return buf->buf;
 }
diff --git a/strbuf.h b/strbuf.h
index f62278a0be..7adeff94a7 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -297,6 +297,14 @@ static inline void strbuf_addstr(struct strbuf *sb, const char *s)
  */
 void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2);

+/**
+ * Apply `fn` to `sb` and each element of the NULL-terminated array
+ * `argv`. Add `separator` between these invocations.
+ */
+void strbuf_map_join_argv(struct strbuf *sb, const char **argv,
+			  void (*fn)(struct strbuf *, const char *),
+			  const char *separator);
+
 /**
  * Join the arguments into a buffer. `delim` is put between every
  * two arguments.
--
2.22.0
Previous: Junio C HamanoNext: Jeff Hostetler
Message 17 of 33 in “trace2: clean up formatting in perf target format”
  1. 0/3 trace2: clean up formatting in perf target formatJeff Hostetler via GitGitGadget, Jul 31, 2019
  2. 1/3 trace2: cleanup column alignment in perf target formatJeff Hostetler via GitGitGadget, Jul 31, 2019
  3. 3/3 trace2: trim whitespace in region messages in perf target formatJeff Hostetler via GitGitGadget, Jul 31, 2019
  4. 2/3 trace2: trim whitespace in start message in perf target formatJeff Hostetler via GitGitGadget, Jul 31, 2019
  5. Junio C HamanoAug 1, 2019
  6. Jeff HostetlerAug 7, 2019
  7. Junio C HamanoAug 7, 2019
  8. Josh SteadmonAug 1, 2019
  9. 0/7 trace2: clean up formatting in perf target formatJeff Hostetler via GitGitGadget, Aug 8, 2019
  10. 1/7 trace2: cleanup column alignment in perf target formatJeff Hostetler via GitGitGadget, Aug 8, 2019
  11. 3/7 trace2: remove dead code in maybe_add_string_va()Jeff Hostetler via GitGitGadget, Aug 8, 2019
  12. 6/7 trace2: cleanup whitespace in normal formatJeff Hostetler via GitGitGadget, Aug 8, 2019
  13. 5/7 quote: add sq_quote_argv_pretty_ltrimJeff Hostetler via GitGitGadget, Aug 8, 2019
  14. Junio C HamanoAug 8, 2019
  15. Jeff HostetlerAug 8, 2019
  16. Junio C HamanoAug 8, 2019
  17. René ScharfeAug 8, 2019
  18. Jeff HostetlerAug 9, 2019
  19. René ScharfeAug 9, 2019
  20. 7/7 trace2: cleanup whitespace in perf formatJeff Hostetler via GitGitGadget, Aug 8, 2019
  21. 4/7 trace2: trim trailing whitespace in normal format error messageJeff Hostetler via GitGitGadget, Aug 8, 2019
  22. 2/7 trace2: trim whitespace in region messages in perf target formatJeff Hostetler via GitGitGadget, Aug 8, 2019
  23. 0/7 trace2: clean up formatting in perf target formatJeff Hostetler via GitGitGadget, Aug 9, 2019
  24. 1/7 trace2: cleanup column alignment in perf target formatJeff Hostetler via GitGitGadget, Aug 9, 2019
  25. 2/7 trace2: trim whitespace in region messages in perf target formatJeff Hostetler via GitGitGadget, Aug 9, 2019
  26. 4/7 trace2: trim trailing whitespace in normal format error messageJeff Hostetler via GitGitGadget, Aug 9, 2019
  27. 6/7 trace2: cleanup whitespace in normal formatJeff Hostetler via GitGitGadget, Aug 9, 2019
  28. 7/7 trace2: cleanup whitespace in perf formatJeff Hostetler via GitGitGadget, Aug 9, 2019
  29. 3/7 trace2: remove dead code in maybe_add_string_va()Jeff Hostetler via GitGitGadget, Aug 9, 2019
  30. 5/7 quote: add sq_append_quote_argv_pretty()Jeff Hostetler via GitGitGadget, Aug 9, 2019
  31. Junio C HamanoAug 9, 2019
  32. Jeff HostetlerAug 9, 2019
  33. Junio C HamanoAug 9, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.