From 06223fc7890af8832b3a87b05bdac52ff8b99fd4 Mon Sep 17 00:00:00 2001 From: Travis Keep Date: Thu, 12 Feb 2015 21:45:27 +0000 Subject: [PATCH] ICU-11524 Fix inconsistency between fastpath and slowpath for when maxIntDigit = 0. X-SVN-Rev: 37026 --- icu4c/source/i18n/decimfmt.cpp | 7 ++++++- icu4c/source/test/intltest/numfmtst.cpp | 13 ++++++++++++- icu4c/source/test/intltest/numfmtst.h | 3 ++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/icu4c/source/i18n/decimfmt.cpp b/icu4c/source/i18n/decimfmt.cpp index a0ee233ccd5..def17a11ecf 100644 --- a/icu4c/source/i18n/decimfmt.cpp +++ b/icu4c/source/i18n/decimfmt.cpp @@ -1435,6 +1435,8 @@ DecimalFormat::_format(int64_t number, U_ASSERT(destIdx >= 0); int32_t length = MAX_IDX - destIdx -1; /*int32_t prefixLen = */ appendAffix(appendTo, static_cast(number), handler, number<0, TRUE); + + // This will be at least 0 even if it was set to a negative number. int32_t maxIntDig = getMaximumIntegerDigits(); int32_t destlength = length<=maxIntDig?length:maxIntDig; // dest length pinned to max int digits @@ -1442,7 +1444,10 @@ DecimalFormat::_format(int64_t number, status = U_ILLEGAL_ARGUMENT_ERROR; } - int32_t prependZero = getMinimumIntegerDigits() - destlength; + int32_t minDigits = getMinimumIntegerDigits(); + + // We always want at least one digit, even if it is just a 0. + int32_t prependZero = (minDigits < 1 ? 1 : minDigits) - destlength; #ifdef FMT_DEBUG printf("prependZero=%d, length=%d, minintdig=%d maxintdig=%d destlength=%d skip=%d\n", prependZero, length, getMinimumIntegerDigits(), maxIntDig, destlength, length-destlength); diff --git a/icu4c/source/test/intltest/numfmtst.cpp b/icu4c/source/test/intltest/numfmtst.cpp index 685e6636aef..bd6caf56000 100644 --- a/icu4c/source/test/intltest/numfmtst.cpp +++ b/icu4c/source/test/intltest/numfmtst.cpp @@ -1,6 +1,6 @@ /******************************************************************** * COPYRIGHT: - * Copyright (c) 1997-2014, International Business Machines Corporation and + * Copyright (c) 1997-2015, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ /* Modification History: @@ -134,6 +134,7 @@ void NumberFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &n TESTCASE_AUTO(TestEquality); TESTCASE_AUTO(TestCurrencyUsage); TESTCASE_AUTO(TestDoubleLimit11439); + TESTCASE_AUTO(TestFastPathConsistent11524); TESTCASE_AUTO_END; } @@ -7773,5 +7774,15 @@ void NumberFormatTest::TestDoubleLimit11439() { } } +void NumberFormatTest::TestFastPathConsistent11524() { + UErrorCode status = U_ZERO_ERROR; + NumberFormat *fmt = NumberFormat::createInstance("en", status); + fmt->setMaximumIntegerDigits(INT32_MIN); + UnicodeString appendTo; + assertEquals("", "0", fmt->format(123, appendTo)); + appendTo.remove(); + assertEquals("", "0", fmt->format(12345, appendTo)); +} + #endif /* #if !UCONFIG_NO_FORMATTING */ diff --git a/icu4c/source/test/intltest/numfmtst.h b/icu4c/source/test/intltest/numfmtst.h index 41f9ded707e..c65c778f0d6 100644 --- a/icu4c/source/test/intltest/numfmtst.h +++ b/icu4c/source/test/intltest/numfmtst.h @@ -1,6 +1,6 @@ /************************************************************************ * COPYRIGHT: - * Copyright (c) 1997-2014, International Business Machines Corporation + * Copyright (c) 1997-2015, International Business Machines Corporation * and others. All Rights Reserved. ************************************************************************/ @@ -184,6 +184,7 @@ class NumberFormatTest: public CalendarTimeZoneTest { void TestCurrencyUsage(); void TestDoubleLimit11439(); + void TestFastPathConsistent11524(); private: UBool testFormattableAsUFormattable(const char *file, int line, Formattable &f); -- 2.40.0