Skip to content

Commit 13c4495

Browse files
peffgitster
authored andcommitted
quote: make sq_dequote_step() a public function
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ce81b1d commit 13c4495

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

quote.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void sq_append_quote_argv_pretty(struct strbuf *dst, const char **argv)
116116
}
117117
}
118118

119-
static char *sq_dequote_step(char *arg, char **next)
119+
char *sq_dequote_step(char *arg, char **next)
120120
{
121121
char *dst = arg;
122122
char *src = arg;
@@ -153,11 +153,8 @@ static char *sq_dequote_step(char *arg, char **next)
153153
}
154154
/* Fallthrough */
155155
default:
156-
if (!next || !isspace(*src))
156+
if (!next)
157157
return NULL;
158-
do {
159-
c = *++src;
160-
} while (isspace(c));
161158
*dst = 0;
162159
*next = src;
163160
return arg;
@@ -182,6 +179,14 @@ static int sq_dequote_to_argv_internal(char *arg,
182179
char *dequoted = sq_dequote_step(next, &next);
183180
if (!dequoted)
184181
return -1;
182+
if (next) {
183+
char c;
184+
if (!isspace(*next))
185+
return -1;
186+
do {
187+
c = *++next;
188+
} while (isspace(c));
189+
}
185190
if (argv) {
186191
ALLOC_GROW(*argv, *nr + 1, *alloc);
187192
(*argv)[(*nr)++] = dequoted;

quote.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,26 @@ void sq_quote_buf_pretty(struct strbuf *, const char *src);
4242
void sq_quote_argv_pretty(struct strbuf *, const char **argv);
4343
void sq_append_quote_argv_pretty(struct strbuf *dst, const char **argv);
4444

45-
/* This unwraps what sq_quote() produces in place, but returns
45+
/*
46+
* This unwraps what sq_quote() produces in place, but returns
4647
* NULL if the input does not look like what sq_quote would have
47-
* produced.
48+
* produced (the full string must be a single quoted item).
4849
*/
4950
char *sq_dequote(char *);
5051

52+
/*
53+
* Like sq_dequote(), but dequote a single item, and leave "next" pointing to
54+
* the next character. E.g., in the string:
55+
*
56+
* 'one' 'two' 'three'
57+
*
58+
* after the first call, the return value would be the unquoted string "one",
59+
* with "next" pointing to the space between "one" and "two"). The caller is
60+
* responsible for advancing the pointer to the start of the next item before
61+
* calling sq_dequote_step() again.
62+
*/
63+
char *sq_dequote_step(char *src, char **next);
64+
5165
/*
5266
* Same as the above, but can be used to unwrap many arguments in the
5367
* same string separated by space. Like sq_quote, it works in place,

0 commit comments

Comments
 (0)