Skip to content

Commit

Permalink
GeneratedRegexAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Feb 23, 2024
1 parent 9173af7 commit 472587b
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
99 changes: 99 additions & 0 deletions src/Polyfill/Regex/GeneratedRegexAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// <auto-generated />
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#pragma warning disable

#if !NET7_0_OR_GREATER

namespace System.Text.RegularExpressions;

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Link = System.ComponentModel.DescriptionAttribute;

/// <summary>
/// Instructs the System.Text.RegularExpressions source generator to generate an implementation of the specified regular expression.
/// </summary>
[ExcludeFromCodeCoverage]
[DebuggerNonUserCode]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.generatedregexattribute")]
#if PolyPublic
public
#endif
public sealed class GeneratedRegexAttribute : Attribute
{
/// <summary>Initializes a new instance of the <see cref="GeneratedRegexAttribute"/> with the specified pattern.</summary>
/// <param name="pattern">The regular expression pattern to match.</param>
public GeneratedRegexAttribute([StringSyntax(StringSyntaxAttribute.Regex)] string pattern)
: this(pattern, RegexOptions.None)
{
}

/// <summary>Initializes a new instance of the <see cref="GeneratedRegexAttribute"/> with the specified pattern and options.</summary>
/// <param name="pattern">The regular expression pattern to match.</param>
/// <param name="options">A bitwise combination of the enumeration values that modify the regular expression.</param>
public GeneratedRegexAttribute([StringSyntax(StringSyntaxAttribute.Regex, nameof(options))] string pattern, RegexOptions options)
: this(pattern, options, -1)
{
}

/// <summary>Initializes a new instance of the <see cref="GeneratedRegexAttribute"/> with the specified pattern and options.</summary>
/// <param name="pattern">The regular expression pattern to match.</param>
/// <param name="options">A bitwise combination of the enumeration values that modify the regular expression.</param>
/// <param name="cultureName">The name of a culture to be used for case sensitive comparisons. <paramref name="cultureName"/> is not case-sensitive.</param>
/// <remarks>
/// For a list of predefined culture names on Windows systems, see the Language tag column in the <see href="https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid/a9eac961-e77d-41a6-90a5-ce1a8b0cdb9c">list of
/// language/region names suported by Windows</see>. Culture names follow the standard defined by <see href="https://tools.ietf.org/html/bcp47">BCP 47</see>. In addition,
/// starting with Windows 10, <paramref name="cultureName"/> can be any valid BCP-47 language tag.
///
/// If <paramref name="cultureName"/> is <see cref="string.Empty"/>, the invariant culture will be used.
/// </remarks>
public GeneratedRegexAttribute([StringSyntax(StringSyntaxAttribute.Regex, nameof(options))] string pattern, RegexOptions options, string cultureName)
: this(pattern, options, -1, cultureName)
{
}

/// <summary>Initializes a new instance of the <see cref="GeneratedRegexAttribute"/> with the specified pattern, options, and timeout.</summary>
/// <param name="pattern">The regular expression pattern to match.</param>
/// <param name="options">A bitwise combination of the enumeration values that modify the regular expression.</param>
/// <param name="matchTimeoutMilliseconds">A time-out interval (milliseconds), or <see cref="Timeout.Infinite"/> to indicate that the method should not time out.</param>
public GeneratedRegexAttribute([StringSyntax(StringSyntaxAttribute.Regex, nameof(options))] string pattern, RegexOptions options, int matchTimeoutMilliseconds)
: this(pattern, options, matchTimeoutMilliseconds, string.Empty /* Empty string means Invariant culture */)
{
}

/// <summary>Initializes a new instance of the <see cref="GeneratedRegexAttribute"/> with the specified pattern, options, and timeout.</summary>
/// <param name="pattern">The regular expression pattern to match.</param>
/// <param name="options">A bitwise combination of the enumeration values that modify the regular expression.</param>
/// <param name="matchTimeoutMilliseconds">A time-out interval (milliseconds), or <see cref="Timeout.Infinite"/> to indicate that the method should not time out.</param>
/// <param name="cultureName">The name of a culture to be used for case sensitive comparisons. <paramref name="cultureName"/> is not case-sensitive.</param>
/// <remarks>
/// For a list of predefined culture names on Windows systems, see the Language tag column in the <see href="https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid/a9eac961-e77d-41a6-90a5-ce1a8b0cdb9c">list of
/// language/region names suported by Windows</see>. Culture names follow the standard defined by <see href="https://tools.ietf.org/html/bcp47">BCP 47</see>. In addition,
/// starting with Windows 10, <paramref name="cultureName"/> can be any valid BCP-47 language tag.
///
/// If <paramref name="cultureName"/> is <see cref="string.Empty"/>, the invariant culture will be used.
/// </remarks>
public GeneratedRegexAttribute([StringSyntax(StringSyntaxAttribute.Regex, nameof(options))] string pattern, RegexOptions options, int matchTimeoutMilliseconds, string cultureName)
{
Pattern = pattern;
Options = options;
MatchTimeoutMilliseconds = matchTimeoutMilliseconds;
CultureName = cultureName;
}

/// <summary>Gets the regular expression pattern to match.</summary>
public string Pattern { get; }

/// <summary>Gets a bitwise combination of the enumeration values that modify the regular expression.</summary>
public RegexOptions Options { get; }

/// <summary>Gets a time-out interval (milliseconds), or <see cref="Timeout.Infinite"/> to indicate that the method should not time out.</summary>
public int MatchTimeoutMilliseconds { get; }

/// <summary>Gets the name of the culture to be used for case sensitive comparisons.</summary>
public string CultureName { get; }
}
#endif
7 changes: 7 additions & 0 deletions src/Tests/PolyfillTests_Regex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public void EnumerateMatches()
Assert.IsTrue(found);
}

[GeneratedRegex("abc|def")]
private static partial Regex GeneratedRegex();

[Test]
public void GeneratedRegexTest() =>
Assert.True(GeneratedRegex().IsMatch("abc"));

[Test]
public void EnumerateMatchesStatic()
{
Expand Down

0 comments on commit 472587b

Please sign in to comment.