@@ -23,27 +23,35 @@ public static partial class MemoryExtensions
23
23
readonly SearchValues < T > SearchValues = null ! ;
24
24
#endif
25
25
26
+ int currentStartIndex ;
27
+ int currentEndIndex ;
28
+ int nextStartIndex ;
26
29
/// <summary>
27
30
/// Gets the current element of the enumeration.
28
31
/// </summary>
29
32
/// <returns>Returns a <see cref="Range"/> instance that indicates the bounds of the current element withing the source span.</returns>
30
- public Range Current { get ; internal set ; }
33
+ public readonly Range Current => new Range ( currentStartIndex , currentEndIndex ) ;
31
34
32
35
internal SpanSplitEnumerator ( ReadOnlySpan < T > source , T delimiter )
33
36
{
34
37
Span = source ;
35
38
Delimiter = delimiter ;
36
- Current = new Range ( 0 , 0 ) ;
37
39
DelimiterSpan = default ;
38
40
mode = SpanSplitEnumeratorMode . Delimiter ;
41
+ currentStartIndex = 0 ;
42
+ currentEndIndex = 0 ;
43
+ nextStartIndex = 0 ;
39
44
}
45
+
40
46
internal SpanSplitEnumerator ( ReadOnlySpan < T > source , ReadOnlySpan < T > delimiter , SpanSplitEnumeratorMode mode )
41
47
{
42
48
Span = source ;
43
49
DelimiterSpan = delimiter ;
44
- Current = new Range ( 0 , 0 ) ;
45
50
Delimiter = default ! ;
46
51
this . mode = mode ;
52
+ currentStartIndex = 0 ;
53
+ currentEndIndex = 0 ;
54
+ nextStartIndex = 0 ;
47
55
}
48
56
49
57
#if NET8_0
@@ -52,9 +60,11 @@ internal SpanSplitEnumerator(ReadOnlySpan<T> source, SearchValues<T> searchValue
52
60
Span = source ;
53
61
Delimiter = default ! ;
54
62
SearchValues = searchValues ;
55
- Current = new Range ( 0 , 0 ) ;
56
63
DelimiterSpan = default ;
57
64
mode = SpanSplitEnumeratorMode . Delimiter ;
65
+ currentStartIndex = 0 ;
66
+ currentEndIndex = 0 ;
67
+ nextStartIndex = 0 ;
58
68
}
59
69
#endif
60
70
/// <summary>
@@ -106,15 +116,20 @@ public bool MoveNext()
106
116
return false ;
107
117
}
108
118
119
+ currentStartIndex = nextStartIndex ;
120
+
109
121
if ( index < 0 )
110
122
{
111
- Current = new Range ( Span . Length , Span . Length ) ;
123
+ currentEndIndex = Span . Length ;
124
+ nextStartIndex = Span . Length ;
125
+
112
126
mode = ( SpanSplitEnumeratorMode ) ( - 1 ) ;
113
127
return true ;
114
128
}
115
129
116
- Current = new Range ( Current . End . Value + length , Current . Start . Value + index ) ;
117
-
130
+ currentEndIndex = currentStartIndex + index ;
131
+ nextStartIndex = currentEndIndex + length ;
132
+
118
133
return true ;
119
134
}
120
135
}
0 commit comments