Skip to content

Commit 5dccc53

Browse files
authored
Fix unicode escapes (#46)
1 parent 2d1b8b4 commit 5dccc53

12 files changed

Lines changed: 44 additions & 7 deletions

src/HJSON/HJSONStringifier.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ private function isWhite($c)
101101

102102
private function quoteReplace($string)
103103
{
104-
mb_ereg_search_init($string, $this->needsEscape);
105-
$r = mb_ereg_search();
106104
$chars = mb_str_split($string);
107105
$chars = array_map(function ($char) {
108106
if (preg_match($this->needsEscape, $char)) {
@@ -111,7 +109,7 @@ private function quoteReplace($string)
111109
if (gettype($c) === 'string') {
112110
return $c;
113111
} else {
114-
return $char;
112+
return substr(json_encode($char), 1, -1);
115113
}
116114
} else {
117115
return $char;

tests/HJSONParserTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ private function runEach($name, $file, $isJson, $inputCr, $outputCr)
7979
echo "Running test for $name, $file, ".(+$isJson).', '.(+$inputCr).', '.(+$outputCr)."\n";
8080
$text = $this->load($file, $inputCr);
8181
$shouldFail = substr($name, 0, 4) === "fail";
82+
$unexpectedPass = false;
8283

8384
try {
8485
$parser = new HJSONParser();
@@ -108,14 +109,19 @@ private function runEach($name, $file, $isJson, $inputCr, $outputCr)
108109
$hjson2 = $this->load("{$name}_result.hjson", $outputCr);
109110
$this->assertEquals($hjson1, $hjson2);
110111
} else {
111-
$this->markTestIncomplete('This test succeeded on data that should fail.');
112+
$unexpectedPass = true;
112113
}
113114
} catch (HJSONException $e) {
114115
if (!$shouldFail) {
115116
echo "\n$e\n";
116117
throw $e;
117118
}
118119
}
120+
121+
if ($unexpectedPass) {
122+
echo "This test succeeded on data that should fail.\n";
123+
throw new HJSONException();
124+
}
119125
}
120126

121127
public function testAll()

tests/assets/charset2_result.hjson

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
uescape: "\u0000,\u0001,\uffff"
3+
"um\u000blaut": äöüßÄÖÜ
4+
hex: ģ䕧覫췯ꯍ
5+
}

tests/assets/charset2_result.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"uescape": "\u0000,\u0001,\uffff",
3+
"um\u000blaut": "äöüßÄÖÜ",
4+
"hex": "ģ䕧覫췯ꯍ"
5+
}

tests/assets/charset2_test.hjson

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
uescape: "\u0000,\u0001,\uffff"
3+
"um\u000blaut": äöüßÄÖÜ
4+
hex: "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A"
5+
}

tests/assets/failObj4_test.hjson

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
a: 1
2+
b: 2
3+
# trailing bracket in bracketless root
4+
}

tests/assets/strings3_result.hjson

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
""
1+
{
2+
a: ""
3+
b: "\u00ad\u0600\u0604\u070f\u17b4\u17b5\u200c\u200f\u2028\u202f\u2060\u206f\ufeff\ufff0\uffff"
4+
}

tests/assets/strings3_result.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
""
1+
{
2+
"a": "",
3+
"b": "\u00ad\u0600\u0604\u070f\u17b4\u17b5\u200c\u200f\u2028\u202f\u2060\u206f\ufeff\ufff0\uffff"
4+
}

tests/assets/strings3_test.hjson

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
''''''
1+
{
2+
// Empty string
3+
a: ""
4+
// Unicode code points that require escape inside quotes.
5+
b: "\u00ad\u0600\u0604\u070f\u17b4\u17b5\u200c\u200f\u2028\u202f\u2060\u206f\ufeff\ufff0\uffff"
6+
}

tests/assets/strings4_result.hjson

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
""

0 commit comments

Comments
 (0)