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

Prepared System.Buffer.Primitives for API Review #1993

Merged
merged 5 commits into from
Dec 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/System.Buffers.Primitives/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("System.Buffers.Primitives.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100039ac461fa5c82c7dd2557400c4fd4e9dcdf7ac47e3d572548c04cd4673e004916610f4ea5cbf86f2b1ca1cb824f2a7b3976afecfcf4eb72d9a899aa6786effa10c30399e6580ed848231fec48374e41b3acf8811931343fc2f73acf72dae745adbcb7063cc4b50550618383202875223fc75401351cd89c44bf9b50e7fa3796")]
[assembly: InternalsVisibleTo("System.IO.Pipelines.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100039ac461fa5c82c7dd2557400c4fd4e9dcdf7ac47e3d572548c04cd4673e004916610f4ea5cbf86f2b1ca1cb824f2a7b3976afecfcf4eb72d9a899aa6786effa10c30399e6580ed848231fec48374e41b3acf8811931343fc2f73acf72dae745adbcb7063cc4b50550618383202875223fc75401351cd89c44bf9b50e7fa3796")]

Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,65 @@ namespace System.Buffers
{
public readonly partial struct ReadOnlyBuffer
{
public static int Seek(Position begin, Position end, out Position result, byte byte0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change them to be extension/instance methods on ReadOnlyBuffer

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do it separately. I think I have seen some code that calls the APIs with positions other that the buffer's start/end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Seek is the last one, but we can do it in a separate pass, just don't like moving things back and forth.

{
var enumerator = new ReadOnlyBuffer(begin, end).GetEnumerator();
while (enumerator.MoveNext())
{
var span = enumerator.Current.Span;

int index = span.IndexOf(byte0);
if (index != -1)
{
result = enumerator.CreateCursor(index);
return span[index];
}
}

result = end;
return -1;
}

public static int Seek(Position begin, Position end, out Position result, byte byte0, byte byte1)
{
var enumerator = new ReadOnlyBuffer(begin, end).GetEnumerator();
while (enumerator.MoveNext())
{
var span = enumerator.Current.Span;
int index = span.IndexOfAny(byte0, byte1);

if (index != -1)
{
result = enumerator.CreateCursor(index);
return span[index];
}
}

result = end;
return -1;
}

public static int Seek(Position begin, Position end, out Position result, byte byte0, byte byte1, byte byte2)
{
var enumerator = new ReadOnlyBuffer(begin, end).GetEnumerator();
while (enumerator.MoveNext())
{
var span = enumerator.Current.Span;
int index = span.IndexOfAny(byte0, byte1, byte2);

if (index != -1)
{
result = enumerator.CreateCursor(index);
return span[index];
}
}

result = end;
return -1;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool TryGetBuffer(Position begin, Position end, out ReadOnlyMemory<byte> data, out Position next)
internal static bool TryGetBuffer(Position begin, Position end, out ReadOnlyMemory<byte> data, out Position next)
{
var segment = begin.Segment;

Expand Down Expand Up @@ -82,7 +139,7 @@ private static bool TryGetBuffer(Position begin, Position end, out ReadOnlyMemor
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Position Seek(Position begin, Position end, long bytes, bool checkEndReachable = true)
internal static Position Seek(Position begin, Position end, long bytes, bool checkEndReachable = true)
{
Position cursor;
if (begin.Segment == end.Segment && end.Index - begin.Index >= bytes)
Expand Down
268 changes: 0 additions & 268 deletions src/System.IO.Pipelines/System/IO/Pipelines/PositionOperations.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/System.Text.Http.Parser/HttpParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public unsafe bool ParseHeaders<T>(T handler, in ReadOnlyBuffer buffer, out Posi
var current = reader.Cursor;

// Split buffers
if (PositionOperations.Seek(current, bufferEnd, out var lineEnd, ByteLF) == -1)
if (ReadOnlyBuffer.Seek(current, bufferEnd, out var lineEnd, ByteLF) == -1)
{
// Not there
return false;
Expand Down Expand Up @@ -640,7 +640,7 @@ private static unsafe bool Contains(byte* searchSpace, int length, byte value)
private static bool TryGetNewLineSpan(in ReadOnlyBuffer buffer, out Position found)
{
var start = buffer.Start;
if (PositionOperations.Seek(start, buffer.End, out found, ByteLF) != -1)
if (ReadOnlyBuffer.Seek(start, buffer.End, out found, ByteLF) != -1)
{
// Move 1 byte past the \n
found = buffer.Move(found, 1);
Expand Down
Loading