Skip to content

Commit 9e16fe2

Browse files
Tweak Invariant IndexOf logic (#108728)
Co-authored-by: Miha Zupan <[email protected]>
1 parent efbd310 commit 9e16fe2

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Icu.cs

+4-12
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,12 @@ private unsafe int IndexOfOrdinalIgnoreCaseHelper(ReadOnlySpan<char> source, Rea
130130
}
131131

132132
int startIndex, endIndex, jump;
133-
ReadOnlySpan<char> remainingSource;
134133
if (fromBeginning)
135134
{
136135
// Left to right, from zero to last possible index in the source string.
137136
// Incrementing by one after each iteration. Stop condition is last possible index plus 1.
138137
startIndex = 0;
139138
endIndex = source.Length - target.Length + 1;
140-
remainingSource = source.Slice(endIndex);
141139
jump = 1;
142140
}
143141
else
@@ -146,7 +144,6 @@ private unsafe int IndexOfOrdinalIgnoreCaseHelper(ReadOnlySpan<char> source, Rea
146144
// Decrementing by one after each iteration. Stop condition is last possible index minus 1.
147145
startIndex = source.Length - target.Length;
148146
endIndex = -1;
149-
remainingSource = source.Slice(0, startIndex);
150147
jump = -1;
151148
}
152149

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

198195
// Before we return -1, check if the remaining source contains any special or non-Ascii characters.
196+
ReadOnlySpan<char> remainingSource = fromBeginning
197+
? source.Slice(endIndex)
198+
: source.Slice(0, startIndex);
199+
199200
if (remainingSource.ContainsAnyExcept(s_nonSpecialAsciiChars))
200201
{
201202
goto InteropCall;
@@ -255,14 +256,12 @@ private unsafe int IndexOfOrdinalHelper(ReadOnlySpan<char> source, ReadOnlySpan<
255256
}
256257

257258
int startIndex, endIndex, jump;
258-
ReadOnlySpan<char> remainingSource;
259259
if (fromBeginning)
260260
{
261261
// Left to right, from zero to last possible index in the source string.
262262
// Incrementing by one after each iteration. Stop condition is last possible index plus 1.
263263
startIndex = 0;
264264
endIndex = source.Length - target.Length + 1;
265-
remainingSource = source.Slice(endIndex);
266265
jump = 1;
267266
}
268267
else
@@ -271,7 +270,6 @@ private unsafe int IndexOfOrdinalHelper(ReadOnlySpan<char> source, ReadOnlySpan<
271270
// Decrementing by one after each iteration. Stop condition is last possible index minus 1.
272271
startIndex = source.Length - target.Length;
273272
endIndex = -1;
274-
remainingSource = source.Slice(0, startIndex);
275273
jump = -1;
276274
}
277275

@@ -309,12 +307,6 @@ private unsafe int IndexOfOrdinalHelper(ReadOnlySpan<char> source, ReadOnlySpan<
309307
Next: ;
310308
}
311309

312-
// Before we return -1, check if the remaining source contains any special or non-Ascii characters.
313-
if (remainingSource.ContainsAnyExcept(s_nonSpecialAsciiChars))
314-
{
315-
goto InteropCall;
316-
}
317-
318310
return -1;
319311

320312
InteropCall:

0 commit comments

Comments
 (0)