Exemple #1
0
    static public string DiffTextString(string a, string b, bool unchanged)
    {
        Diff.Item[] f = Diff.DiffText(a, b, true, true, false);
        if (f == null || f.Length == 0)
        {
            return(null);
        }

        StringBuilder sb = new StringBuilder();

        string[] aLines = a.Split('\n');
        string[] bLines = b.Split('\n');
        int      n      = 0;

        for (int fdx = 0; fdx < f.Length; fdx++)
        {
            Diff.Item aItem = f[fdx];

            // write unchanged lines
            while ((n < aItem.StartB) && (n < bLines.Length))
            {
                if (unchanged)
                {
                    sb.Append(n + ": " + bLines[n]);
                }
                n++;
            }

            // write deleted lines
            for (int m = 0; m < aItem.deletedA; m++)
            {
                sb.Append((unchanged ? "DEL:" : "") + aLines[aItem.StartA + m]);
            }

            // write inserted lines
            while (n < aItem.StartB + aItem.insertedB)
            {
                sb.Append(n + (unchanged ? "INS:" : ":") + bLines[n]);
                n++;
            }
        }

        // write rest of unchanged lines
        while (n < bLines.Length)
        {
            if (unchanged)
            {
                sb.Append(n + ": " + bLines[n]);
            }
            n++;
        }
        return(sb.ToString());
    }
        /// <summary>
        /// Compares the text.
        /// </summary>
        /// <param name="oldText">The old text.</param>
        /// <param name="newText">The new text.</param>
        /// <param name="displayInsertedText">if set to <c>true</c> [display inserted text].</param>
        /// <param name="displayDeletedText">if set to <c>true</c> [display deleted text].</param>
        /// <param name="insertedStyle">The inserted style.</param>
        /// <param name="deletedStyle">The deleted style.</param>
        /// <returns></returns>
        private static string CompareText(string oldText, string newText, bool displayInsertedText,
                                          bool displayDeletedText, string insertedStyle, string deletedStyle)
        {
            var sb    = new StringBuilder();
            var diffs = Diff.DiffText1(oldText, newText);

            int pos = 0;

            for (int n = 0; n < diffs.Length; n++)
            {
                Diff.Item it = diffs[n];

                // write unchanged chars
                while ((pos < it.StartB) && (pos < newText.Length))
                {
                    sb.Append(newText[pos]);
                    pos++;
                } // while

                // write deleted chars
                if (displayDeletedText && it.DeletedA > 0)
                {
                    sb.Append(deletedStyle);
                    for (int m = 0; m < it.DeletedA; m++)
                    {
                        sb.Append(oldText[it.StartA + m]);
                    } // for
                    sb.Append("</span>");
                }

                // write inserted chars
                if (displayInsertedText && pos < it.StartB + it.InsertedB)
                {
                    sb.Append(insertedStyle);
                    while (pos < it.StartB + it.InsertedB)
                    {
                        sb.Append(newText[pos]);
                        pos++;
                    } // while
                    sb.Append("</span>");
                }     // if
            }         // while

            // write rest of unchanged chars
            while (pos < newText.Length)
            {
                sb.Append(newText[pos]);
                pos++;
            } // while

            return(sb.ToString());
        }
Exemple #3
0
        private static void TestTextDiff(string a, string b)
        {
            var a1 = a.Replace(',', '\n');
            var b1 = b.Replace(',', '\n');
            var f  = Diff.DiffText(a1, b1, false, false, false);

            string[] aLines = a1.Split('\n');
            string[] bLines = b1.Split('\n');

            void WriteLine(int nr, string typ, string aText)
            {
                Console.WriteLine($"{typ}({nr}) - {aText}");
            }

            int n = 0;

            for (int fdx = 0; fdx < f.Length; fdx++)
            {
                Diff.Item aItem = f[fdx];

                // write unchanged lines
                while ((n < aItem.StartB) && (n < bLines.Length))
                {
                    //WriteLine(n, "", bLines[n]);
                    n++;
                } // while

                // write deleted lines
                for (int m = 0; m < aItem.deletedA; m++)
                {
                    WriteLine(n, "delete", aLines[aItem.StartA + m]);
                } // for

                // write inserted lines
                while (n < aItem.StartB + aItem.insertedB)
                {
                    WriteLine(n, "add", bLines[n]);
                    n++;
                } // while
            }     // while

            // write rest of unchanged lines
            while (n < bLines.Length)
            {
                WriteLine(n, null, bLines[n]);
                n++;
            } // while
        }
Exemple #4
0
        internal static DiffResult GetDiff(Mathertel.Diff.Item[] Results, string Source, string Destination, char Splitter)
        {
            DiffResult DResult = new DiffResult();

            int n = 0;

            string[] aLines = Source.Split(Splitter);
            string[] bLines = Destination.Split(Splitter);

            foreach (Diff.Item Result in Results)
            {
                Diff.Item aItem = Result;

                // write unchanged lines
                while ((n < aItem.StartB) && (n < bLines.Length))
                {
                    DResult.UnChanged.Add(n + 1);
                    DResult.UnChangedSections.Add(bLines[n]);
                    n++;
                } // while

                // write deleted lines
                for (int m = 0; m < aItem.deletedA; m++)
                {
                    DResult.Deleted.Add(aItem.StartA + m + 1);
                    DResult.MissingAt.Add(n + 1);
                    DResult.DeletedSections.Add(aLines[aItem.StartA + m]);
                } // for

                // write inserted lines
                while (n < aItem.StartB + aItem.insertedB)
                {
                    DResult.Inserted.Add(n + 1);
                    DResult.InsertedSections.Add(bLines[n]);
                    n++;
                } // while
            }     // while

            // write rest of unchanged lines
            while (n < bLines.Length)
            {
                DResult.UnChanged.Add(n + 1);
                DResult.UnChangedSections.Add(bLines[n]);
                n++;
            } // while

            return(DResult);
        }
Exemple #5
0
        private static void TestIntDiff(int[] a, int[] b)
        {
            var f = Diff.DiffInt(a, b);

            void WriteLine(int nr, string typ, int num)
            {
                Console.WriteLine($"{typ}({nr}) - {num}");
            }

            int n = 0;

            for (int fdx = 0; fdx < f.Length; fdx++)
            {
                Diff.Item aItem = f[fdx];

                // write unchanged lines
                while ((n < aItem.StartB) && (n < b.Length))
                {
                    //WriteLine(n, "", bLines[n]);
                    n++;
                } // while

                // write deleted lines
                for (int m = 0; m < aItem.deletedA; m++)
                {
                    WriteLine(n, "delete", a[aItem.StartA + m]);
                } // for

                // write inserted lines
                while (n < aItem.StartB + aItem.insertedB)
                {
                    WriteLine(n, "add", b[n]);
                    n++;
                } // while
            }     // while

            // write rest of unchanged lines
            while (n < b.Length)
            {
                WriteLine(n, null, b[n]);
                n++;
            } // while
        }
Exemple #6
0
 public static Diff.DiffResults GetResults(string partA, string partB, ref Diff.Item[] items)
 {
     Diff.DiffResults result = new Diff.DiffResults();
     foreach (Diff.Item i in items)
     {
         int       pos = 0;
         Diff.Item it  = i;
         //  deleted chars
         if ((it.deletedA > 0))
         {
             string      seq = "";
             Diff.Delete del = new Diff.Delete(partA, it.StartA, it.deletedA);
             for (int m = 0; m < it.deletedA; m++)
             {
                 seq = seq + partA.Substring(it.StartA + m, 1);
             }
             del.Sequence = seq;
             result.Deletes.Add(del);
         }
         //  inserted chars
         if ((it.insertedB > 0))
         {
             string      seq    = "";
             int         pos2   = it.StartB;
             Diff.Insert insert = new Diff.Insert(partB, it.StartB, it.insertedB);
             while ((pos2 < (it.StartB + it.insertedB)))
             {
                 seq   = seq + partB.Substring(pos2, 1);
                 pos2 += 1;
             }
             insert.Sequence = seq;
             result.Inserts.Add(insert);
         }
     }
     return(result);
 }
        // diffLocation - desired output location. Filename will NOT be appended
        public void diffReports(string firstReportLocation, string secondReportLocation, string diffLocation)
        {
            // create reader & open file
            TextReader tr1 = new StreamReader(firstReportLocation);
            TextReader tr2 = new StreamReader(secondReportLocation);

            // read a files
            string file1 = tr1.ReadToEnd();
            string file2 = tr2.ReadToEnd();

            string[] file1Lines = Regex.Split(file1, "\r|\n|(\r\n)");
            string[] file2Lines = Regex.Split(file2, "\r|\n|(\r\n)");

            // close the stream
            tr1.Close();
            tr2.Close();

            // get diffs
            //string reportString = "";
            Diff diffUtil = new Diff();

            Diff.Item[] diffs = diffUtil.DiffText(file1, file2);

            // initialize the list that we will use to insert new lines and build diff IFQ
            List <string> mergeList = new List <string>();
            int           pos       = 0;

            for (int n = 0; n < diffs.Length; n++)
            {
                Diff.Item it = diffs[n];

                // write unchanged lines
                while ((pos < it.StartB) && (pos < file2Lines.Length))
                {
                    mergeList.Add(file2Lines[pos]);
                    pos++;
                } // while
                if (it.deletedA != it.insertedB)
                {
                    // write deleted chars
                    if (it.deletedA > 0)
                    {
                        mergeList.Add("<div style='color: #ff0000; text-decoration: line-through';>");
                        for (int m = 0; m < it.deletedA; m++)
                        {
                            mergeList.Add(file1Lines[it.StartA + m]);
                        } // for
                        mergeList.Add("</div>");
                    }
                }

                // write inserted chars
                if (pos < it.StartB + it.insertedB)
                {
                    mergeList.Add("<div style='color: #008E00'>");
                    while (pos < it.StartB + it.insertedB)
                    {
                        mergeList.Add(file2Lines[pos]);
                        pos++;
                    } // while
                    mergeList.Add("</div>");
                }     // if
            }         // while

            // write rest of unchanged chars
            while (pos < file2Lines.Length)
            {
                mergeList.Add(file2Lines[pos]);
                pos++;
            }

            /*  Saving Output */
            FileStream fStream = new FileStream(
                diffLocation,
                FileMode.Create,
                FileAccess.Write,
                FileShare.Read);
            StreamWriter sw = new StreamWriter(fStream);

            string[] reportStrings = mergeList.ToArray();
            sw.Write(string.Join("\n", reportStrings));
            sw.Close();      // Close the Stream
            fStream.Close(); // Close the File
        }