Skip to content

Commit

Permalink
Speed up surrogate validation in string.Normalize (#110574)
Browse files Browse the repository at this point in the history
* Speed up surrogate validation in string.Normalize

* Remove the else block
  • Loading branch information
MihaZupan authored Dec 10, 2024
1 parent f9c86dc commit ca5cf41
Showing 1 changed file with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,20 @@ private static void ValidateArguments(ReadOnlySpan<char> strInput, Normalization
/// </summary>
private static bool HasInvalidUnicodeSequence(ReadOnlySpan<char> s)
{
for (int i = 0; i < s.Length; i++)
const char Noncharacter = '\uFFFE';

int i = s.IndexOfAnyInRange(CharUnicodeInfo.HIGH_SURROGATE_START, Noncharacter);

for (; (uint)i < (uint)s.Length; i++)
{
char c = s[i];

if (c < '\ud800')
if (c < CharUnicodeInfo.HIGH_SURROGATE_START)
{
continue;
}

if (c == '\uFFFE')
if (c == Noncharacter)
{
return true;
}
Expand All @@ -240,17 +244,14 @@ private static bool HasInvalidUnicodeSequence(ReadOnlySpan<char> s)

if (char.IsHighSurrogate(c))
{
if (i + 1 >= s.Length || !char.IsLowSurrogate(s[i + 1]))
if ((uint)(i + 1) >= (uint)s.Length || !char.IsLowSurrogate(s[i + 1]))
{
// A high surrogate at the end of the string or a high surrogate
// not followed by a low surrogate
return true;
}
else
{
i++; // consume the low surrogate.
continue;
}

i++; // consume the low surrogate.
}
}

Expand Down

0 comments on commit ca5cf41

Please sign in to comment.