diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Icu.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Icu.cs index 92492f37049fd3..a6e37956fd1551 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Icu.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Icu.cs @@ -130,14 +130,12 @@ private unsafe int IndexOfOrdinalIgnoreCaseHelper(ReadOnlySpan source, Rea } int startIndex, endIndex, jump; - ReadOnlySpan remainingSource; if (fromBeginning) { // Left to right, from zero to last possible index in the source string. // Incrementing by one after each iteration. Stop condition is last possible index plus 1. startIndex = 0; endIndex = source.Length - target.Length + 1; - remainingSource = source.Slice(endIndex); jump = 1; } else @@ -146,7 +144,6 @@ private unsafe int IndexOfOrdinalIgnoreCaseHelper(ReadOnlySpan source, Rea // Decrementing by one after each iteration. Stop condition is last possible index minus 1. startIndex = source.Length - target.Length; endIndex = -1; - remainingSource = source.Slice(0, startIndex); jump = -1; } @@ -196,6 +193,10 @@ private unsafe int IndexOfOrdinalIgnoreCaseHelper(ReadOnlySpan source, Rea } // Before we return -1, check if the remaining source contains any special or non-Ascii characters. + ReadOnlySpan remainingSource = fromBeginning + ? source.Slice(endIndex) + : source.Slice(0, startIndex); + if (remainingSource.ContainsAnyExcept(s_nonSpecialAsciiChars)) { goto InteropCall; @@ -255,14 +256,12 @@ private unsafe int IndexOfOrdinalHelper(ReadOnlySpan source, ReadOnlySpan< } int startIndex, endIndex, jump; - ReadOnlySpan remainingSource; if (fromBeginning) { // Left to right, from zero to last possible index in the source string. // Incrementing by one after each iteration. Stop condition is last possible index plus 1. startIndex = 0; endIndex = source.Length - target.Length + 1; - remainingSource = source.Slice(endIndex); jump = 1; } else @@ -271,7 +270,6 @@ private unsafe int IndexOfOrdinalHelper(ReadOnlySpan source, ReadOnlySpan< // Decrementing by one after each iteration. Stop condition is last possible index minus 1. startIndex = source.Length - target.Length; endIndex = -1; - remainingSource = source.Slice(0, startIndex); jump = -1; } @@ -309,12 +307,6 @@ private unsafe int IndexOfOrdinalHelper(ReadOnlySpan source, ReadOnlySpan< Next: ; } - // Before we return -1, check if the remaining source contains any special or non-Ascii characters. - if (remainingSource.ContainsAnyExcept(s_nonSpecialAsciiChars)) - { - goto InteropCall; - } - return -1; InteropCall: