diff options
author | yorah <yoram.harmelin@gmail.com> | 2012-07-12 18:31:59 +0400 |
---|---|---|
committer | yorah <yoram.harmelin@gmail.com> | 2012-07-24 16:03:07 +0400 |
commit | 02a0d651d79b2108dd6b894b9a43f7682270ac51 (patch) | |
tree | e90cbe5dccf69913f226799860a541ab31df29c3 /src | |
parent | 944d250f964698b33d9fa09e2e6af74b1dd84de2 (diff) |
Add git_buf_unescape and git__unescape to unescape all characters in a string (in-place)
Diffstat (limited to 'src')
-rw-r--r-- | src/attr_file.c | 12 | ||||
-rw-r--r-- | src/buffer.c | 4 | ||||
-rw-r--r-- | src/buffer.h | 3 | ||||
-rw-r--r-- | src/util.c | 18 | ||||
-rw-r--r-- | src/util.h | 9 |
5 files changed, 35 insertions, 11 deletions
diff --git a/src/attr_file.c b/src/attr_file.c index 0dad09727..837c42d8e 100644 --- a/src/attr_file.c +++ b/src/attr_file.c @@ -426,17 +426,7 @@ int git_attr_fnmatch__parse( return -1; } else { /* strip '\' that might have be used for internal whitespace */ - char *to = spec->pattern; - for (scan = spec->pattern; *scan; to++, scan++) { - if (*scan == '\\') - scan++; /* skip '\' but include next char */ - if (to != scan) - *to = *scan; - } - if (to != scan) { - *to = '\0'; - spec->length = (to - spec->pattern); - } + spec->length = git__unescape(spec->pattern); } return 0; diff --git a/src/buffer.c b/src/buffer.c index 5d54ee1a5..b57998e1b 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -496,3 +496,7 @@ bool git_buf_is_binary(const git_buf *buf) return ((printable >> 7) < nonprintable); } +void git_buf_unescape(git_buf *buf) +{ + buf->size = git__unescape(buf->ptr); +} diff --git a/src/buffer.h b/src/buffer.h index 75f3b0e4f..17922e408 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -151,4 +151,7 @@ int git_buf_common_prefix(git_buf *buf, const git_strarray *strings); /* Check if buffer looks like it contains binary data */ bool git_buf_is_binary(const git_buf *buf); +/* Unescape all characters in a buffer */ +void git_buf_unescape(git_buf *buf); + #endif diff --git a/src/util.c b/src/util.c index 3093cd767..90bb3d02a 100644 --- a/src/util.c +++ b/src/util.c @@ -435,3 +435,21 @@ int git__parse_bool(int *out, const char *value) return -1; } + +size_t git__unescape(char *str) +{ + char *scan, *pos = str; + + for (scan = str; *scan; pos++, scan++) { + if (*scan == '\\' && *(scan + 1) != '\0') + scan++; /* skip '\' but include next char */ + if (pos != scan) + *pos = *scan; + } + + if (pos != scan) { + *pos = '\0'; + } + + return (pos - str); +} diff --git a/src/util.h b/src/util.h index a84dcab1e..905fc927f 100644 --- a/src/util.h +++ b/src/util.h @@ -238,4 +238,13 @@ extern int git__parse_bool(int *out, const char *value); */ int git__date_parse(git_time_t *out, const char *date); +/* + * Unescapes a string in-place. + * + * Edge cases behavior: + * - "jackie\" -> "jacky\" + * - "chan\\" -> "chan\" + */ +extern size_t git__unescape(char *str); + #endif /* INCLUDE_util_h__ */ |