cvs: php4 /ext/standard array.c basic_functions.c php_array.h

From: Date: Tue, 20 Feb 2001 05:36:40 +0000
Subject: cvs: php4 /ext/standard array.c basic_functions.c php_array.h
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-4132@lists.php.net to get a copy of this message
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. (jason@php.net) 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 <rasmus@php.net> | | Andrei Zmievski <andrei@ispi.net> | | Stig Venaas <venaas@php.net> | + | Jason Greene <jason@php.net> | +----------------------------------------------------------------------+ */ -/* $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 == 0) { + RETURN_FALSE; + } else { + return; + } + +} + + +/* {{{ proto bool in_array(mixed needle, array haystack [, bool strict]) + Checks if the given value exists in the array */ +PHP_FUNCTION(in_array) +{ + php_search_array(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); +} +/* }}} */ + +/* {{{ proto mixed search_array(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_search_array(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); } /* }}} */ Index: php4/ext/standard/basic_functions.c diff -u php4/ext/standard/basic_functions.c:1.305 php4/ext/standard/basic_functions.c:1.306 --- php4/ext/standard/basic_functions.c:1.305 Wed Feb 14 21:21:27 2001 +++ php4/ext/standard/basic_functions.c Mon Feb 19 21:36:40 2001 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.c,v 1.305 2001/02/15 05:21:27 sas Exp $ */ +/* $Id: basic_functions.c,v 1.306 2001/02/20 05:36:40 jason Exp $ */ #include "php.h" #include "php_main.h" @@ -550,6 +550,7 @@ PHP_FE(min, NULL) PHP_FE(max, NULL) PHP_FE(in_array, NULL) + PHP_FE(search_array, 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.18 php4/ext/standard/php_array.h:1.19 --- php4/ext/standard/php_array.h:1.18 Sun Dec 10 21:36:24 2000 +++ php4/ext/standard/php_array.h Mon Feb 19 21:36:40 2001 @@ -19,7 +19,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_array.h,v 1.18 2000/12/11 05:36:24 andrei Exp $ */ +/* $Id: php_array.h,v 1.19 2001/02/20 05:36:40 jason Exp $ */ #ifndef PHP_ARRAY_H #define PHP_ARRAY_H @@ -49,6 +49,7 @@ PHP_FUNCTION(min); PHP_FUNCTION(max); PHP_FUNCTION(in_array); +PHP_FUNCTION(search_array); PHP_FUNCTION(extract); PHP_FUNCTION(compact); PHP_FUNCTION(range);

« previous php.cvs (#4132) next »