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

Commit

Permalink
Removed Old BufferReader (#2025)
Browse files Browse the repository at this point in the history
* Unified Readers
* Removed PositionOf from reader
* Added tests for CopyTo
* reacted to changes to ReadOnlyBuffer
* Renamed CopyTo to Peek
  • Loading branch information
KrzysztofCwalina authored Jan 9, 2018
1 parent 00bc61f commit 1fecfe7
Show file tree
Hide file tree
Showing 12 changed files with 373 additions and 474 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Buffers.Binary;
using System.Collections.Sequences;

namespace System.Buffers
{
public static partial class BufferReaderExtensions
{
public static bool TryRead<TSequence>(ref BufferReader<TSequence> reader, out int value, bool littleEndian = false)
where TSequence : ISequence<ReadOnlyMemory<byte>>
{
var unread = reader.UnreadSegment;
if (littleEndian)
{
if (BinaryPrimitives.TryReadInt32LittleEndian(unread, out value))
{
reader.Skip(sizeof(int));
return true;
}
}
else if (BinaryPrimitives.TryReadInt32BigEndian(unread, out value))
{
reader.Skip(sizeof(int));
return true;
}

Span<byte> tempSpan = stackalloc byte[4];
var copied = BufferReader.Peek(reader, tempSpan);
if (copied < 4)
{
value = default;
return false;
}

if (littleEndian)
{
value = BinaryPrimitives.ReadInt32LittleEndian(tempSpan);
}
else
{
value = BinaryPrimitives.ReadInt32BigEndian(tempSpan);
}
reader.Skip(sizeof(int));
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Sequences;

namespace System.Buffers
{
// TODO: the TryReadUntill methods are very inneficient. We need to fix that.
public static partial class BufferReaderExtensions
{
public static bool TryReadUntill(ref BufferReader<ReadOnlyBytes> reader, out ReadOnlyBytes bytes, byte delimiter)
{
var copy = reader;
var start = reader.Position;
while (!reader.End) {
Position end = reader.Position;
if(reader.Take() == delimiter)
{
bytes = new ReadOnlyBytes(start, end);
return true;
}
}
reader = copy;
bytes = default;
return false;
}

public static bool TryReadUntill(ref BufferReader<ReadOnlyBytes> reader, out ReadOnlyBytes bytes, ReadOnlySpan<byte> delimiter)
{
if (delimiter.Length == 0)
{
bytes = ReadOnlyBytes.Empty;
return true;
}

int matched = 0;
var copy = reader;
var start = reader.Position;
var end = reader.Position;
while (!reader.End)
{
if (reader.Take() == delimiter[matched]) {
matched++;
}
else
{
end = reader.Position;
matched = 0;
}
if(matched >= delimiter.Length)
{
bytes = new ReadOnlyBytes(start, end);
return true;
}
}
reader = copy;
bytes = default;
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Buffers.Text;
using System.Collections.Sequences;

namespace System.Buffers
{
public static partial class BufferReaderExtensions
{
public static bool TryParse<TSequence>(ref BufferReader<TSequence> reader, out bool value)
where TSequence : ISequence<ReadOnlyMemory<byte>>
{
var unread = reader.UnreadSegment;
if (Utf8Parser.TryParse(unread, out value, out int consumed))
{
if (unread.Length > consumed)
{
reader.Skip(consumed);
return true;
}
}

Span<byte> tempSpan = stackalloc byte[5];
var copied = BufferReader.Peek(reader, tempSpan);
if (Utf8Parser.TryParse(tempSpan.Slice(0, copied), out value, out consumed))
{
reader.Skip(consumed);
return true;
}

return false;
}

public static bool TryParse<TSequence>(ref BufferReader<TSequence> reader, out int value)
where TSequence : ISequence<ReadOnlyMemory<byte>>
{
var unread = reader.UnreadSegment;
if (Utf8Parser.TryParse(unread, out value, out int consumed))
{
if (unread.Length > consumed)
{
reader.Skip(consumed);
return true;
}
}

Span<byte> tempSpan = stackalloc byte[15];
var copied = BufferReader.Peek(reader, tempSpan);
if (Utf8Parser.TryParse(tempSpan.Slice(0, copied), out value, out consumed))
{
reader.Skip(consumed);
return true;
}

return false;
}

public static bool TryParse<TSequence>(ref BufferReader<TSequence> reader, out ulong value)
where TSequence : ISequence<ReadOnlyMemory<byte>>
{
var unread = reader.UnreadSegment;
if (Utf8Parser.TryParse(unread, out value, out int consumed))
{
if (unread.Length > consumed)
{
reader.Skip(consumed);
return true;
}
}

Span<byte> tempSpan = stackalloc byte[30];
var copied = BufferReader.Peek(reader, tempSpan);
if (Utf8Parser.TryParse(tempSpan.Slice(0, copied), out value, out consumed))
{
reader.Skip(consumed);
return true;
}

return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,14 @@ public bool TryGet(ref Position position, out ReadOnlyMemory<byte> item, bool ad
{
var start = (int)position;
var length = _endIndex - (int)position;
item = new ReadOnlyMemory<byte>(array, start, length);
if (length == 0)
{
item = ReadOnlyMemory<byte>.Empty;
}
else
{
item = new ReadOnlyMemory<byte>(array, start, length);
}
if (advance) position = default;
return true;
}
Expand All @@ -304,6 +311,16 @@ public bool TryGet(ref Position position, out ReadOnlyMemory<byte> item, bool ad
return true;
}

var om = _start as OwnedMemory<byte>;
if (om != null)
{
var start = (int)position;
var length = _endIndex - (int)position;
item = om.Memory.Slice(start, length);
if (advance) position = default;
return true;
}

throw new NotImplementedException();
}

Expand Down
Loading

0 comments on commit 1fecfe7

Please sign in to comment.