10000 [Yaml] Double-quote strings with single quote marks by Ostrzyciel · Pull Request #41750 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Yaml] Double-quote strings with single quote marks #41750

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 1 commit into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

10000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Yaml/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.1
---

* In cases where it will likely improve readability, strings containing single quotes will be double-quoted.

5.4
---

Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ public static function dump(mixed $value, int $flags = 0): string
case Escaper::requiresDoubleQuoting($value):
return Escaper::escapeWithDoubleQuotes($value);
case Escaper::requiresSingleQuoting($value):
$singleQuoted = Escaper::escapeWithSingleQuotes($value);
if (!str_contains($value, "'")) {
return $singleQuoted;
}
// Attempt double-quoting the string instead to see if it's more efficient.
$doubleQuoted = Escaper::escapeWithDoubleQuotes($value);

return \strlen($doubleQuoted) < \strlen($singleQuoted) ? $doubleQuoted : $singleQuoted;
case Parser::preg_match('{^[0-9]+[_0-9]*$}', $value):
case Parser::preg_match(self::getHexRegex(), $value):
case Parser::preg_match(self::getTimestampRegex(), $value):
Expand Down
12 changes: 6 additions & 6 deletions src/Symfony/Component/Yaml/Tests/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testIndentationInConstructor()
$expected = <<<'EOF'
'': bar
foo: '#bar'
'foo''bar': { }
"foo'bar": { }
bar:
- 1
- foo
Expand Down Expand Up @@ -107,15 +107,15 @@ public function testSpecifications()
public function testInlineLevel()
{
$expected = <<<'EOF'
{ '': bar, foo: '#bar', 'foo''bar': { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
{ '': bar, foo: '#bar', "foo'bar": { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
EOF;
$this->assertEquals($expected, $this->dumper->dump($this->array, -10), '->dump() takes an inline level argument');
$this->assertEquals($expected, $this->dumper->dump($this->array, 0), '->dump() takes an inline level argument');

$expected = <<<'EOF'
'': bar
foo: '#bar'
'foo''bar': { }
"foo'bar": { }
bar: [1, foo]
foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } }

Expand All @@ -125,7 +125,7 @@ public function testInlineLevel()
$expected = <<<'EOF'
'': bar
foo: '#bar'
'foo''bar': { }
"foo'bar": { }
bar:
- 1
- foo
Expand All @@ -140,7 +140,7 @@ public function testInlineLevel()
$expected = <<<'EOF'
'': bar
foo: '#bar'
'foo''bar': { }
"foo'bar": { }
bar:
- 1
- foo
Expand All @@ -159,7 +159,7 @@ public function testInlineLevel()
$expected = <<<'EOF'
'': bar
foo: '#bar'
'foo''bar': { }
"foo'bar": { }
bar:
- 1
- foo
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Yaml/Tests/InlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,10 @@ public function getTestsForDump()
["'foo # bar'", 'foo # bar'],
["'#cfcfcf'", '#cfcfcf'],

["\"isn't it a nice single quote\"", "isn't it a nice single quote"],
['\'this is "double quoted"\'', 'this is "double quoted"'],
["\"one double, four single quotes: \\\"''''\"", 'one double, four single quotes: "\'\'\'\''],
['\'four double, one single quote: """"\'\'\'', 'four double, one single quote: """"\''],
["'a \"string\" with ''quoted strings inside'''", 'a "string" with \'quoted strings inside\''],

["'-dash'", '-dash'],
Expand Down
0