Ejemplo n.º 1
0
        public static ReadOnlySequence <byte> TrimStart(this ReadOnlySequence <byte> buffer)
        {
            var reader   = new SequenceReader <byte>(buffer);
            var advanced = reader.AdvancePastAny((byte)' ', (byte)'\t', (byte)'\r');

            return(reader.Sequence.Slice(reader.Position));
        }
Ejemplo n.º 2
0
        public void Basic(bool singleSegment)
        {
            byte[] buffer = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            ReadOnlySequence <byte> bytes = singleSegment
                ? new ReadOnlySequence <byte>(buffer)
                : SequenceFactory.CreateSplit(buffer, 2, 4);

            SequenceReader <byte> skipReader = new SequenceReader <byte>(bytes);

            Assert.False(skipReader.TryAdvanceTo(10));
            Assert.True(skipReader.TryAdvanceTo(4, advancePastDelimiter: false));
            Assert.True(skipReader.TryRead(out byte value));
            Assert.Equal(4, value);

            Assert.True(skipReader.TryAdvanceToAny(new byte[] { 3, 12, 7 }, advancePastDelimiter: false));
            Assert.True(skipReader.TryRead(out value));
            Assert.Equal(7, value);
            Assert.Equal(1, skipReader.AdvancePast(8));
            Assert.True(skipReader.TryRead(out value));
            Assert.Equal(9, value);

            skipReader = new SequenceReader <byte>(bytes);
            Assert.Equal(0, skipReader.AdvancePast(2));
            Assert.Equal(3, skipReader.AdvancePastAny(new byte[] { 2, 3, 1 }));
            Assert.True(skipReader.TryRead(out value));
            Assert.Equal(4, value);
        }
Ejemplo n.º 3
0
        public void SkipWhiteSpace()
        {
            const byte space          = 0x20;
            const byte tab            = 0x09;
            const byte lineFeed       = 0x0a;
            const byte carriageReturn = 0x0d;

            Reader.AdvancePastAny(space, tab, lineFeed, carriageReturn);
        }
Ejemplo n.º 4
0
    public bool ParseRequest(ref SequenceReader <byte> reader)
    {
        switch (_requestProcessingStatus)
        {
        case RequestProcessingStatus.RequestPending:
            // Skip any empty lines (\r or \n) between requests.
            // Peek first as a minor performance optimization; it's a quick inlined check.
            if (reader.TryPeek(out byte b) && (b == ByteCR || b == ByteLF))
            {
                reader.AdvancePastAny(ByteCR, ByteLF);
            }

            if (reader.End)
            {
                break;
            }

            TimeoutControl.ResetTimeout(_requestHeadersTimeoutTicks, TimeoutReason.RequestHeaders);

            _requestProcessingStatus = RequestProcessingStatus.ParsingRequestLine;
            goto case RequestProcessingStatus.ParsingRequestLine;

        case RequestProcessingStatus.ParsingRequestLine:
            if (TakeStartLine(ref reader))
            {
                _requestProcessingStatus = RequestProcessingStatus.ParsingHeaders;
                goto case RequestProcessingStatus.ParsingHeaders;
            }
            else
            {
                break;
            }

        case RequestProcessingStatus.ParsingHeaders:
            if (TakeMessageHeaders(ref reader, trailers: false))
            {
                _requestProcessingStatus = RequestProcessingStatus.AppStarted;
                // Consumed preamble
                return(true);
            }
            break;
        }

        // Haven't completed consuming preamble
        return(false);
    }
Ejemplo n.º 5
0
        public void Lex()
        {
start:
            while (!reader.End)
            {
                reader.AdvancePastAny(" \r\n");
                reader.TryPeek(out var c);

                switch (c)
                {
                case '#':
                    reader.TryAdvanceTo('\n');
                    // ELSE: Advance to end
                    goto start;     // Avoid recursion

                case '{':
                    reader.Advance(1);
                    Current = new Token <TNumber>(SyntaxKind.OpenBrace);
                    return;

                case '}':
                    reader.Advance(1);
                    Current = new Token <TNumber>(SyntaxKind.CloseBrace);
                    return;

                case '[':
                    reader.Advance(1);
                    Current = new Token <TNumber>(SyntaxKind.OpenBracket);
                    return;

                case ']':
                    reader.Advance(1);
                    Current = new Token <TNumber>(SyntaxKind.CloseBracket);
                    return;

                case '(':
                    reader.Advance(1);
                    Current = new Token <TNumber>(SyntaxKind.OpenParenthesis);
                    return;

                case ')':
                    reader.Advance(1);
                    Current = new Token <TNumber>(SyntaxKind.CloseParenthesis);
                    return;

                case '=':
                    reader.Advance(1);
                    Current = new Token <TNumber>(SyntaxKind.Equals);
                    return;

                case '+':
                case '-':
                case '*':
                case '/':
                    Current = LexCompoundOperator();
                    return;

                case ':':
                    Current = LexColon();
                    return;

                case '<':
                case '>':
                    Current = LexRelationalOperator();
                    return;

                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                case '.':
                    Current = LexInteger();
                    return;

                default:
                    Current = LexIdentifier();
                    return;
                }
            }

            Current = default;
        }
Ejemplo n.º 6
0
 public long AdvancePastAny(ReadOnlyByteSpan values) => Data.AdvancePastAny(values);