Skip to content

Commit

Permalink
Tweak Invariant IndexOf logic (dotnet#108709)
Browse files Browse the repository at this point in the history
Co-authored-by: Tarek Mahmoud Sayed <[email protected]>
  • Loading branch information
2 people authored and rzikm committed Oct 11, 2024
1 parent 021641d commit 90e3812
Showing 1 changed file with 4 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,12 @@ private unsafe int IndexOfOrdinalIgnoreCaseHelper(ReadOnlySpan<char> source, Rea
}

int startIndex, endIndex, jump;
ReadOnlySpan<char> 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
Expand All @@ -146,7 +144,6 @@ private unsafe int IndexOfOrdinalIgnoreCaseHelper(ReadOnlySpan<char> 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;
}

Expand Down Expand Up @@ -196,6 +193,10 @@ private unsafe int IndexOfOrdinalIgnoreCaseHelper(ReadOnlySpan<char> source, Rea
}

// Before we return -1, check if the remaining source contains any special or non-Ascii characters.
ReadOnlySpan<char> remainingSource = fromBeginning
? source.Slice(endIndex)
: source.Slice(0, startIndex);

if (remainingSource.ContainsAnyExcept(s_nonSpecialAsciiChars))
{
goto InteropCall;
Expand Down Expand Up @@ -255,14 +256,12 @@ private unsafe int IndexOfOrdinalHelper(ReadOnlySpan<char> source, ReadOnlySpan<
}

int startIndex, endIndex, jump;
ReadOnlySpan<char> 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
Expand All @@ -271,7 +270,6 @@ private unsafe int IndexOfOrdinalHelper(ReadOnlySpan<char> 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;
}

Expand Down Expand Up @@ -309,12 +307,6 @@ private unsafe int IndexOfOrdinalHelper(ReadOnlySpan<char> 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:
Expand Down

0 comments on commit 90e3812

Please sign in to comment.