Skip to content

Commit

Permalink
Merge pull request #44770 from eerhardt/AlphaRegexSG-main
Browse files Browse the repository at this point in the history
Use the Regex source generator in the AlphaRouteConstraint.
  • Loading branch information
eerhardt authored Nov 16, 2022
2 parents 0a35a67 + 14762a3 commit 442ca2f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
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]*$")]
private static partial Regex GetAlphaRouteRegex();
}
5 changes: 4 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,9 @@ 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, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)]
string regexPattern)
{
if (regexPattern == null)
{
Expand Down

0 comments on commit 442ca2f

Please sign in to comment.