Adjust array_pad(), array_reverse(), and array_unique() for params API

and mark them with U.
This commit is contained in:
Andrei Zmievski 2006-07-14 22:41:22 +00:00
parent 2ef3bb9cb8
commit 853ab6e09f
2 changed files with 39 additions and 66 deletions

View file

@ -2631,44 +2631,33 @@ PHP_FUNCTION(array_count_values)
/* }}} */ /* }}} */
/* {{{ proto array array_reverse(array input [, bool preserve keys]) /* {{{ proto array array_reverse(array input [, bool preserve keys]) U
Return input as a new array with the order of the entries reversed */ Return input as a new array with the order of the entries reversed */
PHP_FUNCTION(array_reverse) PHP_FUNCTION(array_reverse)
{ {
zval **input, /* Input array */ zval *input, /* Input array */
**z_preserve_keys, /* Flag: whether to preserve keys */
**entry; /* An entry in the input array */ **entry; /* An entry in the input array */
zstr string_key; zstr string_key;
uint string_key_len; uint string_key_len;
ulong num_key; ulong num_key;
zend_bool preserve_keys = 0; zend_bool preserve_keys = 0; /* whether to preserve keys */
HashPosition pos; HashPosition pos;
/* Get arguments and do error-checking */ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b", &input,
if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 2 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &input, &z_preserve_keys) == FAILURE) { &preserve_keys) == FAILURE) {
WRONG_PARAM_COUNT;
}
if (Z_TYPE_PP(input) != IS_ARRAY) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The argument should be an array");
return; return;
} }
if (ZEND_NUM_ARGS() > 1) {
convert_to_boolean_ex(z_preserve_keys);
preserve_keys = Z_BVAL_PP(z_preserve_keys);
}
/* Initialize return array */ /* Initialize return array */
array_init(return_value); array_init(return_value);
zend_hash_internal_pointer_end_ex(Z_ARRVAL_PP(input), &pos); zend_hash_internal_pointer_end_ex(Z_ARRVAL_P(input), &pos);
while (zend_hash_get_current_data_ex(Z_ARRVAL_PP(input), (void **)&entry, &pos) == SUCCESS) { while (zend_hash_get_current_data_ex(Z_ARRVAL_P(input), (void **)&entry, &pos) == SUCCESS) {
zend_uchar utype; zend_uchar utype;
(*entry)->refcount++; zval_add_ref(entry);
switch (zend_hash_get_current_key_ex(Z_ARRVAL_PP(input), &string_key, &string_key_len, &num_key, 0, &pos)) { switch (zend_hash_get_current_key_ex(Z_ARRVAL_P(input), &string_key, &string_key_len, &num_key, 0, &pos)) {
case HASH_KEY_IS_STRING: case HASH_KEY_IS_STRING:
utype = IS_STRING; utype = IS_STRING;
goto ukey; goto ukey;
@ -2687,46 +2676,38 @@ ukey:
break; break;
} }
zend_hash_move_backwards_ex(Z_ARRVAL_PP(input), &pos); zend_hash_move_backwards_ex(Z_ARRVAL_P(input), &pos);
} }
} }
/* }}} */ /* }}} */
/* {{{ proto array array_pad(array input, int pad_size, mixed pad_value) /* {{{ proto array array_pad(array input, int pad_size, mixed pad_value) U
Returns a copy of input array padded with pad_value to size pad_size */ Returns a copy of input array padded with pad_value to size pad_size */
PHP_FUNCTION(array_pad) PHP_FUNCTION(array_pad)
{ {
zval **input; /* Input array */ zval *input; /* Input array */
zval **pad_size; /* Size to pad to */ zval *pad_value; /* Padding value obviously */
zval **pad_value; /* Padding value obviously */
zval ***pads; /* Array to pass to splice */ zval ***pads; /* Array to pass to splice */
HashTable *new_hash; /* Return value from splice */ HashTable *new_hash;/* Return value from splice */
long pad_size; /* Size to pad to */
long pad_size_abs; /* Absolute value of pad_size */
int input_size; /* Size of the input array */ int input_size; /* Size of the input array */
int pad_size_abs; /* Absolute value of pad_size */
int num_pads; /* How many pads do we need */ int num_pads; /* How many pads do we need */
int do_pad; /* Whether we should do padding at all */ int do_pad; /* Whether we should do padding at all */
int i; int i;
/* Get arguments and do error-checking */ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|lz", &input, &pad_size, &pad_value) == FAILURE) {
if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &input, &pad_size, &pad_value) == FAILURE) {
WRONG_PARAM_COUNT;
}
/* Make sure arguments are of the proper type */
if (Z_TYPE_PP(input) != IS_ARRAY) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The argument should be an array");
return; return;
} }
convert_to_long_ex(pad_size);
/* Do some initial calculations */ /* Do some initial calculations */
input_size = zend_hash_num_elements(Z_ARRVAL_PP(input)); input_size = zend_hash_num_elements(Z_ARRVAL_P(input));
pad_size_abs = abs(Z_LVAL_PP(pad_size)); pad_size_abs = abs(pad_size);
do_pad = (input_size >= pad_size_abs) ? 0 : 1; do_pad = (input_size >= pad_size_abs) ? 0 : 1;
/* Copy the original array */ /* Copy the original array */
RETVAL_ZVAL(*input, 1, 0); RETVAL_ZVAL(input, 1, 0);
/* If no need to pad, no need to continue */ /* If no need to pad, no need to continue */
if (!do_pad) { if (!do_pad) {
@ -2735,17 +2716,17 @@ PHP_FUNCTION(array_pad)
/* Populate the pads array */ /* Populate the pads array */
num_pads = pad_size_abs - input_size; num_pads = pad_size_abs - input_size;
if(num_pads > 1048576) { if (num_pads > 1048576) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "You may only pad up to 1048576 elements at a time"); php_error_docref(NULL TSRMLS_CC, E_WARNING, "You may only pad up to 1048576 elements at a time");
RETURN_FALSE; RETURN_FALSE;
} }
pads = (zval ***)safe_emalloc(num_pads, sizeof(zval **), 0); pads = (zval ***)safe_emalloc(num_pads, sizeof(zval **), 0);
for (i = 0; i < num_pads; i++) { for (i = 0; i < num_pads; i++) {
pads[i] = pad_value; pads[i] = &pad_value;
} }
/* Pad on the right or on the left */ /* Pad on the right or on the left */
if (Z_LVAL_PP(pad_size) > 0) { if (pad_size > 0) {
new_hash = php_splice(Z_ARRVAL_P(return_value), input_size, 0, pads, num_pads, NULL); new_hash = php_splice(Z_ARRVAL_P(return_value), input_size, 0, pads, num_pads, NULL);
} else { } else {
new_hash = php_splice(Z_ARRVAL_P(return_value), 0, 0, pads, num_pads, NULL); new_hash = php_splice(Z_ARRVAL_P(return_value), 0, 0, pads, num_pads, NULL);
@ -2867,11 +2848,11 @@ PHP_FUNCTION(array_change_key_case)
} }
/* }}} */ /* }}} */
/* {{{ proto array array_unique(array input) /* {{{ proto array array_unique(array input) U
Removes duplicate values from array */ Removes duplicate values from array */
PHP_FUNCTION(array_unique) PHP_FUNCTION(array_unique)
{ {
zval **array; zval *array;
HashTable *target_hash; HashTable *target_hash;
Bucket *p; Bucket *p;
struct bucketindex { struct bucketindex {
@ -2881,17 +2862,14 @@ PHP_FUNCTION(array_unique)
struct bucketindex *arTmp, *cmpdata, *lastkept; struct bucketindex *arTmp, *cmpdata, *lastkept;
unsigned int i; unsigned int i;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &array) == FAILURE) { if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array) == FAILURE) {
WRONG_PARAM_COUNT; return;
}
target_hash = HASH_OF(*array);
if (!target_hash) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The argument should be an array");
RETURN_FALSE;
} }
target_hash = HASH_OF(array);
/* copy the argument array */ /* copy the argument array */
RETVAL_ZVAL(*array, 1, 0); RETVAL_ZVAL(array, 1, 0);
if (target_hash->nNumOfElements <= 1) { /* nothing to do */ if (target_hash->nNumOfElements <= 1) { /* nothing to do */
return; return;
@ -2900,6 +2878,7 @@ PHP_FUNCTION(array_unique)
/* create and sort array with pointers to the target_hash buckets */ /* create and sort array with pointers to the target_hash buckets */
arTmp = (struct bucketindex *) pemalloc((target_hash->nNumOfElements + 1) * sizeof(struct bucketindex), target_hash->persistent); arTmp = (struct bucketindex *) pemalloc((target_hash->nNumOfElements + 1) * sizeof(struct bucketindex), target_hash->persistent);
if (!arTmp) { if (!arTmp) {
zval_dtor(return_value);
RETURN_FALSE; RETURN_FALSE;
} }
for (i = 0, p = target_hash->pListHead; p; i++, p = p->pListNext) { for (i = 0, p = target_hash->pListHead; p; i++, p = p->pListNext) {

View file

@ -31,18 +31,9 @@ ext/standard
array_multisort() array_multisort()
Add SORT_LOCALE_STRING, test Add SORT_LOCALE_STRING, test
array_pad()
Params API, test
array_reduce() array_reduce()
Params API, FCI cache, test Params API, FCI cache, test
array_reverse()
Params API, test
array_unique()
Params API, test
array_walk() array_walk()
Params API, is_callable check, FCI cache, test Params API, is_callable check, FCI cache, test
@ -77,12 +68,15 @@ ext/standard
array_merge_recursive() array_merge_recursive()
array_product() array_product()
array_push(), array_pop(), array_shift(), array_unshift() array_push(), array_pop(), array_shift(), array_unshift()
array_pad()
array_rand() array_rand()
array_reverse()
array_search() array_search()
array_slice() array_slice()
array_splice() array_splice()
array_sum() array_sum()
array_values() array_values()
array_unique()
compact() compact()
count() count()
in_array() in_array()