Exemplo n.º 1
0
        public static List <commonOrDifferentThing> diff_comm(string[] file1, string[] file2)
        {
            // We apply the LCS to build a "comm"-style picture of the
            // differences between file1 and file2.

            var result = new List <commonOrDifferentThing>();

            int tail1 = file1.Length;
            int tail2 = file2.Length;

            commonOrDifferentThing common = new commonOrDifferentThing
            {
                common = new List <string>()
            };

            for (var candidate = TextDiff.longest_common_subsequence(file1, file2);
                 candidate != null;
                 candidate = candidate.chain)
            {
                commonOrDifferentThing different = new commonOrDifferentThing
                {
                    file1 = new List <string>(),
                    file2 = new List <string>()
                };

                while (--tail1 > candidate.file1index)
                {
                    different.file1.Add(file1[tail1]);
                }

                while (--tail2 > candidate.file2index)
                {
                    different.file2.Add(file2[tail2]);
                }

                if (different.file1.Count > 0 || different.file2.Count > 0)
                {
                    processCommon(ref common, result);
                    different.file1.Reverse();
                    different.file2.Reverse();
                    result.Add(different);
                }

                if (tail1 >= 0)
                {
                    common.common.Add(file1[tail1]);
                }
            }

            processCommon(ref common, result);

            result.Reverse();
            return(result);
        }
Exemplo n.º 2
0
        public static List <diffSet> diff_indices(string[] file1, string[] file2)
        {
            // We apply the LCS to give a simple representation of the
            // offsets and lengths of mismatched chunks in the input
            // files. This is used by diff3_merge_indices below.

            var result = new List <diffSet>();
            var tail1  = file1.Length;
            var tail2  = file2.Length;

            for (var candidate = TextDiff.longest_common_subsequence(file1, file2);
                 candidate != null;
                 candidate = candidate.chain)
            {
                var mismatchLength1 = tail1 - candidate.file1index - 1;
                var mismatchLength2 = tail2 - candidate.file2index - 1;
                tail1 = candidate.file1index;
                tail2 = candidate.file2index;

                if (mismatchLength1 > 0 || mismatchLength2 > 0)
                {
                    result.Add(new diffSet
                    {
                        file1 = new chunkReference
                        {
                            offset = tail1 + 1,
                            length = mismatchLength1
                        },
                        file2 = new chunkReference
                        {
                            offset = tail2 + 1,
                            length = mismatchLength2
                        }
                    });
                }
            }

            result.Reverse();
            return(result);
        }
Exemplo n.º 3
0
        public static List <patchResult> diff_patch(string[] file1, string[] file2)
        {
            // We apply the LCD to build a JSON representation of a
            // diff(1)-style patch.

            var result = new List <patchResult>();
            var tail1  = file1.Length;
            var tail2  = file2.Length;

            for (var candidate = TextDiff.longest_common_subsequence(file1, file2);
                 candidate != null;
                 candidate = candidate.chain)
            {
                var mismatchLength1 = tail1 - candidate.file1index - 1;
                var mismatchLength2 = tail2 - candidate.file2index - 1;
                tail1 = candidate.file1index;
                tail2 = candidate.file2index;

                if (mismatchLength1 > 0 || mismatchLength2 > 0)
                {
                    patchResult thisResult = new patchResult
                    {
                        file1 = new patchDescriptionThing(file1,
                                                          candidate.file1index + 1,
                                                          mismatchLength1),
                        file2 = new patchDescriptionThing(file2,
                                                          candidate.file2index + 1,
                                                          mismatchLength2)
                    };
                    result.Add(thisResult);
                }
            }

            result.Reverse();
            return(result);
        }