my fork of dmp
Closes #2
Anubhab Bandyopadhyay 2024-09-02
parent 8d078a2 · commit dc2396d
-rw-r--r--src/dmp.rs14
-rw-r--r--src/html.rs2
-rw-r--r--src/lib.rs8
-rw-r--r--tests/test.rs1
4 files changed, 12 insertions, 13 deletions
diff --git a/src/dmp.rs b/src/dmp.rs
index 8b0bb90..c27d061 100644
--- a/src/dmp.rs
+++ b/src/dmp.rs
@@ -2355,7 +2355,7 @@ impl DiffMatchPatch {
// patches.iter().enumerate().for_each(|(x, p)| {
for (x, p) in patches.iter().enumerate() {
- let expected_loc = p.start2 + delta as usize;
+ let expected_loc = (p.start2 as isize + 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,16 +2385,16 @@ impl DiffMatchPatch {
if let Some(sl) = start_loc {
// Found a match. :)
results[x] = true;
- println!("{sl} {expected_loc}");
- delta = (sl - expected_loc) as isize;
+ delta = sl as isize - expected_loc as isize;
- let txt_new = if let Some(el) = end_loc {
- // safeMid(text, start_loc, end_loc + Match_MaxBits - start_loc);
- &source[sl..el + self.match_max_bits()]
+ let trg_idx = if let Some(el) = end_loc {
+ el + self.match_max_bits()
} else {
- &source[sl..sl + txt_old.len()]
+ sl + txt_old.len()
};
+ let txt_new = &source[sl..trg_idx.min(source.len())];
+
if txt_old == txt_new {
// Perfect match, just shove the replacement text in.
source = [
diff --git a/src/html.rs b/src/html.rs
index 97b6c95..81d22a0 100644
--- a/src/html.rs
+++ b/src/html.rs
@@ -11,7 +11,7 @@
/// `insert_style`, `delete_style` and `equality_style` would add css style property to the output.
/// E.g. if `insert_style: Some("background: yellow; color: purple")` is set the
/// `insert` part of the pretty html would look like `<ins style="background: yellow; color: purple">insert text</ins>`
-///
+///
/// [`pretty_html`]: struct.DiffMatchPatch.html/method.diff_pretty_html
pub struct HtmlConfig<'a> {
insert_tag: &'a str,
diff --git a/src/lib.rs b/src/lib.rs
index 8d7e5af..f94f554 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,13 +1,13 @@
//! # Efficient port of Google's diff-match-patch implemented in Rust
-//!
+//!
//! [<img alt="github" src="https://img.shields.io/badge/github-Anubhab/diff_match_patch_rs-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="20">](https://github.com/AnubhabB/diff-match-patch-rs)
//! [<img alt="crates.io" src="https://img.shields.io/crates/v/diff-match-patch-rs" height="20">](https://crates.io/crates/diff-match-patch-rs)
//! [<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-diff_match_patch_rs?style=for-the-badge&logo=docs.rs&labelColor=%23555555" height="20">](https://docs.rs/diff-match-patch-rs)
-//!
-//!
+//!
+//!
//! A very **fast**, **accurate** and **wasm ready** port of [Diff Match Patch](https://github.com/dmsnell/diff-match-patch) in Rust. The
//! diff implementation is based on [Myers' diff algorithm](https://neil.fraser.name/writing/diff/myers.pdf).
-//!
+//!
//! ## Highlights of this crate
//! - Exposes two modes of operating with `diff-match-patch`, a `Efficient` mode and `Compat` mode. While the `Efficient` mode squeezes out the max performance the `Compat` mode ensures compatibility across other libraries or implementations (rust or otherwise). According to [Benchmarks](#benchmarks), our slower `Compat` mode is still faster than other implementations in rust.
//! - **`Efficient`** mode works on `&[u8]` and the generated diffs break compatibility with other implementation. Use the **`Efficient`** mode ONLY if you are using this [crate](https://crates.io/crates/diff-match-patch-rs) at the source of diff generation and the destination.
diff --git a/tests/test.rs b/tests/test.rs
index db06241..c61cc80 100644
--- a/tests/test.rs
+++ b/tests/test.rs
@@ -1302,7 +1302,6 @@ fn test_patch_apply() -> Result<(), Error> {
// 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 🌊");