Skip to content
Permalink
Browse files

Add "count-only" method to Str.Lines/Words iterators

- makes Str.lines(:count) / Str.lines.elems about 2x as fast
- makes Str.words.elems about 4x as fast
- makes both a *lot* more allocation friendly
  • Loading branch information...
lizmat committed Sep 7, 2018
1 parent da0a81e commit 89055755e270334e326b5364e24d991ac192f08c
Showing with 25 additions and 0 deletions.
  1. +25 −0 src/core/Str.pm6
@@ -1334,6 +1334,17 @@ my class Str does Stringy { # declared in BOOTSTRAP
$!pos = $nextpos + 1;
}
}
method count-only(--> Int:D) {
my int $left;
my int $seen;

while ($left = $!chars - $!pos) > 0 {
($!pos = nqp::findcclass(
nqp::const::CCLASS_NEWLINE, $!str, $!pos, $left) + 1),
($seen = $seen + 1)
}
$seen
}
}
multi method lines(Str:D:) { Seq.new(Lines.new(self)) }

@@ -2188,6 +2199,20 @@ my class Str does Stringy { # declared in BOOTSTRAP
$!str, $nextpos, $!chars - $nextpos);
}
}
method count-only(--> Int:D) {
my int $left;
my int $nextpos;
my int $seen;

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);
$seen = $seen + 1;
}
$seen
}
}
multi method words(Str:D:) { Seq.new(Words.new(self)) }

0 comments on commit 8905575

Please sign in to comment.
You can’t perform that action at this time.