public void Performance() { var str1 = Enumerable.Repeat("Good dog", 1000).SelectMany(c => c); var str2 = Enumerable.Repeat("Bad dog", 1000).SelectMany(c => c); var option = new DiffOption <char>(); option.Limit = 1000; var sw = new System.Diagnostics.Stopwatch(); sw.Start(); var result1 = DiffUtil.Diff(str1, str2); sw.Stop(); var time1 = sw.Elapsed; sw.Restart(); var result2 = DiffUtil.Diff(str1, str2, option); sw.Stop(); var time2 = sw.Elapsed; Assert.IsTrue(time2 < time1); }
List <DiffResult <string> > DiffSheetRow(ISheet sheet1, int row1, ISheet sheet2, int row2, SheetDiffStatus status) { var list1 = new List <string>(); var list2 = new List <string>(); if (sheet1.GetRow(row1) != null) { var row = sheet1.GetRow(row1); var columnCount = books["src"].SheetValideColumn[sheet1.SheetName]; for (int i = 0; i < columnCount; ++i) { list1.Add(Util.GetCellValue(row.GetCell(i))); } } if (sheet2.GetRow(row2) != null) { var row = sheet2.GetRow(row2); var columnCount = books["dst"].SheetValideColumn[sheet2.SheetName]; for (int i = 0; i < columnCount; ++i) { list2.Add(Util.GetCellValue(row.GetCell(i))); } } var diff = DiffUtil.Diff(list1, list2); //var optimized = diff.ToList();// DiffUtil.OptimizeCaseDeletedFirst(diff); var optimized = DiffUtil.OptimizeCaseDeletedFirst(diff); optimized = DiffUtil.OptimizeCaseInsertedFirst(optimized); return(optimized.ToList()); }
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 CaseNull() { string str1 = null; var str2 = string.Empty; var results = DiffUtil.Diff(str1, str2); Assert.IsTrue(!results.Any()); }
public void GivenEmpty() { string str1 = ""; string str2 = ""; var results = DiffUtil.Diff(str1, str2); Assert.IsTrue(!results.Any()); }
public void ModifiedCenter_LazyInsertFirst_OrderIsCollect() { string str1 = "abc"; string str2 = "a_c"; var diff = DiffUtil.Diff(str1, str2).Order(DiffOrderType.LazyInsertFirst); Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(0).Status); Assert.AreEqual(DiffStatus.Inserted, diff.ElementAt(1).Status); Assert.AreEqual(DiffStatus.Deleted, diff.ElementAt(2).Status); Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(3).Status); }
public void Appended_None_ResultIsCorrect() { var str1 = "abc"; var str2 = "abcd"; var diff = DiffUtil.Diff(str1, str2); Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(0).Status); Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(1).Status); Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(2).Status); Assert.AreEqual(DiffStatus.Inserted, diff.ElementAt(3).Status); }
public void GivenSeq2Empty() { string str1 = "abced"; string str2 = ""; var results = DiffUtil.Diff(str1, str2); Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(0).Status); Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(1).Status); Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(2).Status); Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(3).Status); Assert.AreEqual(DiffStatus.Deleted, results.ElementAt(4).Status); }
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 ModifiedSeries_GreedyDeleteFirst_OrderIsCollect() { string str1 = "abbc"; string str2 = "a__c"; var diff = DiffUtil.Diff(str1, str2).Order(DiffOrderType.GreedyDeleteFirst); Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(0).Status); Assert.AreEqual(DiffStatus.Deleted, diff.ElementAt(1).Status); Assert.AreEqual(DiffStatus.Deleted, diff.ElementAt(2).Status); Assert.AreEqual(DiffStatus.Inserted, diff.ElementAt(3).Status); Assert.AreEqual(DiffStatus.Inserted, diff.ElementAt(4).Status); Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(5).Status); }
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 SwapedEquivalentSeries_GreedyInsertFirst_OrderIsCollect() { string str1 = "aaa_bbb"; string str2 = "bbb_aaa"; var diff = DiffUtil.Diff(str1, str2).Order(DiffOrderType.LazyInsertFirst); Assert.AreEqual(DiffStatus.Deleted, diff.ElementAt(0).Status); Assert.AreEqual(DiffStatus.Deleted, diff.ElementAt(1).Status); Assert.AreEqual(DiffStatus.Deleted, diff.ElementAt(2).Status); Assert.AreEqual(DiffStatus.Deleted, diff.ElementAt(3).Status); Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(4).Status); Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(5).Status); Assert.AreEqual(DiffStatus.Equal, diff.ElementAt(6).Status); Assert.AreEqual(DiffStatus.Inserted, diff.ElementAt(7).Status); Assert.AreEqual(DiffStatus.Inserted, diff.ElementAt(8).Status); Assert.AreEqual(DiffStatus.Inserted, diff.ElementAt(9).Status); Assert.AreEqual(DiffStatus.Inserted, diff.ElementAt(10).Status); }
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); }
List <DiffResult <string> > DiffSheetRow(ISheet sheet1, int row1, ISheet sheet2, int row2, SheetDiffStatus status, out int maxLineCount) { var list1 = new List <string>(); var list2 = new List <string>(); maxLineCount = 0; if (sheet1.GetRow(row1) != null) { var row = sheet1.GetRow(row1); var columnCount = books["src"].SheetValideColumn[sheet1.SheetName]; for (int i = 0; i < columnCount; ++i) { var value = Util.GetCellValue(row.GetCell(i)); maxLineCount = Math.Max(maxLineCount, value.Count((c) => { return(c == '\n'); }) + 1); list1.Add(value); } } if (sheet2.GetRow(row2) != null) { var row = sheet2.GetRow(row2); var columnCount = books["dst"].SheetValideColumn[sheet2.SheetName]; for (int i = 0; i < columnCount; ++i) { var value = Util.GetCellValue(row.GetCell(i)); maxLineCount = Math.Max(maxLineCount, value.Count((c) => { return(c == '\n'); }) + 1); list2.Add(value); } } var diff = DiffUtil.Diff(list1, list2); //var optimized = diff.ToList();// DiffUtil.OptimizeCaseDeletedFirst(diff); var optimized = DiffUtil.OptimizeCaseDeletedFirst(diff); optimized = DiffUtil.OptimizeCaseInsertedFirst(optimized); var tlist = optimized.ToList(); optimized = DiffUtil.OptimizeShift(tlist, false); optimized = DiffUtil.OptimizeShift(optimized, true); return(optimized.ToList()); }
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); }
/// <summary> /// Get differences between an original stream and a modified stream. /// </summary> /// <param name="originalStream">Original stream.</param> /// <param name="originalEncoding">Encoding of original stream.</param> /// <param name="modifiedStream">Modified stream.</param> /// <param name="modifiedEncoding">Encoding of modified stream.</param> /// <returns>A summary of the differences between two streams.</returns> private DiffSummary GetDifference(Stream originalStream, Encoding originalEncoding, Stream modifiedStream, Encoding modifiedEncoding) { var diffOptions = new DiffOptions { UseThirdPartyTool = false }; if (_marginSettings.IgnoreLeadingAndTrailingWhiteSpace) { diffOptions.Flags |= DiffOptionFlags.IgnoreLeadingAndTrailingWhiteSpace; } originalStream.Position = 0; modifiedStream.Position = 0; DiffSummary diffSummary = DiffUtil.Diff( originalStream, originalEncoding, modifiedStream, modifiedEncoding, diffOptions, true); return(diffSummary); }
public void UpdateText(string[] newContent) { var diff = DiffUtil.Diff(WorkSpace.List, newContent).ToArray(); ApplyChange(new LineDiffsCommit(diff)); }
// 把第一列认为是id列,检查增删, <value, 行id> List <DiffResult <string2int> > GetIDDiffList(ISheet sheet1, ISheet sheet2, int checkCellCount, bool addRowID = false, int startCheckCell = 0) { var list1 = new List <string2int>(); var list2 = new List <string2int>(); var nameHash = new HashSet <string>(); var startIdx = DiffStartIdx(); bool allNum = checkCellCount == 1; int ignoreEmptyLine = config.EmptyLine; // 尝试找一个id不会重复的前几列的值作为key for (int i = startIdx; ; i++) { var row = sheet1.GetRow(i); if (row == null || !Util.CheckValideRow(row)) { if (ignoreEmptyLine-- > 0) { continue; } else { books["src"].SheetValideRow[sheet1.SheetName] = i; break; } } ; var val = ""; for (var j = startCheckCell; j < startCheckCell + checkCellCount; ++j) { if (row.GetCell(j) == null || row.GetCell(j).CellType != CellType.Numeric) { allNum = false; } val += Util.GetCellValue(row.GetCell(j)); } var hash_val = val; if (addRowID) { hash_val = hash_val + "." + i; } if (nameHash.Contains(hash_val)) { if (checkCellCount < 6) { return(GetIDDiffList(sheet1, sheet2, checkCellCount + 1, addRowID, startCheckCell)); } else { // 已经找不到能作为key的了。把id和行号连一块 return(GetIDDiffList(sheet1, sheet2, 1, true, startCheckCell)); } } nameHash.Add(hash_val); list1.Add(new string2int(val, i)); } nameHash.Clear(); ignoreEmptyLine = config.EmptyLine; for (int i = startIdx; ; i++) { var row = sheet2.GetRow(i); if (row == null || !Util.CheckValideRow(row)) { if (ignoreEmptyLine-- > 0) { continue; } else { books["dst"].SheetValideRow[sheet2.SheetName] = i; break; } } var val = ""; for (var j = startCheckCell; j < startCheckCell + checkCellCount; ++j) { if (row.GetCell(j) == null || row.GetCell(j).CellType != CellType.Numeric) { allNum = false; } val += Util.GetCellValue(row.GetCell(j)); } var hash_val = val; if (addRowID) { hash_val = hash_val + "." + i; } if (nameHash.Contains(hash_val)) { if (checkCellCount < 6) { return(GetIDDiffList(sheet1, sheet2, checkCellCount + 1, addRowID, startCheckCell)); } else { // 已经找不到能作为key的了。把id和行号连一块 return(GetIDDiffList(sheet1, sheet2, 1, true, startCheckCell)); } } nameHash.Add(hash_val); list2.Add(new string2int(val, i)); } list1.Sort(delegate(string2int a, string2int b) { int cmp = 0; if (allNum) { cmp = Double.Parse(a.Key).CompareTo(Double.Parse(b.Key)); } else { cmp = a.Key.CompareTo(b.Key); } if (cmp == 0) { return(a.Value.CompareTo(b.Value)); } return(cmp); }); list2.Sort(delegate(string2int a, string2int b) { int cmp = 0; if (allNum) { cmp = Double.Parse(a.Key).CompareTo(Double.Parse(b.Key)); } else { cmp = a.Key.CompareTo(b.Key); } if (cmp == 0) { return(a.Value.CompareTo(b.Value)); } return(cmp); }); var option = new DiffOption <string2int>(); option.EqualityComparer = new SheetIDComparer(); var result = DiffUtil.Diff(list1, list2, option); //var optimize = result.ToList();// // id列不应该把delete/add优化成modify // var optimize = DiffUtil.OptimizeCaseDeletedFirst(result); return(result.ToList()); }
/// <summary> /// Androidで音声認識のpartialの差分をとる /// </summary> public DiffTcpServer(MessageDispatcherSync msgDispatcher) : base(msgDispatcher) { textId = 0; partial = ""; Task.Run(() => { var ipEndPoint = new IPEndPoint(IPAddress.Parse("sora.nov"), 8888); Console.WriteLine(ipEndPoint.ToString()); tcpListener = new TcpListener(ipEndPoint); tcpListener.Start(); while (true) { Console.WriteLine("待機中"); using (var client = tcpListener.AcceptTcpClient()) using (var netStream = client.GetStream()) using (var streamReader = new StreamReader(netStream, Encoding.UTF8)) { var idStr = streamReader.ReadLine(); Console.WriteLine(idStr); var id = int.Parse(idStr); if (textId != id) { partial = ""; textId = id; } var receivedText = streamReader.ReadLine(); if (receivedText.Length == 0) { continue; } Console.WriteLine("receive : " + receivedText); var lastAdd = -1; if (partial.Length > 0) { Console.WriteLine("diff"); var results = DiffUtil.Diff(partial, receivedText); var resultList = results.ToList(); lastAdd = resultList.FindLastIndex(r => r.ToFormatString().StartsWith("=") || r.ToFormatString().StartsWith("-")); var index = 0; var deleteCount = resultList.Where(r => r.ToFormatString().StartsWith("-") && index++ < lastAdd).Count(); if (lastAdd == -1 && resultList.Any(r => r.ToFormatString().StartsWith("+"))) { //同じ文字がなく追加と削除しかない deleteCount = 0; lastAdd = resultList.FindIndex(r => { if (r.ToFormatString().StartsWith("-")) { deleteCount++; } return(r.ToFormatString().StartsWith("+")); }); } Console.WriteLine("deleteCount:" + deleteCount); lastAdd -= deleteCount; index = 0; resultList.ForEach(r => Console.Write(index++ + ":" + r.ToFormatString() + " ")); Console.WriteLine(); } var askText = receivedText; Console.WriteLine("lastAdd : " + lastAdd); Console.WriteLine("receivedText.Length : " + receivedText.Length); if (lastAdd != -1 && lastAdd != receivedText.Length - 1) { askText = receivedText.Substring(lastAdd + 1); } else if (partial.Length > 0) { continue; } Console.WriteLine("enqueue : " + askText); EnqueueMessage(new YukariMessage(askText)); partial = receivedText; } } }); }
private void ButtonCompare_Click(object sender, EventArgs e) { Console.WriteLine("Button click!"); if ((pdfPanel1.pdfViewer.Document == null) || (pdfPanel2.pdfViewer.Document == null)) { labelResult.Text = "0.0"; return; } var stopwatch = new System.Diagnostics.Stopwatch(); int page1 = pdfPanel1.pdfViewer.Renderer.Page; int page2 = pdfPanel2.pdfViewer.Renderer.Page; //if ((pdfPanel1.GetComparedPage(page1) == page2) && (pdfPanel2.GetComparedPage(page2) == page1)) //{ // labelResult.Text = "0.0"; // return; //} if (pdfPanel1.GetComparedPage(page1) >= 0) { pdfPanel1.ClearDiffMarker(page1); } if (pdfPanel2.GetComparedPage(page2) >= 0) { pdfPanel2.ClearDiffMarker(page2); } // Draw without the marker. pdfPanel1.Update(); pdfPanel2.Update(); //stopwatch.Stop(); //Console.WriteLine("Clear:{0}", stopwatch.Elapsed); //stopwatch.Start(); String text1 = pdfPanel1.GetPageText(); String text2 = pdfPanel2.GetPageText(); List <PdfTextSpan> index1 = new List <PdfTextSpan>(); List <PdfTextSpan> index2 = new List <PdfTextSpan>(); stopwatch.Stop(); Console.WriteLine("Get:{0}", stopwatch.Elapsed); labelResult.Text = "."; labelResult.Update(); stopwatch.Start(); diff_match_patch dmp; List <Diff> diffs; int diffType = comboBoxDiffType.SelectedIndex; switch (diffType) { case 0: // NetDiff var results = DiffUtil.Diff(text1, text2); ExtractDiffSpan(index1, index2, results, page1, page2); break; case 1: // Google Diff - Semantic dmp = new diff_match_patch(); diffs = dmp.diff_main(text1, text2); dmp.diff_cleanupSemantic(diffs); ExtractDiffSpan2(index1, index2, diffs, page1, page2); break; case 2: // Google Diff - Effective 4 dmp = new diff_match_patch(); dmp.Diff_EditCost = 4; diffs = dmp.diff_main(text1, text2); dmp.diff_cleanupEfficiency(diffs); ExtractDiffSpan2(index1, index2, diffs, page1, page2); break; case 3: // Google Diff - Effective 5 dmp = new diff_match_patch(); dmp.Diff_EditCost = 5; diffs = dmp.diff_main(text1, text2); dmp.diff_cleanupEfficiency(diffs); ExtractDiffSpan2(index1, index2, diffs, page1, page2); break; case 4: // Google Diff - Effective 3 dmp = new diff_match_patch(); dmp.Diff_EditCost = 3; diffs = dmp.diff_main(text1, text2); dmp.diff_cleanupEfficiency(diffs); ExtractDiffSpan2(index1, index2, diffs, page1, page2); break; case 5: // Google Diff - Raw dmp = new diff_match_patch(); diffs = dmp.diff_main(text1, text2); ExtractDiffSpan2(index1, index2, diffs, page1, page2); break; default: throw new Exception(); } stopwatch.Stop(); Console.WriteLine("Diff:{0}", stopwatch.Elapsed); labelResult.Text = ".."; labelResult.Update(); stopwatch.Start(); pdfPanel1.SetPageDiff(index1); pdfPanel2.SetPageDiff(index2); pdfPanel1.AddDiffMarker(page2); labelResult.Text = "..."; labelResult.Update(); pdfPanel2.AddDiffMarker(page1); labelResult.Text = "...."; labelResult.Update(); stopwatch.Stop(); Console.WriteLine("Marker:{0}", stopwatch.Elapsed); stopwatch.Start(); //results.ToList().ForEach(r => Console.WriteLine(r.ToFormatString())); if (stopwatch.ElapsedMilliseconds < 50) { Thread.Sleep(30); } // Draw with the markers. pdfPanel1.Update(); pdfPanel2.Update(); stopwatch.Stop(); Console.WriteLine("Draw:{0}", stopwatch.Elapsed); labelResult.Text = String.Format("{0:0.0}", stopwatch.ElapsedMilliseconds / 1000.0); }
// 把第一列认为是id列,检查增删, <value, 行id> List <DiffResult <string2int> > GetIDDiffList(ISheet sheet1, ISheet sheet2, int checkCellCount, bool addRowID = false) { var list1 = new List <string2int>(); var list2 = new List <string2int>(); var nameHash = new HashSet <string>(); var startIdx = DiffStartIdx(); // 尝试找一个id不会重复的前几列的值作为key for (int i = startIdx; ; i++) { var row = sheet1.GetRow(i); if (row == null || !Util.CheckValideRow(row)) { books["src"].SheetValideRow[sheet1.SheetName] = i; break; } ; var val = ""; for (var j = 0; j < checkCellCount; ++j) { val += Util.GetCellValue(row.GetCell(j)); } var hash_val = val; if (addRowID) { hash_val = hash_val + "." + i; } if (nameHash.Contains(hash_val)) { if (checkCellCount < 6) { return(GetIDDiffList(sheet1, sheet2, checkCellCount + 1, addRowID)); } else { // 已经找不到能作为key的了。把id和行号连一块 return(GetIDDiffList(sheet1, sheet2, 1, true)); } } nameHash.Add(hash_val); list1.Add(new string2int(val, i)); } list1.Sort(delegate(string2int a, string2int b) { var cmp = a.Key.CompareTo(b.Key); if (cmp == 0) { return(a.Value.CompareTo(b.Value)); } return(cmp); }); nameHash.Clear(); for (int i = startIdx; ; i++) { var row = sheet2.GetRow(i); if (row == null || !Util.CheckValideRow(row)) { books["dst"].SheetValideRow[sheet2.SheetName] = i; break; } var val = ""; for (var j = 0; j < checkCellCount; ++j) { val += Util.GetCellValue(row.GetCell(j)); } var hash_val = val; if (addRowID) { hash_val = hash_val + "." + i; } if (nameHash.Contains(hash_val)) { if (checkCellCount < 6) { return(GetIDDiffList(sheet1, sheet2, checkCellCount + 1, addRowID)); } else { // 已经找不到能作为key的了。把id和行号连一块 return(GetIDDiffList(sheet1, sheet2, 1, true)); } } nameHash.Add(hash_val); list2.Add(new string2int(val, i)); } list2.Sort(delegate(string2int a, string2int b) { var cmp = a.Key.CompareTo(b.Key); if (cmp == 0) { return(a.Value.CompareTo(b.Value)); } return(cmp); }); var option = new DiffOption <string2int>(); option.Optimize = false; option.EqualityComparer = new SheetIDComparer(); var result = DiffUtil.Diff(list1, list2, option); //var optimize = result.ToList();// var optimize = DiffUtil.OptimizeCaseDeletedFirst(result); return(optimize.ToList()); }
public void Diff(string file1, string file2, bool resetInitFile = true) { if (string.IsNullOrEmpty(file1) || string.IsNullOrEmpty(file2)) { return; } if (resetInitFile) { SrcFile = file1; DstFile = file2; } string oldsheetName = null; if (books.ContainsKey("src")) { oldsheetName = books["src"].sheetname; } var src = InitWorkWrap(file1); var dst = InitWorkWrap(file2); var option = new DiffOption <SheetNameCombo>(); option.EqualityComparer = new SheetNameComboComparer(); var result = DiffUtil.Diff(src.sheetNameCombos, dst.sheetNameCombos, option); //diffSheetName = result.ToList();// diffSheetName = DiffUtil.OptimizeCaseDeletedFirst(result).ToList(); books["src"] = src; books["dst"] = dst; var srcSheetID = -1; var dstSheetID = -1; for (int i = 0; i < diffSheetName.Count; ++i) { var sheetname = diffSheetName[i]; var name = sheetname.Obj1 == null ? sheetname.Obj2.Name : sheetname.Obj1.Name; // 只有sheet名字一样的可以diff, 先这么处理 if (sheetname.Status == DiffStatus.Equal) { var sheet1 = sheetname.Obj1.ID; var sheet2 = sheetname.Obj2.ID; sheetsDiff[name] = DiffSheet(src.book.GetSheetAt(sheet1), dst.book.GetSheetAt(sheet2)); if (sheetsDiff[name] != null) { oldsheetName = sheetname.Obj1.Name; var sheetidx = 0; if (!string.IsNullOrEmpty(oldsheetName)) { sheetidx = src.book.GetSheetIndex(oldsheetName); } if (sheetsDiff[name].changed || srcSheetID == -1) { src.sheet = sheetidx; srcSheetID = sheetidx; } if (!string.IsNullOrEmpty(oldsheetName)) { sheetidx = dst.book.GetSheetIndex(oldsheetName); } if (sheetsDiff[name].changed || dstSheetID == -1) { dst.sheet = sheetidx; dstSheetID = sheetidx; } } } } // refresh ui SrcFilePath.Content = file1; DstFilePath.Content = file2; SrcFileSheetsCombo.Items.Clear(); foreach (var item in src.sheetCombo) { int index = diffSheetName.FindIndex(a => a.Obj1 != null && a.Obj1.ID == (item.Content as SheetNameCombo).ID); SolidColorBrush color = null; DiffStatus status = diffSheetName[index].Status; if (status != DiffStatus.Equal) { color = Util.GetColorByDiffStatus(status); } else { var name = diffSheetName[index].Obj1.Name; color = Util.GetColorByDiffStatus(sheetsDiff.ContainsKey(name) && sheetsDiff[name] != null && sheetsDiff[name].changed ? DiffStatus.Modified : DiffStatus.Equal); } if (color != null) { item.Background = color; } SrcFileSheetsCombo.Items.Add(item); } var comboidx = src.ItemID2ComboIdx[src.sheet]; SrcFileSheetsCombo.SelectedItem = src.sheetCombo[comboidx]; DstFileSheetsCombo.Items.Clear(); foreach (var item in dst.sheetCombo) { int index = diffSheetName.FindIndex(a => a.Obj2 != null && a.Obj2.ID == (item.Content as SheetNameCombo).ID); SolidColorBrush color = null; DiffStatus status = diffSheetName[index].Status; if (status != DiffStatus.Equal) { color = Util.GetColorByDiffStatus(status); } else { var name = diffSheetName[index].Obj1.Name; color = Util.GetColorByDiffStatus(sheetsDiff.ContainsKey(name) && sheetsDiff[name] != null && sheetsDiff[name].changed ? DiffStatus.Modified : DiffStatus.Equal); } if (color != null) { item.Background = color; } DstFileSheetsCombo.Items.Add(item); } comboidx = dst.ItemID2ComboIdx[dst.sheet]; DstFileSheetsCombo.SelectedItem = dst.sheetCombo[comboidx]; DstDataGrid.RefreshData(); SrcDataGrid.RefreshData(); }
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); }