From 54d8f717f9f379694f178952148618385642fd9c Mon Sep 17 00:00:00 2001 From: Nadir Ali <36660597+Xnadir@users.noreply.github.com> Date: Sat, 28 Nov 2026 23:13:30 +0500 Subject: [PATCH] diff: add -i/--ignore-case Closes #406 Compare lines through ComparisonLine with their case folded (all letters in a UTF-8 locale, ASCII letters in the POSIX locale, as GNU diff does), so every output format and -E combine with it. Co-Authored-By: Claude Opus 5.5 --- src/params.rs | 6 ++++ src/utils.rs | 48 ++++++++++++++++++++----- tests/by-util/test_diff.rs | 73 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 9 deletions(-) diff --git a/src/params.rs b/src/params.rs index 84d2c7a5..bb9080a4 100644 --- a/src/params.rs +++ b/src/params.rs @@ -48,6 +48,7 @@ pub struct Params { pub brief: bool, pub expand_tabs: bool, pub ignore_tab_expansion: bool, + pub ignore_case: bool, pub suppress_blank_empty: bool, pub tabsize: usize, pub width: usize, @@ -100,6 +101,7 @@ impl Default for Params { brief: false, expand_tabs: false, ignore_tab_expansion: false, + ignore_case: false, suppress_blank_empty: false, tabsize: 8, width: 130, @@ -281,6 +283,10 @@ pub fn parse_params>( params.ignore_tab_expansion = true; continue; } + if param == "-i" || param == "--ignore-case" { + params.ignore_case = true; + continue; + } if param == "--suppress-blank-empty" { params.suppress_blank_empty = true; continue; diff --git a/src/utils.rs b/src/utils.rs index 6d6d1395..b278d9d1 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -115,29 +115,58 @@ 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). +/// caused by tab expansion (-E / --ignore-tab-expansion) or by case +/// (-i / --ignore-case). #[derive(Debug)] pub struct ComparisonLine<'a> { pub raw: &'a [u8], key: Cow<'a, [u8]>, } +/// How lines are compared: the options that change a line's comparison key. +#[derive(Clone, Copy)] +struct ComparisonRules { + ignore_tab_expansion: bool, + tabsize: usize, + /// `Some(utf8)` with -i: fold the case of all letters in a UTF-8 locale, + /// of ASCII letters only in the POSIX locale (as GNU diff does). + fold_case: Option, +} + +impl ComparisonRules { + fn from_params(params: &Params) -> Self { + Self { + ignore_tab_expansion: params.ignore_tab_expansion, + tabsize: params.tabsize, + fold_case: params.ignore_case.then(|| !is_posix_locale("LC_CTYPE")), + } + } +} + impl<'a> ComparisonLine<'a> { - 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')) { - Cow::Owned(expand_tabs_to_vec(raw, tabsize)) - } else { - Cow::Borrowed(raw) - }; + fn new(raw: &'a [u8], rules: ComparisonRules) -> Self { + let mut key = + if rules.ignore_tab_expansion && (raw.contains(&b'\t') || raw.contains(&b'\x08')) { + Cow::Owned(expand_tabs_to_vec(raw, rules.tabsize)) + } else { + Cow::Borrowed(raw) + }; + if let Some(utf8) = rules.fold_case { + key = Cow::Owned(match std::str::from_utf8(&key) { + Ok(text) if utf8 => text.to_lowercase().into_bytes(), + _ => key.to_ascii_lowercase(), + }); + } Self { raw, key } } } /// Splits `content` into lines wrapped for comparison according to `params`. pub fn comparison_lines<'a>(content: &'a [u8], params: &Params) -> Vec> { + let rules = ComparisonRules::from_params(params); content .split(|&c| c == b'\n') - .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) + .map(|line| ComparisonLine::new(line, rules)) .collect() } @@ -146,9 +175,10 @@ pub fn comparison_lines_inclusive<'a>( content: &'a [u8], params: &Params, ) -> Vec> { + let rules = ComparisonRules::from_params(params); content .split_inclusive(|&c| c == b'\n') - .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) + .map(|line| ComparisonLine::new(line, rules)) .collect() } diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 74c12453..2da46dc9 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -1370,3 +1370,76 @@ fn unified_missing_newline_deletions_first() { s.ends_with("@@ -1 +1,2 @@\n-1\n\\ No newline at end of file\n+0\n+1\n") }); } + +#[rstest] +fn ignore_case( + #[values(&[][..], &["-u"], &["-c"], &["-e"], &["-q"])] format: &[&str], + #[values("-i", "--ignore-case")] option: &str, +) { + let (at, ts) = at_and_ts!(); + at.write("i1", "Funky Stuff\n"); + at.write("i2", "fUNKy stuFf\n"); + ts.ucmd() + .args(format) + .args(&[option, "i1", "i2"]) + .succeeds() + .no_stdout(); + // Without -i the files differ. + ts.ucmd() + .args(format) + .args(&["i1", "i2"]) + .fails_with_code(1); +} + +#[test] +fn ignore_case_side_by_side() { + // Side by side output prints common lines too: with -i the two lines are + // one common line, without a change marker. + let (at, ts) = at_and_ts!(); + at.write("i1", "Funky Stuff\n"); + at.write("i2", "fUNKy stuFf\n"); + ts.ucmd() + .args(&["-y", "-i", "i1", "i2"]) + .succeeds() + .stdout_str_check(|s| { + s.lines().count() == 1 && !s.contains('|') && !s.contains('<') && !s.contains('>') + }); +} + +#[test] +fn ignore_case_reports_other_differences() { + let (at, ts) = at_and_ts!(); + at.write("a", "Foo\nbar\nBaz\n"); + at.write("b", "foo\nBAR\nqux\n"); + ts.ucmd() + .args(&["-i", "a", "b"]) + .fails_with_code(1) + .stdout_is("3c3\n< Baz\n---\n> qux\n"); +} + +#[test] +fn ignore_case_folds_non_ascii_only_in_a_utf8_locale() { + let (at, ts) = at_and_ts!(); + at.write("a", "A\u{100}\u{391}-\u{394}\n"); + at.write("b", "a\u{101}\u{3b1}-\u{3b4}\n"); + ts.ucmd() + .env("LC_ALL", "C.UTF-8") + .args(&["-i", "a", "b"]) + .succeeds() + .no_stdout(); + ts.ucmd() + .env("LC_ALL", "C") + .args(&["-i", "a", "b"]) + .fails_with_code(1); +} + +#[test] +fn ignore_case_with_ignore_tab_expansion() { + let (at, ts) = at_and_ts!(); + at.write("a", "Foo\tBar\n"); + at.write("b", "fOO bAR\n"); + ts.ucmd() + .args(&["-i", "-E", "a", "b"]) + .succeeds() + .no_stdout(); +}