Skip to content

Trailing EOL treated as an additional empty line #51

@hashtagchris

Description

@hashtagchris

byline with keepEmptyLines: true behaves slightly differently than most line readers I've encountered, including node's readline module. I created sample.txt by running:

echo foo> sample.txt
echo bar>> sample.txt
echo>> sample.txt
echo and baz>> sample.txt

byline will return 5 chunks for this file (or 3 with keepEmptyLines: false). The readline sample returns four lines:

Line from file: foo
Line from file: bar
Line from file:
Line from file: and baz

As does this C# program:

using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        using (var stream = new FileStream("sample.txt", FileMode.Open))
        using (var reader = new StreamReader(stream))
        {
            string line;
            while (null != (line = reader.ReadLine()))
            {
                Console.WriteLine($"Line from file: {line}");
            }
        }
    }
}

(perl -ne 'print "Line from file: $_";' sample.txt also prints 4 lines, but with newlines still attached.)

If byline's current behavior is considered by design, it would be nice if there was a new option to ignore the trailing EOL in a stream. This would make byline more of a drop-in replacement for readline.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions