From e8bcc60d6f71937e80057a55f0194c9be276cd2b Mon Sep 17 00:00:00 2001 From: Peter Edberg Date: Tue, 5 Feb 2019 21:45:03 -0800 Subject: [PATCH] ICU-20347 In ICU4J, parsing emoty string should set PARSE_ERROR as before; check ICU4J behavior --- icu4c/source/i18n/decimfmt.cpp | 4 ++++ icu4c/source/test/cintltst/cnumtst.c | 2 ++ .../ibm/icu/dev/test/format/NumberFormatTest.java | 15 +++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/icu4c/source/i18n/decimfmt.cpp b/icu4c/source/i18n/decimfmt.cpp index f1f5aa7c064..11edbad47da 100644 --- a/icu4c/source/i18n/decimfmt.cpp +++ b/icu4c/source/i18n/decimfmt.cpp @@ -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; } diff --git a/icu4c/source/test/cintltst/cnumtst.c b/icu4c/source/test/cintltst/cnumtst.c index 9520bfe9915..906cbb3aa14 100644 --- a/icu4c/source/test/cintltst/cnumtst.c +++ b/icu4c/source/test/cintltst/cnumtst.c @@ -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"}, diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java index 02c87e828ce..2079bdebdf5 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java @@ -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(); -- 2.40.0