DiMaPa (Diff Match and Patch)
A modern Ruby implementation of Google's Diff Match and Patch libraries.
The Diff Match and Patch libraries offer robust algorithms to perform the operations required for synchronizing plain text.
Usage
require 'dimapa'
dmp = DiMaPa.new # or DiffMatchPatch
diff = dmp.diff_main("This is a sentence.", "This is also a sentence.")
#=> [[:equal, "This is a"], [:insert, "lso a"], [:equal, " sentence."]]
dmp.diff_cleanup_semantic(diff)
#=> nil
# diff is modified in place
diff
#=> [[:equal, "This is "], [:insert, "also "], [:equal, "a sentence."]]
patch = dmp.patch_make(diff)
#=> [#<PatchObj:0x00005608e6ac9500 @diffs=
# [[:equal, "This is "], [:insert, "also "], [:equal, "a senten"]],
# @length1=16,
# @length2=21,
# @start1=0,
# @start2=0>]
dmp.patch_to_text(patch)
#=> "@@ -1,16 +1,21 @@\n This is \n+also \n a senten\n"
dmp.patch_apply(patch, "This is a sentence.")
#=> ["This is also a sentence.", [true]]
Installation
# RubyGem
gem install dimapa
# From source
bundle install
bundle exec rake install
Benchmarks
This project includes scripts/ mirroring those in the official project. Performance is on par with those reported for Lua and Python albeit run on a faster machine.
$ rake speedtest
user system total real
diff(t2,t1) 13.658214 0.003937 13.662151 ( 13.662453)
diff(t1,t2) 14.074079 0.000001 14.074080 ( 14.074350)
Tests and Linting
bundle exec rake
Fork of kalmbach/diff_match_patch b/w/o DavidMikeSimon/diff_match_patch
Copyright (c) 2011, Jorge Kalmbach
Work was inspired by the reima/diff_match_patch-ruby module.