Skip to content

Commit fcede41

Browse files
committed
ext/standard/array.c: implement transitive comparison for SORT_REGULAR
Add transitive comparison functions with deterministic ordering: - Numeric types and numeric strings compare numerically - Non-numeric strings sort after numeric types and numeric strings - NaN sorts after all other numeric values - Arrays recurse through transitive comparison - Objects (same class) recurse through transitive property comparison - Enums sort by object handle (stable grouping for array_unique) Fixes GH-20262
1 parent 8398038 commit fcede41

File tree

7 files changed

+951
-23
lines changed

7 files changed

+951
-23
lines changed

Zend/zend_object_handlers.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,6 +2119,8 @@ ZEND_API zend_function *zend_std_get_constructor(zend_object *zobj) /* {{{ */
21192119
}
21202120
/* }}} */
21212121

2122+
/* Note: php_array_compare_objects_transitive() in ext/standard/array.c mirrors
2123+
* this function for SORT_REGULAR. Changes here may need to be reflected there. */
21222124
ZEND_API int zend_std_compare_objects(zval *o1, zval *o2) /* {{{ */
21232125
{
21242126
zend_object *zobj1, *zobj2;

Zend/zend_operators.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,6 +2257,8 @@ ZEND_API zend_result ZEND_FASTCALL compare_function(zval *result, zval *op1, zva
22572257
}
22582258
/* }}} */
22592259

2260+
/* Note: php_array_compare_long_to_string_transitive() in ext/standard/array.c mirrors
2261+
* this function for SORT_REGULAR. Changes here may need to be reflected there. */
22602262
static int compare_long_to_string(zend_long lval, zend_string *str) /* {{{ */
22612263
{
22622264
zend_long str_lval;
@@ -2279,6 +2281,8 @@ static int compare_long_to_string(zend_long lval, zend_string *str) /* {{{ */
22792281
}
22802282
/* }}} */
22812283

2284+
/* Note: php_array_compare_double_to_string_transitive() in ext/standard/array.c mirrors
2285+
* this function for SORT_REGULAR. Changes here may need to be reflected there. */
22822286
static int compare_double_to_string(double dval, zend_string *str) /* {{{ */
22832287
{
22842288
zend_long str_lval;
@@ -2303,6 +2307,8 @@ static int compare_double_to_string(double dval, zend_string *str) /* {{{ */
23032307
}
23042308
/* }}} */
23052309

2310+
/* Note: php_array_compare_transitive() in ext/standard/array.c mirrors this
2311+
* function for SORT_REGULAR. Changes here may need to be reflected there. */
23062312
ZEND_API int ZEND_FASTCALL zend_compare(zval *op1, zval *op2) /* {{{ */
23072313
{
23082314
bool converted = false;
@@ -3418,6 +3424,8 @@ ZEND_API bool ZEND_FASTCALL zendi_smart_streq(zend_string *s1, zend_string *s2)
34183424
}
34193425
/* }}} */
34203426

3427+
/* Note: php_array_smart_strcmp_transitive() in ext/standard/array.c mirrors
3428+
* this function for SORT_REGULAR. Changes here may need to be reflected there. */
34213429
ZEND_API int ZEND_FASTCALL zendi_smart_strcmp(zend_string *s1, zend_string *s2) /* {{{ */
34223430
{
34233431
uint8_t ret1, ret2;
@@ -3475,6 +3483,8 @@ static int hash_zval_compare_function(zval *z1, zval *z2) /* {{{ */
34753483
}
34763484
/* }}} */
34773485

3486+
/* Note: php_array_compare_symbol_tables_transitive() in ext/standard/array.c mirrors
3487+
* this function for SORT_REGULAR. Changes here may need to be reflected there. */
34783488
ZEND_API int ZEND_FASTCALL zend_compare_symbol_tables(HashTable *ht1, HashTable *ht2) /* {{{ */
34793489
{
34803490
if (ht1 == ht2) {
@@ -3493,6 +3503,8 @@ ZEND_API int ZEND_FASTCALL zend_compare_symbol_tables(HashTable *ht1, HashTable
34933503
}
34943504
/* }}} */
34953505

3506+
/* Note: php_array_compare_arrays_transitive() in ext/standard/array.c mirrors
3507+
* this function for SORT_REGULAR. Changes here may need to be reflected there. */
34963508
ZEND_API int ZEND_FASTCALL zend_compare_arrays(zval *a1, zval *a2) /* {{{ */
34973509
{
34983510
return zend_compare_symbol_tables(Z_ARRVAL_P(a1), Z_ARRVAL_P(a2));

0 commit comments

Comments
 (0)