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

Prepared System.Buffer.Primitives for API Review #1993

Merged
merged 5 commits into from
Dec 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,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 +58,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 +72,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
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static int CopyTo(this IMemoryList<byte> list, Span<byte> destination)
{
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 Collections.Sequences.Position(list, index);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System.Collections.Sequences is imported, is there a name clash?

i.e. can this be just Position?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, forgot about it. I will remove.

list = list.Next;
}
return null;
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,7 +39,7 @@ public MemoryList Append(Memory<byte> bytes)

public long VirtualIndex => _virtualIndex;

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

public int CopyTo(Span<byte> buffer)
{
Expand Down Expand Up @@ -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 Collections.Sequences.Position(list._next, 0); }
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ 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 Collections.Sequences.Position((IMemoryList<byte>)_end, _endIndex));
default: throw new NotImplementedException();
}
}
Expand Down Expand Up @@ -236,7 +236,7 @@ Type Kind
}
}

public Collections.Sequences.Position First => Collections.Sequences.Position.Create(_start, _startIndex);
public Collections.Sequences.Position Start => new Collections.Sequences.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 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 Collections.Sequences.Position(node.Next, 0);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ 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 Collections.Sequences.Position((IMemoryList<byte>)_end, _endIndex));
default: throw new NotImplementedException();
}
}
Expand Down Expand Up @@ -233,7 +233,7 @@ Type Kind
}
}

public Collections.Sequences.Position First => Collections.Sequences.Position.Create(_start, _startIndex);
public Collections.Sequences.Position Start => new Collections.Sequences.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 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 Collections.Sequences.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;
Collections.Sequences.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;
Collections.Sequences.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;
Collections.Sequences.Position position = sequence.Start;
int totalIndex = 0;
while (sequence.TryGet(ref position, out ReadOnlyMemory<byte> memory))
{
Expand Down Expand Up @@ -72,7 +72,7 @@ public static long IndexOf<TSequence>(TSequence sequence, byte v1, byte v2) wher
{
if (sequence == null) return null;

Collections.Sequences.Position position = sequence.First;
Collections.Sequences.Position position = sequence.Start;
Collections.Sequences.Position result = position;
while (sequence.TryGet(ref position, out ReadOnlyMemory<byte> memory))
{
Expand All @@ -91,7 +91,7 @@ public static long IndexOf<TSequence>(TSequence sequence, byte v1, byte v2) wher
{
if (sequence == null) return null;

Collections.Sequences.Position position = sequence.First;
Collections.Sequences.Position position = sequence.Start;
Collections.Sequences.Position result = position;
while (sequence.TryGet(ref position, out ReadOnlyMemory<byte> memory))
{
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 Down Expand Up @@ -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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace System.Buffers.Text
public BufferReader(TSequence bytes)
{
_bytes = bytes;
_nextSegmentPosition = bytes.First;
_nextSegmentPosition = bytes.Start;
_currentSegmentPosition = _nextSegmentPosition;
if(_bytes.TryGet(ref _nextSegmentPosition, out ReadOnlyMemory<byte> memory))
{
Expand All @@ -41,7 +41,7 @@ public BufferReader(ReadOnlyMemory<byte> bytes)
{
_bytes = default;
_nextSegmentPosition = default;
_currentSegmentPosition = 0;
_currentSegmentPosition = default;
_currentSpan = bytes.Span;
_currentSpanIndex = 0;
}
Expand All @@ -50,7 +50,7 @@ public BufferReader(ReadOnlySpan<byte> bytes)
{
_bytes = default;
_nextSegmentPosition = default;
_currentSegmentPosition = 0;
_currentSegmentPosition = default;
_currentSpan = bytes;
_currentSpanIndex = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
<PackageReference Include="System.Buffers" Version="$(CoreFxStableVersion)" />
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="$(SystemCompilerServicesUnsafeVersion)" />
<PackageReference Include="System.ValueTuple" Version="$(CoreFxStableVersion)" />
</ItemGroup>
</Project>
72 changes: 0 additions & 72 deletions src/System.Buffers.Primitives/System/Buffers/BufferExtensions.cs

This file was deleted.

Loading