|
1 | 1 | using System; |
2 | 2 | using System.Buffers; |
3 | | -using System.Diagnostics; |
4 | 3 | using System.Runtime.CompilerServices; |
| 4 | +using System.Runtime.InteropServices; |
5 | 5 |
|
6 | 6 | #if !NET9_0_OR_GREATER |
7 | 7 |
|
8 | 8 | namespace SpanExtensions |
9 | 9 | { |
| 10 | + |
10 | 11 | #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member |
11 | 12 | public static partial class MemoryExtensions |
12 | 13 | #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member |
13 | 14 | { |
| 15 | + static readonly char[] WhiteSpaceDelimiters = new char[] { ' ', '\t', '\n', '\v', '\f', '\r', '\u0085', '\u00A0', '\u1680', |
| 16 | + '\u2000', '\u2001', '\u2002', '\u2003', '\u2004', '\u2005', '\u2006', '\u2007', '\u2008', '\u2009', |
| 17 | + '\u200A', '\u2028', '\u2029', '\u202F', '\u205F', '\u3000' }; |
| 18 | +#if NET8_0 |
| 19 | + static readonly SearchValues<char> WhiteSpaceSearchValues = SearchValues.Create(WhiteSpaceDelimiters); |
| 20 | +#endif |
| 21 | + |
14 | 22 | /// <summary> |
15 | 23 | /// Returns a type that allows for enumeration of each element within a split span |
16 | 24 | /// using the provided separator character. |
@@ -47,6 +55,25 @@ public static SpanSplitEnumerator<T> Split<T>(this ReadOnlySpan<T> source, ReadO |
47 | 55 | /// <returns>Returns a <see cref="SpanSplitEnumerator{T}"/>.</returns> |
48 | 56 | public static SpanSplitEnumerator<T> SplitAny<T>(this ReadOnlySpan<T> source, ReadOnlySpan<T> separators) where T : IEquatable<T> |
49 | 57 | { |
| 58 | + if(separators.Length == 0 && typeof(T) == typeof(char)) |
| 59 | + { |
| 60 | +#if NET8_0 |
| 61 | + return new SpanSplitEnumerator<T>(source, Unsafe.As<SearchValues<T>>(WhiteSpaceSearchValues)); |
| 62 | +#elif NET5_0_OR_GREATER |
| 63 | + ref char data = ref MemoryMarshal.GetArrayDataReference(WhiteSpaceDelimiters); |
| 64 | + ref T convertedData = ref Unsafe.As<char, T>(ref data); |
| 65 | + separators = MemoryMarshal.CreateReadOnlySpan(ref convertedData, WhiteSpaceDelimiters.Length); |
| 66 | +#else |
| 67 | + unsafe |
| 68 | + { |
| 69 | + fixed(char* ptr = &WhiteSpaceDelimiters[0]) |
| 70 | + { |
| 71 | + separators = new ReadOnlySpan<T>(ptr, WhiteSpaceDelimiters.Length); |
| 72 | + } |
| 73 | + } |
| 74 | +#endif |
| 75 | + } |
| 76 | + |
50 | 77 | return new SpanSplitEnumerator<T>(source, separators, SpanSplitEnumeratorMode.Any); |
51 | 78 | } |
52 | 79 |
|
|
0 commit comments