Daniel Kinzler has uploaded a new change for review.
https://gerrit.wikimedia.org/r/204242
Change subject: Fix parsing of quantities with scientific notation.
......................................................................
Fix parsing of quantities with scientific notation.
The MediaWikiNumberOnlocalizer now provides a number regex that
includes scientific "e" (decimal exponent) notation.
Bug: T67436
Change-Id: I495f4d12e1982e1d8bfdfad4a524c41682867fa6
---
M lib/includes/parsers/MediaWikiNumberUnlocalizer.php
M lib/tests/phpunit/parsers/MediaWikiNumberUnlocalizerTest.php
2 files changed, 18 insertions(+), 6 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/42/204242/1
diff --git a/lib/includes/parsers/MediaWikiNumberUnlocalizer.php
b/lib/includes/parsers/MediaWikiNumberUnlocalizer.php
index 691bf2e..3e671f6 100644
--- a/lib/includes/parsers/MediaWikiNumberUnlocalizer.php
+++ b/lib/includes/parsers/MediaWikiNumberUnlocalizer.php
@@ -56,6 +56,8 @@
* Constructs a regular expression based on
Language::digitTransformTable()
* and Language::separatorTransformTable().
*
+ * Note that the resulting regex will accept scientific notation.
+ *
* @param string $delimiter The regex delimiter, used for escaping.
*
* @return string regular expression
@@ -65,22 +67,26 @@
$separatorMap = $this->language->separatorTransformTable();
// Always accept canonical digits and separators
- $characters = '0123456789,.';
+ $digits = '0123456789';
+ $separators = ',.';
// Add localized digits and separators
if ( is_array( $digitMap ) ) {
- $characters .= implode( '', array_values( $digitMap ) );
+ $digits .= implode( '', array_values( $digitMap ) );
}
if ( is_array( $separatorMap ) ) {
- $characters .= implode( '', array_values( $separatorMap
) );
+ $separators .= implode( '', array_values( $separatorMap
) );
}
// if any whitespace characters are acceptable, also accept a
regular blank.
- if ( preg_match( '/\s/u', $characters ) ) {
- $characters .= ' ';
+ if ( preg_match( '/\s/u', $separators ) ) {
+ $separators .= ' ';
}
- return '[-+]?[' . preg_quote( $characters, $delimiter ) . ']+';
+ $numberRegex = '[-+]?[' . preg_quote( $digits . $separators ,
$delimiter ) . ']+';
+ $numberRegex .= '(?:[eE][-+]?[' . preg_quote( $digits,
$delimiter ) . ']+)?'; // scientific notation
+
+ return $numberRegex;
}
}
diff --git a/lib/tests/phpunit/parsers/MediaWikiNumberUnlocalizerTest.php
b/lib/tests/phpunit/parsers/MediaWikiNumberUnlocalizerTest.php
index 8c5e8fd..4a27fff 100644
--- a/lib/tests/phpunit/parsers/MediaWikiNumberUnlocalizerTest.php
+++ b/lib/tests/phpunit/parsers/MediaWikiNumberUnlocalizerTest.php
@@ -108,6 +108,10 @@
array( '+.2' ),
array( '-.77' ),
+ array( '3e9' ),
+ array( '3.1E-9' ),
+ array( '-.7E+3' ),
+
array( '1,335.3' ),
array( '+1,333.2' ),
array( '-1,315.77' ),
@@ -153,6 +157,8 @@
array( '.e' ),
array( '12e' ),
array( 'E17' ),
+ array( '2e2.3' ),
+ array( '2e3e4' ),
array( '+-3' ),
array( '++7' ),
--
To view, visit https://gerrit.wikimedia.org/r/204242
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I495f4d12e1982e1d8bfdfad4a524c41682867fa6
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits