示例#1
0
 public void testWriteLine1()
 {
     var a = new RawText(Constants.encodeASCII("foo-a\nfoo-b\n"));
     var o = new MemoryStream();
     a.writeLine(o, 0);
     byte[] r = o.ToArray();
     Assert.AreEqual("foo-a", RawParseUtils.decode(r));
 }
示例#2
0
        public void testEquals()
        {
            var a = new RawText(Constants.encodeASCII("foo-a\nfoo-b\n"));
            var b = new RawText(Constants.encodeASCII("foo-b\nfoo-c\n"));

            Assert.AreEqual(2, a.size());
            Assert.AreEqual(2, b.size());

            // foo-a != foo-b
            Assert.IsFalse(a.equals(0, b, 0));
            Assert.IsFalse(b.equals(0, a, 0));

            // foo-b == foo-b
            Assert.IsTrue(a.equals(1, b, 0));
            Assert.IsTrue(b.equals(0, a, 1));
        }
示例#3
0
 public void testWriteLine3()
 {
     var a = new RawText(Constants.encodeASCII("a\n\nb\n"));
     var o = new MemoryStream();
     a.writeLine(o, 1);
     byte[] r = o.ToArray();
     Assert.AreEqual(string.Empty, RawParseUtils.decode(r));
 }
示例#4
0
 public void testEmpty()
 {
     var r = new RawText(new byte[0]);
     Assert.AreEqual(0, r.size());
 }
示例#5
0
        /// <summary>
        /// Format a patch script, reusing a previously parsed FileHeader.
        ///	<para />
        ///	This formatter is primarily useful for editing an existing patch script
        ///	to increase or reduce the number of lines of context within the script.
        ///	All header lines are reused as-is from the supplied FileHeader.
        /// </summary>
        /// <param name="out">stream to write the patch script out to.</param>
        /// <param name="head">existing file header containing the header lines to copy.</param>
        /// <param name="a">
        /// Text source for the pre-image version of the content. 
        /// This must match the content of <seealso cref="FileHeader.getOldId()"/>.
        /// </param>
        /// <param name="b">writing to the supplied stream failed.</param>
        public void format(Stream @out, FileHeader head, RawText a, RawText b)
        {
            // Reuse the existing FileHeader as-is by blindly copying its
            // header lines, but avoiding its hunks. Instead we recreate
            // the hunks from the text instances we have been supplied.
            //
            int start = head.StartOffset;
            int end = head.EndOffset;

            if (!head.Hunks.isEmpty())
            {
                end = head.Hunks[0].StartOffset;
            }

            @out.Write(head.Buffer, start, end - start);

            FormatEdits(@out, a, b, head.ToEditList());
        }
示例#6
0
        private void FormatEdits(Stream @out, RawText a, RawText b, EditList edits)
        {
            for (int curIdx = 0; curIdx < edits.Count; /* */)
            {
                Edit curEdit = edits.get(curIdx);
                int endIdx = FindCombinedEnd(edits, curIdx);
                Edit endEdit = edits.get(endIdx);

                int aCur = Math.Max(0, curEdit.BeginA - _context);
                int bCur = Math.Max(0, curEdit.BeginB - _context);
                int aEnd = Math.Min(a.size(), endEdit.EndA + _context);
                int bEnd = Math.Min(b.size(), endEdit.EndB + _context);

                WriteHunkHeader(@out, aCur, aEnd, bCur, bEnd);

                while (aCur < aEnd || bCur < bEnd)
                {
                    if (aCur < curEdit.BeginA || endIdx + 1 < curIdx)
                    {
                        WriteLine(@out, ' ', a, aCur);
                        aCur++;
                        bCur++;
                    }
                    else if (aCur < curEdit.EndA)
                    {
                        WriteLine(@out, '-', a, aCur++);

                    }
                    else if (bCur < curEdit.EndB)
                    {
                        WriteLine(@out, '+', b, bCur++);
                    }

                    if (End(curEdit, aCur, bCur) && ++curIdx < edits.Count)
                    {
                        curEdit = edits.get(curIdx);
                    }
                }
            }
        }
示例#7
0
 private static void WriteLine(Stream @out, char prefix, RawText text, int cur)
 {
     @out.WriteByte(Convert.ToByte(prefix));
     text.writeLine(@out, cur);
     @out.WriteByte(Convert.ToByte('\n'));
     if (cur + 1 == text.size() && text.isMissingNewlineAtEnd())
     {
         @out.Write(NoNewLine, 0, NoNewLine.Length);
     }
 }
 private void Init(string name)
 {
     a = new RawText(ReadFile(name + "_PreImage"));
     b = new RawText(ReadFile(name + "_PostImage"));
     file = ParseTestPatchFile(DiffsDir + name + ".patch").getFiles()[0];
 }
示例#9
0
	    private static bool equals(RawText a, int ai, RawText b, int bi)
        {
		    if (a.hashes.get(ai) != b.hashes.get(bi))
			    return false;

		    int a_start = a.lines.get(ai);
		    int b_start = b.lines.get(bi);
		    int a_end = a.lines.get(ai + 1);
		    int b_end = b.lines.get(bi + 1);

		    if (a_end - a_start != b_end - b_start)
			    return false;

		    while (a_start < a_end) {
			    if (a.content[a_start++] != b.content[b_start++])
				    return false;
		    }
		    return true;
	    }