pollita         Thu Jan 30 15:09:20 2003 EDT

  Modified files:              
    /php4/ext/standard  string.c 
  Log:
  Unify str_replace and str_ireplace using INTERNAL_FUNCTION_PARAM_PASSTHRU -- reduce 
codebase/maintenance complexity
  
  
Index: php4/ext/standard/string.c
diff -u php4/ext/standard/string.c:1.352 php4/ext/standard/string.c:1.353
--- php4/ext/standard/string.c:1.352    Thu Jan 30 00:00:41 2003
+++ php4/ext/standard/string.c  Thu Jan 30 15:09:19 2003
@@ -18,7 +18,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: string.c,v 1.352 2003/01/30 05:00:41 pollita Exp $ */
+/* $Id: string.c,v 1.353 2003/01/30 20:09:19 pollita Exp $ */
 
 /* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */
 
@@ -2887,9 +2887,9 @@
 }
 /* }}} */
 
-/* {{{ proto mixed str_replace(mixed search, mixed replace, mixed subject)
-   Replaces all occurrences of search in haystack with replace */
-PHP_FUNCTION(str_replace)
+/* {{{ php_str_replace_common
+ */
+static void php_str_replace_common(INTERNAL_FUNCTION_PARAMETERS, int case_sensitivity)
 {
        zval **subject, **search, **replace, **subject_entry;
        zval *result;
@@ -2923,7 +2923,7 @@
                   and add the result to the return_value array. */
                while (zend_hash_get_current_data(Z_ARRVAL_PP(subject), (void 
**)&subject_entry) == SUCCESS) {
                        MAKE_STD_ZVAL(result);
-                       php_str_replace_in_subject(*search, *replace, subject_entry, 
result, 1);
+                       php_str_replace_in_subject(*search, *replace, subject_entry, 
+result, case_sensitivity);
                        /* Add to return array */
                        switch (zend_hash_get_current_key_ex(Z_ARRVAL_PP(subject), 
&string_key,
                                                                                       
         &string_key_len, &num_key, 0, NULL)) {
@@ -2939,65 +2939,24 @@
                        zend_hash_move_forward(Z_ARRVAL_PP(subject));
                }
        } else {        /* if subject is not an array */
-               php_str_replace_in_subject(*search, *replace, subject, return_value, 
1);
+               php_str_replace_in_subject(*search, *replace, subject, return_value, 
+case_sensitivity);
        }       
 }
 /* }}} */
 
+/* {{{ proto mixed str_replace(mixed search, mixed replace, mixed subject)
+   Replaces all occurrences of search in haystack with replace */
+PHP_FUNCTION(str_replace)
+{
+       php_str_replace_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
+}
+/* }}} */
+
 /* {{{ proto mixed str_ireplace(mixed search, mixed replace, mixed subject)
    Replaces all occurrences of search in haystack with replace / case-insensitive */
 PHP_FUNCTION(str_ireplace)
 {
-       zval **subject, **search, **replace, **subject_entry;
-       zval *result;
-       char *string_key;
-       uint string_key_len;
-       ulong num_key;
-
-       if (ZEND_NUM_ARGS() != 3 ||
-          zend_get_parameters_ex(3, &search, &replace, &subject) == FAILURE) {
-               WRONG_PARAM_COUNT;
-       }
-
-       SEPARATE_ZVAL(search);
-       SEPARATE_ZVAL(replace);
-       SEPARATE_ZVAL(subject);
-
-       /* Make sure we're dealing with strings and do the replacement. */
-       if (Z_TYPE_PP(search) != IS_ARRAY) {
-               convert_to_string_ex(search);
-               convert_to_string_ex(replace);
-       } else if (Z_TYPE_PP(replace) != IS_ARRAY) {
-               convert_to_string_ex(replace);
-       }
-
-       /* if subject is an array */
-       if (Z_TYPE_PP(subject) == IS_ARRAY) {
-               array_init(return_value);
-               zend_hash_internal_pointer_reset(Z_ARRVAL_PP(subject));
-
-               /* For each subject entry, convert it to string, then perform 
replacement
-                  and add the result to the return_value array. */
-               while (zend_hash_get_current_data(Z_ARRVAL_PP(subject), (void 
**)&subject_entry) == SUCCESS) {
-                       MAKE_STD_ZVAL(result);
-                       php_str_replace_in_subject(*search, *replace, subject_entry, 
result, 0);
-                       /* Add to return array */
-                       switch (zend_hash_get_current_key_ex(Z_ARRVAL_PP(subject), 
&string_key,
-                                                                                      
         &string_key_len, &num_key, 0, NULL)) {
-                               case HASH_KEY_IS_STRING:
-                                       add_assoc_zval_ex(return_value, string_key, 
string_key_len, result);
-                                       break;
-
-                               case HASH_KEY_IS_LONG:
-                                       add_index_zval(return_value, num_key, result);
-                                       break;
-                       }
-               
-                       zend_hash_move_forward(Z_ARRVAL_PP(subject));
-               }
-       } else {        /* if subject is not an array */
-               php_str_replace_in_subject(*search, *replace, subject, return_value, 
0);
-       }       
+       php_str_replace_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
 }
 /* }}} */
 



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

Reply via email to