]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
seq: use consistent output format with hex integers
authorPádraig Brady <P@draigBrady.com>
Tue, 23 Jun 2015 21:48:25 +0000 (22:48 +0100)
committerPádraig Brady <P@draigBrady.com>
Wed, 24 Jun 2015 16:03:59 +0000 (17:03 +0100)
* src/seq.c (scan_arg): Set precision to 0 for hex constants
(while avoiding hex floats).  This will use then use the
fast path for these arguments.  Note we also set the precision
of inf to 0 here, which ensures we use consistent precision
on output where possible.
* tests/misc/seq-precision.sh: Add corresponding test cases.

src/seq.c
tests/misc/seq-precision.sh

index e8d4f18924bea37f644ac098051d60d88dcef696..95e6cd639cd1302b073fc7ecd4acde6e0233eae8 100644 (file)
--- a/src/seq.c
+++ b/src/seq.c
@@ -147,15 +147,18 @@ scan_arg (const char *arg)
   while (isspace (to_uchar (*arg)) || *arg == '+')
     arg++;
 
-  ret.width = strlen (arg);
+  ret.width = 0;
   ret.precision = INT_MAX;
 
+  char const *decimal_point = strchr (arg, '.');
+  if (! decimal_point && ! strchr (arg, 'p') /* not a hex float */)
+    ret.precision = 0;
+
   if (! arg[strcspn (arg, "xX")] && isfinite (ret.value))
     {
-      char const *decimal_point = strchr (arg, '.');
-      if (! decimal_point)
-        ret.precision = 0;
-      else
+      ret.width = strlen (arg);
+
+      if (decimal_point)
         {
           size_t fraction_len = strcspn (decimal_point + 1, "eE");
           if (fraction_len <= INT_MAX)
@@ -625,8 +628,8 @@ main (int argc, char **argv)
         }
     }
 
-  if (first.precision == 0 && step.precision == 0
-      && (! isfinite (last.value) || last.precision == 0)
+  if ((isfinite (first.value) && first.precision == 0)
+      && step.precision == 0 && last.precision == 0
       && 0 <= first.value && step.value == 1 && 0 <= last.value
       && !equal_width && !format_str && strlen (separator) == 1)
     {
index 9ba60144650c7a12069369c02f8dbbec92600447..e6a2bd7b81b4c3fa85714d5b2791ca920aeb1f0b 100755 (executable)
@@ -19,7 +19,8 @@
 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
 print_ver_ seq
 
-# Integer only.  Before v8.24 this would switch output format
+# Integer only.  Before v8.24 these would switch output format
+
 seq 999999 inf | head -n2 > out || fail=1
 printf "%s\n" 999999 1000000 > exp || framework_failure_
 compare exp out || fail=1
@@ -34,4 +35,27 @@ for i in $(seq 100); do
   compare exp out || fail=1
 done
 
+seq 0xF423F 0xF4240 > out || fail=1
+printf "%s\n" 999999 1000000 > exp || framework_failure_
+compare exp out || fail=1
+
+# Ensure consistent precision for inf
+seq 1 .1 inf | head -n2 > out || fail=1
+printf "%s\n" 1.0 1.1 > exp || framework_failure_
+compare exp out || fail=1
+
+# Ensure standard output methods with inf start
+seq inf inf | head -n2 | uniq > out || fail=1
+test "$(wc -l < out)" = 1 || fail=1
+
+# Ensure auto precision for hex float
+seq 1 0x1p-1 2 > out || fail=1
+printf "%s\n" 1 1.5 2 > exp || framework_failure_
+compare exp out || fail=1
+
+# Ensure consistent precision for hex
+seq 1 .1 0x2 | head -n2 > out || fail=1
+printf "%s\n" 1.0 1.1 > exp || framework_failure_
+compare exp out || fail=1
+
 Exit $fail