From f0d5b990328889507bea4faec9eee3b2829614a9 Mon Sep 17 00:00:00 2001 From: Krushn Date: Fri, 20 Nov 2026 09:46:42 +0530 Subject: [PATCH] cmp: count lines only when they are printed, and count them faster --- src/cmp.rs | 24 +++++++++++++++++++++++- tests/by-util/test_cmp.rs | 23 +++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/cmp.rs b/src/cmp.rs index d1121ce6..7dffa859 100644 --- a/src/cmp.rs +++ b/src/cmp.rs @@ -334,6 +334,9 @@ pub fn cmp(params: &Params) -> Result { 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; @@ -370,7 +373,9 @@ pub fn cmp(params: &Params) -> Result { 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'; @@ -436,6 +441,14 @@ pub fn cmp(params: &Params) -> Result { 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, @@ -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!( diff --git a/tests/by-util/test_cmp.rs b/tests/by-util/test_cmp.rs index d7ad3a1f..cdd5bc91 100644 --- a/tests/by-util/test_cmp.rs +++ b/tests/by-util/test_cmp.rs @@ -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")]