Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:24210 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 88361 invoked by uid 1010); 25 Jun 2006 11:10:59 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 88346 invoked from network); 25 Jun 2006 11:10:59 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 25 Jun 2006 11:10:59 -0000 X-PHP-List-Original-Sender: php_lists@realplain.com X-Host-Fingerprint: 69.179.208.43 msa3-mx.centurytel.net Linux 2.4/2.6 Received: from ([69.179.208.43:38183] helo=msa3-mx.centurytel.net) by pb1.pair.com (ecelerity 2.1.1.3 r(11751M)) with ESMTP id C7/EB-02438-14F6E944 for ; Sun, 25 Jun 2006 07:10:58 -0400 Received: from pc1 (64-91-107-63.dyn.centurytel.net [64.91.107.63]) by msa3-mx.centurytel.net (8.13.6/8.13.6) with SMTP id k5PBArWf022449 for ; Sun, 25 Jun 2006 06:10:54 -0500 Message-ID: <009a01c69848$06f22e80$0201a8c0@pc1> To: Date: Sun, 25 Jun 2006 06:10:54 -0500 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_0097_01C6981E.1DBA7E80" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1807 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1807 Subject: [PATCH] array_fill: Allow an array to specify keys From: php_lists@realplain.com ("Matt W") ------=_NextPart_000_0097_01C6981E.1DBA7E80 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hi all, I'm on Windows and haven't tried submitting a patch before, so I JUST now installed WinCvs since I don't think you want just a diff of my local file. I used PHP_5_1 since: 5.1's the "regular" download I started working on; if you use it, I assume this can go in 5.1; and I don't know what to do. :-) I've been casually browsing through the PHP source for a while, but never modified it or used C. Just found out about VC++ Express, etc. and set it up to try messing with PHP. Had some missing file errors while building (don't know if that's my problem, or a bug...), but got it done after changing a few files that were causing them. Just some background... ;-) Awhile ago I wished array_fill could also be used like array_fill(array keys, mixed value), which would work like: foreach ($keys as $k) { $array[$k] = $val; } OR $array = array_combine($keys, array_fill(0, count($keys), $val)); So that's what I did for my first, admittedly simple, modification. I couldn't figure out a reason for the *newval variable, since it seemed to be the same as **val (pointer stuff can confuse me, being a C newbie ;-)). Was I wrong to remove it? I also wasn't sure why sizeof(val) was used in one place and sizeof(zval *) in another; but it seems to be random after looking in array.c whether var_name or zval * is used, so I stuck with the latter. I realize now that they're same (right?), but it delayed me, assuming there's a *reason* for everything. Other things I changed while experimenting were initializing the return array AFTER checking the parameters so it doesn't have to be destroyed/freed later; and I made the IS_LONG case first in the switch () for the first parameter's type -- isn't that logical since it's supposed to a PHP int and probably is? :-) I hope it's coded correctly (feedback welcome, as basic as it is, LOL). Everything works like it should as far as I can tell. And it's a lot faster of course than the other 2 methods I mentioned for initializing arbitrary keys. Let me know if I need to diff against a version other than 5.1 or whatever. Thanks, Matt ------=_NextPart_000_0097_01C6981E.1DBA7E80 Content-Type: text/plain; name="array_fill_patch.txt" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="array_fill_patch.txt" Index: ext/standard/array.c=0A= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A= RCS file: /repository/php-src/ext/standard/array.c,v=0A= retrieving revision 1.308.2.22=0A= diff -u -r1.308.2.22 array.c=0A= --- ext/standard/array.c 3 Jun 2006 18:59:55 -0000 1.308.2.22=0A= +++ ext/standard/array.c 24 Jun 2006 03:14:01 -0000=0A= @@ -1536,45 +1536,81 @@=0A= Create an array containing num elements starting with index = start_key each initialized to val */=0A= PHP_FUNCTION(array_fill)=0A= {=0A= - zval **start_key, **num, **val, *newval;=0A= + zval **key_data, **num, **val, **entry;=0A= long i;=0A= + HashPosition pos;=0A= =0A= - if (ZEND_NUM_ARGS() !=3D 3 || zend_get_parameters_ex(3, &start_key, = &num, &val) =3D=3D FAILURE) {=0A= + if ((ZEND_NUM_ARGS() !=3D 3 || zend_get_parameters_ex(3, &key_data, = &num, &val) =3D=3D FAILURE) &&=0A= + (ZEND_NUM_ARGS() !=3D 2 || zend_get_parameters_ex(2, &key_data, &val) = =3D=3D FAILURE)) {=0A= WRONG_PARAM_COUNT;=0A= }=0A= =0A= - switch (Z_TYPE_PP(start_key)) {=0A= - case IS_STRING:=0A= - case IS_LONG:=0A= - case IS_DOUBLE:=0A= - /* allocate an array for return */=0A= - array_init(return_value);=0A= - =0A= - if (PZVAL_IS_REF(*val)) {=0A= - SEPARATE_ZVAL(val);=0A= - }=0A= - convert_to_long_ex(start_key);=0A= - zval_add_ref(val);=0A= - zend_hash_index_update(Z_ARRVAL_P(return_value), = Z_LVAL_PP(start_key), val, sizeof(val), NULL);=0A= - break;=0A= - default:=0A= - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Wrong data type for = start key");=0A= + if (ZEND_NUM_ARGS() =3D=3D 2) {=0A= + if (Z_TYPE_PP(key_data) !=3D IS_ARRAY) {=0A= + php_error_docref(NULL TSRMLS_CC, E_WARNING, "First parameter must be = an array when passing 2 parameters");=0A= RETURN_FALSE;=0A= - break;=0A= - } =0A= + }=0A= + } else {=0A= + switch (Z_TYPE_PP(key_data)) {=0A= + case IS_LONG:=0A= + case IS_STRING:=0A= + case IS_DOUBLE:=0A= + convert_to_long_ex(num);=0A= +=0A= + i =3D Z_LVAL_PP(num) - 1;=0A= + if (i < 0) {=0A= + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number of elements = must be positive");=0A= + RETURN_FALSE;=0A= + }=0A= =0A= - convert_to_long_ex(num);=0A= - i =3D Z_LVAL_PP(num) - 1; =0A= - if (i < 0) {=0A= - zend_hash_destroy(Z_ARRVAL_P(return_value));=0A= - efree(Z_ARRVAL_P(return_value));=0A= - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number of elements must = be positive");=0A= - RETURN_FALSE;=0A= + convert_to_long_ex(key_data);=0A= + break;=0A= + default:=0A= + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Wrong data type for = start key");=0A= + RETURN_FALSE;=0A= + break;=0A= + }=0A= }=0A= - newval =3D *val;=0A= - while (i--) {=0A= - zval_add_ref(&newval);=0A= - zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &newval, = sizeof(zval *), NULL);=0A= +=0A= + /* Initialize return array */=0A= + array_init(return_value);=0A= +=0A= + if (PZVAL_IS_REF(*val)) {=0A= + SEPARATE_ZVAL(val);=0A= + }=0A= +=0A= +=0A= + if (Z_TYPE_PP(key_data) =3D=3D IS_LONG) {=0A= + zval_add_ref(val);=0A= + zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL_PP(key_data), = val, sizeof(zval *), NULL);=0A= +=0A= + while (i--) {=0A= + zval_add_ref(val);=0A= + zend_hash_next_index_insert(Z_ARRVAL_P(return_value), val, = sizeof(zval *), NULL);=0A= + }=0A= + } else {=0A= + zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(key_data), &pos);=0A= +=0A= + while (zend_hash_get_current_data_ex(Z_ARRVAL_PP(key_data), (void = **)&entry, &pos) =3D=3D SUCCESS) {=0A= + zval_add_ref(val);=0A= +=0A= + if (Z_TYPE_PP(entry) =3D=3D IS_STRING) {=0A= + zend_symtable_update(Z_ARRVAL_P(return_value), Z_STRVAL_PP(entry), = Z_STRLEN_PP(entry) + 1, val, sizeof(zval *), NULL);=0A= + } else if (Z_TYPE_PP(entry) =3D=3D IS_LONG) {=0A= + zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL_PP(entry), = val, sizeof(zval *), NULL);=0A= + } else {=0A= + zval tmpkey;=0A= +=0A= + tmpkey =3D **entry;=0A= + zval_copy_ctor(&tmpkey);=0A= + convert_to_string(&tmpkey);=0A= +=0A= + zend_symtable_update(Z_ARRVAL_P(return_value), Z_STRVAL(tmpkey), = Z_STRLEN(tmpkey) + 1, val, sizeof(zval *), NULL);=0A= +=0A= + zval_dtor(&tmpkey);=0A= + }=0A= + zend_hash_move_forward_ex(Z_ARRVAL_PP(key_data), &pos);=0A= + }=0A= }=0A= }=0A= /* }}} */=0A= ------=_NextPart_000_0097_01C6981E.1DBA7E80--