Skip to content

Commit 90db6f2

Browse files
morrisonlevicmb69
authored andcommitted
Add case insensitive find_ptr hash functions
- zend_hash_find_ptr_lc(ht, zend_string *key) - zend_hash_str_find_ptr_lc(ht, const char *str, size_t len) Note that zend_hash_str_find_ptr_lc used to exist in zend_compile.c as zend_hash_find_ptr_lc. When exporting this I figured it was best to use the same conventions as the rest of zend_hash.h.
1 parent e2f1586 commit 90db6f2

File tree

3 files changed

+41
-25
lines changed

3 files changed

+41
-25
lines changed

Zend/zend_compile.c

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -272,23 +272,9 @@ static zend_always_inline zend_bool zend_is_confusable_type(const zend_string *n
272272
}
273273
/* }}} */
274274

275-
static void *zend_hash_find_ptr_lc(HashTable *ht, const char *str, size_t len) {
276-
void *result;
277-
zend_string *lcname;
278-
ALLOCA_FLAG(use_heap);
279-
280-
ZSTR_ALLOCA_ALLOC(lcname, len, use_heap);
281-
zend_str_tolower_copy(ZSTR_VAL(lcname), str, len);
282-
result = zend_hash_find_ptr(ht, lcname);
283-
ZSTR_ALLOCA_FREE(lcname, use_heap);
284-
285-
return result;
286-
}
287-
288275
static zend_bool zend_is_not_imported(zend_string *name) {
289276
/* Assuming "name" is unqualified here. */
290-
return !FC(imports)
291-
|| zend_hash_find_ptr_lc(FC(imports), ZSTR_VAL(name), ZSTR_LEN(name)) == NULL;
277+
return !FC(imports) || zend_hash_find_ptr_lc(FC(imports), name) == NULL;
292278
}
293279

294280
void zend_oparray_context_begin(zend_oparray_context *prev_context) /* {{{ */
@@ -901,7 +887,7 @@ zend_string *zend_resolve_non_class_name(
901887
if (case_sensitive) {
902888
import_name = zend_hash_find_ptr(current_import_sub, name);
903889
} else {
904-
import_name = zend_hash_find_ptr_lc(current_import_sub, ZSTR_VAL(name), ZSTR_LEN(name));
890+
import_name = zend_hash_find_ptr_lc(current_import_sub, name);
905891
}
906892

907893
if (import_name) {
@@ -918,7 +904,7 @@ zend_string *zend_resolve_non_class_name(
918904
if (compound && FC(imports)) {
919905
/* If the first part of a qualified name is an alias, substitute it. */
920906
size_t len = compound - ZSTR_VAL(name);
921-
zend_string *import_name = zend_hash_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
907+
zend_string *import_name = zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
922908

923909
if (import_name) {
924910
return zend_concat_names(
@@ -971,7 +957,7 @@ zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */
971957
/* If the first part of a qualified name is an alias, substitute it. */
972958
size_t len = compound - ZSTR_VAL(name);
973959
zend_string *import_name =
974-
zend_hash_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
960+
zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
975961

976962
if (import_name) {
977963
return zend_concat_names(
@@ -980,7 +966,7 @@ zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */
980966
} else {
981967
/* If an unqualified name is an alias, replace it. */
982968
zend_string *import_name
983-
= zend_hash_find_ptr_lc(FC(imports), ZSTR_VAL(name), ZSTR_LEN(name));
969+
= zend_hash_find_ptr_lc(FC(imports), name);
984970

985971
if (import_name) {
986972
return zend_string_copy(import_name);
@@ -1613,7 +1599,7 @@ static zend_bool zend_verify_ct_const_access(zend_class_constant *c, zend_class_
16131599
if (ce->ce_flags & ZEND_ACC_RESOLVED_PARENT) {
16141600
ce = ce->parent;
16151601
} else {
1616-
ce = zend_hash_find_ptr_lc(CG(class_table), ZSTR_VAL(ce->parent_name), ZSTR_LEN(ce->parent_name));
1602+
ce = zend_hash_find_ptr_lc(CG(class_table), ce->parent_name);
16171603
if (!ce) {
16181604
break;
16191605
}
@@ -1633,7 +1619,7 @@ static zend_bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name,
16331619
if (class_name_refers_to_active_ce(class_name, fetch_type)) {
16341620
cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name);
16351621
} else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1636-
zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), ZSTR_VAL(class_name), ZSTR_LEN(class_name));
1622+
zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), class_name);
16371623
if (ce) {
16381624
cc = zend_hash_find_ptr(&ce->constants_table, name);
16391625
} else {
@@ -6191,8 +6177,8 @@ static void zend_begin_func_decl(znode *result, zend_op_array *op_array, zend_as
61916177
lcname = zend_string_tolower(name);
61926178

61936179
if (FC(imports_function)) {
6194-
zend_string *import_name = zend_hash_find_ptr_lc(
6195-
FC(imports_function), ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name));
6180+
zend_string *import_name =
6181+
zend_hash_find_ptr_lc(FC(imports_function), unqualified_name);
61966182
if (import_name && !zend_string_equals_ci(lcname, import_name)) {
61976183
zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare function %s "
61986184
"because the name is already in use", ZSTR_VAL(name));
@@ -6657,8 +6643,8 @@ void zend_compile_class_decl(znode *result, zend_ast *ast, zend_bool toplevel) /
66576643
lcname = zend_string_tolower(name);
66586644

66596645
if (FC(imports)) {
6660-
zend_string *import_name = zend_hash_find_ptr_lc(
6661-
FC(imports), ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name));
6646+
zend_string *import_name =
6647+
zend_hash_find_ptr_lc(FC(imports), unqualified_name);
66626648
if (import_name && !zend_string_equals_ci(lcname, import_name)) {
66636649
zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare class %s "
66646650
"because the name is already in use", ZSTR_VAL(name));

Zend/zend_hash.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,28 @@ static void _zend_is_inconsistent(const HashTable *ht, const char *file, int lin
8484
zend_hash_do_resize(ht); \
8585
}
8686

87+
ZEND_API void *zend_hash_str_find_ptr_lc(const HashTable *ht, const char *str, size_t len) {
88+
void *result;
89+
char *lc_str;
90+
91+
/* Stack allocate small strings to improve performance */
92+
ALLOCA_FLAG(use_heap)
93+
94+
lc_str = zend_str_tolower_copy(do_alloca(len + 1, use_heap), str, len);
95+
result = zend_hash_str_find_ptr(ht, lc_str, len);
96+
free_alloca(lc_str, use_heap);
97+
98+
return result;
99+
}
100+
101+
ZEND_API void *zend_hash_find_ptr_lc(const HashTable *ht, zend_string *key) {
102+
void *result;
103+
zend_string *lc_key = zend_string_tolower(key);
104+
result = zend_hash_find_ptr(ht, lc_key);
105+
zend_string_release(lc_key);
106+
return result;
107+
}
108+
87109
static void ZEND_FASTCALL zend_hash_do_resize(HashTable *ht);
88110

89111
static zend_always_inline uint32_t zend_hash_check_size(uint32_t nSize)

Zend/zend_hash.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,14 @@ static zend_always_inline void *zend_hash_str_find_ptr(const HashTable *ht, cons
847847
}
848848
}
849849

850+
/* Will lowercase the str; use only if you don't need the lowercased string for
851+
* anything else. If you have a lowered string, use zend_hash_str_find_ptr. */
852+
ZEND_API void *zend_hash_str_find_ptr_lc(const HashTable *ht, const char *str, size_t len);
853+
854+
/* Will lowercase the str; use only if you don't need the lowercased string for
855+
* anything else. If you have a lowered string, use zend_hash_find_ptr. */
856+
ZEND_API void *zend_hash_find_ptr_lc(const HashTable *ht, zend_string *key);
857+
850858
static zend_always_inline void *zend_hash_index_find_ptr(const HashTable *ht, zend_ulong h)
851859
{
852860
zval *zv;

0 commit comments

Comments
 (0)