Bug 10098 – byLine should return empty string instead of null when line is empty

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-05-16T17:15:00Z
Last change time
2013-09-18T05:35:20Z
Assigned to
andrej.mitrovich
Creator
andrej.mitrovich

Comments

Comment #0 by andrej.mitrovich — 2013-05-16T17:15:26Z
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).
Comment #1 by andrej.mitrovich — 2013-09-17T16:31:51Z
Comment #2 by andrej.mitrovich — 2013-09-18T05:35:20Z
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); } } -----