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

[PATCH v3 5/7] quote: add sq_append_quote_argv_pretty()

From
JGJeff Hostetler via GitGitGadget <gitgitgadget@gmail.com>
Date
Aug 9, 2019, 15:00 UTC
Message-ID
<2002108a30246824f8017da1dea344c1a96f7de2.1565362851.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.298.v3.git.gitgitgadget@gmail.com>
From: Jeff Hostetler <jeffhost@microsoft.com>

sq_quote_argv_pretty() builds a "pretty" string from the given argv. It inserts whitespace before each value, rather than just between them, so the resulting string always has a leading space. Lets give callers an option to not have the leading space or have to ltrim() it later.

Create sq_append_quote_argv_pretty() to convert an argv into a pretty, quoted if necessary, string with space delimiters and without a leading space.

Convert the existing sq_quote_argv_pretty() to use this new routine while preserving the leading space behavior.

Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com>
---
 quote.c | 18 +++++++++++++++++-
 quote.h |  1 +
 2 files changed, 18 insertions(+), 1 deletion(-)
diff --git a/quote.c b/quote.c
index 7f2aa6faa4..c8ba6b397a 100644
--- a/quote.c
+++ b/quote.c
@@ -84,12 +84,28 @@ void sq_quote_argv(struct strbuf *dst, const char **argv)
 	}
 }
 
+/*
+ * Legacy function to append each argv value, quoted as necessasry,
+ * with whitespace before each value.  This results in a leading
+ * space in the result.
+ */
 void sq_quote_argv_pretty(struct strbuf *dst, const char **argv)
+{
+	if (argv[0])
+		strbuf_addch(dst, ' ');
+	sq_append_quote_argv_pretty(dst, argv);
+}
+
+/*
+ * Append each argv value, quoted as necessary, with whitespace between them.
+ */
+void sq_append_quote_argv_pretty(struct strbuf *dst, const char **argv)
 {
 	int i;
 
 	for (i = 0; argv[i]; i++) {
-		strbuf_addch(dst, ' ');
+		if (i > 0)
+			strbuf_addch(dst, ' ');
 		sq_quote_buf_pretty(dst, argv[i]);
 	}
 }
diff --git a/quote.h b/quote.h
index fb08dc085c..ca8ee3144a 100644
--- a/quote.h
+++ b/quote.h
@@ -40,6 +40,7 @@ void sq_quotef(struct strbuf *, const char *fmt, ...);
  */
 void sq_quote_buf_pretty(struct strbuf *, const char *src);
 void sq_quote_argv_pretty(struct strbuf *, const char **argv);
+void sq_append_quote_argv_pretty(struct strbuf *dst, const char **argv);
 
 /* This unwraps what sq_quote() produces in place, but returns
  * NULL if the input does not look like what sq_quote would have
-- 
gitgitgadget
Previous: Jeff Hostetler via GitGitGadgetNext: Junio C Hamano
Message 30 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.