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
30 changes: 27 additions & 3 deletions src/differ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ impl Differ {
res
}

fn split_at_char_boundary<'a>(line: &'a str, char_idx: usize) -> (&'a str, &'a str) {
let byte_idx = line
.char_indices()
.nth(char_idx)
.map(|(i, _)| i)
.unwrap_or(line.len());
line.split_at(byte_idx)
}

fn qformat(
&self,
first_line: &str,
Expand All @@ -267,9 +276,9 @@ impl Differ {
count_leading(second_line, '\t'),
);
common = cmp::min(common, count_leading(first_tags.split_at(common).0, ' '));
common = cmp::min(common, count_leading(first_tags.split_at(common).0, ' '));
first_tags = first_tags.split_at(common).1.trim_right();
second_tags = second_tags.split_at(common).1.trim_right();
common = cmp::min(common, count_leading(second_tags.split_at(common).0, ' '));
first_tags = Self::split_at_char_boundary(first_tags, common).1.trim_right();
second_tags = Self::split_at_char_boundary(second_tags, common).1.trim_right();
Comment on lines +280 to +281
let mut s = format!("- {}", first_line);
res.push(s);
if first_tags != "" {
Expand Down Expand Up @@ -338,3 +347,18 @@ fn test_qformat() {
]
);
}

#[test]
fn test_qformat_uses_both_tag_lines_for_alignment() {
let differ = Differ::new();
let result = differ.qformat("\t\tabc\n", "\t\tadc\n", " ^", " ^");
assert_eq!(
result,
vec![
"- \t\tabc\n",
"? \t ^\n",
"+ \t\tadc\n",
"? \t^\n",
]
);
}
2 changes: 1 addition & 1 deletion src/sequencematcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl<'a, T: Sequence> SequenceMatcher<'a, T> {
let second_sequence = self.second_sequence;
let mut second_sequence_elements = HashMap::new();
for (i, item) in second_sequence.iter().enumerate() {
let mut counter = second_sequence_elements
let counter = second_sequence_elements
.entry(item)
.or_insert_with(Vec::new);
counter.push(i);
Expand Down
10 changes: 10 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ fn test_differ_restore() {
assert_eq!(second_text, Differ::restore(&diff, 2));
}

#[test]
fn test_utf8_intraline_diff_no_panic() {
let first_text = vec!["\tcrab 🦀\n"];
let second_text = vec!["\tcrab 🦞\n"];
Comment thread
RandyMcMillan marked this conversation as resolved.
let differ = Differ::new();
let result = differ.compare(&first_text, &second_text).join("");
assert!(result.contains("🦀"));
assert!(result.contains("🦞"));
}

#[test]
fn test_unified_diff() {
let first_text = "one two three four".split(" ").collect::<Vec<&str>>();
Expand Down
Loading