This library provides functions to show the differences between two strings. It uses the Levenshtein distance to compute the minimum number of edit operations: insertions, deletions, substitutions needed to go from one string to the other.
Several options are available to customize this processing:
-
split_sizestrings are broken into a list of lines if they contain any new lines first. Then if any line is too large, according to thesplit_sizeit is broken into smaller pieces for comparison This is done in order to reduce the size of the edit matrix which is used to compute all the edit costs The default is 200 -
separatorsopening and closing pieces of text (brackets by default) used to highlight a difference -
shorten sizethere is the possibly to display mostly the differences with a bit of context around if the input text is too large. The string get elided around separators if it gets greater than theshorten_size(the default is 20) -
shorten_textthe string to use when eliding characters in the original string (the default is"...") -
display_edit_operationsa function to specify how edit operations, insert/delete/substitute/keep are represented
Here are some examples:
use *;
// "between the e and the n the letter i was added"
assert_eq!;
// "at the end of the text 3 letters have been deleted"
assert_eq!;
// "between the t and the n 2 letters have been modified"
assert_eq!;
// "between the t and the n 2 letters have been modified"
let my_split_size = SplitSize ;
assert_eq!;
The output can also be coloured. For example:
Try it in a REPL
You can try this library in a REPL:
- clone this repository
- install the
evcxrrepl:cargo install evcxr_repl - start it
>>
- add the current path as a dependency and import the
editsmodule
>> :dep
>> use ;
- use a
showfunction
>> println!("{}", show_distance_colored("abcd", "abcdefg"));