Skip to content

Commit 79a75c9

Browse files
committed
Set length to safe_strlen() if null for cross PHP compat. Explicitly enable Colors in tests.
1 parent 57befc8 commit 79a75c9

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

lib/cli/cli.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ function safe_strlen( $str ) {
183183
* @return string Substring of string specified by start and length parameters
184184
*/
185185
function safe_substr( $str, $start, $length = false, $width = false ) {
186+
// PHP 5.3 substr takes false as full length, PHP > 5.3 takes null - for compat. do strlen.
187+
if ( null === $length || false === $length ) {
188+
$length = safe_strlen( $str );
189+
}
186190
if ( function_exists( 'mb_substr' ) && function_exists( 'mb_detect_encoding' ) ) {
187191
$encoding = mb_detect_encoding( $str );
188192
if ( false !== $width && 'UTF-8' === $encoding ) {
@@ -191,11 +195,13 @@ function safe_substr( $str, $start, $length = false, $width = false ) {
191195
// Load both regexs generated from Unicode data.
192196
require __DIR__ . '/unicode/regex.php';
193197
}
194-
$cnt = preg_match_all( '/[\x00-\x7f\xc2-\xf4][^\x00-\x7f\xc2-\xf4]*/', $str, $matches );
195-
$width = $length;
198+
if ( preg_match( $eaw_regex, $str ) ) {
199+
$cnt = preg_match_all( '/[\x00-\x7f\xc2-\xf4][^\x00-\x7f\xc2-\xf4]*/', $str, $matches );
200+
$width = $length;
196201

197-
for ( $length = 0; $length < $cnt && $width > 0; $length++ ) {
198-
$width -= preg_match( $eaw_regex, $matches[0][ $length ] ) ? 2 : 1;
202+
for ( $length = 0; $length < $cnt && $width > 0; $length++ ) {
203+
$width -= preg_match( $eaw_regex, $matches[0][ $length ] ) ? 2 : 1;
204+
}
199205
}
200206
}
201207
$substr = mb_substr( $str, $start, $length, $encoding );

tests/test-cli.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ function test_encoded_string_pad() {
4646

4747
function test_colorized_string_pad() {
4848
// Colors enabled.
49+
\cli\Colors::enable( true );
4950

5051
$colorized = \cli\Colors::colorize( '%Gx%n', true ); // colorized `x` string
5152
$this->assertSame( 22, strlen( \cli\Colors::pad( $colorized, 11 ) ) );
@@ -86,6 +87,7 @@ function test_colorized_string_length() {
8687

8788
function test_colorized_string_width() {
8889
// Colors enabled.
90+
\cli\Colors::enable( true );
8991

9092
$colorized = \cli\Colors::colorize( '%Gx%n', true );
9193
$this->assertSame( 1, \cli\Colors::width( $colorized ) );

tests/test-table.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ public function test_ascii_pre_colorized_widths() {
8282
$table->setRows( $items );
8383

8484
$out = $table->getDisplayLines();
85-
error_log( "out=" . print_r( $out, true ) );
8685

8786
// "+ 4" accommodates 3 borders and header.
8887
$this->assertSame( 4 + 4, count( $out ) );

0 commit comments

Comments
 (0)