Skip to content

Commit 8b25640

Browse files
authored
Merge pull request #19 from hjson/fix-utf8
(mostly) fix utf8 implementation
2 parents 602b46e + e1f62ba commit 8b25640

5 files changed

Lines changed: 32 additions & 16 deletions

File tree

src/HJSON/HJSONParser.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,18 +308,30 @@ private function white()
308308

309309
private function error($m)
310310
{
311-
$i=0;
312311
$col=0;
312+
$colBytes = 0;
313313
$line=1;
314-
for ($i = $this->at-1; $i > 0 && @$this->text[$i] !== "\n"; $i--, $col++) {
314+
315+
$i = $this->at;
316+
while ($i > 0) {
317+
$ch = mb_substr(mb_strcut($this->text, $i - 1), 0, 1);
318+
$i -= strlen($ch);
319+
320+
if ($ch === "\n") {
321+
break;
322+
}
323+
324+
$col++;
325+
$colBytes += strlen($ch);
315326
}
327+
316328
for (; $i > 0;
317329
$i--) {
318330
if ($this->text[$i] === "\n") {
319331
$line++;
320332
}
321333
}
322-
throw new HJSONException("$m at line $line, $col >>>". mb_substr($this->text, $this->at - $col, 20) ." ...");
334+
throw new HJSONException("$m at line $line, $col >>>". mb_substr(mb_strcut($this->text, $this->at - $colBytes), 0, 20) ." ...");
323335
}
324336

325337
private function next($c = false)
@@ -332,15 +344,19 @@ private function next($c = false)
332344

333345
// Get the next character. When there are no more characters,
334346
// return the empty string.
335-
$this->ch = (mb_strlen($this->text) > $this->at) ? $this->text[$this->at] : null;
336-
$this->at++;
347+
$this->ch = (strlen($this->text) > $this->at) ? mb_substr(mb_strcut($this->text, $this->at), 0, 1) : null;
348+
$this->at += strlen($this->ch);
337349
return $this->ch;
338350
}
339351

340352
private function peek($offs)
341353
{
342354
// range check is not required
343-
return $this->text[$this->at + $offs];
355+
if ($offs >= 0) {
356+
return mb_substr(mb_strcut($this->text, $this->at), $offs, 1);
357+
} else {
358+
return mb_substr(mb_strcut($this->text, 0, $this->at), $offs, 1);
359+
}
344360
}
345361

346362
private function skipIndent($indent)

tests/HJSONParserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class HJSONParserTest extends TestCase
1111
{
1212

13-
public function setUp()
13+
protected function setUp()
1414
{
1515
parent::setUp();
1616
$this->rootDir = dirname(__FILE__).DIRECTORY_SEPARATOR."assets";

tests/assets/charset_result.hjson

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

tests/assets/charset_result.json

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

tests/assets/charset_test.hjson

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

0 commit comments

Comments
 (0)