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

Creates a new remapper from strings and slices.

Creates a new remapper from a text diff and the original strings.

Slices into the old string.

Slices into the new string.

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.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.