Example #1
0
 private static DiffItem Next(DiffList diffList, int diffIndex)
 {
     if (diffIndex < diffList.Count)
     {
         return(diffList[diffIndex]);
     }
     return(null);
 }
Example #2
0
        public static DiffList Compress(DiffList diffList)
        {
            DiffList result = new DiffList();

            foreach (DiffItem diffItem in diffList)
            {
                if (diffItem.Status != DiffStatus.Unchanged)
                {
                    result.Add(diffItem);
                }
            }

            return(result);
        }
Example #3
0
        public static System.IO.TextReader Patch(System.IO.TextReader source, DiffList diffList)
        {
            System.Collections.ArrayList sourceList = new System.Collections.ArrayList();

            string line;

            while ((line = source.ReadLine()) != null)
            {
                sourceList.Add(line);
            }

            System.Collections.ArrayList results = Patch(sourceList, diffList);

            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
            System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(memoryStream);

            int i = 0;

            foreach (string result in results)
            {
                if (i == (results.Count - 1))
                {
                    streamWriter.Write(result);
                }
                else
                {
                    streamWriter.WriteLine(result);
                }
                i++;
            }
            streamWriter.Flush();

            memoryStream.Position = 0;

            System.IO.StreamReader streamReader = new System.IO.StreamReader(memoryStream);
            return(streamReader);
        }
Example #4
0
        public static DiffList Diff(System.Collections.IList source, System.Collections.IList target)
        {
            DiffList results = new DiffList();

            int i = 0;
            int j = 0;
            int k = 0;

            while (!(i == source.Count && j == target.Count))
            {
                //End of target.
                if (j == target.Count)
                {
                    results.Add(new DiffItem(DiffStatus.Deleted, i, source[i]));
                    i++;
                    continue;
                }

                //End of source.
                if (i == source.Count)
                {
                    results.Add(new DiffItem(DiffStatus.Inserted, j, target[j]));
                    j++;
                    continue;
                }

                //End of source.
                if ((i + k) == source.Count)
                {
                    results.Add(new DiffItem(DiffStatus.Deleted, i, source[i]));
                    results.Add(new DiffItem(DiffStatus.Inserted, j, target[j]));
                    i++;
                    j++;
                    k = 0;
                    continue;
                }

                //End of target.
                if ((j + k) == target.Count)
                {
                    results.Add(new DiffItem(DiffStatus.Deleted, i, source[i]));
                    results.Add(new DiffItem(DiffStatus.Inserted, j, target[j]));
                    i++;
                    j++;
                    k = 0;
                    continue;
                }

                bool match_i = false;
                bool match_j = false;

                if (source[i + k].Equals(target[j]))
                {
                    match_i = true;
                }
                if (source[i].Equals(target[j + k]))
                {
                    match_j = true;
                }

                if (match_i && match_j)
                {
                    results.Add(new DiffItem(DiffStatus.Unchanged, i, source[i]));
                    i++;
                    j++;
                    k = 0;
                }
                else if (match_i)
                {
                    results.Add(new DiffItem(DiffStatus.Deleted, i, source[i]));
                    i++;
                    k = 0;
                }
                else if (match_j)
                {
                    results.Add(new DiffItem(DiffStatus.Inserted, j, target[j]));
                    j++;
                    k = 0;
                }
                else
                {
                    k++;
                }
            }

            return(results);
        }
Example #5
0
        public static System.Collections.ArrayList Patch(System.Collections.IList target, DiffList diffList)
        {
            System.Collections.ArrayList results = new System.Collections.ArrayList();

            diffList = Differ.Compress(diffList);

            int length = target.Count;

            foreach (DiffItem item in diffList)
            {
                switch (item.Status)
                {
                case DiffStatus.Deleted:
                {
                    length--;
                    break;
                }

                case DiffStatus.Inserted:
                {
                    length++;
                    break;
                }
                }
            }

            int      diffIndex = 0;
            DiffItem diffItem  = Next(diffList, diffIndex);

            int targetLineNumber = 0;
            int line             = 0;

            for (int i = 0; i < length; i++)
            {
                if (diffItem != null && diffItem.Position == targetLineNumber && diffItem.Status == DiffStatus.Deleted)
                {
                    //Skip a target line.
                    targetLineNumber++;

                    diffIndex++;
                    diffItem = Next(diffList, diffIndex);
                }


                if (diffItem != null && diffItem.Position == line && diffItem.Status == DiffStatus.Inserted)
                {
                    results.Add(diffItem.Value);
                    line++;

                    diffIndex++;
                    diffItem = Next(diffList, diffIndex);

                    continue;
                }


                results.Add(target[targetLineNumber]);
                targetLineNumber++;
                line++;
            }

            return(results);
        }