-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHJSONStringifier.php
363 lines (320 loc) · 13.2 KB
/
HJSONStringifier.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
namespace tangible\hjson;
/**
* NOTE: this may return an empty string at the end of the array when the input
* string ends with a newline character
*/
function mb_str_split($string)
{
return preg_split('/(?<!^)/u', $string);
}
class HJSONStringifier
{
// needsEscape tests if the string can be written without escapes
private $needsEscape = '/[\\\"\x00-\x1f\x7f-\x9f\x{00ad}\x{0600}-\x{0604}\x{070f}\x{17b4}\x{17b5}\x{200c}-\x{200f}\x{2028}-\x{202f}\x{2060}-\x{206f}\x{feff}\x{fff0}-\x{ffff}\x]/u';
// needsQuotes tests if the string can be written as a quoteless string (includes needsEscape but without \\ and \")
private $needsQuotes = '/^\\s|^"|^\'|^\'\'\'|^#|^\\/\\*|^\\/\\/|^\\{|^\\}|^\\[|^\\]|^:|^,|\\s$|[\x00-\x1f\x7f-\x9f\x{00ad}\x{0600}-\x{0604}\x{070f}\x{17b4}\x{17b5}\x{200c}-\x{200f}\x{2028}-\x{202f}\x{2060}-\x{206f}\x{feff}\x{fff0}-\x{ffff}\x]/u';
// needsEscapeML tests if the string can be written as a multiline string (includes needsEscape but without \n, \r, \\ and \")
private $needsEscapeML = '/^\\s+$|\'\'\'|[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\x9f\x{00ad}\x{0600}-\x{0604}\x{070f}\x{17b4}\x{17b5}\x{200c}-\x{200f}\x{2028}-\x{202f}\x{2060}-\x{206f}\x{feff}\x{fff0}-\x{ffff}\x]/u';
private $startsWithKeyword = '/^(true|false|null)\s*((,|\]|\}|#|\/\/|\/\*).*)?$/';
private $needsEscapeName = '/[,\{\[\}\]\s:#"\']|\/\/|\/\*|\'\'\'/';
private $gap = '';
private $indent = ' ';
private $meta = null;
// options
private $eol;
private $keepWsc;
private $bracesSameLine;
private $quoteAlways;
private $forceKeyQuotes;
private $emitRootBraces;
private $defaultBracesSameLine = false;
public function __construct()
{
$this->meta = [
"\t" => "\\t",
"\n" => "\\n",
"\r" => "\\r",
'"' => '\\"',
'\'' => '\\\'',
'\\' => "\\\\"
];
$this->meta[chr(8)] = '\\b';
$this->meta[chr(12)] = '\\f';
}
public function stringify($value, $opt = [])
{
$this->eol = PHP_EOL;
$this->indent = ' ';
$this->keepWsc = false;
$this->bracesSameLine = $this->defaultBracesSameLine;
$this->quoteAlways = false;
$this->forceKeyQuotes = false;
$this->emitRootBraces = true;
$space = null;
if ($opt && is_array($opt)) {
if (@$opt['eol'] === "\n" || @$opt['eol'] === "\r\n") {
$this->eol = $opt['eol'];
}
$space = @$opt['space'];
$this->keepWsc = @$opt['keepWsc'];
$this->bracesSameLine = @$opt['bracesSameLine'] || $this->defaultBracesSameLine;
$this->emitRootBraces = @$opt['emitRootBraces'];
$this->quoteAlways = @$opt['quotes'] === 'always';
$this->forceKeyQuotes = @$opt['keyQuotes'] === 'always';
}
// If the space parameter is a number, make an indent string containing that
// many spaces. If it is a string, it will be used as the indent string.
if (is_int($space)) {
$this->indent = '';
for ($i = 0; $i < $space; $i++) {
$this->indent .= ' ';
}
} elseif (is_string($space)) {
$this->indent = $space;
}
// Return the result of stringifying the value.
return $this->str($value, null, true, true);
}
public function stringifyWsc($value, $opt = [])
{
return $this->stringify($value, array_merge($opt, ['keepWsc' => true]));
}
private function isWhite($c)
{
return $c <= ' ';
}
private function quoteReplace($string)
{
mb_ereg_search_init($string, $this->needsEscape);
$r = mb_ereg_search();
$chars = mb_str_split($string);
$chars = array_map(function ($char) {
if (preg_match($this->needsEscape, $char)) {
$a = $char;
$c = @$this->meta[$a] ?: null;
if (gettype($c) === 'string') {
return $c;
} else {
return $char;
}
} else {
return $char;
}
}, $chars);
return implode('', $chars);
}
private function quote($string = null, $gap = null, $hasComment = null, $isRootObject = null)
{
if ($string === '' || $string === null) {
return '""';
}
// Check if we can insert this string without quotes
// see hjson syntax (must not parse as true, false, null or number)
if ($this->quoteAlways || $hasComment ||
preg_match($this->needsQuotes, $string) ||
HJSONUtils::tryParseNumber($string, true) !== null ||
preg_match($this->startsWithKeyword, $string)) {
// If the string contains no control characters, no quote characters, and no
// backslash characters, then we can safely slap some quotes around it.
// Otherwise we first check if the string can be expressed in multiline
// format or we must replace the offending characters with safe escape
// sequences.
if (!preg_match($this->needsEscape, $string)) {
return '"' . $string . '"';
} elseif (!preg_match($this->needsEscapeML, $string) && !$isRootObject) {
return $this->mlString($string, $gap);
} else {
return '"' . $this->quoteReplace($string) . '"';
}
} else {
// return without quotes
return $string;
}
}
private function mlString($string, $gap)
{
// wrap the string into the ''' (multiline) format
$a = explode("\n", mb_ereg_replace("\r", "", $string));
$gap .= $this->indent;
if (count($a) === 1) {
// The string contains only a single line. We still use the multiline
// format as it avoids escaping the \ character (e.g. when used in a
// regex).
return "'''" . $a[0] . "'''";
} else {
$res = $this->eol . $gap . "'''";
for ($i = 0; $i < count($a); $i++) {
$res .= $this->eol;
if ($a[$i] !== '') {
$res .= $gap . $a[$i];
}
}
return $res . $this->eol . $gap . "'''";
}
}
private function quoteName($name)
{
if ($name === '') {
return '""';
}
// Check if we can insert this name without quotes
if (preg_match($this->needsEscapeName, $name)) {
return '"' . (preg_match($this->needsEscape, $name) ? $this->quoteReplace($name) : $name) . '"';
} else {
// return without quotes
return $name;
}
}
private function str($value, $hasComment = null, $noIndent = null, $isRootObject = null)
{
// Produce a string from value.
$startsWithNL = function ($str) {
return $str && $str[$str[0] === "\r" ? 1 : 0] === "\n";
};
$testWsc = function ($str) use ($startsWithNL) {
return $str && !$startsWithNL($str);
};
$wsc = function ($str) {
if ($str === '') {
return "";
}
for ($i = 0; $i < mb_strlen($str); $i++) {
$c = $str[$i];
if ($c === "\n" ||
$c === '#' ||
$c === '/' && ($str[$i+1] === '/' || $str[$i+1] === '*')) {
break;
}
if ($c > ' ') {
return ' # ' . $str;
}
}
return $str;
};
// What happens next depends on the value's type.
switch (gettype($value)) {
case 'string':
$str = $this->quote($value, $this->gap, $hasComment, $isRootObject);
return $str;
case 'integer':
case 'double':
return is_numeric($value) ? str_replace('E', 'e', "$value") : 'null';
case 'boolean':
return $value ? 'true' : 'false';
case 'NULL':
return 'null';
case 'object':
case 'array':
$isArray = is_array($value);
$isAssocArray = function (array $arr) {
if (array() === $arr) {
return false;
}
return array_keys($arr) !== range(0, count($arr) - 1);
};
if ($isArray && $isAssocArray($value)) {
$value = (object) $value;
$isArray = false;
}
$kw = null;
$kwl = null; // whitespace & comments
if ($this->keepWsc) {
if ($isArray) {
$kw = @$value['__WSC__'];
} else {
$kw = @$value->__WSC__;
}
}
$showBraces = $isArray || !$isRootObject || ($kw ? !@$kw->noRootBraces : $this->emitRootBraces);
// Make an array to hold the partial results of stringifying this object value.
$mind = $this->gap;
if ($showBraces) {
$this->gap .= $this->indent;
}
$eolMind = $this->eol . $mind;
$eolGap = $this->eol . $this->gap;
$prefix = $noIndent || $this->bracesSameLine ? '' : $eolMind;
$partial = [];
$k;
$v; // key, value
if ($isArray) {
// The value is an array. Stringify every element. Use null as a placeholder
// for non-JSON values.
$length = count($value);
if (array_key_exists('__WSC__', $value)) {
$length--;
}
for ($i = 0; $i < $length; $i++) {
if ($kw) {
$partial[] = $wsc(@$kw[$i]) . $eolGap;
}
$str = $this->str($value[$i], $kw ? $testWsc(@$kw[$i+1]) : false, true);
$partial[] = $str !== null ? $str : 'null';
}
if ($kw) {
$partial[] = $wsc(@$kw[$i]) . $eolMind;
}
// Join all of the elements together, separated with newline, and wrap them in
// brackets.
if ($kw) {
$v = $prefix . '[' . implode('', $partial) . ']';
} elseif (count($partial) === 0) {
$v = '[]';
} else {
$v = $prefix . '[' . $eolGap . implode($eolGap, $partial) . $eolMind . ']';
}
} else {
// Otherwise, iterate through all of the keys in the object.
if ($kw) {
$emptyKey = " ";
$kwl = $wsc($kw->c->$emptyKey);
$keys = $kw->o;
foreach ($value as $k => $vvv) {
$keys[] = $k;
}
$keys = array_unique($keys);
for ($i = 0, $length = count($keys); $i < $length; $i++) {
$k = $keys[$i];
if ($k === '__WSC__') {
continue;
}
if ($showBraces || $i>0 || $kwl) {
$partial[] = $kwl . $eolGap;
}
$kwl = $wsc($kw->c->$k);
$v = $this->str($value->$k, $testWsc($kwl));
if ($v !== null) {
$partial[] = $this->quoteName($k) . ($startsWithNL($v) ? ':' : ': ') . $v;
}
}
if ($showBraces || $kwl) {
$partial[] = $kwl . $eolMind;
}
} else {
foreach ($value as $k => $vvv) {
$v = $this->str($vvv);
if ($v !== null) {
$partial[] = $this->quoteName($k) . ($startsWithNL($v) ? ':' : ': ') . $v;
}
}
}
// Join all of the member texts together, separated with newlines
if (count($partial) === 0) {
$v = '{}';
} elseif ($showBraces) {
// and wrap them in braces
if ($kw) {
$v = $prefix . '{' . implode('', $partial) . '}';
} else {
$v = $prefix . '{' . $eolGap . implode($eolGap, $partial) . $eolMind . '}';
}
} else {
$v = implode($kw ? '' : $eolGap, $partial);
}
}
$this->gap = $mind;
return $v;
}
}
}