summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikita Popov <nikita.ppv@gmail.com>2020-04-14 17:17:38 +0200
committerNikita Popov <nikita.ppv@gmail.com>2020-04-14 17:18:05 +0200
commit4fb705a03dc73bfafbd81bd1ba23b30b0ec2a502 (patch)
tree4beeeb15cfc1f13918c0ca560c9346320795ad12
parent489a51bff077671e77f29b01f2575c54c1ef452b (diff)
downloadphp-git-4fb705a03dc73bfafbd81bd1ba23b30b0ec2a502.tar.gz
Add zend_string_concat2 API
-rw-r--r--Zend/zend_API.c8
-rw-r--r--Zend/zend_compile.c13
-rw-r--r--Zend/zend_string.c14
-rw-r--r--Zend/zend_string.h3
-rw-r--r--ext/ffi/ffi.c8
-rw-r--r--ext/pcre/php_pcre.c6
6 files changed, 28 insertions, 24 deletions
diff --git a/Zend/zend_API.c b/Zend/zend_API.c
index cbd89f7c0c..ea78c22a46 100644
--- a/Zend/zend_API.c
+++ b/Zend/zend_API.c
@@ -3137,11 +3137,9 @@ try_again:
case IS_OBJECT:
{
zend_class_entry *ce = Z_OBJCE_P(callable);
- zend_string *callable_name = zend_string_alloc(
- ZSTR_LEN(ce->name) + sizeof("::__invoke") - 1, 0);
- memcpy(ZSTR_VAL(callable_name), ZSTR_VAL(ce->name), ZSTR_LEN(ce->name));
- memcpy(ZSTR_VAL(callable_name) + ZSTR_LEN(ce->name), "::__invoke", sizeof("::__invoke"));
- return callable_name;
+ return zend_string_concat2(
+ ZSTR_VAL(ce->name), ZSTR_LEN(ce->name),
+ "::__invoke", sizeof("::__invoke") - 1);
}
case IS_REFERENCE:
callable = Z_REFVAL_P(callable);
diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index 1c7f5f1073..953bf6dfd5 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -1161,12 +1161,8 @@ static zend_string *add_type_string(zend_string *type, zend_string *new_type) {
return zend_string_copy(new_type);
}
- // TODO: Switch to smart_str?
- result = zend_string_alloc(ZSTR_LEN(type) + ZSTR_LEN(new_type) + 1, 0);
- memcpy(ZSTR_VAL(result), ZSTR_VAL(type), ZSTR_LEN(type));
- ZSTR_VAL(result)[ZSTR_LEN(type)] = '|';
- memcpy(ZSTR_VAL(result) + ZSTR_LEN(type) + 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
- ZSTR_VAL(result)[ZSTR_LEN(type) + ZSTR_LEN(new_type) + 1] = '\0';
+ result = zend_string_concat3(
+ ZSTR_VAL(type), ZSTR_LEN(type), "|", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
zend_string_release(type);
return result;
}
@@ -1243,10 +1239,7 @@ zend_string *zend_type_to_string_resolved(zend_type type, zend_class_entry *scop
if (type_mask & MAY_BE_NULL) {
zend_bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL;
if (!is_union) {
- zend_string *nullable_str = zend_string_alloc(ZSTR_LEN(str) + 1, 0);
- ZSTR_VAL(nullable_str)[0] = '?';
- memcpy(ZSTR_VAL(nullable_str) + 1, ZSTR_VAL(str), ZSTR_LEN(str));
- ZSTR_VAL(nullable_str)[ZSTR_LEN(nullable_str)] = '\0';
+ zend_string *nullable_str = zend_string_concat2("?", 1, ZSTR_VAL(str), ZSTR_LEN(str));
zend_string_release(str);
return nullable_str;
}
diff --git a/Zend/zend_string.c b/Zend/zend_string.c
index 3caeeb7a9a..7eae674f08 100644
--- a/Zend/zend_string.c
+++ b/Zend/zend_string.c
@@ -462,6 +462,20 @@ ZEND_API zend_bool ZEND_FASTCALL I_WRAP_SONAME_FNNAME_ZU(NONE,zend_string_equal_
#endif
+ZEND_API zend_string *zend_string_concat2(
+ const char *str1, size_t str1_len,
+ const char *str2, size_t str2_len)
+{
+ size_t len = str1_len + str2_len;
+ zend_string *res = zend_string_alloc(len, 0);
+
+ memcpy(ZSTR_VAL(res), str1, str1_len);
+ memcpy(ZSTR_VAL(res) + str1_len, str2, str2_len);
+ ZSTR_VAL(res)[len] = '\0';
+
+ return res;
+}
+
ZEND_API zend_string *zend_string_concat3(
const char *str1, size_t str1_len,
const char *str2, size_t str2_len,
diff --git a/Zend/zend_string.h b/Zend/zend_string.h
index bb75d816c4..45ef3cbb01 100644
--- a/Zend/zend_string.h
+++ b/Zend/zend_string.h
@@ -34,6 +34,9 @@ ZEND_API zend_ulong ZEND_FASTCALL zend_string_hash_func(zend_string *str);
ZEND_API zend_ulong ZEND_FASTCALL zend_hash_func(const char *str, size_t len);
ZEND_API zend_string* ZEND_FASTCALL zend_interned_string_find_permanent(zend_string *str);
+ZEND_API zend_string *zend_string_concat2(
+ const char *str1, size_t str1_len,
+ const char *str2, size_t str2_len);
ZEND_API zend_string *zend_string_concat3(
const char *str1, size_t str1_len,
const char *str2, size_t str2_len,
diff --git a/ext/ffi/ffi.c b/ext/ffi/ffi.c
index 95ae8e3711..ba1130a64e 100644
--- a/ext/ffi/ffi.c
+++ b/ext/ffi/ffi.c
@@ -1605,12 +1605,8 @@ static zend_string *zend_ffi_get_class_name(zend_string *prefix, const zend_ffi_
if (!zend_ffi_ctype_name(&buf, type)) {
return zend_string_copy(prefix);
} else {
- zend_string *name = zend_string_alloc(ZSTR_LEN(prefix) + 1 + buf.end - buf.start, 0);
- memcpy(ZSTR_VAL(name), ZSTR_VAL(prefix), ZSTR_LEN(prefix));
- ZSTR_VAL(name)[ZSTR_LEN(prefix)] = ':';
- memcpy(ZSTR_VAL(name) + ZSTR_LEN(prefix) + 1, buf.start, buf.end - buf.start);
- ZSTR_VAL(name)[ZSTR_LEN(name)] = 0;
- return name;
+ return zend_string_concat3(
+ ZSTR_VAL(prefix), ZSTR_LEN(prefix), ":", 1, buf.start, buf.end - buf.start);
}
}
/* }}} */
diff --git a/ext/pcre/php_pcre.c b/ext/pcre/php_pcre.c
index 26ed8b43e7..a5e806b30c 100644
--- a/ext/pcre/php_pcre.c
+++ b/ext/pcre/php_pcre.c
@@ -597,9 +597,9 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, in
if (locale_aware && BG(locale_string) &&
(ZSTR_LEN(BG(locale_string)) != 1 && ZSTR_VAL(BG(locale_string))[0] != 'C')) {
- key = zend_string_alloc(ZSTR_LEN(regex) + ZSTR_LEN(BG(locale_string)) + 1, 0);
- memcpy(ZSTR_VAL(key), ZSTR_VAL(BG(locale_string)), ZSTR_LEN(BG(locale_string)) + 1);
- memcpy(ZSTR_VAL(key) + ZSTR_LEN(BG(locale_string)), ZSTR_VAL(regex), ZSTR_LEN(regex) + 1);
+ key = zend_string_concat2(
+ ZSTR_VAL(BG(locale_string)), ZSTR_LEN(BG(locale_string)),
+ ZSTR_VAL(regex), ZSTR_LEN(regex));
} else {
key = regex;
}