Skip to content

Commit 2846469

Browse files
committed
Change function structure. trim_default_chars directly add HashTable.
1 parent 650133f commit 2846469

File tree

1 file changed

+46
-38
lines changed

1 file changed

+46
-38
lines changed

ext/mbstring/mbstring.c

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2945,45 +2945,24 @@ typedef enum {
29452945
MB_LTRIM = 1,
29462946
MB_RTRIM = 2,
29472947
MB_BOTH_TRIM = 3
2948-
} MB_TRIM_MODE;
2948+
} mb_trim_mode;
29492949

29502950
static zend_always_inline bool is_trim_wchar(uint32_t w, const HashTable *ht)
29512951
{
29522952
return zend_hash_index_exists(ht, w);
29532953
}
29542954

2955-
static zend_string* trim_each_wchar(zend_string *str, zend_string *what, MB_TRIM_MODE mode, const mbfl_encoding *enc)
2955+
static zend_string* trim_each_wchar(zend_string *str, const HashTable *what_ht, mb_trim_mode mode, const mbfl_encoding *enc)
29562956
{
29572957
unsigned char *in = (unsigned char*)ZSTR_VAL(str);
2958-
unsigned char *what_in = (unsigned char*)ZSTR_VAL(what);
29592958
uint32_t wchar_buf[128];
2960-
uint32_t what_wchar_buf[128];
29612959
size_t in_len = ZSTR_LEN(str);
29622960
size_t out_len = 0;
2963-
size_t what_out_len = 0;
2964-
size_t what_len = ZSTR_LEN(what);
29652961
unsigned int state = 0;
29662962
size_t left = 0;
29672963
size_t right = 0;
29682964
size_t total_len = 0;
29692965

2970-
if (what_len == 0) {
2971-
return zend_string_copy(str);
2972-
}
2973-
2974-
HashTable what_ht;
2975-
zval val;
2976-
ZVAL_TRUE(&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(&what_ht, what_wchar_buf[i], &val);
2984-
}
2985-
}
2986-
29872966
while (in_len) {
29882967
out_len = enc->to_wchar(&in, &in_len, wchar_buf, 128, &state);
29892968
ZEND_ASSERT(out_len <= 128);
@@ -2992,7 +2971,7 @@ static zend_string* trim_each_wchar(zend_string *str, zend_string *what, MB_TRIM
29922971
if (mode & MB_LTRIM) {
29932972
for (size_t i = 0; i < out_len; i++) {
29942973
uint32_t w = wchar_buf[i];
2995-
if (is_trim_wchar(w, &what_ht)) {
2974+
if (is_trim_wchar(w, what_ht)) {
29962975
left += 1;
29972976
} else {
29982977
mode ^= MB_LTRIM;
@@ -3004,7 +2983,7 @@ static zend_string* trim_each_wchar(zend_string *str, zend_string *what, MB_TRIM
30042983
if (mode & MB_RTRIM) {
30052984
for (size_t j = 0; j < out_len; j++) {
30062985
uint32_t w = wchar_buf[j];
3007-
if (is_trim_wchar(w, &what_ht)) {
2986+
if (is_trim_wchar(w, what_ht)) {
30082987
right += 1;
30092988
} else {
30102989
right = 0;
@@ -3013,12 +2992,10 @@ static zend_string* trim_each_wchar(zend_string *str, zend_string *what, MB_TRIM
30132992
}
30142993
}
30152994

3016-
zend_hash_destroy(&what_ht);
3017-
30182995
return mb_get_substr(str, left, total_len - (right + left), enc);
30192996
}
30202997

3021-
static zend_string* mb_trim_default_chars(zend_string *str, MB_TRIM_MODE mode, const mbfl_encoding *enc)
2998+
static zend_string* mb_trim_default_chars(zend_string *str, mb_trim_mode mode, const mbfl_encoding *enc)
30222999
{
30233000
const uint32_t trim_default_chars[] = {
30243001
0x20, 0x0C, 0x0A, 0x0D, 0x09, 0x0B, 0x00, 0xA0, 0x1680,
@@ -3027,18 +3004,49 @@ static zend_string* mb_trim_default_chars(zend_string *str, MB_TRIM_MODE mode, c
30273004
0x85, 0x180E
30283005
};
30293006
size_t trim_default_chars_length = sizeof(trim_default_chars) / sizeof(uint32_t);
3030-
mb_convert_buf buf;
3031-
mb_convert_buf_init(&buf, trim_default_chars_length, MBSTRG(current_filter_illegal_substchar), MBSTRG(current_filter_illegal_mode));
30323007

3033-
enc->from_wchar((uint32_t *)trim_default_chars, trim_default_chars_length, &buf, true);
3008+
HashTable what_ht;
3009+
zval val;
3010+
ZVAL_TRUE(&val);
30343011

3035-
zend_string *what = mb_convert_buf_result(&buf, enc);
3036-
zend_string *result = trim_each_wchar(str, what, mode, enc);
3037-
zend_string_release_ex(what, false);
3038-
return result;
3012+
zend_hash_init(&what_ht, trim_default_chars_length, NULL, NULL, false);
3013+
3014+
for (size_t i = 0; i < trim_default_chars_length; i++) {
3015+
zend_hash_index_add(&what_ht, trim_default_chars[i], &val);
3016+
}
3017+
zend_string* retval = trim_each_wchar(str, &what_ht, mode, enc);
3018+
zend_hash_destroy(&what_ht);
3019+
3020+
return retval;
3021+
}
3022+
3023+
static zend_string* mb_trim_what_chars(zend_string *str, zend_string *what, mb_trim_mode mode, const mbfl_encoding *enc)
3024+
{
3025+
unsigned char *what_in = (unsigned char*)ZSTR_VAL(what);
3026+
uint32_t what_wchar_buf[128];
3027+
size_t what_out_len = 0;
3028+
unsigned int state = 0;
3029+
size_t what_len = ZSTR_LEN(what);
3030+
HashTable what_ht;
3031+
zval val;
3032+
ZVAL_TRUE(&val);
3033+
zend_hash_init(&what_ht, what_len, NULL, NULL, false);
3034+
3035+
while (what_len) {
3036+
what_out_len = enc->to_wchar(&what_in, &what_len, what_wchar_buf, 128, &state);
3037+
ZEND_ASSERT(what_out_len <= 128);
3038+
for (size_t i = 0; i < what_out_len; i++) {
3039+
zend_hash_index_add(&what_ht, what_wchar_buf[i], &val);
3040+
}
3041+
}
3042+
3043+
zend_string *retval = trim_each_wchar(str, &what_ht, mode, enc);
3044+
zend_hash_destroy(&what_ht);
3045+
3046+
return retval;
30393047
}
30403048

3041-
static void php_do_mb_trim(INTERNAL_FUNCTION_PARAMETERS, MB_TRIM_MODE mode)
3049+
static void php_do_mb_trim(INTERNAL_FUNCTION_PARAMETERS, mb_trim_mode mode)
30423050
{
30433051
zend_string *str;
30443052
zend_string *what = NULL;
@@ -3057,9 +3065,9 @@ static void php_do_mb_trim(INTERNAL_FUNCTION_PARAMETERS, MB_TRIM_MODE mode)
30573065
}
30583066

30593067
if (what) {
3060-
RETVAL_STR(trim_each_wchar(str, what, mode, enc));
3068+
RETURN_STR(mb_trim_what_chars(str, what, mode, enc));
30613069
} else {
3062-
RETVAL_STR(mb_trim_default_chars(str, mode, enc));
3070+
RETURN_STR(mb_trim_default_chars(str, mode, enc));
30633071
}
30643072
}
30653073

0 commit comments

Comments
 (0)