1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-08-14 22:19:02 +02:00

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.
This commit is contained in:
Paul Eggert
2023-03-04 11:41:02 -08:00
parent 0789451237
commit f749449e5c
+6 -6
View File
@@ -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);