// 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 ArrayList DiffReport() { ArrayList retval = new ArrayList(); int dcount = _dest.Count(); int scount = _source.Count(); //Deal with the special case of empty files if (dcount == 0) { if (scount > 0) { retval.Add(DiffResultSpan.CreateDeleteSource(0, scount)); } return(retval); } else { if (scount == 0) { retval.Add(DiffResultSpan.CreateAddDestination(0, dcount)); return(retval); } } _matchList.Sort(); int curDest = 0; int curSource = 0; DiffResultSpan last = null; //Process each match record foreach (DiffResultSpan drs in _matchList) { if ((!AddChanges(retval, curDest, drs.DestIndex, curSource, drs.SourceIndex)) && (last != null)) { last.AddLength(drs.Length); } else { retval.Add(drs); } curDest = drs.DestIndex + drs.Length; curSource = drs.SourceIndex + drs.Length; last = drs; } //Process any tail end data AddChanges(retval, curDest, dcount, curSource, scount); return(retval); }
private bool AddChanges( ArrayList report, int curDest, int nextDest, int curSource, int nextSource) { bool retval = false; int diffDest = nextDest - curDest; int diffSource = nextSource - curSource; int minDiff = 0; if (diffDest > 0) { if (diffSource > 0) { minDiff = Math.Min(diffDest, diffSource); report.Add(DiffResultSpan.CreateReplace(curDest, curSource, minDiff)); if (diffDest > diffSource) { curDest += minDiff; report.Add(DiffResultSpan.CreateAddDestination(curDest, diffDest - diffSource)); } else { if (diffSource > diffDest) { curSource += minDiff; report.Add(DiffResultSpan.CreateDeleteSource(curSource, diffSource - diffDest)); } } } else { report.Add(DiffResultSpan.CreateAddDestination(curDest, diffDest)); } retval = true; } else { if (diffSource > 0) { report.Add(DiffResultSpan.CreateDeleteSource(curSource, diffSource)); retval = true; } } return(retval); }
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 string GetSpanText(DiffResultSpan span, bool fromDest) { IDiffList list; int startIndex; if (fromDest) { list = destination; startIndex = span.DestIndex; } else { list = source; startIndex = span.SourceIndex; } string str = ""; for (int i = 0; i < span.Length; i++) { str += ((TextLine)list.GetByIndex(startIndex + i)).Line + (i < span.Length - 1 ? "\n" : ""); } return(str); }
private void ProcessRange(int destStart, int destEnd, int sourceStart, int sourceEnd) { var curBestIndex = -1; var curBestLength = -1; var maxPossibleDestLength = 0; DiffState curItem = null; DiffState bestItem = null; for (var destIndex = destStart; destIndex <= destEnd; destIndex++) { maxPossibleDestLength = destEnd - destIndex + 1; if (maxPossibleDestLength <= curBestLength) { //we won't find a longer one even if we looked break; } curItem = stateList.GetByIndex(destIndex); if (!curItem.HasValidLength(sourceStart, sourceEnd, maxPossibleDestLength)) { //recalc new best length since it isn't valid or has never been done. GetLongestSourceMatch(curItem, destIndex, destEnd, sourceStart, sourceEnd); } if (curItem.Status == DiffStatus.Matched) { switch (level) { case DiffEngineLevel.FastImperfect: if (curItem.Length > curBestLength) { //this is longest match so far curBestIndex = destIndex; curBestLength = curItem.Length; bestItem = curItem; } //Jump over the match destIndex += curItem.Length - 1; break; case DiffEngineLevel.Medium: if (curItem.Length > curBestLength) { //this is longest match so far curBestIndex = destIndex; curBestLength = curItem.Length; bestItem = curItem; //Jump over the match destIndex += curItem.Length - 1; } break; default: if (curItem.Length > curBestLength) { //this is longest match so far curBestIndex = destIndex; curBestLength = curItem.Length; bestItem = curItem; } break; } } } if (curBestIndex < 0) { //we are done - there are no matches in this span } else { var sourceIndex = bestItem.StartIndex; matchList.Add(DiffResultSpan.CreateNoChange(curBestIndex, sourceIndex, curBestLength)); if (destStart < curBestIndex) { //Still have more lower destination data if (sourceStart < sourceIndex) { //Still have more lower source data // Recursive call to process lower indexes ProcessRange(destStart, curBestIndex - 1, sourceStart, sourceIndex - 1); } } var upperDestStart = curBestIndex + curBestLength; var upperSourceStart = sourceIndex + curBestLength; if (destEnd > upperDestStart) { //we still have more upper dest data if (sourceEnd > upperSourceStart) { //set still have more upper source data // Recursive call to process upper indexes ProcessRange(upperDestStart, destEnd, upperSourceStart, sourceEnd); } } } }