private void textBoxNew_TextChanged(object sender, EventArgs e) { string s = this.textBoxOriginal.Text, t = ScriptCorrector.RemoveHTML(this.textBoxNew.Text); List <string> diff = Diff(s, t); this.richTextBoxCompare.Text = ""; foreach (string d in diff) { string action = d.Substring(0, 1); string text = d.Substring(1); Color color = Color.Black; if (action == "+") { color = Color.DarkGreen; } else if (action == "-") { color = Color.DarkRed; } AppendText(text, color, color != Color.Black); } this.richTextBoxCompare.BackColor = (s == t) ? SystemColors.ControlLight : SystemColors.Control; this.buttonOk.Text = (originalText == this.textBoxNew.Text) ? "Next" : "OK"; }
internal static List <string> Diff(string s, string t) { int[,] d; ScriptCorrector.StringEditDistance(s, t, out d); List <string> list = new List <string>(); string current = ""; string currentOp = "="; int i = d.GetLength(0) - 1, j = d.GetLength(1) - 1; while (i > 0 && j > 0) { int del = d[i - 1, j], ins = d[i, j - 1], same = d[i - 1, j - 1]; if (s[i - 1] != t[j - 1]) { same += 1000; } if (currentOp == "=" && same <= ins && same <= del) { i--; j--; if (s[i] != t[j]) { } current = s[i] + current; continue; } if (currentOp == "+" && ins <= same && ins <= del) { j--; current = t[j] + current; continue; } if (currentOp == "-" && del <= ins && del <= same) { i--; current = s[i] + current; continue; } if (current.Length > 0) { list.Insert(0, currentOp + current); current = ""; } if (same <= del && same <= ins) { i--; j--; currentOp = "="; if (s[i] != t[j]) { } current = s[i] + current; continue; } if (ins <= del && ins <= same) { j--; currentOp = "+"; current = t[j] + current; continue; } i--; currentOp = "-"; current = s[i] + current; continue; } while (i > 0) { if (currentOp != "-") { list.Insert(0, currentOp + current); current = ""; currentOp = "-"; } current = s[--i] + current; } while (j > 0) { if (currentOp != "+") { list.Insert(0, currentOp + current); current = ""; currentOp = "+"; } current = t[--j] + current; } if (current.Length > 0) { list.Insert(0, currentOp + current); } return(list); }