Skip to content

Commit 996e723

Browse files
committed
fix #4, ml string containing '0'
1 parent 49d3156 commit 996e723

4 files changed

Lines changed: 33 additions & 16 deletions

File tree

src/HJSON/HJSONParser.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,12 @@ function __construct() {
2525

2626
public function parse($source, $options=[])
2727
{
28-
$result;
29-
3028
$this->keepWsc = $options && $options['keepWsc'];
3129
$this->text = $source;
32-
$this->resetAt();
33-
$result = $this->rootValue();
34-
$this->white();
35-
36-
if ($this->ch) throw new HJSONException("Syntax error, found trailing characters");
37-
38-
return $result;
30+
return $this->rootValue();
3931
}
4032

41-
function resetAt()
33+
private function resetAt()
4234
{
4335
$this->at = 0;
4436
$this->ch = ' ';
@@ -49,23 +41,32 @@ public function parseWsc($source, $options=[])
4941
return $this->parse($source, array_merge($options, ['keepWsc' => true]));
5042
}
5143

44+
private function checkExit($result)
45+
{
46+
$this->white();
47+
if ($this->ch !== null) $this->error("Syntax error, found trailing characters!");
48+
return $result;
49+
}
50+
5251
private function rootValue()
5352
{
5453
// Braces for the root object are optional
54+
55+
$this->resetAt();
5556
$this->white();
5657
switch ($this->ch) {
57-
case '{': return $this->object();
58-
case '[': return $this->_array();
58+
case '{': return $this->checkExit($this->object());
59+
case '[': return $this->checkExit($this->_array());
5960
}
6061

6162
try {
6263
// assume we have a root object without braces
63-
return $this->object(true);
64+
return $this->checkExit($this->object(true));
6465
}
6566
catch (HJSONException $e) {
6667
// test if we are dealing with a single JSON value instead (true/false/null/num/"")
6768
$this->resetAt();
68-
try { return $this->value(); }
69+
try { return $this->checkExit($this->value()); }
6970
catch (HJSONException $e2) { throw $e; } // throw original error
7071
}
7172
}
@@ -272,7 +273,7 @@ private function mlString()
272273
$indent = 0;
273274
while (true) {
274275
$c = $this->peek(-$indent-5);
275-
if (!$c || $c === "\n") break;
276+
if ($c === null || $c === "\n") break;
276277
$indent++;
277278
}
278279

@@ -282,7 +283,7 @@ private function mlString()
282283

283284
// When parsing multiline string values, we must look for ' characters.
284285
while (true) {
285-
if (!$this->ch) $this->error("Bad multiline string");
286+
if ($this->ch === null) $this->error("Bad multiline string");
286287
else if ($this->ch === '\'') {
287288
$triple++;
288289
$this->next();

tests/assets/charset_result.hjson

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
ql-ascii: ! "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
3+
js-ascii: ! "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
4+
ml-ascii: ! "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
5+
}

tests/assets/charset_result.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"ql-ascii": "! \"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
3+
"js-ascii": "! \"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
4+
"ml-ascii": "! \"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
5+
}

tests/assets/charset_test.hjson

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ql-ascii: ! "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
2+
js-ascii: "! \"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
3+
ml-ascii:
4+
'''
5+
! "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
6+
'''

0 commit comments

Comments
 (0)