-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
[API Proposal]: Regex.EnumerateSplits #100369
Comments
Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions |
Looking at this example, I feel that this Regex regex = ...;
ReadOnlySpan<char> input = ...;
foreach (ReadOnlySpan<char> word in regex.EnumerateSplits(input))
{
...
} |
That unnecessarily restricts what you can do if the input is a string, a |
namespace System.Collections.Generic;
public class Regex
{
public static ValueSplitEnumerator EnumerateSplits(ReadOnlySpan<char> input, [StringSyntax(StringSyntaxAttribute.Regex)] string pattern);
public static ValueSplitEnumerator EnumerateSplits(ReadOnlySpan<char> input, [StringSyntax(StringSyntaxAttribute.Regex, nameof(options))] string pattern, RegexOptions options);
public static ValueSplitEnumerator EnumerateSplits(ReadOnlySpan<char> input, [StringSyntax(StringSyntaxAttribute.Regex, nameof(options))] string pattern, RegexOptions options, TimeSpan matchTimeout);
public ValueSplitEnumerator EnumerateSplits(ReadOnlySpan<char> input);
public ValueSplitEnumerator EnumerateSplits(ReadOnlySpan<char> input, int count);
public ValueSplitEnumerator EnumerateSplits(ReadOnlySpan<char> input, int count, int startat);
public ref struct ValueSplitEnumerator
{
public readonly ValueSplitEnumerator GetEnumerator();
public bool MoveNext();
public readonly Range Current { get; }
}
} |
Background and motivation
In .NET 7, we added the EnumerateMatches methods to enable ammortized allocation-free support for matching. However, the Regex.Split method is handy for finding the gaps between matches, and using EnumerateMatches to achieve that is non-trivial; developers then use the more expensive Split.
API Proposal
API Usage
Alternative Designs
int count
are less important with EnumerateSplits, as a caller can always choose to stop iterating. However, they're included for two reasons: 1) to keep the overload shape the same with Split, so that someone calling theinput, count
overload switching to use EnumerateSplits doesn't implicitly start calling ainput, startat
overload, and 2) to keep the behavior the same for the last split, which when the count is smaller than the actual number will end up including all of the remainder of the input.Risks
No response
The text was updated successfully, but these errors were encountered: