Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -281,6 +283,10 @@ pub fn parse_params<I: Iterator<Item = OsString>>(
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;
Expand Down
48 changes: 39 additions & 9 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,29 +115,58 @@ fn expand_tabs_to_vec(line: &[u8], tabsize: usize) -> Vec<u8> {
}

/// 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<bool>,
}

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<ComparisonLine<'a>> {
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()
}

Expand All @@ -146,9 +175,10 @@ pub fn comparison_lines_inclusive<'a>(
content: &'a [u8],
params: &Params,
) -> Vec<ComparisonLine<'a>> {
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()
}

Expand Down
73 changes: 73 additions & 0 deletions tests/by-util/test_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add -y too, side_diff.rs is changed as well

#[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();
}
Loading