Ejemplo n.º 1
0
Archivo: Patch.cs Proyecto: sotaria/gsf
        /// <summary>
        /// Emulate GNU diff's format.
        /// Header: @@ -382,8 +481,9 @@
        /// Indices are printed as 1-based, not 0-based.
        /// </summary>
        /// <returns>The GNU diff string</returns>
        public override string ToString()
        {
            string coords1, coords2;

            if (Length1 == 0)
            {
                coords1 = Start1 + ",0";
            }
            else if (Length1 == 1)
            {
                coords1 = Convert.ToString(Start1 + 1);
            }
            else
            {
                coords1 = (Start1 + 1) + "," + Length1;
            }

            if (Length2 == 0)
            {
                coords2 = Start2 + ",0";
            }
            else if (Length2 == 1)
            {
                coords2 = Convert.ToString(Start2 + 1);
            }
            else
            {
                coords2 = (Start2 + 1) + "," + Length2;
            }

            StringBuilder text = new StringBuilder()
                                 .Append("@@ -")
                                 .Append(coords1)
                                 .Append(" +")
                                 .Append(coords2)
                                 .Append(" @@\n");

            // Escape the body of the patch with %xx notation.
            foreach (Diff aDiff in Diffs)
            {
                switch (aDiff.Operation)
                {
                case Operation.INSERT:
                    text.Append('+');
                    break;

                case Operation.DELETE:
                    text.Append('-');
                    break;

                case Operation.EQUAL:
                    text.Append(' ');
                    break;
                }

                text.Append(DiffMatchPatch.EncodeURI(aDiff.Text)).Append("\n");
            }

            return(text.ToString());
        }