Skip to content

Commit c27314b

Browse files
committed
Update Init() method
1 parent 49e07d0 commit c27314b

1 file changed

Lines changed: 16 additions & 34 deletions

File tree

  • src/System.Management.Automation/engine

src/System.Management.Automation/engine/regex.cs

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -167,44 +167,26 @@ StringComparison GetStringComparison()
167167
return;
168168
}
169169

170-
if (Pattern.Length == 1)
170+
if (Pattern.Length == 1 && Pattern[0] == '*')
171171
{
172-
switch (Pattern[0])
173-
{
174-
case '*':
175-
_isMatch = s_matchAll;
176-
return;
177-
case '?':
178-
case '[':
179-
case ']':
180-
case '`':
181-
break;
182-
default:
183-
// No special characters present in the pattern, so we can just do a string comparison.
184-
_isMatch = str => string.Equals(str, Pattern, GetStringComparison());
185-
return;
186-
}
172+
_isMatch = s_matchAll;
173+
return;
187174
}
188175

189-
if (Pattern.Length > 1 && Pattern.AsSpan().Slice(0, Pattern.Length - 1).IndexOfAny(s_specialChars) == -1)
176+
int index = Pattern.IndexOfAny(s_specialChars);
177+
if (index == -1)
190178
{
191-
switch (Pattern[Pattern.Length - 1])
192-
{
193-
case '*':
194-
// No special characters present in the pattern before last position and last character is asterisk.
195-
var patternWithoutAsterisk = Pattern.Substring(0, Pattern.Length - 1);
196-
_isMatch = str => str.StartsWith(patternWithoutAsterisk, GetStringComparison());
197-
return;
198-
case '?':
199-
case '[':
200-
case ']':
201-
case '`':
202-
break;
203-
default:
204-
// No special characters present in the pattern, so we can just do a string comparison.
205-
_isMatch = str => string.Equals(str, Pattern, GetStringComparison());
206-
return;
207-
}
179+
// No special characters present in the pattern, so we can just do a string comparison.
180+
_isMatch = str => string.Equals(str, Pattern, GetStringComparison());
181+
return;
182+
}
183+
184+
if (index == Pattern.Length - 1 && Pattern[index] == '*')
185+
{
186+
// No special characters present in the pattern before last position and last character is asterisk.
187+
var patternWithoutAsterisk = Pattern.Substring(0, Pattern.Length - 1);
188+
_isMatch = str => str.StartsWith(patternWithoutAsterisk, GetStringComparison());
189+
return;
208190
}
209191

210192
var matcher = new WildcardPatternMatcher(this);

0 commit comments

Comments
 (0)