Skip to content

Commit 4fb705a

Browse files
committed
Add zend_string_concat2 API
1 parent 489a51b commit 4fb705a

File tree

6 files changed

+28
-24
lines changed

6 files changed

+28
-24
lines changed

Zend/zend_API.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3137,11 +3137,9 @@ ZEND_API zend_string *zend_get_callable_name_ex(zval *callable, zend_object *obj
31373137
case IS_OBJECT:
31383138
{
31393139
zend_class_entry *ce = Z_OBJCE_P(callable);
3140-
zend_string *callable_name = zend_string_alloc(
3141-
ZSTR_LEN(ce->name) + sizeof("::__invoke") - 1, 0);
3142-
memcpy(ZSTR_VAL(callable_name), ZSTR_VAL(ce->name), ZSTR_LEN(ce->name));
3143-
memcpy(ZSTR_VAL(callable_name) + ZSTR_LEN(ce->name), "::__invoke", sizeof("::__invoke"));
3144-
return callable_name;
3140+
return zend_string_concat2(
3141+
ZSTR_VAL(ce->name), ZSTR_LEN(ce->name),
3142+
"::__invoke", sizeof("::__invoke") - 1);
31453143
}
31463144
case IS_REFERENCE:
31473145
callable = Z_REFVAL_P(callable);

Zend/zend_compile.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,12 +1161,8 @@ static zend_string *add_type_string(zend_string *type, zend_string *new_type) {
11611161
return zend_string_copy(new_type);
11621162
}
11631163

1164-
// TODO: Switch to smart_str?
1165-
result = zend_string_alloc(ZSTR_LEN(type) + ZSTR_LEN(new_type) + 1, 0);
1166-
memcpy(ZSTR_VAL(result), ZSTR_VAL(type), ZSTR_LEN(type));
1167-
ZSTR_VAL(result)[ZSTR_LEN(type)] = '|';
1168-
memcpy(ZSTR_VAL(result) + ZSTR_LEN(type) + 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1169-
ZSTR_VAL(result)[ZSTR_LEN(type) + ZSTR_LEN(new_type) + 1] = '\0';
1164+
result = zend_string_concat3(
1165+
ZSTR_VAL(type), ZSTR_LEN(type), "|", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
11701166
zend_string_release(type);
11711167
return result;
11721168
}
@@ -1243,10 +1239,7 @@ zend_string *zend_type_to_string_resolved(zend_type type, zend_class_entry *scop
12431239
if (type_mask & MAY_BE_NULL) {
12441240
zend_bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL;
12451241
if (!is_union) {
1246-
zend_string *nullable_str = zend_string_alloc(ZSTR_LEN(str) + 1, 0);
1247-
ZSTR_VAL(nullable_str)[0] = '?';
1248-
memcpy(ZSTR_VAL(nullable_str) + 1, ZSTR_VAL(str), ZSTR_LEN(str));
1249-
ZSTR_VAL(nullable_str)[ZSTR_LEN(nullable_str)] = '\0';
1242+
zend_string *nullable_str = zend_string_concat2("?", 1, ZSTR_VAL(str), ZSTR_LEN(str));
12501243
zend_string_release(str);
12511244
return nullable_str;
12521245
}

Zend/zend_string.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,20 @@ ZEND_API zend_bool ZEND_FASTCALL I_WRAP_SONAME_FNNAME_ZU(NONE,zend_string_equal_
462462

463463
#endif
464464

465+
ZEND_API zend_string *zend_string_concat2(
466+
const char *str1, size_t str1_len,
467+
const char *str2, size_t str2_len)
468+
{
469+
size_t len = str1_len + str2_len;
470+
zend_string *res = zend_string_alloc(len, 0);
471+
472+
memcpy(ZSTR_VAL(res), str1, str1_len);
473+
memcpy(ZSTR_VAL(res) + str1_len, str2, str2_len);
474+
ZSTR_VAL(res)[len] = '\0';
475+
476+
return res;
477+
}
478+
465479
ZEND_API zend_string *zend_string_concat3(
466480
const char *str1, size_t str1_len,
467481
const char *str2, size_t str2_len,

Zend/zend_string.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ ZEND_API zend_ulong ZEND_FASTCALL zend_string_hash_func(zend_string *str);
3434
ZEND_API zend_ulong ZEND_FASTCALL zend_hash_func(const char *str, size_t len);
3535
ZEND_API zend_string* ZEND_FASTCALL zend_interned_string_find_permanent(zend_string *str);
3636

37+
ZEND_API zend_string *zend_string_concat2(
38+
const char *str1, size_t str1_len,
39+
const char *str2, size_t str2_len);
3740
ZEND_API zend_string *zend_string_concat3(
3841
const char *str1, size_t str1_len,
3942
const char *str2, size_t str2_len,

ext/ffi/ffi.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,12 +1605,8 @@ static zend_string *zend_ffi_get_class_name(zend_string *prefix, const zend_ffi_
16051605
if (!zend_ffi_ctype_name(&buf, type)) {
16061606
return zend_string_copy(prefix);
16071607
} else {
1608-
zend_string *name = zend_string_alloc(ZSTR_LEN(prefix) + 1 + buf.end - buf.start, 0);
1609-
memcpy(ZSTR_VAL(name), ZSTR_VAL(prefix), ZSTR_LEN(prefix));
1610-
ZSTR_VAL(name)[ZSTR_LEN(prefix)] = ':';
1611-
memcpy(ZSTR_VAL(name) + ZSTR_LEN(prefix) + 1, buf.start, buf.end - buf.start);
1612-
ZSTR_VAL(name)[ZSTR_LEN(name)] = 0;
1613-
return name;
1608+
return zend_string_concat3(
1609+
ZSTR_VAL(prefix), ZSTR_LEN(prefix), ":", 1, buf.start, buf.end - buf.start);
16141610
}
16151611
}
16161612
/* }}} */

ext/pcre/php_pcre.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,9 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, in
597597

598598
if (locale_aware && BG(locale_string) &&
599599
(ZSTR_LEN(BG(locale_string)) != 1 && ZSTR_VAL(BG(locale_string))[0] != 'C')) {
600-
key = zend_string_alloc(ZSTR_LEN(regex) + ZSTR_LEN(BG(locale_string)) + 1, 0);
601-
memcpy(ZSTR_VAL(key), ZSTR_VAL(BG(locale_string)), ZSTR_LEN(BG(locale_string)) + 1);
602-
memcpy(ZSTR_VAL(key) + ZSTR_LEN(BG(locale_string)), ZSTR_VAL(regex), ZSTR_LEN(regex) + 1);
600+
key = zend_string_concat2(
601+
ZSTR_VAL(BG(locale_string)), ZSTR_LEN(BG(locale_string)),
602+
ZSTR_VAL(regex), ZSTR_LEN(regex));
603603
} else {
604604
key = regex;
605605
}

0 commit comments

Comments
 (0)