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

Changes the algorithm.

The default algorithm is Algorithm::Myers.

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.

Sets a timeout for thediff operation.

This is like deadline but accepts a duration.

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.

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"),
]);

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"),
]);

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"),
]);

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"),
]);

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, "❄️"),
]);

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

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

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 resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
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.