diff --git a/src/context_diff.rs b/src/context_diff.rs index ff303428..75e7092d 100644 --- a/src/context_diff.rs +++ b/src/context_diff.rs @@ -100,7 +100,7 @@ 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 context_queue: VecDeque<&[u8]> = VecDeque::with_capacity(context_size); + let mut context_queue: VecDeque<(&[u8], &[u8])> = VecDeque::with_capacity(context_size); // Like GNU diff, keep changes in one hunk while at most twice the context // size of unchanged lines separates them. let max_gap = 2 * context_size; @@ -137,9 +137,9 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { ); } - while let Some(line) = context_queue.pop_front() { - mismatch.expected.push(DiffLine::Context(line.to_vec())); - mismatch.actual.push(DiffLine::Context(line.to_vec())); + while let Some((exp_line, act_line)) = context_queue.pop_front() { + mismatch.expected.push(DiffLine::Context(exp_line.to_vec())); + mismatch.actual.push(DiffLine::Context(act_line.to_vec())); } ChangeBlock::mark(&mut block, &mismatch, true, false); @@ -159,9 +159,9 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { ); } - while let Some(line) = context_queue.pop_front() { - mismatch.expected.push(DiffLine::Context(line.to_vec())); - mismatch.actual.push(DiffLine::Context(line.to_vec())); + while let Some((exp_line, act_line)) = context_queue.pop_front() { + mismatch.expected.push(DiffLine::Context(exp_line.to_vec())); + mismatch.actual.push(DiffLine::Context(act_line.to_vec())); } ChangeBlock::mark(&mut block, &mismatch, false, true); @@ -172,7 +172,7 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { line_number_actual += 1; lines_since_mismatch = 0; } - diff::Result::Both(str, _) => { + diff::Result::Both(str1, str2) => { // 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) @@ -181,8 +181,8 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { // this is the last line: lines still in the queue are past // the trailing context and there is no next hunk to lead into if lines_since_mismatch < context_size { - mismatch.expected.push(DiffLine::Context(str.to_vec())); - mismatch.actual.push(DiffLine::Context(str.to_vec())); + mismatch.expected.push(DiffLine::Context(str1.to_vec())); + mismatch.actual.push(DiffLine::Context(str2.to_vec())); mismatch.expected_missing_nl = true; mismatch.actual_missing_nl = true; } @@ -194,13 +194,13 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { line_number_actual - context_queue.len(), ); } - while let Some(line) = context_queue.pop_front() { - mismatch.expected.push(DiffLine::Context(line.to_vec())); - mismatch.actual.push(DiffLine::Context(line.to_vec())); + while let Some((exp_line, act_line)) = context_queue.pop_front() { + mismatch.expected.push(DiffLine::Context(exp_line.to_vec())); + mismatch.actual.push(DiffLine::Context(act_line.to_vec())); } ChangeBlock::mark(&mut block, &mismatch, true, true); - mismatch.expected.push(DiffLine::Change(str.to_vec())); - mismatch.actual.push(DiffLine::Change(str.to_vec())); + mismatch.expected.push(DiffLine::Change(str1.to_vec())); + mismatch.actual.push(DiffLine::Change(str2.to_vec())); mismatch.actual_missing_nl = true; lines_since_mismatch = 0; } else if line_number_expected > expected_lines_count { @@ -211,14 +211,14 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { line_number_actual - context_queue.len(), ); } - while let Some(line) = context_queue.pop_front() { - mismatch.expected.push(DiffLine::Context(line.to_vec())); - mismatch.actual.push(DiffLine::Context(line.to_vec())); + while let Some((exp_line, act_line)) = context_queue.pop_front() { + mismatch.expected.push(DiffLine::Context(exp_line.to_vec())); + mismatch.actual.push(DiffLine::Context(act_line.to_vec())); } ChangeBlock::mark(&mut block, &mismatch, true, true); - mismatch.expected.push(DiffLine::Change(str.to_vec())); + mismatch.expected.push(DiffLine::Change(str1.to_vec())); mismatch.expected_missing_nl = true; - mismatch.actual.push(DiffLine::Change(str.to_vec())); + mismatch.actual.push(DiffLine::Change(str2.to_vec())); lines_since_mismatch = 0; } else { ChangeBlock::close(&mut block, &mut mismatch); @@ -227,10 +227,10 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { let _ = context_queue.pop_front(); } if lines_since_mismatch < context_size { - mismatch.expected.push(DiffLine::Context(str.to_vec())); - mismatch.actual.push(DiffLine::Context(str.to_vec())); + mismatch.expected.push(DiffLine::Context(str1.to_vec())); + mismatch.actual.push(DiffLine::Context(str2.to_vec())); } else if context_size > 0 { - context_queue.push_back(str.raw); + context_queue.push_back((str1.raw, str2.raw)); } lines_since_mismatch += 1; } diff --git a/src/normal_diff.rs b/src/normal_diff.rs index e97b5d48..b9beaecf 100644 --- a/src/normal_diff.rs +++ b/src/normal_diff.rs @@ -71,7 +71,7 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { mismatch.actual_missing_nl = line_number_actual > actual_lines_count; line_number_actual += 1; } - diff::Result::Both(str, _) => { + diff::Result::Both(str1, str2) => { match ( line_number_expected > expected_lines_count, line_number_actual > actual_lines_count, @@ -79,16 +79,16 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { (true, false) => { line_number_expected += 1; line_number_actual += 1; - mismatch.expected.push(str.to_vec()); + mismatch.expected.push(str1.to_vec()); mismatch.expected_missing_nl = true; - mismatch.actual.push(str.to_vec()); + mismatch.actual.push(str2.to_vec()); } (false, true) => { line_number_expected += 1; line_number_actual += 1; - mismatch.actual.push(str.to_vec()); + mismatch.actual.push(str2.to_vec()); mismatch.actual_missing_nl = true; - mismatch.expected.push(str.to_vec()); + mismatch.expected.push(str1.to_vec()); } (true, true) | (false, false) => { line_number_expected += 1; @@ -619,4 +619,22 @@ mod tests { ); assert!(nodiff_brief.is_empty()); } + + #[test] + fn test_ignore_case() { + let from = b"Hello World\nLine 2\n"; + let to = b"hello world\nline 2\n"; + let res = diff( + from, + to, + &Params { + ignore_case: true, + ..Default::default() + }, + ); + assert!(res.is_empty()); + + let res_diff = diff(from, to, &Params::default()); + assert!(!res_diff.is_empty()); + } } diff --git a/src/params.rs b/src/params.rs index 61ee9d1f..2232cc55 100644 --- a/src/params.rs +++ b/src/params.rs @@ -64,6 +64,7 @@ pub struct Params { /// The name from which to start comparing the top-level directories /// (`-S`). pub starting_file: Option, + pub ignore_case: bool, pub ignore_file_name_case: bool, pub no_dereference: bool, /// Set when `from` did not exist and was compared as an empty file. @@ -111,6 +112,7 @@ impl Default for Params { function_line_patterns: Vec::new(), recursive: false, starting_file: None, + ignore_case: false, ignore_file_name_case: false, no_dereference: false, from_missing: false, @@ -255,6 +257,14 @@ pub fn parse_params>( params.no_dereference = true; continue; } + if param == "-i" || param == "--ignore-case" { + params.ignore_case = true; + continue; + } + if param == "--no-ignore-case" { + params.ignore_case = false; + continue; + } if param == "--ignore-file-name-case" { params.ignore_file_name_case = true; continue; @@ -1513,6 +1523,24 @@ mod tests { ) .is_err()); } + #[test] + fn ignore_case() { + for (args, expected) in [ + (&[][..], false), + (&["-i"], true), + (&["--ignore-case"], true), + (&["--no-ignore-case"], false), + (&["-i", "--no-ignore-case"], false), + (&["--no-ignore-case", "-i"], true), + ] { + let args = std::iter::once("diff") + .chain(args.iter().copied()) + .chain(["foo", "bar"]); + let params = parse_params(args.map(os).peekable()).unwrap(); + assert_eq!(params.ignore_case, expected); + } + } + #[test] fn ignore_file_name_case() { // The last of --ignore-file-name-case and --no-ignore-file-name-case diff --git a/src/unified_diff.rs b/src/unified_diff.rs index ae72461d..b6658840 100644 --- a/src/unified_diff.rs +++ b/src/unified_diff.rs @@ -125,7 +125,7 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { line_number_actual += 1; lines_since_mismatch = 0; } - diff::Result::Both(str, _) => { + diff::Result::Both(str1, str2) => { // 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) @@ -133,7 +133,7 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { // this is the last line: lines still in the queue are past // the trailing context and there is no next hunk to lead into if lines_since_mismatch < context_size { - mismatch.lines.push(DiffLine::Context(str.to_vec())); + mismatch.lines.push(DiffLine::Context(str1.to_vec())); mismatch.lines.push(DiffLine::MissingNL); } } else if line_number_actual > actual_lines_count { @@ -148,8 +148,8 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { debug_assert!(mismatch.lines.last() != Some(&DiffLine::MissingNL)); mismatch.lines.push(DiffLine::Context(line.to_vec())); } - mismatch.lines.push(DiffLine::Expected(str.to_vec())); - mismatch.lines.push(DiffLine::Actual(str.to_vec())); + mismatch.lines.push(DiffLine::Expected(str1.to_vec())); + mismatch.lines.push(DiffLine::Actual(str2.to_vec())); mismatch.lines.push(DiffLine::MissingNL); lines_since_mismatch = 0; } else if line_number_expected > expected_lines_count { @@ -164,9 +164,9 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { debug_assert!(mismatch.lines.last() != Some(&DiffLine::MissingNL)); mismatch.lines.push(DiffLine::Context(line.to_vec())); } - mismatch.lines.push(DiffLine::Expected(str.to_vec())); + mismatch.lines.push(DiffLine::Expected(str1.to_vec())); mismatch.lines.push(DiffLine::MissingNL); - mismatch.lines.push(DiffLine::Actual(str.to_vec())); + mismatch.lines.push(DiffLine::Actual(str2.to_vec())); lines_since_mismatch = 0; } else { debug_assert!(context_queue.len() <= context_size); @@ -174,9 +174,9 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec { let _ = context_queue.pop_front(); } if lines_since_mismatch < context_size { - mismatch.lines.push(DiffLine::Context(str.to_vec())); + mismatch.lines.push(DiffLine::Context(str1.to_vec())); } else if context_size > 0 { - context_queue.push_back(str.raw); + context_queue.push_back(str1.raw); } lines_since_mismatch += 1; } diff --git a/src/utils.rs b/src/utils.rs index c7e527dc..ba8d9f20 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -123,21 +123,34 @@ pub struct ComparisonLine<'a> { } 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)) + /// `posix` is whether `--ignore-case` folds only ASCII letters (see + /// [`lowercase_key`]); it is looked up once per file, not per line. + fn new(raw: &'a [u8], params: &Params, posix: bool) -> Self { + let expanded = + if params.ignore_tab_expansion && (raw.contains(&b'\t') || raw.contains(&b'\x08')) { + Cow::Owned(expand_tabs_to_vec(raw, params.tabsize)) + } else { + Cow::Borrowed(raw) + }; + let key = if params.ignore_case { + Cow::Owned(lowercase_key(&expanded, true, posix).into_owned()) } else { - Cow::Borrowed(raw) + expanded }; Self { raw, key } } } +fn ignore_case_is_posix(params: &Params) -> bool { + params.ignore_case && is_posix_locale("LC_CTYPE") +} + /// Splits `content` into lines wrapped for comparison according to `params`. pub fn comparison_lines<'a>(content: &'a [u8], params: &Params) -> Vec> { + let posix = ignore_case_is_posix(params); content .split(|&c| c == b'\n') - .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) + .map(|line| ComparisonLine::new(line, params, posix)) .collect() } @@ -146,9 +159,10 @@ pub fn comparison_lines_inclusive<'a>( content: &'a [u8], params: &Params, ) -> Vec> { + let posix = ignore_case_is_posix(params); content .split_inclusive(|&c| c == b'\n') - .map(|line| ComparisonLine::new(line, params.ignore_tab_expansion, params.tabsize)) + .map(|line| ComparisonLine::new(line, params, posix)) .collect() } @@ -520,10 +534,71 @@ pub mod testcmds { }; } +fn lowercase_key<'a>(raw: &'a [u8], ignore_case: bool, posix: bool) -> Cow<'a, [u8]> { + if !ignore_case { + return Cow::Borrowed(raw); + } + if posix { + return Cow::Owned(raw.to_ascii_lowercase()); + } + if let Ok(s) = std::str::from_utf8(raw) { + Cow::Owned(s.to_lowercase().into_bytes()) + } else { + Cow::Owned(raw.to_ascii_lowercase()) + } +} + #[cfg(test)] mod tests { use super::*; + mod ignore_case { + use super::*; + + #[test] + fn basics() { + assert_eq!( + lowercase_key(b"Funky Stuff", true, false), + lowercase_key(b"fUNKy stuFf", true, false) + ); + assert_eq!( + lowercase_key(b"hello", true, false), + lowercase_key(b"HELLO", true, false) + ); + assert_ne!( + lowercase_key(b"hello", true, false), + lowercase_key(b"world", true, false) + ); + assert_eq!( + lowercase_key(b"", true, false), + lowercase_key(b"", true, false) + ); + } + + #[test] + fn unicode_case_folding() { + let s1 = "AĀȀΆΑАӐḀἈA𐐀-Δ".as_bytes(); + let s2 = "aāȁάαаӑḁἀa𐐨-δ".as_bytes(); + assert_eq!( + lowercase_key(s1, true, false), + lowercase_key(s2, true, false) + ); + assert_ne!(lowercase_key(s1, true, true), lowercase_key(s2, true, true)); + } + + #[test] + fn non_utf8() { + assert_eq!( + lowercase_key(b"\xffa", true, false), + lowercase_key(b"\xffA", true, false) + ); + assert_ne!( + lowercase_key(b"\xffa", true, false), + lowercase_key(b"\xfeA", true, false) + ); + } + } + mod expand_tabs { use super::*; use pretty_assertions::assert_eq; diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 62eea35b..8fd5a3d6 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -1132,6 +1132,77 @@ fn label_errors(#[case] args: &[&str], #[case] error: &str) { .stderr_contains(error); } +#[test] +fn ignore_case() { + let (at, ts) = at_and_ts!(); + at.write("i1", "Funky Stuff\n"); + at.write("i2", "fUNKy stuFf\n"); + for option in ["-i", "--ignore-case"] { + ts.ucmd() + .args(&[option, "i1", "i2"]) + .succeeds() + .stdout_is(""); + } + // Without -i the files still differ. + ts.ucmd().args(&["i1", "i2"]).fails_with_code(1); + + // Overriding with --no-ignore-case + ts.ucmd() + .args(&["-i", "--no-ignore-case", "i1", "i2"]) + .fails_with_code(1); + + // Formats: unified, context, ed, side-by-side + ts.ucmd() + .args(&["-i", "-u", "i1", "i2"]) + .succeeds() + .stdout_is(""); + ts.ucmd() + .args(&["-i", "-c", "i1", "i2"]) + .succeeds() + .stdout_is(""); + ts.ucmd() + .args(&["-i", "-e", "i1", "i2"]) + .succeeds() + .stdout_is(""); + ts.ucmd().args(&["-i", "-y", "i1", "i2"]).succeeds(); + + // Unicode case folding: in UTF-8 locale, Unicode characters are folded + let (at, ts) = at_and_ts!(); + at.write("u1", "AĀȀΆΑАӐḀἈA𐐀-Δ\n"); + at.write("u2", "aāȁάαаӑḁἀa𐐨-δ\n"); + ts.ucmd() + .env("LC_ALL", "en_US.UTF-8") + .args(&["-i", "u1", "u2"]) + .succeeds() + .stdout_is(""); + + // In POSIX locale (LC_ALL=C), GNU diff only folds ASCII, so Unicode characters differ + ts.ucmd() + .env("LC_ALL", "C") + .args(&["-i", "u1", "u2"]) + .fails_with_code(1); + ts.ucmd().args(&["u1", "u2"]).fails_with_code(1); +} + +#[test] +fn ignore_case_context_lines() { + let (at, ts) = at_and_ts!(); + at.write("c1", "COMMON LINE\ndiff1\n"); + at.write("c2", "common line\ndiff2\n"); + + // Unified diff: common line differing only in case is emitted as context from from-file + ts.ucmd() + .args(&["-i", "-u", "c1", "c2"]) + .fails_with_code(1) + .stdout_contains(" COMMON LINE\n-diff1\n+diff2\n"); + + // Context diff: from-file hunk shows COMMON LINE, to-file hunk shows common line + ts.ucmd() + .args(&["-i", "-c", "c1", "c2"]) + .fails_with_code(1) + .stdout_contains(" COMMON LINE\n! diff1\n--- 1,2 ----\n common line\n! diff2\n"); +} + #[test] fn ignore_tab_expansion() { let (at, ts) = at_and_ts!(); @@ -1223,3 +1294,20 @@ fn ignore_tab_expansion_with_differing_files() { .fails_with_code(1) .stdout_contains(" header\t1\n-old line\n+new line\n footer\t2\n"); } + +#[test] +fn ignore_case_with_ignore_tab_expansion() { + let (at, ts) = at_and_ts!(); + at.write("a", "Hello\tWorld\n"); + at.write("b", "hello world\n"); + ts.ucmd().args(&["-i", "a", "b"]).fails_with_code(1); + ts.ucmd().args(&["-E", "a", "b"]).fails_with_code(1); + ts.ucmd() + .args(&["-i", "-E", "a", "b"]) + .succeeds() + .no_stdout(); + ts.ucmd() + .args(&["-i", "-E", "-u", "a", "b"]) + .succeeds() + .no_stdout(); +}