Photo by Markus Spiske on Diff.Match.Patch based on Google library.
Diff
Diff are made of:
- text (part of the text related)
- operation to be applied (delete / insert / equal)
It's easy to get diff:
string a = "<SPAN TITLE=\"i=0\">a¶<BR></SPAN><DEL STYLE=\"background:#FFE6E6;\" TITLE=\"i=2\"><B>b</B></DEL><INS STYLE=\"background:#E6FFE6;\" TITLE=\"i=2\">c&d</INS>";
string b = "<SPAN TITLE=\"i=0\">testa¶<BR></SPAN><DEL STYLE=\":#FFE6E6;\" TITLE=\"i=2\"><B>b</B></DEL><INS STYLE=\"background:#E6FFE6;\" TITLE=\"i=2\">c&d</INS>";
var diffs = DiffMatchPatchModule.Default.DiffMain(a, b);
Please note that diffs will contains a lot of Equal diff !
Result:
Patch
It's simple too to get patch from multiple texts!
string a = "<SPAN TITLE=\"i=0\">a¶<BR></SPAN><DEL STYLE=\"background:#FFE6E6;\" TITLE=\"i=2\"><B>b</B></DEL><INS STYLE=\"background:#E6FFE6;\" TITLE=\"i=2\">c&d</INS>";
string b = "<SPAN TITLE=\"i=0\">testa¶<BR></SPAN><DEL STYLE=\":#FFE6E6;\" TITLE=\"i=2\"><B>b</B></DEL><INS STYLE=\"background:#E6FFE6;\" TITLE=\"i=2\">c&d</INS>";
string c = "<SPAN TITLE=\"i=0\">a¶<BR></SPAN><DEL STYLE=\"background:#FFE6E6;\" TITLE=\"i=2\">mon tittre en Or<B>b</B></DEL><INS STYLE=\"background:#E6FFE6;\" TITLE=\"i=2\">c&d</INS>";
var firstPatch = DiffMatchPatchModule.Default.PatchMake(a, b);
var secondPatch = DiffMatchPatchModule.Default.PatchMake(a, c);
DiffMatchPatchModule.Default.PatchApply(secondPatch, DiffMatchPatchModule.Default.PatchApply(firstPatch, a)[0].ToString());
Ouptut:
<SPAN TITLE="i=0">testa¶<BR></SPAN><DEL STYLE=":#FFE6E6;" TITLE="i=2">mon tittre en Or<B>b</B></DEL><INS STYLE="background:#E6FFE6;" TITLE="i=2">c&d</INS>
Please note that DiffMatchPatchModule.Default.PatchApply(firstPatch, a) outputs object[] !
Hope that helps !
Top comments (0)