seq: ensure correct output width for scientific notation input
authorPádraig Brady <P@draigBrady.com>
Fri, 23 Nov 2012 03:06:07 +0000 (03:06 +0000)
committerPádraig Brady <P@draigBrady.com>
Sat, 24 Nov 2012 15:25:19 +0000 (15:25 +0000)
* src/seq.c (scan_arg): Calculate the width more accurately
for numbers specified using scientific notation.
* tests/misc/seq.pl: Add tests for cases that were mishandled.
* NEWS: Mention the fix.
* THANKS.in: Reported by Marcel Böhme.
Fixes http://bugs.gnu.org/12959

NEWS
THANKS.in
src/seq.c
tests/misc/seq.pl

diff --git a/NEWS b/NEWS
index 15fddd4..8529216 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -21,6 +21,10 @@ GNU coreutils NEWS                                    -*- outline -*-
   consistently padded with spaces, rather than with zeros for certain widths.
   [bug introduced in TEXTUTILS-1_22i]
 
+  seq -w ensures that for numbers input in scientific notation,
+  the output numbers are properly aligned and of the correct width.
+  [This bug was present in "the beginning".]
+
 ** Changes in behavior
 
   df --total now prints '-' into the target column (mount point) of the
index 016a41e..6a79e04 100644 (file)
--- a/THANKS.in
+++ b/THANKS.in
@@ -367,6 +367,7 @@ Marc Haber                          mh+debian-bugs@zugschlus.de
 Marc Mengel                         mengel@fnal.gov
 Marc Lehman                         schmorp@schmorp.de
 Marc Olzheim                        marcolz@stack.nl
+Marcel Böhme                        http://www.comp.nus.edu.sg/~mboehme
 Marco Franzen                       Marco.Franzen@Thyron.com
 Marcus Brinkmann                    http://www.marcus-brinkmann.de
 Marcus Daniels                      marcus@ee.pdx.edu
index e5788ca..9c2c51f 100644 (file)
--- a/src/seq.c
+++ b/src/seq.c
@@ -166,6 +166,21 @@ scan_arg (const char *arg)
         {
           long exponent = strtol (e + 1, NULL, 10);
           ret.precision += exponent < 0 ? -exponent : 0;
+          /* Don't account for e.... in the width since this is not output.  */
+          ret.width -= strlen (arg) - (e - arg);
+          /* Adjust the width as per the exponent.  */
+          if (exponent < 0)
+            {
+              if (decimal_point)
+                {
+                  if (e == decimal_point + 1) /* undo #. -> # above  */
+                    ret.width++;
+                }
+              else
+                ret.width++;
+              exponent = -exponent;
+            }
+          ret.width += exponent;
         }
     }
 
index 351097b..447baa4 100755 (executable)
@@ -70,6 +70,13 @@ my @Tests =
    ['eq-wid-6',        qw(-w +1 2),  {OUT => [qw(1 2)]}],
    ['eq-wid-7',        qw(-w "    .1"  "    .1"),  {OUT => [qw(0.1)]}],
    ['eq-wid-8',        qw(-w 9 0.5 10),  {OUT => [qw(09.0 09.5 10.0)]}],
+   # Prior to 8.21, these tests involving numbers in scentific notation
+   # would fail with misalignment or wrong widths.
+   ['eq-wid-9',        qw(-w -1e-3 1),  {OUT => [qw(-0.001 00.999)]}],
+   ['eq-wid-10',qw(-w -1e-003 1),  {OUT => [qw(-0.001 00.999)]}],
+   ['eq-wid-11',qw(-w -1.e-3 1),  {OUT => [qw(-0.001 00.999)]}],
+   ['eq-wid-12',qw(-w -1.0e-4 1),  {OUT => [qw(-0.00010 00.99990)]}],
+   ['eq-wid-13',qw(-w 999 1e3),  {OUT => [qw(0999 1000)]}],
 
    # Prior to coreutils-4.5.11, some of these were not accepted.
    ['fmt-1',   qw(-f %2.1f 1.5 .5 2),{OUT => [qw(1.5 2.0)]}],