From: Paul Eggert Date: Sat, 4 Mar 2023 19:41:02 +0000 (-0800) Subject: split: refactor lines_chunk_split X-Git-Tag: v9.2~37 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f749449e5c8d687468476f39c4a05cb77f0dd433;p=thirdparty%2Fcoreutils.git split: refactor lines_chunk_split * src/split.c (lines_chunk_split): Simplify by having chunk_end point to the first byte after the chunk, rather than to the last byte of the chunk. This will reduce confusion once we allow chunks to be empty. --- diff --git a/src/split.c b/src/split.c index 74b389f852..d3d1dde8c2 100644 --- a/src/split.c +++ b/src/split.c @@ -868,7 +868,7 @@ lines_chunk_split (uintmax_t k, uintmax_t n, char *buf, size_t bufsize, const off_t chunk_size = file_size / n; uintmax_t chunk_no = 1; - off_t chunk_end = chunk_size - 1; + off_t chunk_end = chunk_size; off_t n_written = 0; bool new_file_flag = true; bool chunk_truncated = false; @@ -890,7 +890,7 @@ lines_chunk_split (uintmax_t k, uintmax_t n, char *buf, size_t bufsize, } n_written = start; chunk_no = k - 1; - chunk_end = chunk_no * chunk_size - 1; + chunk_end = chunk_no * chunk_size; } while (n_written < file_size) @@ -920,7 +920,7 @@ lines_chunk_split (uintmax_t k, uintmax_t n, char *buf, size_t bufsize, bool next = false; /* Begin looking for '\n' at last byte of chunk. */ - off_t skip = MIN (n_read, MAX (0, chunk_end - n_written)); + off_t skip = MIN (n_read, MAX (0, chunk_end - 1 - n_written)); char *bp_out = memchr (bp + skip, eolchar, n_read - skip); if (bp_out) { @@ -948,7 +948,7 @@ lines_chunk_split (uintmax_t k, uintmax_t n, char *buf, size_t bufsize, /* A line could have been so long that it skipped entire chunks. So create empty files in that case. */ - while (next || chunk_end <= n_written - 1) + while (next || chunk_end <= n_written) { if (!next && bp == eob) { @@ -960,10 +960,10 @@ lines_chunk_split (uintmax_t k, uintmax_t n, char *buf, size_t bufsize, if (k && chunk_no > k) return; if (chunk_no == n) - chunk_end = file_size - 1; /* >= chunk_size. */ + chunk_end = file_size; /* >= chunk_size. */ else chunk_end += chunk_size; - if (chunk_end <= n_written - 1) + if (chunk_end <= n_written) { if (! k) cwrite (true, NULL, 0);