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

add Static based Regex.EnumerateMatches #135

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions api_list.include.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@

#### RegexPolyfill

* `ValueMatchEnumerator EnumerateMatches(ReadOnlySpan<Char>, String)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.enumeratematches?view=net-8.0#system-text-regularexpressions-regex-enumeratematches(system-readonlyspan((system-char))-system-string))
* `ValueMatchEnumerator EnumerateMatches(ReadOnlySpan<Char>, String, RegexOptions, TimeSpan)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.enumeratematches#system-text-regularexpressions-regex-enumeratematches(system-readonlyspan((system-char))-system-string-system-text-regularexpressions-regexoptions-system-timespan))
* `ValueMatchEnumerator EnumerateMatches(ReadOnlySpan<Char>, String, RegexOptions)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.enumeratematches#system-text-regularexpressions-regex-enumeratematches(system-readonlyspan((system-char))-system-string-system-text-regularexpressions-regexoptions))
* `Boolean IsMatch(ReadOnlySpan<Char>, String, RegexOptions, TimeSpan)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.ismatch#system-text-regularexpressions-regex-ismatch(system-readonlyspan((system-char))-system-string-system-text-regularexpressions-regexoptions-system-timespan))
* `Boolean IsMatch(ReadOnlySpan<Char>, String, RegexOptions)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.ismatch#system-text-regularexpressions-regex-ismatch(system-readonlyspan((system-char))-system-string-system-text-regularexpressions-regexoptions))
* `Boolean IsMatch(ReadOnlySpan<Char>, String)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.ismatch#system-text-regularexpressions-regex-ismatch(system-readonlyspan((system-char))-system-string))
Expand Down
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,9 @@ The class `Polyfill` includes the following extension methods:

#### RegexPolyfill

* `ValueMatchEnumerator EnumerateMatches(ReadOnlySpan<Char>, String)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.enumeratematches?view=net-8.0#system-text-regularexpressions-regex-enumeratematches(system-readonlyspan((system-char))-system-string))
* `ValueMatchEnumerator EnumerateMatches(ReadOnlySpan<Char>, String, RegexOptions, TimeSpan)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.enumeratematches#system-text-regularexpressions-regex-enumeratematches(system-readonlyspan((system-char))-system-string-system-text-regularexpressions-regexoptions-system-timespan))
* `ValueMatchEnumerator EnumerateMatches(ReadOnlySpan<Char>, String, RegexOptions)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.enumeratematches#system-text-regularexpressions-regex-enumeratematches(system-readonlyspan((system-char))-system-string-system-text-regularexpressions-regexoptions))
* `Boolean IsMatch(ReadOnlySpan<Char>, String, RegexOptions, TimeSpan)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.ismatch#system-text-regularexpressions-regex-ismatch(system-readonlyspan((system-char))-system-string-system-text-regularexpressions-regexoptions-system-timespan))
* `Boolean IsMatch(ReadOnlySpan<Char>, String, RegexOptions)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.ismatch#system-text-regularexpressions-regex-ismatch(system-readonlyspan((system-char))-system-string-system-text-regularexpressions-regexoptions))
* `Boolean IsMatch(ReadOnlySpan<Char>, String)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.ismatch#system-text-regularexpressions-regex-ismatch(system-readonlyspan((system-char))-system-string))
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Version>2.5.0</Version>
<Version>2.6.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>Polyfill</PackageTags>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
Expand Down
24 changes: 0 additions & 24 deletions src/Polyfill/Regex/Polyfill_Regex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,5 @@ public static ValueMatchEnumerator EnumerateMatches (this Regex target, ReadOnly
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.enumeratematches#system-text-regularexpressions-regex-enumeratematches(system-readonlyspan((system-char))-system-int32)")]
public static ValueMatchEnumerator EnumerateMatches (this Regex target, ReadOnlySpan<char> input, int startat) =>
new(target, input, startat);
//
// /// <summary>
// /// Searches an input span for all occurrences of a regular expression and returns a Regex.ValueMatchEnumerator to iterate over the matches.
// /// </summary>
// /// <returns>A Regex.ValueMatchEnumerator to iterate over the matches.</returns>
// [Link("https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.enumeratematches#system-text-regularexpressions-regex-enumeratematches(system-readonlyspan((system-char))-system-string)")]
// public static ValueMatchEnumerator EnumerateMatches (this Regex target, ReadOnlySpan<char> input, string pattern) =>
// new(target, input, target.RightToLeft ? input.Length : 0);
//
// /// <summary>
// /// Searches an input span for all occurrences of a regular expression and returns a Regex.ValueMatchEnumerator to iterate over the matches.
// /// </summary>
// /// <returns>A Regex.ValueMatchEnumerator to iterate over the matches.</returns>
// [Link("https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.enumeratematches#system-text-regularexpressions-regex-enumeratematches(system-readonlyspan((system-char))-system-string-system-text-regularexpressions-regexoptions)")]
// public static ValueMatchEnumerator EnumerateMatches (this Regex target, ReadOnlySpan<char> input, string pattern, System.Text.RegularExpressions.RegexOptions options) =>
// new(target, input, target.RightToLeft ? input.Length : 0);
//
// /// <summary>
// /// Searches an input span for all occurrences of a regular expression and returns a Regex.ValueMatchEnumerator to iterate over the matches.
// /// </summary>
// /// <returns>A Regex.ValueMatchEnumerator to iterate over the matches.</returns>
// [Link("https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.enumeratematches#system-text-regularexpressions-regex-enumeratematches(system-readonlyspan((system-char))-system-string-system-text-regularexpressions-regexoptions-system-timespan)")]
// public static ValueMatchEnumerator EnumerateMatches (this Regex target, ReadOnlySpan<char> input, string pattern, System.Text.RegularExpressions.RegexOptions options, TimeSpan matchTimeout) =>
// new(target, input, target.RightToLeft ? input.Length : 0);
}
#endif
64 changes: 57 additions & 7 deletions src/Polyfill/Regex/RegexPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,32 @@
using System.Text.RegularExpressions;
using Link = System.ComponentModel.DescriptionAttribute;

#if HAS_SPAN
#if NET7_0_OR_GREATER
using ValueMatchEnumerator = System.Text.RegularExpressions.Regex.ValueMatchEnumerator;
#else
using ValueMatchEnumerator = System.Text.RegularExpressions.ValueMatchEnumerator;
#endif
#endif

[ExcludeFromCodeCoverage]
#if PolyPublic
public
#endif
static partial class RegexPolyfill
static partial class RegexPolyfill
{
#if HAS_SPAN
/// <summary>
/// Indicates whether the specified regular expression finds a match in the specified input span, using the specified matching options and time-out interval.
/// </summary>
/// <returns>true if the regular expression finds a match; otherwise, false.</returns>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.ismatch#system-text-regularexpressions-regex-ismatch(system-readonlyspan((system-char))-system-string-system-text-regularexpressions-regexoptions-system-timespan)")]
public static bool IsMatch(ReadOnlySpan<char> input, string pattern, RegexOptions options, TimeSpan matchTimeout)
public static bool IsMatch(ReadOnlySpan<char> input, string pattern, RegexOptions options, TimeSpan timeout)
{
#if NET7_0_OR_GREATER
return Regex.IsMatch(input, pattern, options, matchTimeout);
return Regex.IsMatch(input, pattern, options, timeout);
#else
return Regex.IsMatch(input.ToString(), pattern, options, matchTimeout);
return Regex.IsMatch(input.ToString(), pattern, options, timeout);
#endif
}

Expand All @@ -32,12 +40,12 @@ public static bool IsMatch(ReadOnlySpan<char> input, string pattern, RegexOption
/// </summary>
/// <returns>true if the regular expression finds a match; otherwise, false.</returns>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.ismatch#system-text-regularexpressions-regex-ismatch(system-readonlyspan((system-char))-system-string-system-text-regularexpressions-regexoptions)")]
public static bool IsMatch(ReadOnlySpan<char> input, string pattern, System.Text.RegularExpressions.RegexOptions options)
public static bool IsMatch(ReadOnlySpan<char> input, string pattern, RegexOptions options)
{
#if NET7_0_OR_GREATER
return Regex.IsMatch(input, pattern, options);
#else
return Regex.IsMatch(input.ToString(), pattern, options);
return Regex.IsMatch(input.ToString(), pattern, options);
#endif
}

Expand All @@ -51,7 +59,49 @@ public static bool IsMatch(ReadOnlySpan<char> input, string pattern)
#if NET7_0_OR_GREATER
return Regex.IsMatch(input, pattern);
#else
return Regex.IsMatch(input.ToString(), pattern);
return Regex.IsMatch(input.ToString(), pattern);
#endif
}

/// <summary>
/// Searches an input span for all occurrences of a regular expression and returns a Regex.ValueMatchEnumerator to iterate over the matches.
/// </summary>
/// <returns>A Regex.ValueMatchEnumerator to iterate over the matches.</returns>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.enumeratematches?view=net-8.0#system-text-regularexpressions-regex-enumeratematches(system-readonlyspan((system-char))-system-string)")]
public static ValueMatchEnumerator EnumerateMatches(ReadOnlySpan<char> input, string pattern)
{
#if NET7_0_OR_GREATER
return Regex.EnumerateMatches(input, pattern);
#else
return new Regex(pattern).EnumerateMatches(input);
#endif
}

/// <summary>
/// Searches an input span for all occurrences of a regular expression and returns a Regex.ValueMatchEnumerator to iterate over the matches.
/// </summary>
/// <returns>A Regex.ValueMatchEnumerator to iterate over the matches.</returns>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.enumeratematches#system-text-regularexpressions-regex-enumeratematches(system-readonlyspan((system-char))-system-string-system-text-regularexpressions-regexoptions-system-timespan)")]
public static ValueMatchEnumerator EnumerateMatches(ReadOnlySpan<char> input, string pattern, RegexOptions options, TimeSpan timeout)
{
#if NET7_0_OR_GREATER
return Regex.EnumerateMatches(input, pattern, options, timeout);
#else
return new Regex(pattern, options, timeout).EnumerateMatches(input);
#endif
}

/// <summary>
/// Searches an input span for all occurrences of a regular expression and returns a Regex.ValueMatchEnumerator to iterate over the matches.
/// </summary>
/// <returns>A Regex.ValueMatchEnumerator to iterate over the matches.</returns>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.enumeratematches#system-text-regularexpressions-regex-enumeratematches(system-readonlyspan((system-char))-system-string-system-text-regularexpressions-regexoptions)")]
public static ValueMatchEnumerator EnumerateMatches(ReadOnlySpan<char> input, string pattern, RegexOptions options)
{
#if NET7_0_OR_GREATER
return Regex.EnumerateMatches(input, pattern);
#else
return new Regex(pattern, options).EnumerateMatches(input);
#endif
}
#endif
Expand Down
15 changes: 15 additions & 0 deletions src/Tests/PolyfillTests_Regex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,19 @@ public void EnumerateMatches()

Assert.IsTrue(found);
}

[Test]
public void EnumerateMatchesStatic()
{
var span = "a55a".AsSpan();
var found = false;
foreach (var match in RegexPolyfill.EnumerateMatches(span, @"\d+"))
{
found = true;
Assert.AreEqual(1, match.Index);
Assert.AreEqual(2, match.Length);
}

Assert.IsTrue(found);
}
}
Loading