diff --git a/Formatter/OutputFormatter.php b/Formatter/OutputFormatter.php index c72728b27..d7ea7267e 100644 --- a/Formatter/OutputFormatter.php +++ b/Formatter/OutputFormatter.php @@ -123,6 +123,10 @@ public function formatAndWrap(?string $message, int $width): string return ''; } + // For ASCII-only strings, byte positions equal character positions, + // so we can use native strlen/substr which is much faster than Helper::length/substr. + $isAscii = !preg_match('/[\x80-\xFF]/', $message); + $offset = 0; $output = ''; $openTagRegex = '[a-z](?:[^\\\\<>]*+ | \\\\.)*'; @@ -137,11 +141,17 @@ public function formatAndWrap(?string $message, int $width): string continue; } - // convert byte position to character position. - $pos = Helper::length(substr($message, 0, $pos)); - // add the text up to the next tag - $output .= $this->applyCurrentStyle(Helper::substr($message, $offset, $pos - $offset), $output, $width, $currentLineLength); - $offset = $pos + Helper::length($text); + if ($isAscii) { + // For ASCII, byte position = character position, no conversion needed + $output .= $this->applyCurrentStyle(substr($message, $offset, $pos - $offset), $output, $width, $currentLineLength); + $offset = $pos + \strlen($text); + } else { + // convert byte position to character position. + $pos = Helper::length(substr($message, 0, $pos)); + // add the text up to the next tag + $output .= $this->applyCurrentStyle(Helper::substr($message, $offset, $pos - $offset), $output, $width, $currentLineLength); + $offset = $pos + Helper::length($text); + } // opening tag? if ($open = '/' !== $text[1]) { @@ -162,7 +172,7 @@ public function formatAndWrap(?string $message, int $width): string } } - $output .= $this->applyCurrentStyle(Helper::substr($message, $offset), $output, $width, $currentLineLength); + $output .= $this->applyCurrentStyle($isAscii ? substr($message, $offset) : Helper::substr($message, $offset), $output, $width, $currentLineLength); return strtr($output, ["\0" => '\\', '\\<' => '<', '\\>' => '>']); } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 950f1c096..a3dd340ba 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,7 +1,7 @@ + + trigger_deprecation + Doctrine\Deprecations\Deprecation::trigger + Doctrine\Deprecations\Deprecation::triggerIfCalledFromOutside + ./