Ejemplo n.º 1
0
        internal static bool TryParse(string s, out DiffOp res)
        {
            if (string.IsNullOrEmpty(s))
            {
                res = null;
                return(true);
            }

            char   sign = s[0];
            string line = Uri.UnescapeDataString(s.Substring(1).Replace("+", "%2b"));

            if (sign == '-')             // Deletion
            {
                res = new DiffOp(Operation.DELETE, line);
            }
            else if (sign == '+')             // Insertion
            {
                res = new DiffOp(Operation.INSERT, line);
            }
            else if (sign == ' ')             // minori equality
            {
                res = new DiffOp(Operation.EQUAL, line);
            }
            else if (sign == '@')               // Start of next patch
            {
                res = null;
                return(false);
            }
            else               // ?? unrecognized sign.
            {
                throw new ArgumentException($"Invalid patch mode: '{sign}' in: {line}");
            }

            return(true);
        }
Ejemplo n.º 2
0
        public bool Equals(DiffOp obj)
        {
            // if parameter is null return false
            if (obj == null)
            {
                return(false);
            }

            //Return true if the fields match
            return(obj.Operation == Operation && obj.Text == Text);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Parse a textual represenation of patches and return a List of PatchOp objects.
        /// </summary>
        /// <param name="textline">Text representation of patches</param>
        /// <returns>List of PatchOp objects.</returns>
        public static List <PatchOp> PatchFromText(this string textline)
        {
            List <PatchOp> res = new List <PatchOp>();

            if (string.IsNullOrEmpty(textline) || textline == "<null>")
            {
                return(res);
            }

            string[] text = textline.Split('\n');
            int      pos  = 0;

            while (pos < text.Length)
            {
                if (!PatchOp.TryParse(text[pos], out PatchOp patch))
                {
                    throw new ArgumentException($"Invalid path string: {text[pos]}");
                }
                res.Add(patch);
                pos++;

                while (pos < text.Length)
                {
                    if (string.IsNullOrEmpty(text[pos]))
                    {
                        // Blank line; continue.
                        pos++;
                        continue;
                    }
                    if (text[pos].StartsWith("@"))                     // Start next patch.
                    {
                        break;
                    }

                    if (DiffOp.TryParse(text[pos], out DiffOp diff))
                    {
                        patch.Diffs.AddIfNotNull(diff);
                        pos++;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            return(res);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Add some padding on text start and end so that edges can match something.
        /// </summary>
        /// <param name="patches">List of PatchOp objects</param>
        /// <param name="margin">Chuck size for context length.</param>
        /// <returns>The padding added to each side.</returns>
        private static string AddPadding(this List <PatchOp> patches, short margin = 4)
        {
            string padding = string.Empty;

            for (short x = 1; x <= margin; x++)
            {
                padding += (char)x;
            }

            // Bump all the patches forward.
            foreach (var aPatch in patches)
            {
                aPatch.Start1 += margin;
                aPatch.Start2 += margin;
            }

            // Add some padding on start of first diff.
            PatchOp patch = patches.First();

            if (patch.Diffs.Count == 0 || patch.Diffs.First().Operation != Operation.EQUAL)
            {
                // Add padding equality.
                patch.Diffs.Insert(0, new DiffOp(Operation.EQUAL, padding));
                patch.Start1  -= margin;                // should be 0;
                patch.Start2  -= margin;                // should be 0;
                patch.Length1 += margin;
                patch.Length2 += margin;
            }
            else if (margin > patch.Diffs.First().Text.Length)
            {
                // Grow first equality.
                DiffOp fDif     = patch.Diffs.First();
                int    extraLen = margin - fDif.Text.Length;
                fDif.Text      = padding.Substring(fDif.Text.Length) + fDif.Text;
                patch.Start1  -= extraLen;
                patch.Start2  -= extraLen;
                patch.Length1 += extraLen;
                patch.Length2 += extraLen;
            }

            // Add some padding on end of last diff.
            patch = patches.Last();
            if (patch.Diffs.Count == 0 || patch.Diffs.Last().Operation != Operation.EQUAL)
            {
                // Add padding equality
                patch.Diffs.Add(new DiffOp(Operation.EQUAL, padding));
                patch.Length1 += margin;
                patch.Length2 += margin;
            }
            else if (margin > patch.Diffs.Last().Text.Length)
            {
                // Grow last equality.
                DiffOp lDif    = patch.Diffs.Last();
                int    extaLen = margin - lDif.Text.Length;
                lDif.Text     += padding.Substring(0, extaLen);
                patch.Length1 += extaLen;
                patch.Length2 += extaLen;
            }

            return(padding);
        }