]> granicus.if.org Git - icu/commitdiff
ICU-9293 Fixed a problem in strict currency parsing.
authorYoshito Umaoka <y.umaoka@gmail.com>
Fri, 4 May 2012 21:09:02 +0000 (21:09 +0000)
committerYoshito Umaoka <y.umaoka@gmail.com>
Fri, 4 May 2012 21:09:02 +0000 (21:09 +0000)
X-SVN-Rev: 31797

icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormat.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatRegressionTest.java

index bcfb7dd25e059c63496c60866e0b5f99d90e7179..ba398a83c50c5c23429624102b55a9cfc881601e 100644 (file)
@@ -2139,6 +2139,7 @@ public class DecimalFormat extends NumberFormat {
             boolean strictParse = isParseStrict();
             boolean strictFail = false; // did we exit with a strict parse failure?
             int lastGroup = -1; // where did we last see a grouping separator?
+            int digitStart = position; // where did the digit start?
             int gs2 = groupingSize2 == 0 ? groupingSize : groupingSize2;
 
             // equivalent grouping and decimal support
@@ -2189,8 +2190,8 @@ public class DecimalFormat extends NumberFormat {
                         // group. If there was a group separator before that, the group
                         // must == the secondary group length, else it can be <= the the
                         // secondary group length.
-                        if ((lastGroup != -1 && countCodePoints(text,lastGroup,backup) - 1 != gs2)
-                                || (lastGroup == -1 && countCodePoints(text,oldStart,position) - 1 > gs2)) {
+                        if ((lastGroup != -1 && countCodePoints(text, lastGroup, backup) - 1 != gs2)
+                                || (lastGroup == -1 && countCodePoints(text, digitStart, position) - 1 > gs2)) {
                             strictFail = true;
                             break;
                         }
@@ -2218,8 +2219,8 @@ public class DecimalFormat extends NumberFormat {
                 {
                     if (strictParse) {
                         if (backup != -1) {
-                            if ((lastGroup != -1 && countCodePoints(text,lastGroup,backup) - 1 != gs2)
-                                    || (lastGroup == -1 && countCodePoints(text,oldStart,position) - 1 > gs2)) {
+                            if ((lastGroup != -1 && countCodePoints(text, lastGroup, backup) - 1 != gs2)
+                                    || (lastGroup == -1 && countCodePoints(text, digitStart, position) - 1 > gs2)) {
                                 strictFail = true;
                                 break;
                             }
index d12b004a49008b598992f2de49db1b520c35de0b..55f2a5d0a9d530794a675c15f33f25312375e8fc 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *******************************************************************************
- * Copyright (C) 2001-2011, International Business Machines Corporation and    *
+ * Copyright (C) 2001-2012, International Business Machines Corporation and    *
  * others. All Rights Reserved.                                                *
  *******************************************************************************
  */
@@ -16,6 +16,7 @@ import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.text.ParseException;
+import java.text.ParsePosition;
 import java.util.Date;
 import java.util.Locale;
 
@@ -353,5 +354,25 @@ public class NumberFormatRegressionTest extends com.ibm.icu.dev.test.TestFmwk {
     checkNBSPPatternRT(testcase, nf);
     
     }
-    
+
+    /*
+     * Test case for #9293
+     * Parsing currency in strict mode
+     */
+    public void TestT9293() {
+        NumberFormat fmt = NumberFormat.getCurrencyInstance();
+        fmt.setParseStrict(true);
+
+        final int val = 123456;
+        String txt = fmt.format(123456);
+
+        ParsePosition pos = new ParsePosition(0);
+        Number num = fmt.parse(txt, pos);
+
+        if (pos.getErrorIndex() >= 0) {
+            errln("FAIL: Parsing " + txt + " - error index: " + pos.getErrorIndex());
+        } else if (val != num.intValue()) {
+            errln("FAIL: Parsed result: " + num + " - expected: " + val);
+        }
+    }
 }