Skip to content

Commit 0476d55

Browse files
committed
Make array_slice $length param a nullable integer
Instead of having custom parameter handling.
1 parent 623911f commit 0476d55

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed

ext/standard/array.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3470,33 +3470,31 @@ PHP_FUNCTION(array_splice)
34703470
Returns elements specified by offset and length */
34713471
PHP_FUNCTION(array_slice)
34723472
{
3473-
zval *input, /* Input array */
3474-
*z_length = NULL, /* How many elements to get */
3475-
*entry; /* An array entry */
3476-
zend_long offset, /* Offset to get elements from */
3477-
length = 0;
3478-
zend_bool preserve_keys = 0; /* Whether to preserve keys while copying to the new array or not */
3479-
int num_in, /* Number of elements in the input array */
3480-
pos; /* Current position in the array */
3473+
zval *input; /* Input array */
3474+
zval *entry; /* An array entry */
3475+
zend_long offset; /* Offset to get elements from */
3476+
zend_long length = 0; /* How many elements to get */
3477+
zend_bool length_is_null = 1; /* Whether an explicit length has been omitted */
3478+
zend_bool preserve_keys = 0; /* Whether to preserve keys while copying to the new array */
3479+
uint32_t num_in; /* Number of elements in the input array */
3480+
uint32_t pos; /* Current position in the array */
34813481
zend_string *string_key;
34823482
zend_ulong num_key;
34833483

34843484
ZEND_PARSE_PARAMETERS_START(2, 4)
34853485
Z_PARAM_ARRAY(input)
34863486
Z_PARAM_LONG(offset)
34873487
Z_PARAM_OPTIONAL
3488-
Z_PARAM_ZVAL(z_length)
3488+
Z_PARAM_LONG_EX(length, length_is_null, 1, 0)
34893489
Z_PARAM_BOOL(preserve_keys)
34903490
ZEND_PARSE_PARAMETERS_END();
34913491

34923492
/* Get number of entries in the input hash */
34933493
num_in = zend_hash_num_elements(Z_ARRVAL_P(input));
34943494

34953495
/* We want all entries from offset to the end if length is not passed or is null */
3496-
if (ZEND_NUM_ARGS() < 3 || Z_TYPE_P(z_length) == IS_NULL) {
3496+
if (length_is_null) {
34973497
length = num_in;
3498-
} else {
3499-
length = zval_get_long(z_length);
35003498
}
35013499

35023500
/* Clamp the offset.. */

ext/standard/tests/array/array_slice_variation1.phpt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var_dump(array_slice(range(1, 3), 0, $a));
2020
var_dump($a);
2121

2222
?>
23-
--EXPECT--
23+
--EXPECTF--
2424
array(3) {
2525
[0]=>
2626
int(1)
@@ -53,8 +53,10 @@ array(1) {
5353
[2]=>
5454
int(3)
5555
}
56-
array(0) {
57-
}
58-
array(0) {
59-
}
56+
57+
Warning: array_slice() expects parameter 3 to be int, string given in %s on line %d
58+
NULL
59+
60+
Warning: array_slice() expects parameter 3 to be int, string given in %s on line %d
61+
NULL
6062
string(3) "foo"

ext/standard/tests/array/array_slice_variation3.phpt

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ foreach($inputs as $input) {
8181

8282
echo "Done";
8383
?>
84-
--EXPECT--
84+
--EXPECTF--
8585
*** Testing array_slice() : usage variations ***
8686

8787
-- Iteration 1 --
@@ -171,28 +171,34 @@ array(0) {
171171
}
172172

173173
-- Iteration 16 --
174-
array(0) {
175-
}
174+
175+
Warning: array_slice() expects parameter 3 to be int, string given in %s on line %d
176+
NULL
176177

177178
-- Iteration 17 --
178-
array(0) {
179-
}
179+
180+
Warning: array_slice() expects parameter 3 to be int, string given in %s on line %d
181+
NULL
180182

181183
-- Iteration 18 --
182-
array(0) {
183-
}
184+
185+
Warning: array_slice() expects parameter 3 to be int, array given in %s on line %d
186+
NULL
184187

185188
-- Iteration 19 --
186-
array(0) {
187-
}
189+
190+
Warning: array_slice() expects parameter 3 to be int, string given in %s on line %d
191+
NULL
188192

189193
-- Iteration 20 --
190-
array(0) {
191-
}
194+
195+
Warning: array_slice() expects parameter 3 to be int, string given in %s on line %d
196+
NULL
192197

193198
-- Iteration 21 --
194-
array(0) {
195-
}
199+
200+
Warning: array_slice() expects parameter 3 to be int, string given in %s on line %d
201+
NULL
196202

197203
-- Iteration 22 --
198204
array(2) {

0 commit comments

Comments
 (0)