From a204ba831b96fb15d0189863afc158e9ce9d3e9c Mon Sep 17 00:00:00 2001 From: Moritz Lenz Date: Fri, 17 Sep 2010 22:07:18 +0200 Subject: [PATCH] avoid calling .subst in str2num-rat --- src/core/Str.pm | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/core/Str.pm b/src/core/Str.pm index 0f256af87ba..d07e71aa375 100644 --- a/src/core/Str.pm +++ b/src/core/Str.pm @@ -114,8 +114,27 @@ augment class Str does Stringy { }; } + sub chop-trailing-zeros($i) { + Q:PIR { + .local int idx + $P0 = find_lex '$i' + $S0 = $P0 + idx = length $S0 + repl_loop: + if idx == 0 goto done + dec idx + $S1 = substr $S0, idx, 1 + if $S1 == '0' goto repl_loop + done: + inc idx + $S0 = substr $S0, 0, idx + $P0 = $S0 + %r = $P0 + } + } + our sub str2num-rat($negate, $int-part, $frac-part is copy) is export { - $frac-part.=subst(/(\d)0+$/, { ~$_[0] }); + $frac-part = chop-trailing-zeros($frac-part); my $result = upgrade_to_num_if_needed(str2num-int($int-part)) + upgrade_to_num_if_needed(str2num-int($frac-part)) / upgrade_to_num_if_needed(str2num-base($frac-part));