Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/Enumerators/Split/SpanSplitEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace SpanExtensions.Enumerators
{
ReadOnlySpan<T> Span;
readonly T Delimiter;
bool enumerationDone;

/// <summary>
/// Gets the element in the collection at the current position of the enumerator.
Expand All @@ -26,6 +27,7 @@ public SpanSplitEnumerator(ReadOnlySpan<T> source, T delimiter)
Span = source;
Delimiter = delimiter;
Current = default;
enumerationDone = false;
}

/// <summary>
Expand All @@ -42,16 +44,17 @@ public readonly SpanSplitEnumerator<T> GetEnumerator()
/// <returns><see langword="true"/> if the enumerator was successfully advanced to the next element; <see langword="false"/> if the enumerator has passed the end of the collection.</returns>
public bool MoveNext()
{
ReadOnlySpan<T> span = Span;
if(span.IsEmpty)
if(enumerationDone)
{
return false;
}

ReadOnlySpan<T> span = Span;
int index = span.IndexOf(Delimiter);

if(index == -1 || index >= span.Length)
{
Span = ReadOnlySpan<T>.Empty;
enumerationDone = true;
Current = span;
return true;
}
Expand Down
21 changes: 18 additions & 3 deletions src/Enumerators/Split/SpanSplitStringSplitOptionsEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public ref struct SpanSplitStringSplitOptionsEnumerator
ReadOnlySpan<char> Span;
readonly char Delimiter;
readonly StringSplitOptions Options;
bool enumerationDone;

/// <summary>
/// Gets the element in the collection at the current position of the enumerator.
Expand All @@ -28,6 +29,7 @@ public SpanSplitStringSplitOptionsEnumerator(ReadOnlySpan<char> source, char del
Delimiter = delimiter;
Options = options;
Current = default;
enumerationDone = false;
}

/// <summary>
Expand All @@ -44,16 +46,17 @@ public readonly SpanSplitStringSplitOptionsEnumerator GetEnumerator()
/// <returns><see langword="true"/> if the enumerator was successfully advanced to the next element; <see langword="false"/> if the enumerator has passed the end of the collection.</returns>
public bool MoveNext()
{
ReadOnlySpan<char> span = Span;
if(span.IsEmpty)
if(enumerationDone)
{
return false;
}

ReadOnlySpan<char> span = Span;
int index = span.IndexOf(Delimiter);

if(index == -1 || index >= span.Length)
{
Span = ReadOnlySpan<char>.Empty;
enumerationDone = true;
Current = span;
return true;
}
Expand All @@ -69,8 +72,20 @@ public bool MoveNext()
if(Current.IsEmpty)
{
Span = span[(index + 1)..];
if(Span.IsEmpty)
{
enumerationDone = true;
return false;
}
return MoveNext();
}

Span = span[(index + 1)..];
if(Span.IsEmpty)
{
enumerationDone = true;
}
return true;
}
Span = span[(index + 1)..];
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public ref struct SpanSplitStringSplitOptionsWithCountEnumerator
readonly int Count;
readonly CountExceedingBehaviour CountExceedingBehaviour;
int currentCount;
bool enumerationDone;
readonly int CountMinusOne;

/// <summary>
Expand All @@ -37,6 +38,7 @@ public SpanSplitStringSplitOptionsWithCountEnumerator(ReadOnlySpan<char> source,
Options = options;
Current = default;
currentCount = 0;
enumerationDone = false;
CountMinusOne = Math.Max(Count - 1, 0);
}

Expand All @@ -54,11 +56,12 @@ public readonly SpanSplitStringSplitOptionsWithCountEnumerator GetEnumerator()
/// <returns><see langword="true"/> if the enumerator was successfully advanced to the next element; <see langword="false"/> if the enumerator has passed the end of the collection.</returns>
public bool MoveNext()
{
ReadOnlySpan<char> span = Span;
if(span.IsEmpty)
if(enumerationDone)
{
return false;
}

ReadOnlySpan<char> span = Span;
if(currentCount == Count)
{
return false;
Expand Down Expand Up @@ -87,7 +90,7 @@ public bool MoveNext()
}
if(index == -1 || index >= span.Length)
{
Span = ReadOnlySpan<char>.Empty;
enumerationDone = true;
Current = span;
return true;
}
Expand All @@ -105,8 +108,20 @@ public bool MoveNext()
if(Current.IsEmpty)
{
Span = span[(index + 1)..];
if(Span.IsEmpty)
{
enumerationDone = true;
return false;
}
return MoveNext();
}

Span = span[(index + 1)..];
if(Span.IsEmpty)
{
enumerationDone = true;
}
return true;
}
Span = span[(index + 1)..];
return true;
Expand Down
9 changes: 6 additions & 3 deletions src/Enumerators/Split/SpanSplitWithCountEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace SpanExtensions.Enumerators
readonly int Count;
readonly CountExceedingBehaviour CountExceedingBehaviour;
int currentCount;
bool enumerationDone;
readonly int CountMinusOne;

/// <summary>
Expand All @@ -35,6 +36,7 @@ public SpanSplitWithCountEnumerator(ReadOnlySpan<T> source, T delimiter, int cou
CountExceedingBehaviour = countExceedingBehaviour;
Current = default;
currentCount = 0;
enumerationDone = false;
CountMinusOne = Math.Max(Count - 1, 0);
}

Expand All @@ -52,11 +54,12 @@ public readonly SpanSplitWithCountEnumerator<T> GetEnumerator()
/// <returns><see langword="true"/> if the enumerator was successfully advanced to the next element; <see langword="false"/> if the enumerator has passed the end of the collection.</returns>
public bool MoveNext()
{
ReadOnlySpan<T> span = Span;
if(span.IsEmpty)
if(enumerationDone)
{
return false;
}

ReadOnlySpan<T> span = Span;
if(currentCount == Count)
{
return false;
Expand Down Expand Up @@ -84,7 +87,7 @@ public bool MoveNext()
}
if(index == -1 || index >= span.Length)
{
Span = ReadOnlySpan<T>.Empty;
enumerationDone = true;
Current = span;
return true;
}
Expand Down
9 changes: 6 additions & 3 deletions src/Enumerators/SplitAny/SpanSplitAnyEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace SpanExtensions.Enumerators
{
ReadOnlySpan<T> Span;
readonly ReadOnlySpan<T> Delimiters;
bool enumerationDone;

/// <summary>
/// Gets the element in the collection at the current position of the enumerator.
Expand All @@ -27,6 +28,7 @@ public SpanSplitAnyEnumerator(ReadOnlySpan<T> source, ReadOnlySpan<T> delimiters
Span = source;
Delimiters = delimiters;
Current = default;
enumerationDone = false;
}

/// <summary>
Expand All @@ -43,16 +45,17 @@ public readonly SpanSplitAnyEnumerator<T> GetEnumerator()
/// <returns><see langword="true"/> if the enumerator was successfully advanced to the next element; <see langword="false"/> if the enumerator has passed the end of the collection.</returns>
public bool MoveNext()
{
ReadOnlySpan<T> span = Span;
if(span.IsEmpty)
if(enumerationDone)
{
return false;
}

ReadOnlySpan<T> span = Span;
int index = span.IndexOfAny(Delimiters);

if(index == -1 || index >= span.Length)
{
Span = ReadOnlySpan<T>.Empty;
enumerationDone = true;
Current = span;
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public ref struct SpanSplitAnyStringSplitOptionsEnumerator
ReadOnlySpan<char> Span;
readonly ReadOnlySpan<char> Delimiters;
readonly StringSplitOptions Options;
bool enumerationDone;

/// <summary>
/// Gets the element in the collection at the current position of the enumerator.
Expand All @@ -28,6 +29,7 @@ public SpanSplitAnyStringSplitOptionsEnumerator(ReadOnlySpan<char> source, ReadO
Delimiters = delimiters;
Options = options;
Current = default;
enumerationDone = false;
}

/// <summary>
Expand All @@ -44,16 +46,17 @@ public readonly SpanSplitAnyStringSplitOptionsEnumerator GetEnumerator()
/// <returns><see langword="true"/> if the enumerator was successfully advanced to the next element; <see langword="false"/> if the enumerator has passed the end of the collection.</returns>
public bool MoveNext()
{
ReadOnlySpan<char> span = Span;
if(span.IsEmpty)
if(enumerationDone)
{
return false;
}

ReadOnlySpan<char> span = Span;
int index = span.IndexOfAny(Delimiters);

if(index == -1 || index >= span.Length)
{
Span = ReadOnlySpan<char>.Empty;
enumerationDone = true;
Current = span;
return true;
}
Expand All @@ -70,8 +73,20 @@ public bool MoveNext()
if(Current.IsEmpty)
{
Span = span[(index + 1)..];
if(Span.IsEmpty)
{
enumerationDone = true;
return false;
}
return MoveNext();
}

Span = span[(index + 1)..];
if(Span.IsEmpty)
{
enumerationDone = true;
}
return true;
}
Span = span[(index + 1)..];
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public ref struct SpanSplitAnyStringSplitOptionsWithCountEnumerator
readonly int Count;
readonly CountExceedingBehaviour CountExceedingBehaviour;
int currentCount;
bool enumerationDone;
readonly int CountMinusOne;

/// <summary>
Expand All @@ -37,6 +38,7 @@ public SpanSplitAnyStringSplitOptionsWithCountEnumerator(ReadOnlySpan<char> sour
CountExceedingBehaviour = countExceedingBehaviour;
Current = default;
currentCount = 0;
enumerationDone = false;
CountMinusOne = Math.Max(Count - 1, 0);
}

Expand All @@ -54,11 +56,12 @@ public readonly SpanSplitAnyStringSplitOptionsWithCountEnumerator GetEnumerator(
/// <returns><see langword="true"/> if the enumerator was successfully advanced to the next element; <see langword="false"/> if the enumerator has passed the end of the collection.</returns>
public bool MoveNext()
{
ReadOnlySpan<char> span = Span;
if(span.IsEmpty)
if(enumerationDone)
{
return false;
}

ReadOnlySpan<char> span = Span;
if(currentCount == Count)
{
return false;
Expand Down Expand Up @@ -87,7 +90,7 @@ public bool MoveNext()
}
if(index == -1 || index >= span.Length)
{
Span = ReadOnlySpan<char>.Empty;
enumerationDone = true;
Current = span;
return true;
}
Expand All @@ -105,8 +108,20 @@ public bool MoveNext()
if(Current.IsEmpty)
{
Span = span[(index + 1)..];
if(Span.IsEmpty)
{
enumerationDone = true;
return false;
}
return MoveNext();
}

Span = span[(index + 1)..];
if(Span.IsEmpty)
{
enumerationDone = true;
}
return true;
}
Span = span[(index + 1)..];
return true;
Expand Down
9 changes: 6 additions & 3 deletions src/Enumerators/SplitAny/SpanSplitAnyWithCountEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace SpanExtensions.Enumerators
readonly int Count;
readonly CountExceedingBehaviour CountExceedingBehaviour;
int currentCount;
bool enumerationDone;
readonly int CountMinusOne;

/// <summary>
Expand All @@ -35,6 +36,7 @@ public SpanSplitAnyWithCountEnumerator(ReadOnlySpan<T> source, ReadOnlySpan<T> d
CountExceedingBehaviour = countExceedingBehaviour;
Current = default;
currentCount = 0;
enumerationDone = false;
CountMinusOne = Math.Max(Count - 1, 0);
}

Expand All @@ -52,11 +54,12 @@ public readonly SpanSplitAnyWithCountEnumerator<T> GetEnumerator()
/// <returns><see langword="true"/> if the enumerator was successfully advanced to the next element; <see langword="false"/> if the enumerator has passed the end of the collection.</returns>
public bool MoveNext()
{
ReadOnlySpan<T> span = Span;
if(span.IsEmpty)
if(enumerationDone)
{
return false;
}

ReadOnlySpan<T> span = Span;
if(currentCount == Count)
{
return false;
Expand Down Expand Up @@ -84,7 +87,7 @@ public bool MoveNext()
}
if(index == -1 || index >= span.Length)
{
Span = ReadOnlySpan<T>.Empty;
enumerationDone = true;
Current = span;
return true;
}
Expand Down
Loading