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

Use the Regex source generator in the AlphaRouteConstraint. #44770

Merged
merged 2 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using BenchmarkDotNet.Attributes;
Expand All @@ -17,7 +17,7 @@ public class SingleRouteWithConstraintsBenchmark : EndpointRoutingBenchmarkBase
[GlobalSetup]
public void Setup()
{
var template = "Customers/Details/{category}/{region}/{id:int}";
var template = "Customers/Details/{category:alpha}/{region:alpha}/{id:int}";
var defaults = new { controller = "Customers", action = "Details" };
var requiredValues = new { controller = "Customers", action = "Details" };

Expand Down
9 changes: 7 additions & 2 deletions src/Http/Routing/src/Constraints/AlphaRouteConstraint.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.RegularExpressions;

namespace Microsoft.AspNetCore.Routing.Constraints;

/// <summary>
/// Constrains a route parameter to contain only lowercase or uppercase letters A through Z in the English alphabet.
/// </summary>
public class AlphaRouteConstraint : RegexRouteConstraint
public partial class AlphaRouteConstraint : RegexRouteConstraint
{
/// <summary>
/// Initializes a new instance of the <see cref="AlphaRouteConstraint" /> class.
/// </summary>
public AlphaRouteConstraint() : base(@"^[a-z]*$")
public AlphaRouteConstraint() : base(GetAlphaRouteRegex())
{
}

[GeneratedRegex(@"^[A-Za-z]*$", RegexOptions.CultureInvariant)]
eerhardt marked this conversation as resolved.
Show resolved Hide resolved
private static partial Regex GetAlphaRouteRegex();
}
3 changes: 2 additions & 1 deletion src/Http/Routing/src/Constraints/RegexRouteConstraint.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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.CodeAnalysis;
using System.Globalization;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -33,7 +34,7 @@ public RegexRouteConstraint(Regex regex)
/// Constructor for a <see cref="RegexRouteConstraint"/> given a <paramref name="regexPattern"/>.
/// </summary>
/// <param name="regexPattern">A string containing the regex pattern.</param>
public RegexRouteConstraint(string regexPattern)
public RegexRouteConstraint([StringSyntax(StringSyntaxAttribute.Regex)] string regexPattern)
eerhardt marked this conversation as resolved.
Show resolved Hide resolved
{
if (regexPattern == null)
{
Expand Down