24

I am looking at http://code.google.com/p/google-diff-match-patch/ and have downloaded the file. When I look at it is 2 files

DiffMatchPatch.cs
DiffMatchPatchTest.cs

When I try to make a new object of DiffMatchPatch.cs I have to pass in some operation and string text.

http://neil.fraser.name/software/diff_match_patch/svn/trunk/demos/demo_diff.html

In the demo they cross out the words that are different and that is what I am trying to achieve.

I am trying to compare 2 blocks of text on the server side finds the differences and send a email to the user with the file block of text to them like the end result is in the demo that I posted above.

So does anyone have a tutorial on how to use the C# version?

4
  • @ Henrik P. Hessel - That I don't know how to use the methods in the class. They have diff_match_patch or Diff or Patch and each has stuff but I don't know what to use. Commented May 18, 2011 at 22:41
  • 2
    code.google.com/p/google-diff-match-patch/wiki/API Commented Aug 21, 2014 at 22:57
  • Note: the above link is now dead Commented Dec 21, 2016 at 16:55
  • Above link no longer dead... just tried and it works. Commented May 19, 2017 at 5:20

3 Answers 3

29

For reference, this is really easy:

var dmp = new diff_match_patch();
var diffs = dmp.diff_main(text1, text2);
var html = dmp.diff_prettyHtml(diffs);
Sign up to request clarification or add additional context in comments.

3 Comments

What threw me off is that this code format looks like JavaScript and not C#. If anyone has the same issue, this code goes in your C# class.
how do i use the html on c# wpf?
Is there any way I can generate a pdf and display the comparison results in that instead of the HTML file?
9

Implementation with current version(2.1.0) would look like this

var dmp = DiffMatchPatchModule.Default;
var diffs = dmp.DiffMain(text1, text2);
var html = dmp.DiffPrettyHtml(diffs);

3 Comments

Warning! now 'class' is not instantiated, it uses static reference apart of methods changing names. Took me some time to realize the difference in initialization. Looks like one can use only one diff at a time. Documentation example on DMP project git is outdated (github.com/google/diff-match-patch/wiki/Language:-C%23) and new implementation looks more like wrapper than fulfilling the claim that same API is used. Thank you @maxlego !
I'm assuming you refer to this port: github.com/pocketberserker/Diff.Match.Patch
How would you change the MatchThreshold while doing a dmp.MatchMain? I can see MatchThreshold but it is a read only value?
2

For anyone who came across this thread because of the title and expected an explanation on how to use the Google Diff-Match-Patch algorithm via the https://github.com/pocketberserker/Diff.Match.Patch library found on NuGet, to create a diff string, so he can send the change somewhere (e.g. via websocket) and restore it at the destination based on the old value and the diff string, that would work like this:

var oldValue = "Test old text.";
var newValue = "Test new text.";

// create diff string
var dmp = DiffMatchPatch.DiffMatchPatchModule.Default;
var diffs = dmp.DiffMain(oldValue, newValue);
var srcDelta = dmp.DiffToDelta(diffs);
// restore from diff
var dmp = DiffMatchPatch.DiffMatchPatchModule.Default;
var dstDelta = dmp.DiffFromDelta(oldValue, srcDelta);
var restoredNewValue = dmp.DiffText2(dstDelta);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.