From: Andi Gutmans Date: Mon, 20 Sep 1999 16:56:09 +0000 (+0000) Subject: - Add some internal functions to Zend X-Git-Tag: before-sapi-split~110 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=06a18f169b56936be63b45934d722c2bd2134d47;p=php - Add some internal functions to Zend --- diff --git a/Zend/zend_API.h b/Zend/zend_API.h index a6570be2a7..9e65b1d80d 100644 --- a/Zend/zend_API.h +++ b/Zend/zend_API.h @@ -261,6 +261,8 @@ ZEND_API int add_property_stringl(zval *arg, char *key, char *str, uint length, #define ZEND_SET_GLOBAL_VAR_WITH_LENGTH(name, name_length, var) \ ZEND_SET_SYMBOL_WITH_LENGTH(&EG(symbol_table), name, name_length, var) +#define HASH_OF(p) ((p)->type==IS_ARRAY ? (p)->value.ht : (((p)->type==IS_OBJECT ? (p)->value.obj.properties : NULL))) + #endif /* _ZEND_API_H */ /* diff --git a/Zend/zend_builtin_functions.c b/Zend/zend_builtin_functions.c index e5c7dbbbf9..00cf9b901c 100644 --- a/Zend/zend_builtin_functions.c +++ b/Zend/zend_builtin_functions.c @@ -27,11 +27,21 @@ static ZEND_FUNCTION(zend_version); static ZEND_FUNCTION(zend_num_args); static ZEND_FUNCTION(zend_get_arg); +static ZEND_FUNCTION(strlen); +static ZEND_FUNCTION(strcmp); +static ZEND_FUNCTION(strcasecmp); +static ZEND_FUNCTION(each); +static ZEND_FUNCTION(error_reporting); static zend_function_entry builtin_functions[] = { ZEND_FE(zend_version, NULL) ZEND_FE(zend_num_args, NULL) ZEND_FE(zend_get_arg, NULL) + ZEND_FE(strlen, NULL) + ZEND_FE(strcmp, NULL) + ZEND_FE(strcasecmp, NULL) + ZEND_FE(each, NULL) + ZEND_FE(error_reporting, NULL) { NULL, NULL, NULL } }; @@ -101,3 +111,122 @@ ZEND_FUNCTION(zend_get_arg) zval_copy_ctor(return_value); } +/* {{{ proto int strlen(string str) + Get string length */ +ZEND_FUNCTION(strlen) +{ + zval **str; + + if (ARG_COUNT(ht) != 1 || getParametersEx(1, &str) == FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_string_ex(str); + RETVAL_LONG((*str)->value.str.len); +} +/* }}} */ + +/* {{{ proto int strcmp(string str1, string str2) + Binary safe string comparison */ +ZEND_FUNCTION(strcmp) +{ + zval *s1,*s2; + + if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &s1, &s2) == FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_string(s1); + convert_to_string(s2); + RETURN_LONG(zend_binary_strcmp(s1,s2)); +} +/* }}} */ + +/* {{{ proto int strcasecmp(string str1, string str2) + Binary safe case-insensitive string comparison */ +ZEND_FUNCTION(strcasecmp) +{ + zval *s1,*s2; + + if (ARG_COUNT(ht)!=2 || getParameters(ht, 2, &s1, &s2) == FAILURE) { + WRONG_PARAM_COUNT; + } + convert_to_string(s1); + convert_to_string(s2); + RETURN_LONG(zend_binary_strcasecmp(s1, s2)); +} +/* }}} */ + +ZEND_FUNCTION(each) +{ + zval *array,*entry,**entry_ptr, *tmp; + char *string_key; + ulong num_key; + zval **inserted_pointer; + HashTable *target_hash; + + if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &array) == FAILURE) { + WRONG_PARAM_COUNT; + } + target_hash = HASH_OF(array); + if (!target_hash) { + zend_error(E_WARNING,"Variable passed to each() is not an array or object"); + return; + } + if (zend_hash_get_current_data(target_hash, (void **) &entry_ptr)==FAILURE) { + RETURN_FALSE; + } + array_init(return_value); + entry = *entry_ptr; + + /* add value elements */ + if (entry->EA.is_ref) { + tmp = (zval *)emalloc(sizeof(zval)); + *tmp = *entry; + zval_copy_ctor(tmp); + tmp->EA.is_ref=0; + tmp->EA.locks = 0; + tmp->refcount=0; + entry=tmp; + } + zend_hash_index_update(return_value->value.ht, 1, &entry, sizeof(zval *), NULL); + entry->refcount++; + zend_hash_update_ptr(return_value->value.ht, "value", sizeof("value"), entry, sizeof(zval *), NULL); + entry->refcount++; + + /* add the key elements */ + switch (zend_hash_get_current_key(target_hash, &string_key, &num_key)) { + case HASH_KEY_IS_STRING: + add_get_index_string(return_value,0,string_key,(void **) &inserted_pointer,0); + break; + case HASH_KEY_IS_LONG: + add_get_index_long(return_value,0,num_key, (void **) &inserted_pointer); + break; + } + zend_hash_update(return_value->value.ht, "key", sizeof("key"), inserted_pointer, sizeof(zval *), NULL); + (*inserted_pointer)->refcount++; + zend_hash_move_forward(target_hash); +} + +ZEND_FUNCTION(error_reporting) +{ + zval *arg; + int old_error_reporting; + ELS_FETCH(); + + old_error_reporting = EG(error_reporting); + switch (ARG_COUNT(ht)) { + case 0: + break; + case 1: + if (getParameters(ht,1,&arg) == FAILURE) { + RETURN_FALSE; + } + convert_to_long(arg); + EG(error_reporting)=arg->value.lval; + break; + default: + WRONG_PARAM_COUNT; + break; + } + + RETVAL_LONG(old_error_reporting); +} \ No newline at end of file