public void OptimizeCaseDeleteFirst() { var str1 = "aaa"; var str2 = "bbb"; /* * obj1 a a a * obj2 b b b * - - M + + */ var results = DiffUtil.Diff(str1, str2); results = DiffUtil.Order(results, DiffOrderType.GreedyDeleteFirst); results = DiffUtil.OptimizeCaseDeletedFirst(results); Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(0).Status); Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(1).Status); Assert.AreEqual(DiffStatus.Modified, results.ElementAt(2).Status); Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(3).Status); Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(4).Status); /* * obj1 a a a * obj2 b b b + + M - - */ results = DiffUtil.Diff(str1, str2); results = DiffUtil.Order(results, DiffOrderType.GreedyInsertFirst); results = DiffUtil.OptimizeCaseInsertedFirst(results); Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(0).Status); Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(1).Status); Assert.AreEqual(DiffStatus.Modified, results.ElementAt(2).Status); Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(3).Status); Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(4).Status); /* * obj1 a a a * obj2 b b b * M M M */ results = DiffUtil.Diff(str1, str2); results = DiffUtil.Order(results, DiffOrderType.LazyDeleteFirst); results = DiffUtil.OptimizeCaseDeletedFirst(results); Assert.AreEqual(DiffStatus.Modified, results.ElementAt(0).Status); Assert.AreEqual(DiffStatus.Modified, results.ElementAt(1).Status); Assert.AreEqual(DiffStatus.Modified, results.ElementAt(2).Status); /* * obj1 a a a * obj2 b b b * M M M */ results = DiffUtil.Diff(str1, str2); results = DiffUtil.Order(results, DiffOrderType.LazyInsertFirst); results = DiffUtil.OptimizeCaseInsertedFirst(results); Assert.AreEqual(DiffStatus.Modified, results.ElementAt(0).Status); Assert.AreEqual(DiffStatus.Modified, results.ElementAt(1).Status); Assert.AreEqual(DiffStatus.Modified, results.ElementAt(2).Status); }
private IEnumerable <DiffResult <string> > DiffCellValue(IEnumerable <string> src, IEnumerable <string> dst) { var r = DiffUtil.Diff(src, dst); r = DiffUtil.Order(r, DiffOrderType.LazyDeleteFirst); return(DiffUtil.OptimizeCaseDeletedFirst(r)); }
private void UpdateValueDiff(string srcValue, string dstValue) { SrcValueTextBox.Document.Blocks.First().ContentStart.Paragraph.Inlines.Clear(); DstValueTextBox.Document.Blocks.First().ContentStart.Paragraph.Inlines.Clear(); var srcLines = srcValue.Split('\n').Select(s => s.TrimEnd()); var dstLines = dstValue.Split('\n').Select(s => s.TrimEnd()); var lineDiffResults = DiffCellValue(srcLines, dstLines).ToList(); var srcRange = new List <Tuple <string, Color?> >(); var dstRange = new List <Tuple <string, Color?> >(); foreach (var lineDiffResult in lineDiffResults) { if (lineDiffResult.Status == DiffStatus.Equal) { DiffEqualLine(lineDiffResult, srcRange); DiffEqualLine(lineDiffResult, dstRange); } else if (lineDiffResult.Status == DiffStatus.Modified) { var charDiffResults = DiffUtil.Diff(lineDiffResult.Obj1, lineDiffResult.Obj2); charDiffResults = DiffUtil.Order(charDiffResults, DiffOrderType.LazyDeleteFirst); charDiffResults = DiffUtil.OptimizeCaseDeletedFirst(charDiffResults); DiffModifiedLine(charDiffResults.Where(r => r.Status != DiffStatus.Inserted), srcRange, true); DiffModifiedLine(charDiffResults.Where(r => r.Status != DiffStatus.Deleted), dstRange, false); } else if (lineDiffResult.Status == DiffStatus.Deleted) { DiffDeletedLine(lineDiffResult, srcRange, true); DiffDeletedLine(lineDiffResult, dstRange, false); } else if (lineDiffResult.Status == DiffStatus.Inserted) { DiffInsertedLine(lineDiffResult, srcRange, true); DiffInsertedLine(lineDiffResult, dstRange, false); } } foreach (var r in srcRange) { var bc = r.Item2.HasValue ? new SolidColorBrush(r.Item2.Value) : new SolidColorBrush(); SrcValueTextBox.Document.Blocks.First().ContentStart.Paragraph.Inlines.Add(new Run(r.Item1) { Background = bc }); } foreach (var r in dstRange) { var bc = r.Item2.HasValue ? new SolidColorBrush(r.Item2.Value) : new SolidColorBrush(); DstValueTextBox.Document.Blocks.First().ContentStart.Paragraph.Inlines.Add(new Run(r.Item1) { Background = bc }); } }
public void Equal_LazyInsertFirst() { var str1 = "abcde"; var str2 = "abcde"; var results = DiffUtil.Diff(str1, str2); results = DiffUtil.Order(results, DiffOrderType.LazyInsertFirst); Assert.AreEqual(str1.Count(), results.Count()); Assert.IsTrue(results.All(r => r.Status == DiffStatus.Equal)); Assert.IsTrue(results.Select(r => r.Obj1).SequenceEqual(str1.Select(c => c))); Assert.IsTrue(results.Select(r => r.Obj2).SequenceEqual(str1.Select(c => c))); }
public void Restore_GreedyInsertFirst() { var str1 = "q4DU8sbeD4JdhFA4hWShCv3bbtD7djX5SaNnQUHJHdCEJs6X2LJipbEEr7bZZbzcUrpuKpRDKNz92x5P"; var str2 = "3GKLWNDdCxip8kda2r2MUT45RrHUiESQhmhUZtMcpBGcSwJVS9uq4DWBAQk2zPUJCJabaeWuP5mxyPBz"; var results = DiffUtil.Diff(str1, str2); results = DiffUtil.Order(results, DiffOrderType.GreedyInsertFirst); var src = new string(DiffUtil.CreateSrc(results).ToArray()); var dst = new string(DiffUtil.CreateDst(results).ToArray()); Assert.AreEqual(dst, str2); Assert.AreEqual(src, str1); }
public void CaseRepeat_GreedyDeleteFirst() { string str1 = "abbbc"; string str2 = "adbbc"; var results = DiffUtil.Diff(str1, str2); results = DiffUtil.Order(results, DiffOrderType.GreedyDeleteFirst); Assert.AreEqual(DiffStatus.Equal, results.ElementAt(0).Status); Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(1).Status); Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(2).Status); Assert.AreEqual(DiffStatus.Equal, results.ElementAt(3).Status); Assert.AreEqual(DiffStatus.Equal, results.ElementAt(4).Status); }
public void DifferentAll_LazyInsertFirst() { var str1 = "aaa"; var str2 = "bbb"; var results = DiffUtil.Diff(str1, str2); results = DiffUtil.Order(results, DiffOrderType.LazyInsertFirst); Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(0).Status); Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(1).Status); Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(2).Status); Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(3).Status); Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(4).Status); Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(5).Status); }
private static Dictionary <int, ExcelColumnStatus> CreateColumnStatusMap( IEnumerable <ExcelColumn> srcColumns, IEnumerable <ExcelColumn> dstColumns, ExcelSheetDiffConfig config) { var option = new DiffOption <ExcelColumn>(); if (config.SrcHeaderIndex >= 0) { option.EqualityComparer = new HeaderComparer(); foreach (var sc in srcColumns) { sc.HeaderIndex = config.SrcHeaderIndex; } } if (config.DstHeaderIndex >= 0) { foreach (var dc in dstColumns) { dc.HeaderIndex = config.DstHeaderIndex; } } var results = DiffUtil.Diff(srcColumns, dstColumns, option); results = DiffUtil.Order(results, DiffOrderType.LazyDeleteFirst); results = DiffUtil.OptimizeCaseDeletedFirst(results); var ret = new Dictionary <int, ExcelColumnStatus>(); var columnIndex = 0; foreach (var result in results) { var status = ExcelColumnStatus.None; if (result.Status == DiffStatus.Deleted) { status = ExcelColumnStatus.Deleted; } else if (result.Status == DiffStatus.Inserted) { status = ExcelColumnStatus.Inserted; } ret.Add(columnIndex, status); columnIndex++; } return(ret); }
public void SpecifiedComparer() { var str1 = "abc"; var str2 = "dBf"; var option = new DiffOption <char>(); option.EqualityComparer = new CaseInsensitiveComparer(); var results = DiffUtil.Diff(str1, str2, option); results = DiffUtil.Order(results, DiffOrderType.LazyDeleteFirst); Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(0).Status); Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(1).Status); Assert.AreEqual(DiffStatus.Equal, results.ElementAt(2).Status); Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(3).Status); Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(4).Status); }
public void CaseMultiSameScore_LazyInsertFirst() { var str1 = "cdhijkz"; var str2 = "ldxhnokz"; var results = DiffUtil.Diff(str1, str2); results = DiffUtil.Order(results, DiffOrderType.LazyInsertFirst); Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(0).Status); Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(1).Status); Assert.AreEqual(DiffStatus.Equal, results.ElementAt(2).Status); Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(3).Status); Assert.AreEqual(DiffStatus.Equal, results.ElementAt(4).Status); Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(5).Status); Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(6).Status); Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(7).Status); Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(8).Status); Assert.AreEqual(DiffStatus.Equal, results.ElementAt(9).Status); Assert.AreEqual(DiffStatus.Equal, results.ElementAt(10).Status); }
public void Prepended() { var str1 = "bcd"; var str2 = "abcd"; var results = DiffUtil.Diff(str1, str2); results = DiffUtil.Order(results, DiffOrderType.GreedyDeleteFirst); Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(0).Status); Assert.AreEqual(DiffStatus.Equal, results.ElementAt(1).Status); Assert.AreEqual(DiffStatus.Equal, results.ElementAt(2).Status); Assert.AreEqual(DiffStatus.Equal, results.ElementAt(3).Status); results = DiffUtil.Diff(str1, str2); results = DiffUtil.Order(results, DiffOrderType.GreedyInsertFirst); Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(0).Status); Assert.AreEqual(DiffStatus.Equal, results.ElementAt(1).Status); Assert.AreEqual(DiffStatus.Equal, results.ElementAt(2).Status); Assert.AreEqual(DiffStatus.Equal, results.ElementAt(3).Status); results = DiffUtil.Diff(str1, str2); results = DiffUtil.Order(results, DiffOrderType.LazyDeleteFirst); Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(0).Status); Assert.AreEqual(DiffStatus.Equal, results.ElementAt(1).Status); Assert.AreEqual(DiffStatus.Equal, results.ElementAt(2).Status); Assert.AreEqual(DiffStatus.Equal, results.ElementAt(3).Status); results = DiffUtil.Diff(str1, str2); results = DiffUtil.Order(results, DiffOrderType.LazyInsertFirst); Assert.AreEqual(DiffStatus.Inserted, results.ElementAt(0).Status); Assert.AreEqual(DiffStatus.Equal, results.ElementAt(1).Status); Assert.AreEqual(DiffStatus.Equal, results.ElementAt(2).Status); Assert.AreEqual(DiffStatus.Equal, results.ElementAt(3).Status); }
public static ExcelSheetDiff Diff(ExcelSheet src, ExcelSheet dst, ExcelSheetDiffConfig config) { var srcColumns = src.CreateColumns(); var dstColumns = dst.CreateColumns(); var columnStatusMap = CreateColumnStatusMap(srcColumns, dstColumns, config); var option = new DiffOption <ExcelRow>(); option.EqualityComparer = new RowComparer(new HashSet <int>(columnStatusMap.Where(i => i.Value != ExcelColumnStatus.None).Select(i => i.Key))); // 这里实际上计算的是有没有插入新列 foreach (var row in src.Rows.Values) { var shifted = new List <ExcelCell>(); var index = 0; var queue = new Queue <ExcelCell>(row.Cells); while (queue.Any()) { if (columnStatusMap[index] == ExcelColumnStatus.Inserted) { shifted.Add(new ExcelCell(string.Empty, 0, 0)); } else { shifted.Add(queue.Dequeue()); } index++; } row.UpdateCells(shifted); } foreach (var row in dst.Rows.Values) { var shifted = new List <ExcelCell>(); var index = 0; var queue = new Queue <ExcelCell>(row.Cells); while (queue.Any()) { if (columnStatusMap[index] == ExcelColumnStatus.Deleted) { shifted.Add(new ExcelCell(string.Empty, 0, 0)); } else { shifted.Add(queue.Dequeue()); } index++; } row.UpdateCells(shifted); } var r = DiffUtil.Diff(src.Rows.Values, dst.Rows.Values, option); r = DiffUtil.Order(r, DiffOrderType.LazyDeleteFirst); var resultArray = DiffUtil.OptimizeCaseDeletedFirst(r).ToArray(); if (resultArray.Length > 10000) { var count = 0; var indices = Enumerable.Range(0, 100).ToList(); foreach (var result in resultArray) { if (result.Status != DiffStatus.Equal) { indices.AddRange(Enumerable.Range(Math.Max(0, count - 100), 200)); } count++; } indices = indices.Distinct().ToList(); resultArray = indices.Where(i => i < resultArray.Length).Select(i => resultArray[i]).ToArray(); } var sheetDiff = new ExcelSheetDiff(src, dst); DiffCells(resultArray, sheetDiff, columnStatusMap); return(sheetDiff); }