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
46 changes: 23 additions & 23 deletions src/context_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {
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;
Expand Down Expand Up @@ -137,9 +137,9 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {
);
}

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);
Expand All @@ -159,9 +159,9 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {
);
}

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);
Expand All @@ -172,7 +172,7 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {
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)
Expand All @@ -181,8 +181,8 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {
// 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;
}
Expand All @@ -194,13 +194,13 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {
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 {
Expand All @@ -211,14 +211,14 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {
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);
Expand All @@ -227,10 +227,10 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {
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;
}
Expand Down
28 changes: 23 additions & 5 deletions src/normal_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,24 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {
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,
) {
(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;
Expand Down Expand Up @@ -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());
}
}
28 changes: 28 additions & 0 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub struct Params {
/// The name from which to start comparing the top-level directories
/// (`-S`).
pub starting_file: Option<OsString>,
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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -255,6 +257,14 @@ pub fn parse_params<I: Iterator<Item = OsString>>(
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;
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions src/unified_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {
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)
{
// 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 {
Expand All @@ -148,8 +148,8 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {
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 {
Expand All @@ -164,19 +164,19 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {
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);
if context_queue.len() >= context_size {
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);

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.

context lines are printed from the first file here, does it match GNU output?
please add a test with a real hunk where the context lines differ only in case

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added in commit 9a24b6d: added ignore_case_context_lines with a real hunk where context lines differ only in case, verifying they match GNU diff (GNU diff prints context lines from the first file).

}
lines_since_mismatch += 1;
}
Expand Down
87 changes: 81 additions & 6 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ComparisonLine<'a>> {
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()
}

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

Expand Down Expand Up @@ -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;
Expand Down
Loading
Loading