File.byLine ought to return dups?
Graham Fawcett
fawcett at uwindsor.ca
Fri Jun 4 12:27:03 PDT 2010
Hi folks,
I expected the following program to print the lines of a file in
reverse:
// bad.d
import std.stdio;
import std.range;
void main() {
auto f = File("bad.d");
foreach(char[] line; retro(array(f.byLine())))
writeln(line);
}
However, this produces very unusual output: fragments of the same
lines are printed repeatedly. I suspect it's because the byLine()
'generator' is not dup'ing the arrays it reads from the file.
This works as expected:
// good.d
import std.stdio;
import std.range;
Retro!(char[][]) retroLines(File f) {
char[][] lines;
foreach(line; f.byLine())
lines ~= line.dup; // note the .dup
return retro(lines);
}
void main() {
auto f = File("good.d");
foreach(line; retroLines(f))
writeln(line);
}
If you remove the '.dup', then this behaves badly as well.
So is this a bug in File.byLine, or am I just using it badly? :)
Thanks,
Graham
More information about the Digitalmars-d-announce
mailing list