Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
RT #122437 fix sprintf regression
The " " and "+" flags weren't being taken into account for floating
point numbers.  Added checks for these flags and some tests.
  • Loading branch information
perlpilot committed Jul 30, 2014
1 parent 1a78f63 commit 854efd3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/HLL/sprintf.nqp
Expand Up @@ -163,8 +163,9 @@ my module sprintf {
my $int := intify(next_argument($/));
my $pad := padding_char($/);
my $sign := nqp::islt_I($int, $zero) ?? '-'
!! has_flag($/, 'plus')
?? '+' !! '';
!! has_flag($/, 'plus') ?? '+'
!! has_flag($/, 'space') ?? ' '
!! '';
$int := nqp::tostr_I(nqp::abs_I($int, $knowhow));
$int := nqp::substr($int, 0, $<precision>.made) if nqp::chars($<precision>);
if $pad ne ' ' && $<size> {
Expand Down Expand Up @@ -215,13 +216,19 @@ my module sprintf {
$float
#?endif
}
sub fixed-point($float, $precision, $size, $pad) {
my $sign := $float < 0 ?? '-' !! '';
sub fixed-point($float, $precision, $size, $pad, $/) {
my $sign := $float < 0 ?? '-'
!! has_flag($/, 'plus') ?? '+'
!! has_flag($/, 'space') ?? ' '
!! '';
$float := stringify-to-precision(nqp::abs_n($float), $precision);
pad-with-sign($sign, $float, $size, $pad);
}
sub scientific($float, $e, $precision, $size, $pad) {
my $sign := $float < 0 ?? '-' !! '';
sub scientific($float, $e, $precision, $size, $pad, $/) {
my $sign := $float < 0 ?? '-'
!! has_flag($/, 'plus') ?? '+'
!! has_flag($/, 'space') ?? ' '
!! '';
$float := nqp::abs_n($float);
my $exp := $float == 0.0 ?? 0 !! nqp::floor_n(nqp::log_n($float) / nqp::log_n(10));
$float := $float / nqp::pow_n(10, $exp);
Expand All @@ -234,8 +241,11 @@ my module sprintf {
}
pad-with-sign($sign, $float, $size, $pad);
}
sub shortest($float, $e, $precision, $size, $pad) {
my $sign := $float < 0 ?? '-' !! '';
sub shortest($float, $e, $precision, $size, $pad, $/) {
my $sign := $float < 0 ?? '-'
!! has_flag($/, 'plus') ?? '+'
!! has_flag($/, 'space') ?? ' '
!! '';
$float := nqp::abs_n($float);

my $exp := $float == 0.0 ?? 0 !! nqp::floor_n(nqp::log_n($float) / nqp::log_n(10));
Expand Down Expand Up @@ -264,21 +274,21 @@ my module sprintf {
my $precision := $<precision> ?? $<precision>.made !! 6;
my $pad := padding_char($/);
my $size := $<size> ?? $<size>.made !! 0;
make scientific($float, $<sym>, $precision, $size, $pad);
make scientific($float, $<sym>, $precision, $size, $pad, $/);
}
method directive:sym<f>($/) {
my $int := next_argument($/);
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($int, $precision, $size, $pad, $/);
}
method directive:sym<g>($/) {
my $float := next_argument($/);
my $precision := $<precision> ?? $<precision>.made !! 6;
my $pad := padding_char($/);
my $size := $<size> ?? $<size>.made !! 0;
make shortest($float, $<sym> eq 'G' ?? 'E' !! 'e', $precision, $size, $pad);
make shortest($float, $<sym> eq 'G' ?? 'E' !! 'e', $precision, $size, $pad, $/);
}
method directive:sym<o>($/) {
my $int := intify(next_argument($/));
Expand Down
4 changes: 4 additions & 0 deletions t/hll/06-sprintf.t
Expand Up @@ -188,6 +188,10 @@ is(nqp::sprintf("%.0f", [1.969]), "2", '%.0f of 1.969 should be 2 # TODO on
is(nqp::sprintf("%.1f", [1.969]), "2.0", '%.1f of 1.969 should be 2.0 # TODO on nqp-p');
is(nqp::sprintf("%.2f", [1.969]), "1.97", '%.2f of 1.969 should be 1.97');
is(nqp::sprintf("%.3f", [1.969]), "1.969", '%.3f of 1.969 should be 1.969');
is(nqp::sprintf("% .3f", [3.141592]), " 3.142", '% .3f of 3.141592 should be " 3.142"');
is(nqp::sprintf("% .3f", [-3.141592]), "-3.142", '% .3f of -3.141592 should be "-3.142"');
is(nqp::sprintf("%+.3f", [3.141592]), "+3.142", '%+.3f of 3.141592 should be "+3.142"');
is(nqp::sprintf("%+.3f", [-3.141592]), "-3.142", '%+.3f of -3.141592 should be "-3.142"');

is(nqp::sprintf('%5.2g', [3.1415]), ' 3.1', '5.2 %g');
is(nqp::sprintf('%5.2G', [3.1415]), ' 3.1', '5.2 %G');
Expand Down

0 comments on commit 854efd3

Please sign in to comment.