r81191 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r81190‎ | r81191 | r81192 >
Date:15:59, 29 January 2011
Author:happy-melon
Status:ok
Tags:
Comment:
Follow-up r81074: better handling of whitespace, or lack thereof, between the digit and unit.
Modified paths:
  • /trunk/extensions/ParserFunctions/Convert.php (modified) (history)
  • /trunk/extensions/ParserFunctions/convertTests.txt (modified) (history)

Diff [purge]

Index: trunk/extensions/ParserFunctions/convertTests.txt
@@ -19,10 +19,10 @@
2020 *{{#convert: 10.0E2 km | m }}
2121 *{{#convert: 10.0E2.5 km | m }}
2222 !! result
23 -<ul><li>0.01 kilometers
24 -</li><li>0.01 kilometers
 23+<ul><li>0.01 kilometers
 24+</li><li>0.01kilometers
2525 </li><li>10,000 meters
26 -</li><li>10,000- meters
 26+</li><li>10,000-meters
2727 </li><li>1,000,000 meters
2828 </li><li>100 meters
2929 </li><li>1,000,000 meters
@@ -201,8 +201,8 @@
202202 *{{#convert: 10 pa | mmhg | #language = en-gb }}
203203 *{{#convert: 10 pa | mmhg | #language = /../evil/attack }}
204204 !! result
205 -<ul><li>0.01 kilometres
206 -</li><li>0.01 kilometres
 205+<ul><li>0.01 kilometres
 206+</li><li>0.01kilometres
207207 </li><li>10,000 metres
208208 </li><li>0.1 milimetres of mercury
209209 </li><li>0.1 milimeters of mercury
Index: trunk/extensions/ParserFunctions/Convert.php
@@ -16,7 +16,7 @@
1717 class ConvertParser {
1818
1919 # A regex which matches the body of the string and the source unit separately
20 - const UNITS_REGEX = '/^(.+?)\s*([a-z]+\^?\d?(?:\/\w+\^?\d?)*)$/i';
 20+ const UNITS_REGEX = '/^(.+?)([a-z]+\^?\d?(?:\/\w+\^?\d?)*)$/i';
2121
2222 # A regex which matches a number
2323 const NUM_REGEX = '/\b((?:\+|\-|&minus;|\x2212)?(\d+(?:\.\d+)?)(?:E(?:\+|\-|&minus;|\x2212)?\d+)?)\b/i';
@@ -151,6 +151,10 @@
152152 # Get the source unit, if not already set. This throws ConvertError on failure
153153 if ( !$this->sourceUnit instanceof ConvertUnit ){
154154 $this->deduceSourceUnit( $string );
 155+ } else {
 156+ # The string has no unit on the end, so it's been trimmed to the end of the
 157+ # last digit, meaning the unit specified by #sourceunit won't have any space
 158+ $string .= ' ';
155159 }
156160
157161 # Use the default unit (SI usually)
@@ -190,10 +194,10 @@
191195 $string = preg_replace_callback(
192196 self::NUM_REGEX,
193197 array( $this, 'convert' ),
194 - trim( preg_replace( self::UNITS_REGEX, '$1', $string ) )
 198+ ltrim( preg_replace( self::UNITS_REGEX, '$1', $string ) )
195199 );
196200 if( $this->raw ){
197 - return $string;
 201+ return trim( $string );
198202 } else {
199203 $unit = $this->targetUnit->getText(
200204 $this->lastValue,
@@ -201,7 +205,7 @@
202206 $this->abbreviate,
203207 $this->language
204208 );
205 - return "$string $unit";
 209+ return $string . $unit;
206210 }
207211 }
208212
@@ -737,7 +741,7 @@
738742 $msgText = "$msgText/$msg2Text";
739743 }
740744
741 - return $msgText;
 745+ return trim( $msgText );
742746 }
743747
744748 protected function getTextFromMessage( $key, $number, $link, $abbreviate, $language ){

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r81074(bug 235) parser function for conversion of units of measurement....happy-melon00:13, 27 January 2011

Status & tagging log