Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "difflib"
version = "0.4.0"
version = "0.5.0"
authors = ["Dima Kudosh <dimakudosh@gmail.com>"]
description = "Port of Python's difflib library to Rust."
documentation = "https://github.com/DimaKudosh/difflib/wiki"
Expand Down
13 changes: 11 additions & 2 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 @@ -268,8 +277,8 @@ impl Differ {
);
common = cmp::min(common, count_leading(first_tags.split_at(common).0, ' '));
common = cmp::min(common, count_leading(first_tags.split_at(common).0, ' '));
Comment thread
RandyMcMillan marked this conversation as resolved.
first_tags = first_tags.split_at(common).1.trim_right();
second_tags = second_tags.split_at(common).1.trim_right();
first_tags = Self::split_at_char_boundary(first_tags, common).1.trim_end();
second_tags = Self::split_at_char_boundary(second_tags, common).1.trim_end();
let mut s = format!("- {}", first_line);
res.push(s);
if first_tags != "" {
Expand Down
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_differ_compare_utf8() {
let first_text = vec!["\tcrab 🦀\n"];
let second_text = vec!["\tcrab 🦞\n"];
let differ = Differ::new();
let result = differ.compare(&first_text, &second_text).join("");
assert!(result.contains("🦀"));
assert!(result.contains("🦞"));
Comment thread
RandyMcMillan marked this conversation as resolved.
}

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