Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(330)

Unified Diff: icu4j/main/classes/core/src/com/ibm/icu/impl/number/parse/ScientificMatcher.java

Issue 335150043: Refreshing Number Parsing: ICU4J Base URL: svn+icussh://source.icu-project.org/repos/icu/trunk/
Patch Set: Replying to Andy feedback round one. See commit message. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: icu4j/main/classes/core/src/com/ibm/icu/impl/number/parse/ScientificMatcher.java
===================================================================
--- icu4j/main/classes/core/src/com/ibm/icu/impl/number/parse/ScientificMatcher.java (revision 0)
+++ icu4j/main/classes/core/src/com/ibm/icu/impl/number/parse/ScientificMatcher.java (revision 40784)
@@ -0,0 +1,106 @@
+// © 2017 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html#License
+package com.ibm.icu.impl.number.parse;
+
+import com.ibm.icu.number.Grouper;
+import com.ibm.icu.text.DecimalFormatSymbols;
+import com.ibm.icu.text.UnicodeSet;
+
+/**
+ * @author sffc
+ *
+ */
+public class ScientificMatcher implements NumberParseMatcher {
+
+ private final String exponentSeparatorString;
+ private final DecimalMatcher exponentMatcher;
+
+ public static ScientificMatcher getInstance(
+ DecimalFormatSymbols symbols,
+ Grouper grouper,
+ int parseFlags) {
+ // TODO: Static-initialize most common instances?
+ return new ScientificMatcher(symbols, grouper, parseFlags);
+ }
+
+ private ScientificMatcher(DecimalFormatSymbols symbols, Grouper grouper, int parseFlags) {
+ exponentSeparatorString = ParsingUtils.maybeFold(symbols.getExponentSeparator(), parseFlags);
+ exponentMatcher = DecimalMatcher.getInstance(symbols,
+ grouper,
+ ParsingUtils.PARSE_FLAG_DECIMAL_SCIENTIFIC | ParsingUtils.PARSE_FLAG_INTEGER_ONLY);
+ }
+
+ @Override
+ public boolean match(StringSegment segment, ParsedNumber result) {
+ // Only accept scientific notation after the mantissa.
+ if (!result.seenNumber()) {
+ return false;
+ }
+
+ // First match the scientific separator, and then match another number after it.
+ int overlap1 = segment.getCommonPrefixLength(exponentSeparatorString);
+ if (overlap1 == exponentSeparatorString.length()) {
+ // Full exponent separator match.
+
+ // First attempt to get a code point, returning true if we can't get one.
+ segment.adjustOffset(overlap1);
+ if (segment.length() == 0) {
+ return true;
+ }
+ int leadCp = segment.getCodePoint();
+ if (leadCp == -1) {
+ // Partial code point match
+ return true;
+ }
+
+ // Allow a sign, and then try to match digits.
+ boolean minusSign = false;
+ if (UnicodeSetStaticCache.get(UnicodeSetStaticCache.Key.MINUS_SIGN).contains(leadCp)) {
+ minusSign = true;
+ segment.adjustOffset(Character.charCount(leadCp));
+ } else if (UnicodeSetStaticCache.get(UnicodeSetStaticCache.Key.PLUS_SIGN).contains(leadCp)) {
+ segment.adjustOffset(Character.charCount(leadCp));
+ }
+
+ int digitsOffset = segment.getOffset();
+ boolean digitsReturnValue = exponentMatcher.match(segment, result, minusSign);
+ if (segment.getOffset() != digitsOffset) {
+ // At least one exponent digit was matched.
+ result.flags |= ParsedNumber.FLAG_HAS_EXPONENT;
+ } else {
+ // No exponent digits were matched; un-match the exponent separator.
+ segment.adjustOffset(-overlap1);
+ }
+ return digitsReturnValue;
+
+ } else if (overlap1 == segment.length()) {
+ // Partial exponent separator match
+ return true;
+ }
+
+ // No match
+ return false;
+ }
+
+ @Override
+ public UnicodeSet getLeadCodePoints() {
+ int cp = exponentSeparatorString.codePointAt(0);
+ if (cp == 'E') {
+ return UnicodeSetStaticCache.get(UnicodeSetStaticCache.Key.CAPITAL_E);
+ } else if (cp == 'e') {
+ return UnicodeSetStaticCache.get(UnicodeSetStaticCache.Key.FOLDED_E);
+ } else {
+ return new UnicodeSet().add(cp).freeze();
+ }
+ }
+
+ @Override
+ public void postProcess(ParsedNumber result) {
+ // No-op
+ }
+
+ @Override
+ public String toString() {
+ return "<ScientificMatcher " + exponentSeparatorString + ">";
+ }
+}
Property changes on: icu4j/main/classes/core/src/com/ibm/icu/impl/number/parse/ScientificMatcher.java
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+text/plain;charset=utf-8
\ No newline at end of property
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b