DiffText() public method

Find the difference in 2 texts, comparing by textlines.
public DiffText ( string TextA, string TextB ) : System.Item[]
TextA string A-version of the text (usualy the old one)
TextB string B-version of the text (usualy the new one)
return System.Item[]
Exemplo n.º 1
0
        public static Dictionary <ExecutionCall, ExecutionCall> GeneratePathDiff(List <ExecutionCall> left, List <ExecutionCall> right)
        {
            Dictionary <ExecutionCall, ExecutionCall> sameMap = new Dictionary <ExecutionCall, ExecutionCall>();

            var flatLeft  = FlattenPath(left);
            var flatRight = FlattenPath(right);

            StringBuilder sbLeft = new StringBuilder();

            foreach (var call in flatLeft)
            {
                SerializeCall(sbLeft, call);
            }

            StringBuilder sbRight = new StringBuilder();

            foreach (var call in flatRight)
            {
                SerializeCall(sbRight, call);
            }

            sbLeft.Length  -= Environment.NewLine.Length;
            sbRight.Length -= Environment.NewLine.Length;

            var l     = sbLeft.ToString();
            var r     = sbRight.ToString();
            var diff  = new Diff.Diff();
            var items = diff.DiffText(l, r);
            int pos   = 0;

            List <int> diffItems = new List <int>();

            for (var n = 0; n < items.Length; n++)
            {
                Diff.Diff.Item i = items[n];
                while ((pos < i.StartB) && (pos < flatRight.Count))
                {
                    diffItems.Add(0);
                    pos++;
                }
                if (i.deletedA > 0)
                {
                    for (var m = 0; m < i.deletedA; m++)
                    {
                        diffItems.Add(-1);
                    }
                }
                if (pos < i.StartB + i.insertedB)
                {
                    while (pos < i.StartB + i.insertedB)
                    {
                        diffItems.Add(1);
                        pos++;
                    }
                }
            }

            while (pos < flatRight.Count)
            {
                diffItems.Add(0);
                pos++;
            }

            var leftPointer  = 0;
            var rightPointer = 0;
            var lineIndex    = 0;

            for (lineIndex = 0; lineIndex < diffItems.Count; lineIndex++)
            {
                var curDiff = diffItems[lineIndex];
                switch (curDiff)
                {
                case 0:
                    sameMap.Add(flatLeft[leftPointer++], flatRight[rightPointer++]);
                    break;

                case 1:
                    var call = flatRight[rightPointer++];
                    call.DiffStatus = DiffStatus.INSERT;
                    var parent = call.Parent;
                    while (parent != null)
                    {
                        if (parent.DiffStatus == DiffStatus.SAME)
                        {
                            parent.DiffStatus = DiffStatus.MODIFIED;
                        }

                        parent = parent.Parent;
                    }
                    break;

                case -1:
                    call            = flatLeft[leftPointer++];
                    call.DiffStatus = DiffStatus.DELETE;
                    parent          = call.Parent;
                    while (parent != null)
                    {
                        if (parent.DiffStatus == DiffStatus.SAME)
                        {
                            parent.DiffStatus = DiffStatus.MODIFIED;
                        }

                        parent = parent.Parent;
                    }
                    break;
                }
            }
            return(sameMap);
        }
Exemplo n.º 2
0
        /// <summary>
        /// start a self- / box-test for some diff cases and report to the debug output.
        /// </summary>
        /// <param name="args">not used</param>
        /// <returns>always 0</returns>
        public static int Main(string[] args)
        {
            StringBuilder ret = new StringBuilder();
            string        a, b;

            System.Diagnostics.ConsoleTraceListener ctl = new System.Diagnostics.ConsoleTraceListener(false);
            System.Diagnostics.Debug.Listeners.Add(ctl);

            System.Console.WriteLine("Diff Self Test...");

            // test all changes
            a = "a,b,c,d,e,f,g,h,i,j,k,l".Replace(',', '\n');
            b = "0,1,2,3,4,5,6,7,8,9".Replace(',', '\n');
            System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false))
                                            == "12.10.0.0*",
                                            "all-changes test failed.");
            System.Diagnostics.Debug.WriteLine("all-changes test passed.");
            // test all same
            a = "a,b,c,d,e,f,g,h,i,j,k,l".Replace(',', '\n');
            b = a;
            System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false))
                                            == "",
                                            "all-same test failed.");
            System.Diagnostics.Debug.WriteLine("all-same test passed.");

            // test snake
            a = "a,b,c,d,e,f".Replace(',', '\n');
            b = "b,c,d,e,f,x".Replace(',', '\n');
            System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false))
                                            == "1.0.0.0*0.1.6.5*",
                                            "snake test failed.");
            System.Diagnostics.Debug.WriteLine("snake test passed.");

            // 2002.09.20 - repro
            a = "c1,a,c2,b,c,d,e,g,h,i,j,c3,k,l".Replace(',', '\n');
            b = "C1,a,C2,b,c,d,e,I1,e,g,h,i,j,C3,k,I2,l".Replace(',', '\n');
            System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false))
                                            == "1.1.0.0*1.1.2.2*0.2.7.7*1.1.11.13*0.1.13.15*",
                                            "repro20020920 test failed.");
            System.Diagnostics.Debug.WriteLine("repro20020920 test passed.");

            // 2003.02.07 - repro
            a = "F".Replace(',', '\n');
            b = "0,F,1,2,3,4,5,6,7".Replace(',', '\n');
            System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false))
                                            == "0.1.0.0*0.7.1.2*",
                                            "repro20030207 test failed.");
            System.Diagnostics.Debug.WriteLine("repro20030207 test passed.");

            // Muegel - repro
            a = "HELLO\nWORLD";
            b = "\n\nhello\n\n\n\nworld\n";
            System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false))
                                            == "2.8.0.0*",
                                            "repro20030409 test failed.");
            System.Diagnostics.Debug.WriteLine("repro20030409 test passed.");

            // test some differences
            a = "a,b,-,c,d,e,f,f".Replace(',', '\n');
            b = "a,b,x,c,e,f".Replace(',', '\n');
            System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false))
                                            == "1.1.2.2*1.0.4.4*1.0.7.6*",
                                            "some-changes test failed.");
            System.Diagnostics.Debug.WriteLine("some-changes test passed.");

            // test one change within long chain of repeats
            a = "a,a,a,a,a,a,a,a,a,a".Replace(',', '\n');
            b = "a,a,a,a,-,a,a,a,a,a".Replace(',', '\n');
            System.Diagnostics.Debug.Assert(TestHelper(Diff.DiffText(a, b, false, false, false))
                                            == "0.1.4.4*1.0.9.10*",
                                            "long chain of repeats test failed.");

            System.Diagnostics.Debug.WriteLine("End.");
            System.Diagnostics.Debug.Flush();

            return(0);
        }
Exemplo n.º 3
0
        public static Dictionary<ExecutionCall,ExecutionCall> GeneratePathDiff(List<ExecutionCall> left, List<ExecutionCall> right)
        {
            Dictionary<ExecutionCall, ExecutionCall> sameMap = new Dictionary<ExecutionCall, ExecutionCall>();

            var flatLeft = FlattenPath(left);
            var flatRight = FlattenPath(right);

            StringBuilder sbLeft = new StringBuilder();
            foreach (var call in flatLeft)
            {
                SerializeCall(sbLeft, call);
            }

            StringBuilder sbRight = new StringBuilder();
            foreach (var call in flatRight)
            {
                SerializeCall(sbRight, call);
            }

            sbLeft.Length -= Environment.NewLine.Length;
            sbRight.Length -= Environment.NewLine.Length;

            var l = sbLeft.ToString();
            var r = sbRight.ToString();
            var diff = new Diff.Diff();
            var items = diff.DiffText(l, r);
            int pos = 0;

            List<int> diffItems = new List<int>();

            for (var n = 0; n < items.Length; n++)
            {
                Diff.Diff.Item i = items[n];
                while ((pos < i.StartB) && (pos < flatRight.Count))
                {
                    diffItems.Add(0);
                    pos++;
                }
                if (i.deletedA > 0)
                {
                    for (var m = 0; m < i.deletedA; m++)
                    {
                        diffItems.Add(-1);
                    }
                }
                if (pos < i.StartB + i.insertedB)
                {
                    while (pos < i.StartB + i.insertedB)
                    {
                        diffItems.Add(1);
                        pos++;
                    }
                }
            }

            while (pos < flatRight.Count)
            {
                diffItems.Add(0);
            }

            var leftPointer = 0;
            var rightPointer = 0;
            var lineIndex = 0;

            for (lineIndex = 0; lineIndex < diffItems.Count; lineIndex++)
            {
                var curDiff = diffItems[lineIndex];
                switch (curDiff)
                {
                    case 0:
                        sameMap.Add(flatLeft[leftPointer++], flatRight[rightPointer++]);
                        break;
                    case 1:
                        var call = flatRight[rightPointer++];
                        call.DiffStatus = DiffStatus.INSERT;
                        var parent = call.Parent;
                        while (parent != null)
                        {
                            if (parent.DiffStatus == DiffStatus.SAME)
                            {
                                parent.DiffStatus = DiffStatus.MODIFIED;
                            }
                            
                            parent = parent.Parent;
                        }
                        break;
                    case -1:
                        call = flatLeft[leftPointer++];
                        call.DiffStatus = DiffStatus.DELETE;
                        parent = call.Parent;
                        while (parent != null)
                        {
                            if (parent.DiffStatus == DiffStatus.SAME)
                            {
                                parent.DiffStatus = DiffStatus.MODIFIED;
                            }

                            parent = parent.Parent;
                        }
                        break;
                }
            }
            return sameMap;
        }