]> granicus.if.org Git - php/commitdiff
Refactor php_str_to_str returning zend_string
authorXinchen Hui <laruence@gmail.com>
Sun, 23 Feb 2014 11:25:26 +0000 (19:25 +0800)
committerXinchen Hui <laruence@gmail.com>
Sun, 23 Feb 2014 11:25:26 +0000 (19:25 +0800)
ext/standard/php_string.h
ext/standard/string.c
ext/standard/var.c

index 2209e158b9f48fdb2b94dc9f07fdde609e4183ad..2076eff272d77e342f96ffeeb3c32a7a009e486f 100644 (file)
@@ -128,10 +128,10 @@ PHPAPI void php_stripcslashes(char *str, int *len);
 PHPAPI zend_string *php_basename(const char *s, size_t len, char *suffix, size_t sufflen TSRMLS_DC);
 PHPAPI size_t php_dirname(char *str, size_t len);
 PHPAPI char *php_stristr(char *s, char *t, size_t s_len, size_t t_len);
-PHPAPI char *php_str_to_str_ex(char *haystack, int length, char *needle,
-               int needle_len, char *str, int str_len, int *_new_length, int case_sensitivity, int *replace_count);
-PHPAPI char *php_str_to_str(char *haystack, int length, char *needle,
-               int needle_len, char *str, int str_len, int *_new_length);
+PHPAPI zend_string *php_str_to_str_ex(char *haystack, int length, char *needle,
+               int needle_len, char *str, int str_len, int case_sensitivity, int *replace_count);
+PHPAPI zend_string *php_str_to_str(char *haystack, int length, char *needle,
+               int needle_len, char *str, int str_len);
 PHPAPI char *php_trim(char *c, int len, char *what, int what_len, zval *return_value, int mode TSRMLS_DC);
 PHPAPI size_t php_strip_tags(char *rbuf, int len, int *state, char *allow, int allow_len);
 PHPAPI size_t php_strip_tags_ex(char *rbuf, int len, int *stateptr, char *allow, int allow_len, zend_bool allow_tag_spaces);
index f6de4e36efac7b9f619183dd11b28c6cdd2096bd..f16e38921892530d2a680fd5c43ccd6a06e31721 100644 (file)
@@ -3626,22 +3626,21 @@ PHPAPI int php_char_to_str(char *str, uint len, char from, char *to, int to_len,
 
 /* {{{ php_str_to_str_ex
  */
-PHPAPI char *php_str_to_str_ex(char *haystack, int length,
-       char *needle, int needle_len, char *str, int str_len, int *_new_length, int case_sensitivity, int *replace_count)
+PHPAPI zend_string *php_str_to_str_ex(char *haystack, int length,
+       char *needle, int needle_len, char *str, int str_len, int case_sensitivity, int *replace_count)
 {
-       char *new_str;
+       zend_string *new_str;
 
        if (needle_len < length) {
                char *end, *haystack_dup = NULL, *needle_dup = NULL;
                char *e, *s, *p, *r;
 
                if (needle_len == str_len) {
-                       new_str = estrndup(haystack, length);
-                       *_new_length = length;
+                       new_str = STR_INIT(haystack, length, 0);
 
                        if (case_sensitivity) {
-                               end = new_str + length;
-                               for (p = new_str; (r = (char*)php_memnstr(p, needle, needle_len, end)); p = r + needle_len) {
+                               end = new_str->val + length;
+                               for (p = new_str->val; (r = (char*)php_memnstr(p, needle, needle_len, end)); p = r + needle_len) {
                                        memcpy(r, str, str_len);
                                        if (replace_count) {
                                                (*replace_count)++;
@@ -3654,7 +3653,7 @@ PHPAPI char *php_str_to_str_ex(char *haystack, int length,
                                php_strtolower(needle_dup, needle_len);
                                end = haystack_dup + length;
                                for (p = haystack_dup; (r = (char*)php_memnstr(p, needle_dup, needle_len, end)); p = r + needle_len) {
-                                       memcpy(new_str + (r - haystack_dup), str, str_len);
+                                       memcpy(new_str->val + (r - haystack_dup), str, str_len);
                                        if (replace_count) {
                                                (*replace_count)++;
                                        }
@@ -3672,7 +3671,7 @@ PHPAPI char *php_str_to_str_ex(char *haystack, int length,
                        }
 
                        if (str_len < needle_len) {
-                               new_str = emalloc(length + 1);
+                               new_str = STR_ALLOC(length, 0);
                        } else {
                                int count = 0;
                                char *o, *n, *endp;
@@ -3698,17 +3697,14 @@ PHPAPI char *php_str_to_str_ex(char *haystack, int length,
                                        if (needle_dup) {
                                                efree(needle_dup);
                                        }
-                                       new_str = estrndup(haystack, length);
-                                       if (_new_length) {
-                                               *_new_length = length;
-                                       }
+                                       new_str = STR_INIT(haystack, length, 0);
                                        return new_str;
                                } else {
-                                       new_str = safe_emalloc(count, str_len - needle_len, length + 1);
+                                       new_str = STR_ALLOC(count * (str_len - needle_len) + length, 0);
                                }
                        }
 
-                       e = s = new_str;
+                       e = s = new_str->val;
 
                        if (case_sensitivity) {
                                end = haystack + length;
@@ -3753,15 +3749,13 @@ PHPAPI char *php_str_to_str_ex(char *haystack, int length,
                        }
 
                        *e = '\0';
-                       *_new_length = e - s;
 
-                       new_str = erealloc(new_str, *_new_length + 1);
+                       new_str = STR_REALLOC(new_str, e - s, 0);
                        return new_str;
                }
        } else if (needle_len > length) {
 nothing_todo:
-               *_new_length = length;
-               new_str = estrndup(haystack, length);
+               new_str = STR_INIT(haystack, length, 0);
                return new_str;
        } else {
                if (case_sensitivity && memcmp(haystack, needle, length)) {
@@ -3784,8 +3778,7 @@ nothing_todo:
                        efree(l_needle);
                }
 
-               *_new_length = str_len;
-               new_str = estrndup(str, str_len);
+               new_str = STR_INIT(str, str_len, 0);
 
                if (replace_count) {
                        (*replace_count)++;
@@ -3798,10 +3791,9 @@ nothing_todo:
 
 /* {{{ php_str_to_str
  */
-PHPAPI char *php_str_to_str(char *haystack, int length,
-       char *needle, int needle_len, char *str, int str_len, int *_new_length)
+PHPAPI zend_string *php_str_to_str(char *haystack, int length, char *needle, int needle_len, char *str, int str_len)
 {
-       return php_str_to_str_ex(haystack, length, needle, needle_len, str, str_len, _new_length, 1, NULL);
+       return php_str_to_str_ex(haystack, length, needle, needle_len, str, str_len, 1, NULL);
 }
 /* }}} */
 
@@ -3880,9 +3872,9 @@ static void php_str_replace_in_subject(zval *search, zval *replace, zval *subjec
                                                                case_sensitivity,
                                                                replace_count);
                        } else if (Z_STRLEN_P(search_entry) > 1) {
-//???                          Z_STRVAL(temp_result) = php_str_to_str_ex(Z_STRVAL_P(result), Z_STRLEN_P(result),
-//???                                                                                                             Z_STRVAL_P(search_entry), Z_STRLEN_P(search_entry),
-//???                                                                                                             replace_value, replace_len, &Z_STRLEN(temp_result), case_sensitivity, replace_count);
+                               ZVAL_STR(&temp_result, php_str_to_str_ex(Z_STRVAL_P(result), Z_STRLEN_P(result),
+                                                       Z_STRVAL_P(search_entry), Z_STRLEN_P(search_entry),
+                                                       replace_value, replace_len, case_sensitivity, replace_count));
                        }
 
                        STR_FREE(Z_STR_P(result));
@@ -3905,9 +3897,9 @@ static void php_str_replace_in_subject(zval *search, zval *replace, zval *subjec
                                                        case_sensitivity,
                                                        replace_count);
                } else if (Z_STRLEN_P(search) > 1) {
-//???                  Z_STRVAL_P(result) = php_str_to_str_ex(Z_STRVAL_P(subject), Z_STRLEN_P(subject),
-//???                                                                                                  Z_STRVAL_P(search), Z_STRLEN_P(search),
-//???                                                                                                  Z_STRVAL_P(replace), Z_STRLEN_P(replace), &Z_STRLEN_P(result), case_sensitivity, replace_count);
+                       ZVAL_STR(result, php_str_to_str_ex(Z_STRVAL_P(subject), Z_STRLEN_P(subject),
+                                               Z_STRVAL_P(search), Z_STRLEN_P(search),
+                                               Z_STRVAL_P(replace), Z_STRLEN_P(replace), case_sensitivity, replace_count));
                } else {
                        ZVAL_DUP(result, subject);
                }
index 1e2555135612248eba7f0583063e8875cc0f56a8..fa67905f1288b556e48de040cda5607aeb41edac 100644 (file)
@@ -365,19 +365,18 @@ static int php_array_element_export(zval *zv TSRMLS_DC, int num_args, va_list ar
                smart_str_appendl(buf, " => ", 4);
 
        } else { /* string key */
-               char *tmp_str;
-               int tmp_len;
+               zend_string *tmp_str;
                zend_string *key = php_addcslashes(hash_key->key->val, hash_key->key->len, 0, "'\\", 2 TSRMLS_CC);
-               tmp_str = php_str_to_str_ex(key->val, key->len, "\0", 1, "' . \"\\0\" . '", 12, &tmp_len, 0, NULL);
+               tmp_str = php_str_to_str_ex(key->val, key->len, "\0", 1, "' . \"\\0\" . '", 12, 0, NULL);
 
                buffer_append_spaces(buf, level + 1);
 
                smart_str_appendc(buf, '\'');
-               smart_str_appendl(buf, tmp_str, tmp_len);
+               smart_str_appendl(buf, tmp_str->val, tmp_str->len);
                smart_str_appendl(buf, "' => ", 5);
 
-               STR_RELEASE(key);
-               efree(tmp_str);
+               STR_FREE(key);
+               STR_FREE(tmp_str);
        }
        php_var_export_ex(zv, level + 2, buf TSRMLS_CC);
 
@@ -427,7 +426,7 @@ PHPAPI void php_var_export_ex(zval *struc, int level, smart_str *buf TSRMLS_DC)
        char *tmp_str;
        int tmp_len;
        zend_string *class_name;
-       zend_string *ztmp;
+       zend_string *ztmp, *ztmp2;
 
        switch (Z_TYPE_P(struc)) {
        case IS_BOOL:
@@ -450,14 +449,14 @@ PHPAPI void php_var_export_ex(zval *struc, int level, smart_str *buf TSRMLS_DC)
                break;
        case IS_STRING:
                ztmp = php_addcslashes(Z_STRVAL_P(struc), Z_STRLEN_P(struc), 0, "'\\", 2 TSRMLS_CC);
-               tmp_str = php_str_to_str_ex(ztmp->val, ztmp->len, "\0", 1, "' . \"\\0\" . '", 12, &tmp_len, 0, NULL);
+               ztmp2 = php_str_to_str_ex(ztmp->val, ztmp->len, "\0", 1, "' . \"\\0\" . '", 12, 0, NULL);
 
                smart_str_appendc(buf, '\'');
-               smart_str_appendl(buf, tmp_str, tmp_len);
+               smart_str_appendl(buf, ztmp2->val, ztmp2->len);
                smart_str_appendc(buf, '\'');
 
-               STR_RELEASE(ztmp);
-               efree(tmp_str);
+               STR_FREE(ztmp);
+               STR_FREE(ztmp2);
                break;
        case IS_ARRAY:
                myht = Z_ARRVAL_P(struc);