Skip to content

Commit 0fa663f

Browse files
Merge pull request #64 from jesseoverright/master
Adds fallback for mb_strlen, fixes #61
2 parents 4f09ec1 + 3a3adb4 commit 0fa663f

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

lib/cli/cli.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,45 @@ function menu( $items, $default = null, $title = 'Choose an item' ) {
153153
}
154154

155155
/**
156-
* An encoding-safe way of getting string length.
156+
* Attempts an encoding-safe way of getting string length. If mb_string extensions aren't
157+
* installed, falls back to basic strlen if no encoding is present
157158
*
158159
* @param string The string to check
159160
* @return int Numeric value that represents the string's length
160161
*/
161162
function safe_strlen( $str ) {
162-
return mb_strlen( $str, mb_detect_encoding( $str ) );
163+
if ( function_exists( 'mb_strlen' ) ) {
164+
$length = mb_strlen( $str, mb_detect_encoding( $str ) );
165+
} else {
166+
// iconv will return PHP notice if non-ascii characters are present in input string
167+
$str = iconv( 'ASCII' , 'ASCII', $str );
168+
169+
$length = strlen( $str );
170+
}
171+
172+
return $length;
173+
}
174+
175+
/**
176+
* Attempts an encoding-safe way of getting a substring. If mb_string extensions aren't
177+
* installed, falls back to ascii substring if no encoding is present
178+
*
179+
* @param string $str The input string
180+
* @param int $start The starting position of the substring
181+
* @param boolean $length Maximum length of the substring
182+
* @return string Substring of string specified by start and length parameters
183+
*/
184+
function safe_substr( $str, $start, $length = false ) {
185+
if ( function_exists( 'mb_substr' ) ) {
186+
$substr = mb_substr( $str, $start, $length, mb_detect_encoding( $str ) );
187+
} else {
188+
// iconv will return PHP notice if non-ascii characters are present in input string
189+
$str = iconv( 'ASCII' , 'ASCII', $str );
190+
191+
$substr = substr( $str, $start, $length );
192+
}
193+
194+
return $substr;
163195
}
164196

165197
/**

lib/cli/table/Ascii.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,15 @@ public function row( array $row ) {
132132
$col_width = $this->_widths[ $col ];
133133
$original_val_width = Colors::length( $value );
134134
if ( $original_val_width > $col_width ) {
135-
$row[ $col ] = mb_substr( $value, 0, $col_width, mb_detect_encoding( $value ) );
136-
$value = mb_substr( $value, $col_width, $original_val_width, mb_detect_encoding( $value ) );
135+
$row[ $col ] = \cli\safe_substr( $value, 0, $col_width );
136+
$value = \cli\safe_substr( $value, $col_width, $original_val_width );
137137
$i = 0;
138138
do {
139-
$extra_value = mb_substr( $value, 0, $col_width, mb_detect_encoding( $value ) );
140-
$val_width = mb_strlen( $extra_value, mb_detect_encoding( $extra_value ) );
139+
$extra_value = \cli\safe_substr( $value, 0, $col_width );
140+
$val_width = \cli\safe_strlen( $extra_value );
141141
if ( $val_width ) {
142142
$extra_rows[ $col ][] = $extra_value;
143-
$value = mb_substr( $value, $col_width, $original_val_width, mb_detect_encoding( $value ) );
143+
$value = \cli\safe_substr( $value, $col_width, $original_val_width );
144144
$i++;
145145
if ( $i > $extra_row_count ) {
146146
$extra_row_count = $i;

tests/test-cli.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ function test_encoded_string_length() {
2424

2525
}
2626

27+
function test_encoded_substr() {
28+
29+
$this->assertEquals( \cli\safe_substr( \cli\Colors::pad( 'hello', 6), 0, 2 ), 'he' );
30+
$this->assertEquals( \cli\safe_substr( \cli\Colors::pad( 'óra', 6), 0, 2 ), 'ór' );
31+
32+
}
33+
2734
function test_colorized_string_length() {
2835
$this->assertEquals( \cli\Colors::length( \cli\Colors::colorize( '%Gx%n', true ) ), 1 );
2936
}

0 commit comments

Comments
 (0)