DocSpace-client/common/ASC.Data.Storage/Wildcard.cs

70 lines
2.1 KiB
C#
Raw Normal View History

2022-02-10 11:24:16 +00:00
namespace ASC.Data.Storage;
public static class Wildcard
2019-06-04 14:43:20 +00:00
{
2022-02-10 11:24:16 +00:00
public static bool IsMatch(string pattern, string input)
2019-06-04 14:43:20 +00:00
{
2022-02-10 11:24:16 +00:00
return IsMatch(pattern, input, false);
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public static bool IsMatch(string pattern, string input, bool caseInsensitive)
{
var offsetInput = 0;
var isAsterix = false;
2019-06-04 14:43:20 +00:00
2022-01-24 09:27:43 +00:00
int i = 0;
while (i < pattern.Length)
2022-02-10 11:24:16 +00:00
{
switch (pattern[i])
2019-06-04 14:43:20 +00:00
{
2022-02-10 11:24:16 +00:00
case '?':
isAsterix = false;
offsetInput++;
break;
case '*':
isAsterix = true;
while (i < pattern.Length && pattern[i] == '*')
{
i++;
}
if (i >= pattern.Length)
{
return true;
}
continue;
default:
if (offsetInput >= input.Length)
{
return false;
}
if ((caseInsensitive ? char.ToLower(input[offsetInput]) : input[offsetInput]) != (caseInsensitive ? char.ToLower(pattern[i]) : pattern[i]))
{
if (!isAsterix)
2020-10-16 13:21:59 +00:00
{
return false;
}
offsetInput++;
2022-02-10 11:24:16 +00:00
continue;
}
offsetInput++;
break;
} // end switch
i++;
} // end for
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
// have we finished parsing our input?
if (i > input.Length)
{
return false;
}
// do we have any lingering asterixes we need to skip?
while (i < pattern.Length && pattern[i] == '*')
{
++i;
2019-06-04 14:43:20 +00:00
}
2022-02-10 11:24:16 +00:00
// final evaluation. The index should be pointing at the
// end of the string.
return offsetInput == input.Length;
2019-06-04 14:43:20 +00:00
}
2022-02-10 11:24:16 +00:00
}