Re: [PHP4BETA] CVS update: php4/ext/standard
| From: | Andi Gutmans | Date: | Sun, 06 Jun 1999 04:18:23 +0000 |
| Subject: | Re: [PHP4BETA] CVS update: php4/ext/standard | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-1314@lists.php.net to get a copy of this message | ||
I don't have time to look at this code thoroughly right now but it seems there's a problem here. Unless you have a reason for doing this and I'm just not realizing it because I don't have time to look at the code now, you are changing variables to is_ref and increasing the reference count without doing extra work. However if your reference count is more than 1 and the variable is not is_ref before you do this, you'll have to break away this variable from whatever other variables that are pointing at it.
Let me know if this is the case, and I'll talk to Zeev about writing some API function. We wouldn't want you to implement this in each place yourself, it'd be better to have something general that will do the break away code as it exists already.
Andi
At 20:43 05/06/99 +0000, andrey wrote:
Date: Saturday June 5, 1999 @ 16:43 Author: andrey Update of /repository/php4/ext/standard In directory php:/tmp/cvs-serv6114/ext/standard Modified Files:basic_functions.c basic_functions.h reg.cLog Message: Added push(), pop(), shift(), unshift(), splice(), and slice() array functions. The first 5 work like their Perl counterparts. slice() returns a chunk of the array specified by offset and length arguments. Backport to PHP3 and docs will be coming soon. Index: php4/ext/standard/basic_functions.c diff -u php4/ext/standard/basic_functions.c:1.24 php4/ext/standard/basic_functions.c:1.25--- php4/ext/standard/basic_functions.c:1.24 Fri May 28 07:09:39 1999+++ php4/ext/standard/basic_functions.c Sat Jun 5 16:43:36 1999 @@ -297,6 +297,12 @@PHP_FE(in_array, NULL) PHP_FE(extract, NULL) PHP_FE(compact, NULL) + PHP_FE(push, first_arg_force_ref) + PHP_FE(pop, first_arg_force_ref) + PHP_FE(shift, first_arg_force_ref) + PHP_FE(unshift, first_arg_force_ref) + PHP_FE(splice, first_arg_force_ref) + PHP_FE(slice, NULL){NULL, NULL, NULL}}; @@ -2396,7 +2402,7 @@ /* }}} */ -/* {{{ proto void compact(string var_name | array var_names [, ... ]) +/* {{{ proto array compact(string var_name | array var_names [, ... ])Creates a hash containing variables and their values */PHP_FUNCTION(compact) { @@ -2419,6 +2425,404 @@}efree(args);+} +/* }}} */ + + +/* HashTable* _phpi_splice(HashTable *in_hash, int offset, int length,+ zval **list, int list_count, HashTable **removed) */+HashTable* _phpi_splice(HashTable *in_hash, int offset, int length,+ zval **list, int list_count, HashTable **removed)+{+ HashTable *out_hash = NULL; /* Output hashtable */ + int num_in, /* Number of entries in the input hashtable */ + pos, /* Current position in the hashtable */ + i; /* Loop counter */ + Bucket *p; /* Pointer to hash bucket */ + zval *entry; /* Hash entry */++ /* If input hash doesn't exist, we have nothing to do */ + if (!in_hash) + return NULL;++ /* Get number of entries in the input hash */ + num_in = zend_hash_num_elements(in_hash);++ /* Clamp the offset.. */ + if (offset > num_in) + offset = num_in; + else if (offset < 0 && (offset=num_in+offset) < 0) + offset = 0;++ /* ..and the length */ + if (length < 0 || offset+length > num_in) + length = num_in-offset;++ /* Create and initialize output hash */ + out_hash = (HashTable *)emalloc(sizeof(HashTable)); + zend_hash_init(out_hash, 0, NULL, PVAL_PTR_DTOR, 0);++ /* Start at the beginning of the input hash and copy + entries to output hash until offset is reached */ + for (pos=0, p=in_hash->pListHead; pos<offset && p ; pos++, p=p->pListNext) { + /* Get entry and increase reference count */ + entry = *((zval **)p->pData); + entry->refcount++;++ /* Update output hash depending on key type */ + if (p->nKeyLength) + zend_hash_update(out_hash, p->arKey, p->nKeyLength, &entry, sizeof(zval *), NULL); + else + zend_hash_next_index_insert(out_hash, &entry, sizeof(zval *), NULL); + }++ /* If hash for removed entries exists, go until offset+length + and copy the entries to it */ + if (removed != NULL) { + for( ; pos<offset+length && p; pos++, p=p->pListNext) { + entry = *((zval **)p->pData); + entry->refcount++; + if (p->nKeyLength) + zend_hash_update(*removed, p->arKey, p->nKeyLength, &entry, sizeof(zval *), NULL); + else + zend_hash_next_index_insert(*removed, &entry, sizeof(zval *), NULL); + } + } else /* otherwise just skip those entries */ + for( ; pos<offset+length && p; pos++, p=p->pListNext);++ /* If there are entries to insert.. */ + if (list != NULL) { + /* ..for each one, create a new zval, copy entry into it + and copy it into the output hash */ + for (i=0; i<list_count; i++) { + entry = list[i]; + entry->is_ref = 1; + entry->refcount++; + zend_hash_next_index_insert(out_hash, &entry, sizeof(zval *), NULL); + } + }++ /* Copy the remaining input hash entries to the output hash */ + for ( ; p ; p=p->pListNext) { + entry = *((zval **)p->pData); + entry->refcount++; + if (p->nKeyLength) + zend_hash_update(out_hash, p->arKey, p->nKeyLength, &entry, sizeof(zval *), NULL); + else + zend_hash_next_index_insert(out_hash, &entry, sizeof(zval *), NULL); + }++ zend_hash_internal_pointer_reset(out_hash); + return out_hash;+} +/* }}} */ + + +/* {{{ proto int push(array stack, mixed var [, ...]) + Pushes elements onto the end of the array */ +PHP_FUNCTION(push) +{+ zval **args, /* Function arguments array */ + *stack, /* Input array */ + *new_var; /* Variable to be pushed */ + int i, /* Loop counter */ + argc; /* Number of function arguments */++ /* Get the argument count and check it */ + argc = ARG_COUNT(ht); + if (argc < 2) { + WRONG_PARAM_COUNT; + }++ /* Allocate arguments array and get the arguments, checking for errors. */ + args = (zval **)emalloc(argc * sizeof(zval *)); + if (getParametersArray(ht, argc, args) == FAILURE) { + efree(args); + WRONG_PARAM_COUNT; + }++ /* Get first argument and check that it's an array */ + stack = args[0]; + if (stack->type != IS_ARRAY) { + zend_error(E_WARNING, "First argument to push() needs to be an array"); + RETURN_FALSE; + }++ /* For each subsequent argument, make it a reference, increase refcount, + and add it to the end of the array */ + for (i=1; i<argc; i++) { + new_var = args[i]; + new_var->is_ref = 1; + new_var->refcount++;++ zend_hash_next_index_insert(stack->value.ht, &new_var, sizeof(zval *), NULL); + }++ /* Clean up and return the number of values in the stack */ + efree(args); + RETVAL_LONG(zend_hash_num_elements(stack->value.ht));+} +/* }}} */ + + +/* {{{ void _phpi_pop(INTERNAL_FUNCTION_PARAMETERS, int which_end) */ +static void _phpi_pop(INTERNAL_FUNCTION_PARAMETERS, int off_the_end) +{+ zval *stack, /* Input stack */ + **val; /* Value to be popped */ + char *string_key; + ulong num_key;++ /* Get the arguments and do error-checking */ + if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &stack) == FAILURE) { + WRONG_PARAM_COUNT; + }++ if (stack->type != IS_ARRAY) { + zend_error(E_WARNING, "The argument needs to be an array"); + return; + }++ /* Get the first or last value and copy it into the return value */ + if (off_the_end) + zend_hash_internal_pointer_end(stack->value.ht); + else + zend_hash_internal_pointer_reset(stack->value.ht); + zend_hash_get_current_data(stack->value.ht, (void **)&val); + *return_value = **val; + zval_copy_ctor(return_value); + return_value->refcount=1; + return_value->is_ref=0;++ /* Delete the first or last value */ + switch (zend_hash_get_current_key(stack->value.ht, &string_key, &num_key)) { + case HASH_KEY_IS_STRING: + zend_hash_del(stack->value.ht, string_key, strlen(string_key)+1); + efree(string_key); + break;++ case HASH_KEY_IS_LONG: + zend_hash_index_del(stack->value.ht, num_key); + break; + }+} +/* }}} */ + + +/* {{{ proto mixed pop(array stack) + Pops an element off the end of the array */ +PHP_FUNCTION(pop) +{+ _phpi_pop(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);+} +/* }}} */ + + +/* {{{ proto mixed shift(array stack) + Pops an element off the beginning of the array */ +PHP_FUNCTION(shift) +{+ _phpi_pop(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);+} +/* }}} */ + + +/* {{{ proto int unshift(array stack, mixed var [, ...]) + Pushes elements onto the beginning of the array */ +PHP_FUNCTION(unshift) +{+ zval **args, /* Function arguments array */ + *stack; /* Input stack */ + HashTable *new_hash; /* New hashtable for the stack */ + int argc; /* Number of function arguments */+ ++ /* Get the argument count and check it */ + argc = ARG_COUNT(ht); + if (argc < 2) { + WRONG_PARAM_COUNT; + }++ /* Allocate arguments array and get the arguments, checking for errors. */ + args = (zval **)emalloc(argc * sizeof(zval *)); + if (getParametersArray(ht, argc, args) == FAILURE) { + efree(args); + WRONG_PARAM_COUNT; + }++ /* Get first argument and check that it's an array */ + stack = args[0]; + if (stack->type != IS_ARRAY) { + zend_error(E_WARNING, "First argument to push() needs to be an array"); + RETURN_FALSE; + }++ /* Use splice to insert the elements at the beginning. Destroy old + hashtable and replace it with new one */ + new_hash = _phpi_splice(stack->value.ht, 0, 0, &args[1], argc-1, NULL); + zend_hash_destroy(stack->value.ht); + efree(stack->value.ht); + stack->value.ht = new_hash;++ /* Clean up and return the number of elements in the stack */ + efree(args); + RETVAL_LONG(zend_hash_num_elements(stack->value.ht));+} +/* }}} */ + + +/* {{{ proto array splice(array input, int offset [, int length, mixed var [, ...] ]) + Removes the elements designated by offset and length and replace them with + var's if supplied */ +PHP_FUNCTION(splice) +{+ zval **args, /* Function arguments array */ + *array; /* Input array */ + HashTable *removed = NULL, /* Hash for removed elements */ + *new_hash = NULL; /* Output array's hash */ + int argc, /* Number of function arguments */ + offset, + length;++ /* Get the argument count and check it */ + argc = ARG_COUNT(ht); + if (argc < 2) { + WRONG_PARAM_COUNT; + }++ /* Allocate arguments array and get the arguments, checking for errors. */ + args = (zval **)emalloc(argc * sizeof(zval *)); + if (getParametersArray(ht, argc, args) == FAILURE) { + efree(args); + WRONG_PARAM_COUNT; + }++ /* Get first argument and check that it's an array */ + array = args[0]; + if (array->type != IS_ARRAY) { + zend_error(E_WARNING, "First argument to splice() should be an array"); + efree(args); + return; + }++ /* Get the next two arguments. If length is omitted, + it's assumed to be -1 */ + convert_to_long(args[1]); + offset = args[1]->value.lval; + if (argc > 2) { + convert_to_long(args[2]); + length = args[2]->value.lval; + } else + length = -1;++ /* Initialize return value */ + array_init(return_value);++ /* Perform splice */ + new_hash = _phpi_splice(array->value.ht, offset, length, + (argc>3) ? &args[3] : NULL , argc-3, + &return_value->value.ht);++ /* Replace input array's hashtable with the new one */ + zend_hash_destroy(array->value.ht); + efree(array->value.ht); + array->value.ht = new_hash;++ /* Clean up */ + efree(args);+} +/* }}} */ + + +/* {{{ proto array slice(array input, int offset [, int length]) + Returns elements specified by offset and length */ +PHP_FUNCTION(slice) +{+ zval *input, /* Input array */ + *offset, /* Offset to get elements from */ + *length, /* How many elements to get */ + **entry; /* An array entry */ + int offset_val, /* Value of the offset argument */ + length_val, /* Value of the length argument */ + num_in, /* Number of elements in the input array */ + pos, /* Current position in the array */ + argc, /* Number of function arguments */ + i; /* Loop counter */ + char *string_key; + ulong num_key;+ ++ /* Get the arguments and do error-checking */ + argc = ARG_COUNT(ht); + if (argc < 2 || argc > 3 || getParameters(ht, argc, &input, &offset, &length)) { + WRONG_PARAM_COUNT; + }++ if (input->type != IS_ARRAY) { + zend_error(E_WARNING, "First argument to slice() should be an array"); + return; + }++ /* Make sure offset and length are integers and assume + we want all entries from offset to the end if length + is not passed */ + convert_to_long(offset); + offset_val = offset->value.lval; + if (argc == 3) { + convert_to_long(length); + length_val = length->value.lval; + } else + length_val = -1;++ /* Initialize returned array */ + array_init(return_value);++ /* Get number of entries in the input hash */ + num_in = zend_hash_num_elements(input->value.ht);++ /* Clamp the offset.. */ + if (offset_val > num_in) + return; + else if (offset_val < 0 && (offset_val=num_in+offset_val) < 0) + offset_val = 0;++ /* ..and the length */ + if (length_val < 0 || offset_val+length_val > num_in) + length_val = num_in-offset_val;++ if (length_val == 0) + return;++ /* Start at the beginning and go until we hit offset */ + pos = 0; + zend_hash_internal_pointer_reset(input->value.ht); + while(pos < offset_val && + zend_hash_get_current_data(input->value.ht, (void **)&entry) == SUCCESS) { + pos++; + zend_hash_move_forward(input->value.ht); + }++ /* Copy elements from input array to the one that's returned */ + while(pos < offset_val+length_val && + zend_hash_get_current_data(input->value.ht, (void **)&entry) == SUCCESS) {++ (*entry)->refcount++;++ switch (zend_hash_get_current_key(input->value.ht, &string_key, &num_key)) { + case HASH_KEY_IS_STRING: + zend_hash_update(return_value->value.ht, string_key, strlen(string_key)+1, + entry, sizeof(zval *), NULL); + efree(string_key); + break;++ case HASH_KEY_IS_LONG: + zend_hash_next_index_insert(return_value->value.ht, + entry, sizeof(zval *), NULL); + break;
--- Andi Gutmans <andi@zend.com> ICQ: 1568297+ } + pos++; + zend_hash_move_forward(input->value.ht); + }} /* }}} */ Index: php4/ext/standard/basic_functions.h diff -u php4/ext/standard/basic_functions.h:1.6 php4/ext/standard/basic_functions.h:1.7--- php4/ext/standard/basic_functions.h:1.6 Sun May 16 07:19:25 1999+++ php4/ext/standard/basic_functions.h Sat Jun 5 16:43:36 1999 @@ -29,7 +29,7 @@ */ -/* $Id: basic_functions.h,v 1.6 1999/05/16 11:19:25 sas Exp $ */ +/* $Id: basic_functions.h,v 1.7 1999/06/05 20:43:36 andrey Exp $ */ #ifndef _BASIC_FUNCTIONS_H #define _BASIC_FUNCTIONS_H @@ -122,6 +122,12 @@ PHP_FUNCTION(in_array); PHP_FUNCTION(extract); PHP_FUNCTION(compact); +PHP_FUNCTION(push); +PHP_FUNCTION(pop); +PHP_FUNCTION(shift); +PHP_FUNCTION(unshift); +PHP_FUNCTION(splice); +PHP_FUNCTION(slice); #if HAVE_PUTENV typedef struct { Index: php4/ext/standard/reg.c diff -u php4/ext/standard/reg.c:1.11 php4/ext/standard/reg.c:1.12--- php4/ext/standard/reg.c:1.11 Sat Jun 5 09:56:18 1999 +++ php4/ext/standard/reg.c Sat Jun 5 16:43:36 1999@@ -28,7 +28,7 @@| Jaakko Hyvätti <jaakko@hyvatti.iki.fi> | +----------------------------------------------------------------------+*/ -/* $Id: reg.c,v 1.11 1999/06/05 13:56:18 zeev Exp $ */ +/* $Id: reg.c,v 1.12 1999/06/05 20:43:36 andrey Exp $ */ #include <stdio.h> #include "php.h" @@ -564,7 +564,6 @@/* churn through str, generating array entries as we go */ while ((count == -1 || count > 0) && !(err = regexec(&re, strp, 1, subs, 0))) { - printf("In the loop...\n"); if (subs[0].rm_so == 0 && subs[0].rm_eo) { /* match is at start of string, return empty string */ add_next_index_stringl(return_value, empty_string, 0, 1);-- PHP 4 Beta