-
-
Save MasterDuke17/cc73afa856b21163138c1a4692c9b42d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/HLL/sprintf.nqp b/src/HLL/sprintf.nqp | |
index 766f66ba4..c9fbed641 100644 | |
--- a/src/HLL/sprintf.nqp | |
+++ b/src/HLL/sprintf.nqp | |
@@ -192,19 +192,20 @@ my module sprintf { | |
my $size := $<size> ?? $<size>.made !! 0; | |
my $pre := ''; | |
$pre := ($<sym> eq 'b' ?? '0b' !! '0B') if $int ne '0' && has_flag($/, 'hash'); | |
+ my $b := ''; | |
if nqp::chars($<precision>) { | |
- $int := ($<precision>.made == 0 && $int == 0) ?? '' | |
+ $b := ($<precision>.made == 0 && $int == 0) ?? '' | |
!! $sign ~ $pre ~ infix_x('0', $<precision>.made - nqp::chars($int)) ~ $int; | |
} | |
else { | |
if $pad ne ' ' && $size { | |
my $chars_sign_pre := $sign ?? nqp::chars($pre) + 1 !! nqp::chars($pre); | |
- $int := $sign ~ $pre ~ infix_x($pad, $size - $chars_sign_pre - nqp::chars($int)) ~ $int; | |
+ $b := $sign ~ $pre ~ infix_x($pad, $size - $chars_sign_pre - nqp::chars($b)) ~ $b; | |
} else { | |
- $int := $sign ~ $pre ~ $int; | |
+ $b := $sign ~ $pre ~ $b; | |
} | |
} | |
- make $int; | |
+ make $b; | |
} | |
method directive:sym<c>($/) { | |
@@ -228,27 +229,28 @@ my module sprintf { | |
!! ''; | |
$int := nqp::tostr_I(nqp::abs_I($int, $knowhow)); | |
+ my $d := ''; | |
# For `d`, precision is how many digits long the number should be, | |
# prefixing it with zeros, as needed. If precision is zero and | |
# our number is zero, then the result is an empty string. | |
if nqp::chars($<precision>) { | |
if my $prec := +$<precision>.made { | |
if (my $rep := $prec - nqp::chars($int)) > 0 { | |
- $int := infix_x('0', $rep) ~ $int; | |
+ $d := infix_x('0', $rep) ~ $int; | |
} | |
} | |
else { | |
- $int := '' if $int == 0; | |
+ $d := '' if $int == 0; | |
} | |
} | |
if $pad ne ' ' && $<size> { | |
- $int := $sign ~ infix_x($pad, $<size>.made - nqp::chars($int) - 1) ~ $int; | |
+ $d := $sign ~ infix_x($pad, $<size>.made - nqp::chars($d) - 1) ~ $d; | |
} | |
else { | |
- $int := $sign ~ $int; | |
+ $d := $sign ~ $d; | |
} | |
- make $int | |
+ make $d | |
} | |
sub pad-with-sign($sign, $num, $size, $pad) { | |
@@ -259,7 +261,7 @@ my module sprintf { | |
} | |
} | |
- sub normalize($float) { | |
+ sub normalize(num $float) { | |
my @parts := nqp::split('e', nqp::lc($float)); | |
my $sign := ''; | |
if nqp::eqat(@parts[0], '-', 0) { | |
@@ -284,11 +286,12 @@ my module sprintf { | |
$sign ~ $num; | |
} | |
- sub stringify-to-precision($float, $precision) { | |
- $float := normalize($float) | |
- if nqp::index($float, "e") || nqp::index($float, "E"); | |
+ sub stringify-to-precision(num $float, $precision) { | |
+ my $float_str := ~$float; | |
+ $float_str := normalize($float_str) | |
+ if nqp::index($float_str, "e") || nqp::index($float_str, "E"); | |
- my @number := nqp::split('.', $float); | |
+ my @number := nqp::split('.', $float_str); | |
my $lhs_s := @number[0]; | |
my $rhs_s := @number[1]; | |
@@ -319,7 +322,7 @@ my module sprintf { | |
$return; | |
} | |
- sub stringify-to-precision2($float, $precision) { | |
+ sub stringify-to-precision2(num $float, $precision) { | |
my $exp := nqp::iseq_n($float, 0.0) ?? 0 !! nqp::floor_n(nqp::div_n(nqp::log_n($float), nqp::log_n(10.0))); | |
$float := nqp::add_n(nqp::mul_n(nqp::abs_n($float), nqp::pow_n(10, nqp::sub_n($precision, nqp::add_n($exp, 1.0)))), 0.5); | |
$float := nqp::floor_n($float); | |
@@ -333,7 +336,7 @@ my module sprintf { | |
$float | |
#?endif | |
} | |
- sub fixed-point($float, $precision, $size, $pad, $/) { | |
+ sub fixed-point(num $float, $precision, $size, $pad, $/) { | |
# if we have zero; handle its sign: 1/0e0 == +Inf, 1/-0e0 == -Inf | |
my $sign := nqp::islt_n($float, 0.0) | |
|| ( nqp::iseq_n($float, 0.0) && nqp::islt_n(nqp::div_n(1.0, $float), 0.0) ) | |
@@ -342,10 +345,10 @@ my module sprintf { | |
!! has_flag($/, 'space') ?? ' ' | |
!! ''; | |
$float := nqp::abs_n($float); | |
- $float := stringify-to-precision($float, $precision) unless nqp::isnanorinf($float); | |
- pad-with-sign($sign, $float, $size, $pad); | |
+ my $float_str := nqp::isnanorinf($float) ?? ~$float !! stringify-to-precision($float, $precision); | |
+ pad-with-sign($sign, $float_str, $size, $pad); | |
} | |
- sub scientific($float, $e, $precision, $size, $pad, $/) { | |
+ sub scientific(num $float, $e, $precision, $size, $pad, $/) { | |
# if we have zero; handle its sign: 1/0e0 == +Inf, 1/-0e0 == -Inf | |
my $sign := nqp::islt_n($float, 0.0) | |
|| ( nqp::iseq_n($float, 0.0) && nqp::islt_n(nqp::div_n(1.0, $float), 0.0) ) | |
@@ -354,20 +357,21 @@ my module sprintf { | |
!! has_flag($/, 'space') ?? ' ' | |
!! ''; | |
$float := nqp::abs_n($float); | |
+ my $float_str := ''; | |
unless nqp::isnanorinf($float) { | |
- my $exp := nqp::iseq_n($float, 0.0) ?? 0 !! nqp::floor_n(nqp::div_n(nqp::log_n($float), nqp::log_n(10.0))); | |
+ my num $exp := nqp::iseq_n($float, 0.0) ?? 0.0 !! nqp::floor_n(nqp::div_n(nqp::log_n($float), nqp::log_n(10.0))); | |
$float := nqp::div_n($float, nqp::pow_n(10.0, $exp)); | |
- $float := stringify-to-precision($float, $precision); | |
+ $float_str := stringify-to-precision($float, $precision); | |
if nqp::islt_n($exp, 0.0) { | |
- $exp := -$exp; | |
- $float := $float ~ $e ~ '-' ~ (nqp::islt_n($exp, 10.0) ?? '0' !! '') ~ $exp; | |
+ $exp := nqp::neg_n($exp); | |
+ $float_str := $float_str ~ $e ~ '-' ~ (nqp::islt_n($exp, 10.0) ?? '0' !! '') ~ $exp; | |
} else { | |
- $float := $float ~ $e ~ '+' ~ (nqp::islt_n($exp, 10.0) ?? '0' !! '') ~ $exp; | |
+ $float_str := $float_str ~ $e ~ '+' ~ (nqp::islt_n($exp, 10.0) ?? '0' !! '') ~ $exp; | |
} | |
} | |
- pad-with-sign($sign, $float, $size, $pad); | |
+ pad-with-sign($sign, $float_str, $size, $pad); | |
} | |
- sub shortest($float, $e, $precision, $size, $pad, $/) { | |
+ sub shortest(num $float, $e, $precision, $size, $pad, $/) { | |
# if we have zero; handle its sign: 1/0e0 == +Inf, 1/-0e0 == -Inf | |
my $sign := nqp::islt_n($float, 0.0) | |
|| ( nqp::iseq_n($float, 0.0) && nqp::islt_n(nqp::div_n(1.0, $float), 0.0) ) | |
@@ -377,23 +381,23 @@ my module sprintf { | |
!! ''; | |
$float := nqp::abs_n($float); | |
- return pad-with-sign($sign, $float, $size, $pad) if nqp::isnanorinf($float); | |
+ return pad-with-sign($sign, ~$float, $size, $pad) if nqp::isnanorinf($float); | |
- my $exp := nqp::iseq_n($float, 0.0) ?? 0 !! nqp::floor_n(nqp::div_n(nqp::log_n($float), nqp::log_n(10.0))); | |
+ my num $exp := nqp::iseq_n($float, 0.0) ?? 0.0 !! nqp::floor_n(nqp::div_n(nqp::log_n($float), nqp::log_n(10.0))); | |
- if -2 - $precision < $exp && $exp < $precision { | |
+ if nqp::islt_n(-2 - $precision, $exp) && nqp::islt_n($exp, $precision) { | |
my $fixed-precision := $precision - ($exp + 1); | |
my $fixed := stringify-to-precision2($float, $precision); | |
pad-with-sign($sign, $fixed, $size, $pad); | |
} else { | |
$float := nqp::div_n($float, nqp::pow_n(10.0, $exp)); | |
- $float := stringify-to-precision2($float, $precision); | |
- my $sci; | |
+ my $float_str := stringify-to-precision2($float, $precision); | |
+ my $sci := ''; | |
if $exp < 0 { | |
$exp := -$exp; | |
- $sci := $float ~ $e ~ '-' ~ ($exp < 10 ?? '0' !! '') ~ $exp; | |
+ $sci := $float_str ~ $e ~ '-' ~ ($exp < 10 ?? '0' !! '') ~ $exp; | |
} else { | |
- $sci := $float ~ $e ~ '+' ~ ($exp < 10 ?? '0' !! '') ~ $exp; | |
+ $sci := $float_str ~ $e ~ '+' ~ ($exp < 10 ?? '0' !! '') ~ $exp; | |
} | |
pad-with-sign($sign, $sci, $size, $pad); | |
@@ -405,7 +409,7 @@ my module sprintf { | |
CATCH { | |
bad-type-for-directive($next, 'e'); | |
} | |
- my $float := floatify($next); | |
+ my num $float := floatify($next); | |
my $precision := $<precision> ?? $<precision>.made !! 6; | |
my $pad := padding_char($/); | |
my $size := $<size> ?? $<size>.made !! 0; | |
@@ -416,18 +420,18 @@ my module sprintf { | |
CATCH { | |
bad-type-for-directive($next, 'f'); | |
} | |
- my $int := floatify($next); | |
+ my num $float := floatify($next); | |
my $precision := $<precision> ?? $<precision>.made !! 6; | |
my $pad := padding_char($/); | |
my $size := $<size> ?? $<size>.made !! 0; | |
- make fixed-point($int, $precision, $size, $pad, $/); | |
+ make fixed-point($float, $precision, $size, $pad, $/); | |
} | |
method directive:sym<g>($/) { | |
my $next := next_argument($/); | |
CATCH { | |
bad-type-for-directive($next, 'g'); | |
} | |
- my $float := floatify($next); | |
+ my num $float := floatify($next); | |
my $precision := $<precision> ?? $<precision>.made !! 6; | |
my $pad := padding_char($/); | |
my $size := $<size> ?? $<size>.made !! 0; | |
@@ -439,17 +443,17 @@ my module sprintf { | |
bad-type-for-directive($next, 'o'); | |
} | |
my $int := intify($next); | |
- $int := nqp::base_I($int, 8); | |
- my $pre := '0' if $int ne '0' && has_flag($/, 'hash'); | |
+ my $int_str := nqp::base_I($int, 8); | |
+ my $pre := '0' if $int != 0 && has_flag($/, 'hash'); | |
if nqp::chars($<precision>) { | |
- $int := '' if $<precision>.made == 0 && $int == 0; | |
- $pre := '' if $<precision>.made > nqp::chars($int); | |
- $int := $pre ~ infix_x('0', intify($<precision>.made) - nqp::chars($int)) ~ $int; | |
+ $int_str := '' if $<precision>.made == 0 && $int == 0; | |
+ $pre := '' if $<precision>.made > nqp::chars($int_str); | |
+ $int_str := $pre ~ infix_x('0', intify($<precision>.made) - nqp::chars($int_str)) ~ $int_str; | |
} | |
else { | |
- $int := $pre ~ $int | |
+ $int_str := $pre ~ $int_str | |
} | |
- make $int | |
+ make $int_str | |
} | |
method directive:sym<s>($/) { | |
@@ -485,16 +489,16 @@ my module sprintf { | |
bad-type-for-directive($next, 'x'); | |
} | |
my $int := intify($next); | |
- $int := nqp::base_I($int, 16); | |
- my $pre := '0X' if $int ne '0' && has_flag($/, 'hash'); | |
+ my $int_str := nqp::base_I($int, 16); | |
+ my $pre := '0X' if $int != 0 && has_flag($/, 'hash'); | |
if nqp::chars($<precision>) { | |
- $int := '' if $<precision>.made == 0 && $int == 0; | |
- $int := $pre ~ infix_x('0', $<precision>.made - nqp::chars($int)) ~ $int; | |
+ $int_str := '' if $<precision>.made == 0 && $int == 0; | |
+ $int_str := $pre ~ infix_x('0', $<precision>.made - nqp::chars($int_str)) ~ $int_str; | |
} | |
else { | |
- $int := $pre ~ $int | |
+ $int_str := $pre ~ $int_str | |
} | |
- make $<sym> eq 'x' ?? nqp::lc($int) !! $int | |
+ make $<sym> eq 'x' ?? nqp::lc($int_str) !! $int_str | |
} | |
method escape:sym<%>($/) { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment