diff options
author | P@draigBrady.com <P@draigBrady.com> | 2007-07-09 16:07:38 +0100 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 2007-07-09 17:55:57 +0200 |
commit | 86e4b778b148bdd82395fdc312ce8d937f303e33 (patch) | |
tree | ebc16d93da6d1c6247c77d1da1471d0330c59949 /src | |
parent | 55c53863a87516e73c055c885dbefe80b003df82 (diff) | |
download | coreutils-86e4b778b148bdd82395fdc312ce8d937f303e33.tar.xz |
Fix the automatic number width formatting in seq.
* src/seq.c: Fix the -w logic. Ignore spaces and '+'
characters of input numbers when determining width.
Set format correctly for input numbers in scientific notation.
* tests/seq/basic: Add various number width tests.
Details: <http://thread.gmane.org/gmane.comp.gnu.coreutils.bugs/10903>
Signed-off-by: Pádraig Brady <Pádraig Brady P@draigBrady.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/seq.c | 40 |
1 files changed, 26 insertions, 14 deletions
@@ -135,23 +135,35 @@ scan_arg (const char *arg) usage (EXIT_FAILURE); } + /* We don't output spaces or '+' so don't include in width */ + while (isspace (*arg) || *arg == '+') + arg++; + ret.width = strlen (arg); ret.precision = INT_MAX; - if (! arg[strcspn (arg, "eExX")] && isfinite (ret.value)) + if (! arg[strcspn (arg, "xX")] && isfinite (ret.value)) { char const *decimal_point = strchr (arg, '.'); if (! decimal_point) ret.precision = 0; else { - size_t fraction_len = strlen (decimal_point + 1); + size_t fraction_len = strcspn (decimal_point + 1, "eE"); if (fraction_len <= INT_MAX) ret.precision = fraction_len; - ret.width += (fraction_len == 0 + ret.width += (fraction_len == 0 /* #. -> # */ ? -1 - : (decimal_point == arg - || ! ISDIGIT (decimal_point[-1]))); + : (decimal_point == arg /* .# -> 0.# */ + || ! ISDIGIT (decimal_point[-1]))); /* -.# -> 0.# */ + } + char const *e = strchr (arg, 'e'); + if (! e) + e = strchr (arg, 'E'); + if (e) + { + long exponent = strtol (e + 1, NULL, 10); + ret.precision += exponent < 0 ? -exponent : 0; } } @@ -275,18 +287,18 @@ get_default_format (operand first, operand step, operand last) { if (equal_width) { + /* increase first_width by any increased precision in step */ size_t first_width = first.width + (prec - first.precision); + /* adjust last_width to use precision from first/step */ size_t last_width = last.width + (prec - last.precision); - if (first.width <= first_width - && (last.width < last_width) == (prec < last.precision)) + if (last.precision && prec == 0) + last_width--; /* don't include space for '.' */ + size_t width = MAX (first_width, last_width); + if (width <= INT_MAX) { - size_t width = MAX (first_width, last_width); - if (width <= INT_MAX) - { - int w = width; - sprintf (format_buf, "%%0%d.%dLf", w, prec); - return format_buf; - } + int w = width; + sprintf (format_buf, "%%0%d.%dLf", w, prec); + return format_buf; } } else |