From 1c14dd596f0af647224493f51102def6f458cecb Mon Sep 17 00:00:00 2001 From: Jake Hotson Date: Wed, 19 Feb 2025 01:11:57 +0000 Subject: [PATCH 1/2] [CLEANUP] Don't store array length as a property Since PHP 5, if not earlier, the length of an array is internally stored by PHP and can be accessed in O(1) time. Maintaining both the array and its length in class properties is error-prone (particularly regarding possible future code changes). When used in a loop, `\count($array)` is more repetitively expensive than `$arrayLength`, but the latter can be set up as a local variable as and when needed. Reference: https://stackoverflow.com/questions/5835241/is-phps-count-function-o1-or-on-for-arrays This change also ensures that the characters are always a (dazzling) array. Resolves #953. --- src/Parsing/ParserState.php | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/Parsing/ParserState.php b/src/Parsing/ParserState.php index c64f9262..4e07df30 100644 --- a/src/Parsing/ParserState.php +++ b/src/Parsing/ParserState.php @@ -30,7 +30,7 @@ class ParserState /** * @var array */ - private $characters; + private $characters = []; /** * @var int @@ -44,11 +44,6 @@ class ParserState */ private $charset; - /** - * @var int - */ - private $length; - /** * @var int */ @@ -75,7 +70,6 @@ public function setCharset(string $charset): void { $this->charset = $charset; $this->characters = $this->strsplit($this->text); - $this->length = \count($this->characters); } /** @@ -225,7 +219,7 @@ public function consumeWhiteSpace(): array try { $oComment = $this->consumeComment(); } catch (UnexpectedEOFException $e) { - $this->currentPosition = $this->length; + $this->currentPosition = \count($this->characters); return $comments; } } else { @@ -257,7 +251,7 @@ public function comes($sString, $bCaseInsensitive = false): bool public function peek($length = 1, $offset = 0): string { $offset += $this->currentPosition; - if ($offset >= $this->length) { + if ($offset >= \count($this->characters)) { return ''; } return $this->substr($offset, $length); @@ -286,7 +280,7 @@ public function consume($mValue = 1): string $this->currentPosition += $this->strlen($mValue); return $mValue; } else { - if ($this->currentPosition + $mValue > $this->length) { + if ($this->currentPosition + $mValue > \count($this->characters)) { throw new UnexpectedEOFException((string) $mValue, $this->peek(5), 'count', $this->lineNumber); } $result = $this->substr($this->currentPosition, $mValue); @@ -343,7 +337,7 @@ public function consumeComment() public function isEnd(): bool { - return $this->currentPosition >= $this->length; + return $this->currentPosition >= \count($this->characters); } /** @@ -436,10 +430,10 @@ public function strlen($sString): int private function substr($iStart, $length): string { if ($length < 0) { - $length = $this->length - $iStart + $length; + $length = \count($this->characters) - $iStart + $length; } - if ($iStart + $length > $this->length) { - $length = $this->length - $iStart; + if ($iStart + $length > \count($this->characters)) { + $length = \count($this->characters) - $iStart; } $result = ''; while ($length > 0) { From 321fcc93ec48a30ac6e777f85bbc2bf859af976b Mon Sep 17 00:00:00 2001 From: JakeQZ Date: Thu, 20 Feb 2025 18:30:20 +0000 Subject: [PATCH 2/2] Don't initialize `$characters` outside of constructor. Co-authored-by: Oliver Klee --- src/Parsing/ParserState.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Parsing/ParserState.php b/src/Parsing/ParserState.php index 4e07df30..5e87b4cc 100644 --- a/src/Parsing/ParserState.php +++ b/src/Parsing/ParserState.php @@ -30,7 +30,7 @@ class ParserState /** * @var array */ - private $characters = []; + private $characters; /** * @var int