(bug 16852) padleft and padright now handle multibyte characters and multicharacter...
authorAryeh Gregor <simetrical@users.mediawiki.org>
Wed, 14 Jan 2009 17:54:52 +0000 (17:54 +0000)
committerAryeh Gregor <simetrical@users.mediawiki.org>
Wed, 14 Jan 2009 17:54:52 +0000 (17:54 +0000)
Patch by RememberTheDot, with adjustments to comments by me

RELEASE-NOTES
includes/parser/CoreParserFunctions.php
maintenance/parserTests.txt

index dceef20..ba50caf 100644 (file)
@@ -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
index 2dfd34b..35d7de8 100644 (file)
@@ -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 ) {
index c84e0e3..49b7143 100644 (file)
@@ -7233,6 +7233,24 @@ language=fa
 </p>
 !! end
 
+!! test
+Multibyte character in padleft
+!! input
+{{padleft:-Hello|7|Æ}}
+!! result
+<p>Æ-Hello
+</p>
+!! end
+
+!! test
+Multibyte character in padright
+!! input
+{{padright:Hello-|7|Æ}}
+!! result
+<p>Hello-Æ
+</p>
+!! end
+
 #
 #
 #