Skip to content

Commit 1e92e39

Browse files
Merge branch '6.4' into 7.4
* 6.4: [Ldap] Make the Adapter resettable [Serializer] Remove needless line in changelog [MonologBridge] Fix ConsoleHandler losing output after nested command terminates [Cache] Fix tests [EventDispatcher] Fix memory leak in TraceableEventDispatcher for long-running processes [TwigBridge][Mime] Add missing tests [TwigBridge] Refactor image method to use DataPart content ID [Cache] Fix undefined property access [Console] Fix performance regression in OutputFormatter for ASCII content [Serializer] Fix can*() prefix support in GetSetMethodNormalizer [Cache] Fix Psr16Cache::getMultiple() returning ValueWrapper with TagAwareAdapter # Conflicts: # src/Symfony/Bridge/Monolog/Handler/ConsoleHandler.php # src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php # src/Symfony/Bridge/Twig/Mime/WrappedTemplatedEmail.php # src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php # src/Symfony/Component/Ldap/Tests/Adapter/ExtLdap/AdapterTest.php
2 parents f2ef86f + 9f481cf commit 1e92e39

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

Formatter/OutputFormatter.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ public function formatAndWrap(?string $message, int $width): string
123123
return '';
124124
}
125125

126+
// For ASCII-only strings, byte positions equal character positions,
127+
// so we can use native strlen/substr which is much faster than Helper::length/substr.
128+
$isAscii = !preg_match('/[\x80-\xFF]/', $message);
129+
126130
$offset = 0;
127131
$output = '';
128132
$openTagRegex = '[a-z](?:[^\\\\<>]*+ | \\\\.)*';
@@ -137,11 +141,17 @@ public function formatAndWrap(?string $message, int $width): string
137141
continue;
138142
}
139143

140-
// convert byte position to character position.
141-
$pos = Helper::length(substr($message, 0, $pos));
142-
// add the text up to the next tag
143-
$output .= $this->applyCurrentStyle(Helper::substr($message, $offset, $pos - $offset), $output, $width, $currentLineLength);
144-
$offset = $pos + Helper::length($text);
144+
if ($isAscii) {
145+
// For ASCII, byte position = character position, no conversion needed
146+
$output .= $this->applyCurrentStyle(substr($message, $offset, $pos - $offset), $output, $width, $currentLineLength);
147+
$offset = $pos + \strlen($text);
148+
} else {
149+
// convert byte position to character position.
150+
$pos = Helper::length(substr($message, 0, $pos));
151+
// add the text up to the next tag
152+
$output .= $this->applyCurrentStyle(Helper::substr($message, $offset, $pos - $offset), $output, $width, $currentLineLength);
153+
$offset = $pos + Helper::length($text);
154+
}
145155

146156
// opening tag?
147157
if ($open = '/' !== $text[1]) {
@@ -162,7 +172,7 @@ public function formatAndWrap(?string $message, int $width): string
162172
}
163173
}
164174

165-
$output .= $this->applyCurrentStyle(Helper::substr($message, $offset), $output, $width, $currentLineLength);
175+
$output .= $this->applyCurrentStyle($isAscii ? substr($message, $offset) : Helper::substr($message, $offset), $output, $width, $currentLineLength);
166176

167177
return strtr($output, ["\0" => '\\', '\\<' => '<', '\\>' => '>']);
168178
}

0 commit comments

Comments
 (0)