Skip to content

[CLEANUP] Don't store array length as a property #954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions src/Parsing/ParserState.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ class ParserState
*/
private $charset;

/**
* @var int
*/
private $length;

/**
* @var int
*/
Expand All @@ -75,7 +70,6 @@ public function setCharset(string $charset): void
{
$this->charset = $charset;
$this->characters = $this->strsplit($this->text);
$this->length = \count($this->characters);
}

/**
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -343,7 +337,7 @@ public function consumeComment()

public function isEnd(): bool
{
return $this->currentPosition >= $this->length;
return $this->currentPosition >= \count($this->characters);
}

/**
Expand Down Expand Up @@ -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) {
Expand Down