]> granicus.if.org Git - php/commitdiff
Add zend_create_member_string() API
authorNikita Popov <nikita.ppv@gmail.com>
Tue, 14 Apr 2020 14:52:13 +0000 (16:52 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Tue, 14 Apr 2020 14:52:13 +0000 (16:52 +0200)
This is a recurring pattern.

Zend/zend_API.c
Zend/zend_compile.c
Zend/zend_compile.h
ext/opcache/Optimizer/compact_literals.c

index 1b132d64c90dca5774fdf503b3dec6662dcd21fa..6dab0d3f292cd3f6bb7cff7d85f2e3930fea8249 100644 (file)
@@ -3102,25 +3102,13 @@ get_function_via_handler:
 }
 /* }}} */
 
-static zend_string *zend_create_method_string(zend_string *class_name, zend_string *method_name) {
-       zend_string *callable_name = zend_string_alloc(
-               ZSTR_LEN(class_name) + ZSTR_LEN(method_name) + sizeof("::") - 1, 0);
-       char *ptr = ZSTR_VAL(callable_name);
-       memcpy(ptr, ZSTR_VAL(class_name), ZSTR_LEN(class_name));
-       ptr += ZSTR_LEN(class_name);
-       memcpy(ptr, "::", sizeof("::") - 1);
-       ptr += sizeof("::") - 1;
-       memcpy(ptr, ZSTR_VAL(method_name), ZSTR_LEN(method_name) + 1);
-       return callable_name;
-}
-
 ZEND_API zend_string *zend_get_callable_name_ex(zval *callable, zend_object *object) /* {{{ */
 {
 try_again:
        switch (Z_TYPE_P(callable)) {
                case IS_STRING:
                        if (object) {
-                               return zend_create_method_string(object->ce->name, Z_STR_P(callable));
+                               return zend_create_member_string(object->ce->name, Z_STR_P(callable));
                        }
                        return zend_string_copy(Z_STR_P(callable));
 
@@ -3139,9 +3127,9 @@ try_again:
                        }
 
                        if (Z_TYPE_P(obj) == IS_STRING) {
-                               return zend_create_method_string(Z_STR_P(obj), Z_STR_P(method));
+                               return zend_create_member_string(Z_STR_P(obj), Z_STR_P(method));
                        } else if (Z_TYPE_P(obj) == IS_OBJECT) {
-                               return zend_create_method_string(Z_OBJCE_P(obj)->name, Z_STR_P(method));
+                               return zend_create_member_string(Z_OBJCE_P(obj)->name, Z_STR_P(method));
                        } else {
                                return ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
                        }
index 1f7ee48beef23df59debfe83533a897cdcfd4643..1c7f5f1073eadde1ab30b4ac491a21093f589f0d 100644 (file)
@@ -852,6 +852,13 @@ uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
 }
 /* }}} */
 
+ZEND_API zend_string *zend_create_member_string(zend_string *class_name, zend_string *member_name) {
+       return zend_string_concat3(
+               ZSTR_VAL(class_name), ZSTR_LEN(class_name),
+               "::", sizeof("::") - 1,
+               ZSTR_VAL(member_name), ZSTR_LEN(member_name));
+}
+
 zend_string *zend_concat_names(char *name1, size_t name1_len, char *name2, size_t name2_len) {
        return zend_string_concat3(name1, name1_len, "\\", 1, name2, name2_len);
 }
@@ -7160,10 +7167,8 @@ static zend_bool zend_try_ct_eval_magic_const(zval *zv, zend_ast *ast) /* {{{ */
                        }
                        if (op_array && op_array->function_name) {
                                if (op_array->scope) {
-                                       ZVAL_NEW_STR(zv, zend_string_concat3(
-                                               ZSTR_VAL(op_array->scope->name), ZSTR_LEN(op_array->scope->name),
-                                               "::", 2,
-                                               ZSTR_VAL(op_array->function_name), ZSTR_LEN(op_array->function_name)));
+                                       ZVAL_NEW_STR(zv,
+                                               zend_create_member_string(op_array->scope->name, op_array->function_name));
                                } else {
                                        ZVAL_STR_COPY(zv, op_array->function_name);
                                }
@@ -8553,8 +8558,7 @@ void zend_compile_const_expr_class_const(zend_ast **ast_ptr) /* {{{ */
                zend_string_addref(class_name);
        }
 
-       name = zend_string_concat3(
-               ZSTR_VAL(class_name), ZSTR_LEN(class_name), "::", 2, ZSTR_VAL(const_name), ZSTR_LEN(const_name));
+       name = zend_create_member_string(class_name, const_name);
 
        zend_ast_destroy(ast);
        zend_string_release_ex(class_name, 0);
index 5dd4c39d6aebc3bd6b6d6ec24e5e865b266b71e1..b925c809db2e27cadfc423bfb8ef05340517a048 100644 (file)
@@ -783,6 +783,7 @@ ZEND_API void zend_destroy_file_handle(zend_file_handle *file_handle);
 ZEND_API void zend_cleanup_internal_class_data(zend_class_entry *ce);
 ZEND_API void zend_cleanup_internal_classes(void);
 ZEND_API void zend_type_release(zend_type type, zend_bool persistent);
+ZEND_API zend_string *zend_create_member_string(zend_string *class_name, zend_string *member_name);
 
 
 ZEND_API ZEND_COLD void zend_user_exception_handler(void);
index f6f32e45145fe64e11b61c2127681241ad8641a9..f29262bd46b7890e4bbb2a46fb8cffbcb7e8e4a2 100644 (file)
@@ -96,11 +96,7 @@ static uint32_t add_static_slot(HashTable     *hash,
        zval *prop_name = &op_array->literals[op2];
        zval *pos, tmp;
 
-       zend_string *key = zend_string_concat3(
-               Z_STRVAL_P(class_name), Z_STRLEN_P(class_name),
-               "::", sizeof("::") - 1,
-               Z_STRVAL_P(prop_name), Z_STRLEN_P(prop_name));
-
+       zend_string *key = zend_create_member_string(Z_STR_P(class_name), Z_STR_P(prop_name));
        ZSTR_H(key) = zend_string_hash_func(key);
        ZSTR_H(key) += kind;