Skip to content

Commit

Permalink
Improve critical matching loop a little
Browse files Browse the repository at this point in the history
+7% speedup
  • Loading branch information
mvorisek committed Sep 5, 2024
1 parent 3ea1a4e commit 82e0827
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -948,17 +948,18 @@ private function removeNamedGroupsFromRegex(string $regex): array
*/
public function tokenize(string $string): Cursor
{
$tokens = [];
$tokenizeRegex = $this->tokenizeRegex;
$upper = strtoupper($string);

$upper = strtoupper($string);
$tokens = [];
$offset = 0;

while ($offset < strlen($string)) {
// Get the next token and the token type
preg_match($this->tokenizeRegex[0], $upper, $matches, 0, $offset);
preg_match($tokenizeRegex[0], $upper, $matches, 0, $offset);
assert(($matches[0] ?? '') !== '');

$lastMatchesNamedGroup = $this->tokenizeRegex[1][array_key_last($matches)];
$lastMatchesNamedGroup = $tokenizeRegex[1][array_key_last($matches)];
assert(str_starts_with($lastMatchesNamedGroup, 't_'));

/** @var Token::TOKEN_TYPE_* $tokenType */
Expand Down

0 comments on commit 82e0827

Please sign in to comment.