From: Michael Wallner Date: Wed, 15 Dec 2021 19:54:58 +0000 (+0100) Subject: improve symbol comparison X-Git-Tag: v0.1.0~71 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=59145a019faf6d4c60cdbbd965c9377e48d59cf8;p=awesomized%2Fext-ion improve symbol comparison --- diff --git a/ion.c b/ion.c index a4da27e..5a691fd 100644 --- a/ion.c +++ b/ion.c @@ -1532,6 +1532,7 @@ PHP_MINIT_FUNCTION(ion) // Symbol php_ion_register(symbol, Symbol); + oh_Symbol.compare = php_ion_symbol_zval_compare; php_ion_register(symbol_iloc, Symbol_ImportLocation); php_ion_register(symbol_table, Symbol_Table); ce_Symbol_System = register_class_ion_Symbol_System(); diff --git a/ion_private.h b/ion_private.h index a4d89d0..809f853 100644 --- a/ion_private.h +++ b/ion_private.h @@ -332,6 +332,27 @@ typedef struct php_ion_symbol { zend_object *iloc, std; } php_ion_symbol; +static inline int php_ion_symbol_zval_compare(zval *zv1, zval *zv2) { + zend_string *zs1 = zval_get_string(zv1); + zend_string *zs2 = zval_get_string(zv2); + + if (EG(exception)) { + return 0; + } + + int result; + if (zs1->len > zs2->len) { + result = 1; + } else if (zs2->len > zs1->len) { + result = -1; + } else { + result = memcmp(zs1->val, zs2->val, zs1->len); + } + zend_string_release(zs1); + zend_string_release(zs2); + return result; +} + static inline void php_ion_symbol_ctor(php_ion_symbol *obj) { zend_update_property_long(obj->std.ce, &obj->std, ZEND_STRL("sid"), @@ -1492,6 +1513,7 @@ static void php_ion_unserialize_props(php_ion_unserializer *ser, zval *return_va } /** + * @link https://amzn.github.io/ion-docs/docs/spec.html#struct * When two fields in the same struct have the same name [...] Implementations must preserve all such fields, * i.e., they may not discard fields that have repeated names. However, implementations may reorder fields * (the binary format identifies structs that are sorted by symbolID), so certain operations may lead to