my fork of dmp
Reducing dependency burden
Anubhab Bandyopadhyay 2024-09-02
parent dc2396d · commit 521e6e4
-rw-r--r--Cargo.toml14
-rw-r--r--src/dmp.rs34
-rw-r--r--src/traits.rs11
-rw-r--r--tests/compat.rs2
-rw-r--r--tests/test.rs14
5 files changed, 50 insertions, 25 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 5ab495c..e1249a1 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "diff-match-patch-rs"
-version = "0.1.0-beta.4"
+version = "0.2.0"
edition = "2021"
authors = ["Anubhab Bandyopadhyay"]
repository = "https://github.com/AnubhabB/diff-match-patch-rs.git"
@@ -11,11 +11,19 @@ keywords = ["diff", "match", "patch", "text-synchronization"]
categories = ["algorithms", "text-processing", "text-editors", "wasm"]
[dependencies]
-chrono = "0"
percent-encoding = "2"
+[target_arch.'cfg(wasm32-unknown-unknown)'.dependencies]
+chrono = "0"
+
[package.metadata.docs.rs]
-targets = ["x86_64-unknown-linux-gnu", "wasm32-unknown-unknown"]
+targets = [
+ "aarch64-unknown-linux-gnu",
+ "aarch64-apple-darwin",
+ "x86_64-unknown-linux-gnu",
+ "x86_64-apple-darwin",
+ "wasm32-unknown-unknown"
+]
rustdoc-args = ["--generate-link-to-definition"]
[[example]]
diff --git a/src/dmp.rs b/src/dmp.rs
index c27d061..fa07081 100644
--- a/src/dmp.rs
+++ b/src/dmp.rs
@@ -1,7 +1,15 @@
use core::str;
use std::{char, collections::HashMap, fmt::Display};
+#[cfg(target_arch = "wasm32")]
use chrono::{NaiveTime, TimeDelta, Utc};
+#[cfg(not(target_arch = "wasm32"))]
+use std::time::{Duration, Instant};
+
+#[cfg(target_arch = "wasm32")]
+pub(crate) type Time = NaiveTime;
+#[cfg(not(target_arch = "wasm32"))]
+pub(crate) type Time = Instant;
use crate::{errors::Error, html::HtmlConfig, DType, PatchInput};
@@ -166,12 +174,19 @@ impl DiffMatchPatch {
}
/// creates a deadline from the given timeout
- pub fn deadline(&self) -> Option<NaiveTime> {
+ #[cfg(target_arch = "wasm32")]
+ pub fn deadline(&self) -> Option<Time> {
self.timeout()
.and_then(|t| Utc::now().checked_add_signed(TimeDelta::milliseconds(t)))
.map(|t| t.time())
}
+ #[cfg(not(target_arch = "wasm32"))]
+ pub fn deadline(&self) -> Option<Time> {
+ self.timeout()
+ .and_then(|t| Instant::now().checked_add(Duration::from_millis(t as u64)))
+ }
+
// returns configured match_threshold
fn match_threshold(&self) -> f32 {
self.match_threshold
@@ -228,7 +243,7 @@ impl DiffMatchPatch {
old_bytes: &'a [T],
new_bytes: &'a [T],
linemode: bool,
- deadline: Option<NaiveTime>,
+ deadline: Option<Time>,
) -> Result<Vec<Diff<T>>, crate::errors::Error> {
// First, check if lhs and rhs are equal
if old_bytes == new_bytes {
@@ -286,7 +301,7 @@ impl DiffMatchPatch {
old: &'a [T],
new: &'a [T],
linemode: bool,
- deadline: Option<NaiveTime>,
+ deadline: Option<Time>,
) -> Result<Vec<Diff<T>>, crate::errors::Error> {
// returning all of the new part
if old.is_empty() {
@@ -432,7 +447,7 @@ impl DiffMatchPatch {
&self,
old: &'a [T],
new: &'a [T],
- deadline: Option<NaiveTime>,
+ deadline: Option<Time>,
) -> Result<Vec<Diff<T>>, crate::errors::Error> {
let mut diffs = {
let to_chars = Self::lines_to_chars(old, new);
@@ -512,7 +527,7 @@ impl DiffMatchPatch {
&self,
old: &'a [usize],
new: &'a [usize],
- deadline: Option<NaiveTime>,
+ deadline: Option<Time>,
) -> Result<Vec<Diff<usize>>, crate::errors::Error> {
if old == new {
if old.is_empty() {
@@ -557,7 +572,7 @@ impl DiffMatchPatch {
&self,
old: &'a [usize],
new: &'a [usize],
- deadline: Option<NaiveTime>,
+ deadline: Option<Time>,
) -> Result<Vec<Diff<usize>>, crate::errors::Error> {
// returning all of the new part
if old.is_empty() {
@@ -637,7 +652,7 @@ impl DiffMatchPatch {
&self,
old: &'a [T],
new: &'a [T],
- deadline: Option<NaiveTime>,
+ deadline: Option<Time>,
) -> Result<Vec<Diff<T>>, crate::errors::Error> {
// let text1_length = old.len() as isize;
// let text2_length = new.len() as isize;
@@ -669,9 +684,14 @@ impl DiffMatchPatch {
for d in 0..max_d {
// Bail out if deadline is reached.
if let Some(tout) = deadline {
+ #[cfg(target_arch = "wasm32")]
if Utc::now().time() > tout {
break;
}
+ #[cfg(not(target_arch = "wasm32"))]
+ if Instant::now() > tout {
+ break;
+ }
}
// Walk the front path one step.
diff --git a/src/traits.rs b/src/traits.rs
index f02c63c..90f9078 100644
--- a/src/traits.rs
+++ b/src/traits.rs
@@ -1,10 +1,9 @@
use std::hash::Hash;
-use chrono::NaiveTime;
use percent_encoding::{percent_decode, AsciiSet, CONTROLS};
use crate::{
- dmp::{Diff, DiffMatchPatch},
+ dmp::{Diff, DiffMatchPatch, Time},
Ops,
};
@@ -34,7 +33,7 @@ pub trait DType: Copy + Ord + Eq + Hash {
new: &[Self],
x: usize,
y: usize,
- deadline: Option<NaiveTime>,
+ deadline: Option<Time>,
) -> Result<Vec<Diff<Self>>, crate::errors::Error>;
fn from_char(c: char) -> Self;
@@ -60,7 +59,7 @@ impl DType for u8 {
new: &[u8],
x: usize,
y: usize,
- deadline: Option<NaiveTime>,
+ deadline: Option<Time>,
) -> Result<Vec<Diff<u8>>, crate::errors::Error> {
let old_a = &old[..x];
let new_a = &new[..y];
@@ -221,7 +220,7 @@ impl DType for char {
new: &[char],
x: usize,
y: usize,
- deadline: Option<NaiveTime>,
+ deadline: Option<Time>,
) -> Result<Vec<Diff<char>>, crate::errors::Error> {
let old_a = &old[..x];
let new_a = &new[..y];
@@ -302,7 +301,7 @@ impl DType for usize {
new: &[usize],
x: usize,
y: usize,
- deadline: Option<NaiveTime>,
+ deadline: Option<Time>,
) -> Result<Vec<Diff<usize>>, crate::errors::Error> {
let old_a = &old[..x];
let new_a = &new[..y];
diff --git a/tests/compat.rs b/tests/compat.rs
index 29018c2..5423913 100644
--- a/tests/compat.rs
+++ b/tests/compat.rs
@@ -1 +1 @@
-// Compatibility tests have been moved to `https://anubhabb/diff-match-path-rs-bench` repo \ No newline at end of file
+// Compatibility tests have been moved to `https://anubhabb/diff-match-path-rs-bench` repo
diff --git a/tests/test.rs b/tests/test.rs
index c61cc80..2bac015 100644
--- a/tests/test.rs
+++ b/tests/test.rs
@@ -1,7 +1,5 @@
use std::time::Instant;
-use chrono::Utc;
-
use diff_match_patch_rs::dmp::Diff;
use diff_match_patch_rs::html::HtmlConfig;
@@ -387,11 +385,11 @@ fn test_diff_main() -> Result<(), Error> {
let a = vec!["`Twas brillig, and the slithy toves\nDid gyre and gimble in the wabe:\nAll mimsy were the borogoves,\nAnd the mome raths outgrabe.\n"; 2048].join("");
let b = vec!["I am the very model of a modern major general,\nI\'ve information vegetable, animal, and mineral,\nI know the kings of England, and I quote the fights historical,\nFrom Marathon to Waterloo, in order categorical.\n"; 2048].join("");
- let start = Utc::now().time();
+ let start = Instant::now();
dmp.diff_main::<Efficient>(&a, &b)?;
- let end = Utc::now().time();
+ let end = Instant::now();
// Test that we took at least the timeout period (+ 5ms being generous).
- assert!((end - start).num_milliseconds() <= LOW_TIMEOUT as i64 + 5);
+ assert!((end - start).as_millis() <= LOW_TIMEOUT as u128 + 5);
// Test the linemode speedup.
// Must be long to pass the 100 char cutoff.
@@ -599,11 +597,11 @@ fn test_diff_main_compat() -> Result<(), Error> {
let a = vec!["`Twas brillig, and the slithy toves\nDid gyre and gimble in the wabe:\nAll mimsy were the borogoves,\nAnd the mome raths outgrabe.\n"; 2048].join("");
let b = vec!["I am the very model of a modern major general,\nI\'ve information vegetable, animal, and mineral,\nI know the kings of England, and I quote the fights historical,\nFrom Marathon to Waterloo, in order categorical.\n"; 2048].join("");
- let start = Utc::now().time();
+ let start = Instant::now();
dmp.diff_main::<Efficient>(&a, &b)?;
- let end = Utc::now().time();
+ let end = Instant::now();
// Test that we took at least the timeout period (+ 5ms being generous).
- assert!((end - start).num_milliseconds() <= LOW_TIMEOUT as i64 + 5);
+ assert!((end - start).as_millis() <= LOW_TIMEOUT as u128 + 5);
// Test the linemode speedup.
// Must be long to pass the 100 char cutoff.