36

I'm looking for a C or C++ diff library. I know I can use the Unix diff tool in combination with system or exec, but I really want a library. It would be handy if the library could perform patches as well, like the Unix patch tool.

2

6 Answers 6

25

I think I've found a good solution, finally:

The DTL - Diff Template Library --- Tutorial

It supports patch. I had to type "diff.cpp" into Google to find it. Hopefully it works!

0
13

It seems like the Google Diff, Match and Patch libraries are what you need.

5
  • 3
    Eh, but the C++ version of that library is dependent on Qt. I need a diff/patch library for a non-GUI application. Commented Sep 20, 2009 at 23:46
  • 1
    Qt has non-GUI parts, and can be used in a console application.
    – Bill
    Commented Sep 24, 2009 at 17:03
  • 13
    Qt is a large library dependency I don't want to have. Why didn't the author use STL? Commented Sep 29, 2009 at 20:48
  • 8
    An STL port is available on GitHub. Commented Oct 20, 2014 at 20:19
  • That's definitely better than DTL (which apparently does not offer a per-line diff algorithm)
    – jpo38
    Commented Nov 24, 2016 at 20:29
3

This is an implementation of a "solution to SES/LCS with the Hirschberg linear space refinement as described in the following publication":

E. Myers, ``An O(ND) Difference Algorithm and Its Variations,'' Algorithmica 1, 2 (1986), 251-266. http://www.cs.arizona.edu/people/gene/PAPERS/diff.ps

Found it on the Wikipedia page on diff.

That's only for finding a diff though, not applying it as a patch. I think that application of a patch is actually a harder problem; due to the risk of conflicts. It would need some form of user-controlling feedback mechanism, to resolve conflicts.

3
  • This is the best I answer so far, but I really would like a patch library as well. I'll wait a little to see if anyone else has an answer. Commented Sep 24, 2009 at 16:48
  • The file diff.c linked at the beginning appears to have disappeared. (It is still in the Google index though...)
    – Tanuva
    Commented Jul 24, 2017 at 9:02
  • @unwind Oh my, I'm sorry. It is just the https transport that yields a 404 error. Disabling HTTPS Everywhere results in the file being loaded.
    – Tanuva
    Commented Jul 24, 2017 at 14:26
3

Subversion includes a library libsvn_diff.

3

There is one that is part of Mercurial. It exists as some C code that's designed as a Python extension, but it could probably be extracted pretty easily. I believe it can also do binary diffs.

The relevant .c files are mercurial/bdiff.c, mercurial/mpatch.c and possibly mercurial/diffhelpers.c.

1

Also pretty much unfindable in Google, it turns out that Gnulib has a diff module. This one seems sufficient for what I wanted a diff library for. It doesn't seem to have a patch module, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.