Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
An alternate way to do has-substr
- starts-with for checking strings at the start of a string
- substr-eq-at for checking strings anywhere in a string

Both use nqp::eqat underneath.

The Str:D: candidates are about 2x as fast as the Cool:D: candidates.  Since
these methods are really only intended for hot code paths (because the same
functionality can be achieved by just doing .substr eq "foo", but about 4x
times as slow), it seems valid to keep the separate candidates around.

This patch does *not* remove the has-substr implementation.  So either can
be reverted without touching anything else.
  • Loading branch information
lizmat committed Apr 8, 2015
1 parent b05c2cc commit de4c3d8
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/core/Cool.pm
Expand Up @@ -142,6 +142,32 @@ my class Cool { # declared in BOOTSTRAP
);
}

proto method starts-with(|) {*}
multi method starts-with(Str:D: Str(Cool) $needle) {
nqp::p6bool(
nqp::eqat(nqp::unbox_s(self),nqp::unbox_s($needle),0)
);
}
multi method starts-with(Cool:D: Str(Cool) $needle) {
nqp::p6bool(
nqp::eqat(nqp::unbox_s(self.Str),nqp::unbox_s($needle),0)
);
}

proto method substr-eq-at(|) {*}
multi method substr-eq-at(Str:D: Str(Cool) $needle, Int(Cool) $pos) {
nqp::p6bool(
nqp::eqat(nqp::unbox_s(self),nqp::unbox_s($needle),nqp::unbox_i($pos))
);
}
multi method substr-eq-at(Cool:D: Str(Cool) $needle, Int(Cool) $pos) {
nqp::p6bool(nqp::eqat(
nqp::unbox_s(self.Str),
nqp::unbox_s($needle),
nqp::unbox_i($pos)
));
}

proto method index(|) {*}
multi method index(Cool $needle, Cool $pos = 0) {
my int $result = nqp::index(
Expand Down

0 comments on commit de4c3d8

Please sign in to comment.