Struct similar::utils::TextDiffRemapper
source · pub struct TextDiffRemapper<'x, T: ?Sized> { /* private fields */ }
Expand description
A remapper that can remap diff ops to the original slices.
The idea here is that when a TextDiff
is created from
two strings and the internal tokenization is used, this remapper can take
a range in the tokenized sequences and remap it to the original string.
This is particularly useful when you want to do things like character or
grapheme level diffs but you want to not have to iterate over small sequences
but large consequitive ones from the source.
use similar::{ChangeTag, TextDiff};
use similar::utils::TextDiffRemapper;
let old = "yo! foo bar baz";
let new = "yo! foo bor baz";
let diff = TextDiff::from_words(old, new);
let remapper = TextDiffRemapper::from_text_diff(&diff, old, new);
let changes: Vec<_> = diff.ops()
.iter()
.flat_map(move |x| remapper.iter_slices(x))
.collect();
assert_eq!(changes, vec![
(ChangeTag::Equal, "yo! foo "),
(ChangeTag::Delete, "bar"),
(ChangeTag::Insert, "bor"),
(ChangeTag::Equal, " baz")
]);
Implementations§
source§impl<'x, T: DiffableStr + ?Sized> TextDiffRemapper<'x, T>
impl<'x, T: DiffableStr + ?Sized> TextDiffRemapper<'x, T>
sourcepub fn new(
old_slices: &[&'x T],
new_slices: &[&'x T],
old: &'x T,
new: &'x T
) -> TextDiffRemapper<'x, T>
pub fn new( old_slices: &[&'x T], new_slices: &[&'x T], old: &'x T, new: &'x T ) -> TextDiffRemapper<'x, T>
Creates a new remapper from strings and slices.
sourcepub fn from_text_diff<'old, 'new, 'bufs>(
diff: &TextDiff<'old, 'new, 'bufs, T>,
old: &'x T,
new: &'x T
) -> TextDiffRemapper<'x, T>where
'old: 'x,
'new: 'x,
pub fn from_text_diff<'old, 'new, 'bufs>(
diff: &TextDiff<'old, 'new, 'bufs, T>,
old: &'x T,
new: &'x T
) -> TextDiffRemapper<'x, T>where
'old: 'x,
'new: 'x,
Creates a new remapper from a text diff and the original strings.
sourcepub fn iter_slices(
&self,
op: &DiffOp
) -> impl Iterator<Item = (ChangeTag, &'x T)>
pub fn iter_slices( &self, op: &DiffOp ) -> impl Iterator<Item = (ChangeTag, &'x T)>
Given a diffop yields the changes it encodes against the original strings.
This is the same as the DiffOp::iter_slices
method.
§Panics
This method can panic if the input strings passed to the constructor are incompatible with the input strings passed to the diffing algorithm.