]> granicus.if.org Git - php/commitdiff
Allow $num=0 in array_fill()
authorNikita Popov <nikic@php.net>
Tue, 12 Nov 2013 20:56:50 +0000 (21:56 +0100)
committerNikita Popov <nikic@php.net>
Tue, 12 Nov 2013 21:02:51 +0000 (22:02 +0100)
Implemented request #49824.

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

diff --git a/NEWS b/NEWS
index fdfaef53cb58f1c3c5c13793558cd98982225243..445e85d18cbd43b9f9d9d10ac7789edf9a6f71ad 100644 (file)
--- 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). 
index 17be59d6ca4b868523375bf220f8d534a8f0e558..d21badc87c0d74d237077eca01ab7461c2d15a51 100644 (file)
@@ -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);
index 1de7c3142000b62f4b9e40f5bb3471f43d539445..c6c7e1e45708f7432b55139a8cdf64ec35b42405 100644 (file)
@@ -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) {
index 167163228d57405e80763c42294bce60b0e42a81..33ee2b3858a1188b3e28827b6f0b0a19abad42f9 100644 (file)
@@ -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
index 9e9df29b0bb139e9a5e11c0be509b6e6884768d9..ecf4ef435ec9d0244302e90c497c029dfb144a7e 100644 (file)
@@ -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