From: Colin O'Dell Date: Mon, 21 Oct 2019 21:22:04 +0000 (-0400) Subject: Allow array_splice() length to be null X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e7335eb420e40d2226fbf1c2b573875323667560;p=php Allow array_splice() length to be null --- diff --git a/UPGRADING b/UPGRADING index 40af9ca458..41cab54f1e 100644 --- a/UPGRADING +++ b/UPGRADING @@ -275,6 +275,9 @@ PHP 8.0 UPGRADE NOTES iconv_substr() can now be null. Null values will behave as if no length argument was provided and will therefore return the remainder of the string instead of an empty string. + . The length argument for array_splice() can now be null. Null values will + behave identically to omitting the argument, thus removing everything from + the 'offset' to the end of the array. . The 'salt' option of password_hash() is no longer supported. If the 'salt' option is used a warning is generated, the provided salt is ignored, and a generated salt is used instead. diff --git a/ext/standard/array.c b/ext/standard/array.c index d3aac515a7..8fe281da26 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -3401,19 +3401,20 @@ PHP_FUNCTION(array_splice) HashTable *rem_hash = NULL; zend_long offset, length = 0; + zend_bool length_is_null = 1; int num_in; /* Number of elements in the input array */ ZEND_PARSE_PARAMETERS_START(2, 4) Z_PARAM_ARRAY_EX(array, 0, 1) Z_PARAM_LONG(offset) Z_PARAM_OPTIONAL - Z_PARAM_LONG(length) + Z_PARAM_LONG_OR_NULL(length, length_is_null) Z_PARAM_ZVAL(repl_array) ZEND_PARSE_PARAMETERS_END(); num_in = zend_hash_num_elements(Z_ARRVAL_P(array)); - if (ZEND_NUM_ARGS() < 3) { + if (length_is_null) { length = num_in; } diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php index 78bc728d2a..cacaa7ba3a 100755 --- a/ext/standard/basic_functions.stub.php +++ b/ext/standard/basic_functions.stub.php @@ -163,7 +163,7 @@ function array_shift(array &$stack) {} function array_unshift(array &$stack, ...$vars): int {} -function array_splice(array &$arg, int $offset, int $length = UNKNOWN, $replacement = []): array {} +function array_splice(array &$arg, int $offset, ?int $length = null, $replacement = []): array {} function array_slice(array $arg, int $offset, ?int $length = null, bool $preserve_keys = false): array {} diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h index 56b0f116c4..40bac9cc7e 100755 --- a/ext/standard/basic_functions_arginfo.h +++ b/ext/standard/basic_functions_arginfo.h @@ -194,7 +194,7 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_array_splice, 0, 2, IS_ARRAY, 0) ZEND_ARG_TYPE_INFO(1, arg, IS_ARRAY, 0) ZEND_ARG_TYPE_INFO(0, offset, IS_LONG, 0) - ZEND_ARG_TYPE_INFO(0, length, IS_LONG, 0) + ZEND_ARG_TYPE_INFO(0, length, IS_LONG, 1) ZEND_ARG_INFO(0, replacement) ZEND_END_ARG_INFO() diff --git a/ext/standard/tests/array/array_splice_basic.phpt b/ext/standard/tests/array/array_splice_basic.phpt index 9a96ccb362..cf6fcd1db2 100644 --- a/ext/standard/tests/array/array_splice_basic.phpt +++ b/ext/standard/tests/array/array_splice_basic.phpt @@ -14,6 +14,12 @@ var_dump (array_splice($input, 2)); var_dump ($input); // $input is now array("red", "green") +echo "test truncation with null length \n"; +$input = array("red", "green", "blue", "yellow"); +var_dump (array_splice($input, 2, null)); +var_dump ($input); +// $input is now array("red", "green") + echo "test removing entries from the middle \n"; $input = array("red", "green", "blue", "yellow"); var_dump (array_splice($input, 1, -1)); @@ -56,6 +62,19 @@ array(2) { [1]=> string(5) "green" } +test truncation with null length +array(2) { + [0]=> + string(4) "blue" + [1]=> + string(6) "yellow" +} +array(2) { + [0]=> + string(3) "red" + [1]=> + string(5) "green" +} test removing entries from the middle array(2) { [0]=>