From 62268269ffeede5f34cd828fe03d90586239407d Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Mon, 18 Dec 2017 18:10:40 -0800 Subject: [PATCH] Removed Position.End (#1988) --- .../BufferSequence.cs | 12 +++----- .../System/Buffers/BufferExtensions.cs | 4 +-- .../System/Buffers/Sequences/MemoryList.cs | 9 ------ .../System/Buffers/Sequences/ReadOnlyBytes.cs | 30 ++++++++----------- .../Buffers/Sequences/ReadWriteBytes.cs | 19 ++---------- .../Buffers/Sequences/SequenceExtensions.cs | 20 ++++++------- .../System/Buffers/Text/BufferReader.cs | 20 ++++++------- .../System/Collections/Sequences/Position.cs | 10 ++----- .../ReadableBufferSequence.cs | 4 --- .../Formatters/SequenceFormatter.cs | 3 +- src/System.Text.Http.Parser/HttpParser.cs | 19 +++++------- tests/Benchmarks/HttpParserBench.cs | 12 ++++---- .../BytesReaderTests.cs | 6 ++-- .../ReadOnlyBytesTests.cs | 28 ++++++++--------- .../SequenceExtensionsTests.cs | 12 ++++---- .../SampleCollections/Hashtable.cs | 8 ++--- .../SampleCollections/LinkedContainer.cs | 11 ++----- .../ReadableBufferFacts.cs | 6 ++-- .../HttpParserBasicTests.cs | 5 ++-- 19 files changed, 96 insertions(+), 142 deletions(-) diff --git a/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/BufferSequence.cs b/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/BufferSequence.cs index c78b0b12a91..0d080496df3 100644 --- a/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/BufferSequence.cs +++ b/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/BufferSequence.cs @@ -51,11 +51,9 @@ public int CopyTo(Span buffer) public bool TryGet(ref Position position, out Memory item, bool advance = true) { if (position == default) { - item = Memory.Slice(0, _written); - if (advance) { position = Position.Create(_next); } - return true; + item = default; + return false; } - else if (position.IsEnd) { item = default; return false; } var sequence = position.GetItem(); item = sequence.Memory.Slice(0, sequence._written); @@ -67,11 +65,9 @@ public bool TryGet(ref Position position, out ReadOnlyMemory item, bool ad { if (position == default) { - item = Memory.Slice(0, _written); - if (advance) { position = Position.Create(_next); } - return true; + item = default; + return false; } - else if (position.IsEnd) { item = default; return false; } var sequence = position.GetItem(); item = sequence.WrittenMemory; diff --git a/src/System.Buffers.Experimental/System/Buffers/BufferExtensions.cs b/src/System.Buffers.Experimental/System/Buffers/BufferExtensions.cs index 4c3dfca3d0e..b831cfed9b7 100644 --- a/src/System.Buffers.Experimental/System/Buffers/BufferExtensions.cs +++ b/src/System.Buffers.Experimental/System/Buffers/BufferExtensions.cs @@ -49,7 +49,7 @@ public static int CopyTo(this IMemoryList list, Span destination) return copied; } - public static Position PositionOf(this IMemoryList list, byte value) + public static Position? PositionOf(this IMemoryList list, byte value) { while (list != null) { @@ -58,7 +58,7 @@ public static Position PositionOf(this IMemoryList list, byte value) if (index != -1) return Position.Create(list, index); list = list.Next; } - return Position.End; + return null; } // TODO (pri 3): I am pretty sure this whole routine can be written much better diff --git a/src/System.Buffers.Experimental/System/Buffers/Sequences/MemoryList.cs b/src/System.Buffers.Experimental/System/Buffers/Sequences/MemoryList.cs index 6ef09703bd4..c840379c775 100644 --- a/src/System.Buffers.Experimental/System/Buffers/Sequences/MemoryList.cs +++ b/src/System.Buffers.Experimental/System/Buffers/Sequences/MemoryList.cs @@ -74,15 +74,6 @@ public bool TryGet(ref Position position, out ReadOnlyMemory item, bool ad public bool TryGet(ref Position position, out Memory item, bool advance = true) { if (position == default) - { - item = _data; - if (advance) - { - position = Position.Create(_next); - } - return (!_data.IsEmpty || _next != null); - } - else if (position.IsEnd) { item = default; return false; diff --git a/src/System.Buffers.Experimental/System/Buffers/Sequences/ReadOnlyBytes.cs b/src/System.Buffers.Experimental/System/Buffers/Sequences/ReadOnlyBytes.cs index 9b0a41e9feb..9a6eac730b3 100644 --- a/src/System.Buffers.Experimental/System/Buffers/Sequences/ReadOnlyBytes.cs +++ b/src/System.Buffers.Experimental/System/Buffers/Sequences/ReadOnlyBytes.cs @@ -3,6 +3,7 @@ using System.Collections.Sequences; using System.Runtime.InteropServices; +using System.Text; namespace System.Buffers { @@ -271,7 +272,7 @@ public Span ToSpan() public bool TryGet(ref Position position, out ReadOnlyMemory item, bool advance = true) { - if(position.IsEnd) + if(position == default) { item = default; return false; @@ -280,33 +281,21 @@ public bool TryGet(ref Position position, out ReadOnlyMemory item, bool ad var array = _start as byte[]; if (array != null) { - var start = _startIndex + position.Index; - var length = _endIndex - _startIndex - position.Index; + var start = position.Index; + var length = _endIndex - position.Index; item = new ReadOnlyMemory(array, start, length); - if (advance) position = Position.End; + if (advance) position = default; return true; } if (Kind == Type.MemoryList) { - if(position == default) - { - var first = _start as IMemoryList; - item = first.Memory.Slice(_startIndex); - if (advance) position = Position.Create(first.Next); - if (ReferenceEquals(_end, _start)){ - item = item.Slice(0, (int)Length); - if (advance) position = Position.End; - } - return true; - } - var (node, index) = position.Get>(); item = node.Memory.Slice(index); if (ReferenceEquals(node, _end)) { item = item.Slice(0, _endIndex - index); - if (advance) position = Position.End; + if (advance) position = default; } else { @@ -324,6 +313,13 @@ enum Type : byte OwnedMemory, MemoryList, } + + public override string ToString() + { + var buffer = ToSpan().ToArray(); + return Encoding.UTF8.GetString(buffer, 0, buffer.Length); + + } } } diff --git a/src/System.Buffers.Experimental/System/Buffers/Sequences/ReadWriteBytes.cs b/src/System.Buffers.Experimental/System/Buffers/Sequences/ReadWriteBytes.cs index 6e63cc06df2..f1dc601a533 100644 --- a/src/System.Buffers.Experimental/System/Buffers/Sequences/ReadWriteBytes.cs +++ b/src/System.Buffers.Experimental/System/Buffers/Sequences/ReadWriteBytes.cs @@ -269,7 +269,7 @@ public Span ToSpan() public bool TryGet(ref Position position, out Memory item, bool advance = true) { - if (position.IsEnd) + if (position == default) { item = default; return false; @@ -281,31 +281,18 @@ public bool TryGet(ref Position position, out Memory item, bool advance = var start = _startIndex + position.Index; var length = _endIndex - _startIndex - position.Index; item = new Memory(array, start, length); - if (advance) position = Position.End; + if (advance) position = default; return true; } if (Kind == Type.MemoryList) { - if (position == default) - { - var first = _start as IMemoryList; - item = first.Memory.Slice(_startIndex); - if (advance) position = Position.Create(first.Next); - if (ReferenceEquals(_end, _start)) - { - item = item.Slice(0, (int)Length); - if (advance) position = Position.End; - } - return true; - } - var (node, index) = position.Get>(); item = node.Memory.Slice(index); if (ReferenceEquals(node, _end)) { item = item.Slice(0, _endIndex - index); - if (advance) position = Position.End; + if (advance) position = default; } else { diff --git a/src/System.Buffers.Experimental/System/Buffers/Sequences/SequenceExtensions.cs b/src/System.Buffers.Experimental/System/Buffers/Sequences/SequenceExtensions.cs index 485bd199c52..91356b97746 100644 --- a/src/System.Buffers.Experimental/System/Buffers/Sequences/SequenceExtensions.cs +++ b/src/System.Buffers.Experimental/System/Buffers/Sequences/SequenceExtensions.cs @@ -11,7 +11,7 @@ public static class Sequence { public static ReadOnlySpan ToSpan(this T sequence) where T : ISequence> { - Position position = default; + Position position = sequence.First; ResizableArray array = new ResizableArray(1024); while (sequence.TryGet(ref position, out ReadOnlyMemory buffer)) { @@ -26,7 +26,7 @@ public static ReadOnlySpan ToSpan(this T sequence) where T : ISequence< // be used as a type parameter. public static long IndexOf(TSequence sequence, byte value) where TSequence : ISequence> { - Position position = default; + Position position = sequence.First; int totalIndex = 0; while (sequence.TryGet(ref position, out ReadOnlyMemory memory)) { @@ -39,7 +39,7 @@ public static long IndexOf(TSequence sequence, byte value) where TSeq public static long IndexOf(TSequence sequence, byte v1, byte v2) where TSequence : ISequence> { - Position position = default; + Position position = sequence.First; int totalIndex = 0; while (sequence.TryGet(ref position, out ReadOnlyMemory memory)) { @@ -68,9 +68,9 @@ public static long IndexOf(TSequence sequence, byte v1, byte v2) wher return -1; } - public static Position PositionOf(this TSequence sequence, byte value) where TSequence : ISequence> + public static Position? PositionOf(this TSequence sequence, byte value) where TSequence : ISequence> { - if (sequence == null) return Position.End; + if (sequence == null) return null; Position position = sequence.First; Position result = position; @@ -84,12 +84,12 @@ public static Position PositionOf(this TSequence sequence, byte value } result = position; } - return Position.End; + return null; } - public static Position PositionAt(this TSequence sequence, long index) where TSequence : ISequence> + public static Position? PositionAt(this TSequence sequence, long index) where TSequence : ISequence> { - if (sequence == null) return Position.End; + if (sequence == null) return null; Position position = sequence.First; Position result = position; @@ -105,7 +105,7 @@ public static Position PositionAt(this TSequence sequence, long index result = position; } - return Position.End; + return null; } public static int Copy(TSequence sequence, Span buffer) where TSequence : ISequence> @@ -170,7 +170,7 @@ public static bool TryParse(TSequence sequence, out int value, out Po return false; } - consumed = sequence.PositionAt(consumedBytes); + consumed = sequence.PositionAt(consumedBytes).GetValueOrDefault(); return true; } } diff --git a/src/System.Buffers.Experimental/System/Buffers/Text/BufferReader.cs b/src/System.Buffers.Experimental/System/Buffers/Text/BufferReader.cs index 1ce60337203..14fc0748087 100644 --- a/src/System.Buffers.Experimental/System/Buffers/Text/BufferReader.cs +++ b/src/System.Buffers.Experimental/System/Buffers/Text/BufferReader.cs @@ -87,7 +87,7 @@ public bool TryPeek(out byte value) public bool TryReadBytes(out ReadOnlyBytes bytes, byte delimiter) { var range = ReadRange(delimiter); - if(range.Start.IsEnd || range.End.IsEnd) + if(range.Start == default || range.End == default) { bytes = default; return false; @@ -108,7 +108,7 @@ public bool TryReadBytes(out ReadOnlyBytes bytes, byte delimiter) public bool TryReadBytes(out ReadOnlyBytes bytes, ReadOnlySpan delimiter) { var range = ReadRange(delimiter); - if (range.Start.IsEnd || range.End.IsEnd) + if (range.Start == default || range.End == default) { bytes = default; return false; @@ -118,12 +118,12 @@ public bool TryReadBytes(out ReadOnlyBytes bytes, ReadOnlySpan delimiter) } PositionRange ReadRange(byte delimiter) - => new PositionRange(Position, AdvanceToDelimiter(delimiter)); + => new PositionRange(Position, AdvanceToDelimiter(delimiter).GetValueOrDefault()); PositionRange ReadRange(ReadOnlySpan delimiter) { - var range = new PositionRange(Position, PositionOf(delimiter)); - if (!range.End.IsEnd) + var range = new PositionRange(Position, PositionOf(delimiter).GetValueOrDefault()); + if (range.End != default) { Advance(range.End); Advance(delimiter.Length); @@ -311,7 +311,7 @@ Position Position } } - Position AdvanceToDelimiter(byte value) + Position? AdvanceToDelimiter(byte value) { var unread = Unread; var index = unread.IndexOf(value); @@ -342,10 +342,10 @@ Position AdvanceToDelimiter(byte value) _nextSegmentPosition = nextPosition; _currentSegmentPosition = currentPosition; - return Position.End; + return null; } - Position PositionOf(ReadOnlySpan value) + Position? PositionOf(ReadOnlySpan value) { var unread = Unread; var index = unread.IndexOf(value); @@ -359,13 +359,13 @@ Position PositionOf(ReadOnlySpan value) { Span temp = stackalloc byte[value.Length]; int copied = Sequence.Copy(_bytes, _currentSegmentPosition + _currentSpanIndex + index, temp); - if (copied < value.Length) return Position.End; + if (copied < value.Length) return null; if (temp.SequenceEqual(value)) return _currentSegmentPosition + _currentSpanIndex + index; else throw new NotImplementedException(); // need to check farther in this span } - if (unread.Length == 0) return Position.End; + if (unread.Length == 0) return null; throw new NotImplementedException(); } diff --git a/src/System.Collections.Sequences/System/Collections/Sequences/Position.cs b/src/System.Collections.Sequences/System/Collections/Sequences/Position.cs index ba2c5a495c2..098ac06e866 100644 --- a/src/System.Collections.Sequences/System/Collections/Sequences/Position.cs +++ b/src/System.Collections.Sequences/System/Collections/Sequences/Position.cs @@ -12,7 +12,7 @@ namespace System.Collections.Sequences readonly int _index; public static Position Create(T item, int index = 0) where T : class - => item == null ? End : new Position(index, item); + => item == null ? default : new Position(index, item); public static implicit operator Position(int index) => new Position(index, null); @@ -21,15 +21,11 @@ public static Position Create(T item, int index = 0) where T : class public int Index => _index; [MethodImpl(MethodImplOptions.AggressiveInlining)] - public T GetItem() => _item == null || IsEnd ? default : (T)_item; + public T GetItem() => _item == null ? default : (T)_item; [MethodImpl(MethodImplOptions.AggressiveInlining)] public (T item, int index) Get() => (GetItem(), (int)this); - public static readonly Position End = new Position(int.MaxValue, new object()); - - public bool IsEnd => this == End; - public static bool operator ==(Position left, Position right) => left.Index == right.Index && left._item == right._item; public static bool operator !=(Position left, Position right) => left.Index != right.Index || left._item != right._item; @@ -48,7 +44,7 @@ public override int GetHashCode() => [EditorBrowsable(EditorBrowsableState.Never)] public override string ToString() => - IsEnd ? "END" : _item == null ? $"{Index}" : $"{Index}, {_item}"; + this==default ? "(default)" : _item == null ? $"{Index}" : $"{Index}, {_item}"; private Position(int index, object item) { diff --git a/src/System.IO.Pipelines.Extensions/ReadableBufferSequence.cs b/src/System.IO.Pipelines.Extensions/ReadableBufferSequence.cs index 2eee1dac27d..c6d73220b53 100644 --- a/src/System.IO.Pipelines.Extensions/ReadableBufferSequence.cs +++ b/src/System.IO.Pipelines.Extensions/ReadableBufferSequence.cs @@ -19,10 +19,6 @@ public ReadableBufferSequence(ReadableBuffer buffer) : this() public bool TryGet(ref Position position, out ReadOnlyMemory item, bool advance = true) { if (position == default) - { - position = First; - } - else if (position == Position.End) { item = default; return false; diff --git a/src/System.Text.Formatting/System/Text/Formatting/Formatters/SequenceFormatter.cs b/src/System.Text.Formatting/System/Text/Formatting/Formatters/SequenceFormatter.cs index a604940a875..bc5b454315d 100644 --- a/src/System.Text.Formatting/System/Text/Formatting/Formatters/SequenceFormatter.cs +++ b/src/System.Text.Formatting/System/Text/Formatting/Formatters/SequenceFormatter.cs @@ -23,7 +23,7 @@ public class SequenceFormatter : ITextOutput where TSequence : ISeque Position _currentPosition = default; int _currentWrittenBytes; - Position _previousPosition = Position.End; + Position _previousPosition = default; int _previousWrittenBytes; int _totalWritten; @@ -31,6 +31,7 @@ public SequenceFormatter(TSequence buffers, SymbolTable symbolTable) { _symbolTable = symbolTable; _buffers = buffers; + _currentPosition = _buffers.First; _previousWrittenBytes = -1; } diff --git a/src/System.Text.Http.Parser/HttpParser.cs b/src/System.Text.Http.Parser/HttpParser.cs index ea2eb43d256..b6a4822cbe9 100644 --- a/src/System.Text.Http.Parser/HttpParser.cs +++ b/src/System.Text.Http.Parser/HttpParser.cs @@ -368,9 +368,14 @@ public unsafe bool ParseHeaders(ref T handler, ReadOnlyBytes buffer, out int { var index = 0; consumedBytes = 0; - Position position = default; + Position position = buffer.First; - ReadOnlySpan currentSpan = buffer.Memory.Span; + if(!buffer.TryGet(ref position, out ReadOnlyMemory currentMemory)) + { + consumedBytes = 0; + return false; + } + ReadOnlySpan currentSpan = currentMemory.Span; var remaining = currentSpan.Length; while (true) @@ -456,16 +461,6 @@ public unsafe bool ParseHeaders(ref T handler, ReadOnlyBytes buffer, out int } } - // This is just to get the position to be at the second segment - if (position == default) - { - if (!buffer.TryGet(ref position, out ReadOnlyMemory current)) - { - consumedBytes = 0; - return false; - } - } - if (buffer.TryGet(ref position, out var nextSegment)) { currentSpan = nextSegment.Span; diff --git a/tests/Benchmarks/HttpParserBench.cs b/tests/Benchmarks/HttpParserBench.cs index b19514eb89c..4e775bea26c 100644 --- a/tests/Benchmarks/HttpParserBench.cs +++ b/tests/Benchmarks/HttpParserBench.cs @@ -355,11 +355,11 @@ public static bool ParseRequestLine(this HttpParser parser, ref T handler, in { if(parser.ParseRequestLine(ref handler, buffer, out int consumedBytes)) { - consumed = buffer.PositionAt(consumedBytes); + consumed = buffer.PositionAt(consumedBytes).GetValueOrDefault(); examined = consumed; return true; } - consumed = buffer.PositionAt(0); + consumed = buffer.PositionAt(0).GetValueOrDefault(); examined = default; return false; } @@ -368,11 +368,11 @@ public static bool ParseHeaders(this HttpParser parser, ref T handler, in Rea { if (parser.ParseHeaders(ref handler, buffer, out consumedBytes)) { - consumed = buffer.PositionAt(consumedBytes); + consumed = buffer.PositionAt(consumedBytes).GetValueOrDefault(); examined = consumed; return true; } - consumed = buffer.PositionAt(0); + consumed = buffer.PositionAt(0).GetValueOrDefault(); examined = default; return false; } @@ -384,13 +384,13 @@ public static bool ParseRequest(this HttpParser parser, ref T handler, in Rea parser.ParseHeaders(ref handler, buffer.Slice(consumedRLBytes), out var consumedHDBytes) ) { - consumed = buffer.PositionAt(consumedRLBytes + consumedHDBytes); + consumed = buffer.PositionAt(consumedRLBytes + consumedHDBytes).GetValueOrDefault(); examined = consumed; return true; } else { - consumed = buffer.PositionAt(0); + consumed = buffer.PositionAt(0).GetValueOrDefault(); examined = default; return false; } diff --git a/tests/System.Buffers.Experimental.Tests/BytesReaderTests.cs b/tests/System.Buffers.Experimental.Tests/BytesReaderTests.cs index cc85dc610dd..d0e3f509af3 100644 --- a/tests/System.Buffers.Experimental.Tests/BytesReaderTests.cs +++ b/tests/System.Buffers.Experimental.Tests/BytesReaderTests.cs @@ -71,7 +71,7 @@ public void MultiSegmentBytesReader() var reader = BytesReader.Create(bytes); Assert.True(reader.TryReadBytes(out var ab, (byte)' ')); - Assert.Equal("AB", ab.ToString(SymbolTable.InvariantUtf8)); + Assert.Equal("AB", ab.Utf8ToString()); Assert.True(reader.TryReadBytes(out var cd, (byte)'#')); Assert.Equal("CD", cd.Utf8ToString()); @@ -172,7 +172,7 @@ public static string ToString(this TSequence bytes, SymbolTable symbo var sb = new StringBuilder(); if (symbolTable == SymbolTable.InvariantUtf8) { - Position position = default; + Position position = bytes.First; while (bytes.TryGet(ref position, out ReadOnlyMemory segment)) { sb.Append(new Utf8Span(segment.Span).ToString()); @@ -189,7 +189,7 @@ public static string Utf8ToString(this TSequence bytes) where TSequen { var sb = new StringBuilder(); - Position position = default; + Position position = bytes.First; while (bytes.TryGet(ref position, out ReadOnlyMemory segment)) { sb.Append(new Utf8Span(segment.Span).ToString()); diff --git a/tests/System.Buffers.Experimental.Tests/ReadOnlyBytesTests.cs b/tests/System.Buffers.Experimental.Tests/ReadOnlyBytesTests.cs index 4d4296e6db5..b14bc9ea93a 100644 --- a/tests/System.Buffers.Experimental.Tests/ReadOnlyBytesTests.cs +++ b/tests/System.Buffers.Experimental.Tests/ReadOnlyBytesTests.cs @@ -83,8 +83,8 @@ public void MultiSegmentPositionSlicing() ReadOnlyBytes allBytesSlice2 = allBytes.Slice(2); ReadOnlyBytes allBytesSlice3 = allBytes.Slice(3); - var positionOf3 = allBytes.PositionOf(3); - var positionOf1 = allBytes.PositionOf(1); + var positionOf3 = allBytes.PositionOf(3).GetValueOrDefault(); + var positionOf1 = allBytes.PositionOf(1).GetValueOrDefault(); // all bytes { @@ -147,8 +147,8 @@ public void SingleSegmentPositionSlicing() ReadOnlyBytes allBytesSlice = allBytes.Slice(1); - var positionOf3 = allBytes.PositionOf(3); - var positionOf1 = allBytes.PositionOf(1); + var positionOf3 = allBytes.PositionOf(3).GetValueOrDefault(); + var positionOf1 = allBytes.PositionOf(1).GetValueOrDefault(); // all bytes { @@ -183,7 +183,7 @@ public void PavelsScenarioCursorSlicing() // single segment { var rob = ListHelper.CreateRob(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }); - var c = rob.PositionOf(5); + var c = rob.PositionOf(5).GetValueOrDefault(); var slice1 = rob.Slice(2).Slice(c); var slice2 = rob.Slice(c).Slice(2); @@ -196,7 +196,7 @@ public void PavelsScenarioCursorSlicing() // multi segment { var rob = ListHelper.CreateRob(new byte[] { 1, 2, 3 }, new byte[] { 4, 5, 6, 7, 8 }); - var c = rob.PositionOf(5); + var c = rob.PositionOf(5).GetValueOrDefault(); var slice1 = rob.Slice(2).Slice(c); var slice2 = rob.Slice(c).Slice(2); @@ -213,8 +213,8 @@ public void LengthPositionSlicing() // single segment { var rob = new ReadOnlyBytes(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }); - var c2 = rob.PositionOf(2); - var c5 = rob.PositionOf(5); + var c2 = rob.PositionOf(2).GetValueOrDefault(); + var c5 = rob.PositionOf(5).GetValueOrDefault(); var slice = rob.Slice(c2, c5); @@ -225,8 +225,8 @@ public void LengthPositionSlicing() // multi segment { var rob = ListHelper.CreateRob(new byte[] { 1, 2, 3 }, new byte[] { 4, 5, 6, 7, 8 }); - var c2 = rob.PositionOf(2); - var c5 = rob.PositionOf(5); + var c2 = rob.PositionOf(2).GetValueOrDefault(); + var c5 = rob.PositionOf(5).GetValueOrDefault(); var slice = rob.Slice(c2, c5); @@ -357,7 +357,7 @@ public void ReadOnlyBytesEnumeration() { var buffer = new byte[] { 1, 2, 3, 4, 5, 6 }; var bytes = new ReadOnlyBytes(buffer); - Position position = default; + Position position = bytes.First; int length = 0; ReadOnlyMemory segment; while (bytes.TryGet(ref position, out segment)) @@ -367,7 +367,7 @@ public void ReadOnlyBytesEnumeration() Assert.Equal(buffer.Length, length); var multibytes = Parse("A|CD|EFG"); - position = default; + position = multibytes.First; length = 0; while (multibytes.TryGet(ref position, out segment)) { @@ -401,7 +401,7 @@ public void ReadOnlyTailBytesEnumeration() multibytes = multibytes.Slice(i); { - Position position = default; + Position position = multibytes.First; var length = 0; while (multibytes.TryGet(ref position, out ReadOnlyMemory segment)) { @@ -429,7 +429,7 @@ public void ReadOnlyFrontBytesEnumeration() multibytes = multibytes.Slice(0, i); { - Position position = default; + Position position = multibytes.First; var length = 0; while (multibytes.TryGet(ref position, out ReadOnlyMemory segment)) { diff --git a/tests/System.Buffers.Experimental.Tests/SequenceExtensionsTests.cs b/tests/System.Buffers.Experimental.Tests/SequenceExtensionsTests.cs index cc3531487a3..9530bce5459 100644 --- a/tests/System.Buffers.Experimental.Tests/SequenceExtensionsTests.cs +++ b/tests/System.Buffers.Experimental.Tests/SequenceExtensionsTests.cs @@ -73,16 +73,16 @@ public void SequencePositionOfMultiSegment() Assert.Equal(4, bytes.Length); // Static method call to avoid calling instance methods - Assert.Equal(Position.End, Sequence.PositionOf(bytes, 0)); + Assert.False(Sequence.PositionOf(bytes, 0).HasValue); for (int i = 0; i < bytes.Length; i++) { var value = (byte)(i + 1); - var listPosition = MemoryListExtensions.PositionOf(first, value); + var listPosition = MemoryListExtensions.PositionOf(first, value).GetValueOrDefault(); var (node, index) = listPosition.Get>(); - if (!listPosition.IsEnd) + if (listPosition != default) { Assert.Equal(value, node.Memory.Span[index]); } @@ -105,11 +105,11 @@ public void SequencePositionOfMultiSegment() } else { - Assert.Equal(Position.End, robPosition); - Assert.Equal(Position.End, robSequencePosition); + Assert.False(robPosition.HasValue); + Assert.False(robSequencePosition.HasValue); } - if (listPosition != Position.End) + if (listPosition != default) { robSlice = bytes.Slice(listPosition); Assert.Equal(value, robSlice.Memory.Span[0]); diff --git a/tests/System.Collections.Sequences.Tests/SampleCollections/Hashtable.cs b/tests/System.Collections.Sequences.Tests/SampleCollections/Hashtable.cs index 818a6eb5187..a8d90d7a376 100644 --- a/tests/System.Collections.Sequences.Tests/SampleCollections/Hashtable.cs +++ b/tests/System.Collections.Sequences.Tests/SampleCollections/Hashtable.cs @@ -93,15 +93,15 @@ public bool TryGet(ref Position position, out KeyValuePair item, bool adva { item = default; - if (_count == 0 | position == Position.End) { - position = Position.End; + if (_count == 0 | position == default) { + position = default; return false; } if (position == default) { var firstOccupiedSlot = FindFirstStartingAt(0); if (firstOccupiedSlot == -1) { - position = Position.End; + position = default; return false; } @@ -118,7 +118,7 @@ public bool TryGet(ref Position position, out KeyValuePair item, bool adva var first = FindFirstStartingAt(index + 1); position = first; if (first == -1) { - position = Position.End; + position = default; } } diff --git a/tests/System.Collections.Sequences.Tests/SampleCollections/LinkedContainer.cs b/tests/System.Collections.Sequences.Tests/SampleCollections/LinkedContainer.cs index 9441ba3bab4..a8c8bb407b8 100644 --- a/tests/System.Collections.Sequences.Tests/SampleCollections/LinkedContainer.cs +++ b/tests/System.Collections.Sequences.Tests/SampleCollections/LinkedContainer.cs @@ -30,23 +30,16 @@ public void Add(T item) public bool TryGet(ref Position position, out T item, bool advance = true) { - if(_count == 0) + if(_count == 0 || position == default) { item = default; return false; } - if (position == default) - { - item = _head._item; - if (advance) position = Position.Create(_head._next); - return _count > 0; - } - var node = position.GetItem(); if (node == null) { item = default; - position = Position.End; + position = default; return false; } diff --git a/tests/System.IO.Pipelines.Extensions.Tests/ReadableBufferFacts.cs b/tests/System.IO.Pipelines.Extensions.Tests/ReadableBufferFacts.cs index a825167c367..c8111f09578 100644 --- a/tests/System.IO.Pipelines.Extensions.Tests/ReadableBufferFacts.cs +++ b/tests/System.IO.Pipelines.Extensions.Tests/ReadableBufferFacts.cs @@ -32,7 +32,9 @@ public ReadableBufferFacts() _pipe = new Pipe(new PipeOptions(_pool)); } public void Dispose() - { GC.Collect();GC.Collect(); + { + GC.Collect(); + GC.Collect(); GC.WaitForPendingFinalizers(); _pipe.Writer.Complete(); _pipe.Reader.Complete(); @@ -43,7 +45,7 @@ public void Dispose() public void ReadableBufferSequenceWorks() { var readable = BufferUtilities.CreateBuffer(new byte[] { 1 }, new byte[] { 2, 2 }, new byte[] { 3, 3, 3 }).AsSequence(); - Position position = default; + Position position = readable.First; int spanCount = 0; while (readable.TryGet(ref position, out ReadOnlyMemory memory)) { diff --git a/tests/System.Text.Http.Parser.Tests/HttpParserBasicTests.cs b/tests/System.Text.Http.Parser.Tests/HttpParserBasicTests.cs index 0d441bc8b1f..aadbdfc699c 100644 --- a/tests/System.Text.Http.Parser.Tests/HttpParserBasicTests.cs +++ b/tests/System.Text.Http.Parser.Tests/HttpParserBasicTests.cs @@ -42,7 +42,7 @@ public void HttpParserSegmentedRob(string requestText) { var parser = new HttpParser(); - for (int pivot = 24; pivot < requestText.Length; pivot++) { + for (int pivot = 26; pivot < requestText.Length; pivot++) { var front = requestText.Substring(0, pivot); var back = requestText.Substring(pivot); @@ -58,7 +58,8 @@ public void HttpParserSegmentedRob(string requestText) Assert.True(parser.ParseRequestLine(ref request, buffer, out var consumed)); Assert.Equal(25, consumed); - Assert.True(parser.ParseHeaders(ref request, buffer.Slice(consumed), out consumed)); + var unconsumed = buffer.Slice(consumed); + Assert.True(parser.ParseHeaders(ref request, unconsumed, out consumed)); Assert.Equal(8, consumed); } catch {