Skip to content

Instantly share code, notes, and snippets.

@richlander
Created December 4, 2018 16:58
Show Gist options
  • Save richlander/a1b77d072ba4943185705e6b13938c20 to your computer and use it in GitHub Desktop.
Save richlander/a1b77d072ba4943185705e6b13938c20 to your computer and use it in GitHub Desktop.
System.Buffers.SequenceReader Example
private static ReadOnlySpan<byte> CRLF => new byte[] { (byte)'\r', (byte)'\n' };
public static void ReadLines(ReadOnlySequence<byte> sequence)
{
SequenceReader<byte> reader = new SequenceReader<byte>(sequence);
while (!reader.End)
{
if (!reader.TryReadToAny(out ReadOnlySpan<byte> line, CRLF, advancePastDelimiter:false))
{
// Couldn't find another delimiter
// ...
}
if (!reader.IsNext(CRLF, advancePast: true))
{
// Not a good CR/LF pair
// ...
}
// line is valid, process
ProcessLine(line);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment