A translation of Google's public-domain diff_match_patch code into pure Elixir.
For information about the original Google project and its application, see that repository's wiki pages.
References from the Google project:
- diff: An O(ND) Difference Algorithm and Its Variations (Meyers, 1986)
- match: Fast Text Searching with Errors (Wu and Manber, 1991)
Prior art on the BEAM:
If available in Hex, the package can be installed
by adding diff_match_patch
to your list of dependencies in mix.exs
:
def deps do
[
{:diff_match_patch, "~> 0.2.0"}
]
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/cursor.
- Find a match for a pattern inside at text using fuzzy search. The
match_threshold
option determines the exactness required:
loc = Dmp.Match.main(
"I am the very model of a modern major general.",
" that berry ",
5,
match_threshold: 0.7)
The value of loc
is 4, finding the closest match at the string starting " the very".
If no close match is found, the loc
returned is -1.
- Create a diff between two texts:
diffs = Dmp.Diff.main(
"The quick brown fox jumps over the lazy dog.",
"That quick brown fox jumped over a lazy dog.")
- Create a patch from the the original text and the diff we just created. The patch can also be created directly from the two texts. These two patches are equal:
patches1 = Dmp.Patch.make(
"The quick brown fox jumps over the lazy dog.", diffs)
patches2 = Dmp.Patch.make(
"The quick brown fox jumps over the lazy dog.",
"That quick brown fox jumped over a lazy dog.")
- Apply the patch to a third text:
{new_text, matched} = Patch.apply(patches, "The quick red rabbit jumps over the tired tiger.")
IO.puts(new_text)
The new_text
prints out as "That quick red rabbit jumped over a tired tiger."
The matched
value is a list of booleans, showing whether the results of whether
matches were made between the (expanded) list of patches and the third text.