coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] numfmt: handle leading zeros correctly


From: Pádraig Brady
Subject: [PATCH] numfmt: handle leading zeros correctly
Date: Mon, 22 Jun 2015 02:26:31 +0100

* src/numfmt.c (simple_strtod_int): Don't count leading zeros
as significant digits.  Also have leading zeros as optional
for floating point numbers.
* tests/misc/numfmt.pl: Add test cases.
* NEWS: Mention the fix.
---
 NEWS                 |  4 ++++
 src/numfmt.c         |  9 +++++++--
 tests/misc/numfmt.pl | 13 +++++++++++++
 3 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/NEWS b/NEWS
index 3b30000..99a9301 100644
--- a/NEWS
+++ b/NEWS
@@ -35,6 +35,10 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   large numbers, or with numbers with increased precision.
   [bug introduced when numfmt was added in coreutils-8.21]
 
+  numfmt now handles leading zeros correctly, not counting them when
+  settings processing limits, and making them optional with floating point.
+  [bug introduced when numfmt was added in coreutils-8.21]
+
   paste no longer truncates output for large input files.  This would happen
   for example with files larger than 4GiB on 32 bit systems with a '\n'
   character at the 4GiB position.
diff --git a/src/numfmt.c b/src/numfmt.c
index 82a9585..1a7185f 100644
--- a/src/numfmt.c
+++ b/src/numfmt.c
@@ -462,6 +462,7 @@ simple_strtod_int (const char *input_str,
 
   long double val = 0;
   unsigned int digits = 0;
+  bool found_digit = false;
 
   if (*input_str == '-')
     {
@@ -476,7 +477,10 @@ simple_strtod_int (const char *input_str,
     {
       int digit = (**endptr) - '0';
 
-      digits++;
+      found_digit = true;
+
+      if (val || digit)
+        digits++;
 
       if (digits > MAX_UNSCALED_DIGITS)
         e = SSE_OK_PRECISION_LOSS;
@@ -489,7 +493,8 @@ simple_strtod_int (const char *input_str,
 
       ++(*endptr);
     }
-  if (digits == 0)
+  if (! found_digit
+      && ! STREQ_LEN (*endptr, decimal_point, decimal_point_length))
     return SSE_INVALID_NUMBER;
   if (*negative)
     val = -val;
diff --git a/tests/misc/numfmt.pl b/tests/misc/numfmt.pl
index 25bba61..a6432a7 100755
--- a/tests/misc/numfmt.pl
+++ b/tests/misc/numfmt.pl
@@ -659,6 +659,19 @@ my @Tests =
      ['large-15',$limits->{INTMAX_OFLOW}, {OUT=>$limits->{INTMAX_OFLOW}}],
      ['large-16','9.300000000000000000', {OUT=>'9.300000000000000000'}],
 
+     # Leading zeros weren't handled appropriately before 8.24
+     ['leading-1','0000000000000000000000000001', {OUT=>"1"}],
+     ['leading-2','.1', {OUT=>"0.1"}],
+     ['leading-3','bad.1',
+             {ERR => "$prog: invalid number: 'bad.1'\n"},
+             {EXIT => 2}],
+     ['leading-4','..1',
+             {ERR => "$prog: invalid suffix in input: '..1'\n"},
+             {EXIT => 2}],
+     ['leading-5','1.',
+             {ERR => "$prog: invalid number: '1.'\n"},
+             {EXIT => 2}],
+
      # precision override
      ['precision-1','--format=%.4f 9991239123 --to=si', {OUT=>"9.9913G"}],
      ['precision-2','--format=%.1f 9991239123 --to=si', {OUT=>"10.0G"}],
-- 
2.4.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]