From c6e36f4fd2cbd0f0199535d2434f8cab98f0347b Mon Sep 17 00:00:00 2001 From: Milan Pramod Date: Tue, 17 Nov 2026 05:57:33 +0530 Subject: [PATCH 1/8] refactor: replace diff crate with ses for sequence comparison - Replace the external `diff` crate dependency with the `ses` crate across all diff modules - Update pattern matching in context, ed, normal, side, and unified diff implementations to use `ses::Change` variants (`Delete`, `Insert`, `Equal`) --- Cargo.lock | 8 +++++++- Cargo.toml | 2 +- src/context_diff.rs | 9 +++++---- src/ed_diff.rs | 9 +++++---- src/normal_diff.rs | 11 +++++++---- src/side_diff.rs | 17 +++++++++++------ src/unified_diff.rs | 9 +++++---- 7 files changed, 41 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 99188bb2..52eacbff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -301,13 +301,13 @@ dependencies = [ "chrono", "codspeed-divan-compat", "ctor", - "diff", "itoa", "pretty_assertions", "rand", "regex", "rstest", "same-file", + "ses", "tempfile", "unicode-width", "uutests", @@ -974,6 +974,12 @@ dependencies = [ "zmij", ] +[[package]] +name = "ses" +version = "0.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2e023ba18d7a09e2d8e01822d9dbd1c5f4710f0a47f9eabd51b1cb8bdf0ccdc" + [[package]] name = "shlex" version = "2.0.1" diff --git a/Cargo.toml b/Cargo.toml index 5a433257..df524a61 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,10 +18,10 @@ path = "src/main.rs" [dependencies] chrono = "0.4.38" -diff = "0.1.13" itoa = "1.0.11" regex = "1.10.4" same-file = "1.0.6" +ses = "0.0.2" unicode-width = "0.2.0" [dev-dependencies] diff --git a/src/context_diff.rs b/src/context_diff.rs index 790d58fa..a36f98fa 100644 --- a/src/context_diff.rs +++ b/src/context_diff.rs @@ -129,9 +129,10 @@ fn make_diff( let mut block: Option = None; - for result in diff::slice(&expected_lines, &actual_lines) { + let differences = ses::diff_slices(&expected_lines, &actual_lines); + for result in differences.iter_changes() { match result { - diff::Result::Left(str) => { + ses::Change::Delete(str) => { if lines_since_mismatch > max_gap { results.push(mismatch); mismatch = Mismatch::new( @@ -153,7 +154,7 @@ fn make_diff( line_number_expected += 1; lines_since_mismatch = 0; } - diff::Result::Right(str) => { + ses::Change::Insert(str) => { if lines_since_mismatch > max_gap { results.push(mismatch); mismatch = Mismatch::new( @@ -175,7 +176,7 @@ fn make_diff( line_number_actual += 1; lines_since_mismatch = 0; } - diff::Result::Both(str, _) => { + ses::Change::Equal(str, _) => { // if one of them is missing a newline and the other isn't, then they don't actually match if (line_number_actual > actual_lines_count) && (line_number_expected > expected_lines_count) diff --git a/src/ed_diff.rs b/src/ed_diff.rs index 2f75adfa..675d77dc 100644 --- a/src/ed_diff.rs +++ b/src/ed_diff.rs @@ -71,9 +71,10 @@ fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Result { + ses::Change::Delete(str) => { if !mismatch.actual.is_empty() { results.push(mismatch); mismatch = Mismatch::new(line_number_expected, line_number_actual); @@ -81,11 +82,11 @@ fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Result { + ses::Change::Insert(str) => { mismatch.actual.push(str.to_vec()); line_number_actual += 1; } - diff::Result::Both(_str, _) => { + ses::Change::Equal(_str, _) => { line_number_expected += 1; line_number_actual += 1; if !mismatch.actual.is_empty() || !mismatch.expected.is_empty() { diff --git a/src/normal_diff.rs b/src/normal_diff.rs index c930b957..857983d6 100644 --- a/src/normal_diff.rs +++ b/src/normal_diff.rs @@ -5,6 +5,8 @@ use std::io::Write; +use ses::Change; + use crate::params::Params; use crate::utils::do_write_line; @@ -54,9 +56,10 @@ fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Vec actual_lines.pop(); } - for result in diff::slice(&expected_lines, &actual_lines) { + let differences = ses::diff_slices(&expected_lines, &actual_lines); + for result in differences.iter_changes() { match result { - diff::Result::Left(str) => { + Change::Delete(str) => { if !mismatch.actual.is_empty() && !mismatch.actual_missing_nl { results.push(mismatch); mismatch = Mismatch::new(line_number_expected, line_number_actual); @@ -65,12 +68,12 @@ fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Vec mismatch.expected_missing_nl = line_number_expected > expected_lines_count; line_number_expected += 1; } - diff::Result::Right(str) => { + Change::Insert(str) => { mismatch.actual.push(str.to_vec()); mismatch.actual_missing_nl = line_number_actual > actual_lines_count; line_number_actual += 1; } - diff::Result::Both(str, _) => { + Change::Equal(str, _) => { match ( line_number_expected > expected_lines_count, line_number_actual > actual_lines_count, diff --git a/src/side_diff.rs b/src/side_diff.rs index faebfd19..6f258a53 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -4,7 +4,6 @@ // files that was distributed with this source code. use core::cmp::{max, min}; -use diff::Result; use std::{io::Write, vec}; use unicode_width::UnicodeWidthStr; @@ -350,11 +349,17 @@ pub fn diff( More studies are needed to cover GNU diff side by side with 100% accuracy, which is one of the goals of this project : ) */ - for result in diff::slice(&left_lines, &right_lines) { - match result { - Result::Left(left_ln) => push_output(left_ln, b"", b'<', output, &config).unwrap(), - Result::Right(right_ln) => push_output(b"", right_ln, b'>', output, &config).unwrap(), - Result::Both(left_ln, right_ln) => { + let differences = ses::diff_slices(&left_lines, &right_lines); + + for change in differences.iter_changes() { + match change { + ses::Change::Delete(left_ln) => { + push_output(left_ln, b"", b'<', output, &config).unwrap() + } + ses::Change::Insert(right_ln) => { + push_output(b"", right_ln, b'>', output, &config).unwrap() + } + ses::Change::Equal(left_ln, right_ln) => { push_output(left_ln, right_ln, b' ', output, &config).unwrap() } } diff --git a/src/unified_diff.rs b/src/unified_diff.rs index 32f227de..ff2c4913 100644 --- a/src/unified_diff.rs +++ b/src/unified_diff.rs @@ -68,9 +68,10 @@ fn make_diff( actual_lines.pop(); } - for result in diff::slice(&expected_lines, &actual_lines) { + let differences = ses::diff_slices(&expected_lines, &actual_lines); + for result in differences.iter_changes() { match result { - diff::Result::Left(str) => { + ses::Change::Delete(str) => { if lines_since_mismatch > max_gap { results.push(mismatch); mismatch = Mismatch::new( @@ -107,7 +108,7 @@ fn make_diff( line_number_expected += 1; lines_since_mismatch = 0; } - diff::Result::Right(str) => { + ses::Change::Insert(str) => { if lines_since_mismatch > max_gap { results.push(mismatch); mismatch = Mismatch::new( @@ -128,7 +129,7 @@ fn make_diff( line_number_actual += 1; lines_since_mismatch = 0; } - diff::Result::Both(str, _) => { + ses::Change::Equal(str, _) => { // if one of them is missing a newline and the other isn't, then they don't actually match if (line_number_actual > actual_lines_count) && (line_number_expected > expected_lines_count) From ae96980c3c5764520115f5b3335032b6d95a87cf Mon Sep 17 00:00:00 2001 From: Tejesh <188414140+tridev-tej@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:23:48 +0400 Subject: [PATCH 2/8] diff: add --suppress-blank-empty empty lines are written without the blanks of their marker in the normal, context and unified formats. the context colors still apply to them. --- src/color.rs | 24 +++++++++++++++++++++--- src/context_diff.rs | 14 +++++++------- src/normal_diff.rs | 6 +++--- src/params.rs | 6 ++++++ src/unified_diff.rs | 8 ++++---- src/utils.rs | 12 ++++++++++++ tests/by-util/test_diff.rs | 15 +++++++++++++++ 7 files changed, 68 insertions(+), 17 deletions(-) diff --git a/src/color.rs b/src/color.rs index d82b6e58..cc73c351 100644 --- a/src/color.rs +++ b/src/color.rs @@ -119,11 +119,15 @@ pub fn colorize(output: &[u8], format: Format, palette: &Palette) -> Vec { } else if line.starts_with(b"--- ") { in_new_file_part = true; LineKind::LineNumbers - } else if line.starts_with(b"- ") { + } else if line.starts_with(b"- ") || line == b"-\n" { LineKind::Deleted - } else if line.starts_with(b"+ ") { + } else if line.starts_with(b"+ ") || line == b"+\n" { LineKind::Added - } else if line.starts_with(b"! ") || line.starts_with(b" ") { + } else if line.starts_with(b"! ") + || line.starts_with(b" ") + || line == b"!\n" + || line == b"\n" + { if in_new_file_part { LineKind::Added } else { @@ -220,6 +224,20 @@ mod tests { ); } + #[test] + fn context_lines_without_blank_after_the_marker() { + let output = "*** left\n--- right\n***************\n*** 1,3 ****\n-\n\n! bee\n\ + --- 1,3 ----\n\n! fly\n!\n+\n"; + assert_eq!( + colorize_str(output, Format::Context), + "\x1b[1m*** left\x1b[0m\n\x1b[1m--- right\x1b[0m\n***************\n\ + \x1b[36m*** 1,3 ****\x1b[0m\n\x1b[31m-\x1b[0m\n\x1b[31m\x1b[0m\n\ + \x1b[31m! bee\x1b[0m\n\ + \x1b[36m--- 1,3 ----\x1b[0m\n\x1b[32m\x1b[0m\n\x1b[32m! fly\x1b[0m\n\ + \x1b[32m!\x1b[0m\n\x1b[32m+\x1b[0m\n" + ); + } + #[test] fn missing_newline_marker_is_plain() { assert_eq!( diff --git a/src/context_diff.rs b/src/context_diff.rs index a36f98fa..c9b99288 100644 --- a/src/context_diff.rs +++ b/src/context_diff.rs @@ -7,7 +7,7 @@ use std::collections::VecDeque; use std::io::Write; use crate::params::Params; -use crate::utils::do_write_line; +use crate::utils::{do_write_line, line_prefix}; use crate::utils::{ get_modification_time, is_posix_locale, write_file_header, FunctionLineFinder, TimestampStyle, }; @@ -385,19 +385,19 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { for line in result.expected { match line { DiffLine::Context(e) => { - write!(output, " ").expect("write to Vec is infallible"); + output.extend_from_slice(line_prefix(b" ", &e, params)); do_write_line(&mut output, &e, params.expand_tabs, params.tabsize) .expect("write to Vec is infallible"); writeln!(output).unwrap(); } DiffLine::Change(e) => { - write!(output, "! ").expect("write to Vec is infallible"); + output.extend_from_slice(line_prefix(b"! ", &e, params)); do_write_line(&mut output, &e, params.expand_tabs, params.tabsize) .expect("write to Vec is infallible"); writeln!(output).unwrap(); } DiffLine::Add(e) => { - write!(output, "- ").expect("write to Vec is infallible"); + output.extend_from_slice(line_prefix(b"- ", &e, params)); do_write_line(&mut output, &e, params.expand_tabs, params.tabsize) .expect("write to Vec is infallible"); writeln!(output).unwrap(); @@ -415,19 +415,19 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { for line in result.actual { match line { DiffLine::Context(e) => { - write!(output, " ").expect("write to Vec is infallible"); + output.extend_from_slice(line_prefix(b" ", &e, params)); do_write_line(&mut output, &e, params.expand_tabs, params.tabsize) .expect("write to Vec is infallible"); writeln!(output).unwrap(); } DiffLine::Change(e) => { - write!(output, "! ").expect("write to Vec is infallible"); + output.extend_from_slice(line_prefix(b"! ", &e, params)); do_write_line(&mut output, &e, params.expand_tabs, params.tabsize) .expect("write to Vec is infallible"); writeln!(output).unwrap(); } DiffLine::Add(e) => { - write!(output, "+ ").expect("write to Vec is infallible"); + output.extend_from_slice(line_prefix(b"+ ", &e, params)); do_write_line(&mut output, &e, params.expand_tabs, params.tabsize) .expect("write to Vec is infallible"); writeln!(output).unwrap(); diff --git a/src/normal_diff.rs b/src/normal_diff.rs index 857983d6..a709da34 100644 --- a/src/normal_diff.rs +++ b/src/normal_diff.rs @@ -8,7 +8,7 @@ use std::io::Write; use ses::Change; use crate::params::Params; -use crate::utils::do_write_line; +use crate::utils::{do_write_line, line_prefix}; #[derive(Debug, PartialEq)] struct Mismatch { @@ -205,7 +205,7 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { .unwrap(), } for expected in &result.expected { - write!(&mut output, "< ").unwrap(); + output.extend_from_slice(line_prefix(b"< ", expected, params)); do_write_line(&mut output, expected, params.expand_tabs, params.tabsize).unwrap(); writeln!(&mut output).unwrap(); } @@ -216,7 +216,7 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { writeln!(&mut output, "---").unwrap(); } for actual in &result.actual { - write!(&mut output, "> ").unwrap(); + output.extend_from_slice(line_prefix(b"> ", actual, params)); do_write_line(&mut output, actual, params.expand_tabs, params.tabsize).unwrap(); writeln!(&mut output).unwrap(); } diff --git a/src/params.rs b/src/params.rs index b7239181..73ea45e0 100644 --- a/src/params.rs +++ b/src/params.rs @@ -47,6 +47,7 @@ pub struct Params { pub report_identical_files: bool, pub brief: bool, pub expand_tabs: bool, + pub suppress_blank_empty: bool, pub tabsize: usize, pub width: usize, pub missing_file: MissingFile, @@ -97,6 +98,7 @@ impl Default for Params { report_identical_files: false, brief: false, expand_tabs: false, + suppress_blank_empty: false, tabsize: 8, width: 130, missing_file: MissingFile::default(), @@ -275,6 +277,10 @@ pub fn parse_params>( params.expand_tabs = true; continue; } + if param == "--suppress-blank-empty" { + params.suppress_blank_empty = true; + continue; + } if param == "--normal" { if format.is_some() && format != Some(Format::Normal) { return Err("Conflicting output style options".to_string()); diff --git a/src/unified_diff.rs b/src/unified_diff.rs index ff2c4913..60c4bf4f 100644 --- a/src/unified_diff.rs +++ b/src/unified_diff.rs @@ -7,7 +7,7 @@ use std::collections::VecDeque; use std::io::Write; use crate::params::Params; -use crate::utils::do_write_line; +use crate::utils::{do_write_line, line_prefix}; use crate::utils::{get_modification_time, write_file_header, FunctionLineFinder, TimestampStyle}; #[derive(Debug, PartialEq)] @@ -399,19 +399,19 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { for line in result.lines { match line { DiffLine::Expected(e) => { - write!(output, "-").expect("write to Vec is infallible"); + output.extend_from_slice(line_prefix(b"-", &e, params)); do_write_line(&mut output, &e, params.expand_tabs, params.tabsize) .expect("write to Vec is infallible"); writeln!(output).unwrap(); } DiffLine::Context(c) => { - write!(output, " ").expect("write to Vec is infallible"); + output.extend_from_slice(line_prefix(b" ", &c, params)); do_write_line(&mut output, &c, params.expand_tabs, params.tabsize) .expect("write to Vec is infallible"); writeln!(output).unwrap(); } DiffLine::Actual(r) => { - write!(output, "+",).expect("write to Vec is infallible"); + output.extend_from_slice(line_prefix(b"+", &r, params)); do_write_line(&mut output, &r, params.expand_tabs, params.tabsize) .expect("write to Vec is infallible"); writeln!(output).unwrap(); diff --git a/src/utils.rs b/src/utils.rs index 645fece6..522f32d0 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -4,6 +4,7 @@ // files that was distributed with this source code. use crate::bre::Bre; +use crate::params::Params; use regex::Regex; use std::{ borrow::Cow, @@ -63,6 +64,17 @@ pub fn do_write_line( } } +/// The marker to write before a line of the normal, context or unified +/// format: `prefix`, without its trailing blanks if `line` is empty and +/// `--suppress-blank-empty` is given. +pub fn line_prefix<'a>(prefix: &'a [u8], line: &[u8], params: &Params) -> &'a [u8] { + if params.suppress_blank_empty && line.is_empty() { + prefix.trim_ascii_end() + } else { + prefix + } +} + /// Quotes a file name for the headers of the diff output: a name containing /// spaces, double quotes, backslashes, control characters or non-ASCII bytes /// is written between double quotes with C-style escapes, other names are diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index c251c39b..cbbbfa2b 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -993,6 +993,21 @@ fn trailing_context_without_final_newlines(#[case] format: &str, #[case] hunk: & .stdout_str_check(|s| s.ends_with(hunk)); } +#[rstest] +#[case::normal("--normal", "1d0\n<\n4c3,4\n< b\n---\n> c\n>\n")] +#[case::unified("-u", "@@ -1,4 +1,4 @@\n-\n a\n\n-b\n+c\n+\n")] +#[case::context("-c", "*** 1,4 ****\n-\n a\n\n! b\n--- 1,4 ----\n a\n\n! c\n!\n")] +fn suppress_blank_empty(#[case] format: &str, #[case] hunk: &str) { + let (at, ts) = at_and_ts!(); + at.write("file1", "\na\n\nb\n"); + at.write("file2", "a\n\nc\n\n"); + + ts.ucmd() + .args(&[format, "--suppress-blank-empty", "file1", "file2"]) + .fails_with_code(1) + .stdout_str_check(|s| s.ends_with(hunk)); +} + #[rstest] #[case::unified( &["-u", "--label", "a/x", "--label=b/x"], From b3ea32f6465716ca302154bc24ea4041b9e2a269 Mon Sep 17 00:00:00 2001 From: Milan Pramod Date: Wed, 18 Nov 2026 21:19:31 +0530 Subject: [PATCH 3/8] feat: update ses to v0.0.3 --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 52eacbff..16ae7e5f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -976,9 +976,9 @@ dependencies = [ [[package]] name = "ses" -version = "0.0.2" +version = "0.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2e023ba18d7a09e2d8e01822d9dbd1c5f4710f0a47f9eabd51b1cb8bdf0ccdc" +checksum = "ce129eaeb085b1818a876f6c8b30122db30e4a5d1e75cb474d6b65b6a3e40f7d" [[package]] name = "shlex" diff --git a/Cargo.toml b/Cargo.toml index df524a61..3f6f35e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ chrono = "0.4.38" itoa = "1.0.11" regex = "1.10.4" same-file = "1.0.6" -ses = "0.0.2" +ses = "0.0.3" unicode-width = "0.2.0" [dev-dependencies] From 69980da402bc6dd0cc7883d6fa811202ad0022f8 Mon Sep 17 00:00:00 2001 From: Milan Pramod Date: Wed, 18 Nov 2026 22:29:01 +0530 Subject: [PATCH 4/8] test(diff): add comprehensive edge case tests for GNU compatibilty --- tests/by-util/test_diff.rs | 242 +++++++++++++++++++++++++++++++++++++ 1 file changed, 242 insertions(+) diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index cbbbfa2b..d2fa16cb 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -1069,3 +1069,245 @@ fn label_errors(#[case] args: &[&str], #[case] error: &str) { .fails_with_code(2) .stderr_contains(error); } + +#[rstest] +// Symmetrical tie: repeated 'x' with shifting unique 'y' +#[case::abstract_symmetrical_tie("x\nx\ny\nx\nx\n", "x\ny\nx\nx\nx\n", "2d1\n< x\n3a3\n> x\n")] +// Asymmetrical tie 1: Delta = -1 (old has 4 lines, new has 5 lines) +#[case::asymmetric_tie_insert_bias( + "a\na\nb\na\n", + "a\nb\na\na\na\n", + "2d1\n< a\n3a3,4\n> a\n> a\n" +)] +// Asymmetrical tie 2: Delta = +1 (old has 5 lines, new has 4 lines) +#[case::asymmetric_tie_delete_bias( + "x\nx\nx\ny\nx\n", + "x\ny\nx\nx\n", + "2,3d1\n< x\n< x\n4a3\n> x\n" +)] +// Repeated brackets/braces near block end +#[case::braces_shift("}\n}\n}\nend\n", "}\n}\nend\n}\n", "3d2\n< }\n4a4\n> }\n")] +// Repeated code statements around unique variable assignment +#[case::code_statement_tie( + "fn test() {\n let a = 1;\n let a = 1;\n let a = 1;\n let b = 2;\n}\n", + "fn test() {\n let a = 1;\n let a = 1;\n let b = 2;\n let a = 1;\n}\n", + "4d3\n< let a = 1;\n5a5\n> let a = 1;\n" +)] +// Interleaved repeating pattern +#[case::interleaved_repeats( + "alpha\nbeta\nalpha\nbeta\nalpha\n", + "beta\nalpha\nbeta\nalpha\nbeta\n", + "1d0\n< alpha\n5a5\n> beta\n" +)] +// Repeated identical tokens at both start and end of hunk +#[case::repeated_anchors("A\nB\nA\nB\n", "B\nA\nB\nA\n", "1d0\n< A\n4a4\n> A\n")] +fn test_gnu_tie_breaking_edge_cases( + #[case] file1: &str, + #[case] file2: &str, + #[case] expected: &str, +) { + let (at, ts) = at_and_ts!(); + at.write("file1", file1); + at.write("file2", file2); + + ts.ucmd() + .args(&["file1", "file2"]) + .fails_with_code(1) + .stdout_is(expected); +} + +#[rstest] +// Simple 2-line adjacent swap +#[case::swap_adjacent_lines("alpha\nbeta\n", "beta\nalpha\n", "1d0\n< alpha\n2a2\n> alpha\n")] +// 3-line cyclic rotation (A B C -> B C A) +#[case::rotate_three_lines( + "first\nsecond\nthird\n", + "second\nthird\nfirst\n", + "1d0\n< first\n3a3\n> first\n" +)] +// 3-line full reversal (A B C -> C B A) +#[case::reverse_three_lines( + "one\ntwo\nthree\n", + "three\ntwo\none\n", + "1,2d0\n< one\n< two\n3a2,3\n> two\n> one\n" +)] +fn test_gnu_permutation_edge_cases( + #[case] file1: &str, + #[case] file2: &str, + #[case] expected: &str, +) { + let (at, ts) = at_and_ts!(); + at.write("file1", file1); + at.write("file2", file2); + + ts.ucmd() + .args(&["file1", "file2"]) + .fails_with_code(1) + .stdout_is(expected); +} + +#[rstest] +// Prepending lines at beginning of file +#[case::prepend_lines("two\nthree\n", "zero\none\ntwo\nthree\n", "0a1,2\n> zero\n> one\n")] +// Appending lines at end of file +#[case::append_lines("one\ntwo\n", "one\ntwo\nthree\nfour\n", "2a3,4\n> three\n> four\n")] +// Prepending AND appending in the same file +#[case::prepend_and_append("two\n", "one\ntwo\nthree\n", "0a1\n> one\n1a3\n> three\n")] +// Deleting all lines at the top of file +#[case::delete_head("one\ntwo\nthree\n", "three\n", "1,2d0\n< one\n< two\n")] +// Deleting all lines at the bottom of file +#[case::delete_tail("one\ntwo\nthree\n", "one\n", "2,3d1\n< two\n< three\n")] +// 100% disjoint files (every line replaced) +#[case::completely_different( + "foo\nbar\nbaz\n", + "one\ntwo\nthree\n", + "1,3c1,3\n< foo\n< bar\n< baz\n---\n> one\n> two\n> three\n" +)] +// Single character change +#[case::single_char_diff("a\n", "b\n", "1c1\n< a\n---\n> b\n")] +fn test_gnu_extremity_edge_cases(#[case] file1: &str, #[case] file2: &str, #[case] expected: &str) { + let (at, ts) = at_and_ts!(); + at.write("file1", file1); + at.write("file2", file2); + + ts.ucmd() + .args(&["file1", "file2"]) + .fails_with_code(1) + .stdout_is(expected); +} + +#[rstest] +// Many-to-one replacement +#[case::replace_many_to_one("a\nb\nc\n", "x\n", "1,3c1\n< a\n< b\n< c\n---\n> x\n")] +// One-to-many replacement +#[case::replace_one_to_many("x\n", "a\nb\nc\n", "1c1,3\n< x\n---\n> a\n> b\n> c\n")] +// Many-to-many replacement +#[case::replace_many_to_many( + "a\nb\nc\n", + "x\ny\nz\nw\n", + "1,3c1,4\n< a\n< b\n< c\n---\n> x\n> y\n> z\n> w\n" +)] +// Two distinct change hunks separated by common anchors +#[case::two_separated_hunks( + "hunk1_a\ncommon1\ncommon2\nhunk2_a\n", + "hunk1_b\ncommon1\ncommon2\nhunk2_b\n", + "1c1\n< hunk1_a\n---\n> hunk1_b\n4c4\n< hunk2_a\n---\n> hunk2_b\n" +)] +// Inserting unique lines into a block of repeated lines +#[case::insert_into_repeats( + "item\nitem\nitem\n", + "item\nitem\nnew_item\nitem\n", + "2a3\n> new_item\n" +)] +// Deleting unique line from a block of repeated lines +#[case::delete_from_repeats( + "item\nitem\nold_item\nitem\n", + "item\nitem\nitem\n", + "3d2\n< old_item\n" +)] +fn test_gnu_replacement_hunk_edge_cases( + #[case] file1: &str, + #[case] file2: &str, + #[case] expected: &str, +) { + let (at, ts) = at_and_ts!(); + at.write("file1", file1); + at.write("file2", file2); + + ts.ucmd() + .args(&["file1", "file2"]) + .fails_with_code(1) + .stdout_is(expected); +} + +#[rstest] +// Old file has no final newline, new file has newline +#[case::no_newline_old( + "hello", + "hello\n", + "1c1\n< hello\n\\ No newline at end of file\n---\n> hello\n" +)] +// Old file has newline, new file has no final newline +#[case::no_newline_new( + "hello\n", + "hello", + "1c1\n< hello\n---\n> hello\n\\ No newline at end of file\n" +)] +// Both files have no final newline and are different +#[case::both_no_newline( + "hello", + "world", + "1c1\n< hello\n\\ No newline at end of file\n---\n> world\n\\ No newline at end of file\n" +)] +// Multi-line file where replacement occurs on the line without newline +#[case::replace_on_unterminated_line( + "a\nb", + "a\nc", + "2c2\n< b\n\\ No newline at end of file\n---\n> c\n\\ No newline at end of file\n" +)] +fn test_gnu_missing_newline_edge_cases( + #[case] file1: &str, + #[case] file2: &str, + #[case] expected: &str, +) { + let (at, ts) = at_and_ts!(); + at.write("file1", file1); + at.write("file2", file2); + + ts.ucmd() + .args(&["file1", "file2"]) + .fails_with_code(1) + .stdout_is(expected); +} + +#[rstest] +#[case::unified_code_tie( + "fn test() {\n let a = 1;\n let a = 1;\n let a = 1;\n let b = 2;\n}\n", + "fn test() {\n let a = 1;\n let a = 1;\n let b = 2;\n let a = 1;\n}\n", + "@@ -1,6 +1,6 @@\n fn test() {\n let a = 1;\n let a = 1;\n- let a = 1;\n let b = 2;\n+ let a = 1;\n }\n" +)] +#[case::unified_swap_adjacent( + "apple\norange\nbanana\ngrape\n", + "apple\nbanana\norange\ngrape\n", + "@@ -1,4 +1,4 @@\n apple\n-orange\n banana\n+orange\n grape\n" +)] +#[case::unified_append( + "one\ntwo\n", + "one\ntwo\nthree\nfour\n", + "@@ -1,2 +1,4 @@\n one\n two\n+three\n+four\n" +)] +fn test_gnu_unified_diff_hunk_alignment( + #[case] file1: &str, + #[case] file2: &str, + #[case] expected_hunk: &str, +) { + let (at, ts) = at_and_ts!(); + at.write("file1", file1); + at.write("file2", file2); + + ts.ucmd() + .args(&["-u", "--label", "a", "--label", "b", "file1", "file2"]) + .fails_with_code(1) + .stdout_contains(expected_hunk); +} + +#[test] +fn test_large_input_diff() { + let (at, ts) = at_and_ts!(); + let mut file1 = String::new(); + let mut file2 = String::new(); + for i in 0..5000 { + file1.push_str(&format!("line {i}\n")); + if i == 2500 { + file2.push_str("modified line 2500\n"); + } else { + file2.push_str(&format!("line {i}\n")); + } + } + at.write("file1", &file1); + at.write("file2", &file2); + ts.ucmd() + .args(&["-u", "file1", "file2"]) + .fails_with_code(1) + .stdout_contains("@@ -2498,7 +2498,7 @@\n line 2497\n line 2498\n line 2499\n-line 2500\n+modified line 2500\n line 2501\n line 2502\n line 2503\n"); +} From 143b9df1c4d958c0286700184f8f2ab07c2c9aaf Mon Sep 17 00:00:00 2001 From: Milan Pramod Date: Fri, 20 Nov 2026 04:51:21 +0530 Subject: [PATCH 5/8] chore: update `ses` to v0.0.4 --- Cargo.lock | 7 +++- Cargo.toml | 2 +- benches/bench-diffutils.rs | 79 ++++++++++++++++++++++++++++---------- src/context_diff.rs | 2 +- src/ed_diff.rs | 2 +- src/normal_diff.rs | 2 +- src/side_diff.rs | 2 +- src/unified_diff.rs | 2 +- 8 files changed, 69 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 16ae7e5f..ec4664af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -976,9 +976,12 @@ dependencies = [ [[package]] name = "ses" -version = "0.0.3" +version = "0.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce129eaeb085b1818a876f6c8b30122db30e4a5d1e75cb474d6b65b6a3e40f7d" +checksum = "5ffac46567485eaeccaa5970d135dc1e5b4b9bb4af308d6964d4a4666cf1e96f" +dependencies = [ + "rustc-hash", +] [[package]] name = "shlex" diff --git a/Cargo.toml b/Cargo.toml index 3f6f35e3..bcb9013a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ chrono = "0.4.38" itoa = "1.0.11" regex = "1.10.4" same-file = "1.0.6" -ses = "0.0.3" +ses = "0.0.4" unicode-width = "0.2.0" [dev-dependencies] diff --git a/benches/bench-diffutils.rs b/benches/bench-diffutils.rs index e506b3f1..f8973b51 100644 --- a/benches/bench-diffutils.rs +++ b/benches/bench-diffutils.rs @@ -81,28 +81,43 @@ mod diffutils_cmp { #[cfg(not(feature = "feat_bench_not_diff"))] mod diffutils_diff { - // use std::hint::black_box; + use std::hint::black_box; use crate::{binary, prepare::*, FILE_SIZE_KILO_BYTES}; - // use diffutilslib::params; + use diffutilslib::{normal_diff, params}; use divan::Bencher; - // bench the actual compare - // TODO diff does not have a diff function - // #[divan::bench(args = [100_000,10_000])] - // fn diff_compare_files(bencher: Bencher, bytes: u64) { - // let (from, to) = gen_testfiles(lines, 0, "id"); - // let cmd = format!("cmp {from} {to}"); - // let opts = str_to_options(&cmd).into_iter().peekable(); - // let params = params::parse_params(opts).unwrap(); - // - // bencher - // // .with_inputs(|| prepare::cmp_params_identical_testfiles(lines)) - // .with_inputs(|| params.clone()) - // .bench_refs(|params| diff::diff(¶ms).unwrap()); - // } - - // bench original GNU diff + // bench the actual diff comparison in-process on identical files + #[divan::bench(args = FILE_SIZE_KILO_BYTES)] + fn diff_compare_files_equal(bencher: Bencher, bytes: u64) { + let (from, to) = get_context().get_test_files_equal(bytes); + let from_content = std::fs::read(from).expect("Failed to read from file"); + let to_content = std::fs::read(to).expect("Failed to read to file"); + let cmd = format!("diff {from} {to}"); + let opts = str_to_options(&cmd).into_iter().peekable(); + let (params, _) = params::parse_params(opts).unwrap(); + + bencher + .with_inputs(|| (&from_content, &to_content, ¶ms)) + .bench_refs(|(from, to, params)| black_box(normal_diff(from, to, params))); + } + + // bench the actual diff comparison in-process on differing files + #[divan::bench(args = FILE_SIZE_KILO_BYTES)] + fn diff_compare_files_different(bencher: Bencher, bytes: u64) { + let (from, to) = get_context().get_test_files_different(bytes); + let from_content = std::fs::read(from).expect("Failed to read from file"); + let to_content = std::fs::read(to).expect("Failed to read to file"); + let cmd = format!("diff {from} {to}"); + let opts = str_to_options(&cmd).into_iter().peekable(); + let (params, _) = params::parse_params(opts).unwrap(); + + bencher + .with_inputs(|| (&from_content, &to_content, ¶ms)) + .bench_refs(|(from, to, params)| black_box(normal_diff(from, to, params))); + } + + // bench original GNU diff on equal files #[divan::bench(args = FILE_SIZE_KILO_BYTES)] fn cmd_diff_gnu_equal(bencher: Bencher, bytes: u64) { let (from, to) = get_context().get_test_files_equal(bytes); @@ -113,7 +128,7 @@ mod diffutils_diff { .bench_refs(|cmd_args| binary::bench_binary("diff", cmd_args)); } - // bench the compiled release version + // bench the compiled release version on equal files #[divan::bench(args = FILE_SIZE_KILO_BYTES)] fn cmd_diff_release_equal(bencher: Bencher, bytes: u64) { let (from, to) = get_context().get_test_files_equal(bytes); @@ -124,6 +139,27 @@ mod diffutils_diff { .with_inputs(|| args_str.clone()) .bench_refs(|cmd_args| binary::bench_binary("target/release/diffutils", cmd_args)); } + + // bench original GNU diff on differing files + #[divan::bench(args = FILE_SIZE_KILO_BYTES)] + fn cmd_diff_gnu_different(bencher: Bencher, bytes: u64) { + let (from, to) = get_context().get_test_files_different(bytes); + let args_str = format!("{from} {to}"); + bencher + .with_inputs(|| args_str.clone()) + .bench_refs(|cmd_args| binary::bench_binary("diff", cmd_args)); + } + + // bench the compiled release version on differing files + #[divan::bench(args = FILE_SIZE_KILO_BYTES)] + fn cmd_diff_release_different(bencher: Bencher, bytes: u64) { + let (from, to) = get_context().get_test_files_different(bytes); + let args_str = format!("diff {from} {to}"); + + bencher + .with_inputs(|| args_str.clone()) + .bench_refs(|cmd_args| binary::bench_binary("target/release/diffutils", cmd_args)); + } } mod parser { @@ -204,7 +240,6 @@ mod prepare { &self.files_equal[p] } - #[allow(unused)] pub fn get_test_files_different(&self, kb: u64) -> &(String, String) { let p = FILE_SIZE_KILO_BYTES.iter().position(|f| *f == kb).unwrap(); &self.files_different[p] @@ -358,7 +393,7 @@ mod prepare { } mod binary { - use std::process::Command; + use std::process::{Command, Stdio}; use crate::prepare::str_to_options; @@ -366,6 +401,8 @@ mod binary { let args = str_to_options(cmd_args); Command::new(program) .args(args) + .stdout(Stdio::null()) + .stderr(Stdio::null()) .status() .expect("Failed to execute binary") } diff --git a/src/context_diff.rs b/src/context_diff.rs index c9b99288..95986093 100644 --- a/src/context_diff.rs +++ b/src/context_diff.rs @@ -129,7 +129,7 @@ fn make_diff( let mut block: Option = None; - let differences = ses::diff_slices(&expected_lines, &actual_lines); + let differences = ses::diff_lines(&expected_lines, &actual_lines); for result in differences.iter_changes() { match result { ses::Change::Delete(str) => { diff --git a/src/ed_diff.rs b/src/ed_diff.rs index 675d77dc..b56a5c9a 100644 --- a/src/ed_diff.rs +++ b/src/ed_diff.rs @@ -71,7 +71,7 @@ fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Result { diff --git a/src/normal_diff.rs b/src/normal_diff.rs index a709da34..88101a82 100644 --- a/src/normal_diff.rs +++ b/src/normal_diff.rs @@ -56,7 +56,7 @@ fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Vec actual_lines.pop(); } - let differences = ses::diff_slices(&expected_lines, &actual_lines); + let differences = ses::diff_lines(&expected_lines, &actual_lines); for result in differences.iter_changes() { match result { Change::Delete(str) => { diff --git a/src/side_diff.rs b/src/side_diff.rs index 6f258a53..04eb0e2a 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -349,7 +349,7 @@ pub fn diff( More studies are needed to cover GNU diff side by side with 100% accuracy, which is one of the goals of this project : ) */ - let differences = ses::diff_slices(&left_lines, &right_lines); + let differences = ses::diff_lines(&left_lines, &right_lines); for change in differences.iter_changes() { match change { diff --git a/src/unified_diff.rs b/src/unified_diff.rs index 60c4bf4f..40c8bd64 100644 --- a/src/unified_diff.rs +++ b/src/unified_diff.rs @@ -68,7 +68,7 @@ fn make_diff( actual_lines.pop(); } - let differences = ses::diff_slices(&expected_lines, &actual_lines); + let differences = ses::diff_lines(&expected_lines, &actual_lines); for result in differences.iter_changes() { match result { ses::Change::Delete(str) => { From 07651f330a99bbf5ee58323b75e18d7efdff67a2 Mon Sep 17 00:00:00 2001 From: binhvq Date: Thu, 19 Nov 2026 21:36:12 +0700 Subject: [PATCH 6/8] diff: count -t columns like a terminal after CR, backspace and controls (#384) * diff: count -t columns like a terminal after CR, backspace and controls * tests: port the -t control character test to uutests * utils: keep the do_expand_tabs doc comment in one piece * utils: let do_write_line write the line marker --- src/context_diff.rs | 66 ++++++++++++++------ src/ed_diff.rs | 3 +- src/normal_diff.rs | 20 +++++-- src/unified_diff.rs | 6 +- src/utils.rs | 119 +++++++++++++++++++++++++++++-------- tests/by-util/test_diff.rs | 62 +++++++++++++++++++ 6 files changed, 224 insertions(+), 52 deletions(-) diff --git a/src/context_diff.rs b/src/context_diff.rs index 95986093..1a35ff60 100644 --- a/src/context_diff.rs +++ b/src/context_diff.rs @@ -385,21 +385,36 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { for line in result.expected { match line { DiffLine::Context(e) => { - output.extend_from_slice(line_prefix(b" ", &e, params)); - do_write_line(&mut output, &e, params.expand_tabs, params.tabsize) - .expect("write to Vec is infallible"); + do_write_line( + &mut output, + &e, + params.expand_tabs, + params.tabsize, + line_prefix(b" ", &e, params), + ) + .expect("write to Vec is infallible"); writeln!(output).unwrap(); } DiffLine::Change(e) => { - output.extend_from_slice(line_prefix(b"! ", &e, params)); - do_write_line(&mut output, &e, params.expand_tabs, params.tabsize) - .expect("write to Vec is infallible"); + do_write_line( + &mut output, + &e, + params.expand_tabs, + params.tabsize, + line_prefix(b"! ", &e, params), + ) + .expect("write to Vec is infallible"); writeln!(output).unwrap(); } DiffLine::Add(e) => { - output.extend_from_slice(line_prefix(b"- ", &e, params)); - do_write_line(&mut output, &e, params.expand_tabs, params.tabsize) - .expect("write to Vec is infallible"); + do_write_line( + &mut output, + &e, + params.expand_tabs, + params.tabsize, + line_prefix(b"- ", &e, params), + ) + .expect("write to Vec is infallible"); writeln!(output).unwrap(); } } @@ -415,21 +430,36 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { for line in result.actual { match line { DiffLine::Context(e) => { - output.extend_from_slice(line_prefix(b" ", &e, params)); - do_write_line(&mut output, &e, params.expand_tabs, params.tabsize) - .expect("write to Vec is infallible"); + do_write_line( + &mut output, + &e, + params.expand_tabs, + params.tabsize, + line_prefix(b" ", &e, params), + ) + .expect("write to Vec is infallible"); writeln!(output).unwrap(); } DiffLine::Change(e) => { - output.extend_from_slice(line_prefix(b"! ", &e, params)); - do_write_line(&mut output, &e, params.expand_tabs, params.tabsize) - .expect("write to Vec is infallible"); + do_write_line( + &mut output, + &e, + params.expand_tabs, + params.tabsize, + line_prefix(b"! ", &e, params), + ) + .expect("write to Vec is infallible"); writeln!(output).unwrap(); } DiffLine::Add(e) => { - output.extend_from_slice(line_prefix(b"+ ", &e, params)); - do_write_line(&mut output, &e, params.expand_tabs, params.tabsize) - .expect("write to Vec is infallible"); + do_write_line( + &mut output, + &e, + params.expand_tabs, + params.tabsize, + line_prefix(b"+ ", &e, params), + ) + .expect("write to Vec is infallible"); writeln!(output).unwrap(); } } diff --git a/src/ed_diff.rs b/src/ed_diff.rs index b56a5c9a..f258e266 100644 --- a/src/ed_diff.rs +++ b/src/ed_diff.rs @@ -159,7 +159,8 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Result, writeln!(&mut output, "..\n.\ns/.//").unwrap(); in_text = false; } else { - do_write_line(&mut output, actual, params.expand_tabs, params.tabsize).unwrap(); + do_write_line(&mut output, actual, params.expand_tabs, params.tabsize, b"") + .unwrap(); writeln!(&mut output).unwrap(); } } diff --git a/src/normal_diff.rs b/src/normal_diff.rs index 88101a82..c86ee027 100644 --- a/src/normal_diff.rs +++ b/src/normal_diff.rs @@ -205,8 +205,14 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { .unwrap(), } for expected in &result.expected { - output.extend_from_slice(line_prefix(b"< ", expected, params)); - do_write_line(&mut output, expected, params.expand_tabs, params.tabsize).unwrap(); + do_write_line( + &mut output, + expected, + params.expand_tabs, + params.tabsize, + line_prefix(b"< ", expected, params), + ) + .unwrap(); writeln!(&mut output).unwrap(); } if result.expected_missing_nl { @@ -216,8 +222,14 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { writeln!(&mut output, "---").unwrap(); } for actual in &result.actual { - output.extend_from_slice(line_prefix(b"> ", actual, params)); - do_write_line(&mut output, actual, params.expand_tabs, params.tabsize).unwrap(); + do_write_line( + &mut output, + actual, + params.expand_tabs, + params.tabsize, + line_prefix(b"> ", actual, params), + ) + .unwrap(); writeln!(&mut output).unwrap(); } if result.actual_missing_nl { diff --git a/src/unified_diff.rs b/src/unified_diff.rs index 40c8bd64..387ea2d2 100644 --- a/src/unified_diff.rs +++ b/src/unified_diff.rs @@ -400,19 +400,19 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { match line { DiffLine::Expected(e) => { output.extend_from_slice(line_prefix(b"-", &e, params)); - do_write_line(&mut output, &e, params.expand_tabs, params.tabsize) + do_write_line(&mut output, &e, params.expand_tabs, params.tabsize, b"") .expect("write to Vec is infallible"); writeln!(output).unwrap(); } DiffLine::Context(c) => { output.extend_from_slice(line_prefix(b" ", &c, params)); - do_write_line(&mut output, &c, params.expand_tabs, params.tabsize) + do_write_line(&mut output, &c, params.expand_tabs, params.tabsize, b"") .expect("write to Vec is infallible"); writeln!(output).unwrap(); } DiffLine::Actual(r) => { output.extend_from_slice(line_prefix(b"+", &r, params)); - do_write_line(&mut output, &r, params.expand_tabs, params.tabsize) + do_write_line(&mut output, &r, params.expand_tabs, params.tabsize, b"") .expect("write to Vec is infallible"); writeln!(output).unwrap(); } diff --git a/src/utils.rs b/src/utils.rs index 522f32d0..8bd2d18b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -18,37 +18,78 @@ use unicode_width::UnicodeWidthStr; /// The spaces are written in small chunks, so a huge tab size does not /// need a huge buffer. /// This assumes that line does not contain any line breaks (if it does, the result is undefined). +/// +/// The column moves the way it would on a terminal: a carriage return goes +/// back to the start of the line, and `marker` (the `< ` or `! ` in front of +/// the line) is written again after it when more text follows. A backspace +/// goes one column back, or is dropped at the start of the line, and other +/// control characters take no room. pub fn do_expand_tabs( output: &mut W, line: &[u8], tabsize: usize, + marker: &[u8], ) -> std::io::Result<()> { const SPACES: [u8; 64] = [b' '; 64]; - let tab = b'\t'; - let mut offset = 0; - - let mut iter = line.split(|c| *c == tab).peekable(); - while let Some(chunk) = iter.next() { - match std::str::from_utf8(chunk) { - Ok(s) => offset += UnicodeWidthStr::width(s), - Err(_) => offset += chunk.len(), - } - output.write_all(chunk)?; - if iter.peek().is_some() { - let mut spaces = tabsize - offset % tabsize; - while spaces > 0 { - let n = spaces.min(SPACES.len()); - output.write_all(&SPACES[..n])?; - spaces -= n; + let mut column = 0; + let mut rest = line; + while !rest.is_empty() { + let text_len = rest + .iter() + .position(u8::is_ascii_control) + .unwrap_or(rest.len()); + let (text, after) = rest.split_at(text_len); + column += text_width(text); + output.write_all(text)?; + let Some((&control, after)) = after.split_first() else { + break; + }; + match control { + b'\t' => { + let mut spaces = tabsize - column % tabsize; + column += spaces; + while spaces > 0 { + let n = spaces.min(SPACES.len()); + output.write_all(&SPACES[..n])?; + spaces -= n; + } + } + b'\r' => { + column = 0; + output.write_all(&[control])?; + if !after.is_empty() { + output.write_all(marker)?; + } } - offset = 0; + b'\x08' if column == 0 => {} + b'\x08' => { + column -= 1; + output.write_all(&[control])?; + } + _ => output.write_all(&[control])?, } + rest = after; } - Ok(()) } -/// Write a single line to an output stream, expanding tabs to space if necessary. +/// Number of columns `text` takes, where bytes that are not valid UTF-8 take +/// one column each and control characters none. +fn text_width(text: &[u8]) -> usize { + text.utf8_chunks() + .map(|chunk| { + let valid: usize = chunk + .valid() + .split(char::is_control) + .map(UnicodeWidthStr::width) + .sum(); + valid + chunk.invalid().len() + }) + .sum() +} + +/// Write `marker` (the `< ` or `! ` in front of the line, possibly empty) and +/// then a single line to an output stream, expanding tabs to space if necessary. /// This assumes that line does not contain any line breaks /// (if it does and tabs are to be expanded to spaces, the result is undefined). pub fn do_write_line( @@ -56,9 +97,11 @@ pub fn do_write_line( line: &[u8], expand_tabs: bool, tabsize: usize, + marker: &[u8], ) -> std::io::Result<()> { + output.write_all(marker)?; if expand_tabs { - do_expand_tabs(output, line, tabsize) + do_expand_tabs(output, line, tabsize, marker) } else { output.write_all(line) } @@ -427,14 +470,14 @@ mod tests { use super::*; use pretty_assertions::assert_eq; - fn expand(line: &[u8], tabsize: usize) -> Vec { + fn expand(line: &[u8], tabsize: usize, marker: &[u8]) -> Vec { let mut output = Vec::new(); - assert!(do_expand_tabs(&mut output, line, tabsize).is_ok()); + assert!(do_expand_tabs(&mut output, line, tabsize, marker).is_ok()); output } fn assert_tab_expansion(line: &str, tabsize: usize, expected: &str) { - assert_eq!(expand(line.as_bytes(), tabsize), expected.as_bytes()); + assert_eq!(expand(line.as_bytes(), tabsize, b""), expected.as_bytes()); } #[test] @@ -463,9 +506,31 @@ mod tests { fn invalid_utf8() { // [240, 240, 152, 137] is an invalid UTF-8 sequence, so it is handled as 4 bytes assert_eq!( - expand(&[240, 240, 152, 137, 9, 102, 111, 111], 8), + expand(&[240, 240, 152, 137, 9, 102, 111, 111], 8, b""), &[240, 240, 152, 137, 32, 32, 32, 32, 102, 111, 111] ); + // Only the invalid byte counts as a column, not the whole chunk. + assert_tab_expansion_bytes(b"\xff\xc3\xa9\tx", 4, b"\xff\xc3\xa9 x"); + } + + fn assert_tab_expansion_bytes(line: &[u8], tabsize: usize, expected: &[u8]) { + assert_eq!(expand(line, tabsize, b"< "), expected); + } + + #[test] + fn control_chars() { + // A carriage return restarts the line behind the marker again. + assert_tab_expansion_bytes(b"ab\rc\td", 8, b"ab\r< c d"); + assert_tab_expansion_bytes(b"ab\r\r\tc", 4, b"ab\r< \r< c"); + assert_tab_expansion_bytes(b"ab\r", 8, b"ab\r"); + assert_tab_expansion_bytes(b"x\r\ty\r", 4, b"x\r< y\r"); + // A backspace moves one column back, but not before the start. + assert_tab_expansion_bytes(b"abc\x08d\te", 8, b"abc\x08d e"); + assert_tab_expansion_bytes(b"\x08\x08x\ty", 8, b"x y"); + assert_tab_expansion_bytes(b"\x08x", 8, b"x"); + // Other control characters, C1 ones included, take no room. + assert_tab_expansion_bytes(b"a\x01\x7f\x1bb\tc", 8, b"a\x01\x7f\x1bb c"); + assert_tab_expansion_bytes("a\u{80}b\tc".as_bytes(), 4, "a\u{80}b c".as_bytes()); } #[test] @@ -474,7 +539,9 @@ mod tests { // tab size neither overflows nor needs a huge buffer up front. let mut buf = [0u8; 1000]; let mut output = &mut buf[..]; - assert!(do_expand_tabs(&mut output, b"a\tb\tc", crate::params::MAX_TABSIZE).is_err()); + assert!( + do_expand_tabs(&mut output, b"a\tb\tc", crate::params::MAX_TABSIZE, b"").is_err() + ); let mut expected = b"a".to_vec(); expected.resize(1000, b' '); assert_eq!(buf[..], expected[..]); @@ -542,7 +609,7 @@ mod tests { fn assert_line_written(line: &str, expand_tabs: bool, tabsize: usize, expected: &str) { let mut output: Vec = Vec::new(); - assert!(do_write_line(&mut output, line.as_bytes(), expand_tabs, tabsize).is_ok()); + assert!(do_write_line(&mut output, line.as_bytes(), expand_tabs, tabsize, b"").is_ok()); assert_eq!(output, expected.as_bytes()); } diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index d2fa16cb..db0899de 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -368,6 +368,68 @@ fn color_bad_option(#[case] option: &str, #[case] error: &str) { .stderr_contains(error); } +// With -t, the column moves like on a terminal: back to the start after a +// carriage return, where the line marker is printed again, and one back for +// a backspace. Other control characters take no room. +#[test] +fn expand_tabs_after_control_characters() { + let (at, ts) = at_and_ts!(); + at.write( + "old", + "ab\rc\t1\nabc\x08d\t1\n\x08x\t1\na\x01\x1bb\t1\nend\r\n", + ); + at.write( + "new", + "ab\rc\t2\nabc\x08d\t2\n\x08x\t2\na\x01\x1bb\t2\nend\r\n", + ); + let diff = |format: &str| { + ts.ucmd() + .args(&["-t", format, "old", "new"]) + .fails_with_code(1) + }; + + diff("--normal").stdout_only( + "1,4c1,4\n\ + < ab\r< c 1\n\ + < abc\x08d 1\n\ + < x 1\n\ + < a\x01\x1bb 1\n\ + ---\n\ + > ab\r> c 2\n\ + > abc\x08d 2\n\ + > x 2\n\ + > a\x01\x1bb 2\n", + ); + // The unified format does not repeat its marker. + diff("-U0").stdout_str_check(|s| { + s.ends_with( + "@@ -1,4 +1,4 @@\n\ + -ab\rc 1\n\ + -abc\x08d 1\n\ + -x 1\n\ + -a\x01\x1bb 1\n\ + +ab\rc 2\n\ + +abc\x08d 2\n\ + +x 2\n\ + +a\x01\x1bb 2\n", + ) + }); + diff("-C0").stdout_str_check(|s| { + s.ends_with( + "*** 1,4 ****\n\ + ! ab\r! c 1\n\ + ! abc\x08d 1\n\ + ! x 1\n\ + ! a\x01\x1bb 1\n\ + --- 1,4 ----\n\ + ! ab\r! c 2\n\ + ! abc\x08d 2\n\ + ! x 2\n\ + ! a\x01\x1bb 2\n", + ) + }); +} + #[cfg(target_os = "linux")] #[test] fn write_error() { From af2d235791dee13ea91926a92c1d72a1af99117e Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Wed, 18 Nov 2026 06:54:55 +0000 Subject: [PATCH 7/8] chore(deps): update rust crate regex to v1.13.2 --- Cargo.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ec4664af..bcae383f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -799,9 +799,9 @@ checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" [[package]] name = "regex" -version = "1.13.1" +version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d" +checksum = "9807af0e638c12e753aa1b4e601791bf2a8f71274023c8bbeec12fc149950857" dependencies = [ "aho-corasick", "memchr", @@ -811,9 +811,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.18" +version = "0.4.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2" +checksum = "41d60ef07df457506a0a1a2073112eb4aae5733dbd2813c3da77d1b0fd441158" dependencies = [ "aho-corasick", "memchr", @@ -828,9 +828,9 @@ checksum = "cab834c73d247e67f4fae452806d17d3c7501756d98c8808d7c9c7aa7d18f973" [[package]] name = "regex-syntax" -version = "0.8.11" +version = "0.8.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" +checksum = "d902046e6a28a1e4c3cab97258a0ce11e994903f01c2d97a9447a0b3df85f4f6" [[package]] name = "relative-path" From e3a3524c9cfb2c4789ac05f7ed81ca12e5bd3a7d Mon Sep 17 00:00:00 2001 From: Milan Pramod Date: Fri, 20 Nov 2026 17:20:16 +0530 Subject: [PATCH 8/8] fix(diff): optimize comparison for identical files - Check for identical content early in compare_files to bypass binary checks - Return empty output immediately in normal and unified diff generators when inputs are identical perf(utils): optimize file equality checks with sampling heuristic - Add `is_identical` helper to `utils` for O(1) early rejection of non-identical files - Sample tail, midpoint, and quarter points for large buffers ($\ge$ 256 bytes) before falling back to full comparison - Update diff modules (`diff`, `normal_diff`, `unified_diff`) to utilize the new optimized equality check - Add comprehensive unit tests covering empty, small, and large-buffer sampling rejections --- src/diff.rs | 11 +++--- src/normal_diff.rs | 7 +++- src/unified_diff.rs | 9 +++-- src/utils.rs | 86 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 8 deletions(-) diff --git a/src/diff.rs b/src/diff.rs index 55fd30e5..f18293d9 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -5,7 +5,7 @@ use crate::color::{colorize, ColorWhen, Palette, PaletteError}; use crate::params::{parse_params, Format, MissingFile, Params}; -use crate::utils::report_failure_to_read_input_file; +use crate::utils::{is_identical, report_failure_to_read_input_file}; use crate::{context_diff, dir_diff, ed_diff, normal_diff, side_diff, unified_diff}; use std::env::ArgsOs; use std::ffi::OsString; @@ -132,11 +132,12 @@ pub fn compare_files(params: &Params, header: Option<&[u8]>) -> Outcome { return Outcome::Trouble; } + if is_identical(&from_content, &to_content) { + maybe_report_identical_files(); + return Outcome::Identical; + } + if !params.text && (is_binary(&from_content) || is_binary(&to_content)) { - if from_content == to_content { - maybe_report_identical_files(); - return Outcome::Identical; - } print_files_differ( if params.brief { "Files" diff --git a/src/normal_diff.rs b/src/normal_diff.rs index c86ee027..92370912 100644 --- a/src/normal_diff.rs +++ b/src/normal_diff.rs @@ -8,7 +8,7 @@ use std::io::Write; use ses::Change; use crate::params::Params; -use crate::utils::{do_write_line, line_prefix}; +use crate::utils::{do_write_line, is_identical, line_prefix}; #[derive(Debug, PartialEq)] struct Mismatch { @@ -124,6 +124,11 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { // See https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Normal.html // for details on the syntax of the normal format. let mut output = Vec::new(); + + if is_identical(expected, actual) { + return output; + } + let diff_results = make_diff(expected, actual, params.brief); if params.brief && !diff_results.is_empty() { write!(&mut output, "\0").unwrap(); diff --git a/src/unified_diff.rs b/src/unified_diff.rs index 387ea2d2..66b5b5e0 100644 --- a/src/unified_diff.rs +++ b/src/unified_diff.rs @@ -7,8 +7,10 @@ use std::collections::VecDeque; use std::io::Write; use crate::params::Params; -use crate::utils::{do_write_line, line_prefix}; -use crate::utils::{get_modification_time, write_file_header, FunctionLineFinder, TimestampStyle}; +use crate::utils::{ + do_write_line, get_modification_time, is_identical, line_prefix, write_file_header, + FunctionLineFinder, TimestampStyle, +}; #[derive(Debug, PartialEq)] pub enum DiffLine { @@ -238,6 +240,9 @@ fn make_diff( #[must_use] pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { + if is_identical(expected, actual) { + return Vec::new(); + } let from_modified_time = get_modification_time( ¶ms.from.to_string_lossy(), TimestampStyle::Iso, diff --git a/src/utils.rs b/src/utils.rs index 8bd2d18b..8590e596 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -369,6 +369,43 @@ pub fn report_failure_to_read_input_file( ); } +/// Check if two byte slices are identical with early rejection sampling. +/// +/// When files are equal, this verifies full byte equality and returns `true`. +/// When files differ, it samples the tail, midpoint, and quarter points to reject +/// non-identical files in O(1) time before committing to a full O(N) scan. +#[inline] +pub fn is_identical(expected: &[u8], actual: &[u8]) -> bool { + if expected.len() != actual.len() { + return false; + } + let len = expected.len(); + if len == 0 { + return true; + } + + if len >= 256 { + // Fast O(1) checks at tail and key sample points before full memcmp + if expected[len - 256..] != actual[len - 256..] { + return false; + } + let mid = len / 2; + if expected[mid - 128..mid + 128] != actual[mid - 128..mid + 128] { + return false; + } + let q1 = len / 4; + if expected[q1 - 128..q1 + 128] != actual[q1 - 128..q1 + 128] { + return false; + } + let q3 = 3 * len / 4; + if expected[q3 - 128..q3 + 128] != actual[q3 - 128..q3 + 128] { + return false; + } + } + + expected == actual +} + #[cfg(test)] pub mod testcmds { // Command construction wrapper that provides some validation and non-obscure, "fail fast" @@ -665,4 +702,53 @@ mod tests { assert!(m_time > current_time); } } + + mod is_identical { + use super::*; + + #[test] + fn test_empty() { + assert!(is_identical(b"", b"")); + assert!(!is_identical(b"", b"a")); + assert!(!is_identical(b"a", b"")); + } + + #[test] + fn test_small() { + assert!(is_identical(b"hello world", b"hello world")); + assert!(!is_identical(b"hello world", b"hello work!")); + assert!(!is_identical(b"hello", b"world")); + } + + #[test] + fn test_large_sample_rejections() { + let a = vec![b'a'; 1000]; + let mut b = vec![b'a'; 1000]; + assert!(is_identical(&a, &b)); + + // Differ at tail + b[999] = b'b'; + assert!(!is_identical(&a, &b)); + b[999] = b'a'; + + // Differ at midpoint + b[500] = b'b'; + assert!(!is_identical(&a, &b)); + b[500] = b'a'; + + // Differ at quarter 1 + b[250] = b'b'; + assert!(!is_identical(&a, &b)); + b[250] = b'a'; + + // Differ at quarter 3 + b[750] = b'b'; + assert!(!is_identical(&a, &b)); + b[750] = b'a'; + + // Differ at arbitrary position + b[10] = b'b'; + assert!(!is_identical(&a, &b)); + } + } }