static void BuildModificationData(ModificationData a, ModificationData b) { var n = a.HashedPieces.Length; var m = b.HashedPieces.Length; var max = m + n + 1; var forwardDiagonal = new int[max + 1]; var reverseDiagonal = new int[max + 1]; BuildModificationData(a, 0, n, b, 0, m, forwardDiagonal, reverseDiagonal); }
public static DiffResult CreateVbaDiffs(string oldText, string newText) { if (oldText == null) { throw new ArgumentNullException(nameof(oldText)); } if (newText == null) { throw new ArgumentNullException(nameof(newText)); } var pieceHash = new Dictionary <string, int>(); var lineDiffs = new List <DiffBlock>(); var modOld = new ModificationData(oldText); var modNew = new ModificationData(newText); BuildPieceHashes(pieceHash, modOld); BuildPieceHashes(pieceHash, modNew); BuildModificationData(modOld, modNew); var piecesALength = modOld.HashedPieces.Length; var piecesBLength = modNew.HashedPieces.Length; var posA = 0; var posB = 0; do { while (posA < piecesALength && posB < piecesBLength && !modOld.Modifications[posA] && !modNew.Modifications[posB]) { posA++; posB++; } var beginA = posA; var beginB = posB; while (posA < piecesALength && modOld.Modifications[posA]) { posA++; } while (posB < piecesBLength && modNew.Modifications[posB]) { posB++; } var deleteCount = posA - beginA; var insertCount = posB - beginB; if (deleteCount > 0 || insertCount > 0) { lineDiffs.Add(new DiffBlock(beginA, deleteCount, beginB, insertCount)); } } while (posA < piecesALength && posB < piecesBLength); return(new DiffResult(modOld.Pieces, modNew.Pieces, lineDiffs)); }
private static void BuildModificationData(ModificationData a, int startA, int endA, ModificationData b, int startB, int endB, int[] forwardDiagonal, int[] reverseDiagonal) { while (startA < endA && startB < endB && a.HashedPieces[startA].Equals(b.HashedPieces[startB])) { startA++; startB++; } while (startA < endA && startB < endB && a.HashedPieces[endA - 1].Equals(b.HashedPieces[endB - 1])) { endA--; endB--; } int aLength = endA - startA; int bLength = endB - startB; if (aLength > 0 && bLength > 0) { EditLengthResult res = CalculateEditLength(a.HashedPieces, startA, endA, b.HashedPieces, startB, endB, forwardDiagonal, reverseDiagonal); if (res.EditLength <= 0) { return; } if (res.LastEdit == Edit.DeleteRight && res.StartX - 1 > startA) { a.Modifications[--res.StartX] = true; } else if (res.LastEdit == Edit.InsertDown && res.StartY - 1 > startB) { b.Modifications[--res.StartY] = true; } else if (res.LastEdit == Edit.DeleteLeft && res.EndX < endA) { a.Modifications[res.EndX++] = true; } else if (res.LastEdit == Edit.InsertUp && res.EndY < endB) { b.Modifications[res.EndY++] = true; } BuildModificationData(a, startA, res.StartX, b, startB, res.StartY, forwardDiagonal, reverseDiagonal); BuildModificationData(a, res.EndX, endA, b, res.EndY, endB, forwardDiagonal, reverseDiagonal); } else if (aLength > 0) { for (var i = startA; i < endA; i++) { a.Modifications[i] = true; } } else if (bLength > 0) { for (var i = startB; i < endB; i++) { b.Modifications[i] = true; } } }