35

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.

Matt Fichman
  • 5,458
  • 4
  • 39
  • 59
  • It seems to me that a diff library would be pretty common, but Google has yet to turn up a good answer. – Matt Fichman Sep 21 '09 at 19:28
  • [An excellent approach was provided for C/C++ on a similar post.](https://stackoverflow.com/questions/42377443/diff-compare-two-files-by-file-descriptor-fd-instead-of-file-name/42380790#42380790) – Cloud Feb 22 '17 at 22:53

6 Answers6

24

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!

maxschlepzig
  • 35,645
  • 14
  • 145
  • 182
Matt Fichman
  • 5,458
  • 4
  • 39
  • 59
13

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

Paul Biggar
  • 27,579
  • 21
  • 99
  • 152
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.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 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. – Matt Fichman Sep 24 '09 at 16:48
  • The file `diff.c` linked at the beginning appears to have disappeared. (It is still in the Google index though...) – Tanuva Jul 24 '17 at 09: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 Jul 24 '17 at 14:26
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.

Omnifarious
  • 54,333
  • 19
  • 131
  • 194
2

Subversion includes a library libsvn_diff.

Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235
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.

ptomato
  • 56,175
  • 13
  • 112
  • 165