Skip to content

Commit c40c63f

Browse files
committed
Decoder: refactoring, added SIMPLE_TYPES & ESCAPE_SEQUENCES
1 parent b98b91e commit c40c63f

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/Neon/Decoder.php

+14-10
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ class Decoder
4848

4949
const PATTERN_BINARY = '#0b[0-1]+\z#A';
5050

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+
5161
const BRACKETS = [
5262
'[' => ']',
5363
'{' => '}',
@@ -244,11 +254,6 @@ private function parse($indent, $result = NULL, $key = NULL, $hasKey = FALSE)
244254
}
245255

246256
} 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-
];
252257
if ($t[0] === '"' || $t[0] === "'") {
253258
if (preg_match('#^...\n+([\t ]*)#', $t, $m)) {
254259
$converted = substr($t, 3, -3);
@@ -260,8 +265,8 @@ private function parse($indent, $result = NULL, $key = NULL, $hasKey = FALSE)
260265
if ($t[0] === '"') {
261266
$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);
262267
}
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]);
265270
} elseif (is_numeric($t)) {
266271
$converted = $t * 1;
267272
} elseif (preg_match(self::PATTERN_HEX, $t)) {
@@ -324,10 +329,9 @@ private function addValue(& $result, $key, $value)
324329

325330
private function cbString($m)
326331
{
327-
static $mapping = ['t' => "\t", 'n' => "\n", 'r' => "\r", 'f' => "\x0C", 'b' => "\x08", '"' => '"', '\\' => '\\', '/' => '/', '_' => "\xc2\xa0"];
328332
$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]];
331335
} elseif ($sq[1] === 'u' && strlen($sq) >= 6) {
332336
$lead = hexdec(substr($sq, 2, 4));
333337
$tail = hexdec(substr($sq, 8, 4));

0 commit comments

Comments
 (0)