]> granicus.if.org Git - icu/commitdiff
ICU-11524 Fix inconsistency between fastpath and slowpath for when maxIntDigit = 0.
authorTravis Keep <keep94@gmail.com>
Thu, 12 Feb 2015 21:45:27 +0000 (21:45 +0000)
committerTravis Keep <keep94@gmail.com>
Thu, 12 Feb 2015 21:45:27 +0000 (21:45 +0000)
X-SVN-Rev: 37026

icu4c/source/i18n/decimfmt.cpp
icu4c/source/test/intltest/numfmtst.cpp
icu4c/source/test/intltest/numfmtst.h

index a0ee233ccd5eecc7bbdb983bc5bc09dc6c84a24e..def17a11ecf7b7e65b1b1aca0de6356d395b433e 100644 (file)
@@ -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<double>(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);
index 685e6636aef280238147b821d2bdd072234334f8..bd6caf56000d63669ded8408518dffb2a670219d 100644 (file)
@@ -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 */
index 41f9ded707ea8c58500e5cd504727e066904e7a2..c65c778f0d6476cd6ae8b6f73867d6e989a9967c 100644 (file)
@@ -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);