Struct similar::TextDiffConfig
source · pub struct TextDiffConfig { /* private fields */ }
Expand description
A builder type config for more complex uses of TextDiff
.
Requires the text
feature.
Implementations§
source§impl TextDiffConfig
impl TextDiffConfig
sourcepub fn algorithm(&mut self, alg: Algorithm) -> &mut Self
pub fn algorithm(&mut self, alg: Algorithm) -> &mut Self
Changes the algorithm.
The default algorithm is Algorithm::Myers
.
sourcepub fn deadline(&mut self, deadline: Instant) -> &mut Self
pub fn deadline(&mut self, deadline: Instant) -> &mut Self
Sets a deadline for the diff operation.
By default a diff will take as long as it takes. For certain diff algorithms like Myer’s and Patience a maximum running time can be defined after which the algorithm gives up and approximates.
sourcepub fn timeout(&mut self, timeout: Duration) -> &mut Self
pub fn timeout(&mut self, timeout: Duration) -> &mut Self
Sets a timeout for thediff operation.
This is like deadline
but accepts a duration.
sourcepub fn newline_terminated(&mut self, yes: bool) -> &mut Self
pub fn newline_terminated(&mut self, yes: bool) -> &mut Self
Changes the newline termination flag.
The default is automatic based on input. This flag controls the
behavior of TextDiff::iter_changes
and unified diff generation
with regards to newlines. When the flag is set to false
(which
is the default) then newlines are added. Otherwise the newlines
from the source sequences are reused.
sourcepub fn diff_lines<'old, 'new, 'bufs, T: DiffableStrRef + ?Sized>(
&self,
old: &'old T,
new: &'new T,
) -> TextDiff<'old, 'new, 'bufs, T::Output>
pub fn diff_lines<'old, 'new, 'bufs, T: DiffableStrRef + ?Sized>( &self, old: &'old T, new: &'new T, ) -> TextDiff<'old, 'new, 'bufs, T::Output>
Creates a diff of lines.
This splits the text old
and new
into lines preserving newlines
in the input. Line diffs are very common and because of that enjoy
special handling in similar. When a line diff is created with this
method the newline_terminated
flag is flipped to true
and will
influence the behavior of unified diff generation.
use similar::{TextDiff, ChangeTag};
let diff = TextDiff::configure().diff_lines("a\nb\nc", "a\nb\nC");
let changes: Vec<_> = diff
.iter_all_changes()
.map(|x| (x.tag(), x.value()))
.collect();
assert_eq!(changes, vec![
(ChangeTag::Equal, "a\n"),
(ChangeTag::Equal, "b\n"),
(ChangeTag::Delete, "c"),
(ChangeTag::Insert, "C"),
]);
sourcepub fn diff_words<'old, 'new, 'bufs, T: DiffableStrRef + ?Sized>(
&self,
old: &'old T,
new: &'new T,
) -> TextDiff<'old, 'new, 'bufs, T::Output>
pub fn diff_words<'old, 'new, 'bufs, T: DiffableStrRef + ?Sized>( &self, old: &'old T, new: &'new T, ) -> TextDiff<'old, 'new, 'bufs, T::Output>
Creates a diff of words.
This splits the text into words and whitespace.
Note on word diffs: because the text differ will tokenize the strings
into small segments it can be inconvenient to work with the results
depending on the use case. You might also want to combine word level
diffs with the TextDiffRemapper
which lets you remap the diffs back to the original input strings.
use similar::{TextDiff, ChangeTag};
let diff = TextDiff::configure().diff_words("foo bar baz", "foo BAR baz");
let changes: Vec<_> = diff
.iter_all_changes()
.map(|x| (x.tag(), x.value()))
.collect();
assert_eq!(changes, vec![
(ChangeTag::Equal, "foo"),
(ChangeTag::Equal, " "),
(ChangeTag::Delete, "bar"),
(ChangeTag::Insert, "BAR"),
(ChangeTag::Equal, " "),
(ChangeTag::Equal, "baz"),
]);
sourcepub fn diff_chars<'old, 'new, 'bufs, T: DiffableStrRef + ?Sized>(
&self,
old: &'old T,
new: &'new T,
) -> TextDiff<'old, 'new, 'bufs, T::Output>
pub fn diff_chars<'old, 'new, 'bufs, T: DiffableStrRef + ?Sized>( &self, old: &'old T, new: &'new T, ) -> TextDiff<'old, 'new, 'bufs, T::Output>
Creates a diff of characters.
Note on character diffs: because the text differ will tokenize the strings
into small segments it can be inconvenient to work with the results
depending on the use case. You might also want to combine word level
diffs with the TextDiffRemapper
which lets you remap the diffs back to the original input strings.
use similar::{TextDiff, ChangeTag};
let diff = TextDiff::configure().diff_chars("abcdef", "abcDDf");
let changes: Vec<_> = diff
.iter_all_changes()
.map(|x| (x.tag(), x.value()))
.collect();
assert_eq!(changes, vec![
(ChangeTag::Equal, "a"),
(ChangeTag::Equal, "b"),
(ChangeTag::Equal, "c"),
(ChangeTag::Delete, "d"),
(ChangeTag::Delete, "e"),
(ChangeTag::Insert, "D"),
(ChangeTag::Insert, "D"),
(ChangeTag::Equal, "f"),
]);
sourcepub fn diff_unicode_words<'old, 'new, 'bufs, T: DiffableStrRef + ?Sized>(
&self,
old: &'old T,
new: &'new T,
) -> TextDiff<'old, 'new, 'bufs, T::Output>
pub fn diff_unicode_words<'old, 'new, 'bufs, T: DiffableStrRef + ?Sized>( &self, old: &'old T, new: &'new T, ) -> TextDiff<'old, 'new, 'bufs, T::Output>
Creates a diff of unicode words.
This splits the text into words according to unicode rules. This is
generally recommended over TextDiffConfig::diff_words
but
requires a dependency.
This requires the unicode
feature.
Note on word diffs: because the text differ will tokenize the strings
into small segments it can be inconvenient to work with the results
depending on the use case. You might also want to combine word level
diffs with the TextDiffRemapper
which lets you remap the diffs back to the original input strings.
use similar::{TextDiff, ChangeTag};
let diff = TextDiff::configure().diff_unicode_words("ah(be)ce", "ah(ah)ce");
let changes: Vec<_> = diff
.iter_all_changes()
.map(|x| (x.tag(), x.value()))
.collect();
assert_eq!(changes, vec![
(ChangeTag::Equal, "ah"),
(ChangeTag::Equal, "("),
(ChangeTag::Delete, "be"),
(ChangeTag::Insert, "ah"),
(ChangeTag::Equal, ")"),
(ChangeTag::Equal, "ce"),
]);
sourcepub fn diff_graphemes<'old, 'new, 'bufs, T: DiffableStrRef + ?Sized>(
&self,
old: &'old T,
new: &'new T,
) -> TextDiff<'old, 'new, 'bufs, T::Output>
pub fn diff_graphemes<'old, 'new, 'bufs, T: DiffableStrRef + ?Sized>( &self, old: &'old T, new: &'new T, ) -> TextDiff<'old, 'new, 'bufs, T::Output>
Creates a diff of graphemes.
This requires the unicode
feature.
Note on grapheme diffs: because the text differ will tokenize the strings
into small segments it can be inconvenient to work with the results
depending on the use case. You might also want to combine word level
diffs with the TextDiffRemapper
which lets you remap the diffs back to the original input strings.
use similar::{TextDiff, ChangeTag};
let diff = TextDiff::configure().diff_graphemes("💩🇦🇹🦠", "💩🇦🇱❄️");
let changes: Vec<_> = diff
.iter_all_changes()
.map(|x| (x.tag(), x.value()))
.collect();
assert_eq!(changes, vec![
(ChangeTag::Equal, "💩"),
(ChangeTag::Delete, "🇦🇹"),
(ChangeTag::Delete, "🦠"),
(ChangeTag::Insert, "🇦🇱"),
(ChangeTag::Insert, "❄️"),
]);
sourcepub fn diff_slices<'old, 'new, 'bufs, T: DiffableStr + ?Sized>(
&self,
old: &'bufs [&'old T],
new: &'bufs [&'new T],
) -> TextDiff<'old, 'new, 'bufs, T>
pub fn diff_slices<'old, 'new, 'bufs, T: DiffableStr + ?Sized>( &self, old: &'bufs [&'old T], new: &'bufs [&'new T], ) -> TextDiff<'old, 'new, 'bufs, T>
Creates a diff of arbitrary slices.
use similar::{TextDiff, ChangeTag};
let old = &["foo", "bar", "baz"];
let new = &["foo", "BAR", "baz"];
let diff = TextDiff::configure().diff_slices(old, new);
let changes: Vec<_> = diff
.iter_all_changes()
.map(|x| (x.tag(), x.value()))
.collect();
assert_eq!(changes, vec![
(ChangeTag::Equal, "foo"),
(ChangeTag::Delete, "bar"),
(ChangeTag::Insert, "BAR"),
(ChangeTag::Equal, "baz"),
]);
Trait Implementations§
source§impl Clone for TextDiffConfig
impl Clone for TextDiffConfig
source§fn clone(&self) -> TextDiffConfig
fn clone(&self) -> TextDiffConfig
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for TextDiffConfig
impl Debug for TextDiffConfig
source§impl Default for TextDiffConfig
impl Default for TextDiffConfig
source§fn default() -> TextDiffConfig
fn default() -> TextDiffConfig
Auto Trait Implementations§
impl Freeze for TextDiffConfig
impl RefUnwindSafe for TextDiffConfig
impl Send for TextDiffConfig
impl Sync for TextDiffConfig
impl Unpin for TextDiffConfig
impl UnwindSafe for TextDiffConfig
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)