public void AddSubstring(byte[] str, int start, int length)
 {
     ByteArrayUtil.EnsureCapacity(ref buffer, offset, length);
     for (int i = 0; i < length; i++)
     {
         buffer[offset + i] = str[start + i];
     }
     offset += length;
 }
Beispiel #2
0
 public override void WriteTrue()
 {
     ByteArrayUtil.EnsureCapacity(ref buffer, offset, 4);
     buffer[offset + 0] = (byte)'t';
     buffer[offset + 1] = (byte)'r';
     buffer[offset + 2] = (byte)'u';
     buffer[offset + 3] = (byte)'e';
     offset            += 4;
 }
 public void AddString(byte[] str)
 {
     ByteArrayUtil.EnsureCapacity(ref buffer, offset, str.Length);
     for (int i = 0; i < str.Length; i++)
     {
         buffer[offset + i] = str[i];
     }
     offset += str.Length;
 }
Beispiel #4
0
 public override void WriteNull()
 {
     ByteArrayUtil.EnsureCapacity(ref buffer, offset, 4);
     buffer[offset + 0] = (byte)'n';
     buffer[offset + 1] = (byte)'u';
     buffer[offset + 2] = (byte)'l';
     buffer[offset + 3] = (byte)'l';
     offset            += 4;
 }
 public void AddPadding(byte c, int count)
 {
     ByteArrayUtil.EnsureCapacity(ref buffer, offset, count);
     for (int i = 0; i < count; i++)
     {
         buffer[offset + i] = c;
     }
     offset += count;
 }
Beispiel #6
0
 public override void WriteFalse()
 {
     ByteArrayUtil.EnsureCapacity(ref buffer, offset, 5);
     buffer[offset + 0] = (byte)'f';
     buffer[offset + 1] = (byte)'a';
     buffer[offset + 2] = (byte)'l';
     buffer[offset + 3] = (byte)'s';
     buffer[offset + 4] = (byte)'e';
     offset            += 5;
 }
Beispiel #7
0
 public override void WriteBoolean(bool value)
 {
     if (value)
     {
         ByteArrayUtil.EnsureCapacity(ref buffer, offset, 4);
         buffer[offset + 0] = (byte)'t';
         buffer[offset + 1] = (byte)'r';
         buffer[offset + 2] = (byte)'u';
         buffer[offset + 3] = (byte)'e';
         offset            += 4;
     }
     else
     {
         ByteArrayUtil.EnsureCapacity(ref buffer, offset, 5);
         buffer[offset + 0] = (byte)'f';
         buffer[offset + 1] = (byte)'a';
         buffer[offset + 2] = (byte)'l';
         buffer[offset + 3] = (byte)'s';
         buffer[offset + 4] = (byte)'e';
         offset            += 5;
     }
 }
 public void AddCharacter(byte str)
 {
     ByteArrayUtil.EnsureCapacity(ref buffer, offset, 1);
     buffer[offset++] = str;
 }
Beispiel #9
0
 public override void WriteRaw(byte rawValue)
 {
     ByteArrayUtil.EnsureCapacity(ref buffer, offset, 1);
     buffer[offset++] = rawValue;
 }
Beispiel #10
0
 public Span <byte> GetBuffer(int offset, int appendLength)
 {
     ByteArrayUtil.EnsureCapacity(ref this.buffer, offset, appendLength);
     return(new Span <byte>(this.buffer, offset, appendLength));
 }
Beispiel #11
0
 public override void EnsureCapacity(int offset, int appendLength)
 {
     ByteArrayUtil.EnsureCapacity(ref this.buffer, offset, appendLength);
 }
Beispiel #12
0
 public override void WriteBeginArray()
 {
     ByteArrayUtil.EnsureCapacity(ref buffer, offset, 1);
     buffer[offset++] = (byte)'[';
 }
Beispiel #13
0
 public override void WriteQuotation()
 {
     ByteArrayUtil.EnsureCapacity(ref buffer, offset, 1);
     buffer[offset++] = (byte)'\"';
 }
Beispiel #14
0
 public override void WriteNameSeparator()
 {
     ByteArrayUtil.EnsureCapacity(ref buffer, offset, 1);
     buffer[offset++] = (byte)':';
 }
Beispiel #15
0
 public override void WriteEndObject()
 {
     ByteArrayUtil.EnsureCapacity(ref buffer, offset, 1);
     buffer[offset++] = (byte)'}';
 }
Beispiel #16
0
        public override void WriteString(string value)
        {
            if (value == null)
            {
                WriteNull();
                return;
            }

            // single-path escape

            // nonescaped-ensure
            var startoffset = offset;
            var max         = StringEncoding.UTF8.GetMaxByteCount(value.Length) + 2;

            ByteArrayUtil.EnsureCapacity(ref buffer, startoffset, max);

            var from = 0;

            //var to = value.Length;

            buffer[offset++] = (byte)'\"';

            // for JIT Optimization, for-loop i < str.Length
            for (int i = 0; i < value.Length; i++)
            {
                byte escapeChar = default(byte);
                switch (value[i])
                {
                case '"':
                    escapeChar = (byte)'"';
                    break;

                case '\\':
                    escapeChar = (byte)'\\';
                    break;

                case '\b':
                    escapeChar = (byte)'b';
                    break;

                case '\f':
                    escapeChar = (byte)'f';
                    break;

                case '\n':
                    escapeChar = (byte)'n';
                    break;

                case '\r':
                    escapeChar = (byte)'r';
                    break;

                case '\t':
                    escapeChar = (byte)'t';
                    break;

                // use switch jumptable
                case (char)0:
                case (char)1:
                case (char)2:
                case (char)3:
                case (char)4:
                case (char)5:
                case (char)6:
                case (char)7:
                case (char)11:
                case (char)14:
                case (char)15:
                case (char)16:
                case (char)17:
                case (char)18:
                case (char)19:
                case (char)20:
                case (char)21:
                case (char)22:
                case (char)23:
                case (char)24:
                case (char)25:
                case (char)26:
                case (char)27:
                case (char)28:
                case (char)29:
                case (char)30:
                case (char)31:
                case (char)32:
                case (char)33:
                case (char)35:
                case (char)36:
                case (char)37:
                case (char)38:
                case (char)39:
                case (char)40:
                case (char)41:
                case (char)42:
                case (char)43:
                case (char)44:
                case (char)45:
                case (char)46:
                case (char)47:
                case (char)48:
                case (char)49:
                case (char)50:
                case (char)51:
                case (char)52:
                case (char)53:
                case (char)54:
                case (char)55:
                case (char)56:
                case (char)57:
                case (char)58:
                case (char)59:
                case (char)60:
                case (char)61:
                case (char)62:
                case (char)63:
                case (char)64:
                case (char)65:
                case (char)66:
                case (char)67:
                case (char)68:
                case (char)69:
                case (char)70:
                case (char)71:
                case (char)72:
                case (char)73:
                case (char)74:
                case (char)75:
                case (char)76:
                case (char)77:
                case (char)78:
                case (char)79:
                case (char)80:
                case (char)81:
                case (char)82:
                case (char)83:
                case (char)84:
                case (char)85:
                case (char)86:
                case (char)87:
                case (char)88:
                case (char)89:
                case (char)90:
                case (char)91:
                default:
                    continue;
                }

                max += 2;
                ByteArrayUtil.EnsureCapacity(ref buffer, startoffset, max); // check +escape capacity

                offset          += StringEncoding.UTF8.GetBytes(value, from, i - from, buffer, offset);
                from             = i + 1;
                buffer[offset++] = (byte)'\\';
                buffer[offset++] = escapeChar;
            }

            if (from != value.Length)
            {
                offset += StringEncoding.UTF8.GetBytes(value, from, value.Length - from, buffer, offset);
            }

            buffer[offset++] = (byte)'\"';
        }
 public void AddStringSlow(string str)
 {
     ByteArrayUtil.EnsureCapacity(ref buffer, offset, StringEncoding.UTF8.GetMaxByteCount(str.Length));
     offset += StringEncoding.UTF8.GetBytes(str, 0, str.Length, buffer, offset);
 }
Beispiel #18
0
        private void ReadStringSegmentCore(out byte[] resultBytes, out int resultOffset, out int resultLength)
        {
            // SkipWhiteSpace is already called from IsNull

            byte[] builder       = null;
            var    builderOffset = 0;

            char[] codePointStringBuffer = null;
            var    codePointStringOffet  = 0;

            if (this.bytes[this.offset] != '\"')
            {
                throw this.CreateParsingException("String Begin Token");
            }

            this.offset++;

            var from = this.offset;

            // eliminate array-bound check
            for (int i = this.offset; i < this.bytes.Length; i++)
            {
                byte escapeCharacter = 0;
                switch (this.bytes[i])
                {
                case (byte)'\\':     // escape character
                    switch ((char)this.bytes[i + 1])
                    {
                    case '"':
                    case '\\':
                    case '/':
                        escapeCharacter = this.bytes[i + 1];
                        goto COPY;

                    case 'b':
                        escapeCharacter = (byte)'\b';
                        goto COPY;

                    case 'f':
                        escapeCharacter = (byte)'\f';
                        goto COPY;

                    case 'n':
                        escapeCharacter = (byte)'\n';
                        goto COPY;

                    case 'r':
                        escapeCharacter = (byte)'\r';
                        goto COPY;

                    case 't':
                        escapeCharacter = (byte)'\t';
                        goto COPY;

                    case 'u':
                        if (codePointStringBuffer == null)
                        {
                            codePointStringBuffer = StringBuilderCache.GetCodePointStringBuffer();
                        }

                        if (codePointStringOffet == 0)
                        {
                            if (builder == null)
                            {
                                builder = StringBuilderCache.GetBuffer();
                            }

                            var copyCount = i - from;
                            ByteArrayUtil.EnsureCapacity(ref builder, builderOffset, copyCount + 1);         // require + 1
                            Buffer.BlockCopy(this.bytes, from, builder, builderOffset, copyCount);
                            builderOffset += copyCount;
                        }

                        if (codePointStringBuffer.Length == codePointStringOffet)
                        {
                            Array.Resize(ref codePointStringBuffer, codePointStringBuffer.Length * 2);
                        }

                        var a         = (char)this.bytes[i + 2];
                        var b         = (char)this.bytes[i + 3];
                        var c         = (char)this.bytes[i + 4];
                        var d         = (char)this.bytes[i + 5];
                        var codepoint = GetCodePoint(a, b, c, d);
                        codePointStringBuffer[codePointStringOffet++] = (char)codepoint;
                        i           += 5;
                        this.offset += 6;
                        from         = this.offset;
                        continue;

                    default:
                        throw this.CreateParsingExceptionMessage("Bad JSON escape.");
                    }

                case (byte)'"':     // endtoken
                    this.offset++;
                    goto END;

                default:     // string
                    if (codePointStringOffet != 0)
                    {
                        if (builder == null)
                        {
                            builder = StringBuilderCache.GetBuffer();
                        }

                        ByteArrayUtil.EnsureCapacity(ref builder, builderOffset, StringEncoding.UTF8.GetMaxByteCount(codePointStringOffet));
                        builderOffset       += StringEncoding.UTF8.GetBytes(codePointStringBuffer, 0, codePointStringOffet, builder, builderOffset);
                        codePointStringOffet = 0;
                    }
                    this.offset++;
                    continue;
                }

COPY:
                {
                    if (builder == null)
                    {
                        builder = StringBuilderCache.GetBuffer();
                    }

                    if (codePointStringOffet != 0)
                    {
                        ByteArrayUtil.EnsureCapacity(ref builder, builderOffset, StringEncoding.UTF8.GetMaxByteCount(codePointStringOffet));
                        builderOffset       += StringEncoding.UTF8.GetBytes(codePointStringBuffer, 0, codePointStringOffet, builder, builderOffset);
                        codePointStringOffet = 0;
                    }

                    var copyCount = i - from;
                    ByteArrayUtil.EnsureCapacity(ref builder, builderOffset, copyCount + 1); // require + 1!
                    Buffer.BlockCopy(this.bytes, from, builder, builderOffset, copyCount);
                    builderOffset           += copyCount;
                    builder[builderOffset++] = escapeCharacter;
                    i           += 1;
                    this.offset += 2;
                    from         = this.offset;
                }
            }

            resultLength = 0;
            resultBytes  = null;
            resultOffset = 0;
            throw this.CreateParsingException("String End Token");

END:
            if (builderOffset == 0 && codePointStringOffet == 0) // no escape
            {
                resultBytes  = this.bytes;
                resultOffset = from;
                resultLength = this.offset - 1 - from; // skip last quote
            }
            else
            {
                if (builder == null)
                {
                    builder = StringBuilderCache.GetBuffer();
                }

                if (codePointStringOffet != 0)
                {
                    ByteArrayUtil.EnsureCapacity(ref builder, builderOffset, StringEncoding.UTF8.GetMaxByteCount(codePointStringOffet));
                    builderOffset       += StringEncoding.UTF8.GetBytes(codePointStringBuffer, 0, codePointStringOffet, builder, builderOffset);
                    codePointStringOffet = 0;
                }

                var copyCount = this.offset - from - 1;
                ByteArrayUtil.EnsureCapacity(ref builder, builderOffset, copyCount);
                Buffer.BlockCopy(this.bytes, from, builder, builderOffset, copyCount);
                builderOffset += copyCount;

                resultBytes  = builder;
                resultOffset = 0;
                resultLength = builderOffset;
            }
        }