andrei Sun Mar 11 19:06:59 2001 EDT
Modified files:
/php4/ext/pcre php_pcre.c
/php4/ext/standard array.c basic_functions.c php_array.h
Log:
@- Added array_reduce(), which allows iterative reduction of an array
@ to a single value via a callback function. (Andrei)
- Added array_reduce(), which allows iterative reduction of an array
to a single value via a callback function.
- Fixed usage of zend_is_callable() in PCRE.
Index: php4/ext/pcre/php_pcre.c
diff -u php4/ext/pcre/php_pcre.c:1.87 php4/ext/pcre/php_pcre.c:1.88
--- php4/ext/pcre/php_pcre.c:1.87 Sun Feb 25 22:07:11 2001
+++ php4/ext/pcre/php_pcre.c Sun Mar 11 19:06:52 2001
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: php_pcre.c,v 1.87 2001/02/26 06:07:11 andi Exp $ */
+/* $Id: php_pcre.c,v 1.88 2001/03/12 03:06:52 andrei Exp $ */
#include "php.h"
#include "php_globals.h"
@@ -726,8 +726,11 @@
/* Verify and use the replacement value. */
if (use_func) {
- if (!zend_is_callable(replace_val)) {
- php_error(E_WARNING, "Replacement function is invalid or
undefined");
+ char *callable_name;
+
+ if (!zend_is_callable(replace_val, 0, &callable_name)) {
+ php_error(E_WARNING, "Replacement callback '%s' is invalid or
+undefined", callable_name);
+ efree(callable_name);
result = estrndup(subject, subject_len);
*result_len = subject_len;
return result;
@@ -1009,7 +1012,7 @@
if (Z_TYPE_PP(replace) != IS_ARRAY) {
convert_to_string_ex(replace);
} else
- is_callable_replace = zend_is_callable(*replace);
+ is_callable_replace = zend_is_callable(*replace, 1, NULL);
/* if subject is an array */
if (Z_TYPE_PP(subject) == IS_ARRAY) {
Index: php4/ext/standard/array.c
diff -u php4/ext/standard/array.c:1.97 php4/ext/standard/array.c:1.98
--- php4/ext/standard/array.c:1.97 Sun Feb 25 22:07:16 2001
+++ php4/ext/standard/array.c Sun Mar 11 19:06:53 2001
@@ -21,7 +21,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: array.c,v 1.97 2001/02/26 06:07:16 andi Exp $ */
+/* $Id: array.c,v 1.98 2001/03/12 03:06:53 andrei Exp $ */
#include "php.h"
#include "php_ini.h"
@@ -2814,6 +2814,66 @@
}
/* }}} */
+
+/* {{{ proto mixed array_reduce(array input, mixed callback [, int initial])
+ Reduce the array by calling the callback */
+PHP_FUNCTION(array_reduce)
+{
+ zval **input, **callback, **initial;
+ zval **args[2];
+ zval **operand;
+ zval *result = NULL;
+ zval *retval;
+ char *callback_name;
+
+ if (ZEND_NUM_ARGS() < 2 || ZEND_NUM_ARGS() > 3 ||
+ zend_get_parameters_ex(ZEND_NUM_ARGS(), &input, &callback, &initial)
+== FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ if (Z_TYPE_PP(input) != IS_ARRAY) {
+ php_error(E_WARNING, "%s() expects argument 1 to be an array",
+ get_active_function_name());
+ return;
+ }
+
+ if (!zend_is_callable(*callback, 0, &callback_name)) {
+ php_error(E_WARNING, "%s() expects argument 2, '%s', to be a valid
+callback",
+ get_active_function_name(), callback_name);
+ efree(callback_name);
+ return;
+ }
+
+ if (ZEND_NUM_ARGS() > 2)
+ result = *initial;
+
+ if (zend_hash_num_elements(Z_ARRVAL_PP(input)) == 0) {
+ if (result) {
+ *return_value = *result;
+ zval_copy_ctor(return_value);
+ }
+ return;
+ }
+
+ zend_hash_internal_pointer_reset(Z_ARRVAL_PP(input));
+ while (zend_hash_get_current_data(Z_ARRVAL_PP(input), (void **)&operand) ==
+SUCCESS) {
+ if (result) {
+ args[0] = &result;
+ args[1] = operand;
+ if (call_user_function_ex(EG(function_table), NULL, *callback,
+&retval, 2, args, 0, NULL) == SUCCESS && retval) {
+ result = retval;
+ } else {
+ php_error(E_WARNING, "%s() had an error invoking the
+reduction callback", get_active_function_name());
+ return;
+ }
+ } else
+ result = *operand;
+
+ zend_hash_move_forward(Z_ARRVAL_PP(input));
+ }
+
+ *return_value = *result;
+}
/*
* Local variables:
Index: php4/ext/standard/basic_functions.c
diff -u php4/ext/standard/basic_functions.c:1.314
php4/ext/standard/basic_functions.c:1.315
--- php4/ext/standard/basic_functions.c:1.314 Sat Mar 10 19:49:21 2001
+++ php4/ext/standard/basic_functions.c Sun Mar 11 19:06:53 2001
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: basic_functions.c,v 1.314 2001/03/11 03:49:21 sniper Exp $ */
+/* $Id: basic_functions.c,v 1.315 2001/03/12 03:06:53 andrei Exp $ */
#include "php.h"
#include "php_main.h"
@@ -573,6 +573,7 @@
PHP_FE(array_values, NULL)
PHP_FE(array_count_values, NULL)
PHP_FE(array_reverse, NULL)
+ PHP_FE(array_reduce, NULL)
PHP_FE(array_pad,
NULL)
PHP_FE(array_flip,
NULL)
PHP_FE(array_rand,
NULL)
Index: php4/ext/standard/php_array.h
diff -u php4/ext/standard/php_array.h:1.21 php4/ext/standard/php_array.h:1.22
--- php4/ext/standard/php_array.h:1.21 Sun Feb 25 22:07:23 2001
+++ php4/ext/standard/php_array.h Sun Mar 11 19:06:53 2001
@@ -19,7 +19,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: php_array.h,v 1.21 2001/02/26 06:07:23 andi Exp $ */
+/* $Id: php_array.h,v 1.22 2001/03/12 03:06:53 andrei Exp $ */
#ifndef PHP_ARRAY_H
#define PHP_ARRAY_H
@@ -67,6 +67,7 @@
PHP_FUNCTION(array_values);
PHP_FUNCTION(array_count_values);
PHP_FUNCTION(array_reverse);
+PHP_FUNCTION(array_reduce);
PHP_FUNCTION(array_pad);
PHP_FUNCTION(array_flip);
PHP_FUNCTION(array_rand);
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]