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>
impl<'old, 'new, 'bufs> TextDiff<'old, 'new, 'bufs, str>
sourcepub fn configure() -> TextDiffConfig
pub fn configure() -> TextDiffConfig
Configures a text differ before diffing.
sourcepub fn from_lines<T: DiffableStrRef + ?Sized>(
old: &'old T,
new: &'new T
) -> TextDiff<'old, 'new, 'bufs, T::Output>
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
.
sourcepub fn from_words<T: DiffableStrRef + ?Sized>(
old: &'old T,
new: &'new T
) -> TextDiff<'old, 'new, 'bufs, T::Output>
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
.
sourcepub fn from_chars<T: DiffableStrRef + ?Sized>(
old: &'old T,
new: &'new T
) -> TextDiff<'old, 'new, 'bufs, T::Output>
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
.
sourcepub fn from_unicode_words<T: DiffableStrRef + ?Sized>(
old: &'old T,
new: &'new T
) -> TextDiff<'old, 'new, 'bufs, T::Output>
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.
sourcepub fn from_graphemes<T: DiffableStrRef + ?Sized>(
old: &'old T,
new: &'new T
) -> TextDiff<'old, 'new, 'bufs, T::Output>
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>
impl<'old, 'new, 'bufs, T: DiffableStr + ?Sized + 'old + 'new> TextDiff<'old, 'new, 'bufs, T>
sourcepub fn from_slices(
old: &'bufs [&'old T],
new: &'bufs [&'new T]
) -> TextDiff<'old, 'new, 'bufs, T>
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
.
sourcepub fn newline_terminated(&self) -> bool
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.
sourcepub fn old_slices(&self) -> &[&'old T]
pub fn old_slices(&self) -> &[&'old T]
Returns all old slices.
sourcepub fn new_slices(&self) -> &[&'new T]
pub fn new_slices(&self) -> &[&'new T]
Returns all new slices.
sourcepub fn ratio(&self) -> f32
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);
sourcepub fn iter_changes<'x, 'slf>(
&'slf self,
op: &DiffOp
) -> ChangesIter<'slf, [&'x T], [&'x T], &'x T> ⓘwhere
'x: 'slf,
'old: 'x,
'new: 'x,
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.
sourcepub fn grouped_ops(&self, n: usize) -> Vec<Vec<DiffOp>>
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
.
sourcepub fn iter_all_changes<'x, 'slf>(&'slf self) -> AllChangesIter<'slf, 'x, T> ⓘwhere
'x: 'slf + 'old + 'new,
'old: 'x,
'new: 'x,
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
.
sourcepub fn unified_diff<'diff>(
&'diff self
) -> UnifiedDiff<'diff, 'old, 'new, 'bufs, T>
pub fn unified_diff<'diff>( &'diff self ) -> UnifiedDiff<'diff, 'old, 'new, 'bufs, T>
Utility to return a unified diff formatter.
sourcepub fn iter_inline_changes<'slf>(
&'slf self,
op: &DiffOp
) -> impl Iterator<Item = InlineChange<'slf, T>> + '_where
'slf: 'old + 'new,
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.
sourcepub fn iter_inline_changes_deadline<'slf>(
&'slf self,
op: &DiffOp,
deadline: Option<Instant>
) -> impl Iterator<Item = InlineChange<'slf, T>> + '_where
'slf: 'old + 'new,
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.