Project

dimapa

0.0
No release in over 3 years
Low commit activity in last 3 years
A modern Ruby implementation of Google's Diff Match Patch
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

 Project Readme

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.

$ bundle exec rake speedtest
Running benchmark with plain Ruby
ruby scripts/speedtest.rb
              user     system      total        real
diff(t2,t1)  9.587209   0.013017   9.600226 (  9.601807)
diff(t1,t2)  9.750088   0.008715   9.758803 (  9.758928)

Running benchmark with JIT enabled
ruby --jit scripts/speedtest.rb
              user     system      total        real
diff(t2,t1)  5.345181   0.020641   7.082650 (  5.359839)
diff(t1,t2)  6.528630   0.028800   7.395639 (  6.566026)

Tests and Linting

bundle exec rake

Copyright (c) 2011, Jorge Kalmbach <kalmbach.at.gmail.com>

Work was inspired by the reima/diff_match_patch-ruby module.