public void ReEncodeDiffs() { string s1 = (char)0xE000 + "Peter piper picked > a peck of pickled pepper < went home." + (char)0xE001; string s2 = (char)0xE002 + "Peter parker penned < a peck of pickled peppers > came home." + (char)0xE003; List <Diff> diffList = _dmp.diff_wordMode(s1, s2); _helper = new DMPCWMTHelper(); _helper.ReEncodeDiffs(diffList); foreach (var diff in diffList) { Assert.False(diff.text.Contains("<") || diff.text.Contains(">")); } }
private int[] GetWordChangeCount(string patch) { //The lines starting with "-" or "+" are regarded as changing lines, ignoring the spaces before "-" or "+" Regex deleteReg = new Regex(@"^\s*[-]"); Regex insertReg = new Regex(@"^\s*[+]"); diff_match_patch dmp = new diff_match_patch(); string deleteLines = "", insertLines = ""; int deleteCount = 0, insertCount = 0; string[] lines = patch.Split('\n'); foreach (string line in lines) { //Put all the delete lines together if (deleteReg.IsMatch(line)) { deleteLines += line.Substring(line.IndexOf("-") + 1) + "\n"; } //Put all the insertion lines together if (insertReg.IsMatch(line)) { insertLines += line.Substring(line.IndexOf("+") + 1) + "\n"; } } List <Diff> diffs = dmp.diff_wordMode(deleteLines, insertLines); foreach (Diff diff in diffs) { if (diff.operation.ToString().Equals("DELETE")) { deleteCount += diff.text.Split(' ').Length - 1; } if (diff.operation.ToString().Equals("INSERT")) { insertCount += diff.text.Split(' ').Length - 1; } } int[] ret = new int[2]; ret[0] = deleteCount; ret[1] = insertCount; return(ret); }
public string PrettyDiffFormatter(string s1, string s2) { StringBuilder html = new StringBuilder(); TagParser(ref s1, ref s2); s1 = HttpUtility.HtmlDecode(s1); s2 = HttpUtility.HtmlDecode(s2); DefineStyleDupDict("background:#a0ffa0", "background:#ffa0a0; text-decoration: line-through"); AddSpacesToSpecialTags(ref s1, ref s2); List <Diff> diffList = _diff.diff_wordMode(s1, s2); ReEncodeDiffs(diffList); Diff tempDiff; for (int i = 0; i < diffList.Count(); i++) { string text = diffList.ElementAt(i).text; tempDiff = ReplaceStyledTags(diffList.ElementAt(i)); if (tempDiff != diffList.ElementAt(i)) { diffList.Insert(i + 1, tempDiff); i--; } } if (diffList.Count() > 2) { SplitDiffConcatenator(diffList); } for (int i = 0; i < diffList.Count(); i++) { html.Append(diffList[i].text); } return(html.ToString()); }