use core::ops::{Index, Range}; /// Given two lookups and ranges calculates the length of the common prefix. /// Copied from 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() } #[cfg(test)] mod tests { use pretty_assertions::assert_eq; use super::*; #[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 ); } }