Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@ pub fn cmp(params: &Params) -> Result<Cmp, String> {

let mut at_byte: BytesLimitU64 = 1;
let mut at_line: u64 = 1;
// Only the default report gives a line number: -s prints nothing and -l
// prints byte offsets.
let count_lines = !params.quiet && !params.verbose;
let mut start_of_line = true;
let mut stdout = BufWriter::new(io::stdout().lock());
let mut compare = Cmp::Equal;
Expand Down Expand Up @@ -370,7 +373,9 @@ pub fn cmp(params: &Params) -> Result<Cmp, String> {
let last = from_buf[..consumed].last().unwrap();

at_byte += consumed as BytesLimitU64;
at_line += (from_buf[..consumed].iter().filter(|&c| *c == b'\n').count()) as u64;
if count_lines {
at_line += count_newlines(&from_buf[..consumed]);
}

start_of_line = *last == b'\n';

Expand Down Expand Up @@ -436,6 +441,14 @@ pub fn cmp(params: &Params) -> Result<Cmp, String> {
Ok(compare)
}

/// Counts the newlines in `buf`. Summing each chunk in a `u8` lets the compiler
/// compare many bytes at once, and a chunk of 255 bytes can't overflow it.
fn count_newlines(buf: &[u8]) -> u64 {
buf.chunks(255)
.map(|chunk| u64::from(chunk.iter().fold(0u8, |n, &b| n + u8::from(b == b'\n'))))
.sum()
}

// Exit codes are documented at
// https://www.gnu.org/software/diffutils/manual/html_node/Invoking-cmp.html
// An exit status of 0 means no differences were found,
Expand Down Expand Up @@ -709,6 +722,15 @@ mod tests {
OsString::from(s)
}

#[test]
fn count_newlines_across_chunks() {
assert_eq!(count_newlines(b""), 0);
assert_eq!(count_newlines(b"no newline"), 0);
// more than one 255-byte chunk of nothing but newlines
assert_eq!(count_newlines(&[b'\n'; 1000]), 1000);
assert_eq!(count_newlines(&b"ab\ncd\n".repeat(300)), 600);
}

#[test]
fn positional() {
assert_eq!(
Expand Down
23 changes: 23 additions & 0 deletions tests/by-util/test_cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,29 @@ fn cmp_fast_paths() {
.stdout_str_check(|s| s.ends_with(" differ: byte 24577, line 4\n"));
}

// Many newlines spread over several read buffers must still give the right
// line number, both for a difference and for an EOF.
#[test]
fn cmp_line_number_after_many_lines() {
let (at, ts) = at_and_ts!();
let lines = "x\n".repeat(30_000);

at.write("a", &format!("{lines}A"));
at.write("b", &format!("{lines}B"));
ts.ucmd()
.args(&["a", "b"])
.env("LC_ALL", "C")
.fails_with_code(1)
.stdout_only("a b differ: char 60001, line 30001\n");

at.write("a", &lines);
at.write("b", &format!("{lines}more"));
ts.ucmd()
.args(&["a", "b"])
.fails_with_code(1)
.stderr_str_check(|s| s.ends_with(" after byte 60000, line 30000\n"));
}

// LC_ALL overrides LC_MESSAGES, which overrides LANG; empty values are unset.
#[rstest]
#[case::nothing_set(&[], "char")]
Expand Down
Loading