]> git.ipfire.org Git - thirdparty/git.git/commitdiff
quote: make sq_dequote_step() a public function
authorJeff King <peff@peff.net>
Tue, 12 Jan 2021 12:26:49 +0000 (13:26 +0100)
committerJunio C Hamano <gitster@pobox.com>
Tue, 12 Jan 2021 20:03:18 +0000 (12:03 -0800)
We provide a function for dequoting an entire string, as well as one for
handling a space-separated list of quoted strings. But there's no way
for a caller to parse a string like 'foo'='bar', even though it is easy
to generate one using sq_quote_buf() or similar.

Let's make the single-step function available to callers outside of
quote.c. Note that we do need to adjust its implementation slightly: it
insists on seeing whitespace between items, and we'd like to be more
flexible than that. Since it only has a single caller, we can move that
check (and slurping up any extra whitespace) into that caller.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
quote.c
quote.h

diff --git a/quote.c b/quote.c
index 69f4ca45da2040fd64bf060d574a474359d9b98c..8a3a5e39eb12adff643d8bd2d90d1e625203a2ce 100644 (file)
--- a/quote.c
+++ b/quote.c
@@ -116,7 +116,7 @@ void sq_append_quote_argv_pretty(struct strbuf *dst, const char **argv)
        }
 }
 
-static char *sq_dequote_step(char *arg, char **next)
+char *sq_dequote_step(char *arg, char **next)
 {
        char *dst = arg;
        char *src = arg;
@@ -153,11 +153,8 @@ static char *sq_dequote_step(char *arg, char **next)
                        }
                /* Fallthrough */
                default:
-                       if (!next || !isspace(*src))
+                       if (!next)
                                return NULL;
-                       do {
-                               c = *++src;
-                       } while (isspace(c));
                        *dst = 0;
                        *next = src;
                        return arg;
@@ -182,6 +179,14 @@ static int sq_dequote_to_argv_internal(char *arg,
                char *dequoted = sq_dequote_step(next, &next);
                if (!dequoted)
                        return -1;
+               if (next) {
+                       char c;
+                       if (!isspace(*next))
+                               return -1;
+                       do {
+                               c = *++next;
+                       } while (isspace(c));
+               }
                if (argv) {
                        ALLOC_GROW(*argv, *nr + 1, *alloc);
                        (*argv)[(*nr)++] = dequoted;
diff --git a/quote.h b/quote.h
index 4b72a583cfdadd05d2892e1e4c712cd00a52bd31..768cc6338e27d73c8a10cf5eae76ad5626c73e3e 100644 (file)
--- a/quote.h
+++ b/quote.h
@@ -42,12 +42,26 @@ 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
+/*
+ * This unwraps what sq_quote() produces in place, but returns
  * NULL if the input does not look like what sq_quote would have
- * produced.
+ * produced (the full string must be a single quoted item).
  */
 char *sq_dequote(char *);
 
+/*
+ * Like sq_dequote(), but dequote a single item, and leave "next" pointing to
+ * the next character. E.g., in the string:
+ *
+ *   'one' 'two' 'three'
+ *
+ * after the first call, the return value would be the unquoted string "one",
+ * with "next" pointing to the space between "one" and "two"). The caller is
+ * responsible for advancing the pointer to the start of the next item before
+ * calling sq_dequote_step() again.
+ */
+char *sq_dequote_step(char *src, char **next);
+
 /*
  * Same as the above, but can be used to unwrap many arguments in the
  * same string separated by space. Like sq_quote, it works in place,