private void ReadToken(ref uint pos, ref bool couldRead, byte[] tokenBuffer,
                               string tokenString, JsonParserTokenContinuation jsonParserTokenContinuation)
        {
            _unmanagedWriteBuffer.Clear();
            _state.CurrentTokenType      = JsonParserToken.Float;
            _expectedTokenBuffer         = tokenBuffer;
            _expectedTokenBufferPosition = 1;
            _expectedTokenString         = tokenString;
            if (EnsureRestOfToken(ref pos) == false)
            {
                _state.Continuation = jsonParserTokenContinuation;
                return;
            }

            _unmanagedWriteBuffer.Write(tokenBuffer, 0, tokenBuffer.Length);
            _unmanagedWriteBuffer.EnsureSingleChunk(_state);
            couldRead = true;
        }
Beispiel #2
0
        private bool ParseString()
        {
            fixed(byte *inputBufferPtr = _inputBuffer)
            {
                while (true)
                {
                    _currentStrStart = _pos;
                    while (_pos < _bufSize)
                    {
                        var b = inputBufferPtr[_pos++];
                        _charPos++;
                        if (_escapeMode == false)
                        {
                            if (b == _currentQuote)
                            {
                                _stringBuffer.Write(inputBufferPtr + _currentStrStart, _pos - _currentStrStart - 1
                                                    /*don't include the last quote*/);
                                return(true);
                            }
                            if (b == (byte)'\\')
                            {
                                _escapeMode = true;
                                _stringBuffer.Write(inputBufferPtr + _currentStrStart, _pos - _currentStrStart - 1
                                                    /*don't include the escape */);
                                _currentStrStart = _pos;
                            }
                        }
                        else
                        {
                            _currentStrStart++;
                            _escapeMode = false;
                            _charPos++;
                            if (b != (byte)'u')
                            {
                                _state.EscapePositions.Add(_stringBuffer.SizeInBytes - _prevEscapePosition);
                                _prevEscapePosition = _stringBuffer.SizeInBytes + 1;
                            }

                            switch (b)
                            {
                            case (byte)'r':
                                _stringBuffer.WriteByte((byte)'\r');
                                break;

                            case (byte)'n':
                                _stringBuffer.WriteByte((byte)'\n');
                                break;

                            case (byte)'b':
                                _stringBuffer.WriteByte((byte)'\b');
                                break;

                            case (byte)'f':
                                _stringBuffer.WriteByte((byte)'\f');
                                break;

                            case (byte)'t':
                                _stringBuffer.WriteByte((byte)'\t');
                                break;

                            case (byte)'"':
                            case (byte)'\\':
                            case (byte)'/':
                                _stringBuffer.WriteByte(b);
                                break;

                            case (byte)'\r':    // line continuation, skip
                                                // flush the buffer, but skip the \,\r chars
                                if (_pos >= _bufSize)
                                {
                                    return(false);
                                }

                                _line++;
                                _charPos = 1;
                                if (_pos >= _bufSize)
                                {
                                    return(false);
                                }

                                if (inputBufferPtr[_pos] == (byte)'\n')
                                {
                                    _pos++;     // consume the \,\r,\n
                                }
                                break;

                            case (byte)'\n':
                                _line++;
                                _charPos = 1;
                                break;      // line continuation, skip

                            case (byte)'u': // unicode value
                                if (ParseUnicodeValue() == false)
                                {
                                    return(false);
                                }

                                break;

                            default:
                                throw new InvalidOperationException("Invalid escape char, numeric value is " + b);
                            }
                        }
                    }
                    // copy the buffer to the native code, then refill
                    _stringBuffer.Write(inputBufferPtr + _currentStrStart, _pos - _currentStrStart);
                    if (_pos >= _bufSize)
                    {
                        return(false);
                    }
                }
            }
        }