arpad                                    Mon, 29 Aug 2011 20:38:52 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=315740

Log:
Fixed bug #55366: keys lost when using substr_replace an array

Bug: https://bugs.php.net/55366 (Open) keys lost when using substr_replace 
replace an array 
      
Changed paths:
    U   php/php-src/branches/PHP_5_4/NEWS
    U   php/php-src/branches/PHP_5_4/ext/standard/string.c
    A   
php/php-src/branches/PHP_5_4/ext/standard/tests/strings/substr_replace_array.phpt
    U   php/php-src/trunk/ext/standard/string.c
    A   php/php-src/trunk/ext/standard/tests/strings/substr_replace_array.phpt

Modified: php/php-src/branches/PHP_5_4/NEWS
===================================================================
--- php/php-src/branches/PHP_5_4/NEWS   2011-08-29 20:36:22 UTC (rev 315739)
+++ php/php-src/branches/PHP_5_4/NEWS   2011-08-29 20:38:52 UTC (rev 315740)
@@ -12,6 +12,7 @@
     (virsacer at web dot de, Pierre)
   . Added support for SORT_NATURAL and SORT_FLAG_CASE in array sort functions:
     sort, rsort, ksort, krsort, asort, arsort and array_multisort. Req #55158 
(arpad)
+  . Fixed bug #55366: keys lost when using substr_replace an array (arpad)

 - Improved mbstring extension:
   . Added Shift_JIS/UTF-8 Emoji (pictograms) support. (Rui)

Modified: php/php-src/branches/PHP_5_4/ext/standard/string.c
===================================================================
--- php/php-src/branches/PHP_5_4/ext/standard/string.c  2011-08-29 20:36:22 UTC 
(rev 315739)
+++ php/php-src/branches/PHP_5_4/ext/standard/string.c  2011-08-29 20:38:52 UTC 
(rev 315740)
@@ -2398,6 +2398,10 @@
                        RETURN_STRINGL(Z_STRVAL_PP(str), Z_STRLEN_PP(str), 1);
                }
        } else { /* str is array of strings */
+               char *str_index = NULL;
+               uint str_index_len;
+               ulong num_index;
+
                array_init(return_value);

                if (Z_TYPE_PP(from) == IS_ARRAY) {
@@ -2533,7 +2537,13 @@
                        }

                        result[result_len] = '\0';
-                       add_next_index_stringl(return_value, result, 
result_len, 0);
+
+                       if (zend_hash_get_current_key_ex(Z_ARRVAL_PP(str), 
&str_index, &str_index_len, &num_index, 0, &pos_str) == HASH_KEY_IS_STRING) {
+                               add_assoc_stringl_ex(return_value, str_index, 
str_index_len, result, result_len, 0);
+                       } else {
+                               add_index_stringl(return_value, num_index, 
result, result_len, 0);
+                       }
+
                        if(Z_TYPE_PP(tmp_str) != IS_STRING) {
                                zval_dtor(orig_str);
                        }

Added: 
php/php-src/branches/PHP_5_4/ext/standard/tests/strings/substr_replace_array.phpt
===================================================================
--- 
php/php-src/branches/PHP_5_4/ext/standard/tests/strings/substr_replace_array.phpt
                           (rev 0)
+++ 
php/php-src/branches/PHP_5_4/ext/standard/tests/strings/substr_replace_array.phpt
   2011-08-29 20:38:52 UTC (rev 315740)
@@ -0,0 +1,19 @@
+--TEST--
+substr_replace() function - array
+--FILE--
+<?php
+
+$arr = array('abc' => 'llsskdkk','def' => 'llsskjkkdd', 4 => 'hello', 42 => 
'world');
+$newarr = substr_replace($arr, 'zzz', 0, -2);
+
+print_r($newarr);
+
+?>
+--EXPECTF--
+Array
+(
+    [abc] => zzzkk
+    [def] => zzzdd
+    [4] => zzzlo
+    [42] => zzzld
+)

Modified: php/php-src/trunk/ext/standard/string.c
===================================================================
--- php/php-src/trunk/ext/standard/string.c     2011-08-29 20:36:22 UTC (rev 
315739)
+++ php/php-src/trunk/ext/standard/string.c     2011-08-29 20:38:52 UTC (rev 
315740)
@@ -2398,6 +2398,10 @@
                        RETURN_STRINGL(Z_STRVAL_PP(str), Z_STRLEN_PP(str), 1);
                }
        } else { /* str is array of strings */
+               char *str_index = NULL;
+               uint str_index_len;
+               ulong num_index;
+
                array_init(return_value);

                if (Z_TYPE_PP(from) == IS_ARRAY) {
@@ -2533,7 +2537,13 @@
                        }

                        result[result_len] = '\0';
-                       add_next_index_stringl(return_value, result, 
result_len, 0);
+
+                       if (zend_hash_get_current_key_ex(Z_ARRVAL_PP(str), 
&str_index, &str_index_len, &num_index, 0, &pos_str) == HASH_KEY_IS_STRING) {
+                               add_assoc_stringl_ex(return_value, str_index, 
str_index_len, result, result_len, 0);
+                       } else {
+                               add_index_stringl(return_value, num_index, 
result, result_len, 0);
+                       }
+
                        if(Z_TYPE_PP(tmp_str) != IS_STRING) {
                                zval_dtor(orig_str);
                        }

Added: php/php-src/trunk/ext/standard/tests/strings/substr_replace_array.phpt
===================================================================
--- php/php-src/trunk/ext/standard/tests/strings/substr_replace_array.phpt      
                        (rev 0)
+++ php/php-src/trunk/ext/standard/tests/strings/substr_replace_array.phpt      
2011-08-29 20:38:52 UTC (rev 315740)
@@ -0,0 +1,19 @@
+--TEST--
+substr_replace() function - array
+--FILE--
+<?php
+
+$arr = array('abc' => 'llsskdkk','def' => 'llsskjkkdd', 4 => 'hello', 42 => 
'world');
+$newarr = substr_replace($arr, 'zzz', 0, -2);
+
+print_r($newarr);
+
+?>
+--EXPECTF--
+Array
+(
+    [abc] => zzzkk
+    [def] => zzzdd
+    [4] => zzzlo
+    [42] => zzzld
+)

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to