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.
- Loading branch information
1 parent
0c8848a
commit 1e2fba4
Showing
10 changed files
with
342 additions
and
465 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 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; | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
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,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; | ||
} | ||
} | ||
} |
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 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; | ||
} | ||
} | ||
} |
Oops, something went wrong.