Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix lines() returning an empty line at EOF
IO::Handle::lines now behaves exactly like Perl 5's:
lines on a file containing "" will return 0 lines.
lines on a file containing "\n" will return one line containing "".
lines on a file containing "foo" will return one line containing "foo".
lines on a file containing "foo\n" will return one line containing "foo".
lines on a file containing "foo\nbar" will return two lines.
With .chomp set to False on the handle the behavior is the same except for the
\n still there if it was in the input file.
  • Loading branch information
niner committed Sep 16, 2015
1 parent 1152728 commit 9fb93a9
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions src/core/IO/Handle.pm
Expand Up @@ -355,18 +355,28 @@ my class IO::Handle does IO {
IterationEnd
}
else {
nqp::bindattr_i($!handle, IO::Handle, '$!ins',
nqp::add_i(nqp::getattr_i($!handle, IO::Handle, '$!ins'), 1));
nqp::p6box_s(nqp::readlinefh($!PIO)).chomp
my \line = nqp::readlinefh($!PIO);
if nqp::eoffh($!PIO) and nqp::not_i(nqp::chars(line)) {
$!handle.close if $!close;
IterationEnd
}
else {
nqp::bindattr_i($!handle, IO::Handle, '$!ins',
nqp::add_i(nqp::getattr_i($!handle, IO::Handle, '$!ins'), 1));
nqp::p6box_s(line).chomp;
}
}
}
method push-all($target) {
my int $ins;
until nqp::eoffh($!PIO) {
$target.push(
nqp::p6box_s(nqp::readlinefh($!PIO)).chomp
);
$ins = $ins + 1;
my \line = nqp::readlinefh($!PIO);
unless nqp::eoffh($!PIO) and nqp::not_i(nqp::chars(line)) {
$target.push(
nqp::p6box_s(nqp::p6box_s(line).chomp)
);
$ins = $ins + 1;
}
}
nqp::bindattr_i($!handle, IO::Handle, '$!ins', $ins );
$!handle.close if $!close;
Expand All @@ -382,16 +392,26 @@ my class IO::Handle does IO {
IterationEnd
}
else {
nqp::bindattr_i($!handle, IO::Handle, '$!ins',
nqp::add_i(nqp::getattr_i($!handle, IO::Handle, '$!ins'), 1));
nqp::p6box_s(nqp::readlinefh($!PIO))
my \line = nqp::readlinefh($!PIO);
if nqp::eoffh($!PIO) and nqp::not_i(nqp::chars(line)) {
$!handle.close if $!close;
IterationEnd
}
else {
nqp::bindattr_i($!handle, IO::Handle, '$!ins',
nqp::add_i(nqp::getattr_i($!handle, IO::Handle, '$!ins'), 1));
nqp::p6box_s(line);
}
}
}
method push-all($target) {
my int $ins;
until nqp::eoffh($!PIO) {
$target.push(nqp::readlinefh($!PIO));
$ins = $ins + 1;
my \line = nqp::readlinefh($!PIO);
unless nqp::eoffh($!PIO) and nqp::not_i(nqp::chars(line)) {
$target.push(nqp::p6box_s(line));
$ins = $ins + 1;
}
}
nqp::bindattr_i($!handle, IO::Handle, '$!ins', $ins );
$!handle.close if $!close;
Expand Down

0 comments on commit 9fb93a9

Please sign in to comment.