Skip to content

Commit 7f7c33e

Browse files
committed
fixed bug in range-based Split methods
1 parent c819c09 commit 7f7c33e

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/Enumerators/System/SpanSplitEnumerator.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,35 @@ public static partial class MemoryExtensions
2323
readonly SearchValues<T> SearchValues = null!;
2424
#endif
2525

26+
int currentStartIndex;
27+
int currentEndIndex;
28+
int nextStartIndex;
2629
/// <summary>
2730
/// Gets the current element of the enumeration.
2831
/// </summary>
2932
/// <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);
3134

3235
internal SpanSplitEnumerator(ReadOnlySpan<T> source, T delimiter)
3336
{
3437
Span = source;
3538
Delimiter = delimiter;
36-
Current = new Range(0, 0);
3739
DelimiterSpan = default;
3840
mode = SpanSplitEnumeratorMode.Delimiter;
41+
currentStartIndex = 0;
42+
currentEndIndex = 0;
43+
nextStartIndex = 0;
3944
}
45+
4046
internal SpanSplitEnumerator(ReadOnlySpan<T> source, ReadOnlySpan<T> delimiter, SpanSplitEnumeratorMode mode)
4147
{
4248
Span = source;
4349
DelimiterSpan = delimiter;
44-
Current = new Range(0, 0);
4550
Delimiter = default!;
4651
this.mode = mode;
52+
currentStartIndex = 0;
53+
currentEndIndex = 0;
54+
nextStartIndex = 0;
4755
}
4856

4957
#if NET8_0
@@ -52,9 +60,11 @@ internal SpanSplitEnumerator(ReadOnlySpan<T> source, SearchValues<T> searchValue
5260
Span = source;
5361
Delimiter = default!;
5462
SearchValues = searchValues;
55-
Current = new Range(0, 0);
5663
DelimiterSpan = default;
5764
mode = SpanSplitEnumeratorMode.Delimiter;
65+
currentStartIndex = 0;
66+
currentEndIndex = 0;
67+
nextStartIndex = 0;
5868
}
5969
#endif
6070
/// <summary>
@@ -106,15 +116,20 @@ public bool MoveNext()
106116
return false;
107117
}
108118

119+
currentStartIndex = nextStartIndex;
120+
109121
if(index < 0)
110122
{
111-
Current = new Range(Span.Length, Span.Length);
123+
currentEndIndex = Span.Length;
124+
nextStartIndex = Span.Length;
125+
112126
mode = (SpanSplitEnumeratorMode)(-1);
113127
return true;
114128
}
115129

116-
Current = new Range(Current.End.Value + length, Current.Start.Value + index);
117-
130+
currentEndIndex = currentStartIndex + index;
131+
nextStartIndex = currentEndIndex + length;
132+
118133
return true;
119134
}
120135
}

0 commit comments

Comments
 (0)