Skip to content

Commit

Permalink
Fix Str.Lines/Words iterator's count-only
Browse files Browse the repository at this point in the history
- doing a count-only should *not* affect the iterator status
- makes it also about 5% faster
- also fix typo in Lines iterator, which caused an extra infix:<,> call
  • Loading branch information
lizmat committed Sep 7, 2018
1 parent 8905575 commit 86468e1
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/core/Str.pm6
Expand Up @@ -1337,11 +1337,13 @@ my class Str does Stringy { # declared in BOOTSTRAP
method count-only(--> Int:D) {
my int $left;
my int $seen;
my int $pos = $!pos;
my int $chars = $!chars;

while ($left = $!chars - $!pos) > 0 {
($!pos = nqp::findcclass(
nqp::const::CCLASS_NEWLINE, $!str, $!pos, $left) + 1),
($seen = $seen + 1)
while ($left = $chars - $pos) > 0 {
$pos = nqp::findcclass(
nqp::const::CCLASS_NEWLINE, $!str, $pos, $left) + 1;
$seen = $seen + 1;
}
$seen
}
Expand Down Expand Up @@ -2203,12 +2205,14 @@ my class Str does Stringy { # declared in BOOTSTRAP
my int $left;
my int $nextpos;
my int $seen;
my int $pos = $!pos;
my int $chars = $!chars;

while ($left = $!chars - $!pos) > 0 {
while ($left = $chars - $pos) > 0 {
$nextpos = nqp::findcclass(
nqp::const::CCLASS_WHITESPACE, $!str, $!pos, $left);
$!pos = nqp::findnotcclass( nqp::const::CCLASS_WHITESPACE,
$!str, $nextpos, $!chars - $nextpos);
nqp::const::CCLASS_WHITESPACE, $!str, $pos, $left);
$pos = nqp::findnotcclass( nqp::const::CCLASS_WHITESPACE,
$!str, $nextpos, $chars - $nextpos);
$seen = $seen + 1;
}
$seen
Expand Down

0 comments on commit 86468e1

Please sign in to comment.