Skip to content

Latest commit

 

History

History
38 lines (28 loc) · 1.08 KB

Readme.md

File metadata and controls

38 lines (28 loc) · 1.08 KB

StreamLine

Reads lines of ascii or endoded text from a stream without allocating memory and faster than StreamReader.ReadLine.

If one of the lines is larger than the buffer, an exception is thrown. Such is life in no-allocation land.

I have no optimized the code or tested it beyond the testcases.

Example

Read Ascii lines with:

    Span<byte> buffer = stackalloc byte[BufferLength];
    ByteLiner l = new(memoryStream, buffer);
    Span<byte> span = stackalloc byte[0]; // https://github.com/dotnet/roslyn/issues/53014

    int sum = 0;
    while((span = l.ReadLine()) != null) {
      sum += span.Length;
    }

Read characters with whatever encoding with:

    Span<byte> buffer = stackalloc byte[BufferLength];
    Span<char> outBuffer = stackalloc char[BufferLength];

    CharLiner l = new(memoryStream, buffer, outBuffer, utf8);

    int sum = 0;
    Span<char> chars = stackalloc char[0];
    while((chars = l.ReadLine()) != null) {
      sum += chars.Length;
    }

Performance results

See here.