Skip to content

Commit ca5cf41

Browse files
authoredDec 10, 2024··
Speed up surrogate validation in string.Normalize (#110574)
* Speed up surrogate validation in string.Normalize * Remove the else block
1 parent f9c86dc commit ca5cf41

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed
 

‎src/libraries/System.Private.CoreLib/src/System/Globalization/Normalization.Icu.cs

+10-9
Original file line numberDiff line numberDiff line change
@@ -218,16 +218,20 @@ private static void ValidateArguments(ReadOnlySpan<char> strInput, Normalization
218218
/// </summary>
219219
private static bool HasInvalidUnicodeSequence(ReadOnlySpan<char> s)
220220
{
221-
for (int i = 0; i < s.Length; i++)
221+
const char Noncharacter = '\uFFFE';
222+
223+
int i = s.IndexOfAnyInRange(CharUnicodeInfo.HIGH_SURROGATE_START, Noncharacter);
224+
225+
for (; (uint)i < (uint)s.Length; i++)
222226
{
223227
char c = s[i];
224228

225-
if (c < '\ud800')
229+
if (c < CharUnicodeInfo.HIGH_SURROGATE_START)
226230
{
227231
continue;
228232
}
229233

230-
if (c == '\uFFFE')
234+
if (c == Noncharacter)
231235
{
232236
return true;
233237
}
@@ -240,17 +244,14 @@ private static bool HasInvalidUnicodeSequence(ReadOnlySpan<char> s)
240244

241245
if (char.IsHighSurrogate(c))
242246
{
243-
if (i + 1 >= s.Length || !char.IsLowSurrogate(s[i + 1]))
247+
if ((uint)(i + 1) >= (uint)s.Length || !char.IsLowSurrogate(s[i + 1]))
244248
{
245249
// A high surrogate at the end of the string or a high surrogate
246250
// not followed by a low surrogate
247251
return true;
248252
}
249-
else
250-
{
251-
i++; // consume the low surrogate.
252-
continue;
253-
}
253+
254+
i++; // consume the low surrogate.
254255
}
255256
}
256257

0 commit comments

Comments
 (0)
Please sign in to comment.