-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Added support for splitting on ReadOnlySpan<char> #295
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
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
2da6058
Added initial implementation of SpanSplitEnumerator
bbartels e795f69
Merged SpanSplitEnumerator and SpanSplitSequenceEnumerator
bbartels 0bc1a96
Reordered .projitems entry
bbartels addae39
Exposed System.Memory API additions
bbartels d153ebd
Added SpanSplitEnumerator Tests
bbartels 3a5b758
Apply suggestions from code review
bbartels ebed669
Apply suggestions from code review
bbartels 5c5b463
Moved SpanSplitEnumerator to separate file
bbartels 97a2a44
Applied feedback to ReadOnlySpan.Split tests
bbartels 549c41b
Renamed parameters/fields
bbartels a942c87
Merged upstream changes
bbartels 4c8c6ac
Removed mistaken compile include
bbartels ea512e4
Fixed incorrect namespace on GetEnumeartor() return type
bbartels af9ae45
Applied feedback for ReadOnlySpan.Split tests
bbartels 9534d08
Added XML Documentation to public members
bbartels be144a6
Fixed cref reference to SpanSplitEnumerator in XML Documentation
bbartels a906b39
Fixed System.Memory.cs entries missing fully qualified identifier
bbartels 02520e1
Merge branch 'master' of https://github.com/dotnet/runtime
bbartels d42365c
Apply suggestions from code review
bbartels 3eb0220
Merge branch 'master' of https://github.com/bbartels/runtime
bbartels 20486ab
Moved SpanSplit Tests to correct file
bbartels 443dad6
Applied review feedback
bbartels dab7335
Removed trailing whitespace
bbartels be90021
Fixed Unit Tests
bbartels File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
260 changes: 260 additions & 0 deletions
260
src/libraries/System.Memory/tests/ReadOnlySpan/Split.char.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,260 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Linq; | ||
| using Xunit; | ||
|
|
||
| namespace System.SpanTests | ||
| { | ||
| public static partial class ReadOnlySpanTests | ||
| { | ||
| [Fact] | ||
| public static void SplitNoMatchSingleResult() | ||
| { | ||
| ReadOnlySpan<char> value = "a b"; | ||
|
|
||
| string expected = value.ToString(); | ||
| var enumerator = value.Split(','); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(expected, value[enumerator.Current].ToString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void DefaultSpanSplitEnumeratorBehavior() | ||
| { | ||
| var charSpanEnumerator = new SpanSplitEnumerator<string>(); | ||
| Assert.Equal(new Range(0, 0), charSpanEnumerator.Current); | ||
| Assert.False(charSpanEnumerator.MoveNext()); | ||
|
|
||
| // Implicit DoesNotThrow assertion | ||
| charSpanEnumerator.GetEnumerator(); | ||
|
|
||
| var stringSpanEnumerator = new SpanSplitEnumerator<string>(); | ||
| Assert.Equal(new Range(0, 0), stringSpanEnumerator.Current); | ||
| Assert.False(stringSpanEnumerator.MoveNext()); | ||
| stringSpanEnumerator.GetEnumerator(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void ValidateArguments_OverloadWithoutSeparator() | ||
| { | ||
| ReadOnlySpan<char> buffer = default; | ||
|
|
||
| SpanSplitEnumerator<char> enumerator = buffer.Split(); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(new Range(0, 0), enumerator.Current); | ||
| Assert.False(enumerator.MoveNext()); | ||
|
|
||
| buffer = ""; | ||
| enumerator = buffer.Split(); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(new Range(0, 0), enumerator.Current); | ||
| Assert.False(enumerator.MoveNext()); | ||
|
|
||
| buffer = " "; | ||
| enumerator = buffer.Split(); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(new Range(0, 0), enumerator.Current); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(new Range(1, 1), enumerator.Current); | ||
| Assert.False(enumerator.MoveNext()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void ValidateArguments_OverloadWithROSSeparator() | ||
| { | ||
| // Default buffer | ||
| ReadOnlySpan<char> buffer = default; | ||
|
|
||
| SpanSplitEnumerator<char> enumerator = buffer.Split(default(char)); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(enumerator.Current, new Range(0, 0)); | ||
| Assert.False(enumerator.MoveNext()); | ||
|
|
||
| enumerator = buffer.Split(' '); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(enumerator.Current, new Range(0, 0)); | ||
| Assert.False(enumerator.MoveNext()); | ||
|
|
||
| // Empty buffer | ||
| buffer = ""; | ||
|
|
||
| enumerator = buffer.Split(default(char)); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(enumerator.Current, new Range(0, 0)); | ||
| Assert.False(enumerator.MoveNext()); | ||
|
|
||
| enumerator = buffer.Split(' '); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(enumerator.Current, new Range(0, 0)); | ||
| Assert.False(enumerator.MoveNext()); | ||
|
|
||
| // Single whitespace buffer | ||
| buffer = " "; | ||
|
|
||
| enumerator = buffer.Split(default(char)); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.False(enumerator.MoveNext()); | ||
|
|
||
| enumerator = buffer.Split(' '); | ||
| Assert.Equal(new Range(0, 0), enumerator.Current); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(new Range(0, 0), enumerator.Current); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(new Range(1, 1), enumerator.Current); | ||
| Assert.False(enumerator.MoveNext()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void ValidateArguments_OverloadWithStringSeparator() | ||
| { | ||
| // Default buffer | ||
| ReadOnlySpan<char> buffer = default; | ||
|
|
||
| SpanSplitEnumerator<char> enumerator = buffer.Split(null); // null is treated as empty string | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(enumerator.Current, new Range(0, 0)); | ||
| Assert.False(enumerator.MoveNext()); | ||
|
|
||
| enumerator = buffer.Split(""); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(enumerator.Current, new Range(0, 0)); | ||
| Assert.False(enumerator.MoveNext()); | ||
|
|
||
| enumerator = buffer.Split(" "); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(enumerator.Current, new Range(0, 0)); | ||
| Assert.False(enumerator.MoveNext()); | ||
|
|
||
| // Empty buffer | ||
| buffer = ""; | ||
|
|
||
| enumerator = buffer.Split(null); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(enumerator.Current, new Range(0, 0)); | ||
| Assert.False(enumerator.MoveNext()); | ||
|
|
||
| enumerator = buffer.Split(""); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(enumerator.Current, new Range(0, 0)); | ||
| Assert.False(enumerator.MoveNext()); | ||
|
|
||
| enumerator = buffer.Split(" "); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(enumerator.Current, new Range(0, 0)); | ||
| Assert.False(enumerator.MoveNext()); | ||
|
|
||
| // Single whitespace buffer | ||
| buffer = " "; | ||
|
|
||
| enumerator = buffer.Split(null); // null is treated as empty string | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(enumerator.Current, new Range(0, 0)); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(enumerator.Current, new Range(1, 1)); | ||
| Assert.False(enumerator.MoveNext()); | ||
|
|
||
| enumerator = buffer.Split(""); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(enumerator.Current, new Range(0, 0)); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(enumerator.Current, new Range(1, 1)); | ||
| Assert.False(enumerator.MoveNext()); | ||
|
|
||
| enumerator = buffer.Split(" "); | ||
| Assert.Equal(enumerator.Current, new Range(0, 0)); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(enumerator.Current, new Range(0, 0)); | ||
| Assert.True(enumerator.MoveNext()); | ||
| Assert.Equal(enumerator.Current, new Range(1, 1)); | ||
| Assert.False(enumerator.MoveNext()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("", ',', new[] { "" })] | ||
| [InlineData(" ", ' ', new[] { "", "" })] | ||
| [InlineData(",", ',', new[] { "", "" })] | ||
| [InlineData(" ", ' ', new[] { "", "", "", "", "", "" })] | ||
| [InlineData(",,", ',', new[] { "", "", "" })] | ||
| [InlineData("ab", ',', new[] { "ab" })] | ||
| [InlineData("a,b", ',', new[] { "a", "b" })] | ||
| [InlineData("a,", ',', new[] { "a", "" })] | ||
| [InlineData(",b", ',', new[] { "", "b" })] | ||
| [InlineData(",a,b", ',', new[] { "", "a", "b" })] | ||
| [InlineData("a,b,", ',', new[] { "a", "b", "" })] | ||
| [InlineData("a,b,c", ',', new[] { "a", "b", "c" })] | ||
| [InlineData("a,,c", ',', new[] { "a", "", "c" })] | ||
| [InlineData(",a,b,c", ',', new[] { "", "a", "b", "c" })] | ||
| [InlineData("a,b,c,", ',', new[] { "a", "b", "c", "" })] | ||
| [InlineData(",a,b,c,", ',', new[] { "", "a", "b", "c", "" })] | ||
| [InlineData("first,second", ',', new[] { "first", "second" })] | ||
| [InlineData("first,", ',', new[] { "first", "" })] | ||
| [InlineData(",second", ',', new[] { "", "second" })] | ||
| [InlineData(",first,second", ',', new[] { "", "first", "second" })] | ||
| [InlineData("first,second,", ',', new[] { "first", "second", "" })] | ||
| [InlineData("first,second,third", ',', new[] { "first", "second", "third" })] | ||
| [InlineData("first,,third", ',', new[] { "first", "", "third" })] | ||
| [InlineData(",first,second,third", ',', new[] { "", "first", "second", "third" })] | ||
| [InlineData("first,second,third,", ',', new[] { "first", "second", "third", "" })] | ||
| [InlineData(",first,second,third,", ',', new[] { "", "first", "second", "third", "" })] | ||
| [InlineData("Foo Bar Baz", ' ', new[] { "Foo", "Bar", "Baz" })] | ||
| [InlineData("Foo Bar Baz ", ' ', new[] { "Foo", "Bar", "Baz", "" })] | ||
| [InlineData(" Foo Bar Baz ", ' ', new[] { "", "Foo", "Bar", "Baz", "" })] | ||
| [InlineData(" Foo Bar Baz ", ' ', new[] { "", "Foo", "", "Bar", "Baz", "" })] | ||
| [InlineData("Foo Baz Bar", default(char), new[] { "Foo Baz Bar" })] | ||
| [InlineData("Foo Baz \x0000 Bar", default(char), new[] { "Foo Baz ", " Bar" })] | ||
| [InlineData("Foo Baz \x0000 Bar\x0000", default(char), new[] { "Foo Baz ", " Bar", "" })] | ||
| public static void SpanSplitCharSeparator(string valueParam, char separator, string[] expectedParam) | ||
| { | ||
| char[][] expected = expectedParam.Select(x => x.ToCharArray()).ToArray(); | ||
| AssertEqual(expected, valueParam, valueParam.AsSpan().Split(separator)); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("", new[] { "" })] | ||
| [InlineData(" ", new[] { "", "" })] | ||
| [InlineData(" ", new[] { "", "", "", "", "", "" })] | ||
| [InlineData(" ", new[] { "", "", "" })] | ||
| [InlineData("ab", new[] { "ab" })] | ||
| [InlineData("a b", new[] { "a", "b" })] | ||
| [InlineData("a ", new[] { "a", "" })] | ||
| [InlineData(" b", new[] { "", "b" })] | ||
| [InlineData("Foo Bar Baz", new[] { "Foo", "Bar", "Baz" })] | ||
| [InlineData("Foo Bar Baz ", new[] { "Foo", "Bar", "Baz", "" })] | ||
| [InlineData(" Foo Bar Baz ", new[] { "", "Foo", "Bar", "Baz", "" })] | ||
| [InlineData(" Foo Bar Baz ", new[] { "", "Foo", "", "Bar", "Baz", "" })] | ||
| public static void SpanSplitDefaultCharSeparator(string valueParam, string[] expectedParam) | ||
| { | ||
| char[][] expected = expectedParam.Select(x => x.ToCharArray()).ToArray(); | ||
| AssertEqual(expected, valueParam, valueParam.AsSpan().Split()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(" Foo Bar Baz,", ", ", new[] { " Foo Bar Baz," })] | ||
| [InlineData(" Foo Bar Baz, ", ", ", new[] { " Foo Bar Baz", "" })] | ||
| [InlineData(", Foo Bar Baz, ", ", ", new[] { "", "Foo Bar Baz", "" })] | ||
| [InlineData(", Foo, Bar, Baz, ", ", ", new[] { "", "Foo", "Bar", "Baz", "" })] | ||
| [InlineData(", , Foo Bar, Baz", ", ", new[] { "", "", "Foo Bar", "Baz" })] | ||
| [InlineData(", , Foo Bar, Baz, , ", ", ", new[] { "", "", "Foo Bar", "Baz", "", "" })] | ||
| [InlineData(", , , , , ", ", ", new[] { "", "", "", "", "", "" })] | ||
| [InlineData(" ", " ", new[] { "", "", "", "", "", "" })] | ||
| [InlineData(" Foo, Bar Baz ", " ", new[] { "", "Foo, Bar", "Baz", "" })] | ||
| public static void SpanSplitStringSeparator(string valueParam, string separator, string[] expectedParam) | ||
| { | ||
| char[][] expected = expectedParam.Select(x => x.ToCharArray()).ToArray(); | ||
| AssertEqual(expected, valueParam, valueParam.AsSpan().Split(separator)); | ||
| } | ||
|
|
||
| private static void AssertEqual<T>(T[][] items, ReadOnlySpan<T> orig, SpanSplitEnumerator<T> source) where T : IEquatable<T> | ||
| { | ||
| foreach (var item in items) | ||
| { | ||
| Assert.True(source.MoveNext()); | ||
| var slice = orig[source.Current]; | ||
| Assert.Equal(item, slice.ToArray()); | ||
| } | ||
| Assert.False(source.MoveNext()); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.Split.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| namespace System | ||
| { | ||
| public static partial class MemoryExtensions | ||
| { | ||
| /// <summary> | ||
| /// Returns a type that allows for enumeration of each element within a split span | ||
| /// using a single space as a separator character. | ||
| /// </summary> | ||
| /// <param name="span">The source span to be enumerated.</param> | ||
| /// <returns>Returns a <see cref="System.SpanSplitEnumerator{T}"/>.</returns> | ||
| public static SpanSplitEnumerator<char> Split(this ReadOnlySpan<char> span) | ||
layomia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| => new SpanSplitEnumerator<char>(span, ' '); | ||
|
|
||
| /// <summary> | ||
| /// Returns a type that allows for enumeration of each element within a split span | ||
| /// using the provided separator character. | ||
| /// </summary> | ||
| /// <param name="span">The source span to be enumerated.</param> | ||
| /// <param name="separator">The separator character to be used to split the provided span.</param> | ||
| /// <returns>Returns a <see cref="System.SpanSplitEnumerator{T}"/>.</returns> | ||
| public static SpanSplitEnumerator<char> Split(this ReadOnlySpan<char> span, char separator) | ||
| => new SpanSplitEnumerator<char>(span, separator); | ||
|
|
||
| /// <summary> | ||
| /// Returns a type that allows for enumeration of each element within a split span | ||
| /// using the provided separator string. | ||
| /// </summary> | ||
| /// <param name="span">The source span to be enumerated.</param> | ||
| /// <param name="separator">The separator string to be used to split the provided span.</param> | ||
| /// <returns>Returns a <see cref="System.SpanSplitEnumerator{T}"/>.</returns> | ||
| public static SpanSplitEnumerator<char> Split(this ReadOnlySpan<char> span, string separator) | ||
| => new SpanSplitEnumerator<char>(span, separator ?? string.Empty); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.