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

Commit

Permalink
Unified Readers
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofCwalina committed Jan 5, 2018
1 parent 0c8848a commit 1e2fba4
Show file tree
Hide file tree
Showing 10 changed files with 342 additions and 465 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 this 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<TSequence>.CopyTo(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,40 @@
// 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
{
public static partial class BufferReaderExtensions
{
public static bool TryReadUntill<TSequence>(ref this BufferReader<TSequence> reader, out ReadOnlyBuffer bytes, byte delimiter)
where TSequence : ISequence<ReadOnlyMemory<byte>>
{
var position = reader.PositionOf(delimiter);
if (position == null)
{
bytes = default;
return false;
}
bytes = new ReadOnlyBuffer(reader.Position, position.Value);
reader.SkipTo(position.Value);
reader.Skip(1);
return true;
}

public static bool TryReadUntill<TSequence>(ref this BufferReader<TSequence> reader, out ReadOnlyBuffer bytes, ReadOnlySpan<byte> delimiter)
where TSequence : ISequence<ReadOnlyMemory<byte>>
{
var position = reader.PositionOf(delimiter);
if(position == null)
{
bytes = default;
return false;
}
bytes = new ReadOnlyBuffer(reader.Position, position.Value);
reader.SkipTo(position.Value);
reader.Skip(delimiter.Length);
return true;
}
}
}
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 this 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<TSequence>.CopyTo(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 this 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<TSequence>.CopyTo(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 this 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<TSequence>.CopyTo(reader, tempSpan);
if (Utf8Parser.TryParse(tempSpan.Slice(0, copied), out value, out consumed))
{
reader.Skip(consumed);
return true;
}

return false;
}
}
}
Loading

0 comments on commit 1e2fba4

Please sign in to comment.