]> granicus.if.org Git - icu/commitdiff
ICU-13513 Conditionally disabling fraction grouping parsing.
authorShane Carr <shane@unicode.org>
Tue, 30 Jan 2018 05:26:07 +0000 (05:26 +0000)
committerShane Carr <shane@unicode.org>
Tue, 30 Jan 2018 05:26:07 +0000 (05:26 +0000)
X-SVN-Rev: 40827

icu4j/main/classes/core/src/com/ibm/icu/impl/number/parse/DecimalMatcher.java
icu4j/main/classes/core/src/com/ibm/icu/impl/number/parse/NumberParserImpl.java
icu4j/main/classes/core/src/com/ibm/icu/impl/number/parse/ParsingUtils.java
icu4j/main/tests/core/src/com/ibm/icu/dev/data/numberformattestspecification.txt
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberRegressionTests.java

index 10dbc14265a3dc21368de6873b423be0b38e32f2..4a12af11c2069faa78e7c38b6403ffba6825463c 100644 (file)
@@ -21,6 +21,9 @@ public class DecimalMatcher implements NumberParseMatcher {
     /** If true, do not accept grouping separators at all */
     private final boolean groupingDisabled;
 
+    /** If true, do not accept fraction grouping separators */
+    private final boolean fractionGroupingDisabled;
+
     /** If true, do not accept numbers in the fraction */
     private final boolean integerOnly;
 
@@ -91,6 +94,8 @@ public class DecimalMatcher implements NumberParseMatcher {
 
         requireGroupingMatch = 0 != (parseFlags & ParsingUtils.PARSE_FLAG_STRICT_GROUPING_SIZE);
         groupingDisabled = 0 != (parseFlags & ParsingUtils.PARSE_FLAG_GROUPING_DISABLED);
+        fractionGroupingDisabled = 0 != (parseFlags
+                & ParsingUtils.PARSE_FLAG_FRACTION_GROUPING_DISABLED);
         integerOnly = 0 != (parseFlags & ParsingUtils.PARSE_FLAG_INTEGER_ONLY);
         isScientific = 0 != (parseFlags & ParsingUtils.PARSE_FLAG_DECIMAL_SCIENTIFIC);
         grouping1 = grouper.getPrimary();
@@ -248,6 +253,11 @@ public class DecimalMatcher implements NumberParseMatcher {
                     }
                 }
 
+                if (fractionGroupingDisabled && seenDecimal) {
+                    // Stop parsing here.
+                    break;
+                }
+
                 seenGrouping = true;
                 if (!groupingStringMatch) {
                     actualGroupingString = UCharacter.toString(cp);
index 780bfef8c2e33fdeaac17cd4d0d24b486d8962e6..73654686dbb752aef2084bc258fec9400e8170b8 100644 (file)
@@ -168,6 +168,8 @@ public class NumberParserImpl {
         boolean isStrict = properties.getParseMode() == ParseMode.STRICT;
         Grouper grouper = Grouper.defaults().withProperties(properties);
         int parseFlags = 0;
+        // Fraction grouping is disabled by default because it has never been supported in DecimalFormat
+        parseFlags |= ParsingUtils.PARSE_FLAG_FRACTION_GROUPING_DISABLED;
         if (!properties.getParseCaseSensitive()) {
             parseFlags |= ParsingUtils.PARSE_FLAG_IGNORE_CASE;
         }
index 1161146b0845e9a114833187ce11c76acc49f09f..c4a0005c0e70a3c4b4931e5bc940fba5531cfaea 100644 (file)
@@ -22,6 +22,7 @@ public class ParsingUtils {
     public static final int PARSE_FLAG_USE_FULL_AFFIXES = 0x0100;
     public static final int PARSE_FLAG_EXACT_AFFIX = 0x0200;
     public static final int PARSE_FLAG_PLUS_SIGN_ALLOWED = 0x0400;
+    public static final int PARSE_FLAG_FRACTION_GROUPING_DISABLED = 0x0800;
 
     public static void putLeadCodePoints(UnicodeSet input, UnicodeSet output) {
         for (EntryRange range : input.ranges()) {
index e0390010f0400b71e9fc1377dee5195a900f2400..5d3deb758fccefe99d9b622c35a139e91050d350 100644 (file)
@@ -753,10 +753,10 @@ parse     output  breaks
 +3.52EE4       3.52
 +1,234,567.8901        1234567.8901
 +1,23,4567.8901        1234567.8901
-// P supports grouping separators in the fraction; none of the others do.
-+1,23,4567.89,01       1234567.8901    CJK
+// Fraction grouping is disabled by default
++1,23,4567.89,01       1234567.89
 +1,23,456.78.9 123456.78
-+12.34,56      12.3456 CJK
++12.34,56      12.34
 +79,,20,3      79203
 +79  20 3      79203   K
 // Parsing stops at comma as it is different from other separators
@@ -860,9 +860,8 @@ parse       output  breaks
 // Minimum grouping is not satisfied below, but that's ok
 // because minimum grouping is optional.
 +1,234.5       1234.5
-// Comma after decimal means a fractional grouping separator
-// P fails since it finds an invalid grouping size
-+1,23,456.78,9 123456.789      JKP
+// Comma after decimal means parse to a comma
++1,23,456.78,9 123456.78
 // C and J fail upon seeing the second decimal point
 +1,23,456.78.9 123456.78       CJ
 +79    79
@@ -996,9 +995,8 @@ begin
 parse  output  breaks
 123.456        123456
 123,456        123.456
-// The separator after the comma can be inrepreted as a fractional grouping
-987,654.321    987.654321      CJK
-987,654 321    987.654321      CJK
+987,654.321    987.654
+987,654 321    987.654
 987.654,321    987654.321
 
 test select
@@ -1466,7 +1464,6 @@ parse     output  breaks
 test parse ignorables
 set locale ar
 // Note: Prefixes contain RLMs, as do some of the test cases.
-// P does not work on most of these right now.
 set pattern x a‎b0c df 
 set negativePrefix y g‎h
 set negativeSuffix i jk 
index 524ac2ce4ea98334b4b625d73ba295a113c50841..8a85f23a4d0e47992f090d0e1d5c599965ba7a05 100644 (file)
@@ -2801,7 +2801,8 @@ public class NumberFormatTest extends TestFmwk {
                 "0,",          // single zero before comma (not group separator) is not leading
                 "0.0",         // single zero before decimal followed by digit is not leading
                 "0. ",         // same as above before period (or decimal) is not leading
-                "0.100,,5",    // two commas stop parse of decimal
+                "0.100,5",     // comma stops parse of decimal (no grouping)
+                "0.100,,5",    // two commas also stops parse
                 ".00",         // leading decimal is ok, even with zeros
                 "1234567",     // group separators are not required
                 "12345, ",     // comma not followed by digit is not a group separator, but end of number
@@ -2957,7 +2958,7 @@ public class NumberFormatTest extends TestFmwk {
             try {
                 Number n = nf.parse(defaultNonLong[i]);
                 if (n instanceof Long) {
-                    errln("FAIL: parse returned a Long or a BigDecimal");
+                    errln("FAIL: parse returned a Long");
                 }
             } catch (ParseException e) {
                 errln("parse of '" + defaultNonLong[i] + "' threw exception: " + e);
index 9ad678579def8fee9829cc266e2e3d8cfa2f4b8a..0b163c8a7b5c207885e67e41988615f42e8dbe8d 100644 (file)
@@ -870,7 +870,7 @@ public class NumberRegressionTests extends TestFmwk {
         DecimalFormatSymbols(java.util.Locale.US));
         String text = "1.222,111";
         Number num = df.parse(text,new ParsePosition(0));
-        if (!num.toString().equals("1.222111"))
+        if (!num.toString().equals("1.222"))
             errln("\"" + text + "\"  is parsed as " + num);
         text = "1.222x111";
         num = df.parse(text,new ParsePosition(0));