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

Commit4386861

Browse files
committed
[Yaml] Remove escaping regex
1 parent2bc54e0 commit4386861

File tree

3 files changed

+57
-43
lines changed

3 files changed

+57
-43
lines changed

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

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,33 @@
2121
*/
2222
class Escaper
2323
{
24-
// Characters that would cause a dumped string to require double quoting.
25-
constREGEX_CHARACTER_TO_ESCAPE ="[\\x00-\\x1f]|\xc2\x85|\xc2\xa0|\xe2\x80\xa8|\xe2\x80\xa9";
24+
/**
25+
* Characters that would cause a dumped string to require double quoting.
26+
*
27+
* @internal
28+
*/
29+
constCHARACTER_TO_ESCAPE_MASK ="\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\xc2\xe2";
2630

2731
// Mapping arrays for escaping a double quoted string. The backslash is
2832
// first to ensure proper escaping because str_replace operates iteratively
2933
// on the input arrays. This ordering of the characters avoids the use of strtr,
3034
// which performs more slowly.
31-
privatestatic$escapees =array('\\','\\\\','\\"','"',
32-
"\x00","\x01","\x02","\x03","\x04","\x05","\x06","\x07",
33-
"\x08","\x09","\x0a","\x0b","\x0c","\x0d","\x0e","\x0f",
34-
"\x10","\x11","\x12","\x13","\x14","\x15","\x16","\x17",
35-
"\x18","\x19","\x1a","\x1b","\x1c","\x1d","\x1e","\x1f",
36-
"\xc2\x85","\xc2\xa0","\xe2\x80\xa8","\xe2\x80\xa9");
37-
privatestatic$escaped =array('\\\\','\\"','\\\\','\\"',
38-
'\\0','\\x01','\\x02','\\x03','\\x04','\\x05','\\x06','\\a',
39-
'\\b','\\t','\\n','\\v','\\f','\\r','\\x0e','\\x0f',
40-
'\\x10','\\x11','\\x12','\\x13','\\x14','\\x15','\\x16','\\x17',
41-
'\\x18','\\x19','\\x1a','\\e','\\x1c','\\x1d','\\x1e','\\x1f',
42-
'\\N','\\_','\\L','\\P');
35+
privatestatic$escapees =array(
36+
'\\','\\\\','\\"','"','/',
37+
"\x00","\x01","\x02","\x03","\x04","\x05","\x06","\x07",
38+
"\x08","\x09","\x0a","\x0b","\x0c","\x0d","\x0e","\x0f",
39+
"\x10","\x11","\x12","\x13","\x14","\x15","\x16","\x17",
40+
"\x18","\x19","\x1a","\x1b","\x1c","\x1d","\x1e","\x1f",
41+
"\xc2\x85","\xc2\xa0","\xe2\x80\xa8","\xe2\x80\xa9",
42+
);
43+
privatestatic$escaped =array(
44+
'\\\\','\\"','\\\\','\\"','\\/',
45+
'\\0','\\x01','\\x02','\\x03','\\x04','\\x05','\\x06','\\a',
46+
'\\b','\\t','\\n','\\v','\\f','\\r','\\x0e','\\x0f',
47+
'\\x10','\\x11','\\x12','\\x13','\\x14','\\x15','\\x16','\\x17',
48+
'\\x18','\\x19','\\x1a','\\e','\\x1c','\\x1d','\\x1e','\\x1f',
49+
'\\N','\\_','\\L','\\P',
50+
);
4351

4452
/**
4553
* Determines if a PHP value would require double quoting in YAML.
@@ -50,7 +58,7 @@ class Escaper
5058
*/
5159
publicstaticfunctionrequiresDoubleQuoting($value)
5260
{
53-
returnpreg_match('/'.self::REGEX_CHARACTER_TO_ESCAPE.'/u',$value);
61+
returnstrlen($value) !==strcspn($value,self::CHARACTER_TO_ESCAPE_MASK);
5462
}
5563

5664
/**
@@ -80,9 +88,14 @@ public static function requiresSingleQuoting($value)
8088
returntrue;
8189
}
8290

83-
// Determines if the PHP value contains any single characters that would
84-
// cause it to require single quoting in YAML.
85-
returnpreg_match('/[ \s\' " \: \{ \} \[ \] , & \* \# \?] | \A[ \- ? | < > = ! % @ ` ]/x',$value);
91+
// First character is an indicator
92+
// @see http://yaml.org/spec/1.2/spec.html#c-indicator
93+
if ($value &&1 ===strspn($value[0],'-?&*!|<>\'"=%@`')) {
94+
returntrue;
95+
}
96+
97+
// Contains spaces or ambigous characters
98+
returnstrlen($value) !==strcspn($value,"\011\n\013\014\r :{}[],#");
8699
}
87100

88101
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ public static function evaluateBinaryScalar($scalar)
684684

685685
privatestaticfunctionisBinaryString($value)
686686
{
687-
return !preg_match('//u',$value) ||preg_match('/[^\x09-\x0d\x20-\xff]/',$value);
687+
return !preg_match('//u',$value) ||preg_match('/[^\x00-\x1f\x20-\xff]/',$value);
688688
}
689689

690690
/**

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

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function testIndentationInConstructor()
5757
$expected = <<<'EOF'
5858
'': bar
5959
foo: '#bar'
60-
'foo''bar': { }
60+
foo'bar: { }
6161
bar:
6262
- 1
6363
- foo
@@ -86,7 +86,7 @@ public function testSetIndentation()
8686
$expected = <<<'EOF'
8787
'': bar
8888
foo: '#bar'
89-
'foo''bar': { }
89+
foo'bar: { }
9090
bar:
9191
- 1
9292
- foo
@@ -133,15 +133,15 @@ public function testSpecifications()
133133
publicfunctiontestInlineLevel()
134134
{
135135
$expected = <<<'EOF'
136-
{ '': bar, foo: '#bar','foo''bar': { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
136+
{ '': bar, foo: '#bar', foo'bar: { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
137137
EOF;
138138
$this->assertEquals($expected,$this->dumper->dump($this->array, -10),'->dump() takes an inline level argument');
139139
$this->assertEquals($expected,$this->dumper->dump($this->array,0),'->dump() takes an inline level argument');
140140

141141
$expected = <<<'EOF'
142142
'': bar
143143
foo: '#bar'
144-
'foo''bar': { }
144+
foo'bar: { }
145145
bar: [1, foo]
146146
foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } }
147147

@@ -151,7 +151,7 @@ public function testInlineLevel()
151151
$expected = <<<'EOF'
152152
'': bar
153153
foo: '#bar'
154-
'foo''bar': { }
154+
foo'bar: { }
155155
bar:
156156
- 1
157157
- foo
@@ -166,7 +166,7 @@ public function testInlineLevel()
166166
$expected = <<<'EOF'
167167
'': bar
168168
foo: '#bar'
169-
'foo''bar': { }
169+
foo'bar: { }
170170
bar:
171171
- 1
172172
- foo
@@ -185,7 +185,7 @@ public function testInlineLevel()
185185
$expected = <<<'EOF'
186186
'': bar
187187
foo: '#bar'
188-
'foo''bar': { }
188+
foo'bar: { }
189189
bar:
190190
- 1
191191
- foo
@@ -257,23 +257,24 @@ public function testEscapedEscapeSequencesInQuotedScalar($input, $expected)
257257
publicfunctiongetEscapeSequences()
258258
{
259259
returnarray(
260-
'null' =>array("\t\\0",'"\t\\\\0"'),
261-
'bell' =>array("\t\\a",'"\t\\\\a"'),
262-
'backspace' =>array("\t\\b",'"\t\\\\b"'),
263-
'horizontal-tab' =>array("\t\\t",'"\t\\\\t"'),
264-
'line-feed' =>array("\t\\n",'"\t\\\\n"'),
265-
'vertical-tab' =>array("\t\\v",'"\t\\\\v"'),
266-
'form-feed' =>array("\t\\f",'"\t\\\\f"'),
267-
'carriage-return' =>array("\t\\r",'"\t\\\\r"'),
268-
'escape' =>array("\t\\e",'"\t\\\\e"'),
269-
'space' =>array("\t\\",'"\t\\\\ "'),
270-
'double-quote' =>array("\t\\\"",'"\t\\\\\\""'),
271-
'slash' =>array("\t\\/",'"\t\\\\/"'),
272-
'backslash' =>array("\t\\\\",'"\t\\\\\\\\"'),
273-
'next-line' =>array("\t\\N",'"\t\\\\N"'),
274-
'non-breaking-space' =>array("\t\\",'"\t\\\\�"'),
275-
'line-separator' =>array("\t\\L",'"\t\\\\L"'),
276-
'paragraph-separator' =>array("\t\\P",'"\t\\\\P"'),
260+
'null' =>array("\x0",'"\\0"'),
261+
'bell' =>array("\x7",'"\\a"'),
262+
'backspace' =>array("\x8",'"\\b"'),
263+
'horizontal-tab' =>array("\t",'"\\t"'),
264+
'line-feed' =>array("\n",'"\\n"'),
265+
'vertical-tab' =>array("\v",'"\\v"'),
266+
'form-feed' =>array("\xC",'"\\f"'),
267+
'carriage-return' =>array("\r",'"\\r"'),
268+
'escape' =>array("\x1B",'"\\e"'),
269+
'space' =>array('',"' '"),
270+
'double-quote' =>array('"',"'\"'"),
271+
'slash' =>array('/','/'),
272+
'backslash' =>array('\\','\\'),
273+
'next-line' =>array("\xC2\x85",'"\\N"'),
274+
'non-breaking-space' =>array('',''),
275+
'line-separator' =>array("\xE2\x80\xA8",'"\\L"'),
276+
'paragraph-separator' =>array("\xE2\x80\xA9",'"\\P"'),
277+
'colon' =>array(':',"':'"),
277278
);
278279
}
279280

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp