]> granicus.if.org Git - icu/commitdiff
ICU-20347 In ICU4J, parsing emoty string should set PARSE_ERROR as before; check...
authorPeter Edberg <pedberg@unicode.org>
Wed, 6 Feb 2019 05:45:03 +0000 (21:45 -0800)
committerpedberg-icu <42151464+pedberg-icu@users.noreply.github.com>
Wed, 6 Feb 2019 08:22:47 +0000 (00:22 -0800)
icu4c/source/i18n/decimfmt.cpp
icu4c/source/test/cintltst/cnumtst.c
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java

index f1f5aa7c064c0a20707e6c05294fea3849dac4ae..11edbad47da5bf613f516c85e7ef1809504c6648 100644 (file)
@@ -715,6 +715,10 @@ void DecimalFormat::parse(const UnicodeString& text, Formattable& output,
         return;
     }
     if (parsePosition.getIndex() < 0 || parsePosition.getIndex() >= text.length()) {
+        if (parsePosition.getIndex() == text.length()) {
+            // If there is nothing to parse, it is an error
+            parsePosition.setErrorIndex(parsePosition.getIndex());
+        }
         return;
     }
 
index 9520bfe99157f8616e1063eaba2194bdcd8993cf..906cbb3aa144c6076622545f1f2aa7936565cef7 100644 (file)
@@ -3106,6 +3106,8 @@ static const ParseCaseItem parseCaseItems[] = {
     { "en", u"0,000",            TRUE,  FALSE, U_ZERO_ERROR,            5,          0, U_ZERO_ERROR,  5,                0.0, U_ZERO_ERROR,  5, "0" },
     { "en", u"1000,000",         FALSE, FALSE, U_PARSE_ERROR,           0,          0, U_PARSE_ERROR, 0,                0.0, U_PARSE_ERROR, 0, "" },
     { "en", u"1000,000",         TRUE,  FALSE, U_ZERO_ERROR,            8,    1000000, U_ZERO_ERROR,  8,          1000000.0, U_ZERO_ERROR,  8, "1000000" },
+    { "en", u"",                 FALSE, FALSE, U_PARSE_ERROR,           0,          0, U_PARSE_ERROR, 0,                0.0, U_PARSE_ERROR, 0, "" },
+    { "en", u"",                 TRUE,  FALSE, U_PARSE_ERROR,           0,          0, U_PARSE_ERROR, 0,                0.0, U_PARSE_ERROR, 0, "" },
     { "en", u"9999990000503021", FALSE, FALSE, U_INVALID_FORMAT_ERROR, 16, 2147483647, U_ZERO_ERROR, 16, 9999990000503020.0, U_ZERO_ERROR, 16, "9999990000503021" },
     { "en", u"9999990000503021", FALSE, TRUE,  U_INVALID_FORMAT_ERROR, 16, 2147483647, U_ZERO_ERROR, 16, 9999990000503020.0, U_ZERO_ERROR, 16, "9999990000503021" },
     { "en", u"1000000.5",        FALSE, FALSE, U_ZERO_ERROR,            9,    1000000, U_ZERO_ERROR,  9,          1000000.5, U_ZERO_ERROR,  9, "1.0000005E+6"},
index 02c87e828ceb8bbbe4f16c567612e666a47ccdf3..2079bdebdf5d8889fa19936f90cb569ebde7df59 100644 (file)
@@ -1730,6 +1730,21 @@ public class NumberFormatTest extends TestFmwk {
         }
     }
 
+    @Test
+    public void TestParseEmpty(){
+        String parsetxt = "";
+        NumberFormat numfmt = NumberFormat.getInstance(new ULocale("en_US"), NumberFormat.NUMBERSTYLE);
+        ParsePosition ppos = new ParsePosition(0);
+        Number value = null;
+        try {
+            value = numfmt.parse(parsetxt, ppos);
+            // Currently this succeeds (no exception) but returns null (for value).
+            logln("NumberFormat.parse empty string succeeds, ppos " + ppos.getIndex() + ", value " + value);
+        } catch (IllegalArgumentException e){
+            logln("NumberFormat.parse empty string sets IllegalArgumentException, ppos " + ppos.getIndex() + ", value " + value);
+        }
+     }
+
     @Test
     public void TestParseNull() throws ParseException {
         DecimalFormat df = new DecimalFormat();