Skip to content

Commit cf65338

Browse files
committed
fix stringify for strings staring with a punctuator char
1 parent 25903ec commit cf65338

58 files changed

Lines changed: 201 additions & 52 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 4 additions & 0 deletions

src/HJSON/HJSONStringifier.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ function mb_str_split( $string ) {
88

99
class HJSONStringifier {
1010

11+
// needsEscape tests if the string can be written without escapes
1112
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';
12-
private $needsQuotes = '/[\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'; // like needsEscape but without \\ and \"
13-
private $needsEscapeML = '/\'\'\'|[\x00-\x09\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'; // ''' || (needsQuotes but without \n and \r)
13+
// needsQuotes tests if the string can be written as a quoteless string (includes needsEscape but without \\ and \")
14+
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';
15+
// needsEscapeML tests if the string can be written as a multiline string (includes needsEscape but without \n, \r, \\ and \")
16+
private $needsEscapeML = '/\'\'\'|[\x00-\x09\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';
1417
private $startsWithKeyword = '/^(true|false|null)\s*((,|\]|\}|#|\/\/|\/\*).*)?$/';
1518
private $needsEscapeName = '/[,\{\[\}\]\s:#"]|\/\/|\/\*|\'\'\'/';
1619
private $gap = '';
@@ -103,20 +106,10 @@ private function quote($string=null, $gap=null, $hasComment=null, $isRootObject=
103106
{
104107
if (!$string) return '""';
105108

106-
$doEscape = $this->quoteAlways || $hasComment || preg_match($this->needsQuotes, $string);
107-
108109
// Check if we can insert this string without quotes
109110
// see hjson syntax (must not parse as true, false, null or number)
110-
$first = $string[0]; $last = $string[mb_strlen($string)-1];
111-
if ($doEscape ||
112-
$this->isWhite($first) ||
113-
$first === '"' ||
114-
$first === "'" && $string[1] === "'" && $string[2] === "'" ||
115-
$first === '#' ||
116-
$first === '/' && ($string[1] === '*' || $string[1] === '/') ||
117-
$first === '{' ||
118-
$first === '[' ||
119-
$this->isWhite($last) ||
111+
if ($this->quoteAlways || $hasComment ||
112+
preg_match($this->needsQuotes, $string) ||
120113
HJSONUtils::tryParseNumber($string, true) !== null ||
121114
preg_match($this->startsWithKeyword, $string)) {
122115

0 commit comments

Comments
 (0)