diff --git a/src/libraries/System.Private.CoreLib/src/System/SearchValues/Strings/SingleStringSearchValuesThreeChars.cs b/src/libraries/System.Private.CoreLib/src/System/SearchValues/Strings/SingleStringSearchValuesThreeChars.cs index 0060261777b9a8..26fc08cc8dd9e0 100644 --- a/src/libraries/System.Private.CoreLib/src/System/SearchValues/Strings/SingleStringSearchValuesThreeChars.cs +++ b/src/libraries/System.Private.CoreLib/src/System/SearchValues/Strings/SingleStringSearchValuesThreeChars.cs @@ -330,7 +330,8 @@ private bool TryMatch(ref char searchSpaceStart, int searchSpaceLength, ref char // If the value is short (!TValueLength.AtLeast4Chars => 2 or 3 characters), the anchors already represent the whole value. // With case-sensitive comparisons, we've therefore already confirmed the match, so we can skip doing so here. // With case-insensitive comparisons, we applied a mask to the input, so while the anchors likely matched, we can't be sure. - if ((typeof(TCaseSensitivity) == typeof(CaseSensitive) && !TValueLength.AtLeast4Chars) || + // If the value is composed of only ASCII letters, masking the input can't produce false positives, so we can also skip the verification step. + if (((typeof(TCaseSensitivity) == typeof(CaseSensitive) || typeof(TCaseSensitivity) == typeof(CaseInsensitiveAsciiLetters)) && !TValueLength.AtLeast4Chars) || TCaseSensitivity.Equals(ref matchRef, _value)) { offsetFromStart = (int)((nuint)Unsafe.ByteOffset(ref searchSpaceStart, ref matchRef) / 2); @@ -362,7 +363,8 @@ private bool TryMatch(ref char searchSpaceStart, int searchSpaceLength, ref char // If the value is short (!TValueLength.AtLeast4Chars => 2 or 3 characters), the anchors already represent the whole value. // With case-sensitive comparisons, we've therefore already confirmed the match, so we can skip doing so here. // With case-insensitive comparisons, we applied a mask to the input, so while the anchors likely matched, we can't be sure. - if ((typeof(TCaseSensitivity) == typeof(CaseSensitive) && !TValueLength.AtLeast4Chars) || + // If the value is composed of only ASCII letters, masking the input can't produce false positives, so we can also skip the verification step. + if (((typeof(TCaseSensitivity) == typeof(CaseSensitive) || typeof(TCaseSensitivity) == typeof(CaseInsensitiveAsciiLetters)) && !TValueLength.AtLeast4Chars) || TCaseSensitivity.Equals(ref matchRef, _value)) { offsetFromStart = (int)((nuint)Unsafe.ByteOffset(ref searchSpaceStart, ref matchRef) / 2);