From 90d598708199f0506e390bb9866e4bdb592f858a Mon Sep 17 00:00:00 2001 From: Roland McGrath Date: Thu, 24 Oct 2002 01:15:37 +0000 Subject: * stdio-common/tst-fphex.c: New file. * stdio-common/Makefile (tests): Add tst-fphex. * sysdeps/generic/printf_fphex.c (__printf_fphex): Fix initialization of WNUMEND. Fix counting of decimal point in WIDTH. Print '0' pad chars always before the value digits. Reported by James Antill . --- ChangeLog | 9 ++++++++ stdio-common/Makefile | 2 +- stdio-common/tst-fphex.c | 51 ++++++++++++++++++++++++++++++++++++++++++ sysdeps/generic/printf_fphex.c | 23 +++++++++---------- 4 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 stdio-common/tst-fphex.c diff --git a/ChangeLog b/ChangeLog index 6bac4d5f02..2308121a72 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2002-10-23 Roland McGrath + + * stdio-common/tst-fphex.c: New file. + * stdio-common/Makefile (tests): Add tst-fphex. + * sysdeps/generic/printf_fphex.c (__printf_fphex): Fix initialization + of WNUMEND. Fix counting of decimal point in WIDTH. Print '0' pad + chars always before the value digits. + Reported by James Antill . + 2002-10-24 Jakub Jelinek * posix/regcomp.c (re_comp): Call __regfree on re_comp_buf. diff --git a/stdio-common/Makefile b/stdio-common/Makefile index c32a2db428..802e4e37e8 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -54,7 +54,7 @@ tests := tstscanf test_rdwr test-popen tstgetln test-fseek \ scanf1 scanf2 scanf3 scanf4 scanf5 scanf7 scanf8 scanf9 scanf10 \ scanf11 scanf12 tst-tmpnam tst-cookie tst-obprintf tst-sscanf \ tst-swprintf tst-fseek tst-fmemopen test-vfprintf tst-gets \ - tst-perror tst-sprintf tst-rndseek tst-fdopen + tst-perror tst-sprintf tst-rndseek tst-fdopen tst-fphex test-srcs = tst-unbputc tst-printf diff --git a/stdio-common/tst-fphex.c b/stdio-common/tst-fphex.c new file mode 100644 index 0000000000..aca2c0500d --- /dev/null +++ b/stdio-common/tst-fphex.c @@ -0,0 +1,51 @@ +/* Test program for %a printf formats. */ + +#include +#include + +struct testcase +{ + double value; + const char *fmt; + const char *expect; +}; + +static const struct testcase testcases[] = + { + { 0x0.0030p+0, "%a", "0x1.8p-11" }, + { 0x0.0040p+0, "%a", "0x1p-10" }, + { 0x0.0030p+0, "%040a", "0x00000000000000000000000000000001.8p-11" }, + { 0x0.0040p+0, "%040a", "0x0000000000000000000000000000000001p-10" }, + { 0x0.0040p+0, "%40a", " 0x1p-10" }, + { 0x0.0040p+0, "%#40a", " 0x1.p-10" }, + { 0x0.0040p+0, "%-40a", "0x1p-10 " }, + { 0x0.0040p+0, "%#-40a", "0x1.p-10 " }, + { 0x0.0030p+0, "%040e", "00000000000000000000000000007.324219e-04" }, + { 0x0.0040p+0, "%040e", "00000000000000000000000000009.765625e-04" }, + }; + + +static int +do_test (int argc, char **argv) +{ + const struct testcase *t; + int result = 0; + + for (t = testcases; + t < &testcases[sizeof testcases / sizeof testcases[0]]; + ++t) + { + char buf[1024]; + int n = snprintf (buf, sizeof buf, t->fmt, t->value); + if (n != strlen (t->expect) || strcmp (buf, t->expect) != 0) + { + printf ("%s\tExpected \"%s\" (%u)\n\tGot \"%s\" (%d, %u)\n", + t->fmt, t->expect, strlen (t->expect), buf, n, strlen (buf)); + result = 1; + } + } + + return result; +} + +#include "../test-skeleton.c" diff --git a/sysdeps/generic/printf_fphex.c b/sysdeps/generic/printf_fphex.c index 7dfb116aee..5b18c5ebb0 100644 --- a/sysdeps/generic/printf_fphex.c +++ b/sysdeps/generic/printf_fphex.c @@ -338,8 +338,8 @@ __printf_fphex (FILE *fp, /* Look for trailing zeroes. */ if (! zero_mantissa) { - wnumend = wnumbuf + sizeof wnumbuf; - numend = numbuf + sizeof numbuf; + wnumend = &wnumbuf[sizeof wnumbuf / sizeof wnumbuf[0]]; + numend = &numbuf[sizeof numbuf / sizeof numbuf[0]]; while (wnumend[-1] == L'0') { --wnumend; @@ -433,17 +433,13 @@ __printf_fphex (FILE *fp, + ((expbuf + sizeof expbuf) - expstr)); /* Exponent. */ - /* Count the decimal point. */ + /* Count the decimal point. + A special case when the mantissa or the precision is zero and the `#' + is not given. In this case we must not print the decimal point. */ if (precision > 0 || info->alt) width -= wide ? 1 : strlen (decimal); - /* A special case when the mantissa or the precision is zero and the `#' - is not given. In this case we must not print the decimal point. */ - if (precision == 0 && !info->alt) - ++width; /* This nihilates the +1 for the decimal-point - character in the following equation. */ - - if (!info->left && width > 0) + if (!info->left && info->pad != '0' && width > 0) PADN (' ', width); if (negative) @@ -458,6 +454,10 @@ __printf_fphex (FILE *fp, outchar (info->spec + ('x' - 'a')); else outchar (info->spec == 'A' ? 'X' : 'x'); + + if (!info->left && info->pad == '0' && width > 0) + PADN ('0', width); + outchar (leading); if (precision > 0 || info->alt) @@ -474,9 +474,6 @@ __printf_fphex (FILE *fp, PADN ('0', tofill); } - if (info->left && info->pad == '0' && width > 0) - PADN ('0', width); - if ('P' - 'A' == 'p' - 'a') outchar (info->spec + ('p' - 'a')); else -- cgit 1.4.1