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
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.1"
Comment thread
RandyMcMillan marked this conversation as resolved.
authors = ["Dima Kudosh <dimakudosh@gmail.com>"]
description = "Port of Python's difflib library to Rust."
documentation = "https://github.com/DimaKudosh/difflib/wiki"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Simply add difflib to your dependencies block in Cargo.toml

```rust
[dependencies]
difflib = "0.4.0"
difflib = "0.5.1"
```

## Documentation
Expand Down
19 changes: 14 additions & 5 deletions src/differ.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use sequencematcher::SequenceMatcher;
use crate::sequencematcher::SequenceMatcher;
use std::cmp;
use utils::{count_leading, str_with_similar_chars};
use crate::utils::{count_leading, str_with_similar_chars};

#[derive(Default)]
pub struct Differ {
Expand Down 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 @@ -266,10 +275,10 @@ impl Differ {
count_leading(first_line, '\t'),
count_leading(second_line, '\t'),
);
common = cmp::min(common, count_leading(second_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.
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();
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
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ pub mod differ;
pub mod sequencematcher;
mod utils;

use sequencematcher::{Sequence, SequenceMatcher};
use crate::sequencematcher::{Sequence, SequenceMatcher};
use std::collections::HashMap;
use std::fmt::Display;
use utils::{format_range_context, format_range_unified};
use crate::utils::{format_range_context, format_range_unified};

pub fn get_close_matches<'a>(
word: &str,
Expand Down
4 changes: 2 additions & 2 deletions src/sequencematcher.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::cmp::{max, min};
use std::collections::HashMap;
use std::hash::Hash;
use utils::calculate_ratio;
use crate::utils::calculate_ratio;

#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)]
pub struct Match {
Expand Down 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
12 changes: 12 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ 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.
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