Skip to content

fix: 🐛 fix chain_second_seq bug when sequence length gte 200 #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
37 changes: 20 additions & 17 deletions src/differ.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use sequencematcher::SequenceMatcher;
use sequencematcher::{SequenceMatcher, Tag};
use std::cmp;
use utils::{count_leading, str_with_similar_chars};

Expand All @@ -22,8 +22,8 @@ impl Differ {
let mut res = Vec::new();
for opcode in matcher.get_opcodes() {
let mut gen = Vec::new();
match opcode.tag.as_ref() {
"replace" => {
match opcode.tag {
Tag::Replace => {
gen = self.fancy_replace(
first_sequence,
opcode.first_start,
Expand All @@ -33,13 +33,13 @@ impl Differ {
opcode.second_end,
)
}
"delete" => {
Tag::Delete => {
gen = self.dump("-", first_sequence, opcode.first_start, opcode.first_end)
}
"insert" => {
Tag::Insert => {
gen = self.dump("+", second_sequence, opcode.second_start, opcode.second_end)
}
"equal" => {
Tag::Equal => {
gen = self.dump(" ", first_sequence, opcode.first_start, opcode.first_end)
}
_ => {}
Expand Down Expand Up @@ -147,8 +147,9 @@ impl Differ {
second_sequence,
second_start,
second_end,
).iter()
.cloned(),
)
.iter()
.cloned(),
);
return res;
}
Expand All @@ -165,8 +166,9 @@ impl Differ {
second_sequence,
second_start,
best_j,
).iter()
.cloned(),
)
.iter()
.cloned(),
);
let first_element = &first_sequence[best_i];
let second_element = &second_sequence[best_j];
Expand All @@ -181,18 +183,18 @@ impl Differ {
opcode.first_end - opcode.first_start,
opcode.second_end - opcode.second_start,
);
match opcode.tag.as_ref() {
"replace" => {
match opcode.tag {
Tag::Replace => {
first_tag.push_str(&str_with_similar_chars('^', first_length));
second_tag.push_str(&str_with_similar_chars('^', second_length));
}
"delete" => {
Tag::Delete => {
first_tag.push_str(&str_with_similar_chars('-', first_length));
}
"insert" => {
Tag::Insert => {
second_tag.push_str(&str_with_similar_chars('+', second_length));
}
"equal" => {
Tag::Equal => {
first_tag.push_str(&str_with_similar_chars(' ', first_length));
second_tag.push_str(&str_with_similar_chars(' ', second_length));
}
Expand All @@ -217,8 +219,9 @@ impl Differ {
second_sequence,
best_j + 1,
second_end,
).iter()
.cloned(),
)
.iter()
.cloned(),
);
res
}
Expand Down
26 changes: 13 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod differ;
pub mod sequencematcher;
mod utils;

use sequencematcher::{Sequence, SequenceMatcher};
use sequencematcher::{Sequence, SequenceMatcher, Tag};
use std::collections::HashMap;
use std::fmt::Display;
use utils::{format_range_context, format_range_unified};
Expand Down Expand Up @@ -59,7 +59,7 @@ pub fn unified_diff<T: Sequence + Display>(
file1_range, file2_range, lineterm
));
for code in group {
if code.tag == "equal" {
if code.tag == Tag::Equal {
for item in first_sequence
.iter()
.take(code.first_end)
Expand All @@ -69,7 +69,7 @@ pub fn unified_diff<T: Sequence + Display>(
}
continue;
}
if code.tag == "replace" || code.tag == "delete" {
if code.tag == Tag::Replace || code.tag == Tag::Delete {
for item in first_sequence
.iter()
.take(code.first_end)
Expand All @@ -78,7 +78,7 @@ pub fn unified_diff<T: Sequence + Display>(
res.push(format!("-{}", item));
}
}
if code.tag == "replace" || code.tag == "insert" {
if code.tag == Tag::Replace || code.tag == Tag::Insert {
for item in second_sequence
.iter()
.take(code.second_end)
Expand All @@ -103,11 +103,11 @@ pub fn context_diff<T: Sequence + Display>(
) -> Vec<String> {
let mut res = Vec::new();
let lineterm = '\n';
let mut prefix: HashMap<String, String> = HashMap::new();
prefix.insert(String::from("insert"), String::from("+ "));
prefix.insert(String::from("delete"), String::from("- "));
prefix.insert(String::from("replace"), String::from("! "));
prefix.insert(String::from("equal"), String::from(" "));
let mut prefix: HashMap<Tag, String> = HashMap::new();
prefix.insert(Tag::Insert, String::from("+ "));
prefix.insert(Tag::Delete, String::from("- "));
prefix.insert(Tag::Replace, String::from("! "));
prefix.insert(Tag::Equal, String::from(" "));
let mut started = false;
let mut matcher = SequenceMatcher::new(first_sequence, second_sequence);
for group in &matcher.get_grouped_opcodes(n) {
Expand All @@ -124,14 +124,14 @@ pub fn context_diff<T: Sequence + Display>(
res.push(format!("*** {} ****{}", file1_range, lineterm));
let mut any = false;
for opcode in group {
if opcode.tag == "replace" || opcode.tag == "delete" {
if opcode.tag == Tag::Replace || opcode.tag == Tag::Delete {
any = true;
break;
}
}
if any {
for opcode in group {
if opcode.tag != "insert" {
if opcode.tag != Tag::Insert {
for item in first_sequence
.iter()
.take(opcode.first_end)
Expand All @@ -146,14 +146,14 @@ pub fn context_diff<T: Sequence + Display>(
res.push(format!("--- {} ----{}", file2_range, lineterm));
any = false;
for opcode in group {
if opcode.tag == "replace" || opcode.tag == "insert" {
if opcode.tag == Tag::Replace || opcode.tag == Tag::Insert {
any = true;
break;
}
}
if any {
for opcode in group {
if opcode.tag != "delete" {
if opcode.tag != Tag::Delete {
for item in second_sequence
.iter()
.take(opcode.second_end)
Expand Down
Loading