Skip to content

Commit e53096c

Browse files
Merge pull request #130 from mvorisek/fix_perf_refactor
Refactor tokenizer with $firstChar/$secondChar
2 parents 0bcd33e + 89c6ee3 commit e53096c

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/Tokenizer.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -835,14 +835,17 @@ private function createNextToken(string $string, string $upper, int $offset, Tok
835835
return new Token(Token::TOKEN_TYPE_WHITESPACE, $matches[0]);
836836
}
837837

838+
$firstChar = $string[$offset];
839+
$secondChar = $string[$offset + 1] ?? '';
840+
838841
// Comment
839842
if (
840-
$string[$offset] === '#' ||
841-
(isset($string[$offset + 1]) && ($string[$offset] === '-' && $string[$offset + 1] === '-') ||
842-
(isset($string[$offset + 1]) && $string[$offset] === '/' && $string[$offset + 1] === '*'))
843+
$firstChar === '#' ||
844+
(($firstChar === '-' && $secondChar === '-') ||
845+
($firstChar === '/' && $secondChar === '*'))
843846
) {
844847
// Comment until end of line
845-
if ($string[$offset] === '-' || $string[$offset] === '#') {
848+
if ($firstChar === '-' || $firstChar === '#') {
846849
$last = strpos($string, "\n", $offset);
847850
$type = Token::TOKEN_TYPE_COMMENT;
848851
} else { // Comment until closing comment tag
@@ -861,26 +864,26 @@ private function createNextToken(string $string, string $upper, int $offset, Tok
861864
}
862865

863866
// Quoted String
864-
if ($string[$offset] === '"' || $string[$offset] === '\'' || $string[$offset] === '`' || $string[$offset] === '[') {
867+
if ($firstChar === '"' || $firstChar === '\'' || $firstChar === '`' || $firstChar === '[') {
865868
return new Token(
866-
($string[$offset] === '`' || $string[$offset] === '['
869+
($firstChar === '`' || $firstChar === '['
867870
? Token::TOKEN_TYPE_BACKTICK_QUOTE
868871
: Token::TOKEN_TYPE_QUOTE),
869872
$this->getNextQuotedString($string, $offset),
870873
);
871874
}
872875

873876
// User-defined Variable
874-
if (($string[$offset] === '@' || $string[$offset] === ':') && isset($string[$offset + 1])) {
877+
if (($firstChar === '@' || $firstChar === ':') && $secondChar !== '') {
875878
$value = null;
876879
$type = Token::TOKEN_TYPE_VARIABLE;
877880

878881
// If the variable name is quoted
879-
if ($string[$offset + 1] === '"' || $string[$offset + 1] === '\'' || $string[$offset + 1] === '`') {
880-
$value = $string[$offset] . $this->getNextQuotedString($string, $offset + 1);
882+
if ($secondChar === '"' || $secondChar === '\'' || $secondChar === '`') {
883+
$value = $firstChar . $this->getNextQuotedString($string, $offset + 1);
881884
} else {
882885
// Non-quoted variable name
883-
preg_match('/\G(' . $string[$offset] . '[\w.$]+)/', $string, $matches, 0, $offset);
886+
preg_match('/\G(' . $firstChar . '[\w.$]+)/', $string, $matches, 0, $offset);
884887
if ($matches) {
885888
$value = $matches[1];
886889
}

0 commit comments

Comments
 (0)