public void FindCommonPrefixLength(string content1, int startIndex1, int length1, string content2, int startIndex2, int length2, int expectedResult) { Substring substring1 = new Substring(content1, new Range(startIndex1, length1)); Substring substring2 = new Substring(content2, new Range(startIndex2, length2)); Assert.AreEqual(expectedResult, substring1.FindCommonPrefixLength(substring2)); }
private static void FastDiff(IList<Diff> diffs, Substring left, Substring right, bool boundRuntime) { // If either document is empty, then the change covers the whole document. if (left.Length == 0 || right.Length == 0) { diffs.Add(new Diff(DiffKind.Change, left.Range, right.Range)); return; } // Reduce the problem size by identifying a common prefix and suffix, if any. int commonPrefixLength = left.FindCommonPrefixLength(right); if (commonPrefixLength != 0) { if (commonPrefixLength == left.Length && commonPrefixLength == right.Length) { diffs.Add(new Diff(DiffKind.NoChange, left.Range, right.Range)); return; } diffs.Add(new Diff(DiffKind.NoChange, new Range(left.Range.StartIndex, commonPrefixLength), new Range(right.Range.StartIndex, commonPrefixLength))); } int commonSuffixLength = left.Extract(commonPrefixLength).FindCommonSuffixLength(right.Extract(commonPrefixLength)); // Now work on the middle part. Substring leftMiddle = left.Extract(commonPrefixLength, left.Length - commonPrefixLength - commonSuffixLength); Substring rightMiddle = right.Extract(commonPrefixLength, right.Length - commonPrefixLength - commonSuffixLength); SlowDiff(diffs, leftMiddle, rightMiddle, boundRuntime); // Tack on the final diff for the common suffix, if any. if (commonSuffixLength != 0) { diffs.Add(new Diff(DiffKind.NoChange, new Range(leftMiddle.Range.EndIndex, commonSuffixLength), new Range(rightMiddle.Range.EndIndex, commonSuffixLength))); } }