r45734 - Code Review

From MediaWiki.org

Jump to: navigation, search
Repository:MediaWiki
Revision:r45733 | r45734 (on ViewVC) | r45735 >
Date:17:54, 14 January 2009
Author:simetrical
Status:ok (Comments)
Tags:
Comment:(bug 16852) padleft and padright now handle multibyte characters and multicharacter pad strings

Patch by RememberTheDot, with adjustments to comments by me
Modified paths:

Diff [purge]

Index: trunk/phase3/maintenance/parserTests.txt
===================================================================
--- trunk/phase3/maintenance/parserTests.txt	(revision 45733)
+++ trunk/phase3/maintenance/parserTests.txt	(revision 45734)
@@ -7233,6 +7233,24 @@
 </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
+
 #
 #
 #
Index: trunk/phase3/includes/parser/CoreParserFunctions.php
===================================================================
--- trunk/phase3/includes/parser/CoreParserFunctions.php	(revision 45733)
+++ trunk/phase3/includes/parser/CoreParserFunctions.php	(revision 45734)
@@ -310,20 +310,38 @@
 		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: trunk/phase3/RELEASE-NOTES
===================================================================
--- trunk/phase3/RELEASE-NOTES	(revision 45733)
+++ trunk/phase3/RELEASE-NOTES	(revision 45734)
@@ -34,7 +34,9 @@
 * 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 @@
 * (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

Comments

#Comment by Brion VIBBER (Talk | contribs)   21:28, 20 January 2009

Looks ok... :D

#Comment by Plustgarten (Talk | contribs)   21:17, 13 April 2009

What if the padding character in padleft or padright is an HTML character code? In particular, it sure would be useful if it could be (or include) an &nbsp; (which should count as one character). Could we get this effect by judicious application of something like html_entity_decode() to the padding string, before measuring it?

#Comment by Simetrical (Talk | contribs)   21:51, 13 April 2009

Would be sensible. Patches are welcome, post them on Bugzilla if you'd like.

Status & tagging log

Views
Toolbox