diff options
author | ridiculousfish <corydoras@ridiculousfish.com> | 2015-08-19 11:35:24 -0700 |
---|---|---|
committer | ridiculousfish <corydoras@ridiculousfish.com> | 2015-08-19 11:35:24 -0700 |
commit | b59904632dd30a5101d459afca56fa3c48f48f12 (patch) | |
tree | 917af0d2dff5492b209bb024de7dc69d90619e5d /src/parse_util.cpp | |
parent | c1b9b27f860235e604cc4eed85a87fdc01b0b4f1 (diff) |
Rewrite parse_util_unescape_wildcards
Make it simpler, and use wcstring instead of wcsdup
Diffstat (limited to 'src/parse_util.cpp')
-rw-r--r-- | src/parse_util.cpp | 91 |
1 files changed, 29 insertions, 62 deletions
diff --git a/src/parse_util.cpp b/src/parse_util.cpp index 594416bf..87468e54 100644 --- a/src/parse_util.cpp +++ b/src/parse_util.cpp @@ -561,75 +561,42 @@ void parse_util_token_extent(const wchar_t *buff, } -wchar_t *parse_util_unescape_wildcards(const wchar_t *str) +wcstring parse_util_unescape_wildcards(const wcstring &str) { - wchar_t *in, *out; - wchar_t *unescaped; - - CHECK(str, 0); - - unescaped = wcsdup(str); - - if (!unescaped) - { - DIE_MEM(); - } - - for (in=out=unescaped; *in; in++) + wcstring result; + result.reserve(str.size()); + + const wchar_t * const cs = str.c_str(); + for (size_t i=0; cs[i] != L'\0'; i++) { - switch (*in) + if (cs[i] == L'*') { - case L'\\': - { - switch (*(in + 1)) - { - case L'*': - case L'?': - { - in++; - *(out++)=*in; - break; - } - case L'\\': - { - in++; - *(out++)=L'\\'; - *(out++)=L'\\'; - break; - } - default: - { - *(out++)=*in; - break; - } - } - break; - } - - case L'*': - { - *(out++)=ANY_STRING; - break; - } - - case L'?': - { - *(out++)=ANY_CHAR; - break; - } - - default: - { - *(out++)=*in; - break; - } + result.push_back(ANY_STRING); + } + else if (cs[i] == L'?') + { + result.push_back(ANY_CHAR); + } + else if (cs[i] == L'\\' && (cs[i+1] == L'*' || cs[i+1] == L'?')) + { + result.push_back(cs[i+1]); + i += 1; + } + else if (cs[i] == L'\\' && cs[i+1] == L'\\') + { + // Not a wildcard, but ensure the next iteration + // doesn't see this escaped backslash + result.append(L"\\\\"); + i += 1; + } + else + { + result.push_back(cs[i]); } } - *out = *in; - return unescaped; + return result; } - /** Find the outermost quoting style of current token. Returns 0 if token is not quoted. |