similar::utils

Function 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.