diff options
author | Anatol Belski <ab@php.net> | 2015-07-21 12:27:50 +0200 |
---|---|---|
committer | Anatol Belski <ab@php.net> | 2015-07-21 12:27:50 +0200 |
commit | b148ef44965651fcbe93e8ee35a14edc087ef17c (patch) | |
tree | f5c53fb3d448d0174610d077e0bf3c8bd76834b2 /ext/standard/array.c | |
parent | ad8a73dd55c087de465ad80e8715611693bb1460 (diff) | |
parent | 6065b29fe41f09e01dd06ba21980e0344f13230c (diff) | |
download | php-git-b148ef44965651fcbe93e8ee35a14edc087ef17c.tar.gz |
Merge branch 'master' into PHP-7.0.0
* master: (204 commits)
Reverted ad4533fdbabcc3e545277e30023b2fdce16297a0
update UPGRADING
updated NEWS
fix comment
libwebp support for linux
Replaced libvpx by libwebp (first draft; Windows only)
update news with bug #70022
Change E_ERROR and some E_WARNING to E_RECOVERABLE_ERROR.
Add tests for json_last_error()/json_last_error_msg() failures
updated NEWS
updated NEWS
Exclude opcache from a few opcode related tests
updated NEWS
updated NEWS
Fix #66387: Stack overflow with imagefilltoborder
Fix various Windows issues (e.g. dir separators)
Remove bogus exception_save() from FETCH_CLASS
Fix readline/libedit build
Do not use readline when not having a tty This is important for e.g. run-tests.php
Add show_unexecuted option to phpdbg_end_oplog()
...
Conflicts:
Zend/tests/temporary_cleaning_001.phpt
Zend/tests/temporary_cleaning_003.phpt
Zend/tests/temporary_cleaning_004.phpt
Zend/tests/temporary_cleaning_005.phpt
Zend/zend_compile.c
Zend/zend_compile.h
sapi/phpdbg/phpdbg_opcode.c
Diffstat (limited to 'ext/standard/array.c')
-rw-r--r-- | ext/standard/array.c | 121 |
1 files changed, 25 insertions, 96 deletions
diff --git a/ext/standard/array.c b/ext/standard/array.c index 696ad05ff6..7ef9d73fad 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -615,13 +615,11 @@ static int php_array_user_compare(const void *a, const void *b) /* {{{ */ BG(user_compare_fci) = old_user_compare_fci; \ BG(user_compare_fci_cache) = old_user_compare_fci_cache; \ -/* {{{ proto bool usort(array array_arg, string cmp_function) - Sort an array by values using a user-defined comparison function */ -PHP_FUNCTION(usort) +static void php_usort(INTERNAL_FUNCTION_PARAMETERS, compare_func_t compare_func, zend_bool renumber) /* {{{ */ { zval *array; zend_refcounted *arr; - unsigned int refcount; + zend_bool retval; PHP_ARRAY_CMP_FUNC_VARS; PHP_ARRAY_CMP_FUNC_BACKUP(); @@ -633,30 +631,35 @@ PHP_FUNCTION(usort) /* Increase reference counter, so the attempts to modify the array in user * comparison function will create a copy of array and won't affect the - * original array. The fact of modification is detected using refcount - * comparison. The result of sorting in such case is undefined and the - * function returns FALSE. + * original array. The fact of modification is detected by comparing the + * zend_array pointer. The result of sorting in such case is undefined and + * the function returns FALSE. */ Z_ADDREF_P(array); - refcount = Z_REFCOUNT_P(array); arr = Z_COUNTED_P(array); - if (zend_hash_sort(Z_ARRVAL_P(array), php_array_user_compare, 1) == FAILURE) { - RETVAL_FALSE; - } else { - if (refcount > Z_REFCOUNT_P(array)) { - php_error_docref(NULL, E_WARNING, "Array was modified by the user comparison function"); - if (--GC_REFCOUNT(arr) <= 0) { - _zval_dtor_func(arr ZEND_FILE_LINE_CC); - } - RETVAL_FALSE; - } else { - Z_DELREF_P(array); - RETVAL_TRUE; + retval = zend_hash_sort(Z_ARRVAL_P(array), compare_func, renumber) != FAILURE; + + if (arr != Z_COUNTED_P(array)) { + php_error_docref(NULL, E_WARNING, "Array was modified by the user comparison function"); + if (--GC_REFCOUNT(arr) <= 0) { + _zval_dtor_func(arr ZEND_FILE_LINE_CC); } + retval = 0; + } else { + Z_DELREF_P(array); } PHP_ARRAY_CMP_FUNC_RESTORE(); + RETURN_BOOL(retval); +} +/* }}} */ + +/* {{{ proto bool usort(array array_arg, string cmp_function) + Sort an array by values using a user-defined comparison function */ +PHP_FUNCTION(usort) +{ + php_usort(INTERNAL_FUNCTION_PARAM_PASSTHRU, php_array_user_compare, 1); } /* }}} */ @@ -664,44 +667,7 @@ PHP_FUNCTION(usort) Sort an array with a user-defined comparison function and maintain index association */ PHP_FUNCTION(uasort) { - zval *array; - zend_refcounted *arr; - unsigned int refcount; - PHP_ARRAY_CMP_FUNC_VARS; - - PHP_ARRAY_CMP_FUNC_BACKUP(); - - if (zend_parse_parameters(ZEND_NUM_ARGS(), "a/f", &array, &BG(user_compare_fci), &BG(user_compare_fci_cache)) == FAILURE) { - PHP_ARRAY_CMP_FUNC_RESTORE(); - return; - } - - /* Increase reference counter, so the attempts to modify the array in user - * comparison function will create a copy of array and won't affect the - * original array. The fact of modification is detected using refcount - * comparison. The result of sorting in such case is undefined and the - * function returns FALSE. - */ - Z_ADDREF_P(array); - refcount = Z_REFCOUNT_P(array); - arr = Z_COUNTED_P(array); - - if (zend_hash_sort(Z_ARRVAL_P(array), php_array_user_compare, 0) == FAILURE) { - RETVAL_FALSE; - } else { - if (refcount > Z_REFCOUNT_P(array)) { - php_error_docref(NULL, E_WARNING, "Array was modified by the user comparison function"); - if (--GC_REFCOUNT(arr) <= 0) { - _zval_dtor_func(arr ZEND_FILE_LINE_CC); - } - RETVAL_FALSE; - } else { - Z_DELREF_P(array); - RETVAL_TRUE; - } - } - - PHP_ARRAY_CMP_FUNC_RESTORE(); + php_usort(INTERNAL_FUNCTION_PARAM_PASSTHRU, php_array_user_compare, 0); } /* }}} */ @@ -752,44 +718,7 @@ static int php_array_user_key_compare(const void *a, const void *b) /* {{{ */ Sort an array by keys using a user-defined comparison function */ PHP_FUNCTION(uksort) { - zval *array; - zend_refcounted *arr; - unsigned int refcount; - PHP_ARRAY_CMP_FUNC_VARS; - - PHP_ARRAY_CMP_FUNC_BACKUP(); - - if (zend_parse_parameters(ZEND_NUM_ARGS(), "a/f", &array, &BG(user_compare_fci), &BG(user_compare_fci_cache)) == FAILURE) { - PHP_ARRAY_CMP_FUNC_RESTORE(); - return; - } - - /* Increase reference counter, so the attempts to modify the array in user - * comparison function will create a copy of array and won't affect the - * original array. The fact of modification is detected using refcount - * comparison. The result of sorting in such case is undefined and the - * function returns FALSE. - */ - Z_ADDREF_P(array); - refcount = Z_REFCOUNT_P(array); - arr = Z_COUNTED_P(array); - - if (zend_hash_sort(Z_ARRVAL_P(array), php_array_user_key_compare, 0) == FAILURE) { - RETVAL_FALSE; - } else { - if (refcount > Z_REFCOUNT_P(array)) { - php_error_docref(NULL, E_WARNING, "Array was modified by the user comparison function"); - if (--GC_REFCOUNT(arr) <= 0) { - _zval_dtor_func(arr ZEND_FILE_LINE_CC); - } - RETVAL_FALSE; - } else { - Z_DELREF_P(array); - RETVAL_TRUE; - } - } - - PHP_ARRAY_CMP_FUNC_RESTORE(); + php_usort(INTERNAL_FUNCTION_PARAM_PASSTHRU, php_array_user_key_compare, 0); } /* }}} */ |