@@ -3537,10 +3537,9 @@ PHPAPI void php_strip_tags(char *rbuf, int len, int *stateptr, char *allow, int
3537
3537
PHP_FUNCTION (str_repeat )
3538
3538
{
3539
3539
zval * * input_str ; /* Input string */
3540
- zval * * mult ; /* Multiplier */
3541
- char * result ; /* Resulting string */
3542
- int result_len ; /* Length of the resulting string */
3543
- int i ;
3540
+ zval * * mult ; /* Multiplier */
3541
+ char * result ; /* Resulting string */
3542
+ int result_len ; /* Length of the resulting string */
3544
3543
3545
3544
if (ZEND_NUM_ARGS () != 2 || zend_get_parameters_ex (2 , & input_str , & mult ) == FAILURE ) {
3546
3545
WRONG_PARAM_COUNT ;
@@ -3567,12 +3566,24 @@ PHP_FUNCTION(str_repeat)
3567
3566
result_len = Z_STRLEN_PP (input_str ) * Z_LVAL_PP (mult );
3568
3567
result = (char * )emalloc (result_len + 1 );
3569
3568
3570
- /* Copy the input string into the result as many times as necessary */
3571
- for (i = 0 ; i < Z_LVAL_PP (mult ); i ++ ) {
3572
- memcpy (result + Z_STRLEN_PP (input_str ) * i ,
3573
- Z_STRVAL_PP (input_str ),
3574
- Z_STRLEN_PP (input_str ));
3569
+ /* Heavy optimization for situations where multiplier is 1 byte long */
3570
+ if (Z_LVAL_PP (mult ) == 1 ) {
3571
+ memset (result , * (Z_STRVAL_PP (input_str )), Z_LVAL_PP (mult ));
3572
+ } else {
3573
+ char * s , * e , * ee ;
3574
+ int l = 0 ;
3575
+ memcpy (result , Z_STRVAL_PP (input_str ), Z_STRLEN_PP (input_str ));
3576
+ s = result ;
3577
+ e = result + Z_STRLEN_PP (input_str );
3578
+ ee = result + result_len ;
3579
+
3580
+ while (e < ee ) {
3581
+ l = (e - s ) < (ee - e ) ? (e - s ) : (ee - e );
3582
+ memmove (e , s , l );
3583
+ e += l ;
3584
+ }
3575
3585
}
3586
+
3576
3587
result [result_len ] = '\0' ;
3577
3588
3578
3589
RETURN_STRINGL (result , result_len , 0 );
0 commit comments