Add another fast path to WildcardPattern.IsMatch for patterns that only have an asterisk in the end (#10054)

This commit is contained in:
Ilya
2019-07-10 09:15:17 -07:00
committed by Dongbo Wang
parent 2ff65c100b
commit af71952817
@@ -135,20 +135,8 @@ namespace System.Management.Automation
/// <returns>True on success, false otherwise.</returns>
private void Init()
{
if (_isMatch != null)
StringComparison GetStringComparison()
{
return;
}
if (Pattern.Length == 1 && Pattern[0] == '*')
{
_isMatch = s_matchAll;
return;
}
if (Pattern.IndexOfAny(s_specialChars) == -1)
{
// No special characters present in the pattern, so we can just do a string comparison.
StringComparison stringComparison;
if (Options.HasFlag(WildcardOptions.IgnoreCase))
{
@@ -163,7 +151,33 @@ namespace System.Management.Automation
: StringComparison.CurrentCulture;
}
_isMatch = str => string.Equals(str, Pattern, stringComparison);
return stringComparison;
}
if (_isMatch != null)
{
return;
}
if (Pattern.Length == 1 && Pattern[0] == '*')
{
_isMatch = s_matchAll;
return;
}
int index = Pattern.IndexOfAny(s_specialChars);
if (index == -1)
{
// No special characters present in the pattern, so we can just do a string comparison.
_isMatch = str => string.Equals(str, Pattern, GetStringComparison());
return;
}
if (index == Pattern.Length - 1 && Pattern[index] == '*')
{
// No special characters present in the pattern before last position and last character is asterisk.
var patternWithoutAsterisk = Pattern.AsMemory().Slice(0, index);
_isMatch = str => str.AsSpan().StartsWith(patternWithoutAsterisk.Span, GetStringComparison());
return;
}