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 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 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; }