Diff::LCS computes the difference between two Enumerable sequences using the McIlroy-Hunt longest common subsequence (LCS) algorithm. It includes utilities to create a simple HTML diff output format and a standard diff-like tool.
This is release 1.2.4, fixing a bug introduced after diff-lcs 1.1.3 that did not properly prune common sequences at the beginning of a comparison set. Thanks to Paul Kunysch for fixing this issue.
Coincident with the release of diff-lcs 1.2.3, we reported an issue with Rubinius in 1.9 mode (rubinius/rubinius#2268). We are happy to report that this issue has been resolved.
require 'diff/lcs' seq1 = %w(a b c e h j l m n p) seq2 = %w(b c d e f j k l m r s t) lcs = Diff::LCS.LCS(seq1, seq2) diffs = Diff::LCS.diff(seq1, seq2) sdiff = Diff::LCS.sdiff(seq1, seq2) seq = Diff::LCS.traverse_sequences(seq1, seq2, callback_obj) bal = Diff::LCS.traverse_balanced(seq1, seq2, callback_obj) seq2 == Diff::LCS.patch!(seq1, diffs) seq1 == Diff::LCS.unpatch!(seq2, diffs) seq2 == Diff::LCS.patch!(seq1, sdiff) seq1 == Diff::LCS.unpatch!(seq2, sdiff)
Objects can be extended with Diff::LCS:
seq1.extend(Diff::LCS) lcs = seq1.lcs(seq2) diffs = seq1.diff(seq2) sdiff = seq1.sdiff(seq2) seq = seq1.traverse_sequences(seq2, callback_obj) bal = seq1.traverse_balanced(seq2, callback_obj) seq2 == seq1.patch!(diffs) seq1 == seq2.unpatch!(diffs) seq2 == seq1.patch!(sdiff) seq1 == seq2.unpatch!(sdiff)
Note that Diff::LCS requires a sequenced enumerable container, which means that the order of enumeration is both predictable and consistent for the same set of data. While it is theoretically possible to generate a diff for an unordered hash, it will only be meaningful if the enumeration of the hashes is consistent. In general, this will mean that containers that behave like String or Array will perform best.
Diff::LCS is a port of Perl's Algorithm::Diff that uses the McIlroy-Hunt longest common subsequence (LCS) algorithm to compute intelligent differences between two sequenced enumerable containers. The implementation is based on Mario I. Wolczko's Smalltalk version 1.2 (1993) and Ned Konz's Perl version Algorithm::Diff 1.15.
This library is called Diff::LCS because of an early version of Algorithm::Diff which was restrictively licensed.
I value any contribution to Diff::LCS you can provide: a bug report, a feature request, or code contributions.
Changes <strong>will not</strong> be accepted without tests.
The test suite is written with RSpec.‡
Match my coding style.
Use a thoughtfully-named topic branch that contains your change. Rebase your commits into logical chunks as necessary.
Do not change the version number; when your patch is accepted and a release is made, the version will be updated at that point.
Submit a GitHub pull request with your changes.
New features require new documentation.
To run the test suite, you will need to install the development dependencies for Diff::LCS. If you have Bundler, you can accomplish this easily:
$ bundle install
which runs the tests the same way that:
$ rake spec $ rake test $ rake travis
Here’s the most direct way to get your work merged into the project:
Fork the project.
Clone down your fork (+git clone git://github.com/<username>/diff-lcs.git+).
Create a topic branch to contain your change (+git checkout -b my_awesome_feature+).
Hack away, add tests. Not necessarily in that order.
Make sure everything still passes by running `rake`.
If necessary, rebase your commits into logical chunks, without errors.
Push the branch up (+git push origin my_awesome_feature+).
Create a pull request against halostatue/diff-lcs and describe what your change does and the why you think it should be merged.
Austin Ziegler created Diff::LCS.
Thanks to everyone else who has contributed to Diff::LCS:
This software is available under three licenses: the GNU GPL version 2 (or at your option, a later version), the Perl Artistic license, or the MIT license. Note that my preference for licensing is the MIT license, but Algorithm::Diff was dually originally licensed with the Perl Artistic and the GNU GPL (“the same terms as Perl itself”) and given that the Ruby implementation originally hewed pretty closely to the Perl version, I must maintain the additional licensing terms.
Copyright 2004–2013 Austin Ziegler.
Adapted from Algorithm::Diff (Perl) by Ned Konz and a Smalltalk version by Mario I. Wolczko.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
See the file docs/artistic.txt in the main distribution.
See the file docs/COPYING.txt in the main distribution.