From 76f606a9544bd67629bd4766ff6efe8bf42b20d9 Mon Sep 17 00:00:00 2001 From: Pádraig Brady Date: Wed, 19 Dec 2012 19:27:10 +0000 Subject: seq: fix newline output when -s specified This regression was introduced in commit v8.19-132-g3786fb6. * src/seq.c (seq_fast): Don't use puts() to output the first number, and instead insert it into the buffer as for other numbers. Also output the terminator unconditionally. * tests/misc/seq.pl: Add some basic tests for the -s option. * NEWS: Mention the fix. * THANKS.in: Reported by Philipp Gortan. --- src/seq.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/seq.c b/src/seq.c index 9c2c51fbe..108808b2a 100644 --- a/src/seq.c +++ b/src/seq.c @@ -419,30 +419,36 @@ seq_fast (char const *a, char const *b) bool ok = cmp (p, p_len, q, q_len) <= 0; if (ok) { - /* Buffer at least this many output lines per fwrite call. + /* Buffer at least this many numbers per fwrite call. This gives a speed-up of more than 2x over the unbuffered code when printing the first 10^9 integers. */ enum {N = 40}; char *buf = xmalloc (N * (n + 1)); char const *buf_end = buf + N * (n + 1); - puts (p); char *z = buf; + + /* Write first number to buffer. */ + z = mempcpy (z, p, p_len); + + /* Append separator then number. */ while (cmp (p, p_len, q, q_len) < 0) { + *z++ = *separator; incr (&p, &p_len); z = mempcpy (z, p, p_len); - *z++ = *separator; - if (buf_end - n - 1 < z) + /* If no place for another separator + number then + output buffer so far, and reset to start of buffer. */ + if (buf_end - (n + 1) < z) { fwrite (buf, z - buf, 1, stdout); z = buf; } } - /* Write any remaining, buffered output. */ - if (buf < z) - fwrite (buf, z - buf, 1, stdout); + /* Write any remaining buffered output, and the terminator. */ + *z++ = *terminator; + fwrite (buf, z - buf, 1, stdout); IF_LINT (free (buf)); } -- cgit v1.2.3-54-g00ecf