Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcef3d5a

Browse files
committed
feature#41750 [Yaml] Double-quote strings with single quote marks (Ostrzyciel)
This PR was merged into the 6.1 branch.Discussion----------[Yaml] Double-quote strings with single quote marks| Q | A| ------------- | ---| Branch? | 5.4| Bug fix? | no| New feature? | yes| Deprecations? | no| Tickets |Fix#41687| License | MIT| Doc PR | It's a tiny change, is it really required?This changes the behavior of the `Dumper` to double-quote strings containing single quote marks when it's likely to improve readability (measured by the length of the quoted string). See the linked ticket for the detailed motivation behind this.Commits-------dffe98e [YAML] Double escape strings with single quote marks
2 parents05c28ca +dffe98e commitcef3d5a

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

‎src/Symfony/Component/Yaml/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.1
5+
---
6+
7+
* In cases where it will likely improve readability, strings containing single quotes will be double-quoted.
8+
49
5.4
510
---
611

‎src/Symfony/Component/Yaml/Inline.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ public static function dump(mixed $value, int $flags = 0): string
177177
case Escaper::requiresDoubleQuoting($value):
178178
return Escaper::escapeWithDoubleQuotes($value);
179179
case Escaper::requiresSingleQuoting($value):
180+
$singleQuoted = Escaper::escapeWithSingleQuotes($value);
181+
if (!str_contains($value,"'")) {
182+
return$singleQuoted;
183+
}
184+
// Attempt double-quoting the string instead to see if it's more efficient.
185+
$doubleQuoted = Escaper::escapeWithDoubleQuotes($value);
186+
187+
return\strlen($doubleQuoted) <\strlen($singleQuoted) ?$doubleQuoted :$singleQuoted;
180188
case Parser::preg_match('{^[0-9]+[_0-9]*$}',$value):
181189
case Parser::preg_match(self::getHexRegex(),$value):
182190
case Parser::preg_match(self::getTimestampRegex(),$value):

‎src/Symfony/Component/Yaml/Tests/DumperTest.php‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testIndentationInConstructor()
6060
$expected = <<<'EOF'
6161
'': bar
6262
foo: '#bar'
63-
'foo''bar': { }
63+
"foo'bar": { }
6464
bar:
6565
- 1
6666
- foo
@@ -107,15 +107,15 @@ public function testSpecifications()
107107
publicfunctiontestInlineLevel()
108108
{
109109
$expected = <<<'EOF'
110-
{ '': bar, foo: '#bar','foo''bar': { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
110+
{ '': bar, foo: '#bar',"foo'bar": { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
111111
EOF;
112112
$this->assertEquals($expected,$this->dumper->dump($this->array, -10),'->dump() takes an inline level argument');
113113
$this->assertEquals($expected,$this->dumper->dump($this->array,0),'->dump() takes an inline level argument');
114114

115115
$expected = <<<'EOF'
116116
'': bar
117117
foo: '#bar'
118-
'foo''bar': { }
118+
"foo'bar": { }
119119
bar: [1, foo]
120120
foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } }
121121

@@ -125,7 +125,7 @@ public function testInlineLevel()
125125
$expected = <<<'EOF'
126126
'': bar
127127
foo: '#bar'
128-
'foo''bar': { }
128+
"foo'bar": { }
129129
bar:
130130
- 1
131131
- foo
@@ -140,7 +140,7 @@ public function testInlineLevel()
140140
$expected = <<<'EOF'
141141
'': bar
142142
foo: '#bar'
143-
'foo''bar': { }
143+
"foo'bar": { }
144144
bar:
145145
- 1
146146
- foo
@@ -159,7 +159,7 @@ public function testInlineLevel()
159159
$expected = <<<'EOF'
160160
'': bar
161161
foo: '#bar'
162-
'foo''bar': { }
162+
"foo'bar": { }
163163
bar:
164164
- 1
165165
- foo

‎src/Symfony/Component/Yaml/Tests/InlineTest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,10 @@ public function getTestsForDump()
473473
["'foo # bar'",'foo # bar'],
474474
["'#cfcfcf'",'#cfcfcf'],
475475

476+
["\"isn't it a nice single quote\"","isn't it a nice single quote"],
477+
['\'this is "double quoted"\'','this is "double quoted"'],
478+
["\"one double, four single quotes:\\\"''''\"",'one double, four single quotes: "\'\'\'\''],
479+
['\'four double, one single quote: """"\'\'\'','four double, one single quote: """"\''],
476480
["'a\"string\" with ''quoted strings inside'''",'a "string" with\'quoted strings inside\''],
477481

478482
["'-dash'",'-dash'],

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp