Skip to content

Commit

Permalink
Make Str.trim-leading between 20x and 90x faster
Browse files Browse the repository at this point in the history
- about 20x for strings without whitespace at start
- about 90x for strings *with* whitespace at start

Inspired by the notion that nqp::substr is actually optimised to
return the source string if the entire string is indicated.  So we
don't need to do anything special in the setting.
  • Loading branch information
lizmat committed Dec 10, 2019
1 parent 382d18b commit 3941fef
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/core.c/Str.pm6
Expand Up @@ -2200,11 +2200,12 @@ my class Str does Stringy { # declared in BOOTSTRAP
}

method trim-leading(Str:D: --> Str:D) {
my str $str = nqp::unbox_s(self);
my int $pos = nqp::findnotcclass(
nqp::const::CCLASS_WHITESPACE,
$str, 0, nqp::chars($str));
$pos ?? nqp::p6box_s(nqp::substr($str, $pos)) !! self;
nqp::substr(
self,
nqp::findnotcclass(
nqp::const::CCLASS_WHITESPACE,self,0,nqp::chars(self)
)
)
}

method trim-trailing(Str:D: --> Str:D) {
Expand Down

0 comments on commit 3941fef

Please sign in to comment.