Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
Removed Position.End (#1988)
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofCwalina authored Dec 19, 2017
1 parent 8c9bfd6 commit 6226826
Show file tree
Hide file tree
Showing 19 changed files with 96 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ public int CopyTo(Span<byte> buffer)
public bool TryGet(ref Position position, out Memory<byte> 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<BufferSequence>();
item = sequence.Memory.Slice(0, sequence._written);
Expand All @@ -67,11 +65,9 @@ public bool TryGet(ref Position position, out ReadOnlyMemory<byte> 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<BufferSequence>();
item = sequence.WrittenMemory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static int CopyTo(this IMemoryList<byte> list, Span<byte> destination)
return copied;
}

public static Position PositionOf(this IMemoryList<byte> list, byte value)
public static Position? PositionOf(this IMemoryList<byte> list, byte value)
{
while (list != null)
{
Expand All @@ -58,7 +58,7 @@ public static Position PositionOf(this IMemoryList<byte> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,6 @@ public bool TryGet(ref Position position, out ReadOnlyMemory<byte> item, bool ad
public bool TryGet(ref Position position, out Memory<byte> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Sequences;
using System.Runtime.InteropServices;
using System.Text;

namespace System.Buffers
{
Expand Down Expand Up @@ -271,7 +272,7 @@ public Span<byte> ToSpan()

public bool TryGet(ref Position position, out ReadOnlyMemory<byte> item, bool advance = true)
{
if(position.IsEnd)
if(position == default)
{
item = default;
return false;
Expand All @@ -280,33 +281,21 @@ public bool TryGet(ref Position position, out ReadOnlyMemory<byte> 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<byte>(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<byte>;
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<IMemoryList<byte>>();
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
{
Expand All @@ -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);

}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public Span<byte> ToSpan()

public bool TryGet(ref Position position, out Memory<byte> item, bool advance = true)
{
if (position.IsEnd)
if (position == default)
{
item = default;
return false;
Expand All @@ -281,31 +281,18 @@ public bool TryGet(ref Position position, out Memory<byte> item, bool advance =
var start = _startIndex + position.Index;
var length = _endIndex - _startIndex - position.Index;
item = new Memory<byte>(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<byte>;
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<IMemoryList<byte>>();
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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static class Sequence
{
public static ReadOnlySpan<byte> ToSpan<T>(this T sequence) where T : ISequence<ReadOnlyMemory<byte>>
{
Position position = default;
Position position = sequence.First;
ResizableArray<byte> array = new ResizableArray<byte>(1024);
while (sequence.TryGet(ref position, out ReadOnlyMemory<byte> buffer))
{
Expand All @@ -26,7 +26,7 @@ public static ReadOnlySpan<byte> ToSpan<T>(this T sequence) where T : ISequence<
// be used as a type parameter.
public static long IndexOf<TSequence>(TSequence sequence, byte value) where TSequence : ISequence<ReadOnlyMemory<byte>>
{
Position position = default;
Position position = sequence.First;
int totalIndex = 0;
while (sequence.TryGet(ref position, out ReadOnlyMemory<byte> memory))
{
Expand All @@ -39,7 +39,7 @@ public static long IndexOf<TSequence>(TSequence sequence, byte value) where TSeq

public static long IndexOf<TSequence>(TSequence sequence, byte v1, byte v2) where TSequence : ISequence<ReadOnlyMemory<byte>>
{
Position position = default;
Position position = sequence.First;
int totalIndex = 0;
while (sequence.TryGet(ref position, out ReadOnlyMemory<byte> memory))
{
Expand Down Expand Up @@ -68,9 +68,9 @@ public static long IndexOf<TSequence>(TSequence sequence, byte v1, byte v2) wher
return -1;
}

public static Position PositionOf<TSequence>(this TSequence sequence, byte value) where TSequence : ISequence<ReadOnlyMemory<byte>>
public static Position? PositionOf<TSequence>(this TSequence sequence, byte value) where TSequence : ISequence<ReadOnlyMemory<byte>>
{
if (sequence == null) return Position.End;
if (sequence == null) return null;

Position position = sequence.First;
Position result = position;
Expand All @@ -84,12 +84,12 @@ public static Position PositionOf<TSequence>(this TSequence sequence, byte value
}
result = position;
}
return Position.End;
return null;
}

public static Position PositionAt<TSequence>(this TSequence sequence, long index) where TSequence : ISequence<ReadOnlyMemory<byte>>
public static Position? PositionAt<TSequence>(this TSequence sequence, long index) where TSequence : ISequence<ReadOnlyMemory<byte>>
{
if (sequence == null) return Position.End;
if (sequence == null) return null;

Position position = sequence.First;
Position result = position;
Expand All @@ -105,7 +105,7 @@ public static Position PositionAt<TSequence>(this TSequence sequence, long index
result = position;
}

return Position.End;
return null;
}

public static int Copy<TSequence>(TSequence sequence, Span<byte> buffer) where TSequence : ISequence<ReadOnlyMemory<byte>>
Expand Down Expand Up @@ -170,7 +170,7 @@ public static bool TryParse<TSequence>(TSequence sequence, out int value, out Po
return false;
}

consumed = sequence.PositionAt(consumedBytes);
consumed = sequence.PositionAt(consumedBytes).GetValueOrDefault();
return true;
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/System.Buffers.Experimental/System/Buffers/Text/BufferReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -108,7 +108,7 @@ public bool TryReadBytes(out ReadOnlyBytes bytes, byte delimiter)
public bool TryReadBytes(out ReadOnlyBytes bytes, ReadOnlySpan<byte> delimiter)
{
var range = ReadRange(delimiter);
if (range.Start.IsEnd || range.End.IsEnd)
if (range.Start == default || range.End == default)
{
bytes = default;
return false;
Expand All @@ -118,12 +118,12 @@ public bool TryReadBytes(out ReadOnlyBytes bytes, ReadOnlySpan<byte> delimiter)
}

PositionRange ReadRange(byte delimiter)
=> new PositionRange(Position, AdvanceToDelimiter(delimiter));
=> new PositionRange(Position, AdvanceToDelimiter(delimiter).GetValueOrDefault());

PositionRange ReadRange(ReadOnlySpan<byte> 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);
Expand Down Expand Up @@ -311,7 +311,7 @@ Position Position
}
}

Position AdvanceToDelimiter(byte value)
Position? AdvanceToDelimiter(byte value)
{
var unread = Unread;
var index = unread.IndexOf(value);
Expand Down Expand Up @@ -342,10 +342,10 @@ Position AdvanceToDelimiter(byte value)

_nextSegmentPosition = nextPosition;
_currentSegmentPosition = currentPosition;
return Position.End;
return null;
}

Position PositionOf(ReadOnlySpan<byte> value)
Position? PositionOf(ReadOnlySpan<byte> value)
{
var unread = Unread;
var index = unread.IndexOf(value);
Expand All @@ -359,13 +359,13 @@ Position PositionOf(ReadOnlySpan<byte> value)
{
Span<byte> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace System.Collections.Sequences
readonly int _index;

public static Position Create<T>(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);

Expand All @@ -21,15 +21,11 @@ public static Position Create<T>(T item, int index = 0) where T : class
public int Index => _index;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T GetItem<T>() => _item == null || IsEnd ? default : (T)_item;
public T GetItem<T>() => _item == null ? default : (T)_item;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (T item, int index) Get<T>() => (GetItem<T>(), (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;

Expand All @@ -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)
{
Expand Down
4 changes: 0 additions & 4 deletions src/System.IO.Pipelines.Extensions/ReadableBufferSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ public ReadableBufferSequence(ReadableBuffer buffer) : this()
public bool TryGet(ref Position position, out ReadOnlyMemory<byte> item, bool advance = true)
{
if (position == default)
{
position = First;
}
else if (position == Position.End)
{
item = default;
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ public class SequenceFormatter<TSequence> : ITextOutput where TSequence : ISeque

Position _currentPosition = default;
int _currentWrittenBytes;
Position _previousPosition = Position.End;
Position _previousPosition = default;
int _previousWrittenBytes;
int _totalWritten;

public SequenceFormatter(TSequence buffers, SymbolTable symbolTable)
{
_symbolTable = symbolTable;
_buffers = buffers;
_currentPosition = _buffers.First;
_previousWrittenBytes = -1;
}

Expand Down
Loading

0 comments on commit 6226826

Please sign in to comment.