Skip to content

Commit 24a788b

Browse files
committed
Replace HashTable
Thanks for advice nielsdos
1 parent 43b28ad commit 24a788b

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

ext/mbstring/mbstring.c

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2947,14 +2947,9 @@ typedef enum {
29472947
MB_BOTH_TRIM = 3
29482948
} MB_TRIM_MODE;
29492949

2950-
static bool is_trim_wchar(uint32_t w, uint32_t *characters, size_t characters_len)
2950+
static bool is_trim_wchar(uint32_t w, HashTable *ht)
29512951
{
2952-
for (size_t i = 0; i < characters_len; i++) {
2953-
if (w == characters[i]) {
2954-
return true;
2955-
}
2956-
}
2957-
return false;
2952+
return zend_hash_index_exists(ht, w);
29582953
}
29592954

29602955
static zend_string* trim_each_wchar(zend_string *str, zend_string *what, MB_TRIM_MODE mode, const mbfl_encoding *enc)
@@ -2977,43 +2972,49 @@ static zend_string* trim_each_wchar(zend_string *str, zend_string *what, MB_TRIM
29772972
return zend_string_copy(str);
29782973
}
29792974

2975+
HashTable what_ht;
2976+
zval val;
2977+
zend_hash_init(&what_ht, what_len, NULL, NULL, false);
2978+
2979+
while (what_len) {
2980+
what_out_len = enc->to_wchar(&what_in, &what_len, what_wchar_buf, 128, &state);
2981+
ZEND_ASSERT(what_out_len <= 128);
2982+
for (size_t i = 0; i < what_out_len; i++) {
2983+
zend_hash_index_add_new(&what_ht, what_wchar_buf[i], &val);
2984+
}
2985+
}
2986+
29802987
while (in_len) {
29812988
out_len = enc->to_wchar(&in, &in_len, wchar_buf, 128, &state);
29822989
ZEND_ASSERT(out_len <= 128);
29832990
total_len += out_len;
29842991

2985-
while (what_len) {
2986-
what_out_len = enc->to_wchar(&what_in, &what_len, what_wchar_buf, 128, &state);
2987-
ZEND_ASSERT(what_out_len <= 128);
2988-
2989-
if (first && (mode & MB_LTRIM)) {
2990-
for (size_t i = 0; i < out_len; i++) {
2991-
uint32_t w = wchar_buf[i];
2992-
if (is_trim_wchar(w, what_wchar_buf, what_out_len)) {
2993-
left += 1;
2994-
} else {
2995-
first = false;
2996-
break;
2997-
}
2992+
if (first && (mode & MB_LTRIM)) {
2993+
for (size_t i = 0; i < out_len; i++) {
2994+
uint32_t w = wchar_buf[i];
2995+
if (is_trim_wchar(w, &what_ht)) {
2996+
left += 1;
2997+
} else {
2998+
first = false;
2999+
break;
29983000
}
29993001
}
3002+
}
30003003

3001-
if (mode & MB_RTRIM) {
3002-
for (size_t j = 0; j < out_len; j++) {
3003-
uint32_t w = wchar_buf[j];
3004-
if (is_trim_wchar(w, what_wchar_buf, what_out_len)) {
3005-
right -= 1;
3006-
} else {
3007-
right = 0;
3008-
}
3004+
if (mode & MB_RTRIM) {
3005+
for (size_t j = 0; j < out_len; j++) {
3006+
uint32_t w = wchar_buf[j];
3007+
if (is_trim_wchar(w, &what_ht)) {
3008+
right -= 1;
3009+
} else {
3010+
right = 0;
30093011
}
30103012
}
30113013
}
3012-
3013-
what_in -= ZSTR_LEN(what);
3014-
what_len = ZSTR_LEN(what);
30153014
}
30163015

3016+
zend_hash_destroy(&what_ht);
3017+
30173018
return mb_get_substr(str, left, total_len + (right - left), enc);
30183019
}
30193020

0 commit comments

Comments
 (0)