Skip to content

Commit 329d668

Browse files
committed
Zend/zend_string: add zend_string_equals_str()
1 parent fe93e26 commit 329d668

10 files changed

+27
-31
lines changed

Zend/zend_attributes.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,8 @@ static zend_attribute *get_attribute_str(HashTable *attributes, const char *str,
112112
zend_attribute *attr;
113113

114114
ZEND_HASH_PACKED_FOREACH_PTR(attributes, attr) {
115-
if (attr->offset == offset && ZSTR_LEN(attr->lcname) == len) {
116-
if (0 == memcmp(ZSTR_VAL(attr->lcname), str, len)) {
117-
return attr;
118-
}
115+
if (attr->offset == offset && zend_string_equals_str(attr->lcname, str, len)) {
116+
return attr;
119117
}
120118
} ZEND_HASH_FOREACH_END();
121119
}

Zend/zend_compile.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,7 @@ static zend_always_inline bool zend_is_confusable_type(const zend_string *name,
266266
/* Intentionally using case-sensitive comparison here, because "integer" is likely intended
267267
* as a scalar type, while "Integer" is likely a class type. */
268268
for (; info->name; ++info) {
269-
if (ZSTR_LEN(name) == info->name_len
270-
&& memcmp(ZSTR_VAL(name), info->name, info->name_len) == 0
271-
) {
269+
if (zend_string_equals_str(name, info->name, info->name_len)) {
272270
*correct_name = info->correct_name;
273271
return 1;
274272
}
@@ -3378,8 +3376,7 @@ static uint32_t zend_get_arg_num(zend_function *fn, zend_string *arg_name) {
33783376
} else {
33793377
for (uint32_t i = 0; i < fn->common.num_args; i++) {
33803378
zend_internal_arg_info *arg_info = &fn->internal_function.arg_info[i];
3381-
size_t len = strlen(arg_info->name);
3382-
if (len == ZSTR_LEN(arg_name) && !memcmp(arg_info->name, ZSTR_VAL(arg_name), len)) {
3379+
if (zend_string_equals_cstr(arg_name, arg_info->name)) {
33833380
return i + 1;
33843381
}
33853382
}

Zend/zend_execute.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4674,8 +4674,7 @@ static zend_always_inline uint32_t zend_get_arg_offset_by_name(
46744674
} else {
46754675
for (uint32_t i = 0; i < num_args; i++) {
46764676
zend_internal_arg_info *arg_info = &fbc->internal_function.arg_info[i];
4677-
size_t len = strlen(arg_info->name);
4678-
if (len == ZSTR_LEN(arg_name) && !memcmp(arg_info->name, ZSTR_VAL(arg_name), len)) {
4677+
if (zend_string_equals_cstr(arg_name, arg_info->name)) {
46794678
*cache_slot = fbc;
46804679
*(uintptr_t *)(cache_slot + 1) = i;
46814680
return i;

Zend/zend_execute_API.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,8 +1817,7 @@ ZEND_API zend_result zend_set_local_var_str(const char *name, size_t len, zval *
18171817

18181818
do {
18191819
if (ZSTR_H(*str) == h &&
1820-
ZSTR_LEN(*str) == len &&
1821-
memcmp(ZSTR_VAL(*str), name, len) == 0) {
1820+
zend_string_equals_str(*str, name, len)) {
18221821
zval *var = EX_VAR_NUM(str - op_array->vars);
18231822
zval_ptr_dtor(var);
18241823
ZVAL_COPY_VALUE(var, value);

Zend/zend_hash.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -711,8 +711,7 @@ static zend_always_inline Bucket *zend_hash_str_find_bucket(const HashTable *ht,
711711
p = HT_HASH_TO_BUCKET_EX(arData, idx);
712712
if ((p->h == h)
713713
&& p->key
714-
&& (ZSTR_LEN(p->key) == len)
715-
&& !memcmp(ZSTR_VAL(p->key), str, len)) {
714+
&& zend_string_equals_str(p->key, str, len)) {
716715
return p;
717716
}
718717
idx = Z_NEXT(p->val);
@@ -1556,8 +1555,7 @@ ZEND_API zend_result ZEND_FASTCALL zend_hash_str_del_ind(HashTable *ht, const ch
15561555
p = HT_HASH_TO_BUCKET(ht, idx);
15571556
if ((p->h == h)
15581557
&& p->key
1559-
&& (ZSTR_LEN(p->key) == len)
1560-
&& !memcmp(ZSTR_VAL(p->key), str, len)) {
1558+
&& zend_string_equals_str(p->key, str, len)) {
15611559
if (Z_TYPE(p->val) == IS_INDIRECT) {
15621560
zval *data = Z_INDIRECT(p->val);
15631561

@@ -1602,8 +1600,7 @@ ZEND_API zend_result ZEND_FASTCALL zend_hash_str_del(HashTable *ht, const char *
16021600
p = HT_HASH_TO_BUCKET(ht, idx);
16031601
if ((p->h == h)
16041602
&& p->key
1605-
&& (ZSTR_LEN(p->key) == len)
1606-
&& !memcmp(ZSTR_VAL(p->key), str, len)) {
1603+
&& zend_string_equals_str(p->key, str, len)) {
16071604
zend_string_release(p->key);
16081605
p->key = NULL;
16091606
_zend_hash_del_el_ex(ht, idx, p, prev);

Zend/zend_string.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,8 @@ static zend_always_inline zend_string *zend_interned_string_ht_lookup_ex(zend_ul
135135
idx = HT_HASH(interned_strings, nIndex);
136136
while (idx != HT_INVALID_IDX) {
137137
p = HT_HASH_TO_BUCKET(interned_strings, idx);
138-
if ((p->h == h) && (ZSTR_LEN(p->key) == size)) {
139-
if (!memcmp(ZSTR_VAL(p->key), str, size)) {
140-
return p->key;
141-
}
138+
if ((p->h == h) && zend_string_equals_str(p->key, str, size)) {
139+
return p->key;
142140
}
143141
idx = Z_NEXT(p->val);
144142
}

Zend/zend_string.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,16 @@ static zend_always_inline void zend_string_release_ex(zend_string *s, bool persi
339339
}
340340
}
341341

342+
static zend_always_inline bool zend_string_equals_str(const zend_string *s1, const char *s2, size_t s2_length)
343+
{
344+
return ZSTR_LEN(s1) == s2_length && !memcmp(ZSTR_VAL(s1), s2, s2_length);
345+
}
346+
347+
static zend_always_inline bool zend_string_equals_cstr(const zend_string *s1, const char *s2)
348+
{
349+
return zend_string_equals_str(s1, s2, strlen(s2));
350+
}
351+
342352
#if defined(__GNUC__) && (defined(__i386__) || (defined(__x86_64__) && !defined(__ILP32__)))
343353
BEGIN_EXTERN_C()
344354
ZEND_API bool ZEND_FASTCALL zend_string_equal_val(zend_string *s1, zend_string *s2);
@@ -367,7 +377,7 @@ static zend_always_inline bool zend_string_equals(zend_string *s1, zend_string *
367377
(ZSTR_LEN(str) == sizeof(c) - 1 && !zend_binary_strcasecmp(ZSTR_VAL(str), ZSTR_LEN(str), (c), sizeof(c) - 1))
368378

369379
#define zend_string_equals_literal(str, literal) \
370-
(ZSTR_LEN(str) == sizeof(literal)-1 && !memcmp(ZSTR_VAL(str), literal, sizeof(literal) - 1))
380+
zend_string_equals_str(str, literal, sizeof(literal) - 1)
371381

372382
/*
373383
* DJBX33A (Daniel J. Bernstein, Times 33 with Addition)

ext/opcache/ZendAccelerator.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -556,10 +556,8 @@ static zend_always_inline zend_string *accel_find_interned_string_ex(zend_ulong
556556
if (EXPECTED(pos != STRTAB_INVALID_POS)) {
557557
do {
558558
s = STRTAB_POS_TO_STR(&ZCSG(interned_strings), pos);
559-
if (EXPECTED(ZSTR_H(s) == h) && EXPECTED(ZSTR_LEN(s) == size)) {
560-
if (!memcmp(ZSTR_VAL(s), str, size)) {
561-
return s;
562-
}
559+
if (EXPECTED(ZSTR_H(s) == h) && zend_string_equals_str(s, str, size)) {
560+
return s;
563561
}
564562
pos = STRTAB_COLLISION(s);
565563
} while (pos != STRTAB_INVALID_POS);

ext/standard/string.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4738,14 +4738,14 @@ static zend_string *try_setlocale_str(zend_long cat, zend_string *loc) {
47384738
/* C locale is represented as NULL. */
47394739
BG(ctype_string) = NULL;
47404740
return ZSTR_CHAR('C');
4741-
} else if (len == ZSTR_LEN(loc) && !memcmp(ZSTR_VAL(loc), retval, len)) {
4741+
} else if (zend_string_equals_str(loc, retval, len)) {
47424742
BG(ctype_string) = zend_string_copy(loc);
47434743
return zend_string_copy(BG(ctype_string));
47444744
} else {
47454745
BG(ctype_string) = zend_string_init(retval, len, 0);
47464746
return zend_string_copy(BG(ctype_string));
47474747
}
4748-
} else if (len == ZSTR_LEN(loc) && !memcmp(ZSTR_VAL(loc), retval, len)) {
4748+
} else if (zend_string_equals_str(loc, retval, len)) {
47494749
return zend_string_copy(loc);
47504750
}
47514751
}

main/output.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ PHPAPI int php_output_handler_started(const char *name, size_t name_len)
584584
handlers = (php_output_handler **) zend_stack_base(&OG(handlers));
585585

586586
for (i = 0; i < count; ++i) {
587-
if (name_len == ZSTR_LEN(handlers[i]->name) && !memcmp(ZSTR_VAL(handlers[i]->name), name, name_len)) {
587+
if (zend_string_equals_str(handlers[i]->name, name, name_len)) {
588588
return 1;
589589
}
590590
}

0 commit comments

Comments
 (0)