Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Re-implement lines(:count) using iter.count-only
  • Loading branch information
lizmat committed Sep 19, 2015
1 parent c1af489 commit 8158cb5
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/core/IO/Handle.pm
Expand Up @@ -323,15 +323,6 @@ my class IO::Handle does IO {
}

proto method lines (|) { * }
multi method lines(IO::Handle:D: :$count!, :$close) {
return self.lines(:$close) if !$count;

until nqp::eoffh($!PIO) {
nqp::readlinefh($!PIO);
$!ins = $!ins + 1;
}
nqp::box_i($!ins, Int);
}
my role LinesIterCommon does Iterator {
has $!handle;
has $!PIO;
Expand Down Expand Up @@ -361,6 +352,16 @@ my class IO::Handle does IO {
nqp::bindattr(iter, self, '$!PIO', $PIO);
iter
}
method count-only() {
my int $ins = nqp::getattr($!handle, IO::Handle, '$!ins');
my str $line = nqp::readlinefh($!PIO);
while nqp::chars($line) {
$ins = $ins + 1;
$line = nqp::readlinefh($!PIO);
}
nqp::bindattr_i($!handle, IO::Handle, '$!ins', $ins);
$ins
}
}
multi method lines(IO::Handle:D: $limit = *, :$close) {
my int $first;
Expand Down

0 comments on commit 8158cb5

Please sign in to comment.