@@ -48,6 +48,16 @@ class Decoder
48
48
49
49
const PATTERN_BINARY = '#0b[0-1]+\z#A ' ;
50
50
51
+ const SIMPLE_TYPES = [
52
+ 'true ' => 'TRUE ' , 'True ' => 'TRUE ' , 'TRUE ' => 'TRUE ' , 'yes ' => 'TRUE ' , 'Yes ' => 'TRUE ' , 'YES ' => 'TRUE ' , 'on ' => 'TRUE ' , 'On ' => 'TRUE ' , 'ON ' => 'TRUE ' ,
53
+ 'false ' => 'FALSE ' , 'False ' => 'FALSE ' , 'FALSE ' => 'FALSE ' , 'no ' => 'FALSE ' , 'No ' => 'FALSE ' , 'NO ' => 'FALSE ' , 'off ' => 'FALSE ' , 'Off ' => 'FALSE ' , 'OFF ' => 'FALSE ' ,
54
+ 'null ' => 'NULL ' , 'Null ' => 'NULL ' , 'NULL ' => 'NULL ' ,
55
+ ];
56
+
57
+ const ESCAPE_SEQUENCES = [
58
+ 't ' => "\t" , 'n ' => "\n" , 'r ' => "\r" , 'f ' => "\x0C" , 'b ' => "\x08" , '" ' => '" ' , '\\' => '\\' , '/ ' => '/ ' , '_ ' => "\xc2\xa0" ,
59
+ ];
60
+
51
61
const BRACKETS = [
52
62
'[ ' => '] ' ,
53
63
'{ ' => '} ' ,
@@ -244,11 +254,6 @@ private function parse($indent, $result = NULL, $key = NULL, $hasKey = FALSE)
244
254
}
245
255
246
256
} else { // Value
247
- static $ consts = [
248
- 'true ' => TRUE , 'True ' => TRUE , 'TRUE ' => TRUE , 'yes ' => TRUE , 'Yes ' => TRUE , 'YES ' => TRUE , 'on ' => TRUE , 'On ' => TRUE , 'ON ' => TRUE ,
249
- 'false ' => FALSE , 'False ' => FALSE , 'FALSE ' => FALSE , 'no ' => FALSE , 'No ' => FALSE , 'NO ' => FALSE , 'off ' => FALSE , 'Off ' => FALSE , 'OFF ' => FALSE ,
250
- 'null ' => 0 , 'Null ' => 0 , 'NULL ' => 0 ,
251
- ];
252
257
if ($ t [0 ] === '" ' || $ t [0 ] === "' " ) {
253
258
if (preg_match ('#^...\n+([\t ]*)# ' , $ t , $ m )) {
254
259
$ converted = substr ($ t , 3 , -3 );
@@ -260,8 +265,8 @@ private function parse($indent, $result = NULL, $key = NULL, $hasKey = FALSE)
260
265
if ($ t [0 ] === '" ' ) {
261
266
$ converted = preg_replace_callback ('# \\\\(?:ud[89ab][0-9a-f]{2} \\\\ud[c-f][0-9a-f]{2}|u[0-9a-f]{4}|x[0-9a-f]{2}|.)#i ' , [$ this , 'cbString ' ], $ converted );
262
267
}
263
- } elseif (isset ($ consts [$ t ]) && (!isset ($ tokens [$ n + 1 ][0 ]) || ($ tokens [$ n + 1 ][0 ] !== ': ' && $ tokens [$ n + 1 ][0 ] !== '= ' ))) {
264
- $ converted = $ consts [ $ t ] === 0 ? NULL : $ consts [$ t ];
268
+ } elseif (( $ fix56 = self :: SIMPLE_TYPES ) && isset ($ fix56 [$ t ]) && (!isset ($ tokens [$ n + 1 ][0 ]) || ($ tokens [$ n + 1 ][0 ] !== ': ' && $ tokens [$ n + 1 ][0 ] !== '= ' ))) {
269
+ $ converted = constant ( self :: SIMPLE_TYPES [$ t ]) ;
265
270
} elseif (is_numeric ($ t )) {
266
271
$ converted = $ t * 1 ;
267
272
} elseif (preg_match (self ::PATTERN_HEX , $ t )) {
@@ -324,10 +329,9 @@ private function addValue(& $result, $key, $value)
324
329
325
330
private function cbString ($ m )
326
331
{
327
- static $ mapping = ['t ' => "\t" , 'n ' => "\n" , 'r ' => "\r" , 'f ' => "\x0C" , 'b ' => "\x08" , '" ' => '" ' , '\\' => '\\' , '/ ' => '/ ' , '_ ' => "\xc2\xa0" ];
328
332
$ sq = $ m [0 ];
329
- if (isset ($ mapping [$ sq [1 ]])) {
330
- return $ mapping [$ sq [1 ]];
333
+ if (( $ fix56 = self :: ESCAPE_SEQUENCES ) && isset ($ fix56 [$ sq [1 ]])) { // workaround for PHP 5.6
334
+ return self :: ESCAPE_SEQUENCES [$ sq [1 ]];
331
335
} elseif ($ sq [1 ] === 'u ' && strlen ($ sq ) >= 6 ) {
332
336
$ lead = hexdec (substr ($ sq , 2 , 4 ));
333
337
$ tail = hexdec (substr ($ sq , 8 , 4 ));
0 commit comments