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

Commit

Permalink
Prepared System.Buffer.Primitives for API Review (#1993)
Browse files Browse the repository at this point in the history
* Unified Positions

* Removed fully qualified Position names

* Added accidentally removed throw

* Removed one copy of TestBufferFactory

* Rearrange Seek Methods
  • Loading branch information
KrzysztofCwalina authored Dec 22, 2017
1 parent 1c979fa commit 252c882
Show file tree
Hide file tree
Showing 53 changed files with 437 additions and 517 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System;
using System.Buffers;
using System.Collections.Sequences;
using Position = System.Collections.Sequences.Position;

namespace Microsoft.Net
{
Expand Down Expand Up @@ -36,7 +35,7 @@ public BufferSequence(int desiredSize = DefaultBufferSize)

public long VirtualIndex => throw new NotImplementedException();

public Position First => Position.Create(this);
public Position First => new Position(this, 0);

public int CopyTo(Span<byte> buffer)
{
Expand All @@ -58,7 +57,7 @@ public bool TryGet(ref Position position, out Memory<byte> item, bool advance =

var (buffer, index) = position.Get<BufferSequence>();
item = buffer.Memory.Slice(index, buffer._written - index);
if (advance) { position = Position.Create(buffer._next); }
if (advance) { position = new Position(buffer._next, 0); }
return true;
}

Expand All @@ -72,7 +71,7 @@ public bool TryGet(ref Position position, out ReadOnlyMemory<byte> item, bool ad

var (buffer, index) = position.Get<BufferSequence>();
item = buffer.WrittenMemory.Slice(index);
if (advance) { position = Position.Create(buffer._next); }
if (advance) { position = new Position(buffer._next, 0); }
return true;
}

Expand Down
1 change: 1 addition & 0 deletions samples/System.IO.Pipelines.Samples/Framing/Codec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.IO.Pipelines.Networking.Libuv;
using System.IO.Pipelines.Text.Primitives;
using System.Buffers.Text;
using System.Collections.Sequences;

namespace System.IO.Pipelines.Samples.Framing
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.IO.Pipelines.Text.Primitives;
using System.Text.Formatting;
using System.Buffers.Text;
using System.Collections.Sequences;

namespace System.IO.Pipelines.Samples
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Buffers;
using System.Collections.Generic;
using System.Collections.Sequences;
using System.IO.Pipelines.Text.Primitives;
using Microsoft.Extensions.Primitives;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Buffers;
using System.Collections.Sequences;
using System.IO.Pipelines.Samples.Http;
using System.IO.Pipelines.Text.Primitives;

Expand Down
33 changes: 30 additions & 3 deletions src/System.Buffers.Experimental/System/Buffers/BufferExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public static int CopyTo(this IMemoryList<byte> list, Span<byte> destination)
return copied;
}

public static Collections.Sequences.Position? PositionOf(this IMemoryList<byte> list, byte value)
public static Position? PositionOf(this IMemoryList<byte> list, byte value)
{
while (list != null)
{
var current = list.Memory.Span;
var index = current.IndexOf(value);
if (index != -1) return Collections.Sequences.Position.Create(list, index);
if (index != -1) return new Position(list, index);
list = list.Next;
}
return null;
Expand Down Expand Up @@ -135,7 +135,7 @@ public static void Pipe(this IBufferOperation transformation, ReadOnlyBytes sour
ReadOnlySpan<byte> remainder = stackalloc byte[0];
Span<byte> stackSpan = stackalloc byte[stackLength];

Collections.Sequences.Position poisition = default;
Position poisition = default;
while (source.TryGet(ref poisition, out var sourceBuffer))
{
Span<byte> outputSpan = destination.GetSpan();
Expand Down Expand Up @@ -225,6 +225,33 @@ public static void Pipe(this IBufferOperation transformation, ReadOnlyBytes sour
return;
}

public static bool SequenceEqual<T>(this Memory<T> first, Memory<T> second) where T : struct, IEquatable<T>
{
return first.Span.SequenceEqual(second.Span);
}

public static bool SequenceEqual<T>(this ReadOnlyMemory<T> first, ReadOnlyMemory<T> second) where T : struct, IEquatable<T>
{
return first.Span.SequenceEqual(second.Span);
}

public static int SequenceCompareTo(this Span<byte> left, ReadOnlySpan<byte> right)
{
return SequenceCompareTo((ReadOnlySpan<byte>)left, right);
}

public static int SequenceCompareTo(this ReadOnlySpan<byte> left, ReadOnlySpan<byte> right)
{
var minLength = left.Length;
if (minLength > right.Length) minLength = right.Length;
for (int i = 0; i < minLength; i++)
{
var result = left[i].CompareTo(right[i]);
if (result != 0) return result;
}
return left.Length.CompareTo(right.Length);
}

public static bool TryIndicesOf(this Span<byte> buffer, byte value, Span<int> indices, out int numberOfIndices)
{
var length = buffer.Length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public MemoryList Append(Memory<byte> bytes)

public long VirtualIndex => _virtualIndex;

public Collections.Sequences.Position First => Collections.Sequences.Position.Create(this);
public Position First => new Position(this, 0);

public int CopyTo(Span<byte> buffer)
{
int copied = 0;
Collections.Sequences.Position position = default;
Position position = default;
var free = buffer;
while (TryGet(ref position, out ReadOnlyMemory<byte> segment, true))
{
Expand All @@ -64,14 +64,14 @@ public int CopyTo(Span<byte> buffer)
return copied;
}

public bool TryGet(ref Collections.Sequences.Position position, out ReadOnlyMemory<byte> item, bool advance = true)
public bool TryGet(ref Position position, out ReadOnlyMemory<byte> item, bool advance = true)
{
var result = TryGet(ref position, out Memory<byte> memory, advance);
item = memory;
return result;
}

public bool TryGet(ref Collections.Sequences.Position position, out Memory<byte> item, bool advance = true)
public bool TryGet(ref Position position, out Memory<byte> item, bool advance = true)
{
if (position == default)
{
Expand All @@ -81,7 +81,7 @@ public bool TryGet(ref Collections.Sequences.Position position, out Memory<byte>

var (list, index) = position.Get<MemoryList>();
item = list._data.Slice(index);
if (advance) { position = Collections.Sequences.Position.Create(list._next); }
if (advance) { position = new Position(list._next, 0); }
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public ReadOnlyBytes(IMemoryList<byte> first, IMemoryList<byte> last)
Validate();
}

public ReadOnlyBytes(Collections.Sequences.Position first, Collections.Sequences.Position last)
public ReadOnlyBytes(Position first, Position last)
{
(_start, _startIndex) = first.Get<object>();
(_end, _endIndex) = last.Get<object>();
Expand Down Expand Up @@ -134,7 +134,7 @@ public ReadOnlyBytes Slice(long index)
public ReadOnlyBytes Slice(int index)
=> Slice((long)index);

public ReadOnlyBytes Slice(Collections.Sequences.Position position)
public ReadOnlyBytes Slice(Position position)
{
var kind = Kind;
switch (kind)
Expand All @@ -143,12 +143,12 @@ public ReadOnlyBytes Slice(Collections.Sequences.Position position)
var (array, index) = position.Get<byte[]>();
return new ReadOnlyBytes(array, index, array.Length - index);
case Type.MemoryList:
return Slice(position, Collections.Sequences.Position.Create((IMemoryList<byte>)_end, _endIndex));
return Slice(position, new Position((IMemoryList<byte>)_end, _endIndex));
default: throw new NotImplementedException();
}
}

public ReadOnlyBytes Slice(Collections.Sequences.Position start, Collections.Sequences.Position end)
public ReadOnlyBytes Slice(Position start, Position end)
{
var kind = Kind;
switch (kind)
Expand Down Expand Up @@ -236,7 +236,7 @@ Type Kind
}
}

public Collections.Sequences.Position First => Collections.Sequences.Position.Create(_start, _startIndex);
public Position Start => new Position(_start, _startIndex);

public int CopyTo(Span<byte> buffer)
{
Expand All @@ -249,7 +249,7 @@ public int CopyTo(Span<byte> buffer)
return length;
}

var position = First;
var position = Start;
int copied = 0;
while (TryGet(ref position, out var memory) && buffer.Length > 0)
{
Expand All @@ -270,7 +270,7 @@ public Span<byte> ToSpan()
return array;
}

public bool TryGet(ref Collections.Sequences.Position position, out ReadOnlyMemory<byte> item, bool advance = true)
public bool TryGet(ref Position position, out ReadOnlyMemory<byte> item, bool advance = true)
{
if(position == default)
{
Expand Down Expand Up @@ -299,7 +299,7 @@ public bool TryGet(ref Collections.Sequences.Position position, out ReadOnlyMemo
}
else
{
if (advance) position = Collections.Sequences.Position.Create(node.Next);
if (advance) position = new Position(node.Next, 0);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ReadWriteBytes(IMemoryList<byte> first, IMemoryList<byte> last)
Validate();
}

public ReadWriteBytes(Collections.Sequences.Position first, Collections.Sequences.Position last)
public ReadWriteBytes(Position first, Position last)
{
(_start, _startIndex) = first.Get<object>();
(_end, _endIndex) = last.Get<object>();
Expand Down Expand Up @@ -130,7 +130,7 @@ public ReadWriteBytes Slice(long index)
public ReadWriteBytes Slice(int index)
=> Slice((long)index);

public ReadWriteBytes Slice(Collections.Sequences.Position position)
public ReadWriteBytes Slice(Position position)
{
var kind = Kind;
switch (kind)
Expand All @@ -139,12 +139,12 @@ public ReadWriteBytes Slice(Collections.Sequences.Position position)
var (array, index) = position.Get<byte[]>();
return new ReadWriteBytes(array, index, array.Length - index);
case Type.MemoryList:
return Slice(position, Collections.Sequences.Position.Create((IMemoryList<byte>)_end, _endIndex));
return Slice(position, new Position((IMemoryList<byte>)_end, _endIndex));
default: throw new NotImplementedException();
}
}

public ReadWriteBytes Slice(Collections.Sequences.Position start, Collections.Sequences.Position end)
public ReadWriteBytes Slice(Position start, Position end)
{
var kind = Kind;
switch (kind)
Expand Down Expand Up @@ -233,7 +233,7 @@ Type Kind
}
}

public Collections.Sequences.Position First => Collections.Sequences.Position.Create(_start, _startIndex);
public Position Start => new Position(_start, _startIndex);

public int CopyTo(Span<byte> buffer)
{
Expand All @@ -246,7 +246,7 @@ public int CopyTo(Span<byte> buffer)
return length;
}

var position = First;
var position = Start;
int copied = 0;
while (TryGet(ref position, out var memory) && buffer.Length > 0)
{
Expand All @@ -267,7 +267,7 @@ public Span<byte> ToSpan()
return array;
}

public bool TryGet(ref Collections.Sequences.Position position, out Memory<byte> item, bool advance = true)
public bool TryGet(ref Position position, out Memory<byte> item, bool advance = true)
{
if (position == default)
{
Expand Down Expand Up @@ -296,7 +296,7 @@ public bool TryGet(ref Collections.Sequences.Position position, out Memory<byte>
}
else
{
if (advance) position = Collections.Sequences.Position.Create(node.Next);
if (advance) position = new Position(node.Next, 0);
}
return true;
}
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>>
{
Collections.Sequences.Position position = sequence.First;
Position position = sequence.Start;
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>>
{
Collections.Sequences.Position position = sequence.First;
Position position = sequence.Start;
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>>
{
Collections.Sequences.Position position = sequence.First;
Position position = sequence.Start;
int totalIndex = 0;
while (sequence.TryGet(ref position, out ReadOnlyMemory<byte> memory))
{
Expand Down Expand Up @@ -68,12 +68,12 @@ public static long IndexOf<TSequence>(TSequence sequence, byte v1, byte v2) wher
return -1;
}

public static Collections.Sequences.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 null;

Collections.Sequences.Position position = sequence.First;
Collections.Sequences.Position result = position;
Position position = sequence.Start;
Position result = position;
while (sequence.TryGet(ref position, out ReadOnlyMemory<byte> memory))
{
var index = MemoryExtensions.IndexOf(memory.Span, value);
Expand All @@ -87,12 +87,12 @@ public static long IndexOf<TSequence>(TSequence sequence, byte v1, byte v2) wher
return null;
}

public static Collections.Sequences.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 null;

Collections.Sequences.Position position = sequence.First;
Collections.Sequences.Position result = position;
Position position = sequence.Start;
Position result = position;
while (sequence.TryGet(ref position, out ReadOnlyMemory<byte> memory))
{
var span = memory.Span;
Expand All @@ -111,7 +111,7 @@ public static long IndexOf<TSequence>(TSequence sequence, byte v1, byte v2) wher
public static int Copy<TSequence>(TSequence sequence, Span<byte> buffer) where TSequence : ISequence<ReadOnlyMemory<byte>>
{
int copied = 0;
var position = sequence.First;
var position = sequence.Start;
while (sequence.TryGet(ref position, out ReadOnlyMemory<byte> memory, true))
{
var span = memory.Span;
Expand All @@ -123,7 +123,7 @@ public static int Copy<TSequence>(TSequence sequence, Span<byte> buffer) where T
return copied;
}

public static int Copy<TSequence>(TSequence sequence, Collections.Sequences.Position from, Span<byte> buffer) where TSequence : ISequence<ReadOnlyMemory<byte>>
public static int Copy<TSequence>(TSequence sequence, Position from, Span<byte> buffer) where TSequence : ISequence<ReadOnlyMemory<byte>>
{
int copied = 0;
while (sequence.TryGet(ref from, out ReadOnlyMemory<byte> memory, true))
Expand All @@ -139,7 +139,7 @@ public static int Copy<TSequence>(TSequence sequence, Collections.Sequences.Posi

public static bool TryParse<TSequence>(TSequence sequence, out int value, out int consumed) where TSequence : ISequence<ReadOnlyMemory<byte>>
{
var position = sequence.First;
var position = sequence.Start;
if(sequence.TryGet(ref position, out ReadOnlyMemory<byte> memory))
{
var span = memory.Span;
Expand All @@ -162,7 +162,7 @@ public static bool TryParse<TSequence>(TSequence sequence, out int value, out in
return false;
}

public static bool TryParse<TSequence>(TSequence sequence, out int value, out Collections.Sequences.Position consumed) where TSequence : ISequence<ReadOnlyMemory<byte>>
public static bool TryParse<TSequence>(TSequence sequence, out int value, out Position consumed) where TSequence : ISequence<ReadOnlyMemory<byte>>
{
if(!TryParse(sequence, out value, out int consumedBytes))
{
Expand Down
Loading

0 comments on commit 252c882

Please sign in to comment.