mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Add another fast path to WildcardPattern.IsMatch for patterns that only have an asterisk in the end (#10054)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user