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 Regex.EnumerateMatches #134

Merged
merged 8 commits into from
Feb 19, 2024
Merged
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
Next Next commit
.
  • Loading branch information
SimonCropp committed Feb 19, 2024
commit 7b137978cda583867bcfb7b72e730c6b8d311eb8
4 changes: 2 additions & 2 deletions src/Polyfill/Regex/RegexPolyfill.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// <auto-generated />

#pragma warning disable
#if MEMORYREFERENCED
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
@@ -14,6 +13,7 @@
#endif
static partial class RegexPolyfill
{
#if MEMORYREFERENCED
/// <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>
@@ -55,5 +55,5 @@ public static bool IsMatch(ReadOnlySpan<char> input, string pattern)
return Regex.IsMatch(input.ToString(), pattern);
#endif
}
}
#endif
}
52 changes: 52 additions & 0 deletions src/Polyfill/Regex/ValueMatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// <auto-generated />

#pragma warning disable

#if !NET7_0_OR_GREATER && (MEMORYREFERENCED || NET6_0 || NET5_0)

using System;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Link = System.ComponentModel.DescriptionAttribute;

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace System.Text.RegularExpressions;

/// <summary>
/// Represents the results from a single regular expression match.
/// </summary>
/// <remarks>
/// The <see cref="ValueMatch"/> type is immutable and has no public constructor. An instance of the <see cref="ValueMatch"/> struct is returned by the
/// <see cref="Regex.ValueMatchEnumerator.Current"/> method when iterating over the results from calling <see cref="Regex.EnumerateMatches(ReadOnlySpan{char})"/>.
/// </remarks>
public readonly ref struct ValueMatch
{
private readonly int _index;
private readonly int _length;

/// <summary>
/// Crates an instance of the <see cref="ValueMatch"/> type based on the passed in <paramref name="index"/> and <paramref name="length"/>.
/// </summary>
/// <param name="index">The position in the original span where the first character of the captured sliced span is found.</param>
/// <param name="length">The length of the captured sliced span.</param>
internal ValueMatch(int index, int length)
{
_index = index;
_length = length;
}

/// <summary>
/// Gets the position in the original span where the first character of the captured sliced span is found.
/// </summary>
public int Index => _index;

/// <summary>
/// Gets the length of the captured sliced span.
/// </summary>
public int Length => _length;
}

#endif
81 changes: 81 additions & 0 deletions src/Polyfill/Regex/ValueMatchEnumerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// <auto-generated />

#pragma warning disable

#if !NET7_0_OR_GREATER && (MEMORYREFERENCED || NET6_0 || NET5_0)

using System;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace System.Text.RegularExpressions;

/// <summary>
/// Represents an enumerator containing the set of successful matches found by iteratively applying a regular expression pattern to the input span.
/// </summary>
/// <remarks>
/// The enumerator has no public constructor. The <see cref="Regex.EnumerateMatches(ReadOnlySpan{char})"/> method returns a <see cref="Regex.ValueMatchEnumerator"/>
/// object.The enumerator will lazily iterate over zero or more <see cref="ValueMatch"/> objects. If there is at least one successful match in the span, then
/// <see cref="MoveNext"/> returns <see langword="true"/> and <see cref="Current"/> will contain the first <see cref="ValueMatch"/>. If there are no successful matches,
/// then <see cref="MoveNext"/> returns <see langword="false"/> and <see cref="Current"/> throws an <see cref="InvalidOperationException"/>.
///
/// This type is a ref struct since it stores the input span as a field in order to be able to lazily iterate over it.
/// </remarks>
public ref struct ValueMatchEnumerator
{
private readonly Regex _regex;
private readonly ReadOnlySpan<char> _input;
private ValueMatch _current;
MatchCollection matchCollection;
int index = -1;

/// <summary>
/// Creates an instance of the <see cref="ValueMatchEnumerator"/> for the passed in <paramref name="regex"/> which iterates over <paramref name="input"/>.
/// </summary>
/// <param name="regex">The <see cref="Regex"/> to use for finding matches.</param>
/// <param name="input">The input span to iterate over.</param>
/// <param name="startAt">The position where the engine should start looking for matches from.</param>
internal ValueMatchEnumerator(Regex regex, ReadOnlySpan<char> input, int startAt)
{
matchCollection = _regex.Matches(input.ToString(), startAt);
_regex = regex;
_input = input;
_current = default;
}

/// <summary>
/// Provides an enumerator that iterates through the matches in the input span.
/// </summary>
/// <returns>A copy of this enumerator.</returns>
public readonly ValueMatchEnumerator GetEnumerator() => this;

/// <summary>
/// Advances the enumerator to the next match in the span.
/// </summary>
/// <returns>
/// <see langword="true"/> if the enumerator was successfully advanced to the next element; <see langword="false"/> if the enumerator cannot find additional matches.
/// </returns>
public bool MoveNext()
{
index++;
var match = matchCollection[index];
if (match.Success)
{
_current = new ValueMatch(match.Index, match.Length);
return true;
}

return false;
}

/// <summary>
/// Gets the <see cref="ValueMatch"/> element at the current position of the enumerator.
/// </summary>
/// <exception cref="InvalidOperationException">Enumeration has either not started or has already finished.</exception>
public readonly ValueMatch Current => _current;
}

#endif