Пример #1
0
        public static bool ApplyPatch(LinePatch patch, List <string> text)
        {
            int previousIndex = patch.Start1;

            foreach (var diff in patch.Diffs)
            {
                if (diff.Operation.IsEqual)
                {
                    foreach (var line in diff.Lines)
                    {
                        if (text[previousIndex] != line)
                        {
                            return(false);
                        }
                        previousIndex++;
                    }
                }

                if (diff.Operation.IsDelete)
                {
                    text.RemoveRange(previousIndex, diff.Lines.Count);
                }

                if (diff.Operation.IsInsert)
                {
                    text.InsertRange(previousIndex, diff.Lines);
                    previousIndex += diff.Lines.Count;
                }
            }

            return(true);
        }
Пример #2
0
        public static (List <LineDiff> prefix, List <LineDiff> changes, List <LineDiff> suffix) SplitPatchAffix(this LinePatch patch)
        {
            var prefix  = new List <LineDiff>();
            var changes = new List <LineDiff>();
            var suffix  = new List <LineDiff>();

            int i = 0;

            while (i < patch.Diffs.Count)
            {
                if (!patch.Diffs[i].Operation.IsEqual)
                {
                    break;
                }
                prefix.Add(patch.Diffs[i]);
                i++;
            }

            while (i < patch.Diffs.Count)
            {
                if (patch.Diffs[i].Operation.IsEqual)
                {
                    break;
                }
                changes.Add(patch.Diffs[i]);
                i++;
            }

            while (i < patch.Diffs.Count)
            {
                if (!patch.Diffs[i].Operation.IsEqual)
                {
                    break;
                }
                suffix.Add(patch.Diffs[i]);
                i++;
            }

            Debug.Assert(i == patch.Diffs.Count);

            return(prefix, changes, suffix);
        }