@@ -2875,8 +2875,7 @@ static zend_string *php_strtr_ex(zend_string *str, const char *str_from, const c
2875
2875
}
2876
2876
/* }}} */
2877
2877
2878
- /* {{{ php_strtr_array */
2879
- static void php_strtr_array (zval * return_value , zend_string * input , HashTable * pats )
2878
+ static void php_strtr_array_ex (zval * return_value , zend_string * input , HashTable * pats )
2880
2879
{
2881
2880
const char * str = ZSTR_VAL (input );
2882
2881
size_t slen = ZSTR_LEN (input );
@@ -3010,7 +3009,6 @@ static void php_strtr_array(zval *return_value, zend_string *input, HashTable *p
3010
3009
}
3011
3010
efree (num_bitset );
3012
3011
}
3013
- /* }}} */
3014
3012
3015
3013
/* {{{ count_chars */
3016
3014
static zend_always_inline zend_long count_chars (const char * p , zend_long length , char ch )
@@ -3369,6 +3367,46 @@ PHPAPI zend_string *php_str_to_str(const char *haystack, size_t length, const ch
3369
3367
}
3370
3368
/* }}} */
3371
3369
3370
+ static void php_strtr_array (zval * return_value , zend_string * str , HashTable * from_ht )
3371
+ {
3372
+ if (zend_hash_num_elements (from_ht ) < 1 ) {
3373
+ RETURN_STR_COPY (str );
3374
+ } else if (zend_hash_num_elements (from_ht ) == 1 ) {
3375
+ zend_long num_key ;
3376
+ zend_string * str_key , * tmp_str , * replace , * tmp_replace ;
3377
+ zval * entry ;
3378
+
3379
+ ZEND_HASH_FOREACH_KEY_VAL (from_ht , num_key , str_key , entry ) {
3380
+ tmp_str = NULL ;
3381
+ if (UNEXPECTED (!str_key )) {
3382
+ str_key = tmp_str = zend_long_to_str (num_key );
3383
+ }
3384
+ replace = zval_get_tmp_string (entry , & tmp_replace );
3385
+ if (ZSTR_LEN (str_key ) < 1 ) {
3386
+ php_error_docref (NULL , E_WARNING , "Ignoring replacement of empty string" );
3387
+ RETVAL_STR_COPY (str );
3388
+ } else if (ZSTR_LEN (str_key ) == 1 ) {
3389
+ RETVAL_STR (php_char_to_str_ex (str ,
3390
+ ZSTR_VAL (str_key )[0 ],
3391
+ ZSTR_VAL (replace ),
3392
+ ZSTR_LEN (replace ),
3393
+ /* case_sensitive */ true,
3394
+ NULL ));
3395
+ } else {
3396
+ zend_long dummy ;
3397
+ RETVAL_STR (php_str_to_str_ex (str ,
3398
+ ZSTR_VAL (str_key ), ZSTR_LEN (str_key ),
3399
+ ZSTR_VAL (replace ), ZSTR_LEN (replace ), & dummy ));
3400
+ }
3401
+ zend_tmp_string_release (tmp_str );
3402
+ zend_tmp_string_release (tmp_replace );
3403
+ return ;
3404
+ } ZEND_HASH_FOREACH_END ();
3405
+ } else {
3406
+ php_strtr_array_ex (return_value , str , from_ht );
3407
+ }
3408
+ }
3409
+
3372
3410
/* {{{ Translates characters in str using given translation tables */
3373
3411
PHP_FUNCTION (strtr )
3374
3412
{
@@ -3398,42 +3436,7 @@ PHP_FUNCTION(strtr)
3398
3436
}
3399
3437
3400
3438
if (!to ) {
3401
- if (zend_hash_num_elements (from_ht ) < 1 ) {
3402
- RETURN_STR_COPY (str );
3403
- } else if (zend_hash_num_elements (from_ht ) == 1 ) {
3404
- zend_long num_key ;
3405
- zend_string * str_key , * tmp_str , * replace , * tmp_replace ;
3406
- zval * entry ;
3407
-
3408
- ZEND_HASH_FOREACH_KEY_VAL (from_ht , num_key , str_key , entry ) {
3409
- tmp_str = NULL ;
3410
- if (UNEXPECTED (!str_key )) {
3411
- str_key = tmp_str = zend_long_to_str (num_key );
3412
- }
3413
- replace = zval_get_tmp_string (entry , & tmp_replace );
3414
- if (ZSTR_LEN (str_key ) < 1 ) {
3415
- php_error_docref (NULL , E_WARNING , "Ignoring replacement of empty string" );
3416
- RETVAL_STR_COPY (str );
3417
- } else if (ZSTR_LEN (str_key ) == 1 ) {
3418
- RETVAL_STR (php_char_to_str_ex (str ,
3419
- ZSTR_VAL (str_key )[0 ],
3420
- ZSTR_VAL (replace ),
3421
- ZSTR_LEN (replace ),
3422
- /* case_sensitive */ true,
3423
- NULL ));
3424
- } else {
3425
- zend_long dummy ;
3426
- RETVAL_STR (php_str_to_str_ex (str ,
3427
- ZSTR_VAL (str_key ), ZSTR_LEN (str_key ),
3428
- ZSTR_VAL (replace ), ZSTR_LEN (replace ), & dummy ));
3429
- }
3430
- zend_tmp_string_release (tmp_str );
3431
- zend_tmp_string_release (tmp_replace );
3432
- return ;
3433
- } ZEND_HASH_FOREACH_END ();
3434
- } else {
3435
- php_strtr_array (return_value , str , from_ht );
3436
- }
3439
+ php_strtr_array (return_value , str , from_ht );
3437
3440
} else {
3438
3441
RETURN_STR (php_strtr_ex (str ,
3439
3442
ZSTR_VAL (from_str ),
@@ -3443,6 +3446,48 @@ PHP_FUNCTION(strtr)
3443
3446
}
3444
3447
/* }}} */
3445
3448
3449
+ ZEND_FRAMELESS_FUNCTION (strtr , 2 )
3450
+ {
3451
+ zval str_tmp ;
3452
+ zend_string * str ;
3453
+ zval * from ;
3454
+
3455
+ Z_FLF_PARAM_STR (1 , str , str_tmp );
3456
+ Z_FLF_PARAM_ARRAY (2 , from );
3457
+
3458
+ if (ZSTR_LEN (str ) == 0 ) {
3459
+ RETVAL_EMPTY_STRING ();
3460
+ goto flf_clean ;
3461
+ }
3462
+
3463
+ php_strtr_array (return_value , str , Z_ARR_P (from ));
3464
+
3465
+ flf_clean :
3466
+ Z_FLF_PARAM_FREE_STR (1 , str_tmp );
3467
+ }
3468
+
3469
+ ZEND_FRAMELESS_FUNCTION (strtr , 3 )
3470
+ {
3471
+ zval str_tmp , from_tmp , to_tmp ;
3472
+ zend_string * str , * from , * to ;
3473
+
3474
+ Z_FLF_PARAM_STR (1 , str , str_tmp );
3475
+ Z_FLF_PARAM_STR (2 , from , from_tmp );
3476
+ Z_FLF_PARAM_STR (3 , to , to_tmp );
3477
+
3478
+ if (ZSTR_LEN (str ) == 0 ) {
3479
+ RETVAL_EMPTY_STRING ();
3480
+ goto flf_clean ;
3481
+ }
3482
+
3483
+ RETVAL_STR (php_strtr_ex (str , ZSTR_VAL (from ), ZSTR_VAL (to ), MIN (ZSTR_LEN (from ), ZSTR_LEN (to ))));
3484
+
3485
+ flf_clean :
3486
+ Z_FLF_PARAM_FREE_STR (1 , str_tmp );
3487
+ Z_FLF_PARAM_FREE_STR (2 , from_tmp );
3488
+ Z_FLF_PARAM_FREE_STR (3 , to_tmp );
3489
+ }
3490
+
3446
3491
/* {{{ Reverse a string */
3447
3492
#ifdef ZEND_INTRIN_SSSE3_NATIVE
3448
3493
#include <tmmintrin.h>
0 commit comments