Skip to content

Commit bb8972d

Browse files
Ostrzycielxabbuh
authored andcommitted
[YAML] Double escape strings with single quote marks
1 parent bdd43a0 commit bb8972d

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

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

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):

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
public function testInlineLevel()
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

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)