Struct similar::TextDiff

source ·
pub struct TextDiff<'old, 'new, 'bufs, T: DiffableStr + ?Sized> { /* private fields */ }
Expand description

Captures diff op codes for textual diffs.

The exact diff behavior is depending on the underlying DiffableStr. For instance diffs on bytes and strings are slightly different. You can create a text diff from constructors such as TextDiff::from_lines or the TextDiffConfig created by TextDiff::configure.

Requires the text feature.

Implementations§

source§

impl<'old, 'new, 'bufs> TextDiff<'old, 'new, 'bufs, str>

source

pub fn configure() -> TextDiffConfig

Configures a text differ before diffing.

source

pub fn from_lines<T: DiffableStrRef + ?Sized>( old: &'old T, new: &'new T ) -> TextDiff<'old, 'new, 'bufs, T::Output>

Creates a diff of lines.

For more information see TextDiffConfig::diff_lines.

source

pub fn from_words<T: DiffableStrRef + ?Sized>( old: &'old T, new: &'new T ) -> TextDiff<'old, 'new, 'bufs, T::Output>

Creates a diff of words.

For more information see TextDiffConfig::diff_words.

source

pub fn from_chars<T: DiffableStrRef + ?Sized>( old: &'old T, new: &'new T ) -> TextDiff<'old, 'new, 'bufs, T::Output>

Creates a diff of chars.

For more information see TextDiffConfig::diff_chars.

source

pub fn from_unicode_words<T: DiffableStrRef + ?Sized>( old: &'old T, new: &'new T ) -> TextDiff<'old, 'new, 'bufs, T::Output>

Creates a diff of unicode words.

For more information see TextDiffConfig::diff_unicode_words.

This requires the unicode feature.

source

pub fn from_graphemes<T: DiffableStrRef + ?Sized>( old: &'old T, new: &'new T ) -> TextDiff<'old, 'new, 'bufs, T::Output>

Creates a diff of graphemes.

For more information see TextDiffConfig::diff_graphemes.

This requires the unicode feature.

source§

impl<'old, 'new, 'bufs, T: DiffableStr + ?Sized + 'old + 'new> TextDiff<'old, 'new, 'bufs, T>

source

pub fn from_slices( old: &'bufs [&'old T], new: &'bufs [&'new T] ) -> TextDiff<'old, 'new, 'bufs, T>

Creates a diff of arbitrary slices.

For more information see TextDiffConfig::diff_slices.

source

pub fn algorithm(&self) -> Algorithm

The name of the algorithm that created the diff.

source

pub fn newline_terminated(&self) -> bool

Returns true if items in the slice are newline terminated.

This flag is used by the unified diff writer to determine if extra newlines have to be added.

source

pub fn old_slices(&self) -> &[&'old T]

Returns all old slices.

source

pub fn new_slices(&self) -> &[&'new T]

Returns all new slices.

source

pub fn ratio(&self) -> f32

Return a measure of the sequences’ similarity in the range 0..=1.

A ratio of 1.0 means the two sequences are a complete match, a ratio of 0.0 would indicate completely distinct sequences.

let diff = TextDiff::from_chars("abcd", "bcde");
assert_eq!(diff.ratio(), 0.75);
source

pub fn iter_changes<'x, 'slf>( &'slf self, op: &DiffOp ) -> ChangesIter<'slf, [&'x T], [&'x T], &'x T>
where 'x: 'slf, 'old: 'x, 'new: 'x,

Iterates over the changes the op expands to.

This method is a convenient way to automatically resolve the different ways in which a change could be encoded (insert/delete vs replace), look up the value from the appropriate slice and also handle correct index handling.

source

pub fn ops(&self) -> &[DiffOp]

Returns the captured diff ops.

source

pub fn grouped_ops(&self, n: usize) -> Vec<Vec<DiffOp>>

Isolate change clusters by eliminating ranges with no changes.

This is equivalent to calling group_diff_ops on TextDiff::ops.

source

pub fn iter_all_changes<'x, 'slf>(&'slf self) -> AllChangesIter<'slf, 'x, T>
where 'x: 'slf + 'old + 'new, 'old: 'x, 'new: 'x,

Flattens out the diff into all changes.

This is a shortcut for combining TextDiff::ops with TextDiff::iter_changes.

source

pub fn unified_diff<'diff>( &'diff self ) -> UnifiedDiff<'diff, 'old, 'new, 'bufs, T>

Utility to return a unified diff formatter.

source

pub fn iter_inline_changes<'slf>( &'slf self, op: &DiffOp ) -> impl Iterator<Item = InlineChange<'slf, T>> + '_
where 'slf: 'old + 'new,

Iterates over the changes the op expands to with inline emphasis.

This is very similar to TextDiff::iter_changes but it performs a second level diff on adjacent line replacements. The exact behavior of this function with regards to how it detects those inline changes is currently not defined and will likely change over time.

This method has a hardcoded 500ms deadline which is often not ideal. For fine tuning use iter_inline_changes_deadline.

As of similar 1.2.0 the behavior of this function changes depending on if the unicode feature is enabled or not. It will prefer unicode word splitting over word splitting depending on the feature flag.

Requires the inline feature.

source

pub fn iter_inline_changes_deadline<'slf>( &'slf self, op: &DiffOp, deadline: Option<Instant> ) -> impl Iterator<Item = InlineChange<'slf, T>> + '_
where 'slf: 'old + 'new,

Iterates over the changes the op expands to with inline emphasis with a deadline.

Like iter_inline_changes but with an explicit deadline.

Auto Trait Implementations§

§

impl<'old, 'new, 'bufs, T: ?Sized> Freeze for TextDiff<'old, 'new, 'bufs, T>

§

impl<'old, 'new, 'bufs, T: ?Sized> RefUnwindSafe for TextDiff<'old, 'new, 'bufs, T>
where T: RefUnwindSafe,

§

impl<'old, 'new, 'bufs, T: ?Sized> Send for TextDiff<'old, 'new, 'bufs, T>
where T: Sync,

§

impl<'old, 'new, 'bufs, T: ?Sized> Sync for TextDiff<'old, 'new, 'bufs, T>
where T: Sync,

§

impl<'old, 'new, 'bufs, T: ?Sized> Unpin for TextDiff<'old, 'new, 'bufs, T>

§

impl<'old, 'new, 'bufs, T: ?Sized> UnwindSafe for TextDiff<'old, 'new, 'bufs, T>
where T: RefUnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.