Add zend_string_concat2 API

This commit is contained in:
Nikita Popov 2020-04-14 17:17:38 +02:00
parent 489a51bff0
commit 4fb705a03d
6 changed files with 28 additions and 24 deletions

View file

@ -3137,11 +3137,9 @@ try_again:
case IS_OBJECT: case IS_OBJECT:
{ {
zend_class_entry *ce = Z_OBJCE_P(callable); zend_class_entry *ce = Z_OBJCE_P(callable);
zend_string *callable_name = zend_string_alloc( return zend_string_concat2(
ZSTR_LEN(ce->name) + sizeof("::__invoke") - 1, 0); ZSTR_VAL(ce->name), ZSTR_LEN(ce->name),
memcpy(ZSTR_VAL(callable_name), ZSTR_VAL(ce->name), ZSTR_LEN(ce->name)); "::__invoke", sizeof("::__invoke") - 1);
memcpy(ZSTR_VAL(callable_name) + ZSTR_LEN(ce->name), "::__invoke", sizeof("::__invoke"));
return callable_name;
} }
case IS_REFERENCE: case IS_REFERENCE:
callable = Z_REFVAL_P(callable); callable = Z_REFVAL_P(callable);

View file

@ -1161,12 +1161,8 @@ static zend_string *add_type_string(zend_string *type, zend_string *new_type) {
return zend_string_copy(new_type); return zend_string_copy(new_type);
} }
// TODO: Switch to smart_str? result = zend_string_concat3(
result = zend_string_alloc(ZSTR_LEN(type) + ZSTR_LEN(new_type) + 1, 0); ZSTR_VAL(type), ZSTR_LEN(type), "|", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
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';
zend_string_release(type); zend_string_release(type);
return result; 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) { if (type_mask & MAY_BE_NULL) {
zend_bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL; zend_bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL;
if (!is_union) { if (!is_union) {
zend_string *nullable_str = zend_string_alloc(ZSTR_LEN(str) + 1, 0); zend_string *nullable_str = zend_string_concat2("?", 1, ZSTR_VAL(str), ZSTR_LEN(str));
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_release(str); zend_string_release(str);
return nullable_str; return nullable_str;
} }

View file

@ -462,6 +462,20 @@ ZEND_API zend_bool ZEND_FASTCALL I_WRAP_SONAME_FNNAME_ZU(NONE,zend_string_equal_
#endif #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( ZEND_API zend_string *zend_string_concat3(
const char *str1, size_t str1_len, const char *str1, size_t str1_len,
const char *str2, size_t str2_len, const char *str2, size_t str2_len,

View file

@ -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_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_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( ZEND_API zend_string *zend_string_concat3(
const char *str1, size_t str1_len, const char *str1, size_t str1_len,
const char *str2, size_t str2_len, const char *str2, size_t str2_len,

View file

@ -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)) { if (!zend_ffi_ctype_name(&buf, type)) {
return zend_string_copy(prefix); return zend_string_copy(prefix);
} else { } else {
zend_string *name = zend_string_alloc(ZSTR_LEN(prefix) + 1 + buf.end - buf.start, 0); return zend_string_concat3(
memcpy(ZSTR_VAL(name), ZSTR_VAL(prefix), ZSTR_LEN(prefix)); ZSTR_VAL(prefix), ZSTR_LEN(prefix), ":", 1, buf.start, buf.end - buf.start);
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;
} }
} }
/* }}} */ /* }}} */

View file

@ -597,9 +597,9 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, in
if (locale_aware && BG(locale_string) && if (locale_aware && BG(locale_string) &&
(ZSTR_LEN(BG(locale_string)) != 1 && ZSTR_VAL(BG(locale_string))[0] != 'C')) { (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); key = zend_string_concat2(
memcpy(ZSTR_VAL(key), ZSTR_VAL(BG(locale_string)), ZSTR_LEN(BG(locale_string)) + 1); ZSTR_VAL(BG(locale_string)), ZSTR_LEN(BG(locale_string)),
memcpy(ZSTR_VAL(key) + ZSTR_LEN(BG(locale_string)), ZSTR_VAL(regex), ZSTR_LEN(regex) + 1); ZSTR_VAL(regex), ZSTR_LEN(regex));
} else { } else {
key = regex; key = regex;
} }