[go: nahoru, domu]

Skip to content

Commit

Permalink
fixed edge case when delimiter length is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
Guiorgy committed Feb 19, 2024
1 parent c9d777d commit 2d377c0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/Enumerators/SplitSequence/SpanSplitSequenceEnumerator.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> Delimiter;
readonly int DelimiterLength;
readonly bool DelimiterIsEmpty;
bool EnumerationDone;

/// <summary>
Expand All @@ -28,6 +29,7 @@ public SpanSplitSequenceEnumerator(ReadOnlySpan<T> source, ReadOnlySpan<T> delim
Span = source;
Delimiter = delimiter;
DelimiterLength = Delimiter.Length;
DelimiterIsEmpty = Delimiter.IsEmpty;
EnumerationDone = false;
Current = default;
}
Expand All @@ -53,7 +55,7 @@ public bool MoveNext()

int delimiterIndex = Span.IndexOf(Delimiter);

if(delimiterIndex == -1)
if(delimiterIndex == -1 || DelimiterIsEmpty)
{
EnumerationDone = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public ref struct SpanSplitSequenceStringSplitOptionsEnumerator
ReadOnlySpan<char> Span;
readonly ReadOnlySpan<char> Delimiter;
readonly int DelimiterLength;
readonly bool DelimiterIsEmpty;
readonly bool TrimEntries;
readonly bool RemoveEmptyEntries;
bool EnumerationDone;
Expand All @@ -29,7 +30,8 @@ public SpanSplitSequenceStringSplitOptionsEnumerator(ReadOnlySpan<char> source,
{
Span = source;
Delimiter = delimiter;
DelimiterLength = delimiter.Length;
DelimiterLength = Delimiter.Length;
DelimiterIsEmpty = Delimiter.IsEmpty;
TrimEntries = options.IsTrimEntriesSet();
RemoveEmptyEntries = options.IsRemoveEmptyEntriesSet();
EnumerationDone = false;
Expand Down Expand Up @@ -59,7 +61,7 @@ public bool MoveNext()
{
int delimiterIndex = Span.IndexOf(Delimiter);

if(delimiterIndex == -1)
if(delimiterIndex == -1 || DelimiterIsEmpty)
{
EnumerationDone = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public ref struct SpanSplitSequenceStringSplitOptionsWithCountEnumerator
ReadOnlySpan<char> Span;
readonly ReadOnlySpan<char> Delimiter;
readonly int DelimiterLength;
readonly bool DelimiterIsEmpty;
readonly bool TrimEntries;
readonly bool RemoveEmptyEntries;
readonly CountExceedingBehaviour CountExceedingBehaviour;
Expand All @@ -33,8 +34,9 @@ public SpanSplitSequenceStringSplitOptionsWithCountEnumerator(ReadOnlySpan<char>
{
Span = source;
Delimiter = delimiter;
DelimiterLength = delimiter.Length;
CurrentCount = count.ThrowIfNegative();
DelimiterLength = Delimiter.Length;
DelimiterIsEmpty = Delimiter.IsEmpty;
CurrentCount = DelimiterIsEmpty ? 0 : count.ThrowIfNegative();
TrimEntries = options.IsTrimEntriesSet();
RemoveEmptyEntries = options.IsRemoveEmptyEntriesSet();
CountExceedingBehaviour = countExceedingBehaviour.ThrowIfInvalid();
Expand Down Expand Up @@ -100,7 +102,7 @@ public bool MoveNext()
}
else
{
Current = delimiterIndex == -1 || CountExceedingBehaviour.IsIncludeRemainingElements() ? Span : Span[..delimiterIndex];
Current = delimiterIndex == -1 || CountExceedingBehaviour.IsIncludeRemainingElements() || DelimiterIsEmpty ? Span : Span[..delimiterIndex];
}

if(TrimEntries)
Expand Down
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> Delimiter;
readonly int DelimiterLength;
readonly bool DelimiterIsEmpty;
readonly CountExceedingBehaviour CountExceedingBehaviour;
int CurrentCount;
bool EnumerationDone;
Expand All @@ -31,8 +32,9 @@ public SpanSplitSequenceWithCountEnumerator(ReadOnlySpan<T> source, ReadOnlySpan
{
Span = source;
Delimiter = delimiter;
DelimiterLength = delimiter.Length;
CurrentCount = count.ThrowIfNegative();
DelimiterLength = Delimiter.Length;
DelimiterIsEmpty = Delimiter.IsEmpty;
CurrentCount = DelimiterIsEmpty ? 1 : count.ThrowIfNegative();
CountExceedingBehaviour = countExceedingBehaviour.ThrowIfInvalid();
EnumerationDone = count == 0;
Current = default;
Expand Down Expand Up @@ -63,7 +65,7 @@ public bool MoveNext()
{
EnumerationDone = true;

Current = delimiterIndex == -1 || CountExceedingBehaviour.IsIncludeRemainingElements() ? Span : Span[..delimiterIndex];
Current = delimiterIndex == -1 || CountExceedingBehaviour.IsIncludeRemainingElements() || DelimiterIsEmpty ? Span : Span[..delimiterIndex];

return true;
}
Expand Down

0 comments on commit 2d377c0

Please sign in to comment.