Created
December 4, 2018 16:58
-
-
Save richlander/a1b77d072ba4943185705e6b13938c20 to your computer and use it in GitHub Desktop.
System.Buffers.SequenceReader Example
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
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