-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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]: Expose span on SpanSplitEnumerator #109874
Comments
Assuming that While |
Assigned to @adamsitnik for further triage. |
@JeremyKuhne I am looking at the provided example: Which would be the equivalent of: static char[] Equivalent(ReadOnlySpan<char> chars)
{
SpanSplitEnumerator<char> enumerator = chars.Split('\0');
// missing enumerator.MoveNext() so the returned array would always be empty
return chars[enumerator.Current].ToArray();
} Or you would like the |
My understanding is he wants the property to be the span that was passed to Split in the first place (which is why I suggested it be called Source): ReadOnlySpan<char> str = "Hello|World";
var enumerator = str.Split('|');
Assert.True(enumerator.Source == str);
Assert.True(enumerator.MoveNext());
Assert.True(enumerator.Source == str);
Assert.True(enumerator.Current.Length == 5);
Assert.True(enumerator.MoveNext());
Assert.True(enumerator.Source == str);
Assert.True(enumerator.Current.Length == 5);
Assert.False(enumerator.MoveNext());
Assert.True(enumerator.Source == str); which lets him change private static string[] Split(ReadOnlySpan<char> source, char delim)
{
int i = 0;
foreach (var ignored in source.Split(delim))
{
i++;
}
string[] ret = new string[i];
i = 0;
foreach (Range range in source.Split(delim))
{
ret[i] = source[range].ToString();
i++;
}
} into private static string[] Split(ReadOnlySpan<char> source, char delim)
{
return source.Split(delim).ToArray();
}
private static string[] ToArray(this MemoryExtensions.SpanSplitEnumerator<char> enumerator)
{
int i = 0;
foreach (var ignored in enumerator)
{
i++;
}
string[] ret = new string[i];
i = 0;
foreach (Range range in enumerator)
{
ret[i] = enumerator.Source[range].ToString();
i++;
}
} |
@adamsitnik, @bartonjs assumption is correct. :) |
Background and motivation
Exposing it would allow writing extension methods on it, so one could do something like
Split('\0').ToArray()
.#934 #104534
API Proposal
The current API, for context:
API Usage
Alternative Designs
Could possibly expose range related methods so one could do something like
enumerator[enumerator.Current]
?Risks
None known.
The text was updated successfully, but these errors were encountered: