Avoid allocating a normalized copy when counting newlines without carriage returns

This commit is contained in:
Fabien Potencier
2026-06-05 20:33:28 +02:00
parent a82782ac30
commit 3868bac531
+4 -3
View File
@@ -531,9 +531,10 @@ class Lexer
private function moveCursor($text): void
{
$length = \strlen($text);
$this->cursor += $length;
$this->lineno += substr_count($this->normalizeNewlines($text), "\n");
$this->cursor += \strlen($text);
// count "\r\n" and "\r" as a single newline without allocating a
// normalized copy when the chunk has no carriage return (common case)
$this->lineno += str_contains($text, "\r") ? substr_count($this->normalizeNewlines($text), "\n") : substr_count($text, "\n");
}
private function normalizeNewlines(string $text): string