public void Compare(string source, string destination) { this.source = new DiffList_StringData(source + "\n"); this.destination = new DiffList_StringData(destination + "\n"); engine.ProcessDiff(this.source, this.destination); ArrayList list = engine.DiffReport(); diffLines.Clear(); foreach (DiffResultSpan span in list) { switch (span.Status) { case DiffResultSpanStatus.NoChange: diffLines.Add(new DiffLine(0, GetSpanText(span, false))); break; case DiffResultSpanStatus.AddDestination: diffLines.Add(new DiffLine(+1, GetSpanText(span, true))); break; case DiffResultSpanStatus.DeleteSource: diffLines.Add(new DiffLine(-1, GetSpanText(span, false))); break; case DiffResultSpanStatus.Replace: diffLines.Add(new DiffLine(-1, GetSpanText(span, false))); diffLines.Add(new DiffLine(+1, GetSpanText(span, true))); break; } } }
private void BinaryDiff(string sFile, string dFile) { this.Cursor = Cursors.WaitCursor; DiffList_BinaryFile sLF = null; DiffList_BinaryFile dLF = null; try { sLF = new DiffList_BinaryFile(sFile); dLF = new DiffList_BinaryFile(dFile); } catch (Exception ex) { this.Cursor = Cursors.Default; MessageBox.Show(ex.Message, "File Error"); return; } try { double time = 0; DiffEngine de = new DiffEngine(); time = de.ProcessDiff(sLF, dLF, _level); ArrayList rep = de.DiffReport(); BinaryResults dlg = new BinaryResults(rep, time); dlg.ShowDialog(); dlg.Dispose(); } catch (Exception ex) { this.Cursor = Cursors.Default; string tmp = string.Format("{0}{1}{1}***STACK***{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace); MessageBox.Show(tmp, "Compare Error"); return; } this.Cursor = Cursors.Default; }
private void ComputeLineDifferences(LineRecord lrec, LineRecord rrec) { DiffList_CharData left = new DiffList_CharData(lrec.Text); DiffList_CharData right = new DiffList_CharData(rrec.Text); DiffEngine engine = new DiffEngine(); engine.ProcessDiff(left, right, DiffEngineLevel.SlowPerfect); ArrayList report = engine.DiffReport(); foreach (DiffResultSpan dres in report) { switch (dres.Status) { case DiffResultSpanStatus.NoChange: break; case DiffResultSpanStatus.Replace: lrec.AddRange(dres.SourceIndex, dres.SourceIndex + dres.Length, Color.LightSalmon, Color.Empty); rrec.AddRange(dres.DestIndex, dres.DestIndex + dres.Length, Color.LightSalmon, Color.Empty); break; case DiffResultSpanStatus.DeleteSource: lrec.AddRange(dres.SourceIndex, dres.SourceIndex + dres.Length, Color.LightSalmon, Color.Empty); break; case DiffResultSpanStatus.AddDestination: rrec.AddRange(dres.DestIndex, dres.DestIndex + dres.Length, Color.LightSalmon, Color.Empty); break; default: break; } // switch } // foreach }
private void btnCompare_Click(object sender, EventArgs e) { Button btn = sender as Button; if (btn != null) { if (btn.Equals(this.btnCompare)) { this.lbxChangedLine.Items.Clear(); this.tsLblSource.Text = this.tsLblDest.Text = ""; string pText1 = this.ucCompareSelectSource.GetString(); string pText2 = this.ucCompareSelectDestination.GetString(); if (string.IsNullOrEmpty(pText1)) { this.gbxSource.Visible = this.gbxDest.Visible = true; this.pnlCompareInfo.Visible = !this.gbxSource.Visible; } else { if (string.IsNullOrEmpty(pText2)) { this.gbxSource.Visible = this.gbxDest.Visible = true; this.pnlCompareInfo.Visible = !this.gbxSource.Visible; } else { this.tsLblSource.Text = this.ucCompareSelectSource.GetTitle(); this.tsLblDest.Text = this.ucCompareSelectDestination.GetTitle(); this._sorurceText = new DiffListText(pText1); this._destText = new DiffListText(pText2); this.lvSource.Items.Clear(); this.lvDestination.Items.Clear(); DiffEngine diffEngine = new DiffEngine(); DiffEngineLevel level = !this.rbtnFast.Checked ? (!this.rbtnMedium.Checked ? DiffEngineLevel.SlowPerfect : DiffEngineLevel.Medium) : DiffEngineLevel.FastImperfect; diffEngine.ProcessDiff((IDiffList)this._sorurceText, (IDiffList)this._destText, level); this.ShowDiff(this._sorurceText, this._destText, diffEngine.DiffReport()); this.gbxSource.Visible = this.gbxDest.Visible = false; this.pnlCompareInfo.Visible = !this.gbxSource.Visible; } } } else if (btn.Equals(this.btnVisible)) { this.gbxSource.Visible = this.gbxDest.Visible = !this.gbxSource.Visible; this.pnlCompareInfo.Visible = !this.gbxSource.Visible; } else if (btn.Equals(this.btnClose)) { this.Close(); } } }
private void CompareLines(string line1, string line2, StringBuilder res1, StringBuilder res2, ref int changed, ref int added, ref int deleted) { DiffList_CharData s1 = new DiffList_CharData(line1); DiffList_CharData s2 = new DiffList_CharData(line2); DiffEngine de = new DiffEngine(); ArrayList rep; int i = 0; de.ProcessDiff(s1, s2, DiffEngineLevel.SlowPerfect); rep = de.DiffReport(); foreach (DiffResultSpan drs in rep) { switch (drs.Status) { case DiffResultSpanStatus.DeleteSource: res1.Append("<span style=\"background-color: #FBB2A7\">"); for (i = 0; i < drs.Length; i++) { res1.Append(s1.GetByIndex(drs.SourceIndex + i)); deleted++; } res1.Append("</span>"); break; case DiffResultSpanStatus.AddDestination: res2.Append("<span style=\"background-color: #85D685\">"); for (i = 0; i < drs.Length; i++) { res2.Append(s2.GetByIndex(drs.DestIndex + i)); added++; } res2.Append("</span>"); break; case DiffResultSpanStatus.NoChange: for (i = 0; i < drs.Length; i++) { res1.Append(s1.GetByIndex(drs.SourceIndex + i)); res2.Append(s2.GetByIndex(drs.DestIndex + i)); } break; case DiffResultSpanStatus.Replace: res1.Append("<span style=\"background-color: #75C5FF\">"); res2.Append("<span style=\"background-color: #75C5FF\">"); for (i = 0; i < drs.Length; i++) { res1.Append(s1.GetByIndex(drs.SourceIndex + i)); res2.Append(s2.GetByIndex(drs.DestIndex + i)); changed++; } res1.Append("</span>"); res2.Append("</span>"); break; } } }
public string GetDifferences(string first, string second) { DiffEngine engine = new DiffEngine(); DiffListHtml source = new DiffListHtml(StringUtil.RemoveTags(first)); DiffListHtml destination = new DiffListHtml(StringUtil.RemoveTags(second)); engine.ProcessDiff(source, destination, DiffEngineLevel.SlowPerfect); StringBuilder builder = new StringBuilder(); foreach (DiffResultSpan span in engine.DiffReport()) { if (span != null) { switch (span.Status) { case DiffResultSpanStatus.NoChange: this.Append(builder, first, span.SourceIndex, span.Length); break; case DiffResultSpanStatus.Replace: this.Append( builder, first, span.SourceIndex, span.Length, "green"); this.Append( builder, second, span.DestIndex, span.Length, "red;text-decoration:line-through;font-weight:bold"); break; case DiffResultSpanStatus.DeleteSource: this.Append( builder, first, span.SourceIndex, span.Length, "green;font-weight:bold"); break; case DiffResultSpanStatus.AddDestination: this.Append( builder, second, span.DestIndex, span.Length, "red;text-decoration:line-through;font-weight:bold"); break; } } } return(builder.ToString()); }
private void TextDiff(string sFile, string dFile) { this.Cursor = Cursors.Wait; DiffList_TextFile sLF = null; DiffList_TextFile dLF = null; try { sLF = new DiffList_TextFile(sFile); dLF = new DiffList_TextFile(dFile); } catch (Exception ex) { this.Cursor = Cursors.Arrow; MessageBox.Show(ex.Message, "File Error"); return; } try { double time = 0; DiffEngine de = new DiffEngine(); time = de.ProcessDiff(sLF, dLF, DiffEngineLevel.SlowPerfect); ArrayList rep = de.DiffReport(); Results dlg = new Results(sLF, dLF, rep, time); try { this.Hide(); dlg.ShowDialog(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Unexpected Error!", MessageBoxButton.OKCancel, MessageBoxImage.Error, MessageBoxResult.None); dlg.Close(); } finally { this.Show(); } } catch (Exception ex) { this.Cursor = Cursors.Arrow; string tmp = string.Format("{0}{1}{1}***STACK***{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace); MessageBox.Show(tmp, "Compare Error"); return; } this.Cursor = Cursors.Arrow; }
//public static double StringSimilar(string sourceString, string checkString) public static DiffResult matchString(string sourceString, string checkString) { DiffEngine diffEngine = new DiffEngine(); DiffList_CharData source = new DiffList_CharData(sourceString); DiffList_CharData dest = new DiffList_CharData(checkString); var time = diffEngine.ProcessDiff(source, dest); var report = diffEngine.DiffReport(); string result = ""; int addCount = 0; int deleteCount = 0; int replaceCount = 0; int sameCount = 0; foreach (DiffResultSpan diffResultSpan in report) { switch (diffResultSpan.Status) { case DiffResultSpanStatus.AddDestination: { addCount += diffResultSpan.Length; break; } case DiffResultSpanStatus.DeleteSource: { deleteCount += diffResultSpan.Length; break; } case DiffResultSpanStatus.Replace: { for (int i = 0; i < diffResultSpan.Length; i++) { result += dest.GetByIndex(diffResultSpan.DestIndex + i); } replaceCount += diffResultSpan.Length; break; } case DiffResultSpanStatus.NoChange: { for (int i = 0; i < diffResultSpan.Length; i++) { result += dest.GetByIndex(diffResultSpan.DestIndex + i); } sameCount += diffResultSpan.Length; break; } } } return(new DiffResult(addCount, deleteCount, replaceCount, sameCount));//(deleteCount) + "|" + result; }
// Sitecore.Text.Diff.View.TwoCoumnsDiffView protected static void Compare(ref string value1, ref string value2) { DiffEngine diffEngine = new DiffEngine(); string value3 = value1; string value4 = value2; value1 = StringUtil.RemoveTags(value1); value2 = StringUtil.RemoveTags(value2); DiffListHtml source = new DiffListHtml(value1); DiffListHtml destination = new DiffListHtml(value2); diffEngine.ProcessDiff(source, destination, DiffEngineLevel.SlowPerfect); System.Collections.ArrayList arrayList = diffEngine.DiffReport(); System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(); System.Text.StringBuilder stringBuilder2 = new System.Text.StringBuilder(); for (int i = 0; i < arrayList.Count; i++) { DiffResultSpan diffResultSpan = arrayList[i] as DiffResultSpan; if (diffResultSpan != null) { switch (diffResultSpan.Status) { case DiffResultSpanStatus.NoChange: Append(stringBuilder, value1, diffResultSpan.SourceIndex, diffResultSpan.Length); Append(stringBuilder2, value2, diffResultSpan.DestIndex, diffResultSpan.Length); break; case DiffResultSpanStatus.Replace: Append(stringBuilder, value1, diffResultSpan.SourceIndex, diffResultSpan.Length, "blue;font-weight:bold"); Append(stringBuilder2, value2, diffResultSpan.DestIndex, diffResultSpan.Length, "blue;font-weight:bold"); break; case DiffResultSpanStatus.DeleteSource: Append(stringBuilder, value1, diffResultSpan.SourceIndex, diffResultSpan.Length, "blue;font-weight:bold"); Append(stringBuilder2, value2, diffResultSpan.DestIndex, diffResultSpan.Length, "blue;font-weight:bold"); break; case DiffResultSpanStatus.AddDestination: Append(stringBuilder, value1, diffResultSpan.SourceIndex, diffResultSpan.Length, "blue;font-weight:bold"); Append(stringBuilder2, value2, diffResultSpan.DestIndex, diffResultSpan.Length, "blue;font-weight:bold"); break; } } } if (arrayList.Count == 0 && (value3.Contains("<image") || value4.Contains("<image") || value3.Contains("<link") || value4.Contains("<link"))) { stringBuilder.Append("<span>" + value3.Replace("<image", "< image").Replace("<link", "< link") + "</span>"); stringBuilder2.Append("<span>" + value4.Replace("<image", "< image").Replace("<link", "< link") + "</span>"); } value1 = stringBuilder.ToString(); value2 = stringBuilder2.ToString(); }
public void Compare() { TextDiff sourceDiffList = new TextDiff(_originalSourceText); TextDiff destDiffList = new TextDiff(_originalDestText); DiffEngine de = new DiffEngine(); de.ProcessDiff(sourceDiffList, destDiffList, DiffEngineLevel.Medium); ArrayList rep = de.DiffReport(); DisplayDiff(sourceDiffList, destDiffList, rep); }
private static ArrayList GetFileChanges(string source, string destinationPath) { DiffList_TextFile sourceFile = new DiffList_TextFile(source.Split('\n')); DiffList_TextFile destinationFile = new DiffList_TextFile(destinationPath); DiffEngine diffEngine = new DiffEngine(); diffEngine.ProcessDiff(sourceFile, destinationFile, DiffEngineLevel.FastImperfect); ArrayList differenceList = diffEngine.DiffReport(); return(differenceList); }
private void btnDiff_Click(object sender, EventArgs e) { TextFileDiffList sLF = null; TextFileDiffList dLF = null; string sourceFile = null; string targetFile = null; try { _edSvc.SyncSessionCopy(); var set = XmlCompareUtil.PrepareForComparison(_edSvc.CurrentConnection.ResourceService, _edSvc.ResourceID, _edSvc.EditedResourceID); sLF = set.Source; dLF = set.Target; } catch (Exception ex) { ErrorDialog.Show(ex); return; } finally { try { File.Delete(sourceFile); } catch { } try { File.Delete(targetFile); } catch { } } try { double time = 0; DiffEngine de = new DiffEngine(); time = de.ProcessDiff(sLF, dLF, DiffEngineLevel.SlowPerfect); var rep = de.DiffReport(); TextDiffView dlg = new TextDiffView(sLF, dLF, rep, time); dlg.SetLabels(_edSvc.ResourceID, Strings.EditedResource); dlg.ShowDialog(); dlg.Dispose(); } catch (Exception ex) { string nl = Environment.NewLine; string tmp = $"{ex.Message}{nl}{nl}***STACK***{nl}{ex.StackTrace}"; MessageBox.Show(tmp, Strings.CompareError); return; } }
private void btnCompare_Click(object sender, EventArgs e) { bool bValid = true; if (string.IsNullOrEmpty(txtSource.Text)) { errorProvider.SetError(txtSource, Strings.Required); bValid = false; } else { errorProvider.SetError(txtSource, null); } if (string.IsNullOrEmpty(txtTarget.Text)) { errorProvider.SetError(txtTarget, Strings.Required); bValid = false; } else { errorProvider.SetError(txtTarget, null); } if (bValid) { var set = XmlCompareUtil.PrepareForComparison(_conn.ResourceService, this.Source, this.Target); double time = 0; DiffEngine de = new DiffEngine(); time = de.ProcessDiff(set.Source, set.Target, DiffEngineLevel.SlowPerfect); var rep = de.DiffReport(); using (TextDiffView dlg = new TextDiffView(set.Source, set.Target, rep, time)) { dlg.SetLabels(this.Source, this.Target); dlg.ShowDialog(); this.Close(); } } }
private int GetOutOfOrderElements(string[] source, string[] dest) { DiffEngine differ = new DiffEngine(); differ.ProcessDiff(new DiffList_TextFile(source), new DiffList_TextFile(dest)); ArrayList diffs = differ.DiffReport(); diffs.TrimToSize(); Debug.Log("Diffs found: " + diffs.Count); int errors = 0; foreach (DiffResultSpan diff in diffs) { if (!diff.Status.Equals("NoChange")) { errors += diff.Length; } Debug.Log("diff: " + diff); } return(diffs.Count / 2); }
private void TextDiff(string sFile, string dFile) { DiffList_TextFile sLF = null; DiffList_TextFile dLF = null; try { sLF = new DiffList_TextFile(sFile); dLF = new DiffList_TextFile(dFile); } catch (Exception ex) { MessageBox.Show(ex.Message, "File Error"); return; } try { double time = 0; DiffEngine de = new DiffEngine(); time = de.ProcessDiff(sLF, dLF, DiffEngineLevel.FastImperfect); ArrayList rep = de.DiffReport(); string[] res = getResults(dLF, rep); if (res != null) { string[] filesplit = dFile.Split('\\'); string filename = filesplit.Last(); File.WriteAllLines(folderPath + "\\SetItUp_Registry\\" + filename, res); } } catch (Exception ex) { string tmp = string.Format("{0}{1}{1}***STACK***{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace); MessageBox.Show(tmp, "Compare Error"); return; } }
protected static string Compare(string value1, string value2) { DiffEngine diffEngine = new DiffEngine(); value1 = StringUtil.RemoveTags(value1); value2 = StringUtil.RemoveTags(value2); DiffListHtml source = new DiffListHtml(value1); DiffListHtml destination = new DiffListHtml(value2); diffEngine.ProcessDiff(source, destination, DiffEngineLevel.SlowPerfect); System.Collections.ArrayList arrayList = diffEngine.DiffReport(); System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(); for (int i = 0; i < arrayList.Count; i++) { DiffResultSpan diffResultSpan = arrayList[i] as DiffResultSpan; if (diffResultSpan != null) { switch (diffResultSpan.Status) { case DiffResultSpanStatus.NoChange: Append(stringBuilder, value1, diffResultSpan.SourceIndex, diffResultSpan.Length); break; case DiffResultSpanStatus.Replace: Append(stringBuilder, value1, diffResultSpan.SourceIndex, diffResultSpan.Length, "green"); Append(stringBuilder, value2, diffResultSpan.DestIndex, diffResultSpan.Length, "red; text-decoration:line-through;font-weight:bold"); break; case DiffResultSpanStatus.DeleteSource: Append(stringBuilder, value1, diffResultSpan.SourceIndex, diffResultSpan.Length, "green;font-weight:bold"); break; case DiffResultSpanStatus.AddDestination: Append(stringBuilder, value2, diffResultSpan.DestIndex, diffResultSpan.Length, "red; text-decoration:line-through;font-weight:bold"); break; } } } return(stringBuilder.ToString()); }
private void TextDiff(string oldText, string newText) { this.Cursor = Cursors.WaitCursor; DiffList_TextFile sLF = null; DiffList_TextFile dLF = null; try { sLF = new DiffList_TextFile(oldText); dLF = new DiffList_TextFile(newText); } catch (Exception ex) { this.Cursor = Cursors.Default; MessageBox.Show(ex.Message, "File Error"); return; } try { double time = 0; DiffEngine de = new DiffEngine(); time = de.ProcessDiff(sLF, dLF, _level); ArrayList rep = de.DiffReport(); CompareFiles(sLF, dLF, rep, time); } catch (Exception ex) { this.Cursor = Cursors.Default; string tmp = string.Format("{0}{1}{1}***STACK***{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace); MessageBox.Show(tmp, "Compare Error"); return; } this.Cursor = Cursors.Default; }
public static string CompareTextFiles(string sFile, string dFile) { DiffList_TextFile sLF = null; DiffList_TextFile dLF = null; bool IsEqual = true; string reportStr = null; try { sLF = new DiffList_TextFile(sFile); dLF = new DiffList_TextFile(dFile); } catch (Exception ex) { throw new Exception(ex.Message); } try { double time = 0; DiffEngine de = new DiffEngine(); time = de.ProcessDiff(sLF, dLF, DiffEngineLevel.FastImperfect); ArrayList rep = de.DiffReport(); CompareReport theReports = Results(sLF, dLF, rep, time, out IsEqual); if (!IsEqual) { reportStr = CompareReports(sFile, dFile, theReports); } } catch (Exception ex) { throw new Exception(ex.Message); } return(reportStr); }
/// <summary> /// Compares two modules of the same ID but different versions. /// </summary> /// <param name="oldModule"></param> /// <param name="newModule"></param> /// <returns> /// A changelog of the two modules to compare. /// </returns> public static String CompareModules(Modul oldModule, Modul newModule) { DiffEngine engine = new DiffEngine(); String toReturn = ""; List <ModulPartDescription> oldDescr, newDescr; ArrayList result; if (oldModule.ModulID == newModule.ModulID) { if (oldModule.Version > newModule.Version) { newDescr = (List <ModulPartDescription>)oldModule.Descriptions; oldDescr = (List <ModulPartDescription>)newModule.Descriptions; } else { oldDescr = (List <ModulPartDescription>)oldModule.Descriptions; newDescr = (List <ModulPartDescription>)newModule.Descriptions; } for (int i = 0; i < oldDescr.Count; i++) { engine.ProcessDiff(new DiffList_CharData(oldDescr[i].Description), new DiffList_CharData(newDescr[i].Description)); result = engine.DiffReport(); foreach (DiffResultSpan diff in result) { toReturn = toReturn + diff.ToString(); } } } else { toReturn = "Not the same module!"; } return(toReturn); }
/// <summary> /// The get source diff from strings. /// </summary> /// <param name="source"> /// The source. /// </param> /// <param name="destination"> /// The destination. /// </param> /// <param name="level"> /// The level. /// </param> /// <returns> /// The <see cref="ArrayList"/>. /// </returns> public static ArrayList GetSourceDiffFromStrings(string source, string destination, DiffEngineLevel level = DiffEngineLevel.SlowPerfect) { var sourceseparator = "\n"; if (source.Contains("\r\n")) { sourceseparator = "\r\n"; } var destinationsepartor = "\n"; if (destination.Contains("\r\n")) { destinationsepartor = "\r\n"; } var sLf = new DiffListTextFile(source, sourceseparator); var dLf = new DiffListTextFile(destination, destinationsepartor); var de = new DiffEngine(); de.ProcessDiff(sLf, dLf, level); return(de.DiffReport()); }
public void TextDiff(string Source, bool SourceIsFile, string Destination, bool DestinationIsFile) { using (new DisposableState(this, null)) { DiffListText sLF = null; DiffListText dLF = null; try { sLF = new DiffListText(Source, SourceIsFile); dLF = new DiffListText(Destination, DestinationIsFile); } catch (Exception ex) { MessageBox.Show(ex.Message, "File Error"); return; } try { double time = 0; DiffEngine de = new DiffEngine(); time = de.ProcessDiff(sLF, dLF, DiffEngineLevel.SlowPerfect); List <DiffResultSpan> rep = de.DiffReport(); ShowDiff(sLF, dLF, rep, time); } catch (Exception ex) { string tmp = string.Format("{0}{1}{1}***STACK***{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace); MessageBox.Show(tmp, "Compare Error"); } } }
private void TextDiff(string sFile, string dFile) { _level = DiffEngineLevel.SlowPerfect; this.Cursor = Cursors.WaitCursor; DiffList_TextFile sLF = null; DiffList_TextFile dLF = null; try { sLF = new DiffList_TextFile(sFile); dLF = new DiffList_TextFile(dFile); } catch (Exception ex) { this.Cursor = Cursors.Default; MessageBox.Show(ex.Message, "File Error"); return; } try { double time = 0; DiffEngine de = new DiffEngine(); time = de.ProcessDiff(sLF, dLF, _level); ArrayList rep = de.DiffReport(); Results dlg = new Results(sLF, dLF, rep, time); // dlg.MdiParent = this.MdiParent; dlg.Text = Path.GetFileName(sFile); dlg.ShowDialog(); dlg.Dispose(); } catch (Exception ex) { this.Cursor = Cursors.Default; string tmp = string.Format("{0}{1}{1}***STACK***{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace); MessageBox.Show(tmp, "Compare Error"); return; } this.Cursor = Cursors.Default; }
/// <summary> /// 比較結果を取得します。 /// </summary> /// <param name="level">比較レベル</param> /// <returns>比較結果</returns> public DiffReport Report(DiffEngineLevel level = DiffEngineLevel.SlowPerfect) { CheckFileExist(SourcePath); CheckFileExist(DestinationPath); var d1 = new TextLineCollection(SourcePath); var d2 = new TextLineCollection(DestinationPath); DiffEngine de = new DiffEngine(); de.ProcessDiff(d1, d2, level); return de.DiffReport(); }
private void CompareArticles(string ver1, string ver2, ref string leftRes, ref string rightRes) { System.Text.StringBuilder res1 = new System.Text.StringBuilder(); System.Text.StringBuilder res2 = new System.Text.StringBuilder(); DiffList_StringData s1 = new DiffList_StringData(ver1); DiffList_StringData s2 = new DiffList_StringData(ver2); DiffEngine de = new DiffEngine(); ArrayList rep; int i = 0, count1 = 1, count2 = 1; int linesupdated = 0, linesadded = 0, linesdeleted = 0; int symbolschanged = 0, symbolsadded = 0, symbolsdeleted = 0; de.ProcessDiff(s1, s2, DiffEngineLevel.SlowPerfect); rep = de.DiffReport(); foreach (DiffResultSpan drs in rep) { switch (drs.Status) { case DiffResultSpanStatus.DeleteSource: res1.Append("<a name=\"left_" + count1.ToString("0000") + "\">" + "</a>\r\n"); for (i = 0; i < drs.Length; i++) { res1.Append(count1.ToString("0000") + " "); res1.Append(" <span style=\"background-color: #FF7863\">"); res1.Append(((TextLine)s1.GetByIndex(drs.SourceIndex + i)).Line + "<br />"); res1.Append("</span>"); res2.Append("<br />"); count1++; linesdeleted++; } break; case DiffResultSpanStatus.NoChange: for (i = 0; i < drs.Length; i++) { res1.Append(count1.ToString("0000") + " "); res2.Append(count2.ToString("0000") + " "); res1.Append(" "); res2.Append(" "); res1.Append(((TextLine)s1.GetByIndex(drs.SourceIndex + i)).Line + "<br />"); res2.Append(((TextLine)s2.GetByIndex(drs.DestIndex + i)).Line + "<br />"); count1++; count2++; } break; case DiffResultSpanStatus.AddDestination: res1.Append("<a name=\"right_" + count2.ToString("0000") + "\">" + "</a>\r\n"); for (i = 0; i < drs.Length; i++) { res1.Append("<br />"); res2.Append(count2.ToString("0000") + " "); res2.Append(" <span style=\"background-color: #54C954\">"); res2.Append(((TextLine)s2.GetByIndex(drs.DestIndex + i)).Line + "<br />"); res2.Append("</span>"); count2++; linesadded++; } break; case DiffResultSpanStatus.Replace: res1.Append("<a name=\"left_" + count1.ToString("0000") + "\">" + "</a>\r\n"); for (i = 0; i < drs.Length; i++) { res1.Append(count1.ToString("0000") + " "); res2.Append(count2.ToString("0000") + " "); res1.Append(" <span style=\"background-color: #C1E5FF\">"); res2.Append(" <span style=\"background-color: #C1E5FF\">"); CompareLines(((TextLine)s1.GetByIndex(drs.SourceIndex + i)).Line, ((TextLine)s2.GetByIndex(drs.DestIndex + i)).Line, res1, res2, ref symbolschanged, ref symbolsadded, ref symbolsdeleted); res1.Append("</span>"); res2.Append("</span>"); res1.Append("<br />"); res2.Append("<br />"); count1++; count2++; linesupdated++; } break; } } CollectLinesStats(linesupdated, linesadded, linesdeleted); CollectSymbolsStats(symbolschanged, symbolsadded, symbolsdeleted); leftRes = res1.ToString(); rightRes = res2.ToString(); }
/// <summary> /// Runs the diff process for the specified files. /// </summary> public void CompareDifference(Object sender, DoWorkEventArgs e) { this.sourceListView.Items.Clear(); this.destinationListView.Items.Clear(); this.sourceListView.BeginUpdate(); this.destinationListView.BeginUpdate(); try { String source = ((String[])e.Argument)[0]; String destination = ((String[])e.Argument)[1]; Int32 count = 1; ListViewItem lviS, lviD; TextFile file1 = new TextFile(source); TextFile file2 = new TextFile(destination); DiffEngine de = new DiffEngine(); de.ProcessDiff(file1, file2, DiffEngineLevel.Medium); ArrayList lines = de.DiffReport(); Int32 counter = 0, total = lines.Count; foreach (DiffResultSpan drs in lines) { switch (drs.Status) { case DiffResultSpanStatus.DeleteSource: for (Int32 i = 0; i < drs.Length; i++) { lviS = new ListViewItem(count.ToString("00000")); lviD = new ListViewItem(count.ToString("00000")); lviS.Font = lviD.Font = new Font("Courier New", 8.25F); lviS.BackColor = Color.Red; lviS.SubItems.Add(((TextLine)file1.GetByIndex(drs.SourceIndex + i)).Line); lviD.BackColor = Color.LightGray; lviD.SubItems.Add(""); this.sourceListView.Items.Add(lviS); this.destinationListView.Items.Add(lviD); count++; } break; case DiffResultSpanStatus.NoChange: for (Int32 i = 0; i < drs.Length; i++) { lviS = new ListViewItem(count.ToString("00000")); lviD = new ListViewItem(count.ToString("00000")); lviS.Font = lviD.Font = new Font("Courier New", 8.25F); lviS.BackColor = Color.White; lviS.SubItems.Add(((TextLine)file1.GetByIndex(drs.SourceIndex + i)).Line); lviD.BackColor = Color.White; lviD.SubItems.Add(((TextLine)file2.GetByIndex(drs.DestIndex + i)).Line); this.sourceListView.Items.Add(lviS); this.destinationListView.Items.Add(lviD); count++; } break; case DiffResultSpanStatus.AddDestination: for (Int32 i = 0; i < drs.Length; i++) { lviS = new ListViewItem(count.ToString("00000")); lviD = new ListViewItem(count.ToString("00000")); lviS.Font = lviD.Font = new Font("Courier New", 8.25F); lviS.BackColor = Color.LightGray; lviS.SubItems.Add(""); lviD.BackColor = Color.LightGreen; lviD.SubItems.Add(((TextLine)file2.GetByIndex(drs.DestIndex + i)).Line); this.sourceListView.Items.Add(lviS); this.destinationListView.Items.Add(lviD); count++; } break; case DiffResultSpanStatus.Replace: for (Int32 i = 0; i < drs.Length; i++) { lviS = new ListViewItem(count.ToString("00000")); lviD = new ListViewItem(count.ToString("00000")); lviS.Font = lviD.Font = new Font("Courier New", 8.25F); lviS.BackColor = Color.Red; lviS.SubItems.Add(((TextLine)file1.GetByIndex(drs.SourceIndex + i)).Line); lviD.BackColor = Color.LightGreen; lviD.SubItems.Add(((TextLine)file2.GetByIndex(drs.DestIndex + i)).Line); this.sourceListView.Items.Add(lviS); this.destinationListView.Items.Add(lviD); count++; } break; } counter++; Int32 percent = (100 * counter) / total; this.backgroundWorker.ReportProgress(percent); } e.Result = true; } catch (Exception ex) { MessageBox.Show(ex.ToString()); e.Result = false; } this.destinationListView.EndUpdate(); this.sourceListView.EndUpdate(); }
/// <summary> /// 比較結果を取得します。 /// </summary> /// <param name="source">比較元文字列</param> /// <param name="destination">比較先文字列</param> /// <param name="level">比較レベル</param> /// <returns>比較結果</returns> public static DiffReport Report(string source, string destination, DiffEngineLevel level = DiffEngineLevel.SlowPerfect) { var d1 = new StringLineCollection(source); var d2 = new StringLineCollection(destination); DiffEngine de = new DiffEngine(); de.ProcessDiff(d1, d2, level); return de.DiffReport(); }
private void cmdCompare2_Click(object sender, RoutedEventArgs e) { txtResult.TextArea.TextView.LineTransformers.Clear(); var cp1 = new DiffList_String(textEditorA.Text); var cp2 = new DiffList_String(textEditorB.Text); var df = new DiffEngine(); df.ProcessDiff(cp1, cp2, DiffEngineLevel.SlowPerfect); var result = df.DiffReport(); var txt = ""; //diff_match_patch comparer=new diff_match_patch(); //var result = comparer.diff_main(textEditorA.Text, textEditorB.Text); txtResult.Document.Text = ""; foreach (var diff in result) { if (diff.Status == DiffResultSpanStatus.AddDestination) { var st = txt.Length; for (int i = diff.DestIndex; i < diff.DestIndex + diff.Length; i++) { txt += ((TextLine)cp2.GetByIndex(i)).Line + Environment.NewLine;; } var stp = txt.Length; txtResult.TextArea.TextView.LineTransformers.Add(new TextColorizer(st, stp, Brushes.Green)); } else if (diff.Status == DiffResultSpanStatus.DeleteSource) { var st = txt.Length; for (int i = diff.SourceIndex; i < diff.SourceIndex + diff.Length; i++) { txt += ((TextLine)cp1.GetByIndex(i)).Line + Environment.NewLine;; } var stp = txt.Length; txtResult.TextArea.TextView.LineTransformers.Add(new TextColorizer(st, stp, Brushes.Red)); } else if (diff.Status == DiffResultSpanStatus.Replace) { var st = txt.Length; for (int i = diff.DestIndex; i < diff.DestIndex + diff.Length; i++) { txt += ((TextLine)cp2.GetByIndex(i)).Line + Environment.NewLine; } var stp = txt.Length; txtResult.TextArea.TextView.LineTransformers.Add(new TextColorizer(st, stp, Brushes.Orange)); } else { for (int i = diff.SourceIndex; i < diff.Length; i++) { txt += ((TextLine)cp1.GetByIndex(i)).Line + Environment.NewLine;; } } } txtResult.Document.Text = txt; }
public void Process(string[] pages, string[,] replaces, WikiProcessor[] procs, WikiErrorCheck[] checks, bool preview, bool save, WikiMedia.ExportNotify notify) { Regex[] re = CalcRegex(replaces); foreach (string pageName in pages) { if (!String.IsNullOrEmpty(pageName)) { Page page = new Page(site, pageName); page.Load(); if (!String.IsNullOrEmpty(page.text)) { string text = page.text.Trim(); page.text = text; text = DoRegex(page.text, re, replaces); WikiDocument doc = new WikiDocument(pageName, text); foreach (WikiProcessor p in procs) { p.Process(doc); } foreach (WikiErrorCheck check in checks) { check.Process(doc, notify); } text = doc.ToString().Trim(); if (!text.Equals(page.text)) { notify("W\t" + pageName + "\tchanged \n"); if (preview) { char[] sep = { '\n' }; IDiffList oldText = new DiffList_String(page.text, sep); IDiffList newText = new DiffList_String(text, sep); double time = 0; DiffEngine de = new DiffEngine(); time = de.ProcessDiff(oldText, newText); ArrayList rep = de.DiffReport(); Results dlg = new Results(oldText, newText, rep, time); dlg.Size = new Size(1000, 700); dlg.ShowInTaskbar = false; dlg.StartPosition = FormStartPosition.Manual; dlg.ShowDialog(); dlg.Dispose(); } if (save) { try { page.text = text; page.Save(); } catch (Exception e) { notify("E\t" + pageName + "\t" + e.Message + "\n"); } } } else { notify("N\t" + pageName + "\t\n"); } } else { notify("I\t" + pageName + "\t\n"); } } } }
private void treeView_AfterSelect(object sender, TreeViewEventArgs e) { this.lvSource.Items.Clear(); this.lvDestination.Items.Clear(); ChangeInfo info = (ChangeInfo)this.treeView.SelectedNode.Tag; DiffList_TextString source = new DiffList_TextString(info.Original != null ? info.Original.Documentation : String.Empty); DiffList_TextString destination = new DiffList_TextString(info.Change != null ? info.Change.Documentation : String.Empty); DiffEngine de = new DiffEngine(); de.ProcessDiff(source, destination, DiffEngineLevel.SlowPerfect); ArrayList DiffLines = de.DiffReport(); ListViewItem lviS; ListViewItem lviD; int cnt = 1; int i; foreach (DiffResultSpan drs in DiffLines) { switch (drs.Status) { case DiffResultSpanStatus.DeleteSource: for (i = 0; i < drs.Length; i++) { lviS = new ListViewItem(cnt.ToString("00000")); lviD = new ListViewItem(cnt.ToString("00000")); lviS.BackColor = Color.Red; lviS.SubItems.Add(((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line); lviD.BackColor = Color.LightGray; lviD.SubItems.Add(""); lvSource.Items.Add(lviS); lvDestination.Items.Add(lviD); cnt++; } break; case DiffResultSpanStatus.NoChange: for (i = 0; i < drs.Length; i++) { lviS = new ListViewItem(cnt.ToString("00000")); lviD = new ListViewItem(cnt.ToString("00000")); lviS.BackColor = Color.White; lviS.SubItems.Add(((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line); lviD.BackColor = Color.White; lviD.SubItems.Add(((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line); lvSource.Items.Add(lviS); lvDestination.Items.Add(lviD); cnt++; } break; case DiffResultSpanStatus.AddDestination: for (i = 0; i < drs.Length; i++) { lviS = new ListViewItem(cnt.ToString("00000")); lviD = new ListViewItem(cnt.ToString("00000")); lviS.BackColor = Color.LightGray; lviS.SubItems.Add(""); lviD.BackColor = Color.LightGreen; lviD.SubItems.Add(((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line); lvSource.Items.Add(lviS); lvDestination.Items.Add(lviD); cnt++; } break; case DiffResultSpanStatus.Replace: for (i = 0; i < drs.Length; i++) { lviS = new ListViewItem(cnt.ToString("00000")); lviD = new ListViewItem(cnt.ToString("00000")); lviS.BackColor = Color.Red; lviS.SubItems.Add(((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line); lviD.BackColor = Color.LightGreen; lviD.SubItems.Add(((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line); lvSource.Items.Add(lviS); lvDestination.Items.Add(lviD); cnt++; } break; } } this.radioButtonOriginal.Checked = !this.treeView.SelectedNode.Checked; this.radioButtonChange.Checked = this.treeView.SelectedNode.Checked; }
private void lwDatabaseObjects_Click(object sender, EventArgs e) { //MessageBox.Show(lwDatabaseObjects.SelectedItems[0].SubItems[0].Text + "." + lwDatabaseObjects.SelectedItems[0].SubItems[2].Text); try { lvDestination.Items.Clear(); lvSource.Items.Clear(); DataView dwObjectDefinition = new DataView(this.dbOjects, "Type='" + lwDatabaseObjects.SelectedItems[0].SubItems[0].Text + "' AND " + "Name='" + lwDatabaseObjects.SelectedItems[0].SubItems[2].Text + "'", "Name", DataViewRowState.CurrentRows); foreach (DataRowView dr in dwObjectDefinition) { //ObjectDefinition1 string objDef1 = dr["ObjectDefinition1"].ToString(); string objDef2 = dr["ObjectDefinition2"].ToString(); DiffList_TextFile sLF = null; DiffList_TextFile dLF = null; sLF = new DiffList_TextFile(objDef1); dLF = new DiffList_TextFile(objDef2); double time = 0; DiffEngine de = new DiffEngine(); time = de.ProcessDiff(sLF, dLF, DiffEngineLevel.SlowPerfect); ArrayList rep = de.DiffReport(); ListViewItem lviS; ListViewItem lviD; int cnt = 1; int i; foreach (DiffResultSpan drs in rep) { switch (drs.Status) { case DiffResultSpanStatus.DeleteSource: for (i = 0; i < drs.Length; i++) { lviS = new ListViewItem(cnt.ToString("00000")); lviD = new ListViewItem(cnt.ToString("00000")); lviS.BackColor = Color.Red; lviS.SubItems.Add(((TextLine)sLF.GetByIndex(drs.SourceIndex + i)).Line); lviD.BackColor = Color.LightGray; lviD.SubItems.Add(""); lvSource.Items.Add(lviS); lvDestination.Items.Add(lviD); cnt++; } break; case DiffResultSpanStatus.NoChange: for (i = 0; i < drs.Length; i++) { lviS = new ListViewItem(cnt.ToString("00000")); lviD = new ListViewItem(cnt.ToString("00000")); lviS.BackColor = Color.White; lviS.SubItems.Add(((TextLine)sLF.GetByIndex(drs.SourceIndex + i)).Line); lviD.BackColor = Color.White; lviD.SubItems.Add(((TextLine)dLF.GetByIndex(drs.DestIndex + i)).Line); lvSource.Items.Add(lviS); lvDestination.Items.Add(lviD); cnt++; } break; case DiffResultSpanStatus.AddDestination: for (i = 0; i < drs.Length; i++) { lviS = new ListViewItem(cnt.ToString("00000")); lviD = new ListViewItem(cnt.ToString("00000")); lviS.BackColor = Color.LightGray; lviS.SubItems.Add(""); lviD.BackColor = Color.LightGreen; lviD.SubItems.Add(((TextLine)dLF.GetByIndex(drs.DestIndex + i)).Line); lvSource.Items.Add(lviS); lvDestination.Items.Add(lviD); cnt++; } break; case DiffResultSpanStatus.Replace: for (i = 0; i < drs.Length; i++) { lviS = new ListViewItem(cnt.ToString("00000")); lviD = new ListViewItem(cnt.ToString("00000")); lviS.BackColor = Color.Red; lviS.SubItems.Add(((TextLine)sLF.GetByIndex(drs.SourceIndex + i)).Line); lviD.BackColor = Color.LightGreen; lviD.SubItems.Add(((TextLine)dLF.GetByIndex(drs.DestIndex + i)).Line); lvSource.Items.Add(lviS); lvDestination.Items.Add(lviD); cnt++; } break; } } /* * lvSource.Items.Clear(); * using (StringReader sr = new StringReader(objDef1)) * { * string line; * while ((line = sr.ReadLine()) != null) * { * lvSource.Items.Add(line); * } * } * lvDestination.Items.Clear(); * using (StringReader sr = new StringReader(objDef2)) * { * string line; * while ((line = sr.ReadLine()) != null) * { * this.lvDestination.Items.Add(line); * } * }*/ } } catch (Exception err) { string error = err.Message; } }
public override void Run() { var wb = Workbench.Instance; if (wb == null) { return; } var ed = wb.ActiveEditor; if (ed == null) { return; } var edSvc = ed.EditorService; TextFileDiffList sLF = null; TextFileDiffList dLF = null; string sourceFile = null; string targetFile = null; try { ed.SyncSessionCopy(); var set = XmlCompareUtil.PrepareForComparison(edSvc.CurrentConnection.ResourceService, edSvc.ResourceID, edSvc.EditedResourceID); sLF = set.Source; dLF = set.Target; } catch (Exception ex) { ErrorDialog.Show(ex); return; } finally { try { File.Delete(sourceFile); } catch { } try { File.Delete(targetFile); } catch { } } try { double time = 0; DiffEngine de = new DiffEngine(); time = de.ProcessDiff(sLF, dLF, DiffEngineLevel.SlowPerfect); var rep = de.DiffReport(); TextDiffView dlg = new TextDiffView(sLF, dLF, rep, time); dlg.SetLabels(edSvc.ResourceID, Strings.EditedResource); dlg.ShowDialog(); dlg.Dispose(); } catch (Exception ex) { string nl = Environment.NewLine; string tmp = $"{ex.Message}{nl}{nl}***STACK***{nl}{ex.StackTrace}"; MessageBox.Show(tmp, Strings.CompareError); return; } }
private void PrepareDiff(string[] left, string[] right, List <LineRecord> leftrecs, List <LineRecord> rightrecs) { DiffList_TextFile file1 = new DiffList_TextFile(left); DiffList_TextFile file2 = new DiffList_TextFile(right); DiffEngine engine = new DiffEngine(); engine.ProcessDiff(file1, file2, DiffEngineLevel.SlowPerfect); ArrayList report = engine.DiffReport(); foreach (DiffResultSpan dres in report) { switch (dres.Status) { case DiffResultSpanStatus.NoChange: for (int i = 0; i < dres.Length; i++) { LineRecord lrec = new LineRecord(i + dres.SourceIndex, left[i + dres.SourceIndex]); leftrecs.Add(lrec); LineRecord rrec = new LineRecord(i + dres.DestIndex, right[i + dres.DestIndex]); rightrecs.Add(rrec); } break; case DiffResultSpanStatus.Replace: for (int i = 0; i < dres.Length; i++) { LineRecord lrec = new LineRecord(i + dres.SourceIndex, left[i + dres.SourceIndex]); lrec.BackColor = Color.Khaki; leftrecs.Add(lrec); LineRecord rrec = new LineRecord(i + dres.DestIndex, right[i + dres.DestIndex]); rrec.BackColor = Color.Khaki; rightrecs.Add(rrec); ComputeLineDifferences(lrec, rrec); } break; case DiffResultSpanStatus.DeleteSource: for (int i = 0; i < dres.Length; i++) { LineRecord lrec = new LineRecord(i + dres.SourceIndex, left[i + dres.SourceIndex]); lrec.BackColor = Color.Khaki; leftrecs.Add(lrec); LineRecord rrec = new LineRecord(); rrec.BackColor = Color.LightGray; rightrecs.Add(rrec); } break; case DiffResultSpanStatus.AddDestination: for (int i = 0; i < dres.Length; i++) { LineRecord lrec = new LineRecord(); lrec.BackColor = Color.LightGray; leftrecs.Add(lrec); LineRecord rrec = new LineRecord(i + dres.DestIndex, right[i + dres.DestIndex]); rrec.BackColor = Color.Khaki; rightrecs.Add(rrec); } break; default: break; } // switch } // foreach }