This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Unified Readers * Removed PositionOf from reader * Added tests for CopyTo * reacted to changes to ReadOnlyBuffer * Renamed CopyTo to Peek
- Loading branch information
1 parent
00bc61f
commit 1fecfe7
Showing
12 changed files
with
373 additions
and
474 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/System.Buffers.Experimental/System/Buffers/BufferReader_binary.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/System.Buffers.Experimental/System/Buffers/BufferReader_search.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/System.Buffers.Experimental/System/Buffers/BufferReader_text.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.