From 49ad5d3a91bce86e7cea0a61e80971696e3a57ca Mon Sep 17 00:00:00 2001 From: Yunus Ardaguney Date: Thu, 19 Nov 2026 22:40:59 +0300 Subject: [PATCH 1/2] diff: add -E/--ignore-tab-expansion support GNU diff's -E / --ignore-tab-expansion treats two lines as equal when they match after tab expansion. uutils previously failed with unrecognized option '-E'. Implement -E / --ignore-tab-expansion across all diff formats by wrapping comparison lines in ComparisonLine, expanding tabs using the existing terminal column rules (honoring tabsize, carriage return, backspace, and wide characters) when the flag is enabled. Fixes #408 --- src/context_diff.rs | 29 +++++++++-------- src/ed_diff.rs | 21 ++++++++---- src/normal_diff.rs | 21 ++++++++---- src/params.rs | 26 +++++++++++++++ src/side_diff.rs | 23 +++++++++---- src/unified_diff.rs | 29 +++++++++-------- src/utils.rs | 45 ++++++++++++++++++++++++++ tests/by-util/test_diff.rs | 66 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 213 insertions(+), 47 deletions(-) diff --git a/src/context_diff.rs b/src/context_diff.rs index ddc11dd4..8903c220 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, line_prefix}; +use crate::utils::{do_write_line, line_prefix, ComparisonLine}; use crate::utils::{ get_modification_time, is_posix_locale, write_file_header, FunctionLineFinder, TimestampStyle, }; @@ -95,12 +95,9 @@ impl ChangeBlock { } // Produces a diff between the expected output and actual output. -fn make_diff( - expected: &[u8], - actual: &[u8], - context_size: usize, - stop_early: bool, -) -> Vec { +fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { + let context_size = params.context_count; + let stop_early = params.brief; let mut line_number_expected = 1; let mut line_number_actual = 1; let mut context_queue: VecDeque<&[u8]> = VecDeque::with_capacity(context_size); @@ -111,19 +108,25 @@ fn make_diff( let mut results = Vec::new(); let mut mismatch = Mismatch::new(0, 0); - let mut expected_lines: Vec<&[u8]> = expected.split(|&c| c == b'\n').collect(); - let mut actual_lines: Vec<&[u8]> = actual.split(|&c| c == b'\n').collect(); + let mut expected_lines: Vec = expected + .split(|&c| c == b'\n') + .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) + .collect(); + let mut actual_lines: Vec = actual + .split(|&c| c == b'\n') + .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) + .collect(); debug_assert_eq!(b"".split(|&c| c == b'\n').count(), 1); // ^ means that underflow here is impossible let expected_lines_count = expected_lines.len() - 1; let actual_lines_count = actual_lines.len() - 1; - if expected_lines.last() == Some(&&b""[..]) { + if expected_lines.last().is_some_and(|l| l.raw.is_empty()) { expected_lines.pop(); } - if actual_lines.last() == Some(&&b""[..]) { + if actual_lines.last().is_some_and(|l| l.raw.is_empty()) { actual_lines.pop(); } @@ -233,7 +236,7 @@ fn make_diff( mismatch.expected.push(DiffLine::Context(str.to_vec())); mismatch.actual.push(DiffLine::Context(str.to_vec())); } else if context_size > 0 { - context_queue.push_back(str); + context_queue.push_back(str.raw); } lines_since_mismatch += 1; } @@ -337,7 +340,7 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { params.to_label.as_deref(), &to_modified_time, ); - let diff_results = make_diff(expected, actual, params.context_count, params.brief); + let diff_results = make_diff(expected, actual, params); if diff_results.is_empty() { return Vec::new(); } diff --git a/src/ed_diff.rs b/src/ed_diff.rs index 6ec73568..f9ed016e 100644 --- a/src/ed_diff.rs +++ b/src/ed_diff.rs @@ -6,7 +6,7 @@ use std::io::Write; use crate::params::Params; -use crate::utils::do_write_line; +use crate::utils::{do_write_line, ComparisonLine}; #[derive(Debug, PartialEq)] struct Mismatch { @@ -45,27 +45,34 @@ impl Mismatch { } // Produces a diff between the expected output and actual output. -fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Result, DiffError> { +fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Result, DiffError> { + let stop_early = params.brief; let mut line_number_expected = 1; let mut line_number_actual = 1; let mut results = Vec::new(); let mut mismatch = Mismatch::new(line_number_expected, line_number_actual); - let mut expected_lines: Vec<&[u8]> = expected.split(|&c| c == b'\n').collect(); - let mut actual_lines: Vec<&[u8]> = actual.split(|&c| c == b'\n').collect(); + let mut expected_lines: Vec = expected + .split(|&c| c == b'\n') + .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) + .collect(); + let mut actual_lines: Vec = actual + .split(|&c| c == b'\n') + .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) + .collect(); debug_assert_eq!(b"".split(|&c| c == b'\n').count(), 1); // ^ means that underflow here is impossible let _expected_lines_count = expected_lines.len() - 1; let _actual_lines_count = actual_lines.len() - 1; - if expected_lines.last() == Some(&&b""[..]) { + if expected_lines.last().is_some_and(|l| l.raw.is_empty()) { expected_lines.pop(); } else { return Err(DiffError::MissingNL); } - if actual_lines.last() == Some(&&b""[..]) { + if actual_lines.last().is_some_and(|l| l.raw.is_empty()) { actual_lines.pop(); } else { return Err(DiffError::MissingNL); @@ -112,7 +119,7 @@ fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Result Result, DiffError> { let mut output = Vec::new(); - let diff_results = make_diff(expected, actual, params.brief)?; + let diff_results = make_diff(expected, actual, params)?; if params.brief && !diff_results.is_empty() { write!(&mut output, "\0").unwrap(); return Ok(output); diff --git a/src/normal_diff.rs b/src/normal_diff.rs index f5a833f2..921e7158 100644 --- a/src/normal_diff.rs +++ b/src/normal_diff.rs @@ -6,7 +6,7 @@ use std::io::Write; use crate::params::Params; -use crate::utils::{do_write_line, line_prefix}; +use crate::utils::{do_write_line, line_prefix, ComparisonLine}; #[derive(Debug, PartialEq)] struct Mismatch { @@ -32,25 +32,32 @@ impl Mismatch { } // Produces a diff between the expected output and actual output. -fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Vec { +fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { + let stop_early = params.brief; let mut line_number_expected = 1; let mut line_number_actual = 1; let mut results = Vec::new(); let mut mismatch = Mismatch::new(line_number_expected, line_number_actual); - let mut expected_lines: Vec<&[u8]> = expected.split(|&c| c == b'\n').collect(); - let mut actual_lines: Vec<&[u8]> = actual.split(|&c| c == b'\n').collect(); + let mut expected_lines: Vec = expected + .split(|&c| c == b'\n') + .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) + .collect(); + let mut actual_lines: Vec = actual + .split(|&c| c == b'\n') + .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) + .collect(); debug_assert_eq!(b"".split(|&c| c == b'\n').count(), 1); // ^ means that underflow here is impossible let expected_lines_count = expected_lines.len() - 1; let actual_lines_count = actual_lines.len() - 1; - if expected_lines.last() == Some(&&b""[..]) { + if expected_lines.last().is_some_and(|l| l.raw.is_empty()) { expected_lines.pop(); } - if actual_lines.last() == Some(&&b""[..]) { + if actual_lines.last().is_some_and(|l| l.raw.is_empty()) { actual_lines.pop(); } @@ -121,7 +128,7 @@ 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(); - let diff_results = make_diff(expected, actual, params.brief); + let diff_results = make_diff(expected, actual, params); if params.brief && !diff_results.is_empty() { write!(&mut output, "\0").unwrap(); return output; diff --git a/src/params.rs b/src/params.rs index 73ea45e0..61ee9d1f 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 ignore_tab_expansion: bool, pub suppress_blank_empty: bool, pub tabsize: usize, pub width: usize, @@ -98,6 +99,7 @@ impl Default for Params { report_identical_files: false, brief: false, expand_tabs: false, + ignore_tab_expansion: false, suppress_blank_empty: false, tabsize: 8, width: 130, @@ -277,6 +279,10 @@ pub fn parse_params>( params.expand_tabs = true; continue; } + if param == "-E" || param == "--ignore-tab-expansion" { + params.ignore_tab_expansion = true; + continue; + } if param == "--suppress-blank-empty" { params.suppress_blank_empty = true; continue; @@ -1015,6 +1021,26 @@ mod tests { } } #[test] + fn ignore_tab_expansion() { + for option in ["-E", "--ignore-tab-expansion"] { + assert_eq!( + Ok(Params { + executable: os("diff"), + from: os("foo"), + to: os("bar"), + ignore_tab_expansion: true, + ..Default::default() + }), + parse_params( + [os("diff"), os(option), os("foo"), os("bar")] + .iter() + .cloned() + .peekable() + ) + ); + } + } + #[test] fn tabsize() { assert_eq!( Ok(Params { diff --git a/src/side_diff.rs b/src/side_diff.rs index faebfd19..9f0f8ca1 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -9,6 +9,7 @@ use std::{io::Write, vec}; use unicode_width::UnicodeWidthStr; use crate::params::Params; +use crate::utils::ComparisonLine; const GUTTER_WIDTH_MIN: usize = 3; @@ -316,15 +317,21 @@ pub fn diff( ) -> Vec { // ^ The left file ^ The right file - let mut left_lines: Vec<&[u8]> = from_file.split_inclusive(|&c| c == b'\n').collect(); - let mut right_lines: Vec<&[u8]> = to_file.split_inclusive(|&c| c == b'\n').collect(); + let mut left_lines: Vec = from_file + .split_inclusive(|&c| c == b'\n') + .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) + .collect(); + let mut right_lines: Vec = to_file + .split_inclusive(|&c| c == b'\n') + .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) + .collect(); let config = Config::new(params.width, params.tabsize, params.expand_tabs); - if left_lines.last() == Some(&&b""[..]) { + if left_lines.last().is_some_and(|l| l.raw.is_empty()) { left_lines.pop(); } - if right_lines.last() == Some(&&b""[..]) { + if right_lines.last().is_some_and(|l| l.raw.is_empty()) { right_lines.pop(); } @@ -352,10 +359,12 @@ pub fn diff( */ 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::Left(left_ln) => push_output(left_ln.raw, b"", b'<', output, &config).unwrap(), + Result::Right(right_ln) => { + push_output(b"", right_ln.raw, b'>', output, &config).unwrap() + } Result::Both(left_ln, right_ln) => { - push_output(left_ln, right_ln, b' ', output, &config).unwrap() + push_output(left_ln.raw, right_ln.raw, b' ', output, &config).unwrap() } } } diff --git a/src/unified_diff.rs b/src/unified_diff.rs index bd8ea43e..b6e052e0 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, line_prefix}; +use crate::utils::{do_write_line, line_prefix, ComparisonLine}; use crate::utils::{get_modification_time, write_file_header, FunctionLineFinder, TimestampStyle}; #[derive(Debug, PartialEq)] @@ -36,12 +36,9 @@ impl Mismatch { } // Produces a diff between the expected output and actual output. -fn make_diff( - expected: &[u8], - actual: &[u8], - context_size: usize, - stop_early: bool, -) -> Vec { +fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { + let context_size = params.context_count; + let stop_early = params.brief; let mut line_number_expected = 1; let mut line_number_actual = 1; let mut context_queue: VecDeque<&[u8]> = VecDeque::with_capacity(context_size); @@ -52,19 +49,25 @@ fn make_diff( let mut results = Vec::new(); let mut mismatch = Mismatch::new(0, 0); - let mut expected_lines: Vec<&[u8]> = expected.split(|&c| c == b'\n').collect(); - let mut actual_lines: Vec<&[u8]> = actual.split(|&c| c == b'\n').collect(); + let mut expected_lines: Vec = expected + .split(|&c| c == b'\n') + .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) + .collect(); + let mut actual_lines: Vec = actual + .split(|&c| c == b'\n') + .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) + .collect(); debug_assert_eq!(b"".split(|&c| c == b'\n').count(), 1); // ^ means that underflow here is impossible let expected_lines_count = expected_lines.len() as u32 - 1; let actual_lines_count = actual_lines.len() as u32 - 1; - if expected_lines.last() == Some(&&b""[..]) { + if expected_lines.last().is_some_and(|l| l.raw.is_empty()) { expected_lines.pop(); } - if actual_lines.last() == Some(&&b""[..]) { + if actual_lines.last().is_some_and(|l| l.raw.is_empty()) { actual_lines.pop(); } @@ -179,7 +182,7 @@ fn make_diff( if lines_since_mismatch < context_size { mismatch.lines.push(DiffLine::Context(str.to_vec())); } else if context_size > 0 { - context_queue.push_back(str); + context_queue.push_back(str.raw); } lines_since_mismatch += 1; } @@ -262,7 +265,7 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { params.to_label.as_deref(), &to_modified_time, ); - let diff_results = make_diff(expected, actual, params.context_count, params.brief); + let diff_results = make_diff(expected, actual, params); if diff_results.is_empty() { return Vec::new(); } diff --git a/src/utils.rs b/src/utils.rs index 8bd2d18b..29b8913b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -107,6 +107,51 @@ pub fn do_write_line( } } +/// Returns the line with tabs expanded according to `tabsize` and terminal column rules. +pub fn expand_tabs_to_vec(line: &[u8], tabsize: usize) -> Vec { + let mut expanded = Vec::with_capacity(line.len()); + let _ = do_expand_tabs(&mut expanded, line, tabsize, b""); + expanded +} + +/// A line of text for diff comparison that can optionally ignore differences +/// caused by tab expansion (-E / --ignore-tab-expansion). +#[derive(Clone, Debug)] +pub struct ComparisonLine<'a> { + pub raw: &'a [u8], + key: std::borrow::Cow<'a, [u8]>, +} + +impl<'a> ComparisonLine<'a> { + pub fn new(raw: &'a [u8], ignore_tab_expansion: bool, tabsize: usize) -> Self { + let key = if ignore_tab_expansion && (raw.contains(&b'\t') || raw.contains(&b'\x08')) { + std::borrow::Cow::Owned(expand_tabs_to_vec(raw, tabsize)) + } else { + std::borrow::Cow::Borrowed(raw) + }; + Self { raw, key } + } +} + +impl<'a> std::ops::Deref for ComparisonLine<'a> { + type Target = [u8]; + fn deref(&self) -> &Self::Target { + self.raw + } +} + +impl<'a> AsRef<[u8]> for ComparisonLine<'a> { + fn as_ref(&self) -> &[u8] { + self.raw + } +} + +impl<'a> PartialEq for ComparisonLine<'a> { + fn eq(&self, other: &Self) -> bool { + self.key == other.key + } +} + /// 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. diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 6173c199..2b41191c 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -1131,3 +1131,69 @@ fn label_errors(#[case] args: &[&str], #[case] error: &str) { .fails_with_code(2) .stderr_contains(error); } + +#[test] +fn ignore_tab_expansion() { + let (at, ts) = at_and_ts!(); + at.write("a", "a\tb\n"); + at.write("b", "a b\n"); + + // Without -E, they differ + ts.ucmd().args(&["a", "b"]).fails_with_code(1); + + // With -E / --ignore-tab-expansion, treated as equal + for option in ["-E", "--ignore-tab-expansion"] { + ts.ucmd().args(&[option, "a", "b"]).succeeds().no_stdout(); + ts.ucmd() + .args(&["-s", option, "a", "b"]) + .succeeds() + .stdout_is("Files a and b are identical\n"); + } + + // Supported in multiple output formats + for format in ["-u", "-c", "-e"] { + ts.ucmd() + .args(&["-E", format, "a", "b"]) + .succeeds() + .no_stdout(); + } + + // Side-by-side format prints common lines with space gutter and exits 0 + ts.ucmd() + .args(&["-E", "-y", "a", "b"]) + .succeeds() + .stdout_contains("a\tb"); +} + +#[test] +fn ignore_tab_expansion_custom_tabsize() { + let (at, ts) = at_and_ts!(); + at.write("a", "a\tb\n"); + at.write("b_tab4", "a b\n"); + at.write("b_tab8", "a b\n"); + + // With default tabsize 8, matches b_tab8 + ts.ucmd() + .args(&["-E", "a", "b_tab8"]) + .succeeds() + .no_stdout(); + + // With --tabsize=4, matches b_tab4 + ts.ucmd() + .args(&["-E", "--tabsize=4", "a", "b_tab4"]) + .succeeds() + .no_stdout(); + + ts.ucmd() + .args(&["-E", "--tabsize=4", "a", "b_tab8"]) + .fails_with_code(1); +} + +#[test] +fn ignore_tab_expansion_control_and_wide_chars() { + let (at, ts) = at_and_ts!(); + at.write("old", "ab\rc\t1\n\x08x\t1\n中\t2\n"); + at.write("new", "ab\rc 1\nx 1\n中 2\n"); + + ts.ucmd().args(&["-E", "old", "new"]).succeeds().no_stdout(); +} From 8cfa8b27068805a7f05849c86daf403d944a2771 Mon Sep 17 00:00:00 2001 From: Yunus Ardaguney Date: Fri, 20 Nov 2026 11:20:24 +0300 Subject: [PATCH 2/2] refactor: deduplicate comparison line helpers, clean up ComparisonLine, and add differing files test --- src/context_diff.rs | 12 +++--------- src/ed_diff.rs | 12 +++--------- src/normal_diff.rs | 12 +++--------- src/side_diff.rs | 12 +++--------- src/unified_diff.rs | 12 +++--------- src/utils.rs | 37 +++++++++++++++++++++++++------------ tests/by-util/test_diff.rs | 26 ++++++++++++++++++++++++++ 7 files changed, 66 insertions(+), 57 deletions(-) diff --git a/src/context_diff.rs b/src/context_diff.rs index 8903c220..ff303428 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, line_prefix, ComparisonLine}; +use crate::utils::{comparison_lines, do_write_line, line_prefix}; use crate::utils::{ get_modification_time, is_posix_locale, write_file_header, FunctionLineFinder, TimestampStyle, }; @@ -108,14 +108,8 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { let mut results = Vec::new(); let mut mismatch = Mismatch::new(0, 0); - let mut expected_lines: Vec = expected - .split(|&c| c == b'\n') - .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) - .collect(); - let mut actual_lines: Vec = actual - .split(|&c| c == b'\n') - .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) - .collect(); + let mut expected_lines = comparison_lines(expected, params); + let mut actual_lines = comparison_lines(actual, params); debug_assert_eq!(b"".split(|&c| c == b'\n').count(), 1); // ^ means that underflow here is impossible diff --git a/src/ed_diff.rs b/src/ed_diff.rs index f9ed016e..fe0ae7fb 100644 --- a/src/ed_diff.rs +++ b/src/ed_diff.rs @@ -6,7 +6,7 @@ use std::io::Write; use crate::params::Params; -use crate::utils::{do_write_line, ComparisonLine}; +use crate::utils::{comparison_lines, do_write_line}; #[derive(Debug, PartialEq)] struct Mismatch { @@ -52,14 +52,8 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Result = expected - .split(|&c| c == b'\n') - .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) - .collect(); - let mut actual_lines: Vec = actual - .split(|&c| c == b'\n') - .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) - .collect(); + let mut expected_lines = comparison_lines(expected, params); + let mut actual_lines = comparison_lines(actual, params); debug_assert_eq!(b"".split(|&c| c == b'\n').count(), 1); // ^ means that underflow here is impossible diff --git a/src/normal_diff.rs b/src/normal_diff.rs index 921e7158..e97b5d48 100644 --- a/src/normal_diff.rs +++ b/src/normal_diff.rs @@ -6,7 +6,7 @@ use std::io::Write; use crate::params::Params; -use crate::utils::{do_write_line, line_prefix, ComparisonLine}; +use crate::utils::{comparison_lines, do_write_line, line_prefix}; #[derive(Debug, PartialEq)] struct Mismatch { @@ -39,14 +39,8 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { let mut results = Vec::new(); let mut mismatch = Mismatch::new(line_number_expected, line_number_actual); - let mut expected_lines: Vec = expected - .split(|&c| c == b'\n') - .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) - .collect(); - let mut actual_lines: Vec = actual - .split(|&c| c == b'\n') - .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) - .collect(); + let mut expected_lines = comparison_lines(expected, params); + let mut actual_lines = comparison_lines(actual, params); debug_assert_eq!(b"".split(|&c| c == b'\n').count(), 1); // ^ means that underflow here is impossible diff --git a/src/side_diff.rs b/src/side_diff.rs index 9f0f8ca1..5252f1f3 100644 --- a/src/side_diff.rs +++ b/src/side_diff.rs @@ -9,7 +9,7 @@ use std::{io::Write, vec}; use unicode_width::UnicodeWidthStr; use crate::params::Params; -use crate::utils::ComparisonLine; +use crate::utils::comparison_lines_inclusive; const GUTTER_WIDTH_MIN: usize = 3; @@ -317,14 +317,8 @@ pub fn diff( ) -> Vec { // ^ The left file ^ The right file - let mut left_lines: Vec = from_file - .split_inclusive(|&c| c == b'\n') - .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) - .collect(); - let mut right_lines: Vec = to_file - .split_inclusive(|&c| c == b'\n') - .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) - .collect(); + let mut left_lines = comparison_lines_inclusive(from_file, params); + let mut right_lines = comparison_lines_inclusive(to_file, params); let config = Config::new(params.width, params.tabsize, params.expand_tabs); if left_lines.last().is_some_and(|l| l.raw.is_empty()) { diff --git a/src/unified_diff.rs b/src/unified_diff.rs index b6e052e0..ae72461d 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, line_prefix, ComparisonLine}; +use crate::utils::{comparison_lines, do_write_line, line_prefix}; use crate::utils::{get_modification_time, write_file_header, FunctionLineFinder, TimestampStyle}; #[derive(Debug, PartialEq)] @@ -49,14 +49,8 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { let mut results = Vec::new(); let mut mismatch = Mismatch::new(0, 0); - let mut expected_lines: Vec = expected - .split(|&c| c == b'\n') - .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) - .collect(); - let mut actual_lines: Vec = actual - .split(|&c| c == b'\n') - .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) - .collect(); + let mut expected_lines = comparison_lines(expected, params); + let mut actual_lines = comparison_lines(actual, params); debug_assert_eq!(b"".split(|&c| c == b'\n').count(), 1); // ^ means that underflow here is impossible diff --git a/src/utils.rs b/src/utils.rs index 29b8913b..c7e527dc 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -108,7 +108,7 @@ pub fn do_write_line( } /// Returns the line with tabs expanded according to `tabsize` and terminal column rules. -pub fn expand_tabs_to_vec(line: &[u8], tabsize: usize) -> Vec { +fn expand_tabs_to_vec(line: &[u8], tabsize: usize) -> Vec { let mut expanded = Vec::with_capacity(line.len()); let _ = do_expand_tabs(&mut expanded, line, tabsize, b""); expanded @@ -116,23 +116,42 @@ pub fn expand_tabs_to_vec(line: &[u8], tabsize: usize) -> Vec { /// A line of text for diff comparison that can optionally ignore differences /// caused by tab expansion (-E / --ignore-tab-expansion). -#[derive(Clone, Debug)] +#[derive(Debug)] pub struct ComparisonLine<'a> { pub raw: &'a [u8], - key: std::borrow::Cow<'a, [u8]>, + key: Cow<'a, [u8]>, } impl<'a> ComparisonLine<'a> { - pub fn new(raw: &'a [u8], ignore_tab_expansion: bool, tabsize: usize) -> Self { + fn new(raw: &'a [u8], ignore_tab_expansion: bool, tabsize: usize) -> Self { let key = if ignore_tab_expansion && (raw.contains(&b'\t') || raw.contains(&b'\x08')) { - std::borrow::Cow::Owned(expand_tabs_to_vec(raw, tabsize)) + Cow::Owned(expand_tabs_to_vec(raw, tabsize)) } else { - std::borrow::Cow::Borrowed(raw) + Cow::Borrowed(raw) }; Self { raw, key } } } +/// Splits `content` into lines wrapped for comparison according to `params`. +pub fn comparison_lines<'a>(content: &'a [u8], params: &Params) -> Vec> { + content + .split(|&c| c == b'\n') + .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) + .collect() +} + +/// Splits `content` into inclusive lines (preserving trailing newlines) wrapped for comparison. +pub fn comparison_lines_inclusive<'a>( + content: &'a [u8], + params: &Params, +) -> Vec> { + content + .split_inclusive(|&c| c == b'\n') + .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) + .collect() +} + impl<'a> std::ops::Deref for ComparisonLine<'a> { type Target = [u8]; fn deref(&self) -> &Self::Target { @@ -140,12 +159,6 @@ impl<'a> std::ops::Deref for ComparisonLine<'a> { } } -impl<'a> AsRef<[u8]> for ComparisonLine<'a> { - fn as_ref(&self) -> &[u8] { - self.raw - } -} - impl<'a> PartialEq for ComparisonLine<'a> { fn eq(&self, other: &Self) -> bool { self.key == other.key diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 2b41191c..62eea35b 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -1197,3 +1197,29 @@ fn ignore_tab_expansion_control_and_wide_chars() { ts.ucmd().args(&["-E", "old", "new"]).succeeds().no_stdout(); } + +#[test] +fn ignore_tab_expansion_with_differing_files() { + let (at, ts) = at_and_ts!(); + // In tabsize 8, 'header\t1' has 6 chars ('header') + 2 spaces to column 8. + // So 'header\t1' expands to 'header 1'. + at.write("d1", "header\t1\nold line\nfooter\t2\n"); + at.write("d2", "header 1\nnew line\nfooter 2\n"); + + // Without -E, all 3 lines differ + ts.ucmd() + .args(&["-u", "d1", "d2"]) + .fails_with_code(1) + .stdout_contains("-header\t1") + .stdout_contains("+header 1") + .stdout_contains("-old line") + .stdout_contains("+new line") + .stdout_contains("-footer\t2") + .stdout_contains("+footer 2"); + + // With -E, header and footer match as unchanged context lines + ts.ucmd() + .args(&["-E", "-u", "d1", "d2"]) + .fails_with_code(1) + .stdout_contains(" header\t1\n-old line\n+new line\n footer\t2\n"); +}