示例#1
0
 public string TestDiffWith(string text1, string text2, TextDivisions type, int expectedStart, int expectedLength)
 {
     var(start, length, difference) = text1.DiffWith(text2, type);
     Assert.That(start, Is.EqualTo(expectedStart));
     Assert.That(length, Is.EqualTo(expectedLength));
     return(difference);
 }
示例#2
0
        private static (int start, int length, string difference) LineDiff(string text1, string text2, TextDivisions type)
        {
            int    start = -1, length = 0, s;
            string diff = null;

            (start, length, diff) = DiffElements(text1, text2, "\r\n");
            if (start >= 0 && type != TextDivisions.Line)
            {
                (s, length, diff) = WordDiff(text1.Substring(start, length), text2.Substring(start), type);
                start            += s;
            }
            return(start, length, diff);
        }
示例#3
0
        private static (int start, int length, string difference) WordDiff(string line1, string line2, TextDivisions type)
        {
            int    start = -1, length, s;
            string diff = null;

            (start, length, diff) = DiffElements(line1, line2, " ");
            if (start >= 0 && type != TextDivisions.Word)
            {
                (s, length, diff) = LetterDiff(line1.Substring(start, length), line2.Substring(start));
                start            += s;
            }
            return(start, length, diff);
        }
示例#4
0
        /// <summary>
        /// Determines the difference between the target instance and another string.
        /// </summary>
        /// <param name="target">The extension method target.</param>
        /// <param name="other">The string to compare to.</param>
        /// <param name="type">The type of comparison to perform.</param>
        /// <returns>The start index and length within the target instance and the content from the comparison string that differs
        /// from the target instance; or if no difference is found, an index of <c>-1</c>.</returns>
        public static (int start, int length, string difference) DiffWith(this string target, string other, TextDivisions type = TextDivisions.Line)
        {
            if (target == null)
            {
                throw new NullReferenceException();
            }

            if (other == null)
            {
                throw new ArgumentNullException("other");
            }

            if (type == TextDivisions.Line || target.IndexOf('\n') >= 0)
            {
                return(LineDiff(target, other, type));
            }
            if (type == TextDivisions.Word || target.IndexOf(' ') >= 0)
            {
                return(WordDiff(target, other, type));
            }
            return(LetterDiff(target, other));
        }