Beispiel #1
0
        public Collection<MultiLineDiff> GetTraces(string[] originalStrs, string[] newStrs, int[][] buffer)
        {
            Collection<MultiLineDiff> trances = new Collection<MultiLineDiff>();

            int y = 0;
            int x = 0;

            while (y < buffer.Length - 1 && x < buffer[0].Length - 1)
            {
                MultiLineDiff diffItem = new MultiLineDiff();
                diffItem.NewLine = x;
                diffItem.OriginalLine = y;

                int currentValue = buffer[y][x];

                int addLength = buffer[y][x + 1];
                int deleteLength = buffer[y + 1][x];
                int updateLength = buffer[y + 1][x + 1];

                if (currentValue == addLength + 1)
                {
                    diffItem.ChangeType = 1;
                    x++;
                }
                else if (currentValue == deleteLength + 1)
                {
                    diffItem.ChangeType = -1;
                    y++;
                }
                else
                {
                    if (originalStrs[y] == newStrs[x])
                    {
                        diffItem.ChangeType = 0;
                    }
                    else
                    {
                        diffItem.ChangeType = 2;
                    }
                    x = x + 1;
                    y = y + 1;
                }
                trances.Add(diffItem);
            }

            if (y == buffer.Length - 1)
            {
                //Add
                while (x < newStrs.Length)
                {
                    MultiLineDiff diffItem = new MultiLineDiff();
                    diffItem.NewLine = x;
                    diffItem.OriginalLine = -1;
                    diffItem.ChangeType = 1;

                    trances.Add(diffItem);
                    x++;
                }
            }
            else if (x == buffer[0].Length - 1)
            {
                //delete
                while (y < originalStrs.Length)
                {
                    MultiLineDiff diffItem = new MultiLineDiff();
                    diffItem.NewLine = -1;
                    diffItem.OriginalLine = y;
                    diffItem.ChangeType = -1;
                    trances.Add(diffItem);
                    y++;
                }
            }
            return trances;
        }
Beispiel #2
0
        public Collection <MultiLineDiff> GetTraces(string[] originalStrs, string[] newStrs, int[][] buffer)
        {
            Collection <MultiLineDiff> trances = new Collection <MultiLineDiff>();

            int y = 0;
            int x = 0;

            while (y < buffer.Length - 1 && x < buffer[0].Length - 1)
            {
                MultiLineDiff diffItem = new MultiLineDiff();
                diffItem.NewLine      = x;
                diffItem.OriginalLine = y;

                int currentValue = buffer[y][x];

                int addLength    = buffer[y][x + 1];
                int deleteLength = buffer[y + 1][x];
                int updateLength = buffer[y + 1][x + 1];

                if (currentValue == addLength + 1)
                {
                    diffItem.ChangeType = 1;
                    x++;
                }
                else if (currentValue == deleteLength + 1)
                {
                    diffItem.ChangeType = -1;
                    y++;
                }
                else
                {
                    if (originalStrs[y] == newStrs[x])
                    {
                        diffItem.ChangeType = 0;
                    }
                    else
                    {
                        diffItem.ChangeType = 2;
                    }
                    x = x + 1;
                    y = y + 1;
                }
                trances.Add(diffItem);
            }

            if (y == buffer.Length - 1)
            {
                //Add
                while (x < newStrs.Length)
                {
                    MultiLineDiff diffItem = new MultiLineDiff();
                    diffItem.NewLine      = x;
                    diffItem.OriginalLine = -1;
                    diffItem.ChangeType   = 1;

                    trances.Add(diffItem);
                    x++;
                }
            }
            else if (x == buffer[0].Length - 1)
            {
                //delete
                while (y < originalStrs.Length)
                {
                    MultiLineDiff diffItem = new MultiLineDiff();
                    diffItem.NewLine      = -1;
                    diffItem.OriginalLine = y;
                    diffItem.ChangeType   = -1;
                    trances.Add(diffItem);
                    y++;
                }
            }
            return(trances);
        }
        /// <summary>
        /// Get the edit path from diff matrix
        /// </summary>
        /// <param name="originalStrs"></param>
        /// <param name="newStrs"></param>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public Collection <MultiLineDiff> GetTraces(string[] originalStrs, string[] newStrs, int[][] buffer)
        {
            Collection <MultiLineDiff> trances = new Collection <MultiLineDiff>();

            int y = buffer.Length - 1;
            int x = buffer[0].Length - 1;

            while (x > 0 && y > 0)
            {
                int           currentValue = buffer[y][x];
                int           addLength    = buffer[y][x - 1] + 1;
                int           deleteLength = buffer[y - 1][x] + 1;
                int           updateLength = buffer[y - 1][x - 1] + (TrimCompare(originalStrs[y - 1], newStrs[x - 1])?0:1);
                MultiLineDiff diffItem     = new MultiLineDiff();
                diffItem.NewLine      = x - 1;
                diffItem.OriginalLine = y - 1;
                if (currentValue == addLength)
                {
                    diffItem.ChangeType = 1;
                    x--;
                }
                else if (currentValue == deleteLength)
                {
                    diffItem.ChangeType = -1;
                    y--;
                }
                else
                {
                    if (TrimCompare(originalStrs[y - 1], newStrs[x - 1]))
                    {
                        diffItem.ChangeType = 0;
                    }
                    else
                    {
                        diffItem.ChangeType = 2;
                    }
                    y--;
                    x--;
                }

                trances.Insert(0, diffItem);
            }
            if (x == 0)
            {
                while (y != 0)
                {
                    MultiLineDiff diffItem = new MultiLineDiff();
                    diffItem.ChangeType   = -1;
                    diffItem.NewLine      = -1;
                    diffItem.OriginalLine = y - 1;
                    trances.Insert(0, diffItem);
                    y--;
                }
            }
            else if (y == 0)
            {
                while (x != 0)
                {
                    MultiLineDiff diffItem = new MultiLineDiff();
                    diffItem.ChangeType   = 1;
                    diffItem.NewLine      = x - 1;
                    diffItem.OriginalLine = -1;
                    trances.Insert(0, diffItem);
                    x--;
                }
            }

            //while (y < buffer.Length - 1 && x < buffer[0].Length - 1)
            //{
            //    MultiLineDiff diffItem = new MultiLineDiff();
            //    diffItem.NewLine = x;
            //    diffItem.OriginalLine = y;

            //    int currentValue = buffer[y][x];

            //    int addLength = buffer[y][x + 1];
            //    int deleteLength = buffer[y + 1][x];
            //    int updateLength = buffer[y + 1][x + 1];

            //    if (currentValue == addLength + 1)
            //    {
            //        diffItem.ChangeType = 1;
            //        x++;
            //    }
            //    else if (currentValue == deleteLength + 1)
            //    {
            //        diffItem.ChangeType = -1;
            //        y++;
            //    }
            //    else
            //    {
            //        if (TrimCompare(originalStrs[y] , newStrs[x]))
            //        {
            //            diffItem.ChangeType = 0;
            //        }
            //        else
            //        {
            //            diffItem.ChangeType = 2;
            //        }
            //        x = x + 1;
            //        y = y + 1;
            //    }
            //    trances.Add(diffItem);
            //}

            //if (y == buffer.Length - 1)
            //{
            //    //Add
            //    while (x < newStrs.Length)
            //    {
            //        MultiLineDiff diffItem = new MultiLineDiff();
            //        diffItem.NewLine = x;
            //        diffItem.OriginalLine = -1;
            //        diffItem.ChangeType = 1;

            //        trances.Add(diffItem);
            //        x++;
            //    }
            //}
            //else if (x == buffer[0].Length - 1)
            //{
            //    //delete
            //    while (y < originalStrs.Length)
            //    {
            //        MultiLineDiff diffItem = new MultiLineDiff();
            //        diffItem.NewLine = -1;
            //        diffItem.OriginalLine = y;
            //        diffItem.ChangeType = -1;
            //        trances.Add(diffItem);
            //        y++;
            //    }
            //}
            return(trances);
        }