From c02f84a4768a4ab00013ee5f780660af4bfc3e23 Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Sun, 24 Nov 2024 15:52:21 +0000 Subject: [PATCH] Use crate utils --- backend/reconcile/src/diffs/mod.rs | 1 - backend/reconcile/src/diffs/myers.rs | 3 +- backend/reconcile/src/diffs/utils.rs | 86 ---------------------------- 3 files changed, 2 insertions(+), 88 deletions(-) delete mode 100644 backend/reconcile/src/diffs/utils.rs diff --git a/backend/reconcile/src/diffs/mod.rs b/backend/reconcile/src/diffs/mod.rs index 64b0536..b57139a 100644 --- a/backend/reconcile/src/diffs/mod.rs +++ b/backend/reconcile/src/diffs/mod.rs @@ -1,3 +1,2 @@ pub mod myers; pub mod raw_operation; -mod utils; diff --git a/backend/reconcile/src/diffs/myers.rs b/backend/reconcile/src/diffs/myers.rs index d970030..19043dd 100644 --- a/backend/reconcile/src/diffs/myers.rs +++ b/backend/reconcile/src/diffs/myers.rs @@ -25,9 +25,10 @@ use std::time::Instant; use std::vec; use crate::tokenizer::token::Token; +use crate::utils::common_prefix_len::common_prefix_len; +use crate::utils::common_suffix_len::common_suffix_len; use super::raw_operation::RawOperation; -use super::utils::{common_prefix_len, common_suffix_len}; /// Myers' diff algorithm. /// diff --git a/backend/reconcile/src/diffs/utils.rs b/backend/reconcile/src/diffs/utils.rs deleted file mode 100644 index a125824..0000000 --- a/backend/reconcile/src/diffs/utils.rs +++ /dev/null @@ -1,86 +0,0 @@ -use std::ops::{Index, Range}; - -/// Given two lookups and ranges calculates the length of the common prefix. -/// Copied from https://github.com/mitsuhiko/similar/blob/7e15c44de11a1cd61e1149189929e189ef977fd8/src/algorithms/utils.rs -pub fn common_prefix_len( - old: &Old, - old_range: Range, - new: &New, - new_range: Range, -) -> usize -where - Old: Index + ?Sized, - New: Index + ?Sized, - New::Output: PartialEq, -{ - new_range - .zip(old_range) - .take_while(|x| new[x.0] == old[x.1]) - .count() -} - -/// Given two lookups and ranges calculates the length of common suffix. -/// Copied from https://github.com/mitsuhiko/similar/blob/7e15c44de11a1cd61e1149189929e189ef977fd8/src/algorithms/utils.rs -pub fn common_suffix_len( - old: &Old, - old_range: Range, - new: &New, - new_range: Range, -) -> usize -where - Old: Index + ?Sized, - New: Index + ?Sized, - New::Output: PartialEq, -{ - new_range - .rev() - .zip(old_range.rev()) - .take_while(|x| new[x.0] == old[x.1]) - .count() -} - -#[cfg(test)] -mod tests { - use super::*; - use pretty_assertions::assert_eq; - - #[test] - fn test_common_prefix_len() { - assert_eq!( - common_prefix_len("".as_bytes(), 0..0, "".as_bytes(), 0..0), - 0 - ); - assert_eq!( - common_prefix_len("foobarbaz".as_bytes(), 0..9, "foobarblah".as_bytes(), 0..10), - 7 - ); - assert_eq!( - common_prefix_len("foobarbaz".as_bytes(), 0..9, "blablabla".as_bytes(), 0..9), - 0 - ); - assert_eq!( - common_prefix_len("foobarbaz".as_bytes(), 3..9, "foobarblah".as_bytes(), 3..10), - 4 - ); - } - - #[test] - fn test_common_suffix_len() { - assert_eq!( - common_suffix_len("".as_bytes(), 0..0, "".as_bytes(), 0..0), - 0 - ); - assert_eq!( - common_suffix_len("1234".as_bytes(), 0..4, "X0001234".as_bytes(), 0..8), - 4 - ); - assert_eq!( - common_suffix_len("1234".as_bytes(), 0..4, "Xxxx".as_bytes(), 0..4), - 0 - ); - assert_eq!( - common_suffix_len("1234".as_bytes(), 2..4, "01234".as_bytes(), 2..5), - 2 - ); - } -}