]> granicus.if.org Git - php/commitdiff
MFH: Allow mixed $initial (patch by Christian Seiler)
authorLars Strojny <lstrojny@php.net>
Wed, 20 Aug 2008 20:35:40 +0000 (20:35 +0000)
committerLars Strojny <lstrojny@php.net>
Wed, 20 Aug 2008 20:35:40 +0000 (20:35 +0000)
NEWS
ext/standard/array.c
ext/standard/tests/array/array_reduce.phpt [new file with mode: 0644]
ext/standard/url.c
ext/standard/var.c

diff --git a/NEWS b/NEWS
index 628931682f19c3317e59fefef67bf82ef27d26d0..3dbde4dcc07853e3c9bd444e61d201c8bfdd1284 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,7 @@ PHP                                                                        NEWS
 - Changed session_start() to return false when session startup fails. (Jani)
 - Changed property_exists() to check the existence of a property independent of
   accessibility (like method_exists()). (Felipe)
+- Changed array_reduce() to allow mixed $initial (Christian Seiler)
 
 - Added system independent realpath() implementation which caches intermediate
   directories in realpath-cache. (Dmitry)
index a7ab6e167dd1ff06c2f47bf0d5766c82cceca59a..ca37d0bc4dc7fd13cfd406c153a4465af01cdd79 100644 (file)
@@ -4067,7 +4067,7 @@ PHP_FUNCTION(array_product)
 }
 /* }}} */
 
-/* {{{ proto mixed array_reduce(array input, mixed callback [, int initial])
+/* {{{ proto mixed array_reduce(array input, mixed callback [, mixed initial])
    Iteratively reduce the array to a single value via the callback. */
 PHP_FUNCTION(array_reduce)
 {
@@ -4078,18 +4078,19 @@ PHP_FUNCTION(array_reduce)
        zval *retval;
        zend_fcall_info fci;
        zend_fcall_info_cache fci_cache = empty_fcall_info_cache;
-       long initial;
+       zval *initial;
        HashPosition pos;
        HashTable *htbl;
 
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "af|l", &input, &fci, &fci_cache, &initial) == FAILURE) {
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "af|z", &input, &fci, &fci_cache, &initial) == FAILURE) {
                return;
        }
 
        if (ZEND_NUM_ARGS() > 2) {
                ALLOC_ZVAL(result);
+               *result = *initial;
+               zval_copy_ctor(result);
                INIT_PZVAL(result);
-               ZVAL_LONG(result, initial);
        } else {
                MAKE_STD_ZVAL(result);
                ZVAL_NULL(result);
diff --git a/ext/standard/tests/array/array_reduce.phpt b/ext/standard/tests/array/array_reduce.phpt
new file mode 100644 (file)
index 0000000..94ecca6
--- /dev/null
@@ -0,0 +1,79 @@
+--TEST--
+Test array_reduce() function
+--INI--
+precision=14
+--FILE--
+<?php
+/* Prototype: array array_reduce(array $array, mixed $callback, mixed $initial);
+   Description: Iteratively reduce the array to a single value via the callback
+*/
+
+$array = array('foo', 'foo', 'bar', 'qux', 'qux', 'quux');
+
+echo "\n*** Testing array_reduce() to integer ***\n";
+function reduce_int($w, $v) { return $w + strlen($v); }
+$initial = 42;
+var_dump(array_reduce($array, 'reduce_int', $initial), $initial);
+
+echo "\n*** Testing array_reduce() to float ***\n";
+function reduce_float($w, $v) { return $w + strlen($v) / 10; }
+$initial = 4.2;
+var_dump(array_reduce($array, 'reduce_float', $initial), $initial);
+
+echo "\n*** Testing array_reduce() to string ***\n";
+function reduce_string($w, $v) { return $w . $v; }
+$initial = 'quux';
+var_dump(array_reduce($array, 'reduce_string', $initial), $initial);
+
+echo "\n*** Testing array_reduce() to array ***\n";
+function reduce_array($w, $v) { $w[$v]++; return $w; }
+$initial = array('foo' => 42, 'bar' => 17, 'qux' => -2, 'quux' => 0);
+var_dump(array_reduce($array, 'reduce_array', $initial), $initial);
+
+echo "\n*** Testing array_reduce() to null ***\n";
+function reduce_null($w, $v) { return $w . $v; }
+$initial = null;
+var_dump(array_reduce($array, 'reduce_null', $initial), $initial);
+
+echo "\nDone";
+?> 
+--EXPECTF--
+*** Testing array_reduce() to integer ***
+int(61)
+int(42)
+
+*** Testing array_reduce() to float ***
+float(6.1)
+float(4.2)
+
+*** Testing array_reduce() to string ***
+string(23) "quuxfoofoobarquxquxquux"
+string(4) "quux"
+
+*** Testing array_reduce() to array ***
+array(4) {
+  ["foo"]=>
+  int(44)
+  ["bar"]=>
+  int(18)
+  ["qux"]=>
+  int(0)
+  ["quux"]=>
+  int(1)
+}
+array(4) {
+  ["foo"]=>
+  int(42)
+  ["bar"]=>
+  int(17)
+  ["qux"]=>
+  int(-2)
+  ["quux"]=>
+  int(0)
+}
+
+*** Testing array_reduce() to null ***
+string(19) "foofoobarquxquxquux"
+NULL
+
+Done
index 9dc8a2fe8b5eadae28d046add118c2a7a8a7832c..7d354d4c80bfb8541a6c607053c809d7038b0370 100644 (file)
@@ -65,21 +65,23 @@ PHPAPI char *php_replace_controlchars_ex(char *str, int len)
 {
        unsigned char *s = (unsigned char *)str;
        unsigned char *e = (unsigned char *)str + len;
-       
+       unsigned char *r[len + 1];
+       unsigned int c = 0;
+
        if (!str) {
-               return (NULL);
+               return NULL;
        }
-       
+
        while (s < e) {
-           
-               if (iscntrl(*s)) {
-                       *s='_';
-               }       
+               if (0 == iscntrl(*s)) {
+                       r[c] = *s;
+                       c++;
+               }
                s++;
        }
-       
-       return (str);
-} 
+
+       return (unsigned char *)r;
+}
 /* }}} */
 
 PHPAPI char *php_replace_controlchars(char *str)
index 6f4fe053618cb684b89c3085b3845c6a4974877f..f1823b8aa6b7da78e7191aab2402b2f23246e4a5 100644 (file)
@@ -422,7 +422,7 @@ PHPAPI void php_var_export(zval **struc, int level TSRMLS_DC) /* {{{ */
                        php_printf("\n%*c", level - 1, ' ');
                }
                Z_OBJ_HANDLER(**struc, get_class_name)(*struc, &class_name, &class_name_len, 0 TSRMLS_CC);
-               php_printf ("%s::__set_state(array(\n", class_name);
+               php_printf ("%s::__setState(array(\n", class_name);
                efree(class_name);
                if (myht) {
                        zend_hash_apply_with_arguments(myht TSRMLS_CC, (apply_func_args_t) php_object_element_export, 1, level);