]> granicus.if.org Git - icu/commitdiff
ICU-13738 Makes DecimalFormat#toNumberFormatter() return by const-reference.
authorShane Carr <shane@unicode.org>
Sat, 5 May 2018 01:47:12 +0000 (01:47 +0000)
committerShane Carr <shane@unicode.org>
Sat, 5 May 2018 01:47:12 +0000 (01:47 +0000)
X-SVN-Rev: 41327

icu4c/source/i18n/decimfmt.cpp
icu4c/source/i18n/plurfmt.cpp
icu4c/source/i18n/unicode/decimfmt.h

index 7d4d8b006d4909be1229aee5e17bba0496ffe488..662325bcc9e496c7d19a610f51216e17cef92609 100644 (file)
@@ -1113,10 +1113,8 @@ void DecimalFormat::formatToDecimalQuantity(const Formattable& number, DecimalQu
     output = std::move(obj.quantity);
 }
 
-number::LocalizedNumberFormatter&
-DecimalFormat::toNumberFormatter(number::LocalizedNumberFormatter& output) const {
-    output = *fields->formatter; // copy assignment
-    return output;
+const number::LocalizedNumberFormatter& DecimalFormat::toNumberFormatter() const {
+    return *fields->formatter;
 }
 
 /** Rebuilds the formatter object from the property bag. */
index a22941dc9c1e989c6b5486980f52588035813b26..2775766d32df801f1f3304d80ff6ee3bae44b9a8 100644 (file)
@@ -277,8 +277,7 @@ PluralFormat::format(const Formattable& numberObject, double number,
     UnicodeString numberString;
     auto *decFmt = dynamic_cast<DecimalFormat *>(numberFormat);
     if(decFmt != nullptr) {
-        number::LocalizedNumberFormatter formatter;
-        decFmt->toNumberFormatter(formatter).formatImpl(&data, status); // mutates &data
+        decFmt->toNumberFormatter().formatImpl(&data, status); // mutates &data
         numberString = data.string.toUnicodeString();
     } else {
         if (offset == 0) {
index 94945178a44b2aee754d50d49f82354bdfd49896..e7d034ff10c890d141dcf4ebe0c740ed9dddb16f 100644 (file)
@@ -2054,11 +2054,26 @@ class U_I18N_API DecimalFormat : public NumberFormat {
      * Converts this DecimalFormat to a NumberFormatter.  Starting in ICU 60,
      * NumberFormatter is the recommended way to format numbers.
      *
+     * NOTE: The returned LocalizedNumberFormatter is owned by this DecimalFormat.
+     * If a non-const method is called on the DecimalFormat, or if the DecimalFormat
+     * is deleted, the object becomes invalid. If you plan to keep the return value
+     * beyond the lifetime of the DecimalFormat, copy it to a local variable:
+     *
+     * <pre>
+     * LocalizedNumberFormatter f = df->toNumberFormatter();
+     * </pre>
+     *
+     * It is, however, safe to use the return value for chaining:
+     *
+     * <pre>
+     * FormattedNumber result = df->toNumberFormatter().formatDouble(123, status);
+     * </pre>
+     *
      * @param output The variable into which to store the LocalizedNumberFormatter.
      * @return The output variable, for chaining.
      * @draft ICU 62
      */
-    number::LocalizedNumberFormatter& toNumberFormatter(number::LocalizedNumberFormatter& output) const;
+    const number::LocalizedNumberFormatter& toNumberFormatter() const;
 
     /**
      * Return the class ID for this class.  This is useful only for