@@ -2547,14 +2547,14 @@ PHP_FUNCTION(mb_strwidth)
2547
2547
RETVAL_LONG (mb_get_strwidth (string , enc ));
2548
2548
}
2549
2549
2550
- static zend_string * mb_trim_string (zend_string * input , zend_string * marker , const mbfl_encoding * enc , unsigned int from , int width )
2550
+ static zend_string * mb_trim_string (zend_string * input , zend_string * marker , const mbfl_encoding * enc , size_t from , size_t width )
2551
2551
{
2552
2552
uint32_t wchar_buf [128 ];
2553
2553
unsigned char * in = (unsigned char * )ZSTR_VAL (input );
2554
2554
size_t in_len = ZSTR_LEN (input );
2555
2555
unsigned int state = 0 ;
2556
- int remaining_width = width ;
2557
- unsigned int to_skip = from ;
2556
+ size_t remaining_width = width ;
2557
+ size_t to_skip = from ;
2558
2558
size_t out_len = 0 ;
2559
2559
bool first_call = true, input_err = false;
2560
2560
mb_convert_buf buf ;
@@ -2566,17 +2566,23 @@ static zend_string* mb_trim_string(zend_string *input, zend_string *marker, cons
2566
2566
if (out_len <= to_skip ) {
2567
2567
to_skip -= out_len ;
2568
2568
} else {
2569
- for (unsigned int i = to_skip ; i < out_len ; i ++ ) {
2569
+ for (size_t i = to_skip ; i < out_len ; i ++ ) {
2570
2570
uint32_t w = wchar_buf [i ];
2571
+ size_t current_w_width = character_width (w );
2572
+
2571
2573
input_err |= (w == MBFL_BAD_INPUT );
2572
- remaining_width -= character_width ( w );
2573
- if (remaining_width < 0 ) {
2574
- /* We need to truncate string and append trim marker */
2575
- width -= mb_get_strwidth ( marker , enc );
2576
- /* 'width' is now the amount we want to take from 'input' */
2577
- if (width <= 0 ) {
2574
+
2575
+ if (remaining_width < current_w_width ) {
2576
+ size_t marker_width = mb_get_strwidth ( marker , enc );
2577
+
2578
+ /* The trim marker is larger than the desired string width */
2579
+ if (width <= marker_width ) {
2578
2580
return zend_string_copy (marker );
2579
2581
}
2582
+
2583
+ /* We need to truncate string and append trim marker */
2584
+ width -= marker_width ;
2585
+ /* 'width' is now the amount we want to take from 'input' */
2580
2586
mb_convert_buf_init (& buf , width , MBSTRG (current_filter_illegal_substchar ), MBSTRG (current_filter_illegal_mode ));
2581
2587
2582
2588
if (first_call ) {
@@ -2587,6 +2593,7 @@ static zend_string* mb_trim_string(zend_string *input, zend_string *marker, cons
2587
2593
goto restart_conversion ;
2588
2594
}
2589
2595
}
2596
+ remaining_width -= current_w_width ;
2590
2597
}
2591
2598
to_skip = 0 ;
2592
2599
}
@@ -2626,12 +2633,13 @@ static zend_string* mb_trim_string(zend_string *input, zend_string *marker, cons
2626
2633
if (out_len <= from ) {
2627
2634
from -= out_len ;
2628
2635
} else {
2629
- for (unsigned int i = from ; i < out_len ; i ++ ) {
2630
- width - = character_width (wchar_buf [i ]);
2631
- if (width < 0 ) {
2636
+ for (size_t i = from ; i < out_len ; i ++ ) {
2637
+ size_t current_wchar_char_width = character_width (wchar_buf [i ]);
2638
+ if (width < current_wchar_char_width ) {
2632
2639
enc -> from_wchar (wchar_buf + from , i - from , & buf , true);
2633
2640
goto append_trim_marker ;
2634
2641
}
2642
+ width -= character_width (wchar_buf [i ]);
2635
2643
}
2636
2644
ZEND_ASSERT (in_len > 0 );
2637
2645
enc -> from_wchar (wchar_buf + from , out_len - from , & buf , false);
0 commit comments