From fc180591aac039f38592a2b47368cb65c9878460 Mon Sep 17 00:00:00 2001 From: Shaurya Kesarwani Date: Mon, 26 Oct 2026 12:10:53 +0530 Subject: [PATCH] cmp: don't pad the printed bytes when the second byte is non-ASCII With -b, the character column after the first byte was padded to two columns whenever the second byte was 0x7F or higher, so a printable first byte got an extra space ("141 a 303 M-C"). GNU cmp prints the two bytes separated by a single space in every case. --- src/cmp.rs | 3 +-- tests/by-util/test_cmp.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/cmp.rs b/src/cmp.rs index 3ff716f..dc212d2 100644 --- a/src/cmp.rs +++ b/src/cmp.rs @@ -703,9 +703,8 @@ fn report_difference( at_line ); if params.print_bytes { - let char_width = if to_byte >= 0x7F { 2 } else { 1 }; print!( - " is {:>3o} {:char_width$} {:>3o} {:char_width$}", + " is {:>3o} {} {:>3o} {}", from_byte, format_visible_byte(from_byte), to_byte, diff --git a/tests/by-util/test_cmp.rs b/tests/by-util/test_cmp.rs index 0053a46..a36d16a 100644 --- a/tests/by-util/test_cmp.rs +++ b/tests/by-util/test_cmp.rs @@ -452,6 +452,34 @@ fn cmp_skip() -> Result<(), Box> { Ok(()) } +#[test] +fn cmp_print_bytes_high_byte() -> Result<(), Box> { + let tmp_dir = tempdir()?; + + let a_path = tmp_dir.path().join("a"); + let mut a = File::create(&a_path).unwrap(); + a.write_all(b"a").unwrap(); + + let b_path = tmp_dir.path().join("b"); + let mut b = File::create(&b_path).unwrap(); + b.write_all(&[0xC3]).unwrap(); + + // GNU cmp does not pad the printed bytes, whatever the second byte is. + let mut cmd = cargo_bin_cmd!("diffutils"); + cmd.arg("cmp"); + cmd.arg("-b"); + cmd.arg(&a_path).arg(&b_path); + cmd.assert() + .code(predicate::eq(1)) + .failure() + .stderr(predicate::str::is_empty()) + .stdout(predicate::str::ends_with( + " differ: byte 1, line 1 is 141 a 303 M-C\n", + )); + + Ok(()) +} + #[test] fn cmp_binary() -> Result<(), Box> { let tmp_dir = tempdir()?;