diff --git a/Cargo.lock b/Cargo.lock index 99188bb2..e13223ec 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", + "similar", "tempfile", "unicode-width", "uutests", @@ -980,6 +980,12 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" +[[package]] +name = "similar" +version = "3.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2f8bec09ae8fae0f4f265a4559a9334be3929d8504e9b536df0a41302bd1733" + [[package]] name = "slab" version = "0.4.12" diff --git a/Cargo.toml b/Cargo.toml index 5a433257..44b702f4 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" +similar = { version = "3.2.1", default-features = false } 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 790d58fa..212ade6b 100644 --- a/src/context_diff.rs +++ b/src/context_diff.rs @@ -6,6 +6,7 @@ use std::collections::VecDeque; use std::io::Write; +use crate::diff_engine::{diff_slices, Change}; use crate::params::Params; use crate::utils::do_write_line; use crate::utils::{ @@ -129,9 +130,9 @@ fn make_diff( let mut block: Option = None; - for result in diff::slice(&expected_lines, &actual_lines) { + for result in diff_slices(&expected_lines, &actual_lines) { match result { - diff::Result::Left(str) => { + Change::Left(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) => { + Change::Right(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, _) => { + Change::Both(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/diff_engine.rs b/src/diff_engine.rs new file mode 100644 index 00000000..22593c89 --- /dev/null +++ b/src/diff_engine.rs @@ -0,0 +1,267 @@ +// This file is part of the uutils diffutils package. +// +// For the full copyright and license information, please view the LICENSE +// file that was distributed with this source code. + +//! Internal sequence diff engine wrapping the `similar` crate. + +use similar::{capture_diff_slices, Algorithm, DiffOp}; + +/// Represents an element difference between two sequences. +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Change { + /// Present only in the expected (old / left) sequence. + Left(T), + /// Present only in the actual (new / right) sequence. + Right(T), + /// Present in both sequences. + Both(T, T), +} + +/// Computes the difference between two slices using Eugene Myers' linear-space algorithm. +/// +/// Returns a vector of `Change<&'a T>` matching the sequence changes. +pub fn diff_slices<'a, T: std::hash::Hash + Eq>( + expected: &'a [T], + actual: &'a [T], +) -> Vec> { + let ops = capture_diff_slices(Algorithm::Myers, expected, actual); + let mut changes = Vec::with_capacity(expected.len() + actual.len()); + + for op in ops { + match op { + DiffOp::Equal { + old_index, + new_index, + len, + } => { + changes.extend( + expected[old_index..old_index + len] + .iter() + .zip(&actual[new_index..new_index + len]) + .map(|(e, a)| Change::Both(e, a)), + ); + } + DiffOp::Delete { + old_index, old_len, .. + } => { + changes.extend( + expected[old_index..old_index + old_len] + .iter() + .map(Change::Left), + ); + } + DiffOp::Insert { + new_index, new_len, .. + } => { + changes.extend( + actual[new_index..new_index + new_len] + .iter() + .map(Change::Right), + ); + } + DiffOp::Replace { + old_index, + old_len, + new_index, + new_len, + } => { + changes.extend( + expected[old_index..old_index + old_len] + .iter() + .map(Change::Left), + ); + changes.extend( + actual[new_index..new_index + new_len] + .iter() + .map(Change::Right), + ); + } + } + } + + changes +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_identical_slices() { + let a: &[&[u8]] = &[b"alpha", b"beta", b"gamma"]; + let changes = diff_slices(a, a); + assert_eq!( + changes, + vec![ + Change::Both(&a[0], &a[0]), + Change::Both(&a[1], &a[1]), + Change::Both(&a[2], &a[2]), + ] + ); + } + + #[test] + fn test_empty_slices() { + let empty: &[&[u8]] = &[]; + let items: &[&[u8]] = &[b"a"]; + assert_eq!(diff_slices(empty, empty), vec![]); + assert_eq!(diff_slices(empty, items), vec![Change::Right(&items[0])]); + assert_eq!(diff_slices(items, empty), vec![Change::Left(&items[0])]); + } + + #[test] + fn test_modifications_and_replacements() { + let a: &[&[u8]] = &[b"apple", b"banana", b"cherry"]; + let b: &[&[u8]] = &[b"apple", b"blueberry", b"cherry"]; + let changes = diff_slices(a, b); + assert_eq!( + changes, + vec![ + Change::Both(&a[0], &b[0]), + Change::Left(&a[1]), + Change::Right(&b[1]), + Change::Both(&a[2], &b[2]), + ] + ); + } + + #[test] + fn test_insertions_and_deletions() { + let a: &[&[u8]] = &[b"line1", b"line2", b"line3"]; + let b: &[&[u8]] = &[b"line1", b"line1.5", b"line3", b"line4"]; + let changes = diff_slices(a, b); + assert_eq!( + changes, + vec![ + Change::Both(&a[0], &b[0]), + Change::Left(&a[1]), + Change::Right(&b[1]), + Change::Both(&a[2], &b[2]), + Change::Right(&b[3]), + ] + ); + } + + #[test] + fn test_completely_disjoint_slices() { + let a: &[&[u8]] = &[b"one", b"two", b"three"]; + let b: &[&[u8]] = &[b"four", b"five", b"six"]; + let changes = diff_slices(a, b); + assert_eq!( + changes, + vec![ + Change::Left(&a[0]), + Change::Left(&a[1]), + Change::Left(&a[2]), + Change::Right(&b[0]), + Change::Right(&b[1]), + Change::Right(&b[2]), + ] + ); + } + + #[test] + fn test_asymmetric_replace() { + // 1 element replaced by 3 + let a: &[&[u8]] = &[b"start", b"old", b"end"]; + let b: &[&[u8]] = &[b"start", b"new1", b"new2", b"new3", b"end"]; + let changes = diff_slices(a, b); + assert_eq!( + changes, + vec![ + Change::Both(&a[0], &b[0]), + Change::Left(&a[1]), + Change::Right(&b[1]), + Change::Right(&b[2]), + Change::Right(&b[3]), + Change::Both(&a[2], &b[4]), + ] + ); + + // 3 elements replaced by 1 + let changes_rev = diff_slices(b, a); + assert_eq!( + changes_rev, + vec![ + Change::Both(&b[0], &a[0]), + Change::Left(&b[1]), + Change::Left(&b[2]), + Change::Left(&b[3]), + Change::Right(&a[1]), + Change::Both(&b[4], &a[2]), + ] + ); + } + + #[test] + fn test_repeated_and_duplicate_elements() { + let a: &[&[u8]] = &[b"dup", b"dup", b"dup"]; + let b: &[&[u8]] = &[b"dup", b"dup"]; + let changes = diff_slices(a, b); + assert_eq!( + changes, + vec![ + Change::Both(&a[0], &b[0]), + Change::Both(&a[1], &b[1]), + Change::Left(&a[2]), + ] + ); + } + + #[test] + fn test_empty_lines_and_binary_content() { + let a: &[&[u8]] = &[b"", b"\x00\xFF\xFE", b""]; + let b: &[&[u8]] = &[b"", b"\x00\xFF\x01", b""]; + let changes = diff_slices(a, b); + assert_eq!( + changes, + vec![ + Change::Both(&a[0], &b[0]), + Change::Left(&a[1]), + Change::Right(&b[1]), + Change::Both(&a[2], &b[2]), + ] + ); + } + + #[test] + fn test_large_sparse_sequence() { + let a: Vec> = (0..5000) + .map(|i| format!("line {i}").into_bytes()) + .collect(); + let mut b = a.clone(); + b[2500] = b"modified line 2500".to_vec(); + + let a_refs: Vec<&[u8]> = a.iter().map(Vec::as_slice).collect(); + let b_refs: Vec<&[u8]> = b.iter().map(Vec::as_slice).collect(); + + let changes = diff_slices(&a_refs, &b_refs); + assert_eq!(changes.len(), 5001); // 4999 both + 1 left + 1 right + assert_eq!(changes[2500], Change::Left(&a_refs[2500])); + assert_eq!(changes[2501], Change::Right(&b_refs[2500])); + } + + #[test] + fn test_myers_vs_lcs_difference() { + // When multiple minimal edit paths exist, Myers' algorithm preserves common + // runs and shifts the unique element, whereas traditional LCS kept 'b' stationary: + // + // LCS edit path: delete 'a' at 4, insert 'a' at 5 (keeping 'b' matched at 5) + // Myers edit path: insert 'b' at 4, delete 'b' at 5 (keeping all four 'a's matched) + let a: &[&[u8]] = &[b"a", b"a", b"a", b"a", b"b"]; + let b: &[&[u8]] = &[b"a", b"a", b"a", b"b", b"a"]; + let changes = diff_slices(a, b); + assert_eq!( + changes, + vec![ + Change::Both(&a[0], &b[0]), + Change::Both(&a[1], &b[1]), + Change::Both(&a[2], &b[2]), + Change::Right(&b[3]), + Change::Both(&a[3], &b[4]), + Change::Left(&a[4]), + ] + ); + } +} diff --git a/src/ed_diff.rs b/src/ed_diff.rs index 2f75adfa..b966effe 100644 --- a/src/ed_diff.rs +++ b/src/ed_diff.rs @@ -5,6 +5,7 @@ use std::io::Write; +use crate::diff_engine::{diff_slices, Change}; use crate::params::Params; use crate::utils::do_write_line; @@ -71,9 +72,9 @@ fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Result { + Change::Left(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 { + Change::Right(str) => { mismatch.actual.push(str.to_vec()); line_number_actual += 1; } - diff::Result::Both(_str, _) => { + Change::Both(_str, _) => { line_number_expected += 1; line_number_actual += 1; if !mismatch.actual.is_empty() || !mismatch.expected.is_empty() { diff --git a/src/lib.rs b/src/lib.rs index e1e43b6e..0bfd7e63 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ pub mod bre; pub mod cmp; pub mod color; pub mod context_diff; +pub mod diff_engine; pub mod ed_diff; pub mod macros; pub mod normal_diff; diff --git a/src/main.rs b/src/main.rs index 2be28099..d7c8db8a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ mod cmp; mod color; mod context_diff; mod diff; +mod diff_engine; mod dir_diff; mod ed_diff; mod macros; diff --git a/src/normal_diff.rs b/src/normal_diff.rs index c930b957..761a4c2d 100644 --- a/src/normal_diff.rs +++ b/src/normal_diff.rs @@ -5,6 +5,7 @@ use std::io::Write; +use crate::diff_engine::{diff_slices, Change}; use crate::params::Params; use crate::utils::do_write_line; @@ -54,9 +55,9 @@ fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Vec actual_lines.pop(); } - for result in diff::slice(&expected_lines, &actual_lines) { + for result in diff_slices(&expected_lines, &actual_lines) { match result { - diff::Result::Left(str) => { + Change::Left(str) => { if !mismatch.actual.is_empty() && !mismatch.actual_missing_nl { results.push(mismatch); mismatch = Mismatch::new(line_number_expected, line_number_actual); @@ -65,12 +66,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::Right(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::Both(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..5ca6ed30 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -4,10 +4,11 @@ // 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; +use crate::diff_engine::{diff_slices, Change}; + use crate::params::Params; const GUTTER_WIDTH_MIN: usize = 3; @@ -350,11 +351,11 @@ 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) { + for result in diff_slices(&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) => { + Change::Left(left_ln) => push_output(left_ln, b"", b'<', output, &config).unwrap(), + Change::Right(right_ln) => push_output(b"", right_ln, b'>', output, &config).unwrap(), + Change::Both(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..818dcd78 100644 --- a/src/unified_diff.rs +++ b/src/unified_diff.rs @@ -6,6 +6,7 @@ use std::collections::VecDeque; use std::io::Write; +use crate::diff_engine::{diff_slices, Change}; use crate::params::Params; use crate::utils::do_write_line; use crate::utils::{get_modification_time, write_file_header, FunctionLineFinder, TimestampStyle}; @@ -68,9 +69,9 @@ fn make_diff( actual_lines.pop(); } - for result in diff::slice(&expected_lines, &actual_lines) { + for result in diff_slices(&expected_lines, &actual_lines) { match result { - diff::Result::Left(str) => { + Change::Left(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) => { + Change::Right(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, _) => { + Change::Both(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/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index c251c39b..ea1df264 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -1054,3 +1054,43 @@ fn label_errors(#[case] args: &[&str], #[case] error: &str) { .fails_with_code(2) .stderr_contains(error); } + +#[test] +fn test_myers_hunk_alignment_repeated_lines() { + let (at, ts) = at_and_ts!(); + at.write("file1", "a\na\na\na\nb\n"); + at.write("file2", "a\na\na\nb\na\n"); + + // Normal diff format: + ts.ucmd() + .args(&["file1", "file2"]) + .fails_with_code(1) + .stdout_is("3a4\n> b\n5d5\n< b\n"); + + // Unified diff format: + ts.ucmd() + .args(&["-u", "file1", "file2"]) + .fails_with_code(1) + .stdout_contains("@@ -1,5 +1,5 @@\n a\n a\n a\n+b\n a\n-b\n"); +} + +#[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"); +}