Skip to content

Commit

Permalink
[rt.perl.org #128893]: printf %a botches 0 flag for negative values
Browse files Browse the repository at this point in the history
  • Loading branch information
jhi committed Aug 11, 2016
1 parent 75326c4 commit be93048
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
20 changes: 13 additions & 7 deletions sv.c
Expand Up @@ -12421,6 +12421,7 @@ Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN p
int exponent = 0; /* exponent of the floating point input */
bool hexradix = FALSE; /* should we output the radix */
bool subnormal = FALSE; /* IEEE 754 subnormal/denormal */
bool negative = FALSE;

/* XXX: NaN, Inf -- though they are printed as "NaN" and "Inf".
*
Expand Down Expand Up @@ -12448,9 +12449,8 @@ Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN p
# endif
#endif

if (fv < 0
|| Perl_signbit(nv)
)
negative = fv < 0 || Perl_signbit(nv);
if (negative)
*p++ = '-';
else if (plus)
*p++ = plus;
Expand Down Expand Up @@ -12628,12 +12628,18 @@ Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN p
memset(PL_efloatbuf + elen, ' ', width - elen);
}
else if (fill == '0') {
/* Insert the zeros between the "0x" and
* the digits, otherwise we end up with
* "0000xHHH..." */
/* Insert the zeros after the "0x" and the
* the potential sign, but before the digits,
* otherwise we end up with "0000xH.HHH...",
* when we want "0x000H.HHH..." */
STRLEN nzero = width - elen;
char* zerox = PL_efloatbuf + 2;
Move(zerox, zerox + nzero, elen - 2, char);
STRLEN nmove = elen - 2;
if (negative || plus) {
zerox++;
nmove--;
}
Move(zerox, zerox + nzero, nmove, char);
memset(zerox, fill, nzero);
}
else {
Expand Down
11 changes: 10 additions & 1 deletion t/op/sprintf2.t
Expand Up @@ -262,7 +262,7 @@ if ($Config{nvsize} == 8 &&
print "# no hexfloat tests\n";
}

plan tests => 1408 + ($Q ? 0 : 12) + @hexfloat + 64;
plan tests => 1408 + ($Q ? 0 : 12) + @hexfloat + 71;

use strict;
use Config;
Expand Down Expand Up @@ -837,3 +837,12 @@ is(sprintf("%.1a", 0x1.1fp+0), "0x1.2p+0");

is(sprintf("%.2a", 0x1.fffp+0), "0x2.00p+0");
is(sprintf("%.2a", 0xf.fffp+0), "0x2.00p+3");

# [rt.perl.org #128893]
is(sprintf("%020a", 1.5), "0x0000000000001.8p+0");
is(sprintf("%020a", -1.5), "-0x000000000001.8p+0", "[rt.perl.org #128893]");
is(sprintf("%+020a", 1.5), "+0x000000000001.8p+0", "[rt.perl.org #128893]");
is(sprintf("% 020a", 1.5), " 0x000000000001.8p+0", "[rt.perl.org #128893]");
is(sprintf("%20a", -1.5), " -0x1.8p+0");
is(sprintf("%+20a", 1.5), " +0x1.8p+0");
is(sprintf("% 20a", 1.5), " 0x1.8p+0");

0 comments on commit be93048

Please sign in to comment.