Skip to content

Commit

Permalink
Merge pull request #56 from wp-cli/safe-str-pad
Browse files Browse the repository at this point in the history
Encoding-safe string padding
  • Loading branch information
danielbachhuber committed Jun 29, 2014
2 parents c345248 + d93a4c8 commit 8946145
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
7 changes: 1 addition & 6 deletions lib/cli/Colors.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,6 @@ static public function length($string) {
* @return string
*/
static public function pad($string, $length) {
$real_length = safe_strlen($string);
$show_length = self::length($string);
$diff = strlen( $string ) - safe_strlen( $string );
$length += $real_length - $show_length + $diff;

return str_pad($string, $length);
return safe_str_pad( $string, $length );
}
}
16 changes: 16 additions & 0 deletions lib/cli/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,19 @@ function menu( $items, $default = null, $title = 'Choose an item' ) {
function safe_strlen( $str ) {
return mb_strlen( $str, mb_detect_encoding( $str ) );
}

/**
* An encoding-safe way of padding string length
*
* @param string $string The string to pad
* @param int $length The length to pad it to
* @return string
*/
function safe_str_pad( $string, $length ) {
$real_length = safe_strlen($string);
$show_length = Colors::length($string);
$diff = strlen( $string ) - safe_strlen( $string );
$length += $real_length - $show_length + $diff;

return str_pad($string, $length);
}

0 comments on commit 8946145

Please sign in to comment.