Skip to content

Commit d26ead4

Browse files
committed
[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. And adds a type parameter to `ParserState::setCharset()`, which was unavoidably required. Resolves #953.
1 parent ee3d57c commit d26ead4

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/Parsing/ParserState.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ParserState
3030
/**
3131
* @var array<int, string>
3232
*/
33-
private $characters;
33+
private $characters = [];
3434

3535
/**
3636
* @var int
@@ -44,11 +44,6 @@ class ParserState
4444
*/
4545
private $charset;
4646

47-
/**
48-
* @var int
49-
*/
50-
private $length;
51-
5247
/**
5348
* @var int
5449
*/
@@ -75,7 +70,6 @@ public function setCharset(string $charset): void
7570
{
7671
$this->charset = $charset;
7772
$this->characters = $this->strsplit($this->text);
78-
$this->length = \count($this->characters);
7973
}
8074

8175
/**
@@ -225,7 +219,7 @@ public function consumeWhiteSpace(): array
225219
try {
226220
$oComment = $this->consumeComment();
227221
} catch (UnexpectedEOFException $e) {
228-
$this->currentPosition = $this->length;
222+
$this->currentPosition = \count($this->characters);
229223
return $comments;
230224
}
231225
} else {
@@ -256,8 +250,13 @@ public function comes($sString, $bCaseInsensitive = false): bool
256250
*/
257251
public function peek($length = 1, $offset = 0): string
258252
{
253+
<<<<<<< HEAD
259254
$offset += $this->currentPosition;
260255
if ($offset >= $this->length) {
256+
=======
257+
$iOffset += $this->currentPosition;
258+
if ($iOffset >= \count($this->characters)) {
259+
>>>>>>> 521415c ([CLEANUP] Don't store array length as a property)
261260
return '';
262261
}
263262
return $this->substr($offset, $length);
@@ -286,7 +285,7 @@ public function consume($mValue = 1): string
286285
$this->currentPosition += $this->strlen($mValue);
287286
return $mValue;
288287
} else {
289-
if ($this->currentPosition + $mValue > $this->length) {
288+
if ($this->currentPosition + $mValue > \count($this->characters)) {
290289
throw new UnexpectedEOFException((string) $mValue, $this->peek(5), 'count', $this->lineNumber);
291290
}
292291
$result = $this->substr($this->currentPosition, $mValue);
@@ -343,7 +342,7 @@ public function consumeComment()
343342

344343
public function isEnd(): bool
345344
{
346-
return $this->currentPosition >= $this->length;
345+
return $this->currentPosition >= \count($this->characters);
347346
}
348347

349348
/**
@@ -436,10 +435,10 @@ public function strlen($sString): int
436435
private function substr($iStart, $length): string
437436
{
438437
if ($length < 0) {
439-
$length = $this->length - $iStart + $length;
438+
$length = \count($this->characters) - $iStart + $length;
440439
}
441-
if ($iStart + $length > $this->length) {
442-
$length = $this->length - $iStart;
440+
if ($iStart + $length > \count($this->characters)) {
441+
$length = \count($this->characters) - $iStart;
443442
}
444443
$result = '';
445444
while ($length > 0) {

0 commit comments

Comments
 (0)