Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9c796b4

Browse files
committed
bug #23730 Fixed the escaping of back slashes and << in console output (javiereguiluz)
This PR was squashed before being merged into the 2.7 branch (closes #23730). Discussion ---------- Fixed the escaping of back slashes and << in console output | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #18481 | License | MIT | Doc PR | - Not sure if it's a valid solution, but this is my attempt to solve #18481. Commits ------- d5cb1fe Fixed the escaping of back slashes and << in console output
2 parents bc4e015 + d5cb1fe commit 9c796b4

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/Symfony/Component/Console/Formatter/OutputFormatter.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public static function escapeTrailingBackslash($text)
5050
if ('\\' === substr($text, -1)) {
5151
$len = strlen($text);
5252
$text = rtrim($text, '\\');
53-
$text .= str_repeat('<<', $len - strlen($text));
53+
$text = str_replace("\0", '', $text);
54+
$text .= str_repeat("\0", $len - strlen($text));
5455
}
5556

5657
return $text;
@@ -165,8 +166,8 @@ public function format($message)
165166

166167
$output .= $this->applyCurrentStyle(substr($message, $offset));
167168

168-
if (false !== strpos($output, '<<')) {
169-
return strtr($output, array('\\<' => '<', '<<' => '\\'));
169+
if (false !== strpos($output, "\0")) {
170+
return strtr($output, array("\0" => '\\', '\\<' => '<'));
170171
}
171172

172173
return str_replace('\\<', '<', $output);

src/Symfony/Component/Console/Tests/Formatter/OutputFormatterTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public function testLGCharEscaping()
2828
$formatter = new OutputFormatter(true);
2929

3030
$this->assertEquals('foo<bar', $formatter->format('foo\\<bar'));
31+
$this->assertEquals('foo << bar', $formatter->format('foo << bar'));
32+
$this->assertEquals('foo << bar \\', $formatter->format('foo << bar \\'));
33+
$this->assertEquals("foo << \033[32mbar \\ baz\033[39m \\", $formatter->format('foo << <info>bar \\ baz</info> \\'));
3134
$this->assertEquals('<info>some info</info>', $formatter->format('\\<info>some info\\</info>'));
3235
$this->assertEquals('\\<info>some info\\</info>', OutputFormatter::escape('<info>some info</info>'));
3336

0 commit comments

Comments
 (0)