Run cargo fmt
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# Difflib [](https://travis-ci.org/DimaKudosh/difflib)
|
||||
|
||||
Port of Python's difflib library to Rust.
|
||||
Port of Python's difflib library to Rust.
|
||||
It's provide all necessary tools for comparing word sequences.
|
||||
|
||||
## Installation
|
||||
@@ -21,20 +21,33 @@ extern crate difflib;
|
||||
use difflib::differ::Differ;
|
||||
use difflib::sequencematcher::SequenceMatcher;
|
||||
|
||||
|
||||
fn main() {
|
||||
// unified_diff
|
||||
let first_text = "one two three four".split(" ").collect::<Vec<&str>>();
|
||||
let second_text = "zero one tree four".split(" ").collect::<Vec<&str>>();
|
||||
let diff = difflib::unified_diff(&first_text, &second_text, "Original", "Current",
|
||||
"2005-01-26 23:30:50", "2010-04-02 10:20:52", 3);
|
||||
let diff = difflib::unified_diff(
|
||||
&first_text,
|
||||
&second_text,
|
||||
"Original",
|
||||
"Current",
|
||||
"2005-01-26 23:30:50",
|
||||
"2010-04-02 10:20:52",
|
||||
3,
|
||||
);
|
||||
for line in &diff {
|
||||
println!("{:?}", line);
|
||||
}
|
||||
|
||||
//context_diff
|
||||
let diff = difflib::context_diff(&first_text, &second_text, "Original", "Current",
|
||||
"2005-01-26 23:30:50", "2010-04-02 10:20:52", 3);
|
||||
let diff = difflib::context_diff(
|
||||
&first_text,
|
||||
&second_text,
|
||||
"Original",
|
||||
"Current",
|
||||
"2005-01-26 23:30:50",
|
||||
"2010-04-02 10:20:52",
|
||||
3,
|
||||
);
|
||||
for line in &diff {
|
||||
println!("{:?}", line);
|
||||
}
|
||||
@@ -62,7 +75,7 @@ fn main() {
|
||||
let grouped_opcodes = matcher.get_grouped_opcodes(2);
|
||||
println!("{:?}", grouped_opcodes);
|
||||
let ratio = matcher.ratio();
|
||||
println!("{:?}", ratio);
|
||||
println!("{:?}", ratio);
|
||||
matcher.set_seqs("aaaaa", "aaaab");
|
||||
println!("{:?}", matcher.ratio());
|
||||
}
|
||||
|
||||
+18
-15
@@ -3,30 +3,33 @@ extern crate difflib;
|
||||
use difflib::differ::Differ;
|
||||
use difflib::sequencematcher::SequenceMatcher;
|
||||
|
||||
|
||||
fn main() {
|
||||
// unified_diff
|
||||
let first_text = "one two three four".split(" ").collect::<Vec<&str>>();
|
||||
let second_text = "zero one tree four".split(" ").collect::<Vec<&str>>();
|
||||
let diff = difflib::unified_diff(&first_text,
|
||||
&second_text,
|
||||
"Original",
|
||||
"Current",
|
||||
"2005-01-26 23:30:50",
|
||||
"2010-04-02 10:20:52",
|
||||
3);
|
||||
let diff = difflib::unified_diff(
|
||||
&first_text,
|
||||
&second_text,
|
||||
"Original",
|
||||
"Current",
|
||||
"2005-01-26 23:30:50",
|
||||
"2010-04-02 10:20:52",
|
||||
3,
|
||||
);
|
||||
for line in &diff {
|
||||
println!("{:?}", line);
|
||||
}
|
||||
|
||||
//context_diff
|
||||
let diff = difflib::context_diff(&first_text,
|
||||
&second_text,
|
||||
"Original",
|
||||
"Current",
|
||||
"2005-01-26 23:30:50",
|
||||
"2010-04-02 10:20:52",
|
||||
3);
|
||||
let diff = difflib::context_diff(
|
||||
&first_text,
|
||||
&second_text,
|
||||
"Original",
|
||||
"Current",
|
||||
"2005-01-26 23:30:50",
|
||||
"2010-04-02 10:20:52",
|
||||
3,
|
||||
);
|
||||
for line in &diff {
|
||||
println!("{:?}", line);
|
||||
}
|
||||
|
||||
+145
-101
@@ -1,7 +1,6 @@
|
||||
use sequencematcher::{SequenceMatcher, Sequence};
|
||||
use utils::{str_with_similar_chars, count_leading};
|
||||
use sequencematcher::{Sequence, SequenceMatcher};
|
||||
use std::cmp;
|
||||
|
||||
use utils::{count_leading, str_with_similar_chars};
|
||||
|
||||
pub struct Differ {
|
||||
pub line_junk: Option<fn(&str) -> bool>,
|
||||
@@ -16,10 +15,11 @@ impl Differ {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn compare<T: ?Sized + Sequence>(&self,
|
||||
first_sequence: &T,
|
||||
second_sequence: &T)
|
||||
-> Vec<String> {
|
||||
pub fn compare<T: ?Sized + Sequence>(
|
||||
&self,
|
||||
first_sequence: &T,
|
||||
second_sequence: &T,
|
||||
) -> Vec<String> {
|
||||
let mut matcher = SequenceMatcher::new(first_sequence, second_sequence);
|
||||
matcher.set_is_junk(self.line_junk);
|
||||
let mut res = Vec::new();
|
||||
@@ -27,12 +27,14 @@ impl Differ {
|
||||
let mut gen = Vec::new();
|
||||
match opcode.tag.as_ref() {
|
||||
"replace" => {
|
||||
gen = self.fancy_replace(first_sequence,
|
||||
opcode.first_start,
|
||||
opcode.first_end,
|
||||
second_sequence,
|
||||
opcode.second_start,
|
||||
opcode.second_end)
|
||||
gen = self.fancy_replace(
|
||||
first_sequence,
|
||||
opcode.first_start,
|
||||
opcode.first_end,
|
||||
second_sequence,
|
||||
opcode.second_start,
|
||||
opcode.second_end,
|
||||
)
|
||||
}
|
||||
"delete" => {
|
||||
gen = self.dump("-", first_sequence, opcode.first_start, opcode.first_end)
|
||||
@@ -52,12 +54,13 @@ impl Differ {
|
||||
res
|
||||
}
|
||||
|
||||
fn dump<T: ?Sized + Sequence>(&self,
|
||||
tag: &str,
|
||||
sequence: &T,
|
||||
start: usize,
|
||||
end: usize)
|
||||
-> Vec<String> {
|
||||
fn dump<T: ?Sized + Sequence>(
|
||||
&self,
|
||||
tag: &str,
|
||||
sequence: &T,
|
||||
start: usize,
|
||||
end: usize,
|
||||
) -> Vec<String> {
|
||||
let mut res = Vec::new();
|
||||
for i in start..end {
|
||||
match sequence.at_index(i) {
|
||||
@@ -68,14 +71,15 @@ impl Differ {
|
||||
res
|
||||
}
|
||||
|
||||
fn plain_replace<T: ?Sized + Sequence>(&self,
|
||||
first_sequence: &T,
|
||||
first_start: usize,
|
||||
first_end: usize,
|
||||
second_sequence: &T,
|
||||
second_start: usize,
|
||||
second_end: usize)
|
||||
-> Vec<String> {
|
||||
fn plain_replace<T: ?Sized + Sequence>(
|
||||
&self,
|
||||
first_sequence: &T,
|
||||
first_start: usize,
|
||||
first_end: usize,
|
||||
second_sequence: &T,
|
||||
second_start: usize,
|
||||
second_end: usize,
|
||||
) -> Vec<String> {
|
||||
if !(first_start < first_end && second_start < second_end) {
|
||||
return Vec::new();
|
||||
}
|
||||
@@ -94,14 +98,15 @@ impl Differ {
|
||||
first
|
||||
}
|
||||
|
||||
fn fancy_replace<T: ?Sized + Sequence>(&self,
|
||||
first_sequence: &T,
|
||||
first_start: usize,
|
||||
first_end: usize,
|
||||
second_sequence: &T,
|
||||
second_start: usize,
|
||||
second_end: usize)
|
||||
-> Vec<String> {
|
||||
fn fancy_replace<T: ?Sized + Sequence>(
|
||||
&self,
|
||||
first_sequence: &T,
|
||||
first_start: usize,
|
||||
first_end: usize,
|
||||
second_sequence: &T,
|
||||
second_start: usize,
|
||||
second_end: usize,
|
||||
) -> Vec<String> {
|
||||
let mut res = Vec::new();
|
||||
let (mut best_ratio, cutoff) = (0.74, 0.75);
|
||||
let (mut best_i, mut best_j) = (0, 0);
|
||||
@@ -130,14 +135,17 @@ impl Differ {
|
||||
}
|
||||
if best_ratio < cutoff {
|
||||
if eqi.is_none() {
|
||||
res.extend(self.plain_replace(first_sequence,
|
||||
first_start,
|
||||
first_end,
|
||||
second_sequence,
|
||||
second_start,
|
||||
second_end)
|
||||
.iter()
|
||||
.cloned());
|
||||
res.extend(
|
||||
self.plain_replace(
|
||||
first_sequence,
|
||||
first_start,
|
||||
first_end,
|
||||
second_sequence,
|
||||
second_start,
|
||||
second_end,
|
||||
).iter()
|
||||
.cloned(),
|
||||
);
|
||||
return res;
|
||||
}
|
||||
best_i = eqi.unwrap();
|
||||
@@ -145,23 +153,30 @@ impl Differ {
|
||||
} else {
|
||||
eqi = None;
|
||||
}
|
||||
res.extend(self.fancy_helper(first_sequence,
|
||||
first_start,
|
||||
best_i,
|
||||
second_sequence,
|
||||
second_start,
|
||||
best_j)
|
||||
.iter()
|
||||
.cloned());
|
||||
let (first_element, second_element) = (first_sequence.at_index(best_i).unwrap(),
|
||||
second_sequence.at_index(best_j).unwrap());
|
||||
res.extend(
|
||||
self.fancy_helper(
|
||||
first_sequence,
|
||||
first_start,
|
||||
best_i,
|
||||
second_sequence,
|
||||
second_start,
|
||||
best_j,
|
||||
).iter()
|
||||
.cloned(),
|
||||
);
|
||||
let (first_element, second_element) = (
|
||||
first_sequence.at_index(best_i).unwrap(),
|
||||
second_sequence.at_index(best_j).unwrap(),
|
||||
);
|
||||
if eqi.is_none() {
|
||||
let (mut first_tag, mut second_tag) = (String::new(), String::new());
|
||||
let mut cruncher = SequenceMatcher::new(first_element, second_element);
|
||||
cruncher.set_is_junk(self.char_junk);
|
||||
for opcode in &cruncher.get_opcodes() {
|
||||
let (first_length, second_length) = (opcode.first_end - opcode.first_start,
|
||||
opcode.second_end - opcode.second_start);
|
||||
let (first_length, second_length) = (
|
||||
opcode.first_end - opcode.first_start,
|
||||
opcode.second_end - opcode.second_start,
|
||||
);
|
||||
match opcode.tag.as_ref() {
|
||||
"replace" => {
|
||||
first_tag.push_str(&str_with_similar_chars('^', first_length));
|
||||
@@ -180,42 +195,50 @@ impl Differ {
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
res.extend(self.qformat(&first_element, &second_element, &first_tag, &second_tag)
|
||||
.iter()
|
||||
.cloned());
|
||||
res.extend(
|
||||
self.qformat(&first_element, &second_element, &first_tag, &second_tag)
|
||||
.iter()
|
||||
.cloned(),
|
||||
);
|
||||
} else {
|
||||
let mut s = String::from(" ");
|
||||
s.push_str(&first_element);
|
||||
res.push(s);
|
||||
}
|
||||
res.extend(self.fancy_helper(first_sequence,
|
||||
best_i + 1,
|
||||
first_end,
|
||||
second_sequence,
|
||||
best_j + 1,
|
||||
second_end)
|
||||
.iter()
|
||||
.cloned());
|
||||
res.extend(
|
||||
self.fancy_helper(
|
||||
first_sequence,
|
||||
best_i + 1,
|
||||
first_end,
|
||||
second_sequence,
|
||||
best_j + 1,
|
||||
second_end,
|
||||
).iter()
|
||||
.cloned(),
|
||||
);
|
||||
res
|
||||
}
|
||||
|
||||
fn fancy_helper<T: ?Sized + Sequence>(&self,
|
||||
first_sequence: &T,
|
||||
first_start: usize,
|
||||
first_end: usize,
|
||||
second_sequence: &T,
|
||||
second_start: usize,
|
||||
second_end: usize)
|
||||
-> Vec<String> {
|
||||
fn fancy_helper<T: ?Sized + Sequence>(
|
||||
&self,
|
||||
first_sequence: &T,
|
||||
first_start: usize,
|
||||
first_end: usize,
|
||||
second_sequence: &T,
|
||||
second_start: usize,
|
||||
second_end: usize,
|
||||
) -> Vec<String> {
|
||||
let mut res = Vec::new();
|
||||
if first_start < first_end {
|
||||
if second_start < second_end {
|
||||
res = self.fancy_replace(first_sequence,
|
||||
first_start,
|
||||
first_end,
|
||||
second_sequence,
|
||||
second_start,
|
||||
second_end);
|
||||
res = self.fancy_replace(
|
||||
first_sequence,
|
||||
first_start,
|
||||
first_end,
|
||||
second_sequence,
|
||||
second_start,
|
||||
second_end,
|
||||
);
|
||||
} else {
|
||||
res = self.dump("-", first_sequence, first_start, first_end);
|
||||
}
|
||||
@@ -225,17 +248,20 @@ impl Differ {
|
||||
res
|
||||
}
|
||||
|
||||
fn qformat(&self,
|
||||
first_line: &str,
|
||||
second_line: &str,
|
||||
first_tags: &str,
|
||||
second_tags: &str)
|
||||
-> Vec<String> {
|
||||
fn qformat(
|
||||
&self,
|
||||
first_line: &str,
|
||||
second_line: &str,
|
||||
first_tags: &str,
|
||||
second_tags: &str,
|
||||
) -> Vec<String> {
|
||||
let mut res = Vec::new();
|
||||
let mut first_tags = first_tags;
|
||||
let mut second_tags = second_tags;
|
||||
let mut common = cmp::min(count_leading(first_line, '\t'),
|
||||
count_leading(second_line, '\t'));
|
||||
let mut common = cmp::min(
|
||||
count_leading(first_line, '\t'),
|
||||
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();
|
||||
@@ -243,15 +269,21 @@ impl Differ {
|
||||
let mut s = String::from(format!("- {}", first_line));
|
||||
res.push(s);
|
||||
if first_tags != "" {
|
||||
s = String::from(format!("? {}{}\n", str_with_similar_chars('\t', common), first_tags));
|
||||
s = String::from(format!(
|
||||
"? {}{}\n",
|
||||
str_with_similar_chars('\t', common),
|
||||
first_tags
|
||||
));
|
||||
res.push(s);
|
||||
}
|
||||
s = String::from(format!("+ {}", second_line));
|
||||
res.push(s);
|
||||
if second_tags != "" {
|
||||
s = String::from(format!("? {}{}\n",
|
||||
str_with_similar_chars('\t', common),
|
||||
second_tags));
|
||||
s = String::from(format!(
|
||||
"? {}{}\n",
|
||||
str_with_similar_chars('\t', common),
|
||||
second_tags
|
||||
));
|
||||
res.push(s);
|
||||
}
|
||||
res
|
||||
@@ -283,19 +315,31 @@ impl Differ {
|
||||
#[test]
|
||||
fn test_fancy_replace() {
|
||||
let differ = Differ::new();
|
||||
let result = differ.fancy_replace(&vec!["abcDefghiJkl\n"], 0, 1, &vec!["abcdefGhijkl\n"], 0, 1)
|
||||
let result = differ
|
||||
.fancy_replace(&vec!["abcDefghiJkl\n"], 0, 1, &vec!["abcdefGhijkl\n"], 0, 1)
|
||||
.join("");
|
||||
assert_eq!(result,
|
||||
"- abcDefghiJkl\n? ^ ^ ^\n+ abcdefGhijkl\n? ^ ^ ^\n");
|
||||
assert_eq!(
|
||||
result,
|
||||
"- abcDefghiJkl\n? ^ ^ ^\n+ abcdefGhijkl\n? ^ ^ ^\n"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_qformat() {
|
||||
let differ = Differ::new();
|
||||
let result = differ.qformat("\tabcDefghiJkl\n",
|
||||
"\tabcdefGhijkl\n",
|
||||
" ^ ^ ^ ",
|
||||
" ^ ^ ^ ");
|
||||
assert_eq!(result,
|
||||
vec!["- \tabcDefghiJkl\n", "? \t ^ ^ ^\n", "+ \tabcdefGhijkl\n", "? \t ^ ^ ^\n"]);
|
||||
let result = differ.qformat(
|
||||
"\tabcDefghiJkl\n",
|
||||
"\tabcdefGhijkl\n",
|
||||
" ^ ^ ^ ",
|
||||
" ^ ^ ^ ",
|
||||
);
|
||||
assert_eq!(
|
||||
result,
|
||||
vec![
|
||||
"- \tabcDefghiJkl\n",
|
||||
"? \t ^ ^ ^\n",
|
||||
"+ \tabcdefGhijkl\n",
|
||||
"? \t ^ ^ ^\n",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
+41
-34
@@ -1,18 +1,17 @@
|
||||
pub mod sequencematcher;
|
||||
pub mod differ;
|
||||
pub mod sequencematcher;
|
||||
mod utils;
|
||||
|
||||
|
||||
use sequencematcher::{Sequence, SequenceMatcher};
|
||||
use std::collections::HashMap;
|
||||
use sequencematcher::{SequenceMatcher, Sequence};
|
||||
use utils::{format_range_unified, format_range_context};
|
||||
use utils::{format_range_context, format_range_unified};
|
||||
|
||||
|
||||
pub fn get_close_matches<'a>(word: &str,
|
||||
possibilities: Vec<&'a str>,
|
||||
n: usize,
|
||||
cutoff: f32)
|
||||
-> Vec<&'a str> {
|
||||
pub fn get_close_matches<'a>(
|
||||
word: &str,
|
||||
possibilities: Vec<&'a str>,
|
||||
n: usize,
|
||||
cutoff: f32,
|
||||
) -> Vec<&'a str> {
|
||||
if !(0.0 <= cutoff && cutoff <= 1.0) {
|
||||
panic!("Cutoff must be greater than 0.0 and lower than 1.0");
|
||||
}
|
||||
@@ -30,14 +29,15 @@ pub fn get_close_matches<'a>(word: &str,
|
||||
res.iter().map(|x| x.1).collect()
|
||||
}
|
||||
|
||||
pub fn unified_diff<T: Sequence>(first_sequence: &T,
|
||||
second_sequence: &T,
|
||||
from_file: &str,
|
||||
to_file: &str,
|
||||
from_file_date: &str,
|
||||
to_file_date: &str,
|
||||
n: usize)
|
||||
-> Vec<String> {
|
||||
pub fn unified_diff<T: Sequence>(
|
||||
first_sequence: &T,
|
||||
second_sequence: &T,
|
||||
from_file: &str,
|
||||
to_file: &str,
|
||||
from_file_date: &str,
|
||||
to_file_date: &str,
|
||||
n: usize,
|
||||
) -> Vec<String> {
|
||||
let mut res = Vec::new();
|
||||
let lineterm = '\n';
|
||||
let mut started = false;
|
||||
@@ -53,7 +53,10 @@ pub fn unified_diff<T: Sequence>(first_sequence: &T,
|
||||
let (first, last) = (group.first().unwrap(), group.last().unwrap());
|
||||
let file1_range = format_range_unified(first.first_start, last.first_end);
|
||||
let file2_range = format_range_unified(first.second_start, last.second_end);
|
||||
res.push(format!("@@ -{} +{} @@{}", file1_range, file2_range, lineterm));
|
||||
res.push(format!(
|
||||
"@@ -{} +{} @@{}",
|
||||
file1_range, file2_range, lineterm
|
||||
));
|
||||
for code in group {
|
||||
if code.tag == "equal" {
|
||||
for i in code.first_start..code.first_end {
|
||||
@@ -76,15 +79,15 @@ pub fn unified_diff<T: Sequence>(first_sequence: &T,
|
||||
res
|
||||
}
|
||||
|
||||
|
||||
pub fn context_diff<T: Sequence>(first_sequence: &T,
|
||||
second_sequence: &T,
|
||||
from_file: &str,
|
||||
to_file: &str,
|
||||
from_file_date: &str,
|
||||
to_file_date: &str,
|
||||
n: usize)
|
||||
-> Vec<String> {
|
||||
pub fn context_diff<T: Sequence>(
|
||||
first_sequence: &T,
|
||||
second_sequence: &T,
|
||||
from_file: &str,
|
||||
to_file: &str,
|
||||
from_file_date: &str,
|
||||
to_file_date: &str,
|
||||
n: usize,
|
||||
) -> Vec<String> {
|
||||
let mut res = Vec::new();
|
||||
let lineterm = '\n';
|
||||
let mut prefix: HashMap<String, String> = HashMap::new();
|
||||
@@ -117,9 +120,11 @@ pub fn context_diff<T: Sequence>(first_sequence: &T,
|
||||
for opcode in group {
|
||||
if opcode.tag != "insert" {
|
||||
for i in opcode.first_start..opcode.first_end {
|
||||
res.push(format!("{}{}",
|
||||
prefix.get(&opcode.tag).unwrap(),
|
||||
first_sequence.at_index(i).unwrap()));
|
||||
res.push(format!(
|
||||
"{}{}",
|
||||
prefix.get(&opcode.tag).unwrap(),
|
||||
first_sequence.at_index(i).unwrap()
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -137,9 +142,11 @@ pub fn context_diff<T: Sequence>(first_sequence: &T,
|
||||
for opcode in group {
|
||||
if opcode.tag != "delete" {
|
||||
for i in opcode.second_start..opcode.second_end {
|
||||
res.push(format!("{}{}",
|
||||
prefix.get(&opcode.tag).unwrap(),
|
||||
second_sequence.at_index(i).unwrap()));
|
||||
res.push(format!(
|
||||
"{}{}",
|
||||
prefix.get(&opcode.tag).unwrap(),
|
||||
second_sequence.at_index(i).unwrap()
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+60
-41
@@ -1,7 +1,7 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::cmp::{max, min};
|
||||
use utils::calculate_ratio;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fmt::Debug;
|
||||
use utils::calculate_ratio;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)]
|
||||
pub struct Match {
|
||||
@@ -30,12 +30,13 @@ pub struct Opcode {
|
||||
}
|
||||
|
||||
impl Opcode {
|
||||
fn new(tag: String,
|
||||
first_start: usize,
|
||||
first_end: usize,
|
||||
second_start: usize,
|
||||
second_end: usize)
|
||||
-> Opcode {
|
||||
fn new(
|
||||
tag: String,
|
||||
first_start: usize,
|
||||
first_end: usize,
|
||||
second_start: usize,
|
||||
second_end: usize,
|
||||
) -> Opcode {
|
||||
Opcode {
|
||||
tag: tag,
|
||||
first_start: first_start,
|
||||
@@ -113,7 +114,6 @@ pub struct SequenceMatcher<'a, T: 'a + ?Sized + Sequence> {
|
||||
second_sequence_popular: HashSet<&'a str>,
|
||||
}
|
||||
|
||||
|
||||
impl<'a, T: ?Sized + Sequence> SequenceMatcher<'a, T> {
|
||||
pub fn new(first_sequence: &'a T, second_sequence: &'a T) -> SequenceMatcher<'a, T> {
|
||||
let mut matcher = SequenceMatcher {
|
||||
@@ -156,7 +156,8 @@ impl<'a, T: ?Sized + Sequence> SequenceMatcher<'a, T> {
|
||||
let second_sequence = self.second_sequence;
|
||||
let mut second_sequence_elements = HashMap::new();
|
||||
for i in 0..second_sequence.len() {
|
||||
let mut counter = second_sequence_elements.entry(second_sequence.at_index(i).unwrap())
|
||||
let mut counter = second_sequence_elements
|
||||
.entry(second_sequence.at_index(i).unwrap())
|
||||
.or_insert(Vec::new());
|
||||
counter.push(i);
|
||||
}
|
||||
@@ -188,12 +189,13 @@ impl<'a, T: ?Sized + Sequence> SequenceMatcher<'a, T> {
|
||||
self.second_sequence_popular = popular;
|
||||
}
|
||||
|
||||
pub fn find_longest_match(&self,
|
||||
first_start: usize,
|
||||
first_end: usize,
|
||||
second_start: usize,
|
||||
second_end: usize)
|
||||
-> Match {
|
||||
pub fn find_longest_match(
|
||||
&self,
|
||||
first_start: usize,
|
||||
first_end: usize,
|
||||
second_start: usize,
|
||||
second_end: usize,
|
||||
) -> Match {
|
||||
let first_sequence = &self.first_sequence;
|
||||
let second_sequence = &self.second_sequence;
|
||||
let second_sequence_elements = &self.second_sequence_elements;
|
||||
@@ -234,15 +236,18 @@ impl<'a, T: ?Sized + Sequence> SequenceMatcher<'a, T> {
|
||||
j2len = new_j2len;
|
||||
}
|
||||
for _ in 0..2 {
|
||||
while best_i > first_start && best_j > second_start &&
|
||||
first_sequence.at_index(best_i - 1) == second_sequence.at_index(best_j - 1) {
|
||||
while best_i > first_start
|
||||
&& best_j > second_start
|
||||
&& first_sequence.at_index(best_i - 1) == second_sequence.at_index(best_j - 1)
|
||||
{
|
||||
best_i = best_i - 1;
|
||||
best_j = best_j - 1;
|
||||
best_size = best_size + 1;
|
||||
}
|
||||
while best_i + best_size < first_end && best_j + best_size < second_end &&
|
||||
first_sequence.at_index(best_i + best_size) ==
|
||||
second_sequence.at_index(best_j + best_size) {
|
||||
while best_i + best_size < first_end && best_j + best_size < second_end
|
||||
&& first_sequence.at_index(best_i + best_size)
|
||||
== second_sequence.at_index(best_j + best_size)
|
||||
{
|
||||
best_size += 1;
|
||||
}
|
||||
}
|
||||
@@ -266,10 +271,12 @@ impl<'a, T: ?Sized + Sequence> SequenceMatcher<'a, T> {
|
||||
queue.push((first_start, m.first_start, second_start, m.second_start));
|
||||
}
|
||||
if m.first_start + m.size < first_end && m.second_start + m.size < second_end {
|
||||
queue.push((m.first_start + m.size,
|
||||
first_end,
|
||||
m.second_start + m.size,
|
||||
second_end));
|
||||
queue.push((
|
||||
m.first_start + m.size,
|
||||
first_end,
|
||||
m.second_start + m.size,
|
||||
second_end,
|
||||
));
|
||||
}
|
||||
matches.push(m);
|
||||
}
|
||||
@@ -319,9 +326,13 @@ impl<'a, T: ?Sized + Sequence> SequenceMatcher<'a, T> {
|
||||
i = m.first_start + m.size;
|
||||
j = m.second_start + m.size;
|
||||
if m.size != 0 {
|
||||
opcodes.push(
|
||||
Opcode::new(String::from("equal"), m.first_start, i, m.second_start, j)
|
||||
);
|
||||
opcodes.push(Opcode::new(
|
||||
String::from("equal"),
|
||||
m.first_start,
|
||||
i,
|
||||
m.second_start,
|
||||
j,
|
||||
));
|
||||
}
|
||||
}
|
||||
self.opcodes = Some(opcodes);
|
||||
@@ -350,21 +361,25 @@ Opcode::new(String::from("equal"), m.first_start, i, m.second_start, j)
|
||||
for code in &codes {
|
||||
let (mut first_start, mut second_start) = (code.first_start, code.second_start);
|
||||
if code.tag == "equal" && code.first_end - code.first_start > nn {
|
||||
group.push(Opcode::new(code.tag.clone(),
|
||||
code.first_start,
|
||||
min(code.first_end, code.first_start + n),
|
||||
code.second_start,
|
||||
min(code.second_end, code.second_start + n)));
|
||||
group.push(Opcode::new(
|
||||
code.tag.clone(),
|
||||
code.first_start,
|
||||
min(code.first_end, code.first_start + n),
|
||||
code.second_start,
|
||||
min(code.second_end, code.second_start + n),
|
||||
));
|
||||
res.push(group.clone());
|
||||
group.clear();
|
||||
first_start = max(first_start, code.first_end.saturating_sub(n));
|
||||
second_start = max(second_start, code.second_end.saturating_sub(n));
|
||||
}
|
||||
group.push(Opcode::new(code.tag.clone(),
|
||||
first_start,
|
||||
code.first_end,
|
||||
second_start,
|
||||
code.second_end));
|
||||
group.push(Opcode::new(
|
||||
code.tag.clone(),
|
||||
first_start,
|
||||
code.first_end,
|
||||
second_start,
|
||||
code.second_end,
|
||||
));
|
||||
}
|
||||
if !group.is_empty() && !(group.len() == 1 && group.first().unwrap().tag == "equal") {
|
||||
res.push(group.clone());
|
||||
@@ -373,8 +388,12 @@ Opcode::new(String::from("equal"), m.first_start, i, m.second_start, j)
|
||||
}
|
||||
|
||||
pub fn ratio(&mut self) -> f32 {
|
||||
let matches = self.get_matching_blocks().iter().fold(0, |res, &m| res + m.size);
|
||||
calculate_ratio(matches,
|
||||
self.first_sequence.len() + self.second_sequence.len())
|
||||
let matches = self.get_matching_blocks()
|
||||
.iter()
|
||||
.fold(0, |res, &m| res + m.size);
|
||||
calculate_ratio(
|
||||
matches,
|
||||
self.first_sequence.len() + self.second_sequence.len(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ pub fn calculate_ratio(matches: usize, length: usize) -> f32 {
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
|
||||
pub fn str_with_similar_chars(c: char, length: usize) -> String {
|
||||
let mut s = String::new();
|
||||
for _ in 0..length {
|
||||
@@ -14,7 +13,6 @@ pub fn str_with_similar_chars(c: char, length: usize) -> String {
|
||||
s
|
||||
}
|
||||
|
||||
|
||||
pub fn count_leading(line: &str, c: char) -> usize {
|
||||
let (mut i, n) = (0, line.len());
|
||||
let line: Vec<char> = line.chars().collect();
|
||||
@@ -24,7 +22,6 @@ pub fn count_leading(line: &str, c: char) -> usize {
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
pub fn format_range_unified(start: usize, end: usize) -> String {
|
||||
let mut beginning = start + 1;
|
||||
let length = end - start;
|
||||
@@ -37,7 +34,6 @@ pub fn format_range_unified(start: usize, end: usize) -> String {
|
||||
format!("{},{}", beginning, length)
|
||||
}
|
||||
|
||||
|
||||
pub fn format_range_context(start: usize, end: usize) -> String {
|
||||
let mut beginning = start + 1;
|
||||
let length = end - start;
|
||||
|
||||
+38
-28
@@ -1,7 +1,7 @@
|
||||
extern crate difflib;
|
||||
|
||||
use difflib::sequencematcher::{SequenceMatcher, Match, Opcode};
|
||||
use difflib::differ::Differ;
|
||||
use difflib::sequencematcher::{Match, Opcode, SequenceMatcher};
|
||||
|
||||
#[test]
|
||||
fn test_longest_match() {
|
||||
@@ -97,8 +97,10 @@ fn test_differ_compare() {
|
||||
let second_text = vec!["ore\n", "tree\n", "emu\n"];
|
||||
let differ = Differ::new();
|
||||
let result = differ.compare(&first_text, &second_text).join("");
|
||||
assert_eq!(result,
|
||||
"- one\n? ^\n+ ore\n? ^\n- two\n- three\n? -\n+ tree\n+ emu\n");
|
||||
assert_eq!(
|
||||
result,
|
||||
"- one\n? ^\n+ ore\n? ^\n- two\n- three\n? -\n+ tree\n+ emu\n"
|
||||
);
|
||||
}
|
||||
|
||||
fn is_junk_char(ch: &str) -> bool {
|
||||
@@ -115,8 +117,10 @@ fn test_differ_compare_with_func() {
|
||||
let mut differ = Differ::new();
|
||||
differ.char_junk = Some(is_junk_char);
|
||||
let result = differ.compare(&first_text, &second_text).join("");
|
||||
assert_eq!(result,
|
||||
"- one\n? ^\n+ ore\n? ^\n- two\n- three\n? -\n+ tree\n+ emu\n");
|
||||
assert_eq!(
|
||||
result,
|
||||
"- one\n? ^\n+ ore\n? ^\n- two\n- three\n? -\n+ tree\n+ emu\n"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -133,33 +137,39 @@ fn test_differ_restore() {
|
||||
fn test_unified_diff() {
|
||||
let first_text = "one two three four".split(" ").collect::<Vec<&str>>();
|
||||
let second_text = "zero one tree four".split(" ").collect::<Vec<&str>>();
|
||||
let result = difflib::unified_diff(&first_text,
|
||||
&second_text,
|
||||
"Original",
|
||||
"Current",
|
||||
"2005-01-26 23:30:50",
|
||||
"2010-04-02 10:20:52",
|
||||
3)
|
||||
.join("");
|
||||
assert_eq!(result,
|
||||
"--- Original\t2005-01-26 23:30:50\n+++ Current\t2010-04-02 10:20:52\n@@ -1,4 \
|
||||
+1,4 @@\n+zero one-two-three+tree four");
|
||||
let result = difflib::unified_diff(
|
||||
&first_text,
|
||||
&second_text,
|
||||
"Original",
|
||||
"Current",
|
||||
"2005-01-26 23:30:50",
|
||||
"2010-04-02 10:20:52",
|
||||
3,
|
||||
).join("");
|
||||
assert_eq!(
|
||||
result,
|
||||
"--- Original\t2005-01-26 23:30:50\n+++ Current\t2010-04-02 10:20:52\n@@ -1,4 \
|
||||
+1,4 @@\n+zero one-two-three+tree four"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_context_diff() {
|
||||
let first_text = "one two three four".split(" ").collect::<Vec<&str>>();
|
||||
let second_text = "zero one tree four".split(" ").collect::<Vec<&str>>();
|
||||
let result = difflib::context_diff(&first_text,
|
||||
&second_text,
|
||||
"Original",
|
||||
"Current",
|
||||
"2005-01-26 23:30:50",
|
||||
"2010-04-02 10:20:52",
|
||||
3)
|
||||
.join("");
|
||||
assert_eq!(result,
|
||||
"*** Original\t2005-01-26 23:30:50\n--- Current\t2010-04-02 \
|
||||
10:20:52\n***************\n*** 1,4 ****\n one! two! three four--- 1,4 ----\n+ \
|
||||
zero one! tree four");
|
||||
let result = difflib::context_diff(
|
||||
&first_text,
|
||||
&second_text,
|
||||
"Original",
|
||||
"Current",
|
||||
"2005-01-26 23:30:50",
|
||||
"2010-04-02 10:20:52",
|
||||
3,
|
||||
).join("");
|
||||
assert_eq!(
|
||||
result,
|
||||
"*** Original\t2005-01-26 23:30:50\n--- Current\t2010-04-02 \
|
||||
10:20:52\n***************\n*** 1,4 ****\n one! two! three four--- 1,4 ----\n+ \
|
||||
zero one! tree four"
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user