{
int offset = 0;
- if (IndexOfAnyAsciiSearcher.IsVectorizationSupported && span.Length >= Vector128<short>.Count)
+ // We check whether the first character is ASCII before calling into IndexOfAnyAsciiSearcher
+ // in order to minimize the overhead this fast-path has on non-ASCII texts.
+ if (IndexOfAnyAsciiSearcher.IsVectorizationSupported && span.Length >= Vector128<short>.Count && char.IsAscii(span[0]))
{
// We are using IndexOfAnyAsciiSearcher to search for the first ASCII character in the set, or any non-ASCII character.
// We do this by inverting the bitmap and using the opposite search function (Negate instead of DontNegate).
{
int offset = 0;
- if (IndexOfAnyAsciiSearcher.IsVectorizationSupported && span.Length >= Vector128<short>.Count)
+ // We check whether the first character is ASCII before calling into IndexOfAnyAsciiSearcher
+ // in order to minimize the overhead this fast-path has on non-ASCII texts.
+ if (IndexOfAnyAsciiSearcher.IsVectorizationSupported && span.Length >= Vector128<short>.Count && char.IsAscii(span[0]))
{
// Do a regular IndexOfAnyExcept for the ASCII characters. The search will stop if we encounter a non-ASCII char.
offset = IndexOfAnyAsciiSearcher.IndexOfAnyVectorized<IndexOfAnyAsciiSearcher.Negate, TOptimizations>(
internal override int LastIndexOfAny(ReadOnlySpan<char> span)
{
- if (IndexOfAnyAsciiSearcher.IsVectorizationSupported && span.Length >= Vector128<short>.Count)
+ // We check whether the last character is ASCII before calling into IndexOfAnyAsciiSearcher
+ // in order to minimize the overhead this fast-path has on non-ASCII texts.
+ if (IndexOfAnyAsciiSearcher.IsVectorizationSupported && span.Length >= Vector128<short>.Count && char.IsAscii(span[^1]))
{
// We are using IndexOfAnyAsciiSearcher to search for the last ASCII character in the set, or any non-ASCII character.
// We do this by inverting the bitmap and using the opposite search function (Negate instead of DontNegate).
internal override int LastIndexOfAnyExcept(ReadOnlySpan<char> span)
{
- if (IndexOfAnyAsciiSearcher.IsVectorizationSupported && span.Length >= Vector128<short>.Count)
+ // We check whether the last character is ASCII before calling into IndexOfAnyAsciiSearcher
+ // in order to minimize the overhead this fast-path has on non-ASCII texts.
+ if (IndexOfAnyAsciiSearcher.IsVectorizationSupported && span.Length >= Vector128<short>.Count && char.IsAscii(span[^1]))
{
// Do a regular LastIndexOfAnyExcept for the ASCII characters. The search will stop if we encounter a non-ASCII char.
int offset = IndexOfAnyAsciiSearcher.LastIndexOfAnyVectorized<IndexOfAnyAsciiSearcher.Negate, TOptimizations>(