Skip to content

Commit 5aaeb83

Browse files
rvanvelzenondrejmirtes
authored andcommitted
Optimize Lexer::tokenize()
1 parent 07c49b0 commit 5aaeb83

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

src/Lexer/Lexer.php

+5-10
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,11 @@ public function tokenize(string $s): array
105105
assert($this->regexp !== null);
106106
assert($this->types !== null);
107107

108-
preg_match_all($this->regexp, $s, $tokens, PREG_SET_ORDER);
109-
110-
$count = count($this->types);
111-
foreach ($tokens as &$match) {
112-
for ($i = 1; $i <= $count; $i++) {
113-
if ($match[$i] !== null && $match[$i] !== '') {
114-
$match = [$match[0], $this->types[$i - 1]];
115-
break;
116-
}
117-
}
108+
preg_match_all($this->regexp, $s, $matches, PREG_SET_ORDER);
109+
110+
$tokens = [];
111+
foreach ($matches as $match) {
112+
$tokens[] = [$match[0], $this->types[count($match) - 2]];
118113
}
119114

120115
$tokens[] = ['', self::TOKEN_END];

0 commit comments

Comments
 (0)