Skip to content

Commit

Permalink
only set next segment if Sequence spans multiple segments (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
shutdown256 authored and danmoseley committed Dec 26, 2019
1 parent fd662ad commit bc47321
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -649,11 +649,11 @@ internal void GetFirstSpan(out ReadOnlySpan<T> first, out SequencePosition next)
{
// Positive start and end index == ReadOnlySequenceSegment<T>
ReadOnlySequenceSegment<T> segment = (ReadOnlySequenceSegment<T>)startObject;
next = new SequencePosition(segment.Next, 0);
first = segment.Memory.Span;
if (hasMultipleSegments)
{
first = first.Slice(startIndex);
next = new SequencePosition(segment.Next, 0);
}
else
{
Expand Down
12 changes: 12 additions & 0 deletions src/libraries/System.Memory/tests/SequenceReader/ReadTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,17 @@ public void TryReadTo_SingleDelimiter()
Assert.Equal(i + 1, value);
}
}

[Fact]
public void TryReadTo_Span_At_Segments_Boundary()
{
Span<byte> delimiter = new byte[] { 13, 10 }; // \r\n
BufferSegment<byte> segment = new BufferSegment<byte>(Text.Encoding.ASCII.GetBytes("Hello\r"));
segment.Append(Text.Encoding.ASCII.GetBytes("\nWorld")); // add next segment
ReadOnlySequence<byte> inputSeq = new ReadOnlySequence<byte>(segment, 0, segment, 6); // span only the first segment!
SequenceReader<byte> sr = new SequenceReader<byte>(inputSeq);
bool r = sr.TryReadTo(out _, delimiter);
Assert.False(r);
}
}
}

0 comments on commit bc47321

Please sign in to comment.