Skip to content

Commit

Permalink
Speed up surrogate validation in HttpUtility (#110478)
Browse files Browse the repository at this point in the history
* Speed up surrogate validation in HttpUtility
  • Loading branch information
MihaZupan authored and pull[bot] committed Jan 17, 2025
1 parent 9d03f9b commit 2a28fde
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@
<Compile Include="System\Web\HttpUtility.cs" />
<Compile Include="System\Web\Util\HttpEncoder.cs" />
<Compile Include="System\Web\Util\UriUtil.cs" />
<Compile Include="System\Web\Util\Utf16StringValidator.cs" />
<Compile Include="$(CommonPath)System\HexConverter.cs"
Link="Common\System\HexConverter.cs" />
<Compile Include="$(CommonPath)System\Text\ValueStringBuilder.cs"
Link="Common\System\Text\ValueStringBuilder.cs" />
<Compile Include="$(CommonPath)System\Text\ValueStringBuilder.AppendSpanFormattable.cs"
Link="Common\System\Text\ValueStringBuilder.AppendSpanFormattable.cs" />
<Compile Include="$(CommonPath)System\HexConverter.cs" Link="Common\System\HexConverter.cs" />
<Compile Include="$(CommonPath)System\Text\ValueStringBuilder.cs" Link="Common\System\Text\ValueStringBuilder.cs" />
<Compile Include="$(CommonPath)System\Text\ValueStringBuilder.AppendSpanFormattable.cs" Link="Common\System\Text\ValueStringBuilder.AppendSpanFormattable.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ internal static byte[] UrlDecode(ReadOnlySpan<byte> bytes)
helper.AddByte(b);
}

return Utf16StringValidator.ValidateString(helper.GetString());
return helper.GetString();
}

[return: NotNullIfNotNull(nameof(value))]
Expand Down Expand Up @@ -383,7 +383,7 @@ internal static string UrlDecode(ReadOnlySpan<char> value, Encoding encoding)
}
}

return Utf16StringValidator.ValidateString(helper.GetString());
return helper.GetString();
}

[return: NotNullIfNotNull(nameof(bytes))]
Expand Down Expand Up @@ -674,7 +674,35 @@ internal string GetString()
FlushBytes();
}

return _charBuffer.Slice(0, _numChars).ToString();
Span<char> chars = _charBuffer.Slice(0, _numChars);

const char HIGH_SURROGATE_START = '\ud800';
const char LOW_SURROGATE_END = '\udfff';

// Replace any invalid surrogate chars.
int idxOfFirstSurrogate = chars.IndexOfAnyInRange(HIGH_SURROGATE_START, LOW_SURROGATE_END);
for (int i = idxOfFirstSurrogate; (uint)i < (uint)chars.Length; i++)
{
if (char.IsHighSurrogate(chars[i]))
{
if ((uint)(i + 1) >= (uint)chars.Length || !char.IsLowSurrogate(chars[i + 1]))
{
// High surrogate not followed by a low surrogate.
chars[i] = (char)Rune.ReplacementChar.Value;
}
else
{
i++;
}
}
else if (char.IsLowSurrogate(chars[i]))
{
// Low surrogate not preceded by a high surrogate.
chars[i] = (char)Rune.ReplacementChar.Value;
}
}

return chars.ToString();
}
}
}
Expand Down

This file was deleted.

0 comments on commit 2a28fde

Please sign in to comment.