diff --git a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexInterpreter.cs b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexInterpreter.cs index e477f7c742babc..00715b9086a22a 100644 --- a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexInterpreter.cs +++ b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexInterpreter.cs @@ -210,7 +210,6 @@ private char Forwardcharnext(ReadOnlySpan inputSpan) private bool MatchString(string str, ReadOnlySpan inputSpan) { int c = str.Length; - int pos; if (!_rightToLeft) { @@ -219,7 +218,13 @@ private bool MatchString(string str, ReadOnlySpan inputSpan) return false; } - pos = runtextpos + c; + if (!inputSpan.Slice(runtextpos, c).SequenceEqual(str.AsSpan())) + { + return false; + } + + runtextpos += c; + return true; } else { @@ -228,25 +233,18 @@ private bool MatchString(string str, ReadOnlySpan inputSpan) return false; } - pos = runtextpos; - } - - while (c != 0) - { - if (str[--c] != inputSpan[--pos]) + int pos = runtextpos; + while (c != 0) { - return false; + if (str[--c] != inputSpan[--pos]) + { + return false; + } } - } - if (!_rightToLeft) - { - pos += str.Length; + runtextpos = pos; + return true; } - - runtextpos = pos; - - return true; } private bool MatchRef(int index, int length, ReadOnlySpan inputSpan, bool caseInsensitive) @@ -896,12 +894,23 @@ private bool TryMatchAtCurrentPosition(ReadOnlySpan inputSpan) } char ch = (char)Operand(0); - while (c-- > 0) + if (!_rightToLeft) { - if (Forwardcharnext(inputSpan) != ch) + if (inputSpan.Slice(runtextpos, c).ContainsAnyExcept(ch)) { goto BreakBackward; } + runtextpos += c; + } + else + { + while (c-- > 0) + { + if (Forwardcharnext(inputSpan) != ch) + { + goto BreakBackward; + } + } } } advance = 2; @@ -916,12 +925,23 @@ private bool TryMatchAtCurrentPosition(ReadOnlySpan inputSpan) } char ch = (char)Operand(0); - while (c-- > 0) + if (!_rightToLeft) { - if (Forwardcharnext(inputSpan) == ch) + if (inputSpan.Slice(runtextpos, c).Contains(ch)) { goto BreakBackward; } + runtextpos += c; + } + else + { + while (c-- > 0) + { + if (Forwardcharnext(inputSpan) == ch) + { + goto BreakBackward; + } + } } } advance = 2; @@ -957,12 +977,31 @@ private bool TryMatchAtCurrentPosition(ReadOnlySpan inputSpan) char ch = (char)Operand(0); int i; - for (i = len; i > 0; i--) + if (!_rightToLeft) { - if (Forwardcharnext(inputSpan) != ch) + // We're left-to-right, so we can employ the vectorized IndexOfAnyExcept + // to search for any character that isn't the target. + i = inputSpan.Slice(runtextpos, len).IndexOfAnyExcept(ch); + if (i == -1) { - Backwardnext(); - break; + runtextpos += len; + i = 0; + } + else + { + runtextpos += i; + i = len - i; + } + } + else + { + for (i = len; i > 0; i--) + { + if (Forwardcharnext(inputSpan) != ch) + { + Backwardnext(); + break; + } } }