Skip to content

Commit 4cc65fd

Browse files
committed
Optimize double-width safe_substr when all double-width.
1 parent c5f9c03 commit 4cc65fd

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

lib/cli/cli.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,23 @@ function safe_substr( $str, $start, $length = false, $is_width = false, $encodin
203203
$eaw_regex = get_unicode_regexs( 'eaw' );
204204
// If there's any East Asian double-width chars...
205205
if ( preg_match( $eaw_regex, $substr ) ) {
206-
// Explode string into an array of UTF-8 chars. Based on core `_mb_substr()` in "wp-includes/compat.php".
207-
$chars = preg_split( '/([\x00-\x7f\xc2-\xf4][^\x00-\x7f\xc2-\xf4]*)/', $substr, $length + 1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
208-
$cnt = min( count( $chars ), $length );
209-
$width = $length;
206+
// Note that if the length ends in the middle of a double-width char, the char is included, not excluded.
210207

211-
for ( $length = 0; $length < $cnt && $width > 0; $length++ ) {
212-
$width -= preg_match( $eaw_regex, $chars[ $length ] ) ? 2 : 1;
208+
// See if it's all EAW - the most likely case.
209+
if ( preg_match_all( $eaw_regex, $substr ) === $length ) {
210+
// Just halve the length so (rounded up).
211+
$substr = mb_substr( $substr, 0, (int) ( ( $length + 1 ) / 2 ), $encoding );
212+
} else {
213+
// Explode string into an array of UTF-8 chars. Based on core `_mb_substr()` in "wp-includes/compat.php".
214+
$chars = preg_split( '/([\x00-\x7f\xc2-\xf4][^\x00-\x7f\xc2-\xf4]*)/', $substr, $length + 1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
215+
$cnt = min( count( $chars ), $length );
216+
$width = $length;
217+
218+
for ( $length = 0; $length < $cnt && $width > 0; $length++ ) {
219+
$width -= preg_match( $eaw_regex, $chars[ $length ] ) ? 2 : 1;
220+
}
221+
return join( '', array_slice( $chars, 0, $length ) );
213222
}
214-
return join( '', array_slice( $chars, 0, $length ) );
215223
}
216224
}
217225
} else {
@@ -279,7 +287,7 @@ function strwidth( $string, $encoding = false ) {
279287
return $width;
280288
}
281289
}
282-
return safe_strlen( $string );
290+
return safe_strlen( $string, $encoding );
283291
}
284292

285293
/**

0 commit comments

Comments
 (0)