Skip to content

Commit 5a7de5a

Browse files
committed
ext/standard/array.c: refactor sort functions to use shared php_sort helper
- Replace php_array_apply_sort with php_sort that handles parameter parsing - Consolidate duplicate parameter parsing code across asort, arsort, sort, rsort, krsort, and ksort - Each sort function now simply calls php_sort with appropriate compare function and renumber flag
1 parent 25ed7f7 commit 5a7de5a

File tree

1 file changed

+17
-65
lines changed

1 file changed

+17
-65
lines changed

ext/standard/array.c

Lines changed: 17 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -990,111 +990,63 @@ PHP_FUNCTION(natcasesort)
990990

991991
typedef bucket_compare_func_t(*get_compare_function)(zend_long);
992992

993-
static zend_always_inline void php_array_apply_sort(HashTable *array, zend_long sort_type, get_compare_function get_cmp, bool renumber)
994-
{
995-
bucket_compare_func_t cmp = get_cmp(sort_type);
996-
zend_array_sort(array, cmp, renumber);
997-
}
998-
999-
/* {{{ Sort an array and maintain index association */
1000-
PHP_FUNCTION(asort)
1001-
{
993+
static zend_always_inline void php_sort(INTERNAL_FUNCTION_PARAMETERS, get_compare_function get_cmp, bool renumber) {
1002994
HashTable *array;
1003995
zend_long sort_type = PHP_SORT_REGULAR;
996+
bucket_compare_func_t cmp;
1004997

1005998
ZEND_PARSE_PARAMETERS_START(1, 2)
1006999
Z_PARAM_ARRAY_HT_EX(array, 0, 1)
10071000
Z_PARAM_OPTIONAL
10081001
Z_PARAM_LONG(sort_type)
10091002
ZEND_PARSE_PARAMETERS_END();
10101003

1011-
php_array_apply_sort(array, sort_type, php_get_data_compare_func, false);
1004+
cmp = get_cmp(sort_type);
1005+
1006+
zend_array_sort(array, cmp, renumber);
1007+
10121008
RETURN_TRUE;
10131009
}
1010+
1011+
/* {{{ Sort an array and maintain index association */
1012+
PHP_FUNCTION(asort)
1013+
{
1014+
php_sort(INTERNAL_FUNCTION_PARAM_PASSTHRU, php_get_data_compare_func, false);
1015+
}
10141016
/* }}} */
10151017

10161018
/* {{{ Sort an array in reverse order and maintain index association */
10171019
PHP_FUNCTION(arsort)
10181020
{
1019-
HashTable *array;
1020-
zend_long sort_type = PHP_SORT_REGULAR;
1021-
1022-
ZEND_PARSE_PARAMETERS_START(1, 2)
1023-
Z_PARAM_ARRAY_HT_EX(array, 0, 1)
1024-
Z_PARAM_OPTIONAL
1025-
Z_PARAM_LONG(sort_type)
1026-
ZEND_PARSE_PARAMETERS_END();
1027-
1028-
php_array_apply_sort(array, sort_type, php_get_data_reverse_compare_func, false);
1029-
RETURN_TRUE;
1021+
php_sort(INTERNAL_FUNCTION_PARAM_PASSTHRU, php_get_data_reverse_compare_func, false);
10301022
}
10311023
/* }}} */
10321024

10331025
/* {{{ Sort an array */
10341026
PHP_FUNCTION(sort)
10351027
{
1036-
HashTable *array;
1037-
zend_long sort_type = PHP_SORT_REGULAR;
1038-
1039-
ZEND_PARSE_PARAMETERS_START(1, 2)
1040-
Z_PARAM_ARRAY_HT_EX(array, 0, 1)
1041-
Z_PARAM_OPTIONAL
1042-
Z_PARAM_LONG(sort_type)
1043-
ZEND_PARSE_PARAMETERS_END();
1044-
1045-
php_array_apply_sort(array, sort_type, php_get_data_compare_func, true);
1046-
RETURN_TRUE;
1028+
php_sort(INTERNAL_FUNCTION_PARAM_PASSTHRU, php_get_data_compare_func, true);
10471029
}
10481030
/* }}} */
10491031

10501032
/* {{{ Sort an array in reverse order */
10511033
PHP_FUNCTION(rsort)
10521034
{
1053-
HashTable *array;
1054-
zend_long sort_type = PHP_SORT_REGULAR;
1055-
1056-
ZEND_PARSE_PARAMETERS_START(1, 2)
1057-
Z_PARAM_ARRAY_HT_EX(array, 0, 1)
1058-
Z_PARAM_OPTIONAL
1059-
Z_PARAM_LONG(sort_type)
1060-
ZEND_PARSE_PARAMETERS_END();
1061-
1062-
php_array_apply_sort(array, sort_type, php_get_data_reverse_compare_func, true);
1063-
RETURN_TRUE;
1035+
php_sort(INTERNAL_FUNCTION_PARAM_PASSTHRU, php_get_data_reverse_compare_func, true);
10641036
}
10651037
/* }}} */
10661038

10671039
/* {{{ Sort an array by key value in reverse order */
10681040
PHP_FUNCTION(krsort)
10691041
{
1070-
HashTable *array;
1071-
zend_long sort_type = PHP_SORT_REGULAR;
1072-
1073-
ZEND_PARSE_PARAMETERS_START(1, 2)
1074-
Z_PARAM_ARRAY_HT_EX(array, 0, 1)
1075-
Z_PARAM_OPTIONAL
1076-
Z_PARAM_LONG(sort_type)
1077-
ZEND_PARSE_PARAMETERS_END();
1078-
1079-
php_array_apply_sort(array, sort_type, php_get_key_reverse_compare_func, false);
1080-
RETURN_TRUE;
1042+
php_sort(INTERNAL_FUNCTION_PARAM_PASSTHRU, php_get_key_reverse_compare_func, false);
10811043
}
10821044
/* }}} */
10831045

10841046
/* {{{ Sort an array by key */
10851047
PHP_FUNCTION(ksort)
10861048
{
1087-
HashTable *array;
1088-
zend_long sort_type = PHP_SORT_REGULAR;
1089-
1090-
ZEND_PARSE_PARAMETERS_START(1, 2)
1091-
Z_PARAM_ARRAY_HT_EX(array, 0, 1)
1092-
Z_PARAM_OPTIONAL
1093-
Z_PARAM_LONG(sort_type)
1094-
ZEND_PARSE_PARAMETERS_END();
1095-
1096-
php_array_apply_sort(array, sort_type, php_get_key_compare_func, false);
1097-
RETURN_TRUE;
1049+
php_sort(INTERNAL_FUNCTION_PARAM_PASSTHRU, php_get_key_compare_func, false);
10981050
}
10991051
/* }}} */
11001052

0 commit comments

Comments
 (0)