From 4252a8c98d769605ba9f0af3227cd5a6d7458370 Mon Sep 17 00:00:00 2001 From: Elizabeth Mattijsen Date: Tue, 10 Dec 2019 21:40:11 +0100 Subject: [PATCH] Make Str.substr(N) between 1.5 and 3x faster - 3x faster if N == 0 - 1.5x faster if N > 0 Also added !SUBSTR-START-OOR as private method: this was moved to Rakudo::Internals when private methods were bad new wrt performance. --- src/core.c/Str.pm6 | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/core.c/Str.pm6 b/src/core.c/Str.pm6 index fc11cd6b1ff..406bfd0f676 100644 --- a/src/core.c/Str.pm6 +++ b/src/core.c/Str.pm6 @@ -2840,13 +2840,21 @@ my class Str does Stringy { # declared in BOOTSTRAP }).join; } - multi method substr(Str:D: Int:D \start --> Str:D) { - nqp::if( - nqp::islt_i((my int $from = nqp::unbox_i(start)),0) - || nqp::isgt_i($from,nqp::chars($!value)), #?js: NFG - Rakudo::Internals.SUBSTR-START-OOR($from,nqp::chars($!value)), - nqp::substr($!value,$from) #?js: NFG - ) + method !SUBSTR-START-OOR($from) { + Failure.new(X::OutOfRange.new( + :what('Start argument to substr'), + :got($from.gist), + :range("0.." ~ nqp::chars(self)), + :comment( nqp::istype($from, Callable) || -$from > nqp::chars(self) + ?? '' + !! "use *-{abs $from} if you want to index relative to the end"), + )) + } + + multi method substr(Str:D: Int:D $from --> Str:D) { + nqp::islt_i($from,0) || nqp::isgt_i($from,nqp::chars(self)) #?js: NFG + ?? self!SUBSTR-START-OOR($from) + !! nqp::substr(self,$from) #?js: NFG } multi method substr(Str:D: Int:D \start, Int:D \want --> Str:D) { nqp::if(