From: Shane Carr Date: Sat, 5 May 2018 01:47:12 +0000 (+0000) Subject: ICU-13738 Makes DecimalFormat#toNumberFormatter() return by const-reference. X-Git-Tag: release-62-rc~153 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bc9710aa8bd276d19c7b6c6ea5c5ad5e36d8169b;p=icu ICU-13738 Makes DecimalFormat#toNumberFormatter() return by const-reference. X-SVN-Rev: 41327 --- diff --git a/icu4c/source/i18n/decimfmt.cpp b/icu4c/source/i18n/decimfmt.cpp index 7d4d8b006d4..662325bcc9e 100644 --- a/icu4c/source/i18n/decimfmt.cpp +++ b/icu4c/source/i18n/decimfmt.cpp @@ -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. */ diff --git a/icu4c/source/i18n/plurfmt.cpp b/icu4c/source/i18n/plurfmt.cpp index a22941dc9c1..2775766d32d 100644 --- a/icu4c/source/i18n/plurfmt.cpp +++ b/icu4c/source/i18n/plurfmt.cpp @@ -277,8 +277,7 @@ PluralFormat::format(const Formattable& numberObject, double number, UnicodeString numberString; auto *decFmt = dynamic_cast(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) { diff --git a/icu4c/source/i18n/unicode/decimfmt.h b/icu4c/source/i18n/unicode/decimfmt.h index 94945178a44..e7d034ff10c 100644 --- a/icu4c/source/i18n/unicode/decimfmt.h +++ b/icu4c/source/i18n/unicode/decimfmt.h @@ -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: + * + *
+     * LocalizedNumberFormatter f = df->toNumberFormatter();
+     * 
+ * + * It is, however, safe to use the return value for chaining: + * + *
+     * FormattedNumber result = df->toNumberFormatter().formatDouble(123, status);
+     * 
+ * * @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