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

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofCwalina committed Jan 11, 2018
2 parents 60462cd + a2b0538 commit 14ecc71
Show file tree
Hide file tree
Showing 21 changed files with 111 additions and 467 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ public static bool TryRead<TSequence>(ref BufferReader<TSequence> reader, out in
{
if (BinaryPrimitives.TryReadInt32LittleEndian(unread, out value))
{
reader.Skip(sizeof(int));
reader.Advance(sizeof(int));
return true;
}
}
else if (BinaryPrimitives.TryReadInt32BigEndian(unread, out value))
{
reader.Skip(sizeof(int));
reader.Advance(sizeof(int));
return true;
}

Expand All @@ -42,7 +42,7 @@ public static bool TryRead<TSequence>(ref BufferReader<TSequence> reader, out in
{
value = BinaryPrimitives.ReadInt32BigEndian(tempSpan);
}
reader.Skip(sizeof(int));
reader.Advance(sizeof(int));
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@

namespace System.Buffers
{
public interface ISlicable
{
ReadOnlyBuffer Slice(Position start, Position end);
}

// TODO: the TryReadUntill methods are very inneficient. We need to fix that.
public static partial class BufferReaderExtensions
{
public static bool TryReadUntill(ref BufferReader<ROB> reader, out ROB bytes, byte delimiter)
public static bool TryReadUntill<TSequence>(ref BufferReader<TSequence> reader, out ReadOnlyBuffer bytes, byte delimiter)
where TSequence : ISequence<ReadOnlyMemory<byte>>, ISlicable
{
var copy = reader;
var start = reader.Position;
while (!reader.End) {
while (!reader.End)
{
Position end = reader.Position;
if(reader.Take() == delimiter)
if (reader.Take() == delimiter)
{
bytes = new ROB(start, end);
bytes = reader.Sequence.Slice(start, end);
return true;
}
}
Expand All @@ -25,11 +32,12 @@ public static bool TryReadUntill(ref BufferReader<ROB> reader, out ROB bytes, by
return false;
}

public static bool TryReadUntill(ref BufferReader<ROB> reader, out ROB bytes, ReadOnlySpan<byte> delimiter)
public static bool TryReadUntill<TSequence>(ref BufferReader<TSequence> reader, out ReadOnlyBuffer bytes, ReadOnlySpan<byte> delimiter)
where TSequence : ISequence<ReadOnlyMemory<byte>>, ISlicable
{
if (delimiter.Length == 0)
{
bytes = ROB.Empty;
bytes = default;
return true;
}

Expand All @@ -39,17 +47,18 @@ public static bool TryReadUntill(ref BufferReader<ROB> reader, out ROB bytes, Re
var end = reader.Position;
while (!reader.End)
{
if (reader.Take() == delimiter[matched]) {
if (reader.Take() == delimiter[matched])
{
matched++;
}
else
{
end = reader.Position;
matched = 0;
}
if(matched >= delimiter.Length)
if (matched >= delimiter.Length)
{
bytes = new ROB(start, end);
bytes = reader.Sequence.Slice(start, end);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static bool TryParse<TSequence>(ref BufferReader<TSequence> reader, out b
{
if (unread.Length > consumed)
{
reader.Skip(consumed);
reader.Advance(consumed);
return true;
}
}
Expand All @@ -25,7 +25,7 @@ public static bool TryParse<TSequence>(ref BufferReader<TSequence> reader, out b
var copied = BufferReader.Peek(reader, tempSpan);
if (Utf8Parser.TryParse(tempSpan.Slice(0, copied), out value, out consumed))
{
reader.Skip(consumed);
reader.Advance(consumed);
return true;
}

Expand All @@ -40,7 +40,7 @@ public static bool TryParse<TSequence>(ref BufferReader<TSequence> reader, out i
{
if (unread.Length > consumed)
{
reader.Skip(consumed);
reader.Advance(consumed);
return true;
}
}
Expand All @@ -49,7 +49,7 @@ public static bool TryParse<TSequence>(ref BufferReader<TSequence> reader, out i
var copied = BufferReader.Peek(reader, tempSpan);
if (Utf8Parser.TryParse(tempSpan.Slice(0, copied), out value, out consumed))
{
reader.Skip(consumed);
reader.Advance(consumed);
return true;
}

Expand All @@ -64,7 +64,7 @@ public static bool TryParse<TSequence>(ref BufferReader<TSequence> reader, out u
{
if (unread.Length > consumed)
{
reader.Skip(consumed);
reader.Advance(consumed);
return true;
}
}
Expand All @@ -73,7 +73,7 @@ public static bool TryParse<TSequence>(ref BufferReader<TSequence> reader, out u
var copied = BufferReader.Peek(reader, tempSpan);
if (Utf8Parser.TryParse(tempSpan.Slice(0, copied), out value, out consumed))
{
reader.Skip(consumed);
reader.Advance(consumed);
return true;
}

Expand Down
Loading

0 comments on commit 14ecc71

Please sign in to comment.