@@ -2945,45 +2945,24 @@ typedef enum {
2945
2945
MB_LTRIM = 1 ,
2946
2946
MB_RTRIM = 2 ,
2947
2947
MB_BOTH_TRIM = 3
2948
- } MB_TRIM_MODE ;
2948
+ } mb_trim_mode ;
2949
2949
2950
2950
static zend_always_inline bool is_trim_wchar (uint32_t w , const HashTable * ht )
2951
2951
{
2952
2952
return zend_hash_index_exists (ht , w );
2953
2953
}
2954
2954
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 )
2956
2956
{
2957
2957
unsigned char * in = (unsigned char * )ZSTR_VAL (str );
2958
- unsigned char * what_in = (unsigned char * )ZSTR_VAL (what );
2959
2958
uint32_t wchar_buf [128 ];
2960
- uint32_t what_wchar_buf [128 ];
2961
2959
size_t in_len = ZSTR_LEN (str );
2962
2960
size_t out_len = 0 ;
2963
- size_t what_out_len = 0 ;
2964
- size_t what_len = ZSTR_LEN (what );
2965
2961
unsigned int state = 0 ;
2966
2962
size_t left = 0 ;
2967
2963
size_t right = 0 ;
2968
2964
size_t total_len = 0 ;
2969
2965
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
-
2987
2966
while (in_len ) {
2988
2967
out_len = enc -> to_wchar (& in , & in_len , wchar_buf , 128 , & state );
2989
2968
ZEND_ASSERT (out_len <= 128 );
@@ -2992,7 +2971,7 @@ static zend_string* trim_each_wchar(zend_string *str, zend_string *what, MB_TRIM
2992
2971
if (mode & MB_LTRIM ) {
2993
2972
for (size_t i = 0 ; i < out_len ; i ++ ) {
2994
2973
uint32_t w = wchar_buf [i ];
2995
- if (is_trim_wchar (w , & what_ht )) {
2974
+ if (is_trim_wchar (w , what_ht )) {
2996
2975
left += 1 ;
2997
2976
} else {
2998
2977
mode ^= MB_LTRIM ;
@@ -3004,7 +2983,7 @@ static zend_string* trim_each_wchar(zend_string *str, zend_string *what, MB_TRIM
3004
2983
if (mode & MB_RTRIM ) {
3005
2984
for (size_t j = 0 ; j < out_len ; j ++ ) {
3006
2985
uint32_t w = wchar_buf [j ];
3007
- if (is_trim_wchar (w , & what_ht )) {
2986
+ if (is_trim_wchar (w , what_ht )) {
3008
2987
right += 1 ;
3009
2988
} else {
3010
2989
right = 0 ;
@@ -3013,12 +2992,10 @@ static zend_string* trim_each_wchar(zend_string *str, zend_string *what, MB_TRIM
3013
2992
}
3014
2993
}
3015
2994
3016
- zend_hash_destroy (& what_ht );
3017
-
3018
2995
return mb_get_substr (str , left , total_len - (right + left ), enc );
3019
2996
}
3020
2997
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 )
3022
2999
{
3023
3000
const uint32_t trim_default_chars [] = {
3024
3001
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
3027
3004
0x85 , 0x180E
3028
3005
};
3029
3006
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 ));
3032
3007
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 );
3034
3011
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 ;
3039
3047
}
3040
3048
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 )
3042
3050
{
3043
3051
zend_string * str ;
3044
3052
zend_string * what = NULL ;
@@ -3057,9 +3065,9 @@ static void php_do_mb_trim(INTERNAL_FUNCTION_PARAMETERS, MB_TRIM_MODE mode)
3057
3065
}
3058
3066
3059
3067
if (what ) {
3060
- RETVAL_STR ( trim_each_wchar (str , what , mode , enc ));
3068
+ RETURN_STR ( mb_trim_what_chars (str , what , mode , enc ));
3061
3069
} else {
3062
- RETVAL_STR (mb_trim_default_chars (str , mode , enc ));
3070
+ RETURN_STR (mb_trim_default_chars (str , mode , enc ));
3063
3071
}
3064
3072
}
3065
3073
0 commit comments