seq: enable the fast integer printing code in more cases
[coreutils.git] / src / seq.c
blobe5788caab74feedf5c6030a57d12a7b1bcf2fdf7
1 /* seq - print sequence of numbers to standard output.
2 Copyright (C) 1994-2012 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Ulrich Drepper. */
19 #include <config.h>
20 #include <getopt.h>
21 #include <stdio.h>
22 #include <sys/types.h>
24 #include "system.h"
25 #include "c-strtod.h"
26 #include "error.h"
27 #include "quote.h"
28 #include "xstrtod.h"
30 /* Roll our own isfinite rather than using <math.h>, so that we don't
31 have to worry about linking -lm just for isfinite. */
32 #ifndef isfinite
33 # define isfinite(x) ((x) * 0 == 0)
34 #endif
36 /* The official name of this program (e.g., no 'g' prefix). */
37 #define PROGRAM_NAME "seq"
39 #define AUTHORS proper_name ("Ulrich Drepper")
41 /* If true print all number with equal width. */
42 static bool equal_width;
44 /* The string used to separate two numbers. */
45 static char const *separator;
47 /* The string output after all numbers have been output.
48 Usually "\n" or "\0". */
49 static char const terminator[] = "\n";
51 static struct option const long_options[] =
53 { "equal-width", no_argument, NULL, 'w'},
54 { "format", required_argument, NULL, 'f'},
55 { "separator", required_argument, NULL, 's'},
56 {GETOPT_HELP_OPTION_DECL},
57 {GETOPT_VERSION_OPTION_DECL},
58 { NULL, 0, NULL, 0}
61 void
62 usage (int status)
64 if (status != EXIT_SUCCESS)
65 emit_try_help ();
66 else
68 printf (_("\
69 Usage: %s [OPTION]... LAST\n\
70 or: %s [OPTION]... FIRST LAST\n\
71 or: %s [OPTION]... FIRST INCREMENT LAST\n\
72 "), program_name, program_name, program_name);
73 fputs (_("\
74 Print numbers from FIRST to LAST, in steps of INCREMENT.\n\
75 \n\
76 -f, --format=FORMAT use printf style floating-point FORMAT\n\
77 -s, --separator=STRING use STRING to separate numbers (default: \\n)\n\
78 -w, --equal-width equalize width by padding with leading zeroes\n\
79 "), stdout);
80 fputs (HELP_OPTION_DESCRIPTION, stdout);
81 fputs (VERSION_OPTION_DESCRIPTION, stdout);
82 fputs (_("\
83 \n\
84 If FIRST or INCREMENT is omitted, it defaults to 1. That is, an\n\
85 omitted INCREMENT defaults to 1 even when LAST is smaller than FIRST.\n\
86 FIRST, INCREMENT, and LAST are interpreted as floating point values.\n\
87 INCREMENT is usually positive if FIRST is smaller than LAST, and\n\
88 INCREMENT is usually negative if FIRST is greater than LAST.\n\
89 "), stdout);
90 fputs (_("\
91 FORMAT must be suitable for printing one argument of type 'double';\n\
92 it defaults to %.PRECf if FIRST, INCREMENT, and LAST are all fixed point\n\
93 decimal numbers with maximum precision PREC, and to %g otherwise.\n\
94 "), stdout);
95 emit_ancillary_info ();
97 exit (status);
100 /* A command-line operand. */
101 struct operand
103 /* Its value, converted to 'long double'. */
104 long double value;
106 /* Its print width, if it were printed out in a form similar to its
107 input form. An input like "-.1" is treated like "-0.1", and an
108 input like "1." is treated like "1", but otherwise widths are
109 left alone. */
110 size_t width;
112 /* Number of digits after the decimal point, or INT_MAX if the
113 number can't easily be expressed as a fixed-point number. */
114 int precision;
116 typedef struct operand operand;
118 /* Description of what a number-generating format will generate. */
119 struct layout
121 /* Number of bytes before and after the number. */
122 size_t prefix_len;
123 size_t suffix_len;
126 /* Read a long double value from the command line.
127 Return if the string is correct else signal error. */
129 static operand
130 scan_arg (const char *arg)
132 operand ret;
134 if (! xstrtold (arg, NULL, &ret.value, c_strtold))
136 error (0, 0, _("invalid floating point argument: %s"), arg);
137 usage (EXIT_FAILURE);
140 /* We don't output spaces or '+' so don't include in width */
141 while (isspace (to_uchar (*arg)) || *arg == '+')
142 arg++;
144 ret.width = strlen (arg);
145 ret.precision = INT_MAX;
147 if (! arg[strcspn (arg, "xX")] && isfinite (ret.value))
149 char const *decimal_point = strchr (arg, '.');
150 if (! decimal_point)
151 ret.precision = 0;
152 else
154 size_t fraction_len = strcspn (decimal_point + 1, "eE");
155 if (fraction_len <= INT_MAX)
156 ret.precision = fraction_len;
157 ret.width += (fraction_len == 0 /* #. -> # */
158 ? -1
159 : (decimal_point == arg /* .# -> 0.# */
160 || ! ISDIGIT (decimal_point[-1]))); /* -.# -> 0.# */
162 char const *e = strchr (arg, 'e');
163 if (! e)
164 e = strchr (arg, 'E');
165 if (e)
167 long exponent = strtol (e + 1, NULL, 10);
168 ret.precision += exponent < 0 ? -exponent : 0;
172 return ret;
175 /* If FORMAT is a valid printf format for a double argument, return
176 its long double equivalent, allocated from dynamic storage, and
177 store into *LAYOUT a description of the output layout; otherwise,
178 report an error and exit. */
180 static char const *
181 long_double_format (char const *fmt, struct layout *layout)
183 size_t i;
184 size_t prefix_len = 0;
185 size_t suffix_len = 0;
186 size_t length_modifier_offset;
187 bool has_L;
189 for (i = 0; ! (fmt[i] == '%' && fmt[i + 1] != '%'); i += (fmt[i] == '%') + 1)
191 if (!fmt[i])
192 error (EXIT_FAILURE, 0,
193 _("format %s has no %% directive"), quote (fmt));
194 prefix_len++;
197 i++;
198 i += strspn (fmt + i, "-+#0 '");
199 i += strspn (fmt + i, "0123456789");
200 if (fmt[i] == '.')
202 i++;
203 i += strspn (fmt + i, "0123456789");
206 length_modifier_offset = i;
207 has_L = (fmt[i] == 'L');
208 i += has_L;
209 if (fmt[i] == '\0')
210 error (EXIT_FAILURE, 0, _("format %s ends in %%"), quote (fmt));
211 if (! strchr ("efgaEFGA", fmt[i]))
212 error (EXIT_FAILURE, 0,
213 _("format %s has unknown %%%c directive"), quote (fmt), fmt[i]);
215 for (i++; ; i += (fmt[i] == '%') + 1)
216 if (fmt[i] == '%' && fmt[i + 1] != '%')
217 error (EXIT_FAILURE, 0, _("format %s has too many %% directives"),
218 quote (fmt));
219 else if (fmt[i])
220 suffix_len++;
221 else
223 size_t format_size = i + 1;
224 char *ldfmt = xmalloc (format_size + 1);
225 memcpy (ldfmt, fmt, length_modifier_offset);
226 ldfmt[length_modifier_offset] = 'L';
227 strcpy (ldfmt + length_modifier_offset + 1,
228 fmt + length_modifier_offset + has_L);
229 layout->prefix_len = prefix_len;
230 layout->suffix_len = suffix_len;
231 return ldfmt;
235 /* Actually print the sequence of numbers in the specified range, with the
236 given or default stepping and format. */
238 static void
239 print_numbers (char const *fmt, struct layout layout,
240 long double first, long double step, long double last)
242 bool out_of_range = (step < 0 ? first < last : last < first);
244 if (! out_of_range)
246 long double x = first;
247 long double i;
249 for (i = 1; ; i++)
251 long double x0 = x;
252 printf (fmt, x);
253 if (out_of_range)
254 break;
255 x = first + i * step;
256 out_of_range = (step < 0 ? x < last : last < x);
258 if (out_of_range)
260 /* If the number just past LAST prints as a value equal
261 to LAST, and prints differently from the previous
262 number, then print the number. This avoids problems
263 with rounding. For example, with the x86 it causes
264 "seq 0 0.000001 0.000003" to print 0.000003 instead
265 of stopping at 0.000002. */
267 bool print_extra_number = false;
268 long double x_val;
269 char *x_str;
270 int x_strlen;
271 setlocale (LC_NUMERIC, "C");
272 x_strlen = asprintf (&x_str, fmt, x);
273 setlocale (LC_NUMERIC, "");
274 if (x_strlen < 0)
275 xalloc_die ();
276 x_str[x_strlen - layout.suffix_len] = '\0';
278 if (xstrtold (x_str + layout.prefix_len, NULL, &x_val, c_strtold)
279 && x_val == last)
281 char *x0_str = NULL;
282 if (asprintf (&x0_str, fmt, x0) < 0)
283 xalloc_die ();
284 print_extra_number = !STREQ (x0_str, x_str);
285 free (x0_str);
288 free (x_str);
289 if (! print_extra_number)
290 break;
293 fputs (separator, stdout);
296 fputs (terminator, stdout);
300 /* Return the default format given FIRST, STEP, and LAST. */
301 static char const *
302 get_default_format (operand first, operand step, operand last)
304 static char format_buf[sizeof "%0.Lf" + 2 * INT_STRLEN_BOUND (int)];
306 int prec = MAX (first.precision, step.precision);
308 if (prec != INT_MAX && last.precision != INT_MAX)
310 if (equal_width)
312 /* increase first_width by any increased precision in step */
313 size_t first_width = first.width + (prec - first.precision);
314 /* adjust last_width to use precision from first/step */
315 size_t last_width = last.width + (prec - last.precision);
316 if (last.precision && prec == 0)
317 last_width--; /* don't include space for '.' */
318 if (last.precision == 0 && prec)
319 last_width++; /* include space for '.' */
320 size_t width = MAX (first_width, last_width);
321 if (width <= INT_MAX)
323 int w = width;
324 sprintf (format_buf, "%%0%d.%dLf", w, prec);
325 return format_buf;
328 else
330 sprintf (format_buf, "%%.%dLf", prec);
331 return format_buf;
335 return "%Lg";
338 /* The NUL-terminated string S0 of length S_LEN represents a valid
339 non-negative decimal integer. Adjust the string and length so
340 that the pair describe the next-larger value. */
341 static void
342 incr (char **s0, size_t *s_len)
344 char *s = *s0;
345 char *endp = s + *s_len - 1;
349 if ((*endp)++ < '9')
350 return;
351 *endp-- = '0';
353 while (endp >= s);
354 *--(*s0) = '1';
355 ++*s_len;
358 /* Compare A and B (each a NUL-terminated digit string), with lengths
359 given by A_LEN and B_LEN. Return +1 if A < B, -1 if B < A, else 0. */
360 static int
361 cmp (char const *a, size_t a_len, char const *b, size_t b_len)
363 if (a_len < b_len)
364 return -1;
365 if (b_len < a_len)
366 return 1;
367 return (strcmp (a, b));
370 /* Trim leading 0's from S, but if S is all 0's, leave one.
371 Return a pointer to the trimmed string. */
372 static char const * _GL_ATTRIBUTE_PURE
373 trim_leading_zeros (char const *s)
375 char const *p = s;
376 while (*s == '0')
377 ++s;
379 /* If there were only 0's, back up, to leave one. */
380 if (!*s && s != p)
381 --s;
382 return s;
385 /* Print all whole numbers from A to B, inclusive -- to stdout, each
386 followed by a newline. If B < A, return false and print nothing.
387 Otherwise, return true. */
388 static bool
389 seq_fast (char const *a, char const *b)
391 /* Skip past any leading 0's. Without this, our naive cmp
392 function would declare 000 to be larger than 99. */
393 a = trim_leading_zeros (a);
394 b = trim_leading_zeros (b);
396 size_t p_len = strlen (a);
397 size_t q_len = strlen (b);
398 size_t n = MAX (p_len, q_len);
399 char *p0 = xmalloc (n + 1);
400 char *p = memcpy (p0 + n - p_len, a, p_len + 1);
401 char *q0 = xmalloc (n + 1);
402 char *q = memcpy (q0 + n - q_len, b, q_len + 1);
404 bool ok = cmp (p, p_len, q, q_len) <= 0;
405 if (ok)
407 /* Buffer at least this many output lines per fwrite call.
408 This gives a speed-up of more than 2x over the unbuffered code
409 when printing the first 10^9 integers. */
410 enum {N = 40};
411 char *buf = xmalloc (N * (n + 1));
412 char const *buf_end = buf + N * (n + 1);
414 puts (p);
415 char *z = buf;
416 while (cmp (p, p_len, q, q_len) < 0)
418 incr (&p, &p_len);
419 z = mempcpy (z, p, p_len);
420 *z++ = *separator;
421 if (buf_end - n - 1 < z)
423 fwrite (buf, z - buf, 1, stdout);
424 z = buf;
428 /* Write any remaining, buffered output. */
429 if (buf < z)
430 fwrite (buf, z - buf, 1, stdout);
432 IF_LINT (free (buf));
435 free (p0);
436 free (q0);
437 return ok;
440 /* Return true if S consists of at least one digit and no non-digits. */
441 static bool _GL_ATTRIBUTE_PURE
442 all_digits_p (char const *s)
444 size_t n = strlen (s);
445 return ISDIGIT (s[0]) && n == strspn (s, "0123456789");
449 main (int argc, char **argv)
451 int optc;
452 operand first = { 1, 1, 0 };
453 operand step = { 1, 1, 0 };
454 operand last;
455 struct layout layout = { 0, 0 };
457 /* The printf(3) format used for output. */
458 char const *format_str = NULL;
460 initialize_main (&argc, &argv);
461 set_program_name (argv[0]);
462 setlocale (LC_ALL, "");
463 bindtextdomain (PACKAGE, LOCALEDIR);
464 textdomain (PACKAGE);
466 atexit (close_stdout);
468 equal_width = false;
469 separator = "\n";
471 /* We have to handle negative numbers in the command line but this
472 conflicts with the command line arguments. So explicitly check first
473 whether the next argument looks like a negative number. */
474 while (optind < argc)
476 if (argv[optind][0] == '-'
477 && ((optc = argv[optind][1]) == '.' || ISDIGIT (optc)))
479 /* means negative number */
480 break;
483 optc = getopt_long (argc, argv, "+f:s:w", long_options, NULL);
484 if (optc == -1)
485 break;
487 switch (optc)
489 case 'f':
490 format_str = optarg;
491 break;
493 case 's':
494 separator = optarg;
495 break;
497 case 'w':
498 equal_width = true;
499 break;
501 case_GETOPT_HELP_CHAR;
503 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
505 default:
506 usage (EXIT_FAILURE);
510 unsigned int n_args = argc - optind;
511 if (n_args < 1)
513 error (0, 0, _("missing operand"));
514 usage (EXIT_FAILURE);
517 if (3 < n_args)
519 error (0, 0, _("extra operand %s"), quote (argv[optind + 3]));
520 usage (EXIT_FAILURE);
523 if (format_str)
524 format_str = long_double_format (format_str, &layout);
526 if (format_str != NULL && equal_width)
528 error (0, 0, _("format string may not be specified"
529 " when printing equal width strings"));
530 usage (EXIT_FAILURE);
533 /* If the following hold:
534 - no format string, [FIXME: relax this, eventually]
535 - integer start (or no start)
536 - integer end
537 - increment == 1 or not specified [FIXME: relax this, eventually]
538 then use the much more efficient integer-only code. */
539 if (all_digits_p (argv[optind])
540 && (n_args == 1 || all_digits_p (argv[optind + 1]))
541 && (n_args < 3 || STREQ ("1", argv[optind + 2]))
542 && !equal_width && !format_str && strlen (separator) == 1)
544 char const *s1 = n_args == 1 ? "1" : argv[optind];
545 char const *s2 = n_args == 1 ? argv[optind] : argv[optind + 1];
546 if (seq_fast (s1, s2))
547 exit (EXIT_SUCCESS);
549 /* Upon any failure, let the more general code deal with it. */
552 last = scan_arg (argv[optind++]);
554 if (optind < argc)
556 first = last;
557 last = scan_arg (argv[optind++]);
559 if (optind < argc)
561 step = last;
562 last = scan_arg (argv[optind++]);
566 if (first.precision == 0 && step.precision == 0 && last.precision == 0
567 && 0 <= first.value && step.value == 1 && 0 <= last.value
568 && !equal_width && !format_str && strlen (separator) == 1)
570 char *s1;
571 char *s2;
572 if (asprintf (&s1, "%0.Lf", first.value) < 0)
573 xalloc_die ();
574 if (asprintf (&s2, "%0.Lf", last.value) < 0)
575 xalloc_die ();
577 if (seq_fast (s1, s2))
579 IF_LINT (free (s1));
580 IF_LINT (free (s2));
581 exit (EXIT_SUCCESS);
584 free (s1);
585 free (s2);
586 /* Upon any failure, let the more general code deal with it. */
589 if (format_str == NULL)
590 format_str = get_default_format (first, step, last);
592 print_numbers (format_str, layout, first.value, step.value, last.value);
594 exit (EXIT_SUCCESS);