Skip to content

Commit

Permalink
Remove a char[] allocation from HttpUtility (dotnet#32298)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Feb 14, 2020
1 parent df3930e commit 7edf03d
Showing 1 changed file with 27 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,39 +33,41 @@ internal static string ValidateString(string input)
}

// slow case: surrogates exist, so we need to validate them
char[] chars = input.ToCharArray();
for (int i = idxOfFirstSurrogate; i < chars.Length; i++)
return string.Create(input.Length, (input, idxOfFirstSurrogate), (chars, state) =>
{
char thisChar = chars[i];

// If this character is a low surrogate, then it was not preceded by
// a high surrogate, so we'll replace it.
if (char.IsLowSurrogate(thisChar))
state.input.AsSpan().CopyTo(chars);
for (int i = state.idxOfFirstSurrogate; i < chars.Length; i++)
{
chars[i] = UnicodeReplacementChar;
continue;
}
char thisChar = chars[i];

if (char.IsHighSurrogate(thisChar))
{
// If this character is a high surrogate and it is followed by a
// low surrogate, allow both to remain.
if (i + 1 < chars.Length && char.IsLowSurrogate(chars[i + 1]))
// If this character is a low surrogate, then it was not preceded by
// a high surrogate, so we'll replace it.
if (char.IsLowSurrogate(thisChar))
{
i++; // skip the low surrogate also
chars[i] = UnicodeReplacementChar;
continue;
}

// If this character is a high surrogate and it is not followed
// by a low surrogate, replace it.
chars[i] = UnicodeReplacementChar;
continue;
}
if (char.IsHighSurrogate(thisChar))
{
// If this character is a high surrogate and it is followed by a
// low surrogate, allow both to remain.
if (i + 1 < chars.Length && char.IsLowSurrogate(chars[i + 1]))
{
i++; // skip the low surrogate also
continue;
}

// Otherwise, this is a non-surrogate character and just move to the
// next character.
}
return new string(chars);
// If this character is a high surrogate and it is not followed
// by a low surrogate, replace it.
chars[i] = UnicodeReplacementChar;
continue;
}

// Otherwise, this is a non-surrogate character and just move to the
// next character.
}
});
}
}
}

0 comments on commit 7edf03d

Please sign in to comment.