Struct similar::utils::TextDiffRemapper[][src]

pub struct TextDiffRemapper<'x, T: ?Sized> { /* fields omitted */ }

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

impl<'x, 'slices, T: DiffableStr + ?Sized> TextDiffRemapper<'x, T>[src]

pub fn new(
    old_slices: &[&'x T],
    new_slices: &[&'x T],
    old: &'x T,
    new: &'x T
) -> TextDiffRemapper<'x, T>
[src]

Creates a new remapper from strings and slices.

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, 
[src]

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

pub fn slice_old(&self, range: Range<usize>) -> Option<&'x T>[src]

Slices into the old string.

pub fn slice_new(&self, range: Range<usize>) -> Option<&'x T>[src]

Slices into the new string.

pub fn iter_slices(
    &self,
    op: &DiffOp
) -> impl Iterator<Item = (ChangeTag, &'x T)>
[src]

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

impl<'x, T: ?Sized> RefUnwindSafe for TextDiffRemapper<'x, T> where
    T: RefUnwindSafe

impl<'x, T: ?Sized> Send for TextDiffRemapper<'x, T> where
    T: Sync

impl<'x, T: ?Sized> Sync for TextDiffRemapper<'x, T> where
    T: Sync

impl<'x, T: ?Sized> Unpin for TextDiffRemapper<'x, T>

impl<'x, T: ?Sized> UnwindSafe for TextDiffRemapper<'x, T> where
    T: RefUnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.