Function similar::utils::diff_graphemes

source ยท
pub fn diff_graphemes<'x, T: DiffableStrRef + ?Sized>(
    alg: Algorithm,
    old: &'x T,
    new: &'x T,
) -> Vec<(ChangeTag, &'x T::Output)>
Expand description

Shortcut for making a grapheme level diff.

This function produces the diff of two strings and returns a vector with the changes. It returns connected slices into the original string rather than grapheme level slices.

use similar::{Algorithm, ChangeTag};
use similar::utils::diff_graphemes;

let old = "The flag of Austria is ๐Ÿ‡ฆ๐Ÿ‡น";
let new = "The flag of Albania is ๐Ÿ‡ฆ๐Ÿ‡ฑ";
assert_eq!(diff_graphemes(Algorithm::Myers, old, new), vec![
    (ChangeTag::Equal, "The flag of A"),
    (ChangeTag::Delete, "ustr"),
    (ChangeTag::Insert, "lban"),
    (ChangeTag::Equal, "ia is "),
    (ChangeTag::Delete, "๐Ÿ‡ฆ๐Ÿ‡น"),
    (ChangeTag::Insert, "๐Ÿ‡ฆ๐Ÿ‡ฑ"),
]);

This requires the unicode feature.