Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:24237 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 5440 invoked by uid 1010); 1 Jul 2006 10:53:39 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 5425 invoked from network); 1 Jul 2006 10:53:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Jul 2006 10:53:39 -0000 X-PHP-List-Original-Sender: helly@php.net X-Host-Fingerprint: 81.169.182.136 ajaxatwork.net Linux 2.4/2.6 Received: from ([81.169.182.136:42566] helo=strato.aixcept.de) by pb1.pair.com (ecelerity 2.1.1.3 r(11751M)) with ESMTP id CF/0F-15023-13456A44 for ; Sat, 01 Jul 2006 06:53:38 -0400 Received: from baumbart.mbo (dslb-084-063-024-064.pools.arcor-ip.net [84.63.24.64]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by strato.aixcept.de (Postfix) with ESMTP id CA59135C1EA; Sat, 1 Jul 2006 12:53:34 +0200 (CEST) Date: Sat, 1 Jul 2006 12:53:46 +0200 Reply-To: Marcus Boerger X-Priority: 3 (Normal) Message-ID: <1772356126.20060701125346@marcus-boerger.de> To: Matt W Cc: internals@lists.php.net In-Reply-To: <009f01c69cf9$6b493f30$0201a8c0@pc1> References: <009a01c69848$06f22e80$0201a8c0@pc1> <009f01c69cf9$6b493f30$0201a8c0@pc1> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [PATCH] array_fill: Allow an array to specify keys From: helly@php.net (Marcus Boerger) Hello Matt, patch looks fine now, once we agree to this set you'd have to provide a patch for 5.2 as well. Another thing we need is tests to ensure all is working as expected. best regards marcus Saturday, July 1, 2006, 12:30:48 PM, you wrote: > Index: ext/standard/array.c > =================================================================== > RCS file: /repository/php-src/ext/standard/array.c,v > retrieving revision 1.350 > diff -u -r1.350 array.c > --- ext/standard/array.c 25 Jun 2006 19:19:31 -0000 1.350 > +++ ext/standard/array.c 1 Jul 2006 09:58:26 -0000 > @@ -1625,46 +1625,83 @@ > Create an array containing num elements starting with index start_key each initialized to val */ > PHP_FUNCTION(array_fill) > { > - zval **start_key, **num, **val, *newval; > + zval **key_data, **num, **val, **entry; > long i; > + HashPosition pos; > > - if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &start_key, &num, &val) == FAILURE) { > + if ((ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &key_data, &num, &val) == FAILURE) && > + (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &key_data, &val) == FAILURE)) { > WRONG_PARAM_COUNT; > } > > - switch (Z_TYPE_PP(start_key)) { > - case IS_STRING: > - case IS_UNICODE: > - case IS_LONG: > - case IS_DOUBLE: > - /* allocate an array for return */ > - array_init(return_value); > - > - if (PZVAL_IS_REF(*val)) { > - SEPARATE_ZVAL(val); > - } > - convert_to_long_ex(start_key); > - zval_add_ref(val); > - zend_hash_index_update(Z_ARRVAL_P(return_value), > Z_LVAL_PP(start_key), val, sizeof(val), NULL); > - break; > - default: > - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Wrong data type for start key"); > + if (ZEND_NUM_ARGS() == 2) { > + if (Z_TYPE_PP(key_data) != IS_ARRAY) { > + php_error_docref(NULL TSRMLS_CC, E_WARNING, > "First parameter must be an array when passing 2 parameters"); > RETURN_FALSE; > - break; > - } > + } > + } else { > + switch (Z_TYPE_PP(key_data)) { > + case IS_LONG: > + case IS_STRING: > + case IS_UNICODE: > + case IS_DOUBLE: > + convert_to_long_ex(num); > + > + i = Z_LVAL_PP(num) - 1; > + if (i < 0) { > + php_error_docref(NULL TSRMLS_CC, > E_WARNING, "Number of elements must be positive"); > + RETURN_FALSE; > + } > > - convert_to_long_ex(num); > - i = Z_LVAL_PP(num) - 1; > - if (i < 0) { > - zend_hash_destroy(Z_ARRVAL_P(return_value)); > - efree(Z_ARRVAL_P(return_value)); > - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number of elements must be positive"); > - RETURN_FALSE; > + convert_to_long_ex(key_data); > + break; > + default: > + php_error_docref(NULL TSRMLS_CC, > E_WARNING, "Wrong data type for start key"); > + RETURN_FALSE; > + break; > + } > + } > + > + /* Initialize return array */ > + array_init(return_value); > + > + if (PZVAL_IS_REF(*val)) { > + SEPARATE_ZVAL(val); > } > - newval = *val; > - while (i--) { > - zval_add_ref(&newval); > - zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &newval, sizeof(zval *), NULL); > + > + > + if (Z_TYPE_PP(key_data) == IS_LONG) { > + zval_add_ref(val); > + zend_hash_index_update(Z_ARRVAL_P(return_value), > Z_LVAL_PP(key_data), val, sizeof(zval *), NULL); > + > + while (i--) { > + zval_add_ref(val); > + > zend_hash_next_index_insert(Z_ARRVAL_P(return_value), val, sizeof(zval *), NULL); > + } > + } else { > + > zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(key_data), &pos); > + > + while > (zend_hash_get_current_data_ex(Z_ARRVAL_PP(key_data), (void **)&entry, &pos) == SUCCESS) { > + zval_add_ref(val); > + > + if (Z_TYPE_PP(entry) == IS_STRING || > + Z_TYPE_PP(entry) == IS_UNICODE) { > + > zend_u_symtable_update(Z_ARRVAL_P(return_value), Z_TYPE_PP(entry), > Z_UNIVAL_PP(entry), Z_UNILEN_PP(entry) + 1, val, sizeof(zval *), NULL); > + } else if (Z_TYPE_PP(entry) == IS_LONG) { > + > zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL_PP(entry), val, sizeof(zval *), NULL); > + } else { > + zval tmpkey; > + > + tmpkey = **entry; > + zval_copy_ctor(&tmpkey); > + convert_to_string(&tmpkey); > + > + > zend_symtable_update(Z_ARRVAL_P(return_value), Z_STRVAL(tmpkey), > Z_STRLEN(tmpkey) + 1, val, sizeof(zval *), NULL); > + > + zval_dtor(&tmpkey); > + } > + zend_hash_move_forward_ex(Z_ARRVAL_PP(key_data), &pos); > + } > } > } > /* }}} */ Best regards, Marcus