summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2023-03-04 11:41:02 -0800
committerPaul Eggert <eggert@cs.ucla.edu>2023-03-04 14:49:45 -0800
commitf749449e5c8d687468476f39c4a05cb77f0dd433 (patch)
treeca17114254fb571cdf3cc63c74df3fd96a441149
parent0789451237b90324a5d1f5c1b9fbaa5099f4c072 (diff)
downloadcoreutils-f749449e5c8d687468476f39c4a05cb77f0dd433.tar.gz
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.
-rw-r--r--src/split.c12
1 files changed, 6 insertions, 6 deletions
diff --git a/src/split.c b/src/split.c
index 74b389f85..d3d1dde8c 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);