From 9ba65533b552dcb60d324e6d0ab11c9d1941fa49 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Niklas=20Laxstr=C3=B6m?= Date: Fri, 11 Jul 2008 18:08:18 +0000 Subject: [PATCH] * (bug 8604) padright: and similar functions fail with non-ASCII arguments --- RELEASE-NOTES | 1 + includes/GlobalFunctions.php | 23 +++++++++++++++++++++++ includes/parser/CoreParserFunctions.php | 6 +++--- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 8ad7e7083e..be5952ea59 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -431,6 +431,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN had stict standards issues with setFakeSlaveLag() and setFakeMaster(). * (bug 451) Improve the phrase mappings of the Chinese converter arrays. * (bug 12487) Rights log is not fully internationalized +* (bug 8604) padright: and similar functions fail with non-ASCII arguments === API changes in 1.13 === diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 56a313fa0f..b4e018533c 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -70,6 +70,29 @@ if ( !function_exists( 'mb_strlen' ) ) { } } +if ( !function_exists( 'mb_str_pad' ) ) { + /** + * Fallback implementation of mb_str_pad, hardcoded to UTF-8. + * @param $input String: input string + * @param $pad_length Integer: how much to pad + * @param $pad_string Character used for padding, only one! + * @param $pad_type Only STR_PAD_LEFT and STR_PAD_RIGHT supported. + * @return int + */ + function mb_str_pad( $input, $pad_length, $pad_string, $pad_type ) { + $toPad = $pad_length - mb_strlen( $input ); + if ( $toPad <= 0 ) return $input; + for ( $i = 0; $i < $toPad; $i++ ) { + if ( $pad_type === STR_PAD_LEFT ) { + $input = $pad_string . $input; + } elseif( $pad_type === STR_PAD_RIGHT ) { + $input .= $pad_string; + } + } + return $input; + } +} + if ( !function_exists( 'array_diff_key' ) ) { /** * Exists in PHP 5.1.0+ diff --git a/includes/parser/CoreParserFunctions.php b/includes/parser/CoreParserFunctions.php index d9072e9310..58f5eb652c 100644 --- a/includes/parser/CoreParserFunctions.php +++ b/includes/parser/CoreParserFunctions.php @@ -287,9 +287,9 @@ class CoreParserFunctions { static function pad( $string = '', $length = 0, $char = 0, $direction = STR_PAD_RIGHT ) { $length = min( max( $length, 0 ), 500 ); - $char = substr( $char, 0, 1 ); - return ( $string !== '' && (int)$length > 0 && strlen( trim( (string)$char ) ) > 0 ) - ? str_pad( $string, $length, (string)$char, $direction ) + $char = mb_substr( $char, 0, 1 ); + return ( $string !== '' && (int)$length > 0 && mb_strlen( trim($char) ) > 0 ) + ? mb_str_pad( $string, $length, $char, $direction ) : $string; } -- 2.20.1