Diff in .Net Core using textdiffcore

In Unix systems the ‘diff’ command is used to highlight the differences between files, it is an extremely useful tool for comparing documents. Outside of Unix systems this functionality is hard to come by, especially if you need to embed the functionality into your program. This is where the textdiffcore package fits in; textdiffcore is a cross platform utility built using .NET Core. textdiffcore is able to give you a list of changes as well as produce an output from the changes in a variety of formats (at the time of writing only HTML and Markdown is supported).

How to use textdiffcore

First of all you will need to add the textdiffcore package to your .Net Core application. To do this follow one of these steps:

  • Package Manager:
    • PM> Install-Package textdiffcore -Version 1.0.3
  • .NET CLI
    • dotnet add package textdiffcore –version 1.0.3
  • Packet CLI
    • paket add textdiffcore –version 1.0.3

You should replace the version number with the latest available on nuget (https://www.nuget.org/packages/textdiffcore/)

In your code you will need to add the following using statements:

using textdiffcore;
using textdiffcore.DiffOutputGenerators;
using textdiffcore.TextDiffEngine;

to use the package you will need to create an instance of the TextDiff class. This class can be instantiated using the following arguments new TextDiff(ITextDiffEngine, IDiffOutputGenerator), for example:

TextDiff diffobj = new TextDiff(new MyersDiff(), new MarkdownDiffOutputGenerator());

The code above creates a TextDiff object which will use the Myers Diff algorithm (powered by google/diff-match-patch) as ITextDiffEngine and MarkdownDiffOutputGenerator as IDiffOutputGenerator (produces the output in Markdown format).

Using the object is a matter of calling the GenerateDiffOutput(string, string) method, i.e.:

//Generate the output
string output = diffobj.GenerateDiffOutput("oldText", "newText");
//print the output to the console
System.Console.WriteLine(output);

This will output:

**~~old~~** **new**Text

Which will look something like this when rendered: “old newText”.

You can also access the Diffrence list after calling either the GenerateDiffOutput or GenerateDiffList method like so:

List innerDiffList = diffobj.InnerList;

Example 2:

The HTMLDiffOutputGenerator(TagType, AttributeName, addAttributeValue, removeAttributeValue, equalAttributeValue) produces a more profound result. If we were to create the TextDiff object like so:

TextDiff diffobj = new TextDiff(new MyersDiff(), new HTMLDiffOutputGenerator("span", "style", "color:#003300;background-color:#ccff66;","color:#990000;background-color:#ffcc99;text-decoration:line-through;",""));

And compared the following strings:

string oldText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\r\nMaecenas luctus ipsum sit amet turpis pulvinar, et consequat magna tincidunt.\r\nUt tristique sem vitae justo elementum, et fermentum arcu elementum.\r\nDonec blandit facilisis vulputate. Maecenas eu elit ut tortor volutpat condimentum.\r\nPhasellus faucibus vehicula turpis eu pharetra. In imperdiet iaculis cursus.";
string newText = "Lorem ipsum dolor sit amet, consectetur adiafeeing elit.\r\nMaecenas  sit amet turpis pulvinar, et consequat magna tincidunt.\r\nUt tristique sem vitae justo elementum, et free arcu elementum.\r\nDwfvwc blandit facilisis vulputate. Maecenas eu elit ut tortor volutpat condimentum.\r\nPhasellus dotnet vehicula turpis eu pharetra. In imperdiet iaculis cursus chieown.";

The output would result in the following HTML:

html

When rendered it will look like so:

Lorem ipsum dolor sit amet, consectetur adipiscafeeing elit.
Maecenas luctus ipsum sit amet turpis pulvinar, et consequat magna tincidunt.
Ut tristique sem vitae justo elementum, et fermentumearcu elementum.
Donewfvwc blandit facilisis vulputate. Maecenas eu elit ut tortor volutpat condimentum.
Phasellus faucibusdotnet vehicula turpis eu pharetra. In imperdiet iaculis cursus chieown.

Leave a comment