From 535e2f80d5cde76e5af310a4da144b3b4d773e07 Mon Sep 17 00:00:00 2001 From: Yoshito Umaoka Date: Tue, 14 Feb 2012 07:05:58 +0000 Subject: [PATCH] ICU-9109 Fixed missing digit issue with DecimalFormat in lenient mode. X-SVN-Rev: 31391 --- icu4c/source/i18n/decimfmt.cpp | 9 ++++++--- icu4c/source/test/intltest/numrgts.cpp | 27 +++++++++++++++++++++++++- icu4c/source/test/intltest/numrgts.h | 3 ++- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/icu4c/source/i18n/decimfmt.cpp b/icu4c/source/i18n/decimfmt.cpp index eb849277413..06b2043d7da 100644 --- a/icu4c/source/i18n/decimfmt.cpp +++ b/icu4c/source/i18n/decimfmt.cpp @@ -1617,8 +1617,8 @@ void DecimalFormat::parse(const UnicodeString& text, Formattable& result, ParsePosition& parsePosition, UBool parseCurrency) const { - int32_t backup; - int32_t i = backup = parsePosition.getIndex(); + int32_t startIdx, backup; + int32_t i = startIdx = backup = parsePosition.getIndex(); // clear any old contents in the result. In particular, clears any DigitList // that it may be holding. @@ -1677,7 +1677,7 @@ void DecimalFormat::parse(const UnicodeString& text, fPosPrefixPattern, fPosSuffixPattern, FALSE, UCURR_SYMBOL_NAME, parsePosition, *digits, status, currency)) { - parsePosition.setIndex(backup); + parsePosition.setIndex(startIdx); delete digits; return; } @@ -1899,6 +1899,9 @@ UBool DecimalFormat::subparse(const UnicodeString& text, } else if (strictParse){ parsePosition.setErrorIndex(position); return FALSE; + } else { + // Temporary set positive. This might be changed after checking suffix + parsedNum.append('+', err); } // Match padding before prefix diff --git a/icu4c/source/test/intltest/numrgts.cpp b/icu4c/source/test/intltest/numrgts.cpp index befd64801c7..ee6a88422f9 100644 --- a/icu4c/source/test/intltest/numrgts.cpp +++ b/icu4c/source/test/intltest/numrgts.cpp @@ -1,5 +1,5 @@ /*********************************************************************** - * Copyright (c) 1997-2011, International Business Machines Corporation + * Copyright (c) 1997-2012, International Business Machines Corporation * and others. All Rights Reserved. ***********************************************************************/ @@ -169,6 +169,7 @@ NumberFormatRegressionTest::runIndexedTest( int32_t index, UBool exec, const cha CASE(59,Test4243108); CASE(60,TestJ691); CASE(61,Test8199); + CASE(62,Test9109); default: name = ""; break; } @@ -2826,5 +2827,29 @@ void NumberFormatRegressionTest::Test8199(void) { delete nf; } +void NumberFormatRegressionTest::Test9109(void) { + UErrorCode status = U_ZERO_ERROR; + Formattable val; + ParsePosition pos; + DecimalFormat fmt("+##", status); + fmt.setLenient(TRUE); + + if (U_FAILURE(status)) { + errln("Failed to create DecimalFormat with pattern '+##'"); + } + + UnicodeString text("123"); + int32_t expected = 123; + int32_t expos = 3; + + fmt.parse(text, val, pos); + if (pos.getErrorIndex() >= 0) { + errln(UnicodeString("Parse failure at ") + pos.getErrorIndex()); + } else if (val.getLong() != 123) { + errln(UnicodeString("Incorrect parse result: ") + val.getLong() + " expected: " + expected); + } else if (pos.getIndex() != 3) { + errln(UnicodeString("Incorrect parse position: ") + pos.getIndex() + " expected: " + expos); + } +} #endif /* #if !UCONFIG_NO_FORMATTING */ diff --git a/icu4c/source/test/intltest/numrgts.h b/icu4c/source/test/intltest/numrgts.h index ec82e355008..b6ee3729f80 100644 --- a/icu4c/source/test/intltest/numrgts.h +++ b/icu4c/source/test/intltest/numrgts.h @@ -1,6 +1,6 @@ /*********************************************************************** * COPYRIGHT: - * Copyright (c) 1997-2011, International Business Machines Corporation + * Copyright (c) 1997-2012, International Business Machines Corporation * and others. All Rights Reserved. ***********************************************************************/ @@ -91,6 +91,7 @@ public: void Test4243108(void); void TestJ691(void); void Test8199(void); + void Test9109(void); protected: UBool failure(UErrorCode status, const UnicodeString& msg, UBool possibleDataError=FALSE);