my fork of dmp
-rw-r--r--src/dmp.rs9
-rw-r--r--tests/test.rs9
2 files changed, 14 insertions, 4 deletions
diff --git a/src/dmp.rs b/src/dmp.rs
index 6ffda77..8b0bb90 100644
--- a/src/dmp.rs
+++ b/src/dmp.rs
@@ -2350,12 +2350,12 @@ impl DiffMatchPatch {
// of the previous patch. If there are patches expected at positions 10 and
// 20, but the first patch was found at 12, delta is 2 and the second patch
// has an effective expected position of 22.
- let mut delta = 0;
+ let mut delta = 0_isize;
let mut results = vec![false; patches.len()];
// patches.iter().enumerate().for_each(|(x, p)| {
for (x, p) in patches.iter().enumerate() {
- let expected_loc = p.start2 + delta;
+ let expected_loc = p.start2 + delta as usize;
let txt_old = Self::diff_text_old(&p.diffs);
let (start_loc, end_loc) = if txt_old.len() > self.match_max_bits() {
// patch_splitMax will only provide an oversized pattern in the case of
@@ -2385,7 +2385,8 @@ impl DiffMatchPatch {
if let Some(sl) = start_loc {
// Found a match. :)
results[x] = true;
- delta = sl - expected_loc;
+ println!("{sl} {expected_loc}");
+ delta = (sl - expected_loc) as isize;
let txt_new = if let Some(el) = end_loc {
// safeMid(text, start_loc, end_loc + Match_MaxBits - start_loc);
@@ -2442,7 +2443,7 @@ impl DiffMatchPatch {
// No match found. :(
results[x] = false;
// Subtract the delta for this failed patch from subsequent patches.
- delta -= p.length2 - p.length1;
+ delta -= (p.length2 - p.length1) as isize;
}
}
diff --git a/tests/test.rs b/tests/test.rs
index 286200e..db06241 100644
--- a/tests/test.rs
+++ b/tests/test.rs
@@ -1298,6 +1298,15 @@ fn test_patch_apply() -> Result<(), Error> {
dmp.patch_apply(&patches, "x")?
);
+ let dmp = DiffMatchPatch::new();
+ // Tests for issue https://github.com/AnubhabB/diff-match-patch-rs/issues/2
+ let strp = "@@ -1,11 +1,5 @@\n-%F0%9F%8D%8A, a\n+A\n ah o\n@@ -3,17 +3,21 @@\n h orange\n- \n+!%F0%9F%8C%8A\n is the n\n@@ -23,10 +23,8 @@\n new \n-black!\n+%F0%9F%8C%8A\n";
+ let patches = dmp.patch_from_text::<Compat>(strp)?;
+ println!("{patches:?}");
+ assert_eq!(strp, dmp.patch_to_text(&patches));
+ let (patched, _) = dmp.patch_apply(&patches, "🍊, aah orange is the new black!")?;
+ assert_eq!(patched, "Aah orange!🌊is the new 🌊");
+
Ok(())
}