Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4a07085

Browse files
rvanvelzenondrejmirtes
authored andcommitted
Micro-optimize lexer
1 parent 76150ae commit 4a07085

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

‎src/Lexer/Lexer.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
namespace PHPStan\PhpDocParser\Lexer;
44

5-
use function array_keys;
6-
use function assert;
7-
use function count;
85
use function implode;
96
use function preg_match_all;
107
use const PREG_SET_ORDER;
@@ -93,23 +90,17 @@ class Lexer
9390
/** @var string|null */
9491
private $regexp;
9592

96-
/** @var int[]|null */
97-
private $types;
98-
9993
public function tokenize(string $s): array
10094
{
101-
if ($this->regexp === null || $this->types === null) {
102-
$this->initialize();
95+
if ($this->regexp === null) {
96+
$this->regexp = $this->generateRegexp();
10397
}
10498

105-
assert($this->regexp !== null);
106-
assert($this->types !== null);
107-
10899
preg_match_all($this->regexp, $s, $matches, PREG_SET_ORDER);
109100

110101
$tokens = [];
111102
foreach ($matches as $match) {
112-
$tokens[] = [$match[0], $this->types[count($match) - 2]];
103+
$tokens[] = [$match[0], (int) $match['MARK']];
113104
}
114105

115106
$tokens[] = ['', self::TOKEN_END];
@@ -118,7 +109,7 @@ public function tokenize(string $s): array
118109
}
119110

120111

121-
private function initialize(): void
112+
private function generateRegexp(): string
122113
{
123114
$patterns = [
124115
self::TOKEN_HORIZONTAL_WS => '[\\x09\\x20]++',
@@ -166,8 +157,11 @@ private function initialize(): void
166157
self::TOKEN_OTHER => '(?:(?!\\*/)[^\\s])++',
167158
];
168159

169-
$this->regexp = '~(' . implode(')|(', $patterns) . ')~Asi';
170-
$this->types = array_keys($patterns);
160+
foreach ($patterns as $type => &$pattern) {
161+
$pattern = '(?:' . $pattern . ')(*MARK:' . $type . ')';
162+
}
163+
164+
return '~' . implode('|', $patterns) . '~Asi';
171165
}
172166

173167
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /