Skip to content

Commit

Permalink
Make Str.comb() about 3x as fast
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Sep 21, 2015
1 parent e2482f4 commit 07ef13a
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/core/Str.pm
Expand Up @@ -263,9 +263,26 @@ my class Str does Stringy { # declared in BOOTSTRAP
$result ~ '"'
}

role ProcessStr does Iterator {
has str $str;
has int $chars;
submethod BUILD(\string) {
$!str = nqp::unbox_s(string);
$!chars = nqp::chars($!str);
self
}
method new(\string) { nqp::create(self).BUILD(string) }
}

multi method comb(Str:D:) {
my str $self = nqp::unbox_s(self);
(^self.chars).map({ nqp::p6box_s(nqp::substr($self, $_, 1)) });
Seq.new(class :: does ProcessStr {
has int $pos;
method pull-one() {
$!pos < $!chars
?? nqp::p6box_s(nqp::substr($!str, $!pos++, 1))
!! IterationEnd
}
}.new(self));
}
multi method comb(Str:D: Str $pat, $limit = Inf) {
my $count = 0;
Expand Down Expand Up @@ -552,17 +569,6 @@ my class Str does Stringy { # declared in BOOTSTRAP
}
}

role ProcessStr does Iterator {
has str $str;
has int $chars;
submethod BUILD(\string) {
$!str = nqp::unbox_s(string);
$!chars = nqp::chars($!str);
self
}
method new(\string) { nqp::create(self).BUILD(string) }
}

method ords(Str:D:) {
Seq.new(class :: does ProcessStr {
has int $pos;
Expand Down

0 comments on commit 07ef13a

Please sign in to comment.