@@ -26,18 +26,22 @@ class Escaper
2626 // first to ensure proper escaping because str_replace operates iteratively
2727 // on the input arrays. This ordering of the characters avoids the use of strtr,
2828 // which performs more slowly.
29- private static $ escapees = array ('\\' , '\\\\' , '\\" ' , '" ' ,
30- "\x00" , "\x01" , "\x02" , "\x03" , "\x04" , "\x05" , "\x06" , "\x07" ,
31- "\x08" , "\x09" , "\x0a" , "\x0b" , "\x0c" , "\x0d" , "\x0e" , "\x0f" ,
32- "\x10" , "\x11" , "\x12" , "\x13" , "\x14" , "\x15" , "\x16" , "\x17" ,
33- "\x18" , "\x19" , "\x1a" , "\x1b" , "\x1c" , "\x1d" , "\x1e" , "\x1f" ,
34- "\xc2\x85" , "\xc2\xa0" , "\xe2\x80\xa8" , "\xe2\x80\xa9" );
35- private static $ escaped = array ('\\\\' , '\\" ' , '\\\\' , '\\" ' ,
36- '\\0 ' , '\\x01 ' , '\\x02 ' , '\\x03 ' , '\\x04 ' , '\\x05 ' , '\\x06 ' , '\\a ' ,
37- '\\b ' , '\\t ' , '\\n ' , '\\v ' , '\\f ' , '\\r ' , '\\x0e ' , '\\x0f ' ,
38- '\\x10 ' , '\\x11 ' , '\\x12 ' , '\\x13 ' , '\\x14 ' , '\\x15 ' , '\\x16 ' , '\\x17 ' ,
39- '\\x18 ' , '\\x19 ' , '\\x1a ' , '\\e ' , '\\x1c ' , '\\x1d ' , '\\x1e ' , '\\x1f ' ,
40- '\\N ' , '\\_ ' , '\\L ' , '\\P ' );
29+ private static $ escapees = array (
30+ '\\' , '\\\\' , '\\" ' , '" ' , '/ ' ,
31+ "\x00" , "\x01" , "\x02" , "\x03" , "\x04" , "\x05" , "\x06" , "\x07" ,
32+ "\x08" , "\x09" , "\x0a" , "\x0b" , "\x0c" , "\x0d" , "\x0e" , "\x0f" ,
33+ "\x10" , "\x11" , "\x12" , "\x13" , "\x14" , "\x15" , "\x16" , "\x17" ,
34+ "\x18" , "\x19" , "\x1a" , "\x1b" , "\x1c" , "\x1d" , "\x1e" , "\x1f" ,
35+ "\xc2\x85" , "\xc2\xa0" , "\xe2\x80\xa8" , "\xe2\x80\xa9" ,
36+ );
37+ private static $ escaped = array (
38+ '\\\\' , '\\" ' , '\\\\' , '\\" ' , '\\/ ' ,
39+ '\\0 ' , '\\x01 ' , '\\x02 ' , '\\x03 ' , '\\x04 ' , '\\x05 ' , '\\x06 ' , '\\a ' ,
40+ '\\b ' , '\\t ' , '\\n ' , '\\v ' , '\\f ' , '\\r ' , '\\x0e ' , '\\x0f ' ,
41+ '\\x10 ' , '\\x11 ' , '\\x12 ' , '\\x13 ' , '\\x14 ' , '\\x15 ' , '\\x16 ' , '\\x17 ' ,
42+ '\\x18 ' , '\\x19 ' , '\\x1a ' , '\\e ' , '\\x1c ' , '\\x1d ' , '\\x1e ' , '\\x1f ' ,
43+ '\\N ' , '\\_ ' , '\\L ' , '\\P ' ,
44+ );
4145
4246 /**
4347 * Determines if a PHP value would require double quoting in YAML.
@@ -48,7 +52,10 @@ class Escaper
4852 */
4953 public static function requiresDoubleQuoting ($ value )
5054 {
51- return preg_match ('/ ' .self ::REGEX_CHARACTER_TO_ESCAPE .'/u ' , $ value );
55+ // Removes quotes and slashes
56+ $ mask = implode ('' , array_slice (self ::$ escapees , 5 ));
57+
58+ return strlen ($ value ) !== strcspn ($ value , $ mask );
5259 }
5360
5461 /**
@@ -78,9 +85,13 @@ public static function requiresSingleQuoting($value)
7885 return true ;
7986 }
8087
81- // Determines if the PHP value contains any single characters that would
82- // cause it to require single quoting in YAML.
83- return preg_match ('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ \- ? | < > = ! % @ ` ]/x ' , $ value );
88+ // First character is reserved
89+ if ($ value && in_array ($ value [0 ], array ('- ' , '| ' , '< ' , '> ' , '= ' , '! ' , '% ' , '@ ' , '` ' ))) {
90+ return true ;
91+ }
92+
93+ // Contains spaces or ambigous characters
94+ return strlen ($ value ) !== strcspn ($ value , "\011\n\013\014\r ' \":{}[],&*#? " );
8495 }
8596
8697 /**
0 commit comments