Ejemplo n.º 1
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);
        }