[PHP-CVS] cvs: php4 /ext/standard array.c
andrei Mon Apr 2 06:20:17 2001 EDT Modified files: /php4/ext/standard array.c Log: Use correct string length. Index: php4/ext/standard/array.c diff -u php4/ext/standard/array.c:1.106 php4/ext/standard/array.c:1.107 --- php4/ext/standard/array.c:1.106 Mon Mar 19 13:20:02 2001 +++ php4/ext/standard/array.c Mon Apr 2 06:20:16 2001 @@ -21,7 +21,7 @@ +--+ */ -/* $Id: array.c,v 1.106 2001/03/19 21:20:02 andrei Exp $ */ +/* $Id: array.c,v 1.107 2001/04/02 13:20:16 andrei Exp $ */ #include "php.h" #include "php_ini.h" @@ -2202,7 +2202,7 @@ switch (zend_hash_get_current_key_ex(target_hash, string_key, str_key_len, num_key, 1, pos)) { case HASH_KEY_IS_STRING: Z_STRVAL_P(data) = string_key; - Z_STRLEN_P(data) = str_key_len; + Z_STRLEN_P(data) = str_key_len-1; Z_TYPE_P(data) = IS_STRING; break; case HASH_KEY_IS_LONG: -- 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]
[PHP-CVS] cvs: php4 /ext/standard array.c basic_functions.c php_array.h
andrei Mon Mar 19 13:20:03 2001 EDT Modified files: /php4/ext/standard array.c basic_functions.c php_array.h Log: @- Added array_map() function that applies a callback to the elements @ of given arrays and returns the result. It can also be used with a @ null callback to transpose arrays. (Andrei) Index: php4/ext/standard/array.c diff -u php4/ext/standard/array.c:1.105 php4/ext/standard/array.c:1.106 --- php4/ext/standard/array.c:1.105 Fri Mar 16 12:46:33 2001 +++ php4/ext/standard/array.c Mon Mar 19 13:20:02 2001 @@ -21,7 +21,7 @@ +--+ */ -/* $Id: array.c,v 1.105 2001/03/16 20:46:33 andrei Exp $ */ +/* $Id: array.c,v 1.106 2001/03/19 21:20:02 andrei Exp $ */ #include "php.h" #include "php_ini.h" @@ -2941,7 +2941,7 @@ } else zval_ptr_dtor(retval); } else { - php_error(E_WARNING, "%s() had an error invoking the reduction callback", get_active_function_name()); + php_error(E_WARNING, "%s() had an error invoking the +filter callback", get_active_function_name()); return; } } else if (!zend_is_true(*operand)) @@ -2960,6 +2960,117 @@ break; } } +} +/* }}} */ + + +/* {{{ proto array array_map(mixed callback, array input1 [, array input2 ,...]) + Applies the callback to the elements in given arrays. */ +PHP_FUNCTION(array_map) +{ + zval ***args = NULL; + zval ***params; + zval *callback; + zval *result, *null; + char *callback_name; + int i, k, maxlen = 0; + int *array_len; + + if (ZEND_NUM_ARGS() 2) { + WRONG_PARAM_COUNT; + } + + args = (zval ***)emalloc(ZEND_NUM_ARGS() * sizeof(zval **)); + if (zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) { + efree(args); + WRONG_PARAM_COUNT; + } + + callback = *args[0]; + if (Z_TYPE_P(callback) != IS_NULL !zend_is_callable(callback, 0, +callback_name)) { + php_error(E_WARNING, "%s() expects argument 1, '%s', to be either NULL +or a valid callback", + get_active_function_name(), callback_name); + efree(callback_name); + efree(args); + return; + } + + /* Cache array sizes. */ + array_len = (int*)emalloc((ZEND_NUM_ARGS()-1) * sizeof(int)); + + /* Check that arrays are indeed arrays and calculate maximum size. */ + for (i = 0; i ZEND_NUM_ARGS()-1; i++) { + if (Z_TYPE_PP(args[i+1]) != IS_ARRAY) { + php_error(E_WARNING, "%s() expects argument %d to be an array", + get_active_function_name(), i + 2); + efree(array_len); + efree(args); + return; + } + array_len[i] = zend_hash_num_elements(Z_ARRVAL_PP(args[i+1])); + if (array_len[i] maxlen) + maxlen = array_len[i]; + } + + /* Short-circuit: if no callback and only one array, just return it. */ + if (Z_TYPE_P(callback) == IS_NULL ZEND_NUM_ARGS() == 2) { + *return_value = **args[1]; + zval_copy_ctor(return_value); + efree(array_len); + efree(args); + return; + } + + array_init(return_value); + params = (zval ***)emalloc((ZEND_NUM_ARGS()-1) * sizeof(zval **)); + MAKE_STD_ZVAL(null); + ZVAL_NULL(null); + + /* We iterate through all the arrays at once. */ + for (k = 0; k maxlen; k++) { + /* +* If no callback, the result will be an array, consisting of current +* entries from all arrays. +*/ + if (Z_TYPE_P(callback) == IS_NULL) { + MAKE_STD_ZVAL(result); + array_init(result); + } + + for (i = 0; i ZEND_NUM_ARGS()-1; i++) { + /* +* If this array still hash elements, add the current one to +the +* parameter list, otherwise use null value. +*/ + if (k array_len[i]) { + zend_hash_index_find(Z_ARRVAL_PP(args[i+1]), k, (void +**)params[i]); + } else { + if (Z_TYPE_P(callback) == IS_NULL) + zval_add_ref(null); + params[i] = null; + } + + if (Z_TYPE_P(callback) == IS_NULL) +
[PHP-CVS] cvs: php4 /ext/standard array.c
andrei Fri Mar 16 11:29:23 2001 EDT Modified files: /php4/ext/standard array.c Log: @- Fixed all relevant array functions to avoid moving the internal array @ pointer during operations. (Andrei) Index: php4/ext/standard/array.c diff -u php4/ext/standard/array.c:1.101 php4/ext/standard/array.c:1.102 --- php4/ext/standard/array.c:1.101 Mon Mar 12 02:14:00 2001 +++ php4/ext/standard/array.c Fri Mar 16 11:29:23 2001 @@ -21,7 +21,7 @@ +--+ */ -/* $Id: array.c,v 1.101 2001/03/12 10:14:00 stas Exp $ */ +/* $Id: array.c,v 1.102 2001/03/16 19:29:23 andrei Exp $ */ #include "php.h" #include "php_ini.h" @@ -954,9 +954,11 @@ *retval_ptr, /* Return value - unused */ *key; /* Entry key */ char *string_key; + ulong string_key_len; ulong num_key; + HashPosition pos; CLS_FETCH(); - BLS_FETCH(); + ELS_FETCH(); /* Allocate space for key */ MAKE_STD_ZVAL(key); @@ -965,22 +967,22 @@ args[1] = key; args[2] = userdata; - zend_hash_internal_pointer_reset(target_hash); + zend_hash_internal_pointer_reset_ex(target_hash, pos); /* Iterate through hash */ - while(zend_hash_get_current_data(target_hash, (void **)args[0]) == SUCCESS) { + while(zend_hash_get_current_data_ex(target_hash, (void **)args[0], pos) == +SUCCESS) { /* Set up the key */ - if (zend_hash_get_current_key(target_hash, string_key, num_key, 1) == HASH_KEY_IS_LONG) { + if (zend_hash_get_current_key_ex(target_hash, string_key, +string_key_len, num_key, 0, pos) == HASH_KEY_IS_LONG) { Z_TYPE_P(key) = IS_LONG; Z_LVAL_P(key) = num_key; } else { Z_TYPE_P(key) = IS_STRING; Z_STRVAL_P(key) = string_key; - Z_STRLEN_P(key) = strlen(string_key); + Z_STRLEN_P(key) = string_key_len-1; } /* Call the userland function */ - if (call_user_function_ex(CG(function_table), NULL, *BG(array_walk_func_name), + if (call_user_function_ex(EG(function_table), NULL, +*BG(array_walk_func_name), retval_ptr, userdata ? 3 : 2, args, 0, NULL) == SUCCESS) { zval_ptr_dtor(retval_ptr); @@ -988,11 +990,7 @@ php_error(E_WARNING,"Unable to call %s() - function does not exist", (*BG(array_walk_func_name))-value.str.val); - /* Clean up the key */ - if (zend_hash_get_current_key_type(target_hash) == HASH_KEY_IS_STRING) - efree(Z_STRVAL_P(key)); - - zend_hash_move_forward(target_hash); + zend_hash_move_forward_ex(target_hash, pos); } efree(key); @@ -1156,6 +1154,7 @@ char *var_name, *final_name; ulong num_key, var_name_len; int var_exists, extract_type, key_type, count = 0; + HashPosition pos; switch(ZEND_NUM_ARGS()) { case 1: @@ -1204,9 +1203,9 @@ return; } - zend_hash_internal_pointer_reset(Z_ARRVAL_PP(var_array)); - while(zend_hash_get_current_data(Z_ARRVAL_PP(var_array), (void **)entry) == SUCCESS) { - key_type = zend_hash_get_current_key_ex(Z_ARRVAL_PP(var_array), var_name, var_name_len, num_key, 0, NULL); + zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(var_array), pos); + while(zend_hash_get_current_data_ex(Z_ARRVAL_PP(var_array), (void **)entry, +pos) == SUCCESS) { + key_type = zend_hash_get_current_key_ex(Z_ARRVAL_PP(var_array), +var_name, var_name_len, num_key, 0, pos); final_name = NULL; var_exists = 0; @@ -1217,7 +1216,7 @@ final_name = emalloc(MAX_LENGTH_OF_LONG + Z_STRLEN_PP(prefix) + 2); zend_sprintf(final_name, "%s_%ld", Z_STRVAL_PP(prefix), num_key); } else { - zend_hash_move_forward(Z_ARRVAL_PP(var_array)); + zend_hash_move_forward_ex(Z_ARRVAL_PP(var_array), pos); continue; } @@ -1271,7 +1270,7 @@ efree(final_name); } - zend_hash_move_forward(Z_ARRVAL_PP(var_array)); + zend_hash_move_forward_ex(Z_ARRVAL_PP(var_array), pos); } RETURN_LONG(count); @@ -1279,8 +1278,7 @@ /* }}} */ -/* {{{ void _compact_var(HashTable *eg_active_symbol_table, zval *return_value, zval *entry) */
[PHP-CVS] cvs: php4 /ext/standard array.c
fmk Fri Mar 16 11:48:40 2001 EDT Modified files: /php4/ext/standard array.c Log: Fixing build on Win32 by adding missing BLS_FETCH(); Index: php4/ext/standard/array.c diff -u php4/ext/standard/array.c:1.102 php4/ext/standard/array.c:1.103 --- php4/ext/standard/array.c:1.102 Fri Mar 16 11:29:23 2001 +++ php4/ext/standard/array.c Fri Mar 16 11:48:40 2001 @@ -21,7 +21,7 @@ +--+ */ -/* $Id: array.c,v 1.102 2001/03/16 19:29:23 andrei Exp $ */ +/* $Id: array.c,v 1.103 2001/03/16 19:48:40 fmk Exp $ */ #include "php.h" #include "php_ini.h" @@ -957,6 +957,7 @@ ulong string_key_len; ulong num_key; HashPosition pos; + BLS_FETCH(); CLS_FETCH(); ELS_FETCH(); -- 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]
[PHP-CVS] cvs: php4 /ext/standard array.c
andrei Fri Mar 16 11:51:08 2001 EDT Modified files: /php4/ext/standard array.c Log: All user callbacks should be to EG(function_table). Index: php4/ext/standard/array.c diff -u php4/ext/standard/array.c:1.103 php4/ext/standard/array.c:1.104 --- php4/ext/standard/array.c:1.103 Fri Mar 16 11:48:40 2001 +++ php4/ext/standard/array.c Fri Mar 16 11:51:08 2001 @@ -21,7 +21,7 @@ +--+ */ -/* $Id: array.c,v 1.103 2001/03/16 19:48:40 fmk Exp $ */ +/* $Id: array.c,v 1.104 2001/03/16 19:51:08 andrei Exp $ */ #include "php.h" #include "php_ini.h" @@ -521,7 +521,7 @@ Bucket *s; pval **args[2]; pval *retval_ptr; - CLS_FETCH(); + ELS_FETCH(); BLS_FETCH(); f = *((Bucket **) a); @@ -530,7 +530,7 @@ args[0] = (pval **) f-pData; args[1] = (pval **) s-pData; - if (call_user_function_ex(CG(function_table), NULL, *BG(user_compare_func_name), retval_ptr, 2, args, 0, NULL)==SUCCESS + if (call_user_function_ex(EG(function_table), NULL, +*BG(user_compare_func_name), retval_ptr, 2, args, 0, NULL)==SUCCESS retval_ptr) { long retval; @@ -609,7 +609,7 @@ pval *args[2]; pval retval; int status; - CLS_FETCH(); + ELS_FETCH(); BLS_FETCH(); args[0] = key1; @@ -637,7 +637,7 @@ Z_TYPE(key2) = IS_LONG; } - status = call_user_function(CG(function_table), NULL, *BG(user_compare_func_name), retval, 2, args); + status = call_user_function(EG(function_table), NULL, +*BG(user_compare_func_name), retval, 2, args); zval_dtor(key1); zval_dtor(key2); @@ -958,7 +958,6 @@ ulong num_key; HashPosition pos; BLS_FETCH(); - CLS_FETCH(); ELS_FETCH(); /* Allocate space for key */ -- 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]
[PHP-CVS] cvs: php4 /ext/standard array.c basic_functions.c php_array.h
andrei Fri Mar 16 12:46:35 2001 EDT Modified files: /php4/ext/standard array.c basic_functions.c php_array.h Log: @- Added array_filter(), which allows filtering of array elements via @ the specified callback. (Andrei) Index: php4/ext/standard/array.c diff -u php4/ext/standard/array.c:1.104 php4/ext/standard/array.c:1.105 --- php4/ext/standard/array.c:1.104 Fri Mar 16 11:51:08 2001 +++ php4/ext/standard/array.c Fri Mar 16 12:46:33 2001 @@ -21,7 +21,7 @@ +--+ */ -/* $Id: array.c,v 1.104 2001/03/16 19:51:08 andrei Exp $ */ +/* $Id: array.c,v 1.105 2001/03/16 20:46:33 andrei Exp $ */ #include "php.h" #include "php_ini.h" @@ -2887,6 +2887,82 @@ *return_value = *result; } +/* }}} */ + + +/* {{{ proto array array_filter(array input [, mixed callback]) + Filters elements from the array via the callback. */ +PHP_FUNCTION(array_filter) +{ + zval **input, **callback = NULL; + zval **operand; + zval **args[1]; + zval *retval = NULL; + char *callback_name; + char *string_key; + ulong string_key_len; + ulong num_key; + HashPosition pos; + + if (ZEND_NUM_ARGS() 1 || ZEND_NUM_ARGS() 2 || + zend_get_parameters_ex(ZEND_NUM_ARGS(), input, callback) == 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_NUM_ARGS() 1) { + 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; + } + } + + array_init(return_value); + if (zend_hash_num_elements(Z_ARRVAL_PP(input)) == 0) + return; + + for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(input), pos); +zend_hash_get_current_data_ex(Z_ARRVAL_PP(input), (void **)operand, +pos) == SUCCESS; +zend_hash_move_forward_ex(Z_ARRVAL_PP(input), pos)) { + + if (callback) { + args[0] = operand; + if (call_user_function_ex(EG(function_table), NULL, *callback, +retval, 1, args, 0, NULL) == SUCCESS retval) { + if (!zend_is_true(retval)) { + zval_ptr_dtor(retval); + continue; + } else + zval_ptr_dtor(retval); + } else { + php_error(E_WARNING, "%s() had an error invoking the +reduction callback", get_active_function_name()); + return; + } + } else if (!zend_is_true(*operand)) + continue; + + zval_add_ref(operand); + switch (zend_hash_get_current_key_ex(Z_ARRVAL_PP(input), string_key, +string_key_len, num_key, 0, pos)) { + case HASH_KEY_IS_STRING: + zend_hash_update(Z_ARRVAL_P(return_value), string_key, +string_key_len, +operand, sizeof(zval *), NULL); + break; + + case HASH_KEY_IS_LONG: + zend_hash_index_update(Z_ARRVAL_P(return_value), +num_key, + operand, +sizeof(zval *), NULL); + break; + } + } +} +/* }}} */ + /* * Local variables: Index: php4/ext/standard/basic_functions.c diff -u php4/ext/standard/basic_functions.c:1.316 php4/ext/standard/basic_functions.c:1.317 --- php4/ext/standard/basic_functions.c:1.316 Fri Mar 16 10:18:01 2001 +++ php4/ext/standard/basic_functions.c Fri Mar 16 12:46:33 2001 @@ -17,7 +17,7 @@ +--+ */ -/* $Id: basic_functions.c,v 1.316 2001/03/16 18:18:01 sniper Exp $ */ +/* $Id: basic_functions.c,v 1.317 2001/03/16 20:46:33 andrei Exp $ */ #include "php.h" #include "php_main.h" @@ -581,6 +581,7 @@ PHP_FE(array_intersect, NULL) PHP_FE(array_diff, NULL) PHP_FE(array_sum, NULL) + PHP_FE(array_filter,NULL)
[PHP-CVS] cvs: php4 /ext/standard array.c
stasMon Mar 12 02:14:00 2001 EDT Modified files: /php4/ext/standard array.c Log: Prevent memory leak Index: php4/ext/standard/array.c diff -u php4/ext/standard/array.c:1.100 php4/ext/standard/array.c:1.101 --- php4/ext/standard/array.c:1.100 Mon Mar 12 00:07:00 2001 +++ php4/ext/standard/array.c Mon Mar 12 02:14:00 2001 @@ -21,7 +21,7 @@ +--+ */ -/* $Id: array.c,v 1.100 2001/03/12 08:07:00 stas Exp $ */ +/* $Id: array.c,v 1.101 2001/03/12 10:14:00 stas Exp $ */ #include "php.h" #include "php_ini.h" @@ -2209,7 +2209,7 @@ } else if (Z_TYPE_PP(entry) == IS_STRING) { zend_hash_update(Z_ARRVAL_P(return_value),Z_STRVAL_PP(entry),Z_STRLEN_PP(entry) + 1, data, sizeof(data), NULL); } else { - zval_dtor(data); + zval_ptr_dtor(data); /* will free also zval structure */ php_error(E_WARNING, "Can only flip STRING and INTEGER values!"); } -- 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]
[PHP-CVS] cvs: php4 /ext/standard array.c
andrei Sun Mar 11 19:10:15 2001 EDT Modified files: /php4/ext/standard array.c Log: This will have to do until the docs come along. Index: php4/ext/standard/array.c diff -u php4/ext/standard/array.c:1.98 php4/ext/standard/array.c:1.99 --- php4/ext/standard/array.c:1.98 Sun Mar 11 19:06:53 2001 +++ php4/ext/standard/array.c Sun Mar 11 19:10:15 2001 @@ -21,7 +21,7 @@ +--+ */ -/* $Id: array.c,v 1.98 2001/03/12 03:06:53 andrei Exp $ */ +/* $Id: array.c,v 1.99 2001/03/12 03:10:15 andrei Exp $ */ #include "php.h" #include "php_ini.h" @@ -2816,7 +2816,7 @@ /* }}} */ /* {{{ proto mixed array_reduce(array input, mixed callback [, int initial]) - Reduce the array by calling the callback */ + Iteratively reduce the array to a single value via the callback. */ PHP_FUNCTION(array_reduce) { zval **input, **callback, **initial; -- 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]
[PHP-CVS] cvs: php4 /ext/standard array.c
stasMon Mar 12 00:07:00 2001 EDT Modified files: /php4/ext/standard array.c Log: Use hash position with array_flip Index: php4/ext/standard/array.c diff -u php4/ext/standard/array.c:1.99 php4/ext/standard/array.c:1.100 --- php4/ext/standard/array.c:1.99 Sun Mar 11 19:10:15 2001 +++ php4/ext/standard/array.c Mon Mar 12 00:07:00 2001 @@ -21,7 +21,7 @@ +--+ */ -/* $Id: array.c,v 1.99 2001/03/12 03:10:15 andrei Exp $ */ +/* $Id: array.c,v 1.100 2001/03/12 08:07:00 stas Exp $ */ #include "php.h" #include "php_ini.h" @@ -2173,8 +2173,10 @@ zval **array, **entry, *data; HashTable *target_hash; char *string_key; + ulong str_key_len; ulong num_key; - + HashPosition pos; + if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, array) == FAILURE) { WRONG_PARAM_COUNT; } @@ -2187,13 +2189,13 @@ array_init(return_value); - zend_hash_internal_pointer_reset(target_hash); - while (zend_hash_get_current_data(target_hash, (void **)entry) == SUCCESS) { + zend_hash_internal_pointer_reset_ex(target_hash, pos); + while (zend_hash_get_current_data_ex(target_hash, (void **)entry, pos) == +SUCCESS) { MAKE_STD_ZVAL(data); - switch (zend_hash_get_current_key(target_hash, string_key, num_key, 1)) { + switch (zend_hash_get_current_key_ex(target_hash, string_key, +str_key_len, num_key, 1, pos)) { case HASH_KEY_IS_STRING: Z_STRVAL_P(data) = string_key; - Z_STRLEN_P(data) = strlen(string_key); + Z_STRLEN_P(data) = str_key_len; Z_TYPE_P(data) = IS_STRING; break; case HASH_KEY_IS_LONG: @@ -2211,7 +2213,7 @@ php_error(E_WARNING, "Can only flip STRING and INTEGER values!"); } - zend_hash_move_forward(target_hash); + zend_hash_move_forward_ex(target_hash, pos); } } /* }}} */ -- 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]
[PHP-CVS] cvs: php4 /ext/standard array.c
andrei Wed Feb 21 09:22:27 2001 EDT Modified files: /php4/ext/standard array.c Log: Fix memory leak. Index: php4/ext/standard/array.c diff -u php4/ext/standard/array.c:1.94 php4/ext/standard/array.c:1.95 --- php4/ext/standard/array.c:1.94 Tue Feb 20 08:44:57 2001 +++ php4/ext/standard/array.c Wed Feb 21 09:22:26 2001 @@ -21,7 +21,7 @@ +--+ */ -/* $Id: array.c,v 1.94 2001/02/20 16:44:57 jason Exp $ */ +/* $Id: array.c,v 1.95 2001/02/21 17:22:26 andrei Exp $ */ #include "php.h" #include "php_ini.h" @@ -1517,6 +1517,7 @@ stack = *args[0]; if (Z_TYPE_P(stack) != IS_ARRAY) { php_error(E_WARNING, "First argument to array_push() needs to be an array"); + efree(args); RETURN_FALSE; } @@ -1621,6 +1622,7 @@ stack = *args[0]; if (Z_TYPE_P(stack) != IS_ARRAY) { php_error(E_WARNING, "First argument to array_unshift() needs to be an array"); + efree(args); RETURN_FALSE; } -- 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]
[PHP-CVS] cvs: php4 /ext/standard array.c crypt.c php_rand.h rand.c
jmoore Wed Feb 21 16:24:19 2001 EDT Modified files: /php4/ext/standard array.c crypt.c php_rand.h rand.c Log: Adding php_rand() and php_srand(seed) as a wrapper around random, lrand48 and rand. Index: php4/ext/standard/array.c diff -u php4/ext/standard/array.c:1.95 php4/ext/standard/array.c:1.96 --- php4/ext/standard/array.c:1.95 Wed Feb 21 09:22:26 2001 +++ php4/ext/standard/array.c Wed Feb 21 16:24:19 2001 @@ -21,7 +21,7 @@ +--+ */ -/* $Id: array.c,v 1.95 2001/02/21 17:22:26 andrei Exp $ */ +/* $Id: array.c,v 1.96 2001/02/22 00:24:19 jmoore Exp $ */ #include "php.h" #include "php_ini.h" @@ -1364,18 +1364,7 @@ static int array_data_shuffle(const void *a, const void*b) { - return ( - /* This is just a little messy. */ -#ifdef HAVE_RANDOM -random() -#else -#ifdef HAVE_LRAND48 -lrand48() -#else -rand() -#endif -#endif - % 2) ? 1 : -1; + return (php_rand() % 2) ? 1 : -1; } Index: php4/ext/standard/crypt.c diff -u php4/ext/standard/crypt.c:1.35 php4/ext/standard/crypt.c:1.36 --- php4/ext/standard/crypt.c:1.35 Tue Feb 6 08:27:08 2001 +++ php4/ext/standard/crypt.c Wed Feb 21 16:24:19 2001 @@ -17,7 +17,7 @@ | Rasmus Lerdorf [EMAIL PROTECTED] | +--+ */ -/* $Id: crypt.c,v 1.35 2001/02/06 16:27:08 jimjag Exp $ */ +/* $Id: crypt.c,v 1.36 2001/02/22 00:24:19 jmoore Exp $ */ #include stdlib.h #include "php.h" @@ -85,14 +85,10 @@ #define PHP_STD_DES_CRYPT 1 #endif -#if HAVE_RANDOM -#define PHP_CRYPT_RAND random() -#elif HAVE_LRAND48 -#define PHP_CRYPT_RAND lrand48() -#else -#define PHP_CRYPT_RAND rand() -#endif +#define PHP_CRYPT_RAND php_rand() + + PHP_MINIT_FUNCTION(crypt) { #if PHP_STD_DES_CRYPT @@ -105,13 +101,7 @@ REGISTER_LONG_CONSTANT("CRYPT_MD5", PHP_MD5_CRYPT, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("CRYPT_BLOWFISH", PHP_BLOWFISH_CRYPT, CONST_CS | CONST_PERSISTENT); -#if HAVE_SRANDOM - srandom((unsigned int) time(0) * getpid() * (php_combined_lcg() * 1.0)); -#elif HAVE_SRAND48 - srand48((long) time(0) * (long) getpid() * (long) (php_combined_lcg() * 1.0)); -#else - srand((unsigned int) time(0) * getpid() * (php_combined_lcg() * 1.0)); -#endif + php_srand(time(0) * getpid() * (php_combined_lcg() * 1.0)); return SUCCESS; } Index: php4/ext/standard/php_rand.h diff -u php4/ext/standard/php_rand.h:1.6 php4/ext/standard/php_rand.h:1.7 --- php4/ext/standard/php_rand.h:1.6Sun Jul 2 16:46:47 2000 +++ php4/ext/standard/php_rand.hWed Feb 21 16:24:19 2001 @@ -19,7 +19,7 @@ | Based on code from: Shawn Cokus [EMAIL PROTECTED] | +--+ */ -/* $Id: php_rand.h,v 1.6 2000/07/02 23:46:47 sas Exp $ */ +/* $Id: php_rand.h,v 1.7 2001/02/22 00:24:19 jmoore Exp $ */ #ifndef PHP_RAND_H #definePHP_RAND_H @@ -34,6 +34,28 @@ #define PHP_RAND_MAX 2147483647 #else #define PHP_RAND_MAX RAND_MAX +#endif + +/* Define rand Function wrapper */ +#ifdef HAVE_RANDOM +#define php_rand() random() +#else +#ifdef HAVE_LRAND48 +#define php_rand() lrand48() +#else +#define php_rand() rand() +#endif +#endif + +/* Define srand Function wrapper */ +#ifdef HAVE_SRANDOM +#define php_srand(seed) srandom((unsigned int)seed) +#else +#ifdef HAVE_SRAND48 +#define php_srand(seed) srand48((long)seed) +#else +#define php_srand(seed) srand((unsigned int)seed) +#endif #endif #endif /* PHP_RAND_H */ Index: php4/ext/standard/rand.c diff -u php4/ext/standard/rand.c:1.23 php4/ext/standard/rand.c:1.24 --- php4/ext/standard/rand.c:1.23 Mon Feb 19 11:20:47 2001 +++ php4/ext/standard/rand.cWed Feb 21 16:24:19 2001 @@ -19,7 +19,7 @@ | Based on code from: Shawn Cokus [EMAIL PROTECTED] | +--+ */ -/* $Id: rand.c,v 1.23 2001/02/19 19:20:47 derick Exp $ */ +/* $Id: rand.c,v 1.24 2001/02/22 00:24:19 jmoore Exp $ */ #include stdlib.h @@ -199,15 +199,7 @@ WRONG_PARAM_COUNT; } convert_to_long_ex(arg); -#ifdef HAVE_SRANDOM - srandom((unsigned int) (*arg)-value.lval); -#else -#ifdef HAVE_SRAND48 - srand48((unsigned int) (*arg)-value.lval); -#else - srand((unsigned int) (*arg)-value.lval); -#endif -#endif + php_srand((*arg)-value.lval); } /* }}} */ @@ -253,15 +245,9 @@ } return_value-type = IS_LONG; -#ifdef HAVE_RANDOM - return_value-value.lval = random(); -#else -#ifdef HAVE_LRAND48 - return_value-value.lval = lrand48(); -#else - return_value-value.lval = rand(); -#endif -#endif + + return_value-value.lval = php_rand(); + /* * A bit of tricky math
Re: [PHP-CVS] cvs: php4 /ext/standard array.c basic_functions.c php_array.h
On Tue, 20 Feb 2001, Jason Greene wrote: + if (behavior == 0) { + compare_func = is_equal_function; + } else { + /* Lets not return a key unless the values are exact */ + compare_func = is_identical_function; + } Why not? Why should it be different from behavior of in_array? -Andrei "Later in this talk, I intend to define the universe and give three examples." -- Larry Wall -- 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]
[PHP-CVS] cvs: php4 /ext/standard array.c basic_functions.c php_array.h
andrei Tue Feb 20 06:23:03 2001 EDT Modified files: /php4/ext/standard array.c basic_functions.c php_array.h Log: Rename to array_search(). Index: php4/ext/standard/array.c diff -u php4/ext/standard/array.c:1.92 php4/ext/standard/array.c:1.93 --- php4/ext/standard/array.c:1.92 Mon Feb 19 21:36:40 2001 +++ php4/ext/standard/array.c Tue Feb 20 06:23:03 2001 @@ -21,7 +21,7 @@ +--+ */ -/* $Id: array.c,v 1.92 2001/02/20 05:36:40 jason Exp $ */ +/* $Id: array.c,v 1.93 2001/02/20 14:23:03 andrei Exp $ */ #include "php.h" #include "php_ini.h" @@ -1107,12 +1107,11 @@ zend_hash_move_forward_ex(target_hash, pos); } - if (behavior == 0) { - RETURN_FALSE; + if (behavior == 0) { + RETURN_FALSE; } else { return; } - } @@ -1124,9 +1123,9 @@ } /* }}} */ -/* {{{ proto mixed search_array(mixed needle, array haystack [, bool strict]) +/* {{{ proto mixed array_search(mixed needle, array haystack [, bool strict]) Searches the array for a given value and returns the corresponding key if successful */ -PHP_FUNCTION(search_array) +PHP_FUNCTION(array_search) { php_search_array(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); } Index: php4/ext/standard/basic_functions.c diff -u php4/ext/standard/basic_functions.c:1.306 php4/ext/standard/basic_functions.c:1.307 --- php4/ext/standard/basic_functions.c:1.306 Mon Feb 19 21:36:40 2001 +++ php4/ext/standard/basic_functions.c Tue Feb 20 06:23:03 2001 @@ -17,7 +17,7 @@ +--+ */ -/* $Id: basic_functions.c,v 1.306 2001/02/20 05:36:40 jason Exp $ */ +/* $Id: basic_functions.c,v 1.307 2001/02/20 14:23:03 andrei Exp $ */ #include "php.h" #include "php_main.h" @@ -550,7 +550,7 @@ PHP_FE(min, NULL) PHP_FE(max, NULL) PHP_FE(in_array, NULL) - PHP_FE(search_array, NULL) + PHP_FE(array_search,NULL) PHP_FE(extract, NULL) PHP_FE(compact, NULL) PHP_FE(range, NULL) Index: php4/ext/standard/php_array.h diff -u php4/ext/standard/php_array.h:1.19 php4/ext/standard/php_array.h:1.20 --- php4/ext/standard/php_array.h:1.19 Mon Feb 19 21:36:40 2001 +++ php4/ext/standard/php_array.h Tue Feb 20 06:23:03 2001 @@ -19,7 +19,7 @@ +--+ */ -/* $Id: php_array.h,v 1.19 2001/02/20 05:36:40 jason Exp $ */ +/* $Id: php_array.h,v 1.20 2001/02/20 14:23:03 andrei Exp $ */ #ifndef PHP_ARRAY_H #define PHP_ARRAY_H @@ -49,7 +49,7 @@ PHP_FUNCTION(min); PHP_FUNCTION(max); PHP_FUNCTION(in_array); -PHP_FUNCTION(search_array); +PHP_FUNCTION(array_search); PHP_FUNCTION(extract); PHP_FUNCTION(compact); PHP_FUNCTION(range); -- 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]
Re: [PHP-CVS] cvs: php4 /ext/standard array.c basic_functions.c php_array.h
Well, My thoughts on this one were that if you are actually grabbing a key, people are more likely to trust that the returned key does in fact reference the value that was searched. Perhaps defaulting to strict is not a good idea, but to me it seemed logical, and I figured that if anyone else in the group disagreed, we could easily change it. What do you think? -Jason Andrei Zmievski wrote: On Tue, 20 Feb 2001, Jason Greene wrote: + if (behavior == 0) { + compare_func = is_equal_function; + } else { + /* Lets not return a key unless the values are exact */ + compare_func = is_identical_function; + } Why not? Why should it be different from behavior of in_array? -Andrei "Later in this talk, I intend to define the universe and give three examples." -- Larry Wall -- 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]
Re: [PHP-CVS] cvs: php4 /ext/standard array.c basic_functions.c php_array.h
On Tue, 20 Feb 2001, Jason Greene wrote: Well, My thoughts on this one were that if you are actually grabbing a key, people are more likely to trust that the returned key does in fact reference the value that was searched. Perhaps defaulting to strict is not a good idea, but to me it seemed logical, and I figured that if anyone else in the group disagreed, we could easily change it. What do you think? I think the default behavior between in_array() and array_search() should be the same. -Andrei Linux is like living in a teepee. No Windows, no Gates, Apache in house. - Usenet signature -- 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]
[PHP-CVS] cvs: php4 /ext/standard array.c basic_functions.c php_array.h
jason Mon Feb 19 21:36:40 2001 EDT Modified files: /php4/ext/standard array.c basic_functions.c php_array.h Log: Moved the core of in_array into the function php_search_array, which is called by in_array and search_array (new) @ Added search_array which works similar to in_array but returns @ the key instead of a boolean. ([EMAIL PROTECTED]) Index: php4/ext/standard/array.c diff -u php4/ext/standard/array.c:1.91 php4/ext/standard/array.c:1.92 --- php4/ext/standard/array.c:1.91 Tue Feb 6 08:27:08 2001 +++ php4/ext/standard/array.c Mon Feb 19 21:36:40 2001 @@ -17,10 +17,11 @@ | Rasmus Lerdorf [EMAIL PROTECTED] | | Andrei Zmievski [EMAIL PROTECTED] | | Stig Venaas [EMAIL PROTECTED]| + | Jason Greene [EMAIL PROTECTED]| +--+ */ -/* $Id: array.c,v 1.91 2001/02/06 16:27:08 jimjag Exp $ */ +/* $Id: array.c,v 1.92 2001/02/20 05:36:40 jason Exp $ */ #include "php.h" #include "php_ini.h" @@ -1035,18 +1036,29 @@ } /* }}} */ -/* {{{ proto bool in_array(mixed needle, array haystack [, bool strict]) - Checks if the given value exists in the array */ -PHP_FUNCTION(in_array) +/* void php_search_array(INTERNAL_FUNCTION_PARAMETERS, int behavior) + * 0 = return boolean + * 1 = return key + */ +static void php_search_array(INTERNAL_FUNCTION_PARAMETERS, int behavior) { - zval **value, /* value to check for */ + zval **value, /* value to check for */ **array, /* array to check in */ **strict, /* strict comparison or not */ **entry, /* pointer to array entry */ res; /* comparison result */ HashTable *target_hash; /* array hashtable */ HashPosition pos; /* hash iterator */ - int (*compare_func)(zval *, zval *, zval *) = is_equal_function; + ulong num_key; + char *string_key; + int (*compare_func)(zval *, zval *, zval *); + + if (behavior == 0) { + compare_func = is_equal_function; + } else { + /* Lets not return a key unless the values are exact */ + compare_func = is_identical_function; + } if (ZEND_NUM_ARGS() 2 || ZEND_NUM_ARGS() 3 || zend_get_parameters_ex(ZEND_NUM_ARGS(), value, array, strict) == FAILURE) { @@ -1054,19 +1066,22 @@ } if (Z_TYPE_PP(value) == IS_ARRAY || Z_TYPE_PP(value) == IS_OBJECT) { - php_error(E_WARNING, "Wrong datatype for first argument in call to in_array()"); + php_error(E_WARNING, "Wrong datatype for first argument in call to +%s", get_active_function_name()); RETURN_FALSE; } if (Z_TYPE_PP(array) != IS_ARRAY) { - php_error(E_WARNING, "Wrong datatype for second argument in call to in_array()"); + php_error(E_WARNING, "Wrong datatype for second argument in call to +%s", get_active_function_name()); RETURN_FALSE; } if (ZEND_NUM_ARGS() == 3) { convert_to_boolean_ex(strict); - if (Z_LVAL_PP(strict) == 1) + if (Z_LVAL_PP(strict)) { compare_func = is_identical_function; + } else { + compare_func = is_equal_function; + } } target_hash = HASH_OF(*array); @@ -1074,13 +1089,46 @@ while(zend_hash_get_current_data_ex(target_hash, (void **)entry, pos) == SUCCESS) { compare_func(res, *value, *entry); if (Z_LVAL(res) == 1) { - RETURN_TRUE; + if (behavior==0) { + RETURN_TRUE; + } else { + /* Return current key */ + switch (zend_hash_get_current_key_ex(target_hash, +string_key, NULL, num_key, 1, pos)) { + case HASH_KEY_IS_STRING: + RETVAL_STRING(string_key, 0); + break; + case HASH_KEY_IS_LONG: + RETVAL_LONG(num_key); + break; + } + } } zend_hash_move_forward_ex(target_hash, pos); } - - RETURN_FALSE; + + if (behavior ==