From d7c500aab6c08989f5e4a7cb888ed05d8b9b08fe Mon Sep 17 00:00:00 2001 From: trobro Date: Sun, 20 Apr 2025 14:51:12 +0200 Subject: [PATCH 1/2] fix unicode escapes --- src/HJSON/HJSONStringifier.php | 4 +--- tests/HJSONParserTest.php | 8 +++++++- tests/assets/charset2_result.hjson | 5 +++++ tests/assets/charset2_result.json | 5 +++++ tests/assets/charset2_test.hjson | 5 +++++ tests/assets/failObj4_test.hjson | 4 ++++ tests/assets/strings3_result.hjson | 5 ++++- tests/assets/strings3_result.json | 5 ++++- tests/assets/strings3_test.hjson | 7 ++++++- tests/assets/strings4_result.hjson | 1 + tests/assets/strings4_result.json | 1 + tests/assets/strings4_test.hjson | 1 + 12 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 tests/assets/charset2_result.hjson create mode 100644 tests/assets/charset2_result.json create mode 100644 tests/assets/charset2_test.hjson create mode 100644 tests/assets/failObj4_test.hjson create mode 100644 tests/assets/strings4_result.hjson create mode 100644 tests/assets/strings4_result.json create mode 100644 tests/assets/strings4_test.hjson diff --git a/src/HJSON/HJSONStringifier.php b/src/HJSON/HJSONStringifier.php index ea88440..21001d3 100644 --- a/src/HJSON/HJSONStringifier.php +++ b/src/HJSON/HJSONStringifier.php @@ -101,8 +101,6 @@ private function isWhite($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)) { @@ -111,7 +109,7 @@ private function quoteReplace($string) if (gettype($c) === 'string') { return $c; } else { - return $char; + return substr(json_encode($char), 1, -1); } } else { return $char; diff --git a/tests/HJSONParserTest.php b/tests/HJSONParserTest.php index 6d5a115..cbd1f11 100644 --- a/tests/HJSONParserTest.php +++ b/tests/HJSONParserTest.php @@ -79,6 +79,7 @@ private function runEach($name, $file, $isJson, $inputCr, $outputCr) echo "Running test for $name, $file, ".(+$isJson).', '.(+$inputCr).', '.(+$outputCr)."\n"; $text = $this->load($file, $inputCr); $shouldFail = substr($name, 0, 4) === "fail"; + $unexpectedPass = false; try { $parser = new HJSONParser(); @@ -108,7 +109,7 @@ private function runEach($name, $file, $isJson, $inputCr, $outputCr) $hjson2 = $this->load("{$name}_result.hjson", $outputCr); $this->assertEquals($hjson1, $hjson2); } else { - $this->markTestIncomplete('This test succeeded on data that should fail.'); + $unexpectedPass = true; } } catch (HJSONException $e) { if (!$shouldFail) { @@ -116,6 +117,11 @@ private function runEach($name, $file, $isJson, $inputCr, $outputCr) throw $e; } } + + if ($unexpectedPass) { + echo "This test succeeded on data that should fail.\n"; + throw new HJSONException(); + } } public function testAll() diff --git a/tests/assets/charset2_result.hjson b/tests/assets/charset2_result.hjson new file mode 100644 index 0000000..e2733fa --- /dev/null +++ b/tests/assets/charset2_result.hjson @@ -0,0 +1,5 @@ +{ + uescape: "\u0000,\u0001,\uffff" + "um\u000blaut": äöüßÄÖÜ + hex: ģ䕧覫췯ꯍ +} diff --git a/tests/assets/charset2_result.json b/tests/assets/charset2_result.json new file mode 100644 index 0000000..440499e --- /dev/null +++ b/tests/assets/charset2_result.json @@ -0,0 +1,5 @@ +{ + "uescape": "\u0000,\u0001,\uffff", + "um\u000blaut": "äöüßÄÖÜ", + "hex": "ģ䕧覫췯ꯍ" +} diff --git a/tests/assets/charset2_test.hjson b/tests/assets/charset2_test.hjson new file mode 100644 index 0000000..1ccedc1 --- /dev/null +++ b/tests/assets/charset2_test.hjson @@ -0,0 +1,5 @@ +{ + uescape: "\u0000,\u0001,\uffff" + "um\u000blaut": äöüßÄÖÜ + hex: "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A" +} diff --git a/tests/assets/failObj4_test.hjson b/tests/assets/failObj4_test.hjson new file mode 100644 index 0000000..3da33dc --- /dev/null +++ b/tests/assets/failObj4_test.hjson @@ -0,0 +1,4 @@ +a: 1 +b: 2 +# trailing bracket in bracketless root +} diff --git a/tests/assets/strings3_result.hjson b/tests/assets/strings3_result.hjson index e16c76d..8845dc5 100644 --- a/tests/assets/strings3_result.hjson +++ b/tests/assets/strings3_result.hjson @@ -1 +1,4 @@ -"" +{ + a: "" + b: "\u00ad\u0600\u0604\u070f\u17b4\u17b5\u200c\u200f\u2028\u202f\u2060\u206f\ufeff\ufff0\uffff" +} diff --git a/tests/assets/strings3_result.json b/tests/assets/strings3_result.json index e16c76d..79bcfd6 100644 --- a/tests/assets/strings3_result.json +++ b/tests/assets/strings3_result.json @@ -1 +1,4 @@ -"" +{ + "a": "", + "b": "\u00ad\u0600\u0604\u070f\u17b4\u17b5\u200c\u200f\u2028\u202f\u2060\u206f\ufeff\ufff0\uffff" +} diff --git a/tests/assets/strings3_test.hjson b/tests/assets/strings3_test.hjson index 9cad736..d8e87d6 100644 --- a/tests/assets/strings3_test.hjson +++ b/tests/assets/strings3_test.hjson @@ -1 +1,6 @@ -'''''' +{ + // Empty string + a: "" + // Unicode code points that require escape inside quotes. + b: "\u00ad\u0600\u0604\u070f\u17b4\u17b5\u200c\u200f\u2028\u202f\u2060\u206f\ufeff\ufff0\uffff" +} \ No newline at end of file diff --git a/tests/assets/strings4_result.hjson b/tests/assets/strings4_result.hjson new file mode 100644 index 0000000..e16c76d --- /dev/null +++ b/tests/assets/strings4_result.hjson @@ -0,0 +1 @@ +"" diff --git a/tests/assets/strings4_result.json b/tests/assets/strings4_result.json new file mode 100644 index 0000000..e16c76d --- /dev/null +++ b/tests/assets/strings4_result.json @@ -0,0 +1 @@ +"" diff --git a/tests/assets/strings4_test.hjson b/tests/assets/strings4_test.hjson new file mode 100644 index 0000000..9cad736 --- /dev/null +++ b/tests/assets/strings4_test.hjson @@ -0,0 +1 @@ +'''''' From 6c2ee6487837e672de22f43eeff65afe6bfce0f9 Mon Sep 17 00:00:00 2001 From: trobro Date: Sun, 20 Apr 2025 14:54:09 +0200 Subject: [PATCH 2/2] eol --- tests/assets/strings3_test.hjson | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/assets/strings3_test.hjson b/tests/assets/strings3_test.hjson index d8e87d6..6811103 100644 --- a/tests/assets/strings3_test.hjson +++ b/tests/assets/strings3_test.hjson @@ -3,4 +3,4 @@ a: "" // Unicode code points that require escape inside quotes. b: "\u00ad\u0600\u0604\u070f\u17b4\u17b5\u200c\u200f\u2028\u202f\u2060\u206f\ufeff\ufff0\uffff" -} \ No newline at end of file +}