Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
make Str.lines cheaper too by not always checking $limit
  • Loading branch information
FROGGS committed Apr 21, 2014
1 parent bd9e2a3 commit 4332d7e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/core/IO.pm
Expand Up @@ -181,13 +181,13 @@ my class IO::Handle does IO::FileTestable {

method lines($limit = $Inf) {
if $limit == $Inf {
gather while (my $line = self.get).DEFINITE {
gather while nqp::p6definite(my $line = self.get) {
take $line;
}
}
else {
my $count = 0;
gather while ++$count <= $limit && (my $line = self.get).DEFINITE {
gather while ++$count <= $limit && nqp::p6definite(my $line = self.get) {
take $line;
}
}
Expand Down
23 changes: 17 additions & 6 deletions src/core/Str.pm
Expand Up @@ -678,13 +678,24 @@ my class Str does Stringy { # declared in BOOTSTRAP

method lines(Str:D: $limit = $Inf) {
my $prev_pos = -1;
my $l = 0;
gather {
while defined(my $current_pos = self.index("\n", $prev_pos + 1)) && $l++ < $limit {
take self.substr($prev_pos + 1, $current_pos - $prev_pos - 1);
$prev_pos = $current_pos;
if $limit == $Inf {
gather {
while nqp::p6definite(my $current_pos = self.index("\n", $prev_pos + 1)) {
take self.substr($prev_pos + 1, $current_pos - $prev_pos - 1);
$prev_pos = $current_pos;
}
take self.substr($prev_pos + 1) if $prev_pos + 1 < self.chars;
}
}
else {
my $l = 0;
gather {
while nqp::p6definite(my $current_pos = self.index("\n", $prev_pos + 1)) && $l++ < $limit {
take self.substr($prev_pos + 1, $current_pos - $prev_pos - 1);
$prev_pos = $current_pos;
}
take self.substr($prev_pos + 1) if $prev_pos + 1 < self.chars && $l <= $limit;
}
take self.substr($prev_pos + 1) if $prev_pos + 1 < self.chars && $l <= $limit;
}
}

Expand Down

0 comments on commit 4332d7e

Please sign in to comment.