From a34b502a9e2beb230de5b8bd78d1ea4bbead57cc Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Tue, 26 Apr 2022 11:34:59 +0200 Subject: [PATCH] fix userland string serialization --- ion_private.h | 4 ++- tests/~integration/detect_utf8.phpt | 56 +++++++++++++++++++++++++++++ tests/~integration/no_decimals.phpt | 4 +-- 3 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 tests/~integration/detect_utf8.phpt diff --git a/ion_private.h b/ion_private.h index 2d1c54a..344d20e 100644 --- a/ion_private.h +++ b/ion_private.h @@ -1950,7 +1950,9 @@ static void php_ion_serialize_string(php_ion_serializer *ser, zend_string *str) ION_STRING is; ION_CHECK(ion_writer_write_string(php_ion_obj(writer, ser->wri)->writer, ion_string_from_zend(&is, str))); } else { - + zval zs; + ZVAL_STR(&zs, str); + zend_call_method_with_1_params(ser->wri, NULL, NULL, "writeString", NULL, &zs); } } diff --git a/tests/~integration/detect_utf8.phpt b/tests/~integration/detect_utf8.phpt new file mode 100644 index 0000000..e9fee64 --- /dev/null +++ b/tests/~integration/detect_utf8.phpt @@ -0,0 +1,56 @@ +--TEST-- +integration: custom serializer which naïvly decides to write strings/clobs/blobs +--EXTENSIONS-- +ion +--FILE-- +TEST + 1) { + return false; + } elseif ($c < "\040") return match ($c) { + "\n", "\r", "\t" => false, + default => true + }; + return $c >= "\177"; + } + public function writeString(string $value) : void { + $dist = [0, 0]; + foreach ($this->iterate($value) as $c) { + if (false === $c) { + goto binary; + } + ++$dist[$this->classify($c)]; + } + if ($dist[0] <= $dist[1]) { + binary: + parent::writeBLob($value); + } elseif ($dist[1]) { + parent::writeCLob($value); + } else { + parent::writeString($value); + } + } + }); + } +} + +$data = ["abc", "äöü", "\nabc\n", "foo\0bar", hex2bin("1f8b08003d96676200034bcacc4b2caae40200f5127b4207000000")]; +echo ion\serialize($data), "\n"; +echo ion\serialize($data, new AutoUtf8), "\n"; +?> +DONE +--EXPECTF-- +TEST +["abc","äöü","\nabc\n","foo\0bar","\x1F%s\x12{B\a\0\0\0"] +["abc","äöü","\nabc\n",{{"foo\0bar"}},{{H4sIAD2WZ2IAA0vKzEssquQCAPUSe0IHAAAA}}] +DONE diff --git a/tests/~integration/no_decimals.phpt b/tests/~integration/no_decimals.phpt index d938230..c81c3ab 100644 --- a/tests/~integration/no_decimals.phpt +++ b/tests/~integration/no_decimals.phpt @@ -1,12 +1,12 @@ --TEST-- -integration: do not write decimals but native floats only +integration: custom serializer which does not write decimals but native floats only --EXTENSIONS-- ion --FILE-- TEST