Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions src/System.Management.Automation/engine/regex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,8 @@ public static WildcardPattern Get(string pattern, WildcardOptions options)
/// <returns>True on success, false otherwise.</returns>
private void Init()
{
if (_isMatch != null)
{
return;
}

if (Pattern.Length == 1 && Pattern[0] == '*')
{
_isMatch = s_matchAll;
return;
}

if (Pattern.IndexOfAny(s_specialChars) == -1)
StringComparison GetStringComparison()
{
// No special characters present in the pattern, so we can just do a string comparison.
StringComparison stringComparison;
if (Options.HasFlag(WildcardOptions.IgnoreCase))
{
Expand All @@ -171,7 +159,33 @@ private void Init()
: 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;
}

Expand Down