Thread overview
[Issue 10098] New: byLine should return empty string instead of null when line is empty
May 17, 2013
Andrej Mitrovic
Sep 17, 2013
Andrej Mitrovic
Sep 18, 2013
Andrej Mitrovic
May 17, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10098

           Summary: byLine should return empty string instead of null when
                    line is empty
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: andrej.mitrovich@gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-05-16 17:15:26 PDT ---
The following code is well-intentioned, however it's currently broken:

-----
import std.stdio;
import std.range;

void main()
{
    auto file1 = File("text1.txt", "r");
    auto file2 = File("text2.txt", "r");

    foreach (char[] line1, char[] line2; zip(StoppingPolicy.longest,
file1.byLine, file2.byLine))
    {
        if (line1 is null)
        {
            // file 1 has less lines
            writefln("<!empty!> <%s>", line2);
        }

        if (line2 is null)
        {
            // file 2 has less lines
            writefln("<%s> <!empty!>", line1);
        }

        writefln("<%s> <%s>", line1, line2);
    }
}
-----

The problem is, line1 or line 2 will be null when an empty line is found. They should really be a zero-length non-null array, otherwise you can't tell that zip has actually ran out of lines for one of the files (note that we're using StoppingPolicy.longest here).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 17, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10098


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull
         AssignedTo|nobody@puremagic.com        |andrej.mitrovich@gmail.com


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-09-17 16:31:51 PDT ---
https://github.com/D-Programming-Language/phobos/pull/1584

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 18, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10098


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|pull                        |
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-09-18 05:35:20 PDT ---
The test-case was invalid, the code worked as-is in 2.060+. Here's a better example:

-----
import std.stdio;
import std.range;

void main()
{
    auto fn1 = "foo1.txt";
    auto fn2 = "foo2.txt";
    scope(exit) std.file.remove(fn1);
    scope(exit) std.file.remove(fn2);
    std.file.write(fn1, "\n\n\n\n");
    std.file.write(fn2, "a\nb\nc\n");
    auto file1 = File(fn1, "r");
    auto file2 = File(fn2, "r");
    size_t lines1, lines2;

    foreach (char[] line1, char[] line2; zip(StoppingPolicy.longest,
file1.byLine, file2.byLine))
    {
        // line1 or line2 should be null only if the files are exhausted,
        // and not when the lines are empty (tested as true in 2.060+)
        writefln(`line1 is null: %s - "%s"`, line1 is null, line1);
        writefln(`line2 is null: %s - "%s"`, line2 is null, line2);
    }
}
-----

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