Skip to content
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

Ascii.Equals and Ascii.EqualsIgnoreCase #84886

Merged
merged 6 commits into from
Apr 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 5 additions & 16 deletions src/libraries/Common/src/System/Net/CaseInsensitiveAscii.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using System.Diagnostics;
using System.Text;

namespace System.Net
{
Expand Down Expand Up @@ -106,22 +108,9 @@ public new bool Equals(object? firstObject, object? secondObject)
}
if (secondString != null)
{
int index = firstString.Length;
if (index == secondString.Length)
{
if (FastGetHashCode(firstString) == FastGetHashCode(secondString))
{
while (index > 0)
{
index--;
if (AsciiToLower[firstString[index]] != AsciiToLower[secondString[index]])
{
return false;
}
}
return true;
}
}
Debug.Assert(Ascii.IsValid(firstString) || Ascii.IsValid(secondString), "Expected at least one of the inputs to be valid ASCII");

return Ascii.EqualsIgnoreCase(firstString, secondString);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,11 @@ private static void UseFullString(ReadOnlySpan<string> uniqueStrings, bool ignor
results = new(allAscii, ignoreCase, 0, 0, 0, 0);
}

// TODO https://github.com/dotnet/runtime/issues/28230:
// Replace this once Ascii.IsValid exists.
internal static unsafe bool IsAllAscii(ReadOnlySpan<char> s)
{
#if NET8_0_OR_GREATER
return System.Text.Ascii.IsValid(s);
#else
fixed (char* src = s)
{
uint* ptrUInt32 = (uint*)src;
Expand Down Expand Up @@ -172,6 +173,7 @@ internal static unsafe bool IsAllAscii(ReadOnlySpan<char> s)

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static bool AllCharsInUInt32AreAscii(uint value) => (value & ~0x007F_007Fu) == 0;
#endif
}

private static double GetUniquenessFactor(HashSet<string> set, ReadOnlySpan<string> uniqueStrings)
Expand Down
1 change: 0 additions & 1 deletion src/libraries/System.Net.Http/src/System.Net.Http.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' != ''">
<Compile Include="System\Net\Http\ByteArrayContent.cs" />
<Compile Include="System\Net\Http\ByteArrayHelpers.cs" />
<Compile Include="System\Net\Http\CancellationHelper.cs" />
<Compile Include="System\Net\Http\ClientCertificateOption.cs" />
<Compile Include="System\Net\Http\DelegatingHandler.cs" />
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public string GetHeaderValue(ReadOnlySpan<byte> headerValue, Encoding? valueEnco
{
for (int i = 0; i < knownValues.Length; i++)
{
if (ByteArrayHelpers.EqualsOrdinalAscii(knownValues[i], headerValue))
if (Ascii.Equals(headerValue, knownValues[i]))
{
return knownValues[i];
}
Expand Down Expand Up @@ -251,7 +251,7 @@ public string GetHeaderValue(ReadOnlySpan<byte> headerValue, Encoding? valueEnco

Debug.Assert(candidate is null || candidate.Length == contentTypeValue.Length);

return candidate != null && ByteArrayHelpers.EqualsOrdinalAscii(candidate, contentTypeValue) ?
return candidate != null && Ascii.Equals(contentTypeValue, candidate) ?
candidate :
null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net.Http.HPack;
using System.Net.Http.QPack;
using System.Runtime.InteropServices;
using System.Text;

namespace System.Net.Http.Headers
{
Expand Down Expand Up @@ -427,7 +428,7 @@ public BytePtrAccessor(byte* p, int length)
fixed (byte* p = &MemoryMarshal.GetReference(name))
{
KnownHeader? candidate = GetCandidate(new BytePtrAccessor(p, name.Length));
if (candidate != null && ByteArrayHelpers.EqualsOrdinalAsciiIgnoreCase(candidate.Name, name))
if (candidate != null && Ascii.EqualsIgnoreCase(name, candidate.Name))
{
return candidate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ private static void ParseStatusLineCore(Span<byte> line, HttpResponseMessage res
{
ReadOnlySpan<byte> reasonBytes = line.Slice(MinStatusLineLength + 1);
string? knownReasonPhrase = HttpStatusDescription.Get(response.StatusCode);
if (knownReasonPhrase != null && ByteArrayHelpers.EqualsOrdinalAscii(knownReasonPhrase, reasonBytes))
if (knownReasonPhrase != null && Ascii.Equals(reasonBytes, knownReasonPhrase))
{
response.SetReasonPhraseWithoutValidation(knownReasonPhrase);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public string GetResponseHeaderValueWithCaching(HeaderDescriptor descriptor, Rea
static string GetOrAddCachedValue([NotNull] ref string? cache, HeaderDescriptor descriptor, ReadOnlySpan<byte> value, Encoding? encoding)
{
string? lastValue = cache;
if (lastValue is null || !ByteArrayHelpers.EqualsOrdinalAscii(lastValue, value))
if (lastValue is null || !Ascii.Equals(value, lastValue))
{
cache = lastValue = descriptor.GetHeaderValue(value, encoding);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@
Link="ProductionCode\System\Net\Http\DelegatingHandler.cs" />
<Compile Include="$(CommonPath)System\IO\DelegatingStream.cs"
Link="ProductionCode\System\IO\DelegatingStream.cs" />
<Compile Include="..\..\src\System\Net\Http\ByteArrayHelpers.cs"
Link="ProductionCode\System\Net\Http\ByteArrayHelpers.cs" />
<Compile Include="..\..\src\System\Net\Http\EmptyContent.cs"
Link="ProductionCode\System\Net\Http\EmptyContent.cs" />
<Compile Include="..\..\src\System\Net\Http\EmptyReadStream.cs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\SystemException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Text\Ascii.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Text\Ascii.CaseConversion.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Text\Ascii.Equality.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Text\Ascii.Transcoding.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Text\Ascii.Trimming.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Text\Ascii.Utility.cs" />
Expand Down