예제 #1
0
        /**
         * Emulate GNU diff's format.
         * Header: @@ -382,8 +481,9 @@
         * Indices are printed as 1-based, not 0-based.
         * @return The GNU diff string.
         */
        public override string ToString()
        {
            string coords1, coords2;

            if (this.length1 == 0)
            {
                coords1 = this.start1 + ",0";
            }
            else if (this.length1 == 1)
            {
                coords1 = Convert.ToString(this.start1 + 1);
            }
            else
            {
                coords1 = (this.start1 + 1) + "," + this.length1;
            }

            if (this.length2 == 0)
            {
                coords2 = this.start2 + ",0";
            }
            else if (this.length2 == 1)
            {
                coords2 = Convert.ToString(this.start2 + 1);
            }
            else
            {
                coords2 = (this.start2 + 1) + "," + this.length2;
            }

            StringBuilder text = new StringBuilder();

            text.Append("@@ -").Append(coords1).Append(" +").Append(coords2)
            .Append(" @@\n");
            // Escape the body of the patch with %xx notation.
            foreach (Diff aDiff in this.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());
        }