com php-src: Allow $num=0 in array_fill(): NEWS ext/standard/array.c ext/standard/tests/array/array _fill.phpt
ext/standard/tests/array/array_fill_error .phpt ext/standard/tests/array/array_fill_variation2 .phpt
| From: | Nikita Popov | Date: | Tue, 12 Nov 2013 21:02:51 +0000 |
| Subject: | com php-src: Allow $num=0 in array_fill(): NEWS ext/standard/array.c ext/standard/tests/array/array _fill.phpt ext/standard/tests/array/array_fill_error .phpt ext/standard/tests/array/array_fill_variation2 .phpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-73944@lists.php.net to get a copy of this message | ||
Commit: dc4bf922d9a0d738b621dff4a55abb715edeee61
Author: Nikita Popov <nikic@php.net> Tue, 12 Nov 2013 21:56:50 +0100
Parents: 1205a6c7dc1e5123279e1cbb946dfbc538d8a976
Branches: PHP-5.6 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=dc4bf922d9a0d738b621dff4a55abb715edeee61
Log:
Allow $num=0 in array_fill()
Implemented request #49824.
Bugs:
https://bugs.php.net/49824
Changed paths:
M NEWS
M ext/standard/array.c
M ext/standard/tests/array/array_fill.phpt
M ext/standard/tests/array/array_fill_error.phpt
M ext/standard/tests/array/array_fill_variation2.phpt
Diff:
diff --git a/NEWS b/NEWS
index fdfaef5..445e85d 100644
--- a/NEWS
+++ b/NEWS
@@ -67,6 +67,8 @@ PHP NEWS
1.1). (Adam)
. Implemented Change crypt() behavior w/o salt RFC. (Yasuo)
https://wiki.php.net/rfc/crypt_function_salt
+ . Implemented request #49824 (Change array_fill() to allow creating empty
+ array). (Nikita)
- XMLReader:
. Fixed bug #55285 (XMLReader::getAttribute/No/Ns methods inconsistency).
diff --git a/ext/standard/array.c b/ext/standard/array.c
index 17be59d..d21badc 100644
--- a/ext/standard/array.c
+++ b/ext/standard/array.c
@@ -1521,14 +1521,18 @@ PHP_FUNCTION(array_fill)
return;
}
- if (num < 1) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number of elements must be positive");
+ if (num < 0) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number of elements can't be
negative");
RETURN_FALSE;
}
/* allocate an array for return */
array_init_size(return_value, num);
+ if (num == 0) {
+ return;
+ }
+
num--;
zend_hash_index_update(Z_ARRVAL_P(return_value), start_key, &val, sizeof(zval *), NULL);
zval_add_ref(&val);
diff --git a/ext/standard/tests/array/array_fill.phpt b/ext/standard/tests/array/array_fill.phpt
index 1de7c31..c6c7e1e 100644
--- a/ext/standard/tests/array/array_fill.phpt
+++ b/ext/standard/tests/array/array_fill.phpt
@@ -23,34 +23,28 @@ echo '== Done ==';
--EXPECTF--
===========================
start: 0 num: 0 value: 1
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
===========================
start: 0 num: 0 value:
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
===========================
start: 0 num: 0 value:
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
===========================
start: 0 num: 0 value: d
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
===========================
start: 0 num: 0 value: e
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
===========================
start: 0 num: 0 value: f
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
===========================
start: 0 num: 1 value: 1
array(1) {
@@ -137,34 +131,28 @@ array(2) {
}
===========================
start: 1 num: 0 value: 1
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
===========================
start: 1 num: 0 value:
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
===========================
start: 1 num: 0 value:
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
===========================
start: 1 num: 0 value: d
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
===========================
start: 1 num: 0 value: e
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
===========================
start: 1 num: 0 value: f
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
===========================
start: 1 num: 1 value: 1
array(1) {
@@ -251,34 +239,28 @@ array(2) {
}
===========================
start: 2.5 num: 0 value: 1
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
===========================
start: 2.5 num: 0 value:
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
===========================
start: 2.5 num: 0 value:
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
===========================
start: 2.5 num: 0 value: d
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
===========================
start: 2.5 num: 0 value: e
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
===========================
start: 2.5 num: 0 value: f
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
===========================
start: 2.5 num: 1 value: 1
array(1) {
diff --git a/ext/standard/tests/array/array_fill_error.phpt
b/ext/standard/tests/array/array_fill_error.phpt
index 1671632..33ee2b3 100644
--- a/ext/standard/tests/array/array_fill_error.phpt
+++ b/ext/standard/tests/array/array_fill_error.phpt
@@ -32,10 +32,6 @@ var_dump( array_fill($start_key,$num) );
$num = -1;
var_dump( array_fill($start_key,$num,$val) );
-//callin array_fill with 'num' equal to zero value
-$num = 0;
-var_dump( array_fill($start_key,$num,$val) );
-
echo "Done";
?>
--EXPECTF--
@@ -53,9 +49,6 @@ NULL
Warning: array_fill() expects exactly 3 parameters, 2 given in %s on line %d
NULL
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
+Warning: array_fill(): Number of elements can't be negative in %s on line %d
bool(false)
Done
diff --git a/ext/standard/tests/array/array_fill_variation2.phpt
b/ext/standard/tests/array/array_fill_variation2.phpt
index 9e9df29..ecf4ef4 100644
--- a/ext/standard/tests/array/array_fill_variation2.phpt
+++ b/ext/standard/tests/array/array_fill_variation2.phpt
@@ -106,7 +106,7 @@ array(2) {
}
-- Iteration 2 --
-Warning: array_fill(): Number of elements must be positive in %s on line %d
+Warning: array_fill(): Number of elements can't be negative in %s on line %d
bool(false)
-- Iteration 3 --
array(5) {
@@ -122,13 +122,11 @@ array(5) {
int(100)
}
-- Iteration 4 --
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
-- Iteration 5 --
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
-- Iteration 6 --
Warning: array_fill() expects parameter 2 to be long, array given in %s on line %d
@@ -150,31 +148,27 @@ NULL
Warning: array_fill() expects parameter 2 to be long, array given in %s on line %d
NULL
-- Iteration 11 --
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
-- Iteration 12 --
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
-- Iteration 13 --
array(1) {
[0]=>
int(100)
}
-- Iteration 14 --
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
-- Iteration 15 --
array(1) {
[0]=>
int(100)
}
-- Iteration 16 --
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
-- Iteration 17 --
Warning: array_fill() expects parameter 2 to be long, string given in %s on line %d
@@ -196,11 +190,9 @@ NULL
Warning: array_fill() expects parameter 2 to be long, object given in %s on line %d
NULL
-- Iteration 22 --
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
-- Iteration 23 --
-
-Warning: array_fill(): Number of elements must be positive in %s on line %d
-bool(false)
+array(0) {
+}
Done