Skip to content

Speed up surrogate validation in HttpUtility #110478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading