my fork of dmp
Match APIs now work with `Efficient` and `Compat` modes
| -rw-r--r-- | CHANGELOG.md | 5 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | README.md | 21 | ||||
| -rw-r--r-- | src/dmp.rs | 25 | ||||
| -rw-r--r-- | src/lib.rs | 19 | ||||
| -rw-r--r-- | tests/test.rs | 38 |
6 files changed, 99 insertions, 11 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index dfb308e..22e155e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # CHANGELOG.md +## 0.3.0 +Breaking Change: + + - the `match_main` API now supports `Efficient` and `Compat` modes. The call to `match_main` is now `match_main::<Efficient>` or `match_main::<Compat>` depending on your use-case + ## 0.2.1 Fix: @@ -1,6 +1,6 @@ [package] name = "diff-match-patch-rs" -version = "0.2.1" +version = "0.3.0" edition = "2021" authors = ["Anubhab Bandyopadhyay"] homepage = "https://docs.rs/diff-match-patch-rs" @@ -145,6 +145,27 @@ fn main() -> Result<(), Error> { } ``` +### `Match` - fuzzy match of pattern in Text + +```rust +use diff_match_patch_rs::{DiffMatchPatch, Compat, Error, PatchInput}; + +// This is the source text +const TXT: &str = "I am the very model of a modern Major-General, I've information on vegetable, animal, and mineral, 🚀👏👀"; + +// The patter we are trying to fing +const PATTERN: &str = " that berry "; + +// Returns `location` of match if found, `None` if not found +fn main() -> Option<usize> { + let dmp = DiffMatchPatch::new(); + + // works with both `Efficient` and `Compat` modes + // `5` here is an approx location to find `nearby` matches + dmp.match_main::<Efficient>(TXT, PATTERN, 5) // this should return Some(4) +} +``` + #### Note The `Efficient` and `Compat` mode APIs are identical with the only chage being the `generic` parameter declared during the calls. @@ -2773,9 +2773,28 @@ impl DiffMatchPatch { /// If `match_threshold` is closer to 1 then it is more likely that a match will be found. /// The larger `match_threshold` is, the slower match_main() may take to compute. `match_threshold` defaults to 0.5 and can be updated by `dmp.set_match_threshold()` method. /// - /// If no match is found, the function returns -1. - pub fn match_main(&self, text: &str, pattern: &str, loc: usize) -> Option<usize> { - self.match_internal(text.as_bytes(), pattern.as_bytes(), loc) + /// If no match is found, the function returns `None` + /// + /// # Examle + /// ``` + /// # use diff_match_patch_rs::{DiffMatchPatch, Error, Efficient}; + /// + /// # fn main() { + /// let dmp = DiffMatchPatch::new(); + /// // Works with both `Compat` and `Efficient` modes + /// let matched = dmp.match_main::<Efficient>( + /// "I am the very model of a modern major general.", + /// " that berry ", + /// 5 + /// ); + /// + /// # assert_eq!(matched, Some(4)); + /// # } + /// ``` + pub fn match_main<T: DType>(&self, text: &str, pattern: &str, loc: usize) -> Option<usize> { + let text = T::from_str(text); + let pattern = T::from_str(pattern); + self.match_internal(&text, &pattern, loc) } /// Given two texts, or an already computed list of differences (`diffs`), return an array of patch objects. @@ -146,6 +146,25 @@ //! at_destination(&patches) //! } //! ``` +//! ### `Match` - fuzzy match of pattern in Text +//! +//! ```rust +//! use diff_match_patch_rs::{DiffMatchPatch, Compat, Error, PatchInput}; +//! // This is the source text +//! const TXT: &str = "I am the very model of a modern Major-General, I've information on vegetable, animal, and mineral, 🚀👏👀"; +//! +//! // The patter we are trying to fing +//! const PATTERN: &str = " that berry "; +//! +//! // Returns `location` of match if found, `None` if not found +//! fn main() -> Option<usize> { +//! let dmp = DiffMatchPatch::new(); +//! +//! // works with both `Efficient` and `Compat` modes +//! // `5` here is an approx location to find `nearby` matches +//! dmp.match_main::<Efficient>(TXT, PATTERN, 5) // this should return Some(4) +//! } +//! ``` //! //! #### Note //! The `Efficient` and `Compat` mode APIs are identical with the only chage being the `generic` parameter declared during the calls. diff --git a/tests/test.rs b/tests/test.rs index 2bac015..5e96a5f 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1312,21 +1312,45 @@ fn test_match_main() { let dmp = DiffMatchPatch::default(); // Full match. // Shortcut matches. - assert_eq!(Some(0), dmp.match_main("abcdef", "abcdef", 1000)); - assert_eq!(None, dmp.match_main("", "abcdef", 1)); - assert_eq!(Some(3), dmp.match_main("abcdef", "", 3)); - assert_eq!(Some(3), dmp.match_main("abcdef", "de", 3)); + assert_eq!( + Some(0), + dmp.match_main::<Efficient>("abcdef", "abcdef", 1000) + ); + assert_eq!(None, dmp.match_main::<Efficient>("", "abcdef", 1)); + assert_eq!(Some(3), dmp.match_main::<Efficient>("abcdef", "", 3)); + assert_eq!(Some(3), dmp.match_main::<Efficient>("abcdef", "de", 3)); + + // Beyond end match. + assert_eq!(Some(3), dmp.match_main::<Efficient>("abcdef", "defy", 4)); + + // Oversized pattern. + assert_eq!(Some(0), dmp.match_main::<Efficient>("abcdef", "abcdefy", 0)); + + // Complex match. + assert_eq!( + Some(4), + dmp.match_main::<Efficient>( + "I am the very model of a modern major general.", + " that berry ", + 5 + ) + ); + + assert_eq!(Some(0), dmp.match_main::<Compat>("abcdef", "abcdef", 1000)); + assert_eq!(None, dmp.match_main::<Compat>("", "abcdef", 1)); + assert_eq!(Some(3), dmp.match_main::<Compat>("abcdef", "", 3)); + assert_eq!(Some(3), dmp.match_main::<Compat>("abcdef", "de", 3)); // Beyond end match. - assert_eq!(Some(3), dmp.match_main("abcdef", "defy", 4)); + assert_eq!(Some(3), dmp.match_main::<Compat>("abcdef", "defy", 4)); // Oversized pattern. - assert_eq!(Some(0), dmp.match_main("abcdef", "abcdefy", 0)); + assert_eq!(Some(0), dmp.match_main::<Compat>("abcdef", "abcdefy", 0)); // Complex match. assert_eq!( Some(4), - dmp.match_main( + dmp.match_main::<Compat>( "I am the very model of a modern major general.", " that berry ", 5 |