From: Aryeh Gregor Date: Wed, 14 Jan 2009 17:54:52 +0000 (+0000) Subject: (bug 16852) padleft and padright now handle multibyte characters and multicharacter... X-Git-Tag: 1.31.0-rc.0~43429 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/banques/ajouter.php?a=commitdiff_plain;h=7f1b0402486bcde128badd5694415d0371f4e878;p=lhc%2Fweb%2Fwiklou.git (bug 16852) padleft and padright now handle multibyte characters and multicharacter pad strings Patch by RememberTheDot, with adjustments to comments by me --- diff --git a/RELEASE-NOTES b/RELEASE-NOTES index dceef20503..ba50caf875 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -34,7 +34,9 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * Added "__\" magic word to eat up all whitespace and newlines to the next non-whitespace character, to facilitate writing readable template code where whitespace is significant. -* (bug 17002) Add &minor= and &summary= as parameters in the url when editing, to automatically add a summary or a minor edit. +* (bug 17002) Add &minor= and &summary= as parameters in the url when editing, + to automatically add a summary or a minor edit. +* (bug 16852) padleft and padright now accept multiletter pad characters === Bug fixes in 1.15 === * Fixing the caching issue by using -{T|xxx}- syntax (only applies on wiki with LanguageConverter class) @@ -42,6 +44,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * (bug 16968) Special:Upload no longer throws useless warnings. * (bug 15470) Special:Upload no longer force-capitalizes titles * (bug 17000) Special:RevisionDelete now checks if the database is locked before trying to delete the edit. +* (bug 16852) padleft and padright now handle multibyte characters correctly == API changes in 1.15 == * (bug 16798) JSON encoding errors for some characters outside the BMP diff --git a/includes/parser/CoreParserFunctions.php b/includes/parser/CoreParserFunctions.php index 2dfd34bfea..35d7de88a1 100644 --- a/includes/parser/CoreParserFunctions.php +++ b/includes/parser/CoreParserFunctions.php @@ -310,20 +310,38 @@ class CoreParserFunctions { return $lang != '' ? $lang : $arg; } - 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 ) - : $string; + /** + * Unicode-safe str_pad with the restriction that $length is forced to be <= 500 + */ + static function pad( $string, $length, $padding = '0', $direction = STR_PAD_RIGHT ) { + $lengthOfPadding = mb_strlen( $padding ); + if ( $lengthOfPadding == 0 ) return $string; + + # The remaining length to add counts down to 0 as padding is added + $length = min( $length, 500 ) - mb_strlen( $string ); + # $finalPadding is just $padding repeated enough times so that + # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length + $finalPadding = ''; + while ( $length > 0 ) { + # If $length < $lengthofPadding, truncate $padding so we get the + # exact length desired. + $finalPadding .= mb_substr( $padding, 0, $length ); + $length -= $lengthOfPadding; + } + + if ( $direction == STR_PAD_LEFT ) { + return $finalPadding . $string; + } else { + return $string . $finalPadding; + } } - static function padleft( $parser, $string = '', $length = 0, $char = 0 ) { - return self::pad( $string, $length, $char, STR_PAD_LEFT ); + static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) { + return self::pad( $string, $length, $padding, STR_PAD_LEFT ); } - static function padright( $parser, $string = '', $length = 0, $char = 0 ) { - return self::pad( $string, $length, $char ); + static function padright( $parser, $string = '', $length = 0, $padding = '0' ) { + return self::pad( $string, $length, $padding ); } static function anchorencode( $parser, $text ) { diff --git a/maintenance/parserTests.txt b/maintenance/parserTests.txt index c84e0e35cf..49b71434d6 100644 --- a/maintenance/parserTests.txt +++ b/maintenance/parserTests.txt @@ -7233,6 +7233,24 @@ language=fa

!! end +!! test +Multibyte character in padleft +!! input +{{padleft:-Hello|7|Æ}} +!! result +

Æ-Hello +

+!! end + +!! test +Multibyte character in padright +!! input +{{padright:Hello-|7|Æ}} +!! result +

Hello-Æ +

+!! end + # # #