public static string GetDiffMessage(object currentSnapshot, object newSnapshot) { var currentSnapshotJObject = JObjectHelper.FromObject(currentSnapshot); var newSnapshotJObject = JObjectHelper.FromObject(newSnapshot); var dmp = new diff_match_patch(); var a = dmp.diff_linesToChars(currentSnapshotJObject.ToString(), newSnapshotJObject.ToString()); var lineText1 = (string)a[0]; var lineText2 = (string)a[1]; var lineArray = (List <string>)a[2]; var diffs = dmp.diff_main(lineText1, lineText2, false); dmp.diff_charsToLines(diffs, lineArray); dmp.diff_cleanupEfficiency(diffs); var builder = new StringBuilder(Environment.NewLine); builder.AppendLine("Snapshots do not match"); builder.AppendLine(RemovedLegendString); builder.AppendLine(AddedLegendString); builder.AppendLine(Environment.NewLine); for (var i = 0; i < diffs.Count; i++) { var diff = diffs[i]; var lines = GetLines(diff.text); switch (diff.operation) { case Operation.DELETE: lines.ForEach(l => builder.AppendLine($"- {l}")); break; case Operation.INSERT: lines.ForEach(l => builder.AppendLine($"+ {l}")); break; case Operation.EQUAL: GetEqualLines(lines, i == 0, i == diffs.Count - 1).ForEach(l => builder.AppendLine(l)); break; } } return(builder.ToString()); }
public static string DiffTwoFiles(string file1, string file2) { diff_match_patch dmp = new diff_match_patch(); dmp.Diff_Timeout = 0; List <Diff> diffs; using (StreamReader r = new StreamReader(File.Open(file1, FileMode.Open))) using (StreamReader r2 = new StreamReader(File.Open(file2, FileMode.Open))) { diffs = dmp.diff_main(r.ReadToEnd(), r2.ReadToEnd()); dmp.diff_cleanupEfficiency(diffs); dmp.diff_cleanupSemantic(diffs); } return(dmp.diff_prettyHtml(diffs)); }
public void ShowDifferenceBetweenTwoFile() { diff_match_patch DIFF = new diff_match_patch(); List <Chunk> chunklist1; List <Chunk> chunklist2; diffs = DIFF.diff_main(richTextBoxCurrentVersion.Text, richTextBoxLastVersion.Text); DIFF.diff_cleanupEfficiency(diffs); chunklist1 = collectChunks(richTextBoxCurrentVersion); chunklist2 = collectChunks(richTextBoxLastVersion); paintChunks(richTextBoxCurrentVersion, chunklist1); paintChunks(richTextBoxLastVersion, chunklist2); richTextBoxCurrentVersion.SelectionLength = 0; richTextBoxLastVersion.SelectionLength = 0; }
private static string GetDiffMessage(JToken old, JToken @new) { var dmp = new diff_match_patch(); var a = dmp.diff_linesToChars(old.ToString(), @new.ToString()); var lineText1 = (string)a[0]; var lineText2 = (string)a[1]; var lineArray = (List <string>)a[2]; var diffs = dmp.diff_main(lineText1, lineText2, false); dmp.diff_charsToLines(diffs, lineArray); dmp.diff_cleanupEfficiency(diffs); var builder = new StringBuilder("\n"); builder.AppendLine("- Snapshot"); builder.AppendLine("+ Receieved\n\n"); for (var i = 0; i < diffs.Count; i++) { var diff = diffs[i]; var lines = GetLines(diff.text); switch (diff.operation) { case Operation.DELETE: lines.ForEach(l => builder.AppendLine($"- {l}")); break; case Operation.INSERT: lines.ForEach(l => builder.AppendLine($"+ {l}")); break; case Operation.EQUAL: GetEqualLines(lines, i == 0, i == diffs.Count - 1).ForEach(l => builder.AppendLine(l)); break; } } return(builder.ToString()); }
private static void updateContent(VisualDiffTextBlock textBlock) { textBlock.Inlines.Clear(); if (textBlock.IsVisualDiffVisible) { var brushConverter = new BrushConverter(); diff_match_patch dmp = new diff_match_patch(); List<Diff> diffList = dmp.diff_main( textBlock.PreviousText.Replace("\n", "\u00b6").Replace("\r", "").Replace(' ', '\u00B7'), textBlock.CurrentText.Replace("\n", "\u00b6").Replace("\r", "").Replace(' ', '\u00B7'), false); // Apply a clean up, the default value of this function is 4 chars. // To change the value, you'll need to do so inside the DiffMatchPatch.cs file. dmp.diff_cleanupEfficiency(diffList); foreach(Diff diffItem in diffList) { switch(diffItem.operation) { case Operation.DELETE: textBlock.Inlines.Add(new Run(diffItem.text) { Background = (Brush)brushConverter.ConvertFromString("#ff6a1010"), Foreground = (Brush)brushConverter.ConvertFromString("#ffdddddd"), TextDecorations = System.Windows.TextDecorations.Strikethrough }); break; case Operation.EQUAL: textBlock.Inlines.Add(new Run(diffItem.text.Replace("\u00b6", "\u00b6" + System.Environment.NewLine))); break; case Operation.INSERT: textBlock.Inlines.Add(new Run(diffItem.text.Replace("\u00b6", "\u00b6" + System.Environment.NewLine)) { Background = (Brush)brushConverter.ConvertFromString("#ff005e41"), Foreground = (Brush)brushConverter.ConvertFromString("#ffdddddd") }); break; } } } else { textBlock.Text = textBlock.CurrentText; } }
private void ButtonCompare_Click(object sender, EventArgs e) { Console.WriteLine("Button click!"); if ((pdfPanel1.pdfViewer.Document == null) || (pdfPanel2.pdfViewer.Document == null)) { labelResult.Text = "0.0"; return; } var stopwatch = new System.Diagnostics.Stopwatch(); int page1 = pdfPanel1.pdfViewer.Renderer.Page; int page2 = pdfPanel2.pdfViewer.Renderer.Page; //if ((pdfPanel1.GetComparedPage(page1) == page2) && (pdfPanel2.GetComparedPage(page2) == page1)) //{ // labelResult.Text = "0.0"; // return; //} if (pdfPanel1.GetComparedPage(page1) >= 0) { pdfPanel1.ClearDiffMarker(page1); } if (pdfPanel2.GetComparedPage(page2) >= 0) { pdfPanel2.ClearDiffMarker(page2); } // Draw without the marker. pdfPanel1.Update(); pdfPanel2.Update(); //stopwatch.Stop(); //Console.WriteLine("Clear:{0}", stopwatch.Elapsed); //stopwatch.Start(); String text1 = pdfPanel1.GetPageText(); String text2 = pdfPanel2.GetPageText(); List <PdfTextSpan> index1 = new List <PdfTextSpan>(); List <PdfTextSpan> index2 = new List <PdfTextSpan>(); stopwatch.Stop(); Console.WriteLine("Get:{0}", stopwatch.Elapsed); labelResult.Text = "."; labelResult.Update(); stopwatch.Start(); diff_match_patch dmp; List <Diff> diffs; int diffType = comboBoxDiffType.SelectedIndex; switch (diffType) { case 0: // NetDiff var results = DiffUtil.Diff(text1, text2); ExtractDiffSpan(index1, index2, results, page1, page2); break; case 1: // Google Diff - Semantic dmp = new diff_match_patch(); diffs = dmp.diff_main(text1, text2); dmp.diff_cleanupSemantic(diffs); ExtractDiffSpan2(index1, index2, diffs, page1, page2); break; case 2: // Google Diff - Effective 4 dmp = new diff_match_patch(); dmp.Diff_EditCost = 4; diffs = dmp.diff_main(text1, text2); dmp.diff_cleanupEfficiency(diffs); ExtractDiffSpan2(index1, index2, diffs, page1, page2); break; case 3: // Google Diff - Effective 5 dmp = new diff_match_patch(); dmp.Diff_EditCost = 5; diffs = dmp.diff_main(text1, text2); dmp.diff_cleanupEfficiency(diffs); ExtractDiffSpan2(index1, index2, diffs, page1, page2); break; case 4: // Google Diff - Effective 3 dmp = new diff_match_patch(); dmp.Diff_EditCost = 3; diffs = dmp.diff_main(text1, text2); dmp.diff_cleanupEfficiency(diffs); ExtractDiffSpan2(index1, index2, diffs, page1, page2); break; case 5: // Google Diff - Raw dmp = new diff_match_patch(); diffs = dmp.diff_main(text1, text2); ExtractDiffSpan2(index1, index2, diffs, page1, page2); break; default: throw new Exception(); } stopwatch.Stop(); Console.WriteLine("Diff:{0}", stopwatch.Elapsed); labelResult.Text = ".."; labelResult.Update(); stopwatch.Start(); pdfPanel1.SetPageDiff(index1); pdfPanel2.SetPageDiff(index2); pdfPanel1.AddDiffMarker(page2); labelResult.Text = "..."; labelResult.Update(); pdfPanel2.AddDiffMarker(page1); labelResult.Text = "...."; labelResult.Update(); stopwatch.Stop(); Console.WriteLine("Marker:{0}", stopwatch.Elapsed); stopwatch.Start(); //results.ToList().ForEach(r => Console.WriteLine(r.ToFormatString())); if (stopwatch.ElapsedMilliseconds < 50) { Thread.Sleep(30); } // Draw with the markers. pdfPanel1.Update(); pdfPanel2.Update(); stopwatch.Stop(); Console.WriteLine("Draw:{0}", stopwatch.Elapsed); labelResult.Text = String.Format("{0:0.0}", stopwatch.ElapsedMilliseconds / 1000.0); }