diff --git a/src/System.Management.Automation/engine/regex.cs b/src/System.Management.Automation/engine/regex.cs index 086d459a19..4c9fe3f7cd 100644 --- a/src/System.Management.Automation/engine/regex.cs +++ b/src/System.Management.Automation/engine/regex.cs @@ -135,20 +135,8 @@ namespace System.Management.Automation /// True on success, false otherwise. 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; }