Skip to content

Commit

Permalink
use new System.Text.Ascii APIs, remove internal helpers (#48368)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsitnik authored Jul 17, 2023
1 parent 58a8f22 commit d994d31
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 421 deletions.
2 changes: 1 addition & 1 deletion src/Http/Headers/src/ContentDispositionHeaderValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ private static string Encode5987(StringSegment input)
int totalBytesConsumed = 0;
while (totalBytesConsumed < inputBytes.Length)
{
if (inputBytes[totalBytesConsumed] <= 0x7F)
if (Ascii.IsValid(inputBytes[totalBytesConsumed]))
{
// This is an ASCII char. Let's handle it ourselves.

Expand Down
73 changes: 0 additions & 73 deletions src/Http/Routing/src/Matching/Ascii.cs

This file was deleted.

3 changes: 2 additions & 1 deletion src/Http/Routing/src/Matching/JumpTableBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Text;

namespace Microsoft.AspNetCore.Routing.Matching;

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

// The IL Emit jump table is not faster for a single entry - but we have an optimized version when all text
// is ASCII
if (pathEntries.Length == 1 && Ascii.IsAscii(pathEntries[0].text))
if (pathEntries.Length == 1 && Ascii.IsValid(pathEntries[0].text))
{
var entry = pathEntries[0];
return new SingleEntryAsciiJumpTable(defaultDestination, exitDestination, entry.text, entry.destination);
Expand Down
7 changes: 6 additions & 1 deletion src/Http/Routing/src/Matching/SingleEntryAsciiJumpTable.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Text;

namespace Microsoft.AspNetCore.Routing.Matching;

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

return Ascii.AsciiIgnoreCaseEquals(a, b, length) ? _destination : _defaultDestination;
Debug.Assert(a.Length == b.Length && b.Length == length);

return Ascii.EqualsIgnoreCase(a, b) ? _destination : _defaultDestination;
}

public override string DebuggerToString()
Expand Down
110 changes: 0 additions & 110 deletions src/Http/Routing/test/UnitTests/Matching/AsciiTest.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ private static ulong GetAsciiStringAsLong(string str)
{
Debug.Assert(str.Length == 8, "String must be exactly 8 (ASCII) characters long.");

var bytes = Encoding.ASCII.GetBytes(str);
Span<byte> bytes = stackalloc byte[8];
OperationStatus operationStatus = Ascii.FromUtf16(str, bytes, out _);

Debug.Assert(operationStatus == OperationStatus.Done);

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

var bytes = Encoding.ASCII.GetBytes(str);
Span<byte> bytes = stackalloc byte[4];
OperationStatus operationStatus = Ascii.FromUtf16(str, bytes, out _);

Debug.Assert(operationStatus == OperationStatus.Done);

return BinaryPrimitives.ReadUInt32LittleEndian(bytes);
}

Expand Down
Loading

0 comments on commit d994d31

Please sign in to comment.