summaryrefslogtreecommitdiff
path: root/quote.c
diff options
context:
space:
mode:
authorPierre Habouzit <madcoder@debian.org>2007-09-19 22:42:14 (GMT)
committerJunio C Hamano <gitster@pobox.com>2007-09-21 06:32:18 (GMT)
commit7fb1011e610a28518959b1d2d48cea17ecc32048 (patch)
tree2f07cfdbd6e14dd3e98e1325c7f30c325ea4c785 /quote.c
parentc76689df6c64a1e987bd779bd71a2042b5131fb9 (diff)
downloadgit-7fb1011e610a28518959b1d2d48cea17ecc32048.zip
git-7fb1011e610a28518959b1d2d48cea17ecc32048.tar.gz
git-7fb1011e610a28518959b1d2d48cea17ecc32048.tar.bz2
Rework unquote_c_style to work on a strbuf.
If the gain is not obvious in the diffstat, the resulting code is more readable, _and_ in checkout-index/update-index we now reuse the same buffer to unquote strings instead of always freeing/mallocing. This also is more coherent with the next patch that reworks quoting functions. The quoting function is also made more efficient scanning for backslashes and treating portions of strings without a backslash at once. Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Diffstat (limited to 'quote.c')
-rw-r--r--quote.c104
1 files changed, 51 insertions, 53 deletions
diff --git a/quote.c b/quote.c
index d88bf75..7771c9c 100644
--- a/quote.c
+++ b/quote.c
@@ -239,73 +239,71 @@ int quote_c_style(const char *name, char *outbuf, FILE *outfp, int no_dq)
/*
* C-style name unquoting.
*
- * Quoted should point at the opening double quote. Returns
- * an allocated memory that holds unquoted name, which the caller
- * should free when done. Updates endp pointer to point at
- * one past the ending double quote if given.
+ * Quoted should point at the opening double quote.
+ * + Returns 0 if it was able to unquote the string properly, and appends the
+ * result in the strbuf `sb'.
+ * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
+ * that this function will allocate memory in the strbuf, so calling
+ * strbuf_release is mandatory whichever result unquote_c_style returns.
+ *
+ * Updates endp pointer to point at one past the ending double quote if given.
*/
-
-char *unquote_c_style(const char *quoted, const char **endp)
+int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
{
- const char *sp;
- char *name = NULL, *outp = NULL;
- int count = 0, ch, ac;
-
-#undef EMIT
-#define EMIT(c) (outp ? (*outp++ = (c)) : (count++))
+ size_t oldlen = sb->len, len;
+ int ch, ac;
if (*quoted++ != '"')
- return NULL;
+ return -1;
+
+ for (;;) {
+ len = strcspn(quoted, "\"\\");
+ strbuf_add(sb, quoted, len);
+ quoted += len;
- while (1) {
- /* first pass counts and allocates, second pass fills */
- for (sp = quoted; (ch = *sp++) != '"'; ) {
- if (ch == '\\') {
- switch (ch = *sp++) {
- case 'a': ch = '\a'; break;
- case 'b': ch = '\b'; break;
- case 'f': ch = '\f'; break;
- case 'n': ch = '\n'; break;
- case 'r': ch = '\r'; break;
- case 't': ch = '\t'; break;
- case 'v': ch = '\v'; break;
-
- case '\\': case '"':
- break; /* verbatim */
-
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- /* octal */
+ switch (*quoted++) {
+ case '"':
+ if (endp)
+ *endp = quoted + 1;
+ return 0;
+ case '\\':
+ break;
+ default:
+ goto error;
+ }
+
+ switch ((ch = *quoted++)) {
+ case 'a': ch = '\a'; break;
+ case 'b': ch = '\b'; break;
+ case 'f': ch = '\f'; break;
+ case 'n': ch = '\n'; break;
+ case 'r': ch = '\r'; break;
+ case 't': ch = '\t'; break;
+ case 'v': ch = '\v'; break;
+
+ case '\\': case '"':
+ break; /* verbatim */
+
+ /* octal values with first digit over 4 overflow */
+ case '0': case '1': case '2': case '3':
ac = ((ch - '0') << 6);
- if ((ch = *sp++) < '0' || '7' < ch)
- return NULL;
+ if ((ch = *quoted++) < '0' || '7' < ch)
+ goto error;
ac |= ((ch - '0') << 3);
- if ((ch = *sp++) < '0' || '7' < ch)
- return NULL;
+ if ((ch = *quoted++) < '0' || '7' < ch)
+ goto error;
ac |= (ch - '0');
ch = ac;
break;
default:
- return NULL; /* malformed */
- }
+ goto error;
}
- EMIT(ch);
+ strbuf_addch(sb, ch);
}
- if (name) {
- *outp = 0;
- if (endp)
- *endp = sp;
- return name;
- }
- outp = name = xmalloc(count + 1);
- }
+ error:
+ strbuf_setlen(sb, oldlen);
+ return -1;
}
void write_name_quoted(const char *prefix, int prefix_len,