[Issue 11995] New: std.File.ByLine strips trailing empty line

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Jan 25 12:34:36 PST 2014


https://d.puremagic.com/issues/show_bug.cgi?id=11995

           Summary: std.File.ByLine strips trailing empty line
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: monarchdodra at gmail.com


--- Comment #0 from monarchdodra at gmail.com 2014-01-25 12:34:33 PST ---
//----
import std.stdio;
void main()
{
    File("stuff0", "w").write("line 1\nline 2");
    File("stuff1", "w").write("line 1\nline 2\n");
    File("stuff2", "w").write("line 1\nline 2\na");
    File("stuff3", "w").write("line 1\nline 2\n\n\n");
    writeln(File("stuff0").byLine());
    writeln(File("stuff1").byLine());
    writeln(File("stuff2").byLine());
    writeln(File("stuff3").byLine());
}
//----
["line 1", "line 2"]
["line 1", "line 2"]
["line 1", "line 2", "a"]
["line 1", "line 2", "", ""]
//----

The problem is that the last empty line is stripped away. This can be a problem
for algorithms that copy/mutate files, as they can't reliably preserve the
exact amount of lines to produce: File0 and File1 produced the exact same
output, but are different :/

I think the correct output should be:
//----
["line 1", "line 2"]
["line 1", "line 2", ""]
["line 1", "line 2", "a"]
["line 1", "line 2", "", "", ""]
//----

With this scheme, it's simple: every line in the file gets an entry, and the
last line of byLine does not actually have a terminator.

This means that something like:
File("out.txt", "w").writefln("%(%s\n%)", File("in.txt").byLine());
Will *exactly* duplicate the input file.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list