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..d947e42db1960a 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 @@ -209,44 +209,27 @@ private char Forwardcharnext(ReadOnlySpan inputSpan) private bool MatchString(string str, ReadOnlySpan inputSpan) { - int c = str.Length; - int pos; - if (!_rightToLeft) { - if (inputSpan.Length - runtextpos < c) + if (runtextpos > inputSpan.Length || + !inputSpan.Slice(runtextpos).StartsWith(str.AsSpan())) { return false; } - pos = runtextpos + c; + runtextpos += str.Length; + return true; } else { - if (runtextpos < c) + if (!inputSpan.Slice(0, runtextpos).EndsWith(str.AsSpan())) { return false; } - pos = runtextpos; - } - - while (c != 0) - { - if (str[--c] != inputSpan[--pos]) - { - return false; - } + runtextpos -= str.Length; + return true; } - - if (!_rightToLeft) - { - pos += str.Length; - } - - runtextpos = pos; - - return true; } private bool MatchRef(int index, int length, ReadOnlySpan inputSpan, bool caseInsensitive)