private static List <T> ProcessStream <T>(MemoryStream stream, FuncSpanT <T> parser) { var reader = PipeReader.Create(stream); var records = new List <T>(); while (true) { ReadResult read = reader.ReadAsync().Result; ReadOnlySequence <byte> buffer = read.Buffer; while (TryReadLine(ref buffer, out ReadOnlySequence <byte> sequence)) { var item = ProcessSequence(sequence, parser); records.Add(item); } reader.AdvanceTo(buffer.Start, buffer.End); if (read.IsCompleted) { break; } } return(records); }
public static async IAsyncEnumerable <T> ProcessFile <T>(Stream stream, FuncSpanT <T> parser) { PipeReader reader = PipeReader.Create(stream); var i = 0; while (true) { ReadResult read = await reader.ReadAsync(); ReadOnlySequence <byte> buffer = read.Buffer; ReadOnlySequence <byte> sequence; // read header row. TryReadLine(ref buffer, out sequence); while (TryReadLine(ref buffer, out sequence)) { var item = ProcessSequence(sequence, parser); yield return(item); } reader.AdvanceTo(buffer.Start, buffer.End); if (read.IsCompleted) { break; } } }
private static T Parse <T>(ReadOnlySpan <byte> bytes, FuncSpanT <T> parser) { Span <char> chars = stackalloc char[bytes.Length]; Encoding.UTF8.GetChars(bytes, chars); return(parser(chars)); }
private static T ProcessSequence <T>(ReadOnlySequence <byte> sequence, FuncSpanT <T> parser) { if (sequence.IsSingleSegment) { return(Parse(sequence.FirstSpan, parser)); } var length = (int)sequence.Length; Span <byte> span = stackalloc byte[length]; sequence.CopyTo(span); return(Parse(span, parser)); }
private static T ProcessSequence <T>(ReadOnlySequence <byte> sequence, FuncSpanT <T> parser) { const int LengthLimit = 256; if (sequence.IsSingleSegment) { return(Parse(sequence.FirstSpan, parser)); } var length = (int)sequence.Length; if (length > LengthLimit) { throw new ArgumentException($"Line has a length exceeding the limit: {length}"); } Span <byte> span = stackalloc byte[(int)sequence.Length]; sequence.CopyTo(span); return(Parse(span, parser)); }