Skip to content

Commit a361f7f

Browse files
authored
Use IndexOf in WebUtility (#70700)
The IndexOfHtmlDecodingChars method was iterating character by character looking for either a `&` or a surrogate, but then the slow path if one of those is found doesn't special-case surrogates. So, we can just collapse this to a vectorized `IndexOf('&')`, which makes the fast path of detecting whether there's anything to decode much faster if there's any meaningful amount of input prior to a `&`. (I experimented with also using `IndexOf('&')` in the main routine, but it made cases with lots of entities slower, and so I'm not including that here.)
1 parent 84f7cad commit a361f7f

1 file changed

Lines changed: 2 additions & 17 deletions

File tree

src/libraries/System.Private.CoreLib/src/System/Net/WebUtility.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private static void HtmlEncode(ReadOnlySpan<char> input, ref ValueStringBuilder
185185

186186
ReadOnlySpan<char> valueSpan = value.AsSpan();
187187

188-
int index = IndexOfHtmlDecodingChars(valueSpan);
188+
int index = valueSpan.IndexOf('&');
189189
if (index < 0)
190190
{
191191
return value;
@@ -215,7 +215,7 @@ public static void HtmlDecode(string? value, TextWriter output)
215215

216216
ReadOnlySpan<char> valueSpan = value.AsSpan();
217217

218-
int index = IndexOfHtmlDecodingChars(valueSpan);
218+
int index = valueSpan.IndexOf('&');
219219
if (index == -1)
220220
{
221221
output.Write(value);
@@ -701,21 +701,6 @@ private static bool ValidateUrlEncodingParameters(byte[]? bytes, int offset, int
701701
return true;
702702
}
703703

704-
private static int IndexOfHtmlDecodingChars(ReadOnlySpan<char> input)
705-
{
706-
// this string requires html decoding if it contains '&' or a surrogate character
707-
for (int i = 0; i < input.Length; i++)
708-
{
709-
char c = input[i];
710-
if (c == '&' || char.IsSurrogate(c))
711-
{
712-
return i;
713-
}
714-
}
715-
716-
return -1;
717-
}
718-
719704
#endregion
720705

721706
// Internal struct to facilitate URL decoding -- keeps char buffer and byte buffer, allows appending of either chars or bytes

0 commit comments

Comments
 (0)