From 7603aeb7088c81809c53ce69443e1e77131a0b8a Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 24 Jan 2021 11:43:15 +0100 Subject: [PATCH] Added ratio function to changelog and added empty test case --- CHANGELOG.md | 1 + src/text.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d02acd1..3a0e254 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to similar are documented here. * Added grapheme and character level diffing utilities. * `DiffOp::as_tag_tuple` is now taking the argument by reference. +* Added `TextDiff::ratio`. ## 0.2.0 diff --git a/src/text.rs b/src/text.rs index c0cdab1..627dadc 100644 --- a/src/text.rs +++ b/src/text.rs @@ -303,6 +303,27 @@ impl<'old, 'new, 'bufs> TextDiff<'old, 'new, 'bufs> { &self.new } + /// Return a measure of the sequences' similarity in the range `0..=1`. + pub fn ratio(&self) -> f32 { + let matches = self + .ops() + .iter() + .map(|op| { + if let DiffOp::Equal { len, .. } = *op { + len + } else { + 0 + } + }) + .sum::(); + let len = self.old.len() + self.new.len(); + if len == 0 { + 1.0 + } else { + 2.0 * matches as f32 / len as f32 + } + } + /// Iterates over the changes the op expands to. /// /// This method is a convenient way to automatically resolve the different @@ -639,3 +660,11 @@ fn test_char_diff() { let diff = TextDiff::from_chars("Hello World", "Hallo Welt"); insta::assert_debug_snapshot!(diff.ops()); } + +#[test] +fn test_ratio() { + let diff = TextDiff::from_chars("abcd", "bcde"); + assert_eq!(diff.ratio(), 0.75); + let diff = TextDiff::from_chars("", ""); + assert_eq!(diff.ratio(), 1.0); +}