Skip to content

Commit 1d7ab99

Browse files
committed
implemented .net9-style split methods for backwards-compatibility and made original split methods available only as static methods.
1 parent 0cc8423 commit 1d7ab99

File tree

6 files changed

+165
-12
lines changed

6 files changed

+165
-12
lines changed

src/Enumerators/Split/SystemSpanSplitEnumerator.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
using System.Diagnostics;
33
using System.Runtime.CompilerServices;
44

5+
#if !NET9_0_OR_GREATER
6+
57
namespace System
68
{
9+
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
710
public static partial class MemoryExtensions
11+
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
812
{
913
/// <summary>
1014
/// Enables enumerating each split within a <see cref="ReadOnlySpan{T}"/> that has been divided using one or more separators.
@@ -108,9 +112,9 @@ public bool MoveNext()
108112
mode = (SpanSplitEnumeratorMode)(-1);
109113
return true;
110114
}
111-
115+
112116
Current = new Range(Current.End.Value + length, Current.Start.Value + index);
113-
117+
114118
return true;
115119
}
116120
}
@@ -124,4 +128,6 @@ internal enum SpanSplitEnumeratorMode
124128
SearchValues
125129
}
126130
}
127-
}
131+
}
132+
133+
#endif
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System.Buffers;
2+
using System.Diagnostics;
3+
using System.Runtime.CompilerServices;
4+
5+
#if !NET9_0_OR_GREATER
6+
7+
namespace System
8+
{
9+
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
10+
public static partial class MemoryExtensions
11+
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
12+
{
13+
/// <summary>
14+
/// Returns a type that allows for enumeration of each element within a split span
15+
/// using the provided separator character.
16+
/// </summary>
17+
/// <typeparam name="T">The type of the elements.</typeparam>
18+
/// <param name="source">The source span to be enumerated.</param>
19+
/// <param name="separator">The separator character to be used to split the provided span.</param>
20+
/// <returns>Returns a <see cref="SpanSplitEnumerator{T}"/>.</returns>
21+
public static SpanSplitEnumerator<T> Split<T>(this ReadOnlySpan<T> source, T separator) where T : IEquatable<T>
22+
{
23+
return new SpanSplitEnumerator<T>(source, separator);
24+
}
25+
26+
/// <summary>
27+
/// Returns a type that allows for enumeration of each element within a split span
28+
/// using the provided separator span.
29+
/// </summary>
30+
/// <typeparam name="T">The type of the elements.</typeparam>
31+
/// <param name="source">The source span to be enumerated.</param>
32+
/// <param name="separator">The separator span to be used to split the provided span.</param>
33+
/// <returns>Returns a <see cref="SpanSplitEnumerator{T}"/>.</returns>
34+
public static SpanSplitEnumerator<T> Split<T>(this ReadOnlySpan<T> source, ReadOnlySpan<T> separator) where T : IEquatable<T>
35+
{
36+
return new SpanSplitEnumerator<T>(source, separator, SpanSplitEnumeratorMode.Sequence);
37+
}
38+
39+
/// <summary>
40+
/// Returns a type that allows for enumeration of each element within a split span
41+
/// using any of the provided elements.
42+
/// </summary>
43+
/// <typeparam name="T">The type of the elements.</typeparam>
44+
/// <param name="source">The source span to be enumerated.</param>
45+
/// <param name="separators">The separators to be used to split the provided span.</param>
46+
/// <returns>Returns a <see cref="SpanSplitEnumerator{T}"/>.</returns>
47+
public static SpanSplitEnumerator<T> SplitAny<T>(this ReadOnlySpan<T> source, ReadOnlySpan<T> separators) where T : IEquatable<T>
48+
{
49+
return new SpanSplitEnumerator<T>(source, separators, SpanSplitEnumeratorMode.Any);
50+
}
51+
52+
#if NET8_0
53+
/// <summary>
54+
/// Returns a type that allows for enumeration of each element within a split span
55+
/// using the provided <see cref="SpanSplitEnumerator{T}"/>.
56+
/// </summary>
57+
/// <typeparam name="T">The type of the elements.</typeparam>
58+
/// <param name="source">The source span to be enumerated.</param>
59+
/// <param name="separators">The <see cref="SpanSplitEnumerator{T}"/> to be used to split the provided span.</param>
60+
/// <returns>Returns a <see cref="SpanSplitEnumerator{T}"/>.</returns>
61+
/// <remarks>
62+
/// Unlike <see cref="SplitAny{T}(ReadOnlySpan{T}, ReadOnlySpan{T})"/>, the <paramref name="separators"/> is not checked for being empty.
63+
/// An empty <paramref name="separators"/> will result in no separators being found, regardless of the type of <typeparamref name="T"/>, whereas <see cref="SplitAny{T}(ReadOnlySpan{T}, ReadOnlySpan{T})"/> will use all Unicode whitespace characters as separators if <paramref name="separators"/> is empty and <typeparamref name="T"/> is <see cref="char"/>.
64+
/// </remarks>
65+
public static SpanSplitEnumerator<T> SplitAny<T>(this ReadOnlySpan<T> source, SearchValues<T> separators) where T : IEquatable<T>
66+
{
67+
return new SpanSplitEnumerator<T>(source, separators);
68+
}
69+
#endif
70+
71+
}
72+
}
73+
74+
#endif

src/Extensions/ReadOnlySpan/String/Split.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static partial class ReadOnlySpanExtensions
1313
/// <param name="source">The <see cref="ReadOnlySpan{T}"/> to be split.</param>
1414
/// <param name="delimiter">An instance of <typeparamref name="T"/> that delimits the various sub-ReadOnlySpans in <paramref name="source"/>.</param>
1515
/// <returns>An instance of the ref struct <see cref="SpanSplitEnumerator{T}"/>, which works the same way as every <see cref="IEnumerator"/> does and can be used in a foreach construct.</returns>
16-
public static SpanSplitEnumerator<T> Split<T>(this ReadOnlySpan<T> source, T delimiter) where T : IEquatable<T>
16+
public static SpanSplitEnumerator<T> Split<T>(ReadOnlySpan<T> source, T delimiter) where T : IEquatable<T>
1717
{
1818
return new SpanSplitEnumerator<T>(source, delimiter);
1919
}
@@ -27,7 +27,7 @@ public static SpanSplitEnumerator<T> Split<T>(this ReadOnlySpan<T> source, T del
2727
/// <param name="count">The maximum number of sub-ReadOnlySpans to split into.</param>
2828
/// <param name="countExceedingBehaviour">The handling of the instances more than count.</param>
2929
/// <returns>An instance of the ref struct <see cref="SpanSplitWithCountEnumerator{T}"/>, which works the same way as every <see cref="IEnumerator"/> does and can be used in a foreach construct.</returns>
30-
public static SpanSplitWithCountEnumerator<T> Split<T>(this ReadOnlySpan<T> source, T delimiter, int count, CountExceedingBehaviour countExceedingBehaviour = CountExceedingBehaviour.AppendRemainingElements) where T : IEquatable<T>
30+
public static SpanSplitWithCountEnumerator<T> Split<T>(ReadOnlySpan<T> source, T delimiter, int count, CountExceedingBehaviour countExceedingBehaviour = CountExceedingBehaviour.AppendRemainingElements) where T : IEquatable<T>
3131
{
3232
return new SpanSplitWithCountEnumerator<T>(source, delimiter, count, countExceedingBehaviour);
3333
}
@@ -39,7 +39,7 @@ public static SpanSplitWithCountEnumerator<T> Split<T>(this ReadOnlySpan<T> sour
3939
/// <param name="delimiter">A <see cref="char"/> that delimits the various sub-ReadOnlySpans in <paramref name="source"/>.</param>
4040
/// <param name="options">A bitwise combination of the enumeration values that specifies whether to trim results and include empty results.</param>
4141
/// <returns>An instance of the ref struct <see cref="SpanSplitStringSplitOptionsEnumerator"/>, which works the same way as every <see cref="IEnumerator"/> does and can be used in a foreach construct.</returns>
42-
public static SpanSplitStringSplitOptionsEnumerator Split(this ReadOnlySpan<char> source, char delimiter, StringSplitOptions options)
42+
public static SpanSplitStringSplitOptionsEnumerator Split(ReadOnlySpan<char> source, char delimiter, StringSplitOptions options)
4343
{
4444
return new SpanSplitStringSplitOptionsEnumerator(source, delimiter, options);
4545
}
@@ -53,7 +53,7 @@ public static SpanSplitStringSplitOptionsEnumerator Split(this ReadOnlySpan<char
5353
/// <param name="options">A bitwise combination of the enumeration values that specifies whether to trim results and include empty results.</param>
5454
/// <param name="countExceedingBehaviour">The handling of the instances more than count.</param>
5555
/// <returns>An instance of the ref struct <see cref="SpanSplitAnyStringSplitOptionsWithCountEnumerator"/>, which works the same way as every <see cref="IEnumerator"/> does and can be used in a foreach construct.</returns>
56-
public static SpanSplitStringSplitOptionsWithCountEnumerator Split(this ReadOnlySpan<char> source, char delimiter, int count, StringSplitOptions options, CountExceedingBehaviour countExceedingBehaviour = CountExceedingBehaviour.AppendRemainingElements)
56+
public static SpanSplitStringSplitOptionsWithCountEnumerator Split(ReadOnlySpan<char> source, char delimiter, int count, StringSplitOptions options, CountExceedingBehaviour countExceedingBehaviour = CountExceedingBehaviour.AppendRemainingElements)
5757
{
5858
return new SpanSplitStringSplitOptionsWithCountEnumerator(source, delimiter, count, options, countExceedingBehaviour);
5959
}

src/Extensions/Span/Span/Split.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System.Buffers;
2+
using System.Diagnostics;
3+
using System.Runtime.CompilerServices;
4+
5+
#if !NET9_0_OR_GREATER
6+
7+
namespace System
8+
{
9+
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
10+
public static partial class MemoryExtensions
11+
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
12+
{
13+
/// <summary>
14+
/// Returns a type that allows for enumeration of each element within a split span
15+
/// using the provided separator character.
16+
/// </summary>
17+
/// <typeparam name="T">The type of the elements.</typeparam>
18+
/// <param name="source">The source span to be enumerated.</param>
19+
/// <param name="separator">The separator character to be used to split the provided span.</param>
20+
/// <returns>Returns a <see cref="SpanSplitEnumerator{T}"/>.</returns>
21+
public static SpanSplitEnumerator<T> Split<T>(this Span<T> source, T separator) where T : IEquatable<T>
22+
{
23+
return new SpanSplitEnumerator<T>(source, separator);
24+
}
25+
26+
/// <summary>
27+
/// Returns a type that allows for enumeration of each element within a split span
28+
/// using the provided separator span.
29+
/// </summary>
30+
/// <typeparam name="T">The type of the elements.</typeparam>
31+
/// <param name="source">The source span to be enumerated.</param>
32+
/// <param name="separator">The separator span to be used to split the provided span.</param>
33+
/// <returns>Returns a <see cref="SpanSplitEnumerator{T}"/>.</returns>
34+
public static SpanSplitEnumerator<T> Split<T>(this Span<T> source, ReadOnlySpan<T> separator) where T : IEquatable<T>
35+
{
36+
return new SpanSplitEnumerator<T>(source, separator, SpanSplitEnumeratorMode.Sequence);
37+
}
38+
39+
/// <summary>
40+
/// Returns a type that allows for enumeration of each element within a split span
41+
/// using any of the provided elements.
42+
/// </summary>
43+
/// <typeparam name="T">The type of the elements.</typeparam>
44+
/// <param name="source">The source span to be enumerated.</param>
45+
/// <param name="separators">The separators to be used to split the provided span.</param>
46+
/// <returns>Returns a <see cref="SpanSplitEnumerator{T}"/>.</returns>
47+
public static SpanSplitEnumerator<T> SplitAny<T>(this Span<T> source, ReadOnlySpan<T> separators) where T : IEquatable<T>
48+
{
49+
return new SpanSplitEnumerator<T>(source, separators, SpanSplitEnumeratorMode.Any);
50+
}
51+
52+
#if NET8_0
53+
/// <summary>
54+
/// Returns a type that allows for enumeration of each element within a split span
55+
/// using the provided <see cref="SpanSplitEnumerator{T}"/>.
56+
/// </summary>
57+
/// <typeparam name="T">The type of the elements.</typeparam>
58+
/// <param name="source">The source span to be enumerated.</param>
59+
/// <param name="separators">The <see cref="SpanSplitEnumerator{T}"/> to be used to split the provided span.</param>
60+
/// <returns>Returns a <see cref="SpanSplitEnumerator{T}"/>.</returns>
61+
/// <remarks>
62+
/// Unlike <see cref="SplitAny{T}(Span{T}, ReadOnlySpan{T})"/>, the <paramref name="separators"/> is not checked for being empty.
63+
/// An empty <paramref name="separators"/> will result in no separators being found, regardless of the type of <typeparamref name="T"/>, whereas <see cref="SplitAny{T}(Span{T}, ReadOnlySpan{T})"/> will use all Unicode whitespace characters as separators if <paramref name="separators"/> is empty and <typeparamref name="T"/> is <see cref="char"/>.
64+
/// </remarks>
65+
public static SpanSplitEnumerator<T> SplitAny<T>(this Span<T> source, SearchValues<T> separators) where T : IEquatable<T>
66+
{
67+
return new SpanSplitEnumerator<T>(source, separators);
68+
}
69+
#endif
70+
71+
}
72+
}
73+
74+
#endif

src/Extensions/Span/String/Split.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ namespace SpanExtensions
66
{
77
public static partial class SpanExtensions
88
{
9-
109
/// <summary>
1110
/// Splits a <see cref="Span{T}"/> into multiple ReadOnlySpans based on the specified <paramref name="delimiter"/>.
1211
/// </summary>

src/Extensions/Span/String/SplitSequence.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static partial class SpanExtensions
1616
/// <param name="source">The <see cref="Span{T}"/> to be split.</param>
1717
/// <param name="delimiter">An instance of <see cref="ReadOnlySpan{T}"/> that delimits the various sub-ReadOnlySpans in <paramref name="source"/>.</param>
1818
/// <returns>An instance of the ref struct <see cref="SpanSplitSequenceEnumerator{T}"/>, which works the same way as every <see cref="IEnumerator"/> does and can be used in a foreach construct.</returns>
19-
public static SpanSplitSequenceEnumerator<T> Split<T>(this Span<T> source, ReadOnlySpan<T> delimiter) where T : IEquatable<T>
19+
public static SpanSplitSequenceEnumerator<T> Split<T>(Span<T> source, ReadOnlySpan<T> delimiter) where T : IEquatable<T>
2020
{
2121
return new SpanSplitSequenceEnumerator<T>(source, delimiter);
2222
}
@@ -30,7 +30,7 @@ public static SpanSplitSequenceEnumerator<T> Split<T>(this Span<T> source, ReadO
3030
/// <param name="count">The maximum number of sub-ReadOnlySpans to split into.</param>
3131
/// <param name="countExceedingBehaviour">The handling of the instances more than count.</param>
3232
/// <returns>An instance of the ref struct , which works the same way as every <see cref="IEnumerator"/> does and can be used in a foreach construct.</returns>
33-
public static SpanSplitSequenceWithCountEnumerator<T> Split<T>(this Span<T> source, ReadOnlySpan<T> delimiter, int count, CountExceedingBehaviour countExceedingBehaviour = CountExceedingBehaviour.AppendRemainingElements) where T : IEquatable<T>
33+
public static SpanSplitSequenceWithCountEnumerator<T> Split<T>(Span<T> source, ReadOnlySpan<T> delimiter, int count, CountExceedingBehaviour countExceedingBehaviour = CountExceedingBehaviour.AppendRemainingElements) where T : IEquatable<T>
3434
{
3535
return new SpanSplitSequenceWithCountEnumerator<T>(source, delimiter, count, countExceedingBehaviour);
3636
}
@@ -42,7 +42,7 @@ public static SpanSplitSequenceWithCountEnumerator<T> Split<T>(this Span<T> sour
4242
/// <param name="delimiter">An instance of <see cref="ReadOnlySpan{Char}"/> that delimits the various sub-ReadOnlySpans in <paramref name="source"/>.</param>
4343
/// <param name="options">A bitwise combination of the enumeration values that specifies whether to trim results and include empty results.</param>
4444
/// <returns>An instance of the ref struct <see cref="SpanSplitSequenceStringSplitOptionsEnumerator"/>, which works the same way as every <see cref="IEnumerator"/> does and can be used in a foreach construct.</returns>
45-
public static SpanSplitSequenceStringSplitOptionsEnumerator Split(this Span<char> source, ReadOnlySpan<char> delimiter, StringSplitOptions options)
45+
public static SpanSplitSequenceStringSplitOptionsEnumerator Split(Span<char> source, ReadOnlySpan<char> delimiter, StringSplitOptions options)
4646
{
4747
return new SpanSplitSequenceStringSplitOptionsEnumerator(source, delimiter, options);
4848
}
@@ -56,7 +56,7 @@ public static SpanSplitSequenceStringSplitOptionsEnumerator Split(this Span<char
5656
/// <param name="options">A bitwise combination of the enumeration values that specifies whether to trim results and include empty results.</param>
5757
/// <param name="countExceedingBehaviour">The handling of the instances more than count.</param>
5858
/// <returns>An instance of the ref struct <see cref="SpanSplitSequenceStringSplitOptionsWithCountEnumerator"/>, which works the same way as every <see cref="IEnumerator"/> does and can be used in a foreach construct.</returns>
59-
public static SpanSplitSequenceStringSplitOptionsWithCountEnumerator Split(this Span<char> source, ReadOnlySpan<char> delimiter, int count, StringSplitOptions options, CountExceedingBehaviour countExceedingBehaviour = CountExceedingBehaviour.AppendRemainingElements)
59+
public static SpanSplitSequenceStringSplitOptionsWithCountEnumerator Split(Span<char> source, ReadOnlySpan<char> delimiter, int count, StringSplitOptions options, CountExceedingBehaviour countExceedingBehaviour = CountExceedingBehaviour.AppendRemainingElements)
6060
{
6161
return new SpanSplitSequenceStringSplitOptionsWithCountEnumerator(source, delimiter, count, options, countExceedingBehaviour);
6262
}

0 commit comments

Comments
 (0)