From bc47321f0c1922408fa07e1953e5b9b5735cf7d2 Mon Sep 17 00:00:00 2001 From: Vladislav Richter Date: Fri, 27 Dec 2019 00:47:48 +0100 Subject: [PATCH] only set next segment if Sequence spans multiple segments (#276) --- .../src/System/Buffers/ReadOnlySequence.Helpers.cs | 2 +- src/libraries/System.Memory/tests/SequenceReader/ReadTo.cs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Memory/src/System/Buffers/ReadOnlySequence.Helpers.cs b/src/libraries/System.Memory/src/System/Buffers/ReadOnlySequence.Helpers.cs index a509fef..56afdd3 100644 --- a/src/libraries/System.Memory/src/System/Buffers/ReadOnlySequence.Helpers.cs +++ b/src/libraries/System.Memory/src/System/Buffers/ReadOnlySequence.Helpers.cs @@ -649,11 +649,11 @@ namespace System.Buffers { // Positive start and end index == ReadOnlySequenceSegment ReadOnlySequenceSegment segment = (ReadOnlySequenceSegment)startObject; - next = new SequencePosition(segment.Next, 0); first = segment.Memory.Span; if (hasMultipleSegments) { first = first.Slice(startIndex); + next = new SequencePosition(segment.Next, 0); } else { diff --git a/src/libraries/System.Memory/tests/SequenceReader/ReadTo.cs b/src/libraries/System.Memory/tests/SequenceReader/ReadTo.cs index f76d2a7..e69a0e6 100644 --- a/src/libraries/System.Memory/tests/SequenceReader/ReadTo.cs +++ b/src/libraries/System.Memory/tests/SequenceReader/ReadTo.cs @@ -198,5 +198,17 @@ namespace System.Memory.Tests.SequenceReader Assert.Equal(i + 1, value); } } + + [Fact] + public void TryReadTo_Span_At_Segments_Boundary() + { + Span delimiter = new byte[] { 13, 10 }; // \r\n + BufferSegment segment = new BufferSegment(Text.Encoding.ASCII.GetBytes("Hello\r")); + segment.Append(Text.Encoding.ASCII.GetBytes("\nWorld")); // add next segment + ReadOnlySequence inputSeq = new ReadOnlySequence(segment, 0, segment, 6); // span only the first segment! + SequenceReader sr = new SequenceReader(inputSeq); + bool r = sr.TryReadTo(out _, delimiter); + Assert.False(r); + } } } -- 2.7.4