Merge release/2.2
This commit is contained in:
commit
87084945a6
|
|
@ -10,10 +10,10 @@ namespace Microsoft.AspNetCore.Routing.Matching
|
|||
{
|
||||
public class JumpTableSingleEntryBenchmark
|
||||
{
|
||||
private JumpTable _implementation;
|
||||
private JumpTable _prototype;
|
||||
private JumpTable _default;
|
||||
private JumpTable _trie;
|
||||
private JumpTable _vectorTrie;
|
||||
private JumpTable _ascii;
|
||||
|
||||
private string[] _strings;
|
||||
private PathSegment[] _segments;
|
||||
|
|
@ -21,10 +21,10 @@ namespace Microsoft.AspNetCore.Routing.Matching
|
|||
[GlobalSetup]
|
||||
public void Setup()
|
||||
{
|
||||
_implementation = new SingleEntryJumpTable(0, -1, "hello-world", 1);
|
||||
_prototype = new SingleEntryAsciiVectorizedJumpTable(0, -2, "hello-world", 1);
|
||||
_trie = new ILEmitTrieJumpTable(0, -1, new[] { ("hello-world", 1), }, vectorize: false, _implementation);
|
||||
_vectorTrie = new ILEmitTrieJumpTable(0, -1, new[] { ("hello-world", 1), }, vectorize: true, _implementation);
|
||||
_default = new SingleEntryJumpTable(0, -1, "hello-world", 1);
|
||||
_trie = new ILEmitTrieJumpTable(0, -1, new[] { ("hello-world", 1), }, vectorize: false, _default);
|
||||
_vectorTrie = new ILEmitTrieJumpTable(0, -1, new[] { ("hello-world", 1), }, vectorize: true, _default);
|
||||
_ascii = new SingleEntryAsciiJumpTable(0, -1, "hello-world", 1);
|
||||
|
||||
_strings = new string[]
|
||||
{
|
||||
|
|
@ -80,7 +80,7 @@ namespace Microsoft.AspNetCore.Routing.Matching
|
|||
}
|
||||
|
||||
[Benchmark(OperationsPerInvoke = 5)]
|
||||
public int Implementation()
|
||||
public int Default()
|
||||
{
|
||||
var strings = _strings;
|
||||
var segments = _segments;
|
||||
|
|
@ -88,14 +88,14 @@ namespace Microsoft.AspNetCore.Routing.Matching
|
|||
var destination = 0;
|
||||
for (var i = 0; i < strings.Length; i++)
|
||||
{
|
||||
destination = _implementation.GetDestination(strings[i], segments[i]);
|
||||
destination = _default.GetDestination(strings[i], segments[i]);
|
||||
}
|
||||
|
||||
return destination;
|
||||
}
|
||||
|
||||
[Benchmark(OperationsPerInvoke = 5)]
|
||||
public int Prototype()
|
||||
public int Ascii()
|
||||
{
|
||||
var strings = _strings;
|
||||
var segments = _segments;
|
||||
|
|
@ -103,7 +103,7 @@ namespace Microsoft.AspNetCore.Routing.Matching
|
|||
var destination = 0;
|
||||
for (var i = 0; i < strings.Length; i++)
|
||||
{
|
||||
destination = _prototype.GetDestination(strings[i], segments[i]);
|
||||
destination = _ascii.GetDestination(strings[i], segments[i]);
|
||||
}
|
||||
|
||||
return destination;
|
||||
|
|
@ -138,181 +138,5 @@ namespace Microsoft.AspNetCore.Routing.Matching
|
|||
|
||||
return destination;
|
||||
}
|
||||
|
||||
private class SingleEntryAsciiVectorizedJumpTable : JumpTable
|
||||
{
|
||||
private readonly int _defaultDestination;
|
||||
private readonly int _exitDestination;
|
||||
private readonly string _text;
|
||||
private readonly int _destination;
|
||||
|
||||
private readonly ulong[] _values;
|
||||
private readonly int _residue0Lower;
|
||||
private readonly int _residue0Upper;
|
||||
private readonly int _residue1Lower;
|
||||
private readonly int _residue1Upper;
|
||||
private readonly int _residue2Lower;
|
||||
private readonly int _residue2Upper;
|
||||
|
||||
public SingleEntryAsciiVectorizedJumpTable(
|
||||
int defaultDestination,
|
||||
int exitDestination,
|
||||
string text,
|
||||
int destination)
|
||||
{
|
||||
_defaultDestination = defaultDestination;
|
||||
_exitDestination = exitDestination;
|
||||
_text = text;
|
||||
_destination = destination;
|
||||
|
||||
var length = text.Length;
|
||||
var span = text.ToLowerInvariant().AsSpan();
|
||||
ref var p = ref Unsafe.As<char, byte>(ref MemoryMarshal.GetReference(span));
|
||||
|
||||
_values = new ulong[length / 4];
|
||||
for (var i = 0; i < length / 4; i++)
|
||||
{
|
||||
_values[i] = Unsafe.ReadUnaligned<ulong>(ref p);
|
||||
p = Unsafe.Add(ref p, 64);
|
||||
}
|
||||
switch (length % 4)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
var c = Unsafe.ReadUnaligned<char>(ref p);
|
||||
_residue0Lower = char.ToLowerInvariant(c);
|
||||
_residue0Upper = char.ToUpperInvariant(c);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 2:
|
||||
{
|
||||
var c = Unsafe.ReadUnaligned<char>(ref p);
|
||||
_residue0Lower = char.ToLowerInvariant(c);
|
||||
_residue0Upper = char.ToUpperInvariant(c);
|
||||
|
||||
p = Unsafe.Add(ref p, 2);
|
||||
c = Unsafe.ReadUnaligned<char>(ref p);
|
||||
_residue1Lower = char.ToLowerInvariant(c);
|
||||
_residue1Upper = char.ToUpperInvariant(c);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 3:
|
||||
{
|
||||
var c = Unsafe.ReadUnaligned<char>(ref p);
|
||||
_residue0Lower = char.ToLowerInvariant(c);
|
||||
_residue0Upper = char.ToUpperInvariant(c);
|
||||
|
||||
p = Unsafe.Add(ref p, 2);
|
||||
c = Unsafe.ReadUnaligned<char>(ref p);
|
||||
_residue1Lower = char.ToLowerInvariant(c);
|
||||
_residue1Upper = char.ToUpperInvariant(c);
|
||||
|
||||
p = Unsafe.Add(ref p, 2);
|
||||
c = Unsafe.ReadUnaligned<char>(ref p);
|
||||
_residue2Lower = char.ToLowerInvariant(c);
|
||||
_residue2Upper = char.ToUpperInvariant(c);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override int GetDestination(string path, PathSegment segment)
|
||||
{
|
||||
var length = segment.Length;
|
||||
var span = path.AsSpan(segment.Start, length);
|
||||
ref var p = ref Unsafe.As<char, byte>(ref MemoryMarshal.GetReference(span));
|
||||
|
||||
var i = 0;
|
||||
while (length > 3)
|
||||
{
|
||||
var value = Unsafe.ReadUnaligned<ulong>(ref p);
|
||||
|
||||
if ((value & ~0x007F007F007F007FUL) == 0)
|
||||
{
|
||||
return _defaultDestination;
|
||||
}
|
||||
|
||||
var ulongLowerIndicator = value + (0x0080008000800080UL - 0x0041004100410041UL);
|
||||
var ulongUpperIndicator = value + (0x0080008000800080UL - 0x005B005B005B005BUL);
|
||||
var ulongCombinedIndicator = (ulongLowerIndicator ^ ulongUpperIndicator) & 0x0080008000800080UL;
|
||||
var mask = (ulongCombinedIndicator) >> 2;
|
||||
|
||||
value ^= mask;
|
||||
|
||||
if (value != _values[i])
|
||||
{
|
||||
return _defaultDestination;
|
||||
}
|
||||
|
||||
i++;
|
||||
length -= 4;
|
||||
p = ref Unsafe.Add(ref p, 64);
|
||||
}
|
||||
|
||||
switch (length)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
var c = Unsafe.ReadUnaligned<char>(ref p);
|
||||
if (c != _residue0Lower && c != _residue0Upper)
|
||||
{
|
||||
return _defaultDestination;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 2:
|
||||
{
|
||||
var c = Unsafe.ReadUnaligned<char>(ref p);
|
||||
if (c != _residue0Lower && c != _residue0Upper)
|
||||
{
|
||||
return _defaultDestination;
|
||||
}
|
||||
|
||||
p = ref Unsafe.Add(ref p, 2);
|
||||
c = Unsafe.ReadUnaligned<char>(ref p);
|
||||
if (c != _residue1Lower && c != _residue1Upper)
|
||||
{
|
||||
return _defaultDestination;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 3:
|
||||
{
|
||||
var c = Unsafe.ReadUnaligned<char>(ref p);
|
||||
if (c != _residue0Lower && c != _residue0Upper)
|
||||
{
|
||||
return _defaultDestination;
|
||||
}
|
||||
|
||||
p = ref Unsafe.Add(ref p, 2);
|
||||
c = Unsafe.ReadUnaligned<char>(ref p);
|
||||
if (c != _residue1Lower && c != _residue1Upper)
|
||||
{
|
||||
return _defaultDestination;
|
||||
}
|
||||
|
||||
p = ref Unsafe.Add(ref p, 2);
|
||||
c = Unsafe.ReadUnaligned<char>(ref p);
|
||||
if (c != _residue2Lower && c != _residue2Upper)
|
||||
{
|
||||
return _defaultDestination;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return _destination;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Routing
|
|||
private RouteData _routeData;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="RouteContext"/> for the provided <paramref name="httpContext"/>.
|
||||
/// Creates a new instance of <see cref="RouteContext"/> for the provided <paramref name="httpContext"/>.
|
||||
/// </summary>
|
||||
/// <param name="httpContext">The <see cref="Http.HttpContext"/> associated with the current request.</param>
|
||||
public RouteContext(HttpContext httpContext)
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Routing
|
|||
private RouteValueDictionary _values;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="RouteData"/> instance.
|
||||
/// Creates a new instance of <see cref="RouteData"/> instance.
|
||||
/// </summary>
|
||||
public RouteData()
|
||||
{
|
||||
|
|
@ -24,7 +24,7 @@ namespace Microsoft.AspNetCore.Routing
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="RouteData"/> instance with values copied from <paramref name="other"/>.
|
||||
/// Creates a new instance of <see cref="RouteData"/> instance with values copied from <paramref name="other"/>.
|
||||
/// </summary>
|
||||
/// <param name="other">The other <see cref="RouteData"/> instance to copy.</param>
|
||||
public RouteData(RouteData other)
|
||||
|
|
@ -52,7 +52,7 @@ namespace Microsoft.AspNetCore.Routing
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="RouteData"/> instance with the specified values.
|
||||
/// Creates a new instance of <see cref="RouteData"/> instance with the specified values.
|
||||
/// </summary>
|
||||
/// <param name="values">The <see cref="RouteValueDictionary"/> values.</param>
|
||||
public RouteData(RouteValueDictionary values)
|
||||
|
|
@ -197,7 +197,7 @@ namespace Microsoft.AspNetCore.Routing
|
|||
private readonly RouteValueDictionary _values;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="RouteDataSnapshot"/> for <paramref name="routeData"/>.
|
||||
/// Creates a new instance of <see cref="RouteDataSnapshot"/> for <paramref name="routeData"/>.
|
||||
/// </summary>
|
||||
/// <param name="routeData">The <see cref="RouteData"/>.</param>
|
||||
/// <param name="dataTokens">The data tokens.</param>
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Routing
|
|||
public class VirtualPathContext
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="VirtualPathContext"/>.
|
||||
/// Creates a new instance of <see cref="VirtualPathContext"/>.
|
||||
/// </summary>
|
||||
/// <param name="httpContext">The <see cref="Http.HttpContext"/> associated with the current request.</param>
|
||||
/// <param name="ambientValues">The set of route values associated with the current request.</param>
|
||||
|
|
@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.Routing
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="VirtualPathContext"/>.
|
||||
/// Creates a new instance of <see cref="VirtualPathContext"/>.
|
||||
/// </summary>
|
||||
/// <param name="httpContext">The <see cref="Http.HttpContext"/> associated with the current request.</param>
|
||||
/// <param name="ambientValues">The set of route values associated with the current request.</param>
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Routing.Constraints
|
|||
public class HttpMethodRouteConstraint : IRouteConstraint
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="HttpMethodRouteConstraint"/> that accepts the HTTP methods specified
|
||||
/// Creates a new instance of <see cref="HttpMethodRouteConstraint"/> that accepts the HTTP methods specified
|
||||
/// by <paramref name="allowedMethods"/>.
|
||||
/// </summary>
|
||||
/// <param name="allowedMethods">The allowed HTTP methods.</param>
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Routing
|
|||
public class EndpointNameMetadata : IEndpointNameMetadata
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="EndpointNameMetadata"/> with the provided endpoint name.
|
||||
/// Creates a new instance of <see cref="EndpointNameMetadata"/> with the provided endpoint name.
|
||||
/// </summary>
|
||||
/// <param name="endpointName">The endpoint name.</param>
|
||||
public EndpointNameMetadata(string endpointName)
|
||||
|
|
|
|||
|
|
@ -5,9 +5,20 @@ using System.Collections.Generic;
|
|||
|
||||
namespace Microsoft.AspNetCore.Routing
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents metadata used during link generation to find
|
||||
/// the associated endpoint using route values.
|
||||
/// </summary>
|
||||
public interface IRouteValuesAddressMetadata
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the route name. Can be null.
|
||||
/// </summary>
|
||||
string RouteName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the required route values.
|
||||
/// </summary>
|
||||
IReadOnlyDictionary<string, object> RequiredValues { get; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,75 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Microsoft.AspNetCore.Routing.Matching
|
||||
{
|
||||
internal static class Ascii
|
||||
{
|
||||
// case-sensitive equality comparison when we KNOW that 'a' is in the ASCII range
|
||||
// and we know that the spans are the same length.
|
||||
//
|
||||
// Similar to https://github.com/dotnet/coreclr/blob/master/src/System.Private.CoreLib/shared/System/Globalization/CompareInfo.cs#L549
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool AsciiIgnoreCaseEquals(ReadOnlySpan<char> a, ReadOnlySpan<char> b, int length)
|
||||
{
|
||||
// The caller should have checked the length. We enforce that here by THROWING if the
|
||||
// lengths are unequal.
|
||||
if (a.Length < length || b.Length < length)
|
||||
{
|
||||
// This should never happen, but we don't want to have undefined
|
||||
// behavior if it does.
|
||||
ThrowArgumentExceptionForLength();
|
||||
}
|
||||
|
||||
ref var charA = ref MemoryMarshal.GetReference(a);
|
||||
ref var charB = ref MemoryMarshal.GetReference(b);
|
||||
|
||||
// Iterates each span for the provided length and compares each character
|
||||
// case-insensitively. This looks funky because we're using unsafe operations
|
||||
// to elide bounds-checks.
|
||||
while (length > 0 && AsciiIgnoreCaseEquals(charA, charB))
|
||||
{
|
||||
charA = ref Unsafe.Add(ref charA, 1);
|
||||
charB = ref Unsafe.Add(ref charB, 1);
|
||||
length--;
|
||||
}
|
||||
|
||||
return length == 0;
|
||||
}
|
||||
|
||||
// case-insensitive equality comparison for characters in the ASCII range
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool AsciiIgnoreCaseEquals(char charA, char charB)
|
||||
{
|
||||
const uint AsciiToLower = 0x20;
|
||||
return
|
||||
// Equal when chars are exactly equal
|
||||
charA == charB ||
|
||||
|
||||
// Equal when converted to-lower AND they are letters
|
||||
((charA | AsciiToLower) == (charB | AsciiToLower) && (uint)((charA | AsciiToLower) - 'a') <= (uint)('z' - 'a'));
|
||||
}
|
||||
|
||||
public static bool IsAscii(string text)
|
||||
{
|
||||
for (var i = 0; i < text.Length; i++)
|
||||
{
|
||||
if (text[i] > (char)0x7F)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void ThrowArgumentExceptionForLength()
|
||||
{
|
||||
throw new ArgumentException("length");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -39,7 +39,15 @@ namespace Microsoft.AspNetCore.Routing.Matching
|
|||
return new ZeroEntryJumpTable(defaultDestination, exitDestination);
|
||||
}
|
||||
|
||||
// The IL Emit jump table is not faster for a single entry
|
||||
// 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))
|
||||
{
|
||||
var entry = pathEntries[0];
|
||||
return new SingleEntryAsciiJumpTable(defaultDestination, exitDestination, entry.text, entry.destination);
|
||||
}
|
||||
|
||||
// We have a fallback that works for non-ASCII
|
||||
if (pathEntries.Length == 1)
|
||||
{
|
||||
var entry = pathEntries[0];
|
||||
|
|
|
|||
|
|
@ -0,0 +1,55 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Microsoft.AspNetCore.Routing.Matching
|
||||
{
|
||||
// Optimized implementation for cases where we know that we're
|
||||
// comparing to ASCII.
|
||||
internal class SingleEntryAsciiJumpTable : JumpTable
|
||||
{
|
||||
private readonly int _defaultDestination;
|
||||
private readonly int _exitDestination;
|
||||
private readonly string _text;
|
||||
private readonly int _destination;
|
||||
|
||||
public SingleEntryAsciiJumpTable(
|
||||
int defaultDestination,
|
||||
int exitDestination,
|
||||
string text,
|
||||
int destination)
|
||||
{
|
||||
_defaultDestination = defaultDestination;
|
||||
_exitDestination = exitDestination;
|
||||
_text = text;
|
||||
_destination = destination;
|
||||
}
|
||||
|
||||
public unsafe override int GetDestination(string path, PathSegment segment)
|
||||
{
|
||||
var length = segment.Length;
|
||||
if (length == 0)
|
||||
{
|
||||
return _exitDestination;
|
||||
}
|
||||
|
||||
var text = _text;
|
||||
if (length != text.Length)
|
||||
{
|
||||
return _defaultDestination;
|
||||
}
|
||||
|
||||
var a = path.AsSpan(segment.Start, length);
|
||||
var b = text.AsSpan();
|
||||
|
||||
return Ascii.AsciiIgnoreCaseEquals(a, b, length) ? _destination : _defaultDestination;
|
||||
}
|
||||
|
||||
public override string DebuggerToString()
|
||||
{
|
||||
return $"{{ {_text}: {_destination}, $+: {_defaultDestination}, $0: {_exitDestination} }}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="RoutePatternException"/>.
|
||||
/// Creates a new instance of <see cref="RoutePatternException"/>.
|
||||
/// </summary>
|
||||
/// <param name="pattern">The route pattern as raw text.</param>
|
||||
/// <param name="message">The exception message.</param>
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="RoutePattern"/> from a collection of segments.
|
||||
/// Creates a new instance of <see cref="RoutePattern"/> from a collection of segments.
|
||||
/// </summary>
|
||||
/// <param name="segments">The collection of segments.</param>
|
||||
/// <returns>The <see cref="RoutePattern"/>.</returns>
|
||||
|
|
@ -82,7 +82,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="RoutePattern"/> from a collection of segments.
|
||||
/// Creates a new instance of <see cref="RoutePattern"/> from a collection of segments.
|
||||
/// </summary>
|
||||
/// <param name="rawText">The raw text to associate with the route pattern. May be null.</param>
|
||||
/// <param name="segments">The collection of segments.</param>
|
||||
|
|
@ -158,7 +158,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="RoutePattern"/> from a collection of segments.
|
||||
/// Creates a new instance of <see cref="RoutePattern"/> from a collection of segments.
|
||||
/// </summary>
|
||||
/// <param name="segments">The collection of segments.</param>
|
||||
/// <returns>The <see cref="RoutePattern"/>.</returns>
|
||||
|
|
@ -173,7 +173,7 @@ namespace Microsoft.AspNetCore.Routing.Patterns
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="RoutePattern"/> from a collection of segments.
|
||||
/// Creates a new instance of <see cref="RoutePattern"/> from a collection of segments.
|
||||
/// </summary>
|
||||
/// <param name="rawText">The raw text to associate with the route pattern. May be null.</param>
|
||||
/// <param name="segments">The collection of segments.</param>
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Routing
|
|||
private readonly Dictionary<string, List<IRouteConstraint>> _constraints;
|
||||
private readonly HashSet<string> _optionalParameters;
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="RouteConstraintBuilder"/> instance.
|
||||
/// Creates a new instance of <see cref="RouteConstraintBuilder"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="inlineConstraintResolver">The <see cref="IInlineConstraintResolver"/>.</param>
|
||||
/// <param name="displayName">The display name (for use in error messages).</param>
|
||||
|
|
|
|||
|
|
@ -3,22 +3,61 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
|
||||
namespace Microsoft.AspNetCore.Routing
|
||||
{
|
||||
/// <summary>
|
||||
/// Metadata used during link generation to find the associated endpoint using route values.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("{DebuggerToString(),nq}")]
|
||||
public sealed class RouteValuesAddressMetadata : IRouteValuesAddressMetadata
|
||||
{
|
||||
private static readonly IReadOnlyDictionary<string, object> EmptyRouteValues =
|
||||
new ReadOnlyDictionary<string, object>(new Dictionary<string, object>());
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="RouteValuesAddressMetadata"/> with the provided route name.
|
||||
/// </summary>
|
||||
/// <param name="routeName">The route name. Can be null.</param>
|
||||
public RouteValuesAddressMetadata(string routeName) : this(routeName, EmptyRouteValues)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="RouteValuesAddressMetadata"/> with the provided required route values.
|
||||
/// </summary>
|
||||
/// <param name="requiredValues">The required route values.</param>
|
||||
public RouteValuesAddressMetadata(IReadOnlyDictionary<string, object> requiredValues) : this(null, requiredValues)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="RouteValuesAddressMetadata"/> with the provided route name and required route values.
|
||||
/// </summary>
|
||||
/// <param name="routeName">The route name. Can be null.</param>
|
||||
/// <param name="requiredValues">The required route values.</param>
|
||||
public RouteValuesAddressMetadata(string routeName, IReadOnlyDictionary<string, object> requiredValues)
|
||||
{
|
||||
if (requiredValues == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(requiredValues));
|
||||
}
|
||||
|
||||
RouteName = routeName;
|
||||
RequiredValues = requiredValues;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the route name. Can be null.
|
||||
/// </summary>
|
||||
public string RouteName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the required route values.
|
||||
/// </summary>
|
||||
public IReadOnlyDictionary<string, object> RequiredValues { get; }
|
||||
|
||||
internal string DebuggerToString()
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Routing.Template
|
|||
public class InlineConstraint
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="InlineConstraint"/>.
|
||||
/// Creates a new instance of <see cref="InlineConstraint"/>.
|
||||
/// </summary>
|
||||
/// <param name="constraint">The constraint text.</param>
|
||||
public InlineConstraint(string constraint)
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ namespace Microsoft.AspNetCore.Routing.Tree
|
|||
private readonly ILogger _constraintLogger;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="TreeRouter"/>.
|
||||
/// Creates a new instance of <see cref="TreeRouter"/>.
|
||||
/// </summary>
|
||||
/// <param name="trees">The list of <see cref="UrlMatchingTree"/> that contains the route entries.</param>
|
||||
/// <param name="linkGenerationEntries">The set of <see cref="OutboundRouteEntry"/>.</param>
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Routing
|
|||
var d = new List<object>(metadata ?? Array.Empty<object>());
|
||||
if (requiredValues != null)
|
||||
{
|
||||
d.Add(new RouteValuesAddressMetadata(null, new RouteValueDictionary(requiredValues)));
|
||||
d.Add(new RouteValuesAddressMetadata(new RouteValueDictionary(requiredValues)));
|
||||
}
|
||||
|
||||
return new RouteEndpoint(
|
||||
|
|
|
|||
|
|
@ -24,11 +24,11 @@ namespace Microsoft.AspNetCore.Routing
|
|||
var endpoint1 = EndpointFactory.CreateRouteEndpoint(
|
||||
"Home/Index/{id}",
|
||||
defaults: new { controller = "Home", action = "Index", },
|
||||
metadata: new[] { new RouteValuesAddressMetadata(routeName: null, new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
var endpoint2 = EndpointFactory.CreateRouteEndpoint(
|
||||
"Home/Index/{id?}",
|
||||
defaults: new { controller = "Home", action = "Index", },
|
||||
metadata: new[] { new RouteValuesAddressMetadata(routeName: null, new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
|
||||
var linkGenerator = CreateLinkGenerator(endpoint1, endpoint2);
|
||||
|
||||
|
|
@ -59,11 +59,11 @@ namespace Microsoft.AspNetCore.Routing
|
|||
var endpoint1 = EndpointFactory.CreateRouteEndpoint(
|
||||
"Home/Index/{id}",
|
||||
defaults: new { controller = "Home", action = "Index", },
|
||||
metadata: new[] { new RouteValuesAddressMetadata(routeName: null, new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
var endpoint2 = EndpointFactory.CreateRouteEndpoint(
|
||||
"Home/Index/{id?}",
|
||||
defaults: new { controller = "Home", action = "Index", },
|
||||
metadata: new[] { new RouteValuesAddressMetadata(routeName: null, new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
|
||||
var linkGenerator = CreateLinkGenerator(endpoint1, endpoint2);
|
||||
|
||||
|
|
@ -86,11 +86,11 @@ namespace Microsoft.AspNetCore.Routing
|
|||
var endpoint1 = EndpointFactory.CreateRouteEndpoint(
|
||||
"Home/Index/{id}",
|
||||
defaults: new { controller = "Home", action = "Index", },
|
||||
metadata: new[] { new RouteValuesAddressMetadata(routeName: null, new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
var endpoint2 = EndpointFactory.CreateRouteEndpoint(
|
||||
"Home/Index/{id?}",
|
||||
defaults: new { controller = "Home", action = "Index", },
|
||||
metadata: new[] { new RouteValuesAddressMetadata(routeName: null, new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
|
||||
var linkGenerator = CreateLinkGenerator(endpoint1, endpoint2);
|
||||
|
||||
|
|
@ -116,11 +116,11 @@ namespace Microsoft.AspNetCore.Routing
|
|||
var endpoint1 = EndpointFactory.CreateRouteEndpoint(
|
||||
"Home/Index/{id}",
|
||||
defaults: new { controller = "Home", action = "Index", },
|
||||
metadata: new[] { new RouteValuesAddressMetadata(routeName: null, new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
var endpoint2 = EndpointFactory.CreateRouteEndpoint(
|
||||
"Home/Index/{id?}",
|
||||
defaults: new { controller = "Home", action = "Index", },
|
||||
metadata: new[] { new RouteValuesAddressMetadata(routeName: null, new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
|
||||
var linkGenerator = CreateLinkGenerator(endpoint1, endpoint2);
|
||||
|
||||
|
|
@ -145,11 +145,11 @@ namespace Microsoft.AspNetCore.Routing
|
|||
var endpoint1 = EndpointFactory.CreateRouteEndpoint(
|
||||
"Home/Index/{id}",
|
||||
defaults: new { controller = "Home", action = "Index", },
|
||||
metadata: new[] { new RouteValuesAddressMetadata(routeName: null, new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
var endpoint2 = EndpointFactory.CreateRouteEndpoint(
|
||||
"Home/Index/{id?}",
|
||||
defaults: new { controller = "Home", action = "Index", },
|
||||
metadata: new[] { new RouteValuesAddressMetadata(routeName: null, new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
|
||||
var linkGenerator = CreateLinkGenerator(endpoint1, endpoint2);
|
||||
|
||||
|
|
@ -177,11 +177,11 @@ namespace Microsoft.AspNetCore.Routing
|
|||
var endpoint1 = EndpointFactory.CreateRouteEndpoint(
|
||||
"Home/Index/{id}",
|
||||
defaults: new { controller = "Home", action = "Index", },
|
||||
metadata: new[] { new RouteValuesAddressMetadata(routeName: null, new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
var endpoint2 = EndpointFactory.CreateRouteEndpoint(
|
||||
"Home/Index/{id?}",
|
||||
defaults: new { controller = "Home", action = "Index", },
|
||||
metadata: new[] { new RouteValuesAddressMetadata(routeName: null, new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
|
||||
|
||||
var linkGenerator = CreateLinkGenerator(endpoint1, endpoint2);
|
||||
|
||||
|
|
@ -208,10 +208,10 @@ namespace Microsoft.AspNetCore.Routing
|
|||
// Arrange
|
||||
var endpoint1 = EndpointFactory.CreateRouteEndpoint(
|
||||
"{controller}/{action}/{id}",
|
||||
metadata: new[] { new RouteValuesAddressMetadata(routeName: null, new RouteValueDictionary(new { controller = "Home", action = "In?dex", })) });
|
||||
metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "In?dex", })) });
|
||||
var endpoint2 = EndpointFactory.CreateRouteEndpoint(
|
||||
"{controller}/{action}/{id?}",
|
||||
metadata: new[] { new RouteValuesAddressMetadata(routeName: null, new RouteValueDictionary(new { controller = "Home", action = "In?dex", })) });
|
||||
metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "In?dex", })) });
|
||||
|
||||
var linkGenerator = CreateLinkGenerator(endpoint1, endpoint2);
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,114 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNetCore.Routing.Matching
|
||||
{
|
||||
// Note that while we don't intend for this code to be used with non-ASCII test,
|
||||
// we still call into these methods with some non-ASCII characters so that
|
||||
// we are sure of how it behaves.
|
||||
public class AsciiTest
|
||||
{
|
||||
[Fact]
|
||||
public void IsAscii_ReturnsTrueForAscii()
|
||||
{
|
||||
// Arrange
|
||||
var text = "abcd\u007F";
|
||||
|
||||
// Act
|
||||
var result = Ascii.IsAscii(text);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsAscii_ReturnsFalseForNonAscii()
|
||||
{
|
||||
// Arrange
|
||||
var text = "abcd\u0080";
|
||||
|
||||
// Act
|
||||
var result = Ascii.IsAscii(text);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
||||
// Identity
|
||||
[InlineData('c', 'c')]
|
||||
[InlineData('C', 'C')]
|
||||
[InlineData('#', '#')]
|
||||
[InlineData('\u0080', '\u0080')]
|
||||
|
||||
// Case-insensitive
|
||||
[InlineData('c', 'C')]
|
||||
public void AsciiIgnoreCaseEquals_ReturnsTrue(char x, char y)
|
||||
{
|
||||
// Arrange
|
||||
|
||||
// Act
|
||||
var result = Ascii.AsciiIgnoreCaseEquals(x, y);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
||||
// Different letter
|
||||
[InlineData('c', 'd')]
|
||||
[InlineData('C', 'D')]
|
||||
|
||||
// Non-letter + casing difference - 'a' and 'A' are 32 bits apart and so are ' ' and '@'
|
||||
[InlineData(' ', '@')]
|
||||
[InlineData('\u0080', '\u0080' + 32)] // Outside of ASCII range
|
||||
public void AsciiIgnoreCaseEquals_ReturnsFalse(char x, char y)
|
||||
{
|
||||
// Arrange
|
||||
|
||||
// Act
|
||||
var result = Ascii.AsciiIgnoreCaseEquals(x, y);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("", "", 0)]
|
||||
[InlineData("abCD", "abcF", 3)]
|
||||
[InlineData("ab#\u0080-$%", "Ab#\u0080-$%", 7)]
|
||||
public void UnsafeAsciiIgnoreCaseEquals_ReturnsTrue(string x, string y, int length)
|
||||
{
|
||||
// Arrange
|
||||
var spanX = x.AsSpan();
|
||||
var spanY = y.AsSpan();
|
||||
|
||||
// Act
|
||||
var result = Ascii.AsciiIgnoreCaseEquals(spanX, spanY, length);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("abcD", "abCE", 4)]
|
||||
[InlineData("ab#\u0080-$%", "Ab#\u0081-$%", 7)]
|
||||
public void UnsafeAsciiIgnoreCaseEquals_ReturnsFalse(string x, string y, int length)
|
||||
{
|
||||
// Arrange
|
||||
var spanX = x.AsSpan();
|
||||
var spanY = y.AsSpan();
|
||||
|
||||
// Act
|
||||
var result = Ascii.AsciiIgnoreCaseEquals(spanX, spanY, length);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
namespace Microsoft.AspNetCore.Routing.Matching
|
||||
{
|
||||
public class SingleEntryAsciiJumpTableTest : SingleEntryJumpTableTestBase
|
||||
{
|
||||
private protected override JumpTable CreateJumpTable(int defaultDestination, int exitDestination, string text, int destination)
|
||||
{
|
||||
return new SingleEntryAsciiJumpTable(defaultDestination, exitDestination, text, destination);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,62 +1,13 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNetCore.Routing.Matching
|
||||
{
|
||||
public class SingleEntryJumpTableTest
|
||||
public class SingleEntryJumpTableTest : SingleEntryJumpTableTestBase
|
||||
{
|
||||
[Fact]
|
||||
public void GetDestination_ZeroLengthSegment_JumpsToExit()
|
||||
private protected override JumpTable CreateJumpTable(int defaultDestination, int exitDestination, string text, int destination)
|
||||
{
|
||||
// Arrange
|
||||
var table = new SingleEntryJumpTable(0, 1, "text", 2);
|
||||
|
||||
// Act
|
||||
var result = table.GetDestination("ignored", new PathSegment(0, 0));
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetDestination_NonMatchingSegment_JumpsToDefault()
|
||||
{
|
||||
// Arrange
|
||||
var table = new SingleEntryJumpTable(0, 1, "text", 2);
|
||||
|
||||
// Act
|
||||
var result = table.GetDestination("text", new PathSegment(1, 2));
|
||||
|
||||
// Assert
|
||||
Assert.Equal(0, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetDestination_SegmentMatchingText_JumpsToDestination()
|
||||
{
|
||||
// Arrange
|
||||
var table = new SingleEntryJumpTable(0, 1, "text", 2);
|
||||
|
||||
// Act
|
||||
var result = table.GetDestination("some-text", new PathSegment(5, 4));
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetDestination_SegmentMatchingTextIgnoreCase_JumpsToDestination()
|
||||
{
|
||||
// Arrange
|
||||
var table = new SingleEntryJumpTable(0, 1, "text", 2);
|
||||
|
||||
// Act
|
||||
var result = table.GetDestination("some-tExt", new PathSegment(5, 4));
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, result);
|
||||
return new SingleEntryJumpTable(defaultDestination, exitDestination, text, destination);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,68 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNetCore.Routing.Matching
|
||||
{
|
||||
public abstract class SingleEntryJumpTableTestBase
|
||||
{
|
||||
private protected abstract JumpTable CreateJumpTable(
|
||||
int defaultDestination,
|
||||
int exitDestination,
|
||||
string text,
|
||||
int destination);
|
||||
|
||||
[Fact]
|
||||
public void GetDestination_ZeroLengthSegment_JumpsToExit()
|
||||
{
|
||||
// Arrange
|
||||
var table = CreateJumpTable(0, 1, "text", 2);
|
||||
|
||||
// Act
|
||||
var result = table.GetDestination("ignored", new PathSegment(0, 0));
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetDestination_NonMatchingSegment_JumpsToDefault()
|
||||
{
|
||||
// Arrange
|
||||
var table = CreateJumpTable(0, 1, "text", 2);
|
||||
|
||||
// Act
|
||||
var result = table.GetDestination("text", new PathSegment(1, 2));
|
||||
|
||||
// Assert
|
||||
Assert.Equal(0, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetDestination_SegmentMatchingText_JumpsToDestination()
|
||||
{
|
||||
// Arrange
|
||||
var table = CreateJumpTable(0, 1, "text", 2);
|
||||
|
||||
// Act
|
||||
var result = table.GetDestination("some-text", new PathSegment(5, 4));
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetDestination_SegmentMatchingTextIgnoreCase_JumpsToDestination()
|
||||
{
|
||||
// Arrange
|
||||
var table = CreateJumpTable(0, 1, "text", 2);
|
||||
|
||||
// Act
|
||||
var result = table.GetDestination("some-tExt", new PathSegment(5, 4));
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Routing
|
|||
[Fact]
|
||||
public void DebuggerToString_NoNameAndRequiredValues_ReturnsString()
|
||||
{
|
||||
var metadata = new RouteValuesAddressMetadata(null, null);
|
||||
var metadata = new RouteValuesAddressMetadata(null, new Dictionary<string, object>());
|
||||
|
||||
Assert.Equal("Name: - Required values: ", metadata.DebuggerToString());
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue