com php-src: Allow array_splice() length to be nu ll: UPGRADING ext/standard/array.c ext/standard/basic _functions.stub.php
ext/standard/basic_functions_ar ginfo.h ext/standard/tests/array/array_splice_basic.p hpt
| From: | Nikita Popov | Date: | Wed, 23 Oct 2019 09:22:12 +0000 |
| Subject: | com php-src: Allow array_splice() length to be nu ll: UPGRADING ext/standard/array.c ext/standard/basic _functions.stub.php ext/standard/basic_functions_ar ginfo.h ext/standard/tests/array/array_splice_basic.p hpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-109024@lists.php.net to get a copy of this message | ||
Commit: e7335eb420e40d2226fbf1c2b573875323667560
Author: Colin O'Dell <colinodell@gmail.com> Mon, 21 Oct 2019 17:22:04 -0400
Committer: Nikita Popov <nikita.ppv@gmail.com> Wed, 23 Oct 2019 11:22:12 +0200
Parents: 80103bdb78544495d6a34cbd75fac25492832a95
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=e7335eb420e40d2226fbf1c2b573875323667560
Log:
Allow array_splice() length to be null
Changed paths:
M UPGRADING
M ext/standard/array.c
M ext/standard/basic_functions.stub.php
M ext/standard/basic_functions_arginfo.h
M ext/standard/tests/array/array_splice_basic.phpt
Diff:
diff --git a/UPGRADING b/UPGRADING
index 40af9ca4588..41cab54f1e5 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 d3aac515a7d..8fe281da264 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 78bc728d2af..cacaa7ba3ad 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 56b0f116c49..40bac9cc7ee 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 9a96ccb362a..cf6fcd1db25 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]=>