diff --git a/src/Connection/ImapTokenizer.php b/src/Connection/ImapTokenizer.php index 11f4c38f..7d9c7b14 100644 --- a/src/Connection/ImapTokenizer.php +++ b/src/Connection/ImapTokenizer.php @@ -308,57 +308,28 @@ protected function readLiteral(): Literal */ protected function readNumberOrAtom(): Token { - $position = $this->position; + $length = 0; - // First char must be a digit to even consider a number. - if (! ctype_digit($this->buffer[$position] ?? '')) { - return $this->readAtom(); - } + // Look ahead without consuming digits until we can classify the token. + do { + $this->ensureBuffer($length + 1); - // Walk forward to find the end of the digit run. - while (ctype_digit($this->buffer[$position] ?? '')) { - $position++; + $digits = strspn($this->buffer, '0123456789', $this->position + $length); - $this->ensureBuffer($position - $this->position + 1); - } + $length += $digits; + } while ($digits > 0 && ! isset($this->buffer[$this->position + $length])); - $next = $this->buffer[$position] ?? null; + $next = $this->buffer[$this->position + $length] ?? null; - // If next is EOF or a delimiter, it's a Number. - if ($next === null || $this->isDelimiter($next)) { - return $this->readNumber(); + if ($length === 0 || ($next !== null && ! $this->isDelimiter($next))) { + return $this->readAtom(); } - // Otherwise it's an Atom. - return $this->readAtom(); - } - - /** - * Reads a number token. - * - * A number consists of one or more digit characters and represents a numeric value. - */ - protected function readNumber(): Number - { - $start = $this->position; - - while (true) { - $this->ensureBuffer(1); - - $char = $this->currentChar(); - - if ($char === null) { - break; - } - - if (! ctype_digit($char)) { - break; - } + $value = substr($this->buffer, $this->position, $length); - $this->advance(); - } + $this->advance($length); - return new Number(substr($this->buffer, $start, $this->position - $start)); + return new Number($value); } /** diff --git a/tests/Unit/Connection/ImapTokenizerTest.php b/tests/Unit/Connection/ImapTokenizerTest.php index 796284c0..ec46ae89 100644 --- a/tests/Unit/Connection/ImapTokenizerTest.php +++ b/tests/Unit/Connection/ImapTokenizerTest.php @@ -561,3 +561,54 @@ public function fgets(): string|false expect($tokenizer->nextToken())->toEqual(new EmailAddress('')); expect($tokenizer->nextToken())->toBeNull(); }); + +test('tokenizer reads numeric tokens across buffer boundaries', function (string $input, Token $expected, int $chunkSize) { + $stream = new FakeStream; + $stream->open(); + $stream->feedRaw(str_split($input, $chunkSize)); + + $tokenizer = new ImapTokenizer($stream); + + expect($tokenizer->nextToken())->toEqual($expected); +})->with([ + ['12345 ', new Number('12345')], + ['000123)', new Number('000123')], + ['0 ', new Number('0')], + ['123456789012345678901234567890 ', new Number('123456789012345678901234567890')], + ['123abc ', new Atom('123abc')], + ['123.45 ', new Atom('123.45')], + ['123"abc ', new Atom('123"abc')], + ['123\\abc ', new Atom('123\\abc')], + ["123\x80 ", new Atom("123\x80")], + ["123\t", new Atom('123')], + ["123\r\n", new Atom('123')], +])->with(range(1, 32)); + +test('tokenizer preserves a numeric token at end of stream', function (int $chunkSize) { + $stream = new FakeStream; + $stream->open(); + $stream->feedRaw(str_split('000123456789', $chunkSize)); + + $tokenizer = new ImapTokenizer($stream); + + expect($tokenizer->nextToken())->toEqual(new Number('000123456789')); + expect($tokenizer->nextToken())->toBeNull(); +})->with(range(1, 12)); + +test('tokenizer leaves numeric token delimiters for the next token', function (string $delimiter, Token $expected) { + $stream = new FakeStream; + $stream->open(); + $stream->feedRaw(['123', $delimiter]); + + $tokenizer = new ImapTokenizer($stream); + + expect($tokenizer->nextToken())->toEqual(new Number('123')); + expect($tokenizer->nextToken())->toEqual($expected); +})->with([ + ['(', new ListOpen('(')], + [')', new ListClose(')')], + ['[', new ResponseCodeOpen('[')], + [']', new ResponseCodeClose(']')], + ['<>', new EmailAddress('')], + ["{0}\r\n", new Literal('')], +]);