Exemplo n.º 1
0
        protected bool subItemsEqual(DifferenceItem otherItem)
        {
            if (subItems.Count == 0)
            {
                return(otherItem.SubItems.Count == 0);
            }
            else if (otherItem.SubItems.Count == 0)
            {
                return(false);
            }

            if (subItems.Count != otherItem.SubItems.Count)
            {
                return(false);
            }

            for (var i = 0; i < subItems.Count; i++)
            {
                if (!subItems[i].Equals(otherItem.SubItems[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        public static Difference FromDifferenceItem(DifferenceItem item)
        {
            var position = item.Position.DefaultTo(() => - 1);

            return(new Difference(item.Text, item.Type, position));
        }
Exemplo n.º 3
0
        protected static void buildItems(DifferenceResult result, List <DifferenceItem> oldItems, List <DifferenceItem> newItems,
                                         Maybe <ItemBuilder> _subItemBuilder)
        {
            var oldPosition = 0;
            var newPosition = 0;

            foreach (var diffBlock in result.DifferenceBlocks)
            {
                while (newPosition < diffBlock.NewInsertStart && oldPosition < diffBlock.OldDeleteStart)
                {
                    oldItems.Add(new DifferenceItem(result.OldItems[oldPosition], DifferenceType.Unchanged, oldPosition + 1));
                    newItems.Add(new DifferenceItem(result.NewItems[newPosition], DifferenceType.Unchanged, newPosition + 1));
                    oldPosition++;
                    newPosition++;
                }

                var i = 0;
                while (i < Math.Min(diffBlock.OldDeleteCount, diffBlock.NewInsertCount))
                {
                    var oldItem = new DifferenceItem(result.OldItems[i + diffBlock.OldDeleteStart], DifferenceType.Deleted,
                                                     oldPosition + 1);
                    var newItem = new DifferenceItem(result.NewItems[i + diffBlock.NewInsertStart], DifferenceType.Inserted,
                                                     newPosition + 1);
                    if (_subItemBuilder.If(out var subItemBuilder))
                    {
                        var oldWords = result.OldItems[oldPosition].Split("/s+; f");
                        var newWords = result.NewItems[newPosition].Split("/s+; f");
                        var differ   = new DifferenceBuilder(oldWords, newWords, false, false);

                        if (differ.Build().If(out _))
                        {
                            subItemBuilder(result.OldItems[oldPosition], result.NewItems[newPosition], oldItem.SubItems, newItem.SubItems);
                            newItem.Type = DifferenceType.Modified;
                            oldItem.Type = DifferenceType.Modified;
                        }
                    }

                    oldItems.Add(oldItem);
                    newItems.Add(newItem);

                    oldPosition++;
                    newPosition++;
                    i++;
                }

                if (diffBlock.OldDeleteCount > diffBlock.NewInsertCount)
                {
                    while (i < diffBlock.OldDeleteCount)
                    {
                        oldItems.Add(new DifferenceItem(result.OldItems[i + diffBlock.OldDeleteStart], DifferenceType.Deleted,
                                                        oldPosition + 1));
                        newItems.Add(new DifferenceItem());

                        oldPosition++;
                        i++;
                    }
                }
                else
                {
                    while (i < diffBlock.NewInsertCount)
                    {
                        newItems.Add(new DifferenceItem(result.NewItems[i + diffBlock.NewInsertStart], DifferenceType.Inserted,
                                                        newPosition + 1));
                        oldItems.Add(new DifferenceItem());

                        newPosition++;
                        i++;
                    }
                }
            }

            while (newPosition < result.NewItems.Length && oldPosition < result.OldItems.Length)
            {
                oldItems.Add(new DifferenceItem(result.OldItems[oldPosition], DifferenceType.Unchanged, oldPosition + 1));
                newItems.Add(new DifferenceItem(result.NewItems[newPosition], DifferenceType.Unchanged, newPosition + 1));

                oldPosition++;
                newPosition++;
            }
        }