COMMON: Allow '\#' to match '#' in matchString

matchString patterns couldn't be used to find files with the # character
as it was only treated as a digit wildcard. SCI expected that to work as
it looks for files that start with the # character.
This commit is contained in:
sluicebox 2019-02-21 16:36:01 -08:00 committed by Filippos Karapetis
parent a3873e7e4b
commit 85333d8050
3 changed files with 25 additions and 5 deletions

View File

@ -884,6 +884,7 @@ bool matchString(const char *str, const char *pat, bool ignoreCase, bool pathMod
const char *p = nullptr;
const char *q = nullptr;
bool escaped = false;
for (;;) {
if (pathMode && *str == '/') {
@ -893,6 +894,7 @@ bool matchString(const char *str, const char *pat, bool ignoreCase, bool pathMod
return false;
}
const char curPat = *pat;
switch (*pat) {
case '*':
if (*str) {
@ -912,12 +914,23 @@ bool matchString(const char *str, const char *pat, bool ignoreCase, bool pathMod
return true;
break;
case '\\':
if (!escaped) {
pat++;
break;
}
// fallthrough
case '#':
if (!isDigit(*str))
return false;
pat++;
str++;
break;
// treat # as a wildcard for digits unless escaped
if (!escaped) {
if (!isDigit(*str))
return false;
pat++;
str++;
break;
}
// fallthrough
default:
if ((!ignoreCase && *pat != *str) ||
@ -940,6 +953,8 @@ bool matchString(const char *str, const char *pat, bool ignoreCase, bool pathMod
pat++;
str++;
}
escaped = !escaped && (curPat == '\\');
}
}

View File

@ -179,6 +179,7 @@ public:
* "*": any character, any amount of times.
* "?": any character, only once.
* "#": any decimal digit, only once.
* "\#": #, only once.
*
* Example strings/patterns:
* String: monkey.s01 Pattern: monkey.s?? => true

View File

@ -335,6 +335,10 @@ class StringTestSuite : public CxxTest::TestSuite
TS_ASSERT(Common::matchString("monkey.s01", "monkey.s##"));
TS_ASSERT(!Common::matchString("monkey.s01", "monkey.###"));
TS_ASSERT(Common::matchString("monkey.s0#", "monkey.s0\\#"));
TS_ASSERT(!Common::matchString("monkey.s0#", "monkey.s0#"));
TS_ASSERT(!Common::matchString("monkey.s01", "monkey.s0\\#"));
TS_ASSERT(!Common::String("").matchString("*_"));
TS_ASSERT(Common::String("a").matchString("a***"));
}