Beispiel #1
0
        private 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);
        }
Beispiel #2
0
        private 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);
        }
Beispiel #3
0
        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--;
            }

            var aLength = endA - startA;
            var bLength = endB - startB;

            if (aLength > 0 && bLength > 0)
            {
                var 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;
                }
            }
        }
Beispiel #4
0
        public static DiffResult CreateCustomDiffs(string oldText, string newText, bool ignoreWhiteSpace, bool ignoreCase, Func <string, string[]> chunker)
        {
            if (oldText == null)
            {
                throw new ArgumentNullException(nameof(oldText));
            }
            if (newText == null)
            {
                throw new ArgumentNullException(nameof(newText));
            }
            if (chunker == null)
            {
                throw new ArgumentNullException(nameof(chunker));
            }

            var pieceHash = new Dictionary <string, int>();
            var lineDiffs = new List <DiffBlock>();

            var modOld = new ModificationData(oldText);
            var modNew = new ModificationData(newText);

            BuildPieceHashes(pieceHash, modOld, ignoreWhiteSpace, ignoreCase, chunker);
            BuildPieceHashes(pieceHash, modNew, ignoreWhiteSpace, ignoreCase, chunker);

            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;
                for (; posA < piecesALength && modOld.Modifications[posA]; posA++)
                {
                    ;
                }

                for (; 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));
        }