Skip to content

Commit d994d31

Browse files
authored
use new System.Text.Ascii APIs, remove internal helpers (#48368)
1 parent 58a8f22 commit d994d31

File tree

7 files changed

+20
-421
lines changed

7 files changed

+20
-421
lines changed

src/Http/Headers/src/ContentDispositionHeaderValue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ private static string Encode5987(StringSegment input)
631631
int totalBytesConsumed = 0;
632632
while (totalBytesConsumed < inputBytes.Length)
633633
{
634-
if (inputBytes[totalBytesConsumed] <= 0x7F)
634+
if (Ascii.IsValid(inputBytes[totalBytesConsumed]))
635635
{
636636
// This is an ASCII char. Let's handle it ourselves.
637637

src/Http/Routing/src/Matching/Ascii.cs

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/Http/Routing/src/Matching/JumpTableBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Diagnostics.CodeAnalysis;
55
using System.Runtime.CompilerServices;
6+
using System.Text;
67

78
namespace Microsoft.AspNetCore.Routing.Matching;
89

@@ -42,7 +43,7 @@ public static JumpTable Build(int defaultDestination, int exitDestination, (stri
4243

4344
// The IL Emit jump table is not faster for a single entry - but we have an optimized version when all text
4445
// is ASCII
45-
if (pathEntries.Length == 1 && Ascii.IsAscii(pathEntries[0].text))
46+
if (pathEntries.Length == 1 && Ascii.IsValid(pathEntries[0].text))
4647
{
4748
var entry = pathEntries[0];
4849
return new SingleEntryAsciiJumpTable(defaultDestination, exitDestination, entry.text, entry.destination);

src/Http/Routing/src/Matching/SingleEntryAsciiJumpTable.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics;
5+
using System.Text;
6+
47
namespace Microsoft.AspNetCore.Routing.Matching;
58

69
// Optimized implementation for cases where we know that we're
@@ -41,7 +44,9 @@ public override int GetDestination(string path, PathSegment segment)
4144
var a = path.AsSpan(segment.Start, length);
4245
var b = text.AsSpan();
4346

44-
return Ascii.AsciiIgnoreCaseEquals(a, b, length) ? _destination : _defaultDestination;
47+
Debug.Assert(a.Length == b.Length && b.Length == length);
48+
49+
return Ascii.EqualsIgnoreCase(a, b) ? _destination : _defaultDestination;
4550
}
4651

4752
public override string DebuggerToString()

src/Http/Routing/test/UnitTests/Matching/AsciiTest.cs

Lines changed: 0 additions & 110 deletions
This file was deleted.

src/Servers/Kestrel/Core/src/Internal/Infrastructure/HttpUtilities.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ private static ulong GetAsciiStringAsLong(string str)
5454
{
5555
Debug.Assert(str.Length == 8, "String must be exactly 8 (ASCII) characters long.");
5656

57-
var bytes = Encoding.ASCII.GetBytes(str);
57+
Span<byte> bytes = stackalloc byte[8];
58+
OperationStatus operationStatus = Ascii.FromUtf16(str, bytes, out _);
59+
60+
Debug.Assert(operationStatus == OperationStatus.Done);
5861

5962
return BinaryPrimitives.ReadUInt64LittleEndian(bytes);
6063
}
@@ -63,7 +66,11 @@ private static uint GetAsciiStringAsInt(string str)
6366
{
6467
Debug.Assert(str.Length == 4, "String must be exactly 4 (ASCII) characters long.");
6568

66-
var bytes = Encoding.ASCII.GetBytes(str);
69+
Span<byte> bytes = stackalloc byte[4];
70+
OperationStatus operationStatus = Ascii.FromUtf16(str, bytes, out _);
71+
72+
Debug.Assert(operationStatus == OperationStatus.Done);
73+
6774
return BinaryPrimitives.ReadUInt32LittleEndian(bytes);
6875
}
6976

0 commit comments

Comments
 (0)