Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 1 | // © 2016 and later: Unicode, Inc. and others. |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 2 | // License & terms of use: http://www.unicode.org/copyright.html |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 3 | /* |
| 4 | ******************************************************************************* |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 5 | * Copyright (C) 1997-2015, International Business Machines Corporation and |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 6 | * others. All Rights Reserved. |
| 7 | ******************************************************************************* |
| 8 | * |
| 9 | * File NUMFMT.CPP |
| 10 | * |
| 11 | * Modification History: |
| 12 | * |
| 13 | * Date Name Description |
| 14 | * 02/19/97 aliu Converted from java. |
| 15 | * 03/18/97 clhuang Implemented with C++ APIs. |
Frank Tang | 7e7574b | 2021-04-13 21:19:13 -0700 | [diff] [blame] | 16 | * 04/17/97 aliu Enlarged MAX_INTEGER_DIGITS to fully accommodate the |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 17 | * largest double, by default. |
| 18 | * Changed DigitCount to int per code review. |
| 19 | * 07/20/98 stephen Changed operator== to check for grouping |
| 20 | * Changed setMaxIntegerDigits per Java implementation. |
| 21 | * Changed setMinIntegerDigits per Java implementation. |
| 22 | * Changed setMinFractionDigits per Java implementation. |
| 23 | * Changed setMaxFractionDigits per Java implementation. |
| 24 | ******************************************************************************** |
| 25 | */ |
| 26 | |
| 27 | #include "unicode/utypes.h" |
| 28 | |
| 29 | #if !UCONFIG_NO_FORMATTING |
| 30 | |
| 31 | #include "unicode/numfmt.h" |
| 32 | #include "unicode/locid.h" |
| 33 | #include "unicode/dcfmtsym.h" |
| 34 | #include "unicode/decimfmt.h" |
| 35 | #include "unicode/ustring.h" |
| 36 | #include "unicode/ucurr.h" |
| 37 | #include "unicode/curramt.h" |
| 38 | #include "unicode/numsys.h" |
| 39 | #include "unicode/rbnf.h" |
| 40 | #include "unicode/localpointer.h" |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 41 | #include "unicode/udisplaycontext.h" |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 42 | #include "charstr.h" |
| 43 | #include "winnmfmt.h" |
| 44 | #include "uresimp.h" |
| 45 | #include "uhash.h" |
| 46 | #include "cmemory.h" |
| 47 | #include "servloc.h" |
| 48 | #include "ucln_in.h" |
| 49 | #include "cstring.h" |
| 50 | #include "putilimp.h" |
| 51 | #include "uassert.h" |
| 52 | #include "umutex.h" |
| 53 | #include "mutex.h" |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 54 | #include <float.h> |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 55 | #include "sharednumberformat.h" |
| 56 | #include "unifiedcache.h" |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 57 | #include "number_decimalquantity.h" |
| 58 | #include "number_utils.h" |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 59 | |
| 60 | //#define FMT_DEBUG |
| 61 | |
| 62 | #ifdef FMT_DEBUG |
| 63 | #include <stdio.h> |
| 64 | static inline void debugout(UnicodeString s) { |
| 65 | char buf[2000]; |
| 66 | s.extract((int32_t) 0, s.length(), buf); |
| 67 | printf("%s", buf); |
| 68 | } |
| 69 | #define debug(x) printf("%s", x); |
| 70 | #else |
| 71 | #define debugout(x) |
| 72 | #define debug(x) |
| 73 | #endif |
| 74 | |
| 75 | // If no number pattern can be located for a locale, this is the last |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 76 | // resort. The patterns are same as the ones in root locale. |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 77 | static const UChar gLastResortDecimalPat[] = { |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 78 | 0x23, 0x2C, 0x23, 0x23, 0x30, 0x2E, 0x23, 0x23, 0x23, 0 /* "#,##0.###" */ |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 79 | }; |
| 80 | static const UChar gLastResortCurrencyPat[] = { |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 81 | 0xA4, 0xA0, 0x23, 0x2C, 0x23, 0x23, 0x30, 0x2E, 0x30, 0x30, 0 /* "\u00A4\u00A0#,##0.00" */ |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 82 | }; |
| 83 | static const UChar gLastResortPercentPat[] = { |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 84 | 0x23, 0x2C, 0x23, 0x23, 0x30, 0x25, 0 /* "#,##0%" */ |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 85 | }; |
| 86 | static const UChar gLastResortScientificPat[] = { |
| 87 | 0x23, 0x45, 0x30, 0 /* "#E0" */ |
| 88 | }; |
| 89 | static const UChar gLastResortIsoCurrencyPat[] = { |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 90 | 0xA4, 0xA4, 0xA0, 0x23, 0x2C, 0x23, 0x23, 0x30, 0x2E, 0x30, 0x30, 0 /* "\u00A4\u00A4\u00A0#,##0.00" */ |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 91 | }; |
| 92 | static const UChar gLastResortPluralCurrencyPat[] = { |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 93 | 0x23, 0x2C, 0x23, 0x23, 0x30, 0x2E, 0x23, 0x23, 0x23, 0x20, 0xA4, 0xA4, 0xA4, 0 /* "#,##0.### \u00A4\u00A4\u00A4*/ |
| 94 | }; |
| 95 | static const UChar gLastResortAccountingCurrencyPat[] = { |
| 96 | 0xA4, 0xA0, 0x23, 0x2C, 0x23, 0x23, 0x30, 0x2E, 0x30, 0x30, 0 /* "\u00A4\u00A0#,##0.00" */ |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | static const UChar gSingleCurrencySign[] = {0xA4, 0}; |
| 100 | static const UChar gDoubleCurrencySign[] = {0xA4, 0xA4, 0}; |
| 101 | |
| 102 | static const UChar gSlash = 0x2f; |
| 103 | |
| 104 | // If the maximum base 10 exponent were 4, then the largest number would |
| 105 | // be 99,999 which has 5 digits. |
| 106 | // On IEEE754 systems gMaxIntegerDigits is 308 + possible denormalized 15 digits + rounding digit |
| 107 | // With big decimal, the max exponent is 999,999,999 and the max number of digits is the same, 999,999,999 |
| 108 | const int32_t icu::NumberFormat::gDefaultMaxIntegerDigits = 2000000000; |
| 109 | const int32_t icu::NumberFormat::gDefaultMinIntegerDigits = 127; |
| 110 | |
| 111 | static const UChar * const gLastResortNumberPatterns[UNUM_FORMAT_STYLE_COUNT] = { |
| 112 | NULL, // UNUM_PATTERN_DECIMAL |
| 113 | gLastResortDecimalPat, // UNUM_DECIMAL |
| 114 | gLastResortCurrencyPat, // UNUM_CURRENCY |
| 115 | gLastResortPercentPat, // UNUM_PERCENT |
| 116 | gLastResortScientificPat, // UNUM_SCIENTIFIC |
| 117 | NULL, // UNUM_SPELLOUT |
| 118 | NULL, // UNUM_ORDINAL |
| 119 | NULL, // UNUM_DURATION |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 120 | gLastResortDecimalPat, // UNUM_NUMBERING_SYSTEM |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 121 | NULL, // UNUM_PATTERN_RULEBASED |
| 122 | gLastResortIsoCurrencyPat, // UNUM_CURRENCY_ISO |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 123 | gLastResortPluralCurrencyPat, // UNUM_CURRENCY_PLURAL |
| 124 | gLastResortAccountingCurrencyPat, // UNUM_CURRENCY_ACCOUNTING |
| 125 | gLastResortCurrencyPat, // UNUM_CASH_CURRENCY |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 126 | NULL, // UNUM_DECIMAL_COMPACT_SHORT |
| 127 | NULL, // UNUM_DECIMAL_COMPACT_LONG |
| 128 | gLastResortCurrencyPat, // UNUM_CURRENCY_STANDARD |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | // Keys used for accessing resource bundles |
| 132 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 133 | static const icu::number::impl::CldrPatternStyle gFormatCldrStyles[UNUM_FORMAT_STYLE_COUNT] = { |
| 134 | /* NULL */ icu::number::impl::CLDR_PATTERN_STYLE_COUNT, // UNUM_PATTERN_DECIMAL |
| 135 | icu::number::impl::CLDR_PATTERN_STYLE_DECIMAL, // UNUM_DECIMAL |
| 136 | icu::number::impl::CLDR_PATTERN_STYLE_CURRENCY, // UNUM_CURRENCY |
| 137 | icu::number::impl::CLDR_PATTERN_STYLE_PERCENT, // UNUM_PERCENT |
| 138 | icu::number::impl::CLDR_PATTERN_STYLE_SCIENTIFIC, // UNUM_SCIENTIFIC |
| 139 | /* NULL */ icu::number::impl::CLDR_PATTERN_STYLE_COUNT, // UNUM_SPELLOUT |
| 140 | /* NULL */ icu::number::impl::CLDR_PATTERN_STYLE_COUNT, // UNUM_ORDINAL |
| 141 | /* NULL */ icu::number::impl::CLDR_PATTERN_STYLE_COUNT, // UNUM_DURATION |
| 142 | /* NULL */ icu::number::impl::CLDR_PATTERN_STYLE_COUNT, // UNUM_NUMBERING_SYSTEM |
| 143 | /* NULL */ icu::number::impl::CLDR_PATTERN_STYLE_COUNT, // UNUM_PATTERN_RULEBASED |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 144 | // For UNUM_CURRENCY_ISO and UNUM_CURRENCY_PLURAL, |
| 145 | // the pattern is the same as the pattern of UNUM_CURRENCY |
| 146 | // except for replacing the single currency sign with |
| 147 | // double currency sign or triple currency sign. |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 148 | icu::number::impl::CLDR_PATTERN_STYLE_CURRENCY, // UNUM_CURRENCY_ISO |
| 149 | icu::number::impl::CLDR_PATTERN_STYLE_CURRENCY, // UNUM_CURRENCY_PLURAL |
| 150 | icu::number::impl::CLDR_PATTERN_STYLE_ACCOUNTING, // UNUM_CURRENCY_ACCOUNTING |
| 151 | icu::number::impl::CLDR_PATTERN_STYLE_CURRENCY, // UNUM_CASH_CURRENCY |
| 152 | /* NULL */ icu::number::impl::CLDR_PATTERN_STYLE_COUNT, // UNUM_DECIMAL_COMPACT_SHORT |
| 153 | /* NULL */ icu::number::impl::CLDR_PATTERN_STYLE_COUNT, // UNUM_DECIMAL_COMPACT_LONG |
| 154 | icu::number::impl::CLDR_PATTERN_STYLE_CURRENCY, // UNUM_CURRENCY_STANDARD |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | // Static hashtable cache of NumberingSystem objects used by NumberFormat |
| 158 | static UHashtable * NumberingSystem_cache = NULL; |
Frank Tang | 1c67b4e | 2022-05-18 10:13:51 -0700 | [diff] [blame] | 159 | static icu::UInitOnce gNSCacheInitOnce {}; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 160 | |
| 161 | #if !UCONFIG_NO_SERVICE |
| 162 | static icu::ICULocaleService* gService = NULL; |
Frank Tang | 1c67b4e | 2022-05-18 10:13:51 -0700 | [diff] [blame] | 163 | static icu::UInitOnce gServiceInitOnce {}; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 164 | #endif |
| 165 | |
| 166 | /** |
| 167 | * Release all static memory held by Number Format. |
| 168 | */ |
| 169 | U_CDECL_BEGIN |
| 170 | static void U_CALLCONV |
| 171 | deleteNumberingSystem(void *obj) { |
| 172 | delete (icu::NumberingSystem *)obj; |
| 173 | } |
| 174 | |
| 175 | static UBool U_CALLCONV numfmt_cleanup(void) { |
| 176 | #if !UCONFIG_NO_SERVICE |
| 177 | gServiceInitOnce.reset(); |
| 178 | if (gService) { |
| 179 | delete gService; |
| 180 | gService = NULL; |
| 181 | } |
| 182 | #endif |
| 183 | gNSCacheInitOnce.reset(); |
| 184 | if (NumberingSystem_cache) { |
| 185 | // delete NumberingSystem_cache; |
| 186 | uhash_close(NumberingSystem_cache); |
| 187 | NumberingSystem_cache = NULL; |
| 188 | } |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 189 | return true; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 190 | } |
| 191 | U_CDECL_END |
| 192 | |
| 193 | // ***************************************************************************** |
| 194 | // class NumberFormat |
| 195 | // ***************************************************************************** |
| 196 | |
| 197 | U_NAMESPACE_BEGIN |
| 198 | |
| 199 | UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(NumberFormat) |
| 200 | |
| 201 | #if !UCONFIG_NO_SERVICE |
| 202 | // ------------------------------------- |
| 203 | // SimpleNumberFormatFactory implementation |
| 204 | NumberFormatFactory::~NumberFormatFactory() {} |
| 205 | SimpleNumberFormatFactory::SimpleNumberFormatFactory(const Locale& locale, UBool visible) |
| 206 | : _visible(visible) |
| 207 | { |
| 208 | LocaleUtility::initNameFromLocale(locale, _id); |
| 209 | } |
| 210 | |
| 211 | SimpleNumberFormatFactory::~SimpleNumberFormatFactory() {} |
| 212 | |
| 213 | UBool SimpleNumberFormatFactory::visible(void) const { |
| 214 | return _visible; |
| 215 | } |
| 216 | |
| 217 | const UnicodeString * |
| 218 | SimpleNumberFormatFactory::getSupportedIDs(int32_t &count, UErrorCode& status) const |
| 219 | { |
| 220 | if (U_SUCCESS(status)) { |
| 221 | count = 1; |
| 222 | return &_id; |
| 223 | } |
| 224 | count = 0; |
| 225 | return NULL; |
| 226 | } |
| 227 | #endif /* #if !UCONFIG_NO_SERVICE */ |
| 228 | |
| 229 | // ------------------------------------- |
| 230 | // default constructor |
| 231 | NumberFormat::NumberFormat() |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 232 | : fGroupingUsed(true), |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 233 | fMaxIntegerDigits(gDefaultMaxIntegerDigits), |
| 234 | fMinIntegerDigits(1), |
| 235 | fMaxFractionDigits(3), // invariant, >= minFractionDigits |
| 236 | fMinFractionDigits(0), |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 237 | fParseIntegerOnly(false), |
| 238 | fLenient(false), |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 239 | fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE) |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 240 | { |
| 241 | fCurrency[0] = 0; |
| 242 | } |
| 243 | |
| 244 | // ------------------------------------- |
| 245 | |
| 246 | NumberFormat::~NumberFormat() |
| 247 | { |
| 248 | } |
| 249 | |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 250 | SharedNumberFormat::~SharedNumberFormat() { |
| 251 | delete ptr; |
| 252 | } |
| 253 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 254 | // ------------------------------------- |
| 255 | // copy constructor |
| 256 | |
| 257 | NumberFormat::NumberFormat(const NumberFormat &source) |
| 258 | : Format(source) |
| 259 | { |
| 260 | *this = source; |
| 261 | } |
| 262 | |
| 263 | // ------------------------------------- |
| 264 | // assignment operator |
| 265 | |
| 266 | NumberFormat& |
| 267 | NumberFormat::operator=(const NumberFormat& rhs) |
| 268 | { |
| 269 | if (this != &rhs) |
| 270 | { |
| 271 | Format::operator=(rhs); |
| 272 | fGroupingUsed = rhs.fGroupingUsed; |
| 273 | fMaxIntegerDigits = rhs.fMaxIntegerDigits; |
| 274 | fMinIntegerDigits = rhs.fMinIntegerDigits; |
| 275 | fMaxFractionDigits = rhs.fMaxFractionDigits; |
| 276 | fMinFractionDigits = rhs.fMinFractionDigits; |
| 277 | fParseIntegerOnly = rhs.fParseIntegerOnly; |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 278 | u_strncpy(fCurrency, rhs.fCurrency, 3); |
| 279 | fCurrency[3] = 0; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 280 | fLenient = rhs.fLenient; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 281 | fCapitalizationContext = rhs.fCapitalizationContext; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 282 | } |
| 283 | return *this; |
| 284 | } |
| 285 | |
| 286 | // ------------------------------------- |
| 287 | |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 288 | bool |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 289 | NumberFormat::operator==(const Format& that) const |
| 290 | { |
| 291 | // Format::operator== guarantees this cast is safe |
| 292 | NumberFormat* other = (NumberFormat*)&that; |
| 293 | |
| 294 | #ifdef FMT_DEBUG |
| 295 | // This code makes it easy to determine why two format objects that should |
| 296 | // be equal aren't. |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 297 | UBool first = true; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 298 | if (!Format::operator==(that)) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 299 | if (first) { printf("[ "); first = false; } else { printf(", "); } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 300 | debug("Format::!="); |
| 301 | } |
| 302 | if (!(fMaxIntegerDigits == other->fMaxIntegerDigits && |
| 303 | fMinIntegerDigits == other->fMinIntegerDigits)) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 304 | if (first) { printf("[ "); first = false; } else { printf(", "); } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 305 | debug("Integer digits !="); |
| 306 | } |
| 307 | if (!(fMaxFractionDigits == other->fMaxFractionDigits && |
| 308 | fMinFractionDigits == other->fMinFractionDigits)) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 309 | if (first) { printf("[ "); first = false; } else { printf(", "); } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 310 | debug("Fraction digits !="); |
| 311 | } |
| 312 | if (!(fGroupingUsed == other->fGroupingUsed)) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 313 | if (first) { printf("[ "); first = false; } else { printf(", "); } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 314 | debug("fGroupingUsed != "); |
| 315 | } |
| 316 | if (!(fParseIntegerOnly == other->fParseIntegerOnly)) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 317 | if (first) { printf("[ "); first = false; } else { printf(", "); } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 318 | debug("fParseIntegerOnly != "); |
| 319 | } |
| 320 | if (!(u_strcmp(fCurrency, other->fCurrency) == 0)) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 321 | if (first) { printf("[ "); first = false; } else { printf(", "); } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 322 | debug("fCurrency !="); |
| 323 | } |
| 324 | if (!(fLenient == other->fLenient)) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 325 | if (first) { printf("[ "); first = false; } else { printf(", "); } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 326 | debug("fLenient != "); |
| 327 | } |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 328 | if (!(fCapitalizationContext == other->fCapitalizationContext)) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 329 | if (first) { printf("[ "); first = false; } else { printf(", "); } |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 330 | debug("fCapitalizationContext != "); |
| 331 | } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 332 | if (!first) { printf(" ]"); } |
| 333 | #endif |
| 334 | |
| 335 | return ((this == &that) || |
| 336 | ((Format::operator==(that) && |
| 337 | fMaxIntegerDigits == other->fMaxIntegerDigits && |
| 338 | fMinIntegerDigits == other->fMinIntegerDigits && |
| 339 | fMaxFractionDigits == other->fMaxFractionDigits && |
| 340 | fMinFractionDigits == other->fMinFractionDigits && |
| 341 | fGroupingUsed == other->fGroupingUsed && |
| 342 | fParseIntegerOnly == other->fParseIntegerOnly && |
| 343 | u_strcmp(fCurrency, other->fCurrency) == 0 && |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 344 | fLenient == other->fLenient && |
| 345 | fCapitalizationContext == other->fCapitalizationContext))); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | // ------------------------------------- |
| 349 | // Default implementation sets unsupported error; subclasses should |
| 350 | // override. |
| 351 | |
| 352 | UnicodeString& |
| 353 | NumberFormat::format(double /* unused number */, |
| 354 | UnicodeString& toAppendTo, |
| 355 | FieldPositionIterator* /* unused posIter */, |
| 356 | UErrorCode& status) const |
| 357 | { |
| 358 | if (!U_FAILURE(status)) { |
| 359 | status = U_UNSUPPORTED_ERROR; |
| 360 | } |
| 361 | return toAppendTo; |
| 362 | } |
| 363 | |
| 364 | // ------------------------------------- |
| 365 | // Default implementation sets unsupported error; subclasses should |
| 366 | // override. |
| 367 | |
| 368 | UnicodeString& |
| 369 | NumberFormat::format(int32_t /* unused number */, |
| 370 | UnicodeString& toAppendTo, |
| 371 | FieldPositionIterator* /* unused posIter */, |
| 372 | UErrorCode& status) const |
| 373 | { |
| 374 | if (!U_FAILURE(status)) { |
| 375 | status = U_UNSUPPORTED_ERROR; |
| 376 | } |
| 377 | return toAppendTo; |
| 378 | } |
| 379 | |
| 380 | // ------------------------------------- |
| 381 | // Default implementation sets unsupported error; subclasses should |
| 382 | // override. |
| 383 | |
| 384 | UnicodeString& |
| 385 | NumberFormat::format(int64_t /* unused number */, |
| 386 | UnicodeString& toAppendTo, |
| 387 | FieldPositionIterator* /* unused posIter */, |
| 388 | UErrorCode& status) const |
| 389 | { |
| 390 | if (!U_FAILURE(status)) { |
| 391 | status = U_UNSUPPORTED_ERROR; |
| 392 | } |
| 393 | return toAppendTo; |
| 394 | } |
| 395 | |
| 396 | // ------------------------------------------ |
| 397 | // These functions add the status code, just fall back to the non-status versions |
| 398 | UnicodeString& |
| 399 | NumberFormat::format(double number, |
| 400 | UnicodeString& appendTo, |
| 401 | FieldPosition& pos, |
| 402 | UErrorCode &status) const { |
| 403 | if(U_SUCCESS(status)) { |
| 404 | return format(number,appendTo,pos); |
| 405 | } else { |
| 406 | return appendTo; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | UnicodeString& |
| 411 | NumberFormat::format(int32_t number, |
| 412 | UnicodeString& appendTo, |
| 413 | FieldPosition& pos, |
| 414 | UErrorCode &status) const { |
| 415 | if(U_SUCCESS(status)) { |
| 416 | return format(number,appendTo,pos); |
| 417 | } else { |
| 418 | return appendTo; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | UnicodeString& |
| 423 | NumberFormat::format(int64_t number, |
| 424 | UnicodeString& appendTo, |
| 425 | FieldPosition& pos, |
| 426 | UErrorCode &status) const { |
| 427 | if(U_SUCCESS(status)) { |
| 428 | return format(number,appendTo,pos); |
| 429 | } else { |
| 430 | return appendTo; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | |
| 435 | |
| 436 | // ------------------------------------- |
| 437 | // Decimal Number format() default implementation |
| 438 | // Subclasses do not normally override this function, but rather the DigitList |
| 439 | // formatting functions.. |
| 440 | // The expected call chain from here is |
| 441 | // this function -> |
| 442 | // NumberFormat::format(Formattable -> |
| 443 | // DecimalFormat::format(DigitList |
| 444 | // |
| 445 | // Or, for subclasses of Formattable that do not know about DigitList, |
| 446 | // this Function -> |
| 447 | // NumberFormat::format(Formattable -> |
| 448 | // NumberFormat::format(DigitList -> |
| 449 | // XXXFormat::format(double |
| 450 | |
| 451 | UnicodeString& |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 452 | NumberFormat::format(StringPiece decimalNum, |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 453 | UnicodeString& toAppendTo, |
| 454 | FieldPositionIterator* fpi, |
| 455 | UErrorCode& status) const |
| 456 | { |
| 457 | Formattable f; |
| 458 | f.setDecimalNumber(decimalNum, status); |
| 459 | format(f, toAppendTo, fpi, status); |
| 460 | return toAppendTo; |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * |
| 465 | // Formats the number object and save the format |
| 466 | // result in the toAppendTo string buffer. |
| 467 | |
| 468 | // utility to save/restore state, used in two overloads |
| 469 | // of format(const Formattable&...) below. |
| 470 | * |
| 471 | * Old purpose of ArgExtractor was to avoid const. Not thread safe! |
| 472 | * |
| 473 | * keeping it around as a shim. |
| 474 | */ |
| 475 | class ArgExtractor { |
| 476 | const Formattable* num; |
| 477 | UChar save[4]; |
| 478 | UBool fWasCurrency; |
| 479 | |
| 480 | public: |
| 481 | ArgExtractor(const NumberFormat& nf, const Formattable& obj, UErrorCode& status); |
| 482 | ~ArgExtractor(); |
| 483 | |
| 484 | const Formattable* number(void) const; |
| 485 | const UChar *iso(void) const; |
| 486 | UBool wasCurrency(void) const; |
| 487 | }; |
| 488 | |
| 489 | inline const Formattable* |
| 490 | ArgExtractor::number(void) const { |
| 491 | return num; |
| 492 | } |
| 493 | |
| 494 | inline UBool |
| 495 | ArgExtractor::wasCurrency(void) const { |
| 496 | return fWasCurrency; |
| 497 | } |
| 498 | |
| 499 | inline const UChar * |
| 500 | ArgExtractor::iso(void) const { |
| 501 | return save; |
| 502 | } |
| 503 | |
| 504 | ArgExtractor::ArgExtractor(const NumberFormat& /*nf*/, const Formattable& obj, UErrorCode& /*status*/) |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 505 | : num(&obj), fWasCurrency(false) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 506 | |
| 507 | const UObject* o = obj.getObject(); // most commonly o==NULL |
| 508 | const CurrencyAmount* amt; |
| 509 | if (o != NULL && (amt = dynamic_cast<const CurrencyAmount*>(o)) != NULL) { |
| 510 | // getISOCurrency() returns a pointer to internal storage, so we |
| 511 | // copy it to retain it across the call to setCurrency(). |
| 512 | //const UChar* curr = amt->getISOCurrency(); |
| 513 | u_strcpy(save, amt->getISOCurrency()); |
| 514 | num = &amt->getNumber(); |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 515 | fWasCurrency=true; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 516 | } else { |
| 517 | save[0]=0; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | ArgExtractor::~ArgExtractor() { |
| 522 | } |
| 523 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 524 | UnicodeString& NumberFormat::format(const number::impl::DecimalQuantity &number, |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 525 | UnicodeString& appendTo, |
| 526 | FieldPositionIterator* posIter, |
| 527 | UErrorCode& status) const { |
| 528 | // DecimalFormat overrides this function, and handles DigitList based big decimals. |
Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 529 | // Other subclasses (ChoiceFormat) do not (yet) handle DigitLists, |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 530 | // so this default implementation falls back to formatting decimal numbers as doubles. |
| 531 | if (U_FAILURE(status)) { |
| 532 | return appendTo; |
| 533 | } |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 534 | double dnum = number.toDouble(); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 535 | format(dnum, appendTo, posIter, status); |
| 536 | return appendTo; |
| 537 | } |
| 538 | |
| 539 | |
| 540 | |
| 541 | UnicodeString& |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 542 | NumberFormat::format(const number::impl::DecimalQuantity &number, |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 543 | UnicodeString& appendTo, |
| 544 | FieldPosition& pos, |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 545 | UErrorCode &status) const { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 546 | // DecimalFormat overrides this function, and handles DigitList based big decimals. |
Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 547 | // Other subclasses (ChoiceFormat) do not (yet) handle DigitLists, |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 548 | // so this default implementation falls back to formatting decimal numbers as doubles. |
| 549 | if (U_FAILURE(status)) { |
| 550 | return appendTo; |
| 551 | } |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 552 | double dnum = number.toDouble(); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 553 | format(dnum, appendTo, pos, status); |
| 554 | return appendTo; |
| 555 | } |
| 556 | |
| 557 | UnicodeString& |
| 558 | NumberFormat::format(const Formattable& obj, |
| 559 | UnicodeString& appendTo, |
| 560 | FieldPosition& pos, |
| 561 | UErrorCode& status) const |
| 562 | { |
| 563 | if (U_FAILURE(status)) return appendTo; |
| 564 | |
| 565 | ArgExtractor arg(*this, obj, status); |
| 566 | const Formattable *n = arg.number(); |
| 567 | const UChar *iso = arg.iso(); |
| 568 | |
| 569 | if(arg.wasCurrency() && u_strcmp(iso, getCurrency())) { |
| 570 | // trying to format a different currency. |
| 571 | // Right now, we clone. |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 572 | LocalPointer<NumberFormat> cloneFmt(this->clone()); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 573 | cloneFmt->setCurrency(iso, status); |
| 574 | // next line should NOT recurse, because n is numeric whereas obj was a wrapper around currency amount. |
| 575 | return cloneFmt->format(*n, appendTo, pos, status); |
| 576 | } |
| 577 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 578 | if (n->isNumeric() && n->getDecimalQuantity() != NULL) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 579 | // Decimal Number. We will have a DigitList available if the value was |
| 580 | // set to a decimal number, or if the value originated with a parse. |
| 581 | // |
| 582 | // The default implementation for formatting a DigitList converts it |
| 583 | // to a double, and formats that, allowing formatting classes that don't |
| 584 | // know about DigitList to continue to operate as they had. |
| 585 | // |
| 586 | // DecimalFormat overrides the DigitList formatting functions. |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 587 | format(*n->getDecimalQuantity(), appendTo, pos, status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 588 | } else { |
| 589 | switch (n->getType()) { |
| 590 | case Formattable::kDouble: |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 591 | format(n->getDouble(), appendTo, pos, status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 592 | break; |
| 593 | case Formattable::kLong: |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 594 | format(n->getLong(), appendTo, pos, status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 595 | break; |
| 596 | case Formattable::kInt64: |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 597 | format(n->getInt64(), appendTo, pos, status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 598 | break; |
| 599 | default: |
| 600 | status = U_INVALID_FORMAT_ERROR; |
| 601 | break; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | return appendTo; |
| 606 | } |
| 607 | |
| 608 | // -------------------------------------x |
| 609 | // Formats the number object and save the format |
| 610 | // result in the toAppendTo string buffer. |
| 611 | |
| 612 | UnicodeString& |
| 613 | NumberFormat::format(const Formattable& obj, |
| 614 | UnicodeString& appendTo, |
| 615 | FieldPositionIterator* posIter, |
| 616 | UErrorCode& status) const |
| 617 | { |
| 618 | if (U_FAILURE(status)) return appendTo; |
| 619 | |
| 620 | ArgExtractor arg(*this, obj, status); |
| 621 | const Formattable *n = arg.number(); |
| 622 | const UChar *iso = arg.iso(); |
| 623 | |
| 624 | if(arg.wasCurrency() && u_strcmp(iso, getCurrency())) { |
| 625 | // trying to format a different currency. |
| 626 | // Right now, we clone. |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 627 | LocalPointer<NumberFormat> cloneFmt(this->clone()); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 628 | cloneFmt->setCurrency(iso, status); |
| 629 | // next line should NOT recurse, because n is numeric whereas obj was a wrapper around currency amount. |
| 630 | return cloneFmt->format(*n, appendTo, posIter, status); |
| 631 | } |
| 632 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 633 | if (n->isNumeric() && n->getDecimalQuantity() != NULL) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 634 | // Decimal Number |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 635 | format(*n->getDecimalQuantity(), appendTo, posIter, status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 636 | } else { |
| 637 | switch (n->getType()) { |
| 638 | case Formattable::kDouble: |
| 639 | format(n->getDouble(), appendTo, posIter, status); |
| 640 | break; |
| 641 | case Formattable::kLong: |
| 642 | format(n->getLong(), appendTo, posIter, status); |
| 643 | break; |
| 644 | case Formattable::kInt64: |
| 645 | format(n->getInt64(), appendTo, posIter, status); |
| 646 | break; |
| 647 | default: |
| 648 | status = U_INVALID_FORMAT_ERROR; |
| 649 | break; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | return appendTo; |
| 654 | } |
| 655 | |
| 656 | // ------------------------------------- |
| 657 | |
| 658 | UnicodeString& |
| 659 | NumberFormat::format(int64_t number, |
| 660 | UnicodeString& appendTo, |
| 661 | FieldPosition& pos) const |
| 662 | { |
| 663 | // default so we don't introduce a new abstract method |
| 664 | return format((int32_t)number, appendTo, pos); |
| 665 | } |
| 666 | |
| 667 | // ------------------------------------- |
| 668 | // Parses the string and save the result object as well |
| 669 | // as the final parsed position. |
| 670 | |
| 671 | void |
| 672 | NumberFormat::parseObject(const UnicodeString& source, |
| 673 | Formattable& result, |
| 674 | ParsePosition& parse_pos) const |
| 675 | { |
| 676 | parse(source, result, parse_pos); |
| 677 | } |
| 678 | |
| 679 | // ------------------------------------- |
| 680 | // Formats a double number and save the result in a string. |
| 681 | |
| 682 | UnicodeString& |
| 683 | NumberFormat::format(double number, UnicodeString& appendTo) const |
| 684 | { |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 685 | FieldPosition pos(FieldPosition::DONT_CARE); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 686 | return format(number, appendTo, pos); |
| 687 | } |
| 688 | |
| 689 | // ------------------------------------- |
| 690 | // Formats a long number and save the result in a string. |
| 691 | |
| 692 | UnicodeString& |
| 693 | NumberFormat::format(int32_t number, UnicodeString& appendTo) const |
| 694 | { |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 695 | FieldPosition pos(FieldPosition::DONT_CARE); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 696 | return format(number, appendTo, pos); |
| 697 | } |
| 698 | |
| 699 | // ------------------------------------- |
| 700 | // Formats a long number and save the result in a string. |
| 701 | |
| 702 | UnicodeString& |
| 703 | NumberFormat::format(int64_t number, UnicodeString& appendTo) const |
| 704 | { |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 705 | FieldPosition pos(FieldPosition::DONT_CARE); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 706 | return format(number, appendTo, pos); |
| 707 | } |
| 708 | |
| 709 | // ------------------------------------- |
| 710 | // Parses the text and save the result object. If the returned |
| 711 | // parse position is 0, that means the parsing failed, the status |
| 712 | // code needs to be set to failure. Ignores the returned parse |
| 713 | // position, otherwise. |
| 714 | |
| 715 | void |
| 716 | NumberFormat::parse(const UnicodeString& text, |
| 717 | Formattable& result, |
| 718 | UErrorCode& status) const |
| 719 | { |
| 720 | if (U_FAILURE(status)) return; |
| 721 | |
| 722 | ParsePosition parsePosition(0); |
| 723 | parse(text, result, parsePosition); |
| 724 | if (parsePosition.getIndex() == 0) { |
| 725 | status = U_INVALID_FORMAT_ERROR; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | CurrencyAmount* NumberFormat::parseCurrency(const UnicodeString& text, |
| 730 | ParsePosition& pos) const { |
| 731 | // Default implementation only -- subclasses should override |
| 732 | Formattable parseResult; |
| 733 | int32_t start = pos.getIndex(); |
| 734 | parse(text, parseResult, pos); |
| 735 | if (pos.getIndex() != start) { |
| 736 | UChar curr[4]; |
| 737 | UErrorCode ec = U_ZERO_ERROR; |
| 738 | getEffectiveCurrency(curr, ec); |
| 739 | if (U_SUCCESS(ec)) { |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 740 | LocalPointer<CurrencyAmount> currAmt(new CurrencyAmount(parseResult, curr, ec), ec); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 741 | if (U_FAILURE(ec)) { |
| 742 | pos.setIndex(start); // indicate failure |
| 743 | } else { |
| 744 | return currAmt.orphan(); |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | return NULL; |
| 749 | } |
| 750 | |
| 751 | // ------------------------------------- |
| 752 | // Sets to only parse integers. |
| 753 | |
| 754 | void |
| 755 | NumberFormat::setParseIntegerOnly(UBool value) |
| 756 | { |
| 757 | fParseIntegerOnly = value; |
| 758 | } |
| 759 | |
| 760 | // ------------------------------------- |
| 761 | // Sets whether lenient parse is enabled. |
| 762 | |
| 763 | void |
| 764 | NumberFormat::setLenient(UBool enable) |
| 765 | { |
| 766 | fLenient = enable; |
| 767 | } |
| 768 | |
| 769 | // ------------------------------------- |
| 770 | // Create a number style NumberFormat instance with the default locale. |
| 771 | |
| 772 | NumberFormat* U_EXPORT2 |
| 773 | NumberFormat::createInstance(UErrorCode& status) |
| 774 | { |
| 775 | return createInstance(Locale::getDefault(), UNUM_DECIMAL, status); |
| 776 | } |
| 777 | |
| 778 | // ------------------------------------- |
| 779 | // Create a number style NumberFormat instance with the inLocale locale. |
| 780 | |
| 781 | NumberFormat* U_EXPORT2 |
| 782 | NumberFormat::createInstance(const Locale& inLocale, UErrorCode& status) |
| 783 | { |
| 784 | return createInstance(inLocale, UNUM_DECIMAL, status); |
| 785 | } |
| 786 | |
| 787 | // ------------------------------------- |
| 788 | // Create a currency style NumberFormat instance with the default locale. |
| 789 | |
| 790 | NumberFormat* U_EXPORT2 |
| 791 | NumberFormat::createCurrencyInstance(UErrorCode& status) |
| 792 | { |
| 793 | return createCurrencyInstance(Locale::getDefault(), status); |
| 794 | } |
| 795 | |
| 796 | // ------------------------------------- |
| 797 | // Create a currency style NumberFormat instance with the inLocale locale. |
| 798 | |
| 799 | NumberFormat* U_EXPORT2 |
| 800 | NumberFormat::createCurrencyInstance(const Locale& inLocale, UErrorCode& status) |
| 801 | { |
| 802 | return createInstance(inLocale, UNUM_CURRENCY, status); |
| 803 | } |
| 804 | |
| 805 | // ------------------------------------- |
| 806 | // Create a percent style NumberFormat instance with the default locale. |
| 807 | |
| 808 | NumberFormat* U_EXPORT2 |
| 809 | NumberFormat::createPercentInstance(UErrorCode& status) |
| 810 | { |
| 811 | return createInstance(Locale::getDefault(), UNUM_PERCENT, status); |
| 812 | } |
| 813 | |
| 814 | // ------------------------------------- |
| 815 | // Create a percent style NumberFormat instance with the inLocale locale. |
| 816 | |
| 817 | NumberFormat* U_EXPORT2 |
| 818 | NumberFormat::createPercentInstance(const Locale& inLocale, UErrorCode& status) |
| 819 | { |
| 820 | return createInstance(inLocale, UNUM_PERCENT, status); |
| 821 | } |
| 822 | |
| 823 | // ------------------------------------- |
| 824 | // Create a scientific style NumberFormat instance with the default locale. |
| 825 | |
| 826 | NumberFormat* U_EXPORT2 |
| 827 | NumberFormat::createScientificInstance(UErrorCode& status) |
| 828 | { |
| 829 | return createInstance(Locale::getDefault(), UNUM_SCIENTIFIC, status); |
| 830 | } |
| 831 | |
| 832 | // ------------------------------------- |
| 833 | // Create a scientific style NumberFormat instance with the inLocale locale. |
| 834 | |
| 835 | NumberFormat* U_EXPORT2 |
| 836 | NumberFormat::createScientificInstance(const Locale& inLocale, UErrorCode& status) |
| 837 | { |
| 838 | return createInstance(inLocale, UNUM_SCIENTIFIC, status); |
| 839 | } |
| 840 | |
| 841 | // ------------------------------------- |
| 842 | |
| 843 | const Locale* U_EXPORT2 |
| 844 | NumberFormat::getAvailableLocales(int32_t& count) |
| 845 | { |
| 846 | return Locale::getAvailableLocales(count); |
| 847 | } |
| 848 | |
| 849 | // ------------------------------------------ |
| 850 | // |
| 851 | // Registration |
| 852 | // |
| 853 | //------------------------------------------- |
| 854 | |
| 855 | #if !UCONFIG_NO_SERVICE |
| 856 | |
| 857 | // ------------------------------------- |
| 858 | |
| 859 | class ICUNumberFormatFactory : public ICUResourceBundleFactory { |
| 860 | public: |
| 861 | virtual ~ICUNumberFormatFactory(); |
| 862 | protected: |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 863 | virtual UObject* handleCreate(const Locale& loc, int32_t kind, const ICUService* /* service */, UErrorCode& status) const override { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 864 | return NumberFormat::makeInstance(loc, (UNumberFormatStyle)kind, status); |
| 865 | } |
| 866 | }; |
| 867 | |
| 868 | ICUNumberFormatFactory::~ICUNumberFormatFactory() {} |
| 869 | |
| 870 | // ------------------------------------- |
| 871 | |
| 872 | class NFFactory : public LocaleKeyFactory { |
| 873 | private: |
| 874 | NumberFormatFactory* _delegate; |
| 875 | Hashtable* _ids; |
| 876 | |
| 877 | public: |
| 878 | NFFactory(NumberFormatFactory* delegate) |
| 879 | : LocaleKeyFactory(delegate->visible() ? VISIBLE : INVISIBLE) |
| 880 | , _delegate(delegate) |
| 881 | , _ids(NULL) |
| 882 | { |
| 883 | } |
| 884 | |
| 885 | virtual ~NFFactory(); |
| 886 | |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 887 | virtual UObject* create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const override |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 888 | { |
| 889 | if (handlesKey(key, status)) { |
| 890 | const LocaleKey& lkey = (const LocaleKey&)key; |
| 891 | Locale loc; |
| 892 | lkey.canonicalLocale(loc); |
| 893 | int32_t kind = lkey.kind(); |
| 894 | |
| 895 | UObject* result = _delegate->createFormat(loc, (UNumberFormatStyle)kind); |
| 896 | if (result == NULL) { |
| 897 | result = service->getKey((ICUServiceKey&)key /* cast away const */, NULL, this, status); |
| 898 | } |
| 899 | return result; |
| 900 | } |
| 901 | return NULL; |
| 902 | } |
| 903 | |
| 904 | protected: |
| 905 | /** |
| 906 | * Return the set of ids that this factory supports (visible or |
| 907 | * otherwise). This can be called often and might need to be |
| 908 | * cached if it is expensive to create. |
| 909 | */ |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 910 | virtual const Hashtable* getSupportedIDs(UErrorCode& status) const override |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 911 | { |
| 912 | if (U_SUCCESS(status)) { |
| 913 | if (!_ids) { |
| 914 | int32_t count = 0; |
| 915 | const UnicodeString * const idlist = _delegate->getSupportedIDs(count, status); |
| 916 | ((NFFactory*)this)->_ids = new Hashtable(status); /* cast away const */ |
| 917 | if (_ids) { |
| 918 | for (int i = 0; i < count; ++i) { |
| 919 | _ids->put(idlist[i], (void*)this, status); |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | return _ids; |
| 924 | } |
| 925 | return NULL; |
| 926 | } |
| 927 | }; |
| 928 | |
| 929 | NFFactory::~NFFactory() |
| 930 | { |
| 931 | delete _delegate; |
| 932 | delete _ids; |
| 933 | } |
| 934 | |
| 935 | class ICUNumberFormatService : public ICULocaleService { |
| 936 | public: |
| 937 | ICUNumberFormatService() |
| 938 | : ICULocaleService(UNICODE_STRING_SIMPLE("Number Format")) |
| 939 | { |
| 940 | UErrorCode status = U_ZERO_ERROR; |
| 941 | registerFactory(new ICUNumberFormatFactory(), status); |
| 942 | } |
| 943 | |
| 944 | virtual ~ICUNumberFormatService(); |
| 945 | |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 946 | virtual UObject* cloneInstance(UObject* instance) const override { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 947 | return ((NumberFormat*)instance)->clone(); |
| 948 | } |
| 949 | |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 950 | virtual UObject* handleDefault(const ICUServiceKey& key, UnicodeString* /* actualID */, UErrorCode& status) const override { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 951 | LocaleKey& lkey = (LocaleKey&)key; |
| 952 | int32_t kind = lkey.kind(); |
| 953 | Locale loc; |
| 954 | lkey.currentLocale(loc); |
| 955 | return NumberFormat::makeInstance(loc, (UNumberFormatStyle)kind, status); |
| 956 | } |
| 957 | |
Frank Tang | 3e05d9d | 2021-11-08 14:04:04 -0800 | [diff] [blame] | 958 | virtual UBool isDefault() const override { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 959 | return countFactories() == 1; |
| 960 | } |
| 961 | }; |
| 962 | |
| 963 | ICUNumberFormatService::~ICUNumberFormatService() {} |
| 964 | |
| 965 | // ------------------------------------- |
| 966 | |
| 967 | static void U_CALLCONV initNumberFormatService() { |
| 968 | U_ASSERT(gService == NULL); |
| 969 | ucln_i18n_registerCleanup(UCLN_I18N_NUMFMT, numfmt_cleanup); |
| 970 | gService = new ICUNumberFormatService(); |
| 971 | } |
| 972 | |
| 973 | static ICULocaleService* |
| 974 | getNumberFormatService(void) |
| 975 | { |
| 976 | umtx_initOnce(gServiceInitOnce, &initNumberFormatService); |
| 977 | return gService; |
| 978 | } |
| 979 | |
| 980 | static UBool haveService() { |
| 981 | return !gServiceInitOnce.isReset() && (getNumberFormatService() != NULL); |
| 982 | } |
| 983 | |
| 984 | // ------------------------------------- |
| 985 | |
| 986 | URegistryKey U_EXPORT2 |
| 987 | NumberFormat::registerFactory(NumberFormatFactory* toAdopt, UErrorCode& status) |
| 988 | { |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 989 | if (U_FAILURE(status)) { |
| 990 | delete toAdopt; |
| 991 | return nullptr; |
| 992 | } |
| 993 | ICULocaleService *service = getNumberFormatService(); |
| 994 | if (service) { |
| 995 | NFFactory *tempnnf = new NFFactory(toAdopt); |
| 996 | if (tempnnf != NULL) { |
| 997 | return service->registerFactory(tempnnf, status); |
| 998 | } |
| 999 | } |
| 1000 | status = U_MEMORY_ALLOCATION_ERROR; |
| 1001 | return NULL; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | // ------------------------------------- |
| 1005 | |
| 1006 | UBool U_EXPORT2 |
| 1007 | NumberFormat::unregister(URegistryKey key, UErrorCode& status) |
| 1008 | { |
| 1009 | if (U_FAILURE(status)) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 1010 | return false; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1011 | } |
| 1012 | if (haveService()) { |
| 1013 | return gService->unregister(key, status); |
| 1014 | } else { |
| 1015 | status = U_ILLEGAL_ARGUMENT_ERROR; |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 1016 | return false; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | // ------------------------------------- |
| 1021 | StringEnumeration* U_EXPORT2 |
| 1022 | NumberFormat::getAvailableLocales(void) |
| 1023 | { |
| 1024 | ICULocaleService *service = getNumberFormatService(); |
| 1025 | if (service) { |
| 1026 | return service->getAvailableLocales(); |
| 1027 | } |
| 1028 | return NULL; // no way to return error condition |
| 1029 | } |
| 1030 | #endif /* UCONFIG_NO_SERVICE */ |
| 1031 | // ------------------------------------- |
| 1032 | |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 1033 | enum { kKeyValueLenMax = 32 }; |
| 1034 | |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 1035 | NumberFormat* |
| 1036 | NumberFormat::internalCreateInstance(const Locale& loc, UNumberFormatStyle kind, UErrorCode& status) { |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 1037 | if (kind == UNUM_CURRENCY) { |
| 1038 | char cfKeyValue[kKeyValueLenMax] = {0}; |
| 1039 | UErrorCode kvStatus = U_ZERO_ERROR; |
| 1040 | int32_t kLen = loc.getKeywordValue("cf", cfKeyValue, kKeyValueLenMax, kvStatus); |
| 1041 | if (U_SUCCESS(kvStatus) && kLen > 0 && uprv_strcmp(cfKeyValue,"account")==0) { |
| 1042 | kind = UNUM_CURRENCY_ACCOUNTING; |
| 1043 | } |
| 1044 | } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1045 | #if !UCONFIG_NO_SERVICE |
| 1046 | if (haveService()) { |
| 1047 | return (NumberFormat*)gService->get(loc, kind, status); |
| 1048 | } |
| 1049 | #endif |
| 1050 | return makeInstance(loc, kind, status); |
| 1051 | } |
| 1052 | |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 1053 | NumberFormat* U_EXPORT2 |
| 1054 | NumberFormat::createInstance(const Locale& loc, UNumberFormatStyle kind, UErrorCode& status) { |
| 1055 | if (kind != UNUM_DECIMAL) { |
| 1056 | return internalCreateInstance(loc, kind, status); |
| 1057 | } |
| 1058 | const SharedNumberFormat *shared = createSharedInstance(loc, kind, status); |
| 1059 | if (U_FAILURE(status)) { |
| 1060 | return NULL; |
| 1061 | } |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 1062 | NumberFormat *result = (*shared)->clone(); |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 1063 | shared->removeRef(); |
| 1064 | if (result == NULL) { |
| 1065 | status = U_MEMORY_ALLOCATION_ERROR; |
| 1066 | } |
| 1067 | return result; |
| 1068 | } |
| 1069 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1070 | |
| 1071 | // ------------------------------------- |
| 1072 | // Checks if the thousand/10 thousand grouping is used in the |
| 1073 | // NumberFormat instance. |
| 1074 | |
| 1075 | UBool |
| 1076 | NumberFormat::isGroupingUsed() const |
| 1077 | { |
| 1078 | return fGroupingUsed; |
| 1079 | } |
| 1080 | |
| 1081 | // ------------------------------------- |
| 1082 | // Sets to use the thousand/10 thousand grouping in the |
| 1083 | // NumberFormat instance. |
| 1084 | |
| 1085 | void |
| 1086 | NumberFormat::setGroupingUsed(UBool newValue) |
| 1087 | { |
| 1088 | fGroupingUsed = newValue; |
| 1089 | } |
| 1090 | |
| 1091 | // ------------------------------------- |
| 1092 | // Gets the maximum number of digits for the integral part for |
| 1093 | // this NumberFormat instance. |
| 1094 | |
| 1095 | int32_t NumberFormat::getMaximumIntegerDigits() const |
| 1096 | { |
| 1097 | return fMaxIntegerDigits; |
| 1098 | } |
| 1099 | |
| 1100 | // ------------------------------------- |
| 1101 | // Sets the maximum number of digits for the integral part for |
| 1102 | // this NumberFormat instance. |
| 1103 | |
| 1104 | void |
| 1105 | NumberFormat::setMaximumIntegerDigits(int32_t newValue) |
| 1106 | { |
| 1107 | fMaxIntegerDigits = uprv_max(0, uprv_min(newValue, gDefaultMaxIntegerDigits)); |
| 1108 | if(fMinIntegerDigits > fMaxIntegerDigits) |
| 1109 | fMinIntegerDigits = fMaxIntegerDigits; |
| 1110 | } |
| 1111 | |
| 1112 | // ------------------------------------- |
| 1113 | // Gets the minimum number of digits for the integral part for |
| 1114 | // this NumberFormat instance. |
| 1115 | |
| 1116 | int32_t |
| 1117 | NumberFormat::getMinimumIntegerDigits() const |
| 1118 | { |
| 1119 | return fMinIntegerDigits; |
| 1120 | } |
| 1121 | |
| 1122 | // ------------------------------------- |
| 1123 | // Sets the minimum number of digits for the integral part for |
| 1124 | // this NumberFormat instance. |
| 1125 | |
| 1126 | void |
| 1127 | NumberFormat::setMinimumIntegerDigits(int32_t newValue) |
| 1128 | { |
| 1129 | fMinIntegerDigits = uprv_max(0, uprv_min(newValue, gDefaultMinIntegerDigits)); |
| 1130 | if(fMinIntegerDigits > fMaxIntegerDigits) |
| 1131 | fMaxIntegerDigits = fMinIntegerDigits; |
| 1132 | } |
| 1133 | |
| 1134 | // ------------------------------------- |
| 1135 | // Gets the maximum number of digits for the fractional part for |
| 1136 | // this NumberFormat instance. |
| 1137 | |
| 1138 | int32_t |
| 1139 | NumberFormat::getMaximumFractionDigits() const |
| 1140 | { |
| 1141 | return fMaxFractionDigits; |
| 1142 | } |
| 1143 | |
| 1144 | // ------------------------------------- |
| 1145 | // Sets the maximum number of digits for the fractional part for |
| 1146 | // this NumberFormat instance. |
| 1147 | |
| 1148 | void |
| 1149 | NumberFormat::setMaximumFractionDigits(int32_t newValue) |
| 1150 | { |
| 1151 | fMaxFractionDigits = uprv_max(0, uprv_min(newValue, gDefaultMaxIntegerDigits)); |
| 1152 | if(fMaxFractionDigits < fMinFractionDigits) |
| 1153 | fMinFractionDigits = fMaxFractionDigits; |
| 1154 | } |
| 1155 | |
| 1156 | // ------------------------------------- |
| 1157 | // Gets the minimum number of digits for the fractional part for |
| 1158 | // this NumberFormat instance. |
| 1159 | |
| 1160 | int32_t |
| 1161 | NumberFormat::getMinimumFractionDigits() const |
| 1162 | { |
| 1163 | return fMinFractionDigits; |
| 1164 | } |
| 1165 | |
| 1166 | // ------------------------------------- |
| 1167 | // Sets the minimum number of digits for the fractional part for |
| 1168 | // this NumberFormat instance. |
| 1169 | |
| 1170 | void |
| 1171 | NumberFormat::setMinimumFractionDigits(int32_t newValue) |
| 1172 | { |
| 1173 | fMinFractionDigits = uprv_max(0, uprv_min(newValue, gDefaultMinIntegerDigits)); |
| 1174 | if (fMaxFractionDigits < fMinFractionDigits) |
| 1175 | fMaxFractionDigits = fMinFractionDigits; |
| 1176 | } |
| 1177 | |
| 1178 | // ------------------------------------- |
| 1179 | |
| 1180 | void NumberFormat::setCurrency(const UChar* theCurrency, UErrorCode& ec) { |
| 1181 | if (U_FAILURE(ec)) { |
| 1182 | return; |
| 1183 | } |
| 1184 | if (theCurrency) { |
| 1185 | u_strncpy(fCurrency, theCurrency, 3); |
| 1186 | fCurrency[3] = 0; |
| 1187 | } else { |
| 1188 | fCurrency[0] = 0; |
| 1189 | } |
| 1190 | } |
| 1191 | |
Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 1192 | const char16_t* NumberFormat::getCurrency() const { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1193 | return fCurrency; |
| 1194 | } |
| 1195 | |
| 1196 | void NumberFormat::getEffectiveCurrency(UChar* result, UErrorCode& ec) const { |
| 1197 | const UChar* c = getCurrency(); |
| 1198 | if (*c != 0) { |
| 1199 | u_strncpy(result, c, 3); |
| 1200 | result[3] = 0; |
| 1201 | } else { |
| 1202 | const char* loc = getLocaleID(ULOC_VALID_LOCALE, ec); |
| 1203 | if (loc == NULL) { |
| 1204 | loc = uloc_getDefault(); |
| 1205 | } |
| 1206 | ucurr_forLocale(loc, result, 4, &ec); |
| 1207 | } |
| 1208 | } |
| 1209 | |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 1210 | //---------------------------------------------------------------------- |
| 1211 | |
| 1212 | |
| 1213 | void NumberFormat::setContext(UDisplayContext value, UErrorCode& status) |
| 1214 | { |
| 1215 | if (U_FAILURE(status)) |
| 1216 | return; |
| 1217 | if ( (UDisplayContextType)((uint32_t)value >> 8) == UDISPCTX_TYPE_CAPITALIZATION ) { |
| 1218 | fCapitalizationContext = value; |
| 1219 | } else { |
| 1220 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | |
| 1225 | UDisplayContext NumberFormat::getContext(UDisplayContextType type, UErrorCode& status) const |
| 1226 | { |
| 1227 | if (U_FAILURE(status)) |
| 1228 | return (UDisplayContext)0; |
| 1229 | if (type != UDISPCTX_TYPE_CAPITALIZATION) { |
| 1230 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 1231 | return (UDisplayContext)0; |
| 1232 | } |
| 1233 | return fCapitalizationContext; |
| 1234 | } |
| 1235 | |
| 1236 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1237 | // ------------------------------------- |
| 1238 | // Creates the NumberFormat instance of the specified style (number, currency, |
| 1239 | // or percent) for the desired locale. |
| 1240 | |
| 1241 | static void U_CALLCONV nscacheInit() { |
| 1242 | U_ASSERT(NumberingSystem_cache == NULL); |
| 1243 | ucln_i18n_registerCleanup(UCLN_I18N_NUMFMT, numfmt_cleanup); |
| 1244 | UErrorCode status = U_ZERO_ERROR; |
| 1245 | NumberingSystem_cache = uhash_open(uhash_hashLong, |
| 1246 | uhash_compareLong, |
| 1247 | NULL, |
| 1248 | &status); |
| 1249 | if (U_FAILURE(status)) { |
| 1250 | // Number Format code will run with no cache if creation fails. |
| 1251 | NumberingSystem_cache = NULL; |
| 1252 | return; |
| 1253 | } |
| 1254 | uhash_setValueDeleter(NumberingSystem_cache, deleteNumberingSystem); |
| 1255 | } |
| 1256 | |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 1257 | template<> U_I18N_API |
| 1258 | const SharedNumberFormat *LocaleCacheKey<SharedNumberFormat>::createObject( |
| 1259 | const void * /*unused*/, UErrorCode &status) const { |
| 1260 | const char *localeId = fLoc.getName(); |
| 1261 | NumberFormat *nf = NumberFormat::internalCreateInstance( |
| 1262 | localeId, UNUM_DECIMAL, status); |
| 1263 | if (U_FAILURE(status)) { |
| 1264 | return NULL; |
| 1265 | } |
| 1266 | SharedNumberFormat *result = new SharedNumberFormat(nf); |
| 1267 | if (result == NULL) { |
| 1268 | status = U_MEMORY_ALLOCATION_ERROR; |
| 1269 | delete nf; |
| 1270 | return NULL; |
| 1271 | } |
| 1272 | result->addRef(); |
| 1273 | return result; |
| 1274 | } |
| 1275 | |
| 1276 | const SharedNumberFormat* U_EXPORT2 |
| 1277 | NumberFormat::createSharedInstance(const Locale& loc, UNumberFormatStyle kind, UErrorCode& status) { |
| 1278 | if (U_FAILURE(status)) { |
| 1279 | return NULL; |
| 1280 | } |
| 1281 | if (kind != UNUM_DECIMAL) { |
| 1282 | status = U_UNSUPPORTED_ERROR; |
| 1283 | return NULL; |
| 1284 | } |
| 1285 | const SharedNumberFormat *result = NULL; |
| 1286 | UnifiedCache::getByLocale(loc, result, status); |
| 1287 | return result; |
| 1288 | } |
| 1289 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1290 | UBool |
| 1291 | NumberFormat::isStyleSupported(UNumberFormatStyle style) { |
| 1292 | return gLastResortNumberPatterns[style] != NULL; |
| 1293 | } |
| 1294 | |
| 1295 | NumberFormat* |
| 1296 | NumberFormat::makeInstance(const Locale& desiredLocale, |
| 1297 | UNumberFormatStyle style, |
| 1298 | UErrorCode& status) { |
| 1299 | return makeInstance(desiredLocale, style, false, status); |
| 1300 | } |
| 1301 | |
| 1302 | NumberFormat* |
| 1303 | NumberFormat::makeInstance(const Locale& desiredLocale, |
| 1304 | UNumberFormatStyle style, |
| 1305 | UBool mustBeDecimalFormat, |
| 1306 | UErrorCode& status) { |
| 1307 | if (U_FAILURE(status)) return NULL; |
| 1308 | |
| 1309 | if (style < 0 || style >= UNUM_FORMAT_STYLE_COUNT) { |
| 1310 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 1311 | return NULL; |
| 1312 | } |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 1313 | |
| 1314 | // For the purposes of general number formatting, UNUM_NUMBERING_SYSTEM should behave the same |
| 1315 | // was as UNUM_DECIMAL. In both cases, you get either a DecimalFormat or a RuleBasedNumberFormat |
| 1316 | // depending on the locale's numbering system (either the default one for the locale or a specific |
| 1317 | // one specified by using the "@numbers=" or "-u-nu-" parameter in the locale ID. |
| 1318 | if (style == UNUM_NUMBERING_SYSTEM) { |
| 1319 | style = UNUM_DECIMAL; |
| 1320 | } |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1321 | |
| 1322 | // Some styles are not supported. This is a result of merging |
| 1323 | // the @draft ICU 4.2 NumberFormat::EStyles into the long-existing UNumberFormatStyle. |
| 1324 | // Ticket #8503 is for reviewing/fixing/merging the two relevant implementations: |
| 1325 | // this one and unum_open(). |
| 1326 | // The UNUM_PATTERN_ styles are not supported here |
| 1327 | // because this method does not take a pattern string. |
| 1328 | if (!isStyleSupported(style)) { |
| 1329 | status = U_UNSUPPORTED_ERROR; |
| 1330 | return NULL; |
| 1331 | } |
| 1332 | |
| 1333 | #if U_PLATFORM_USES_ONLY_WIN32_API |
| 1334 | if (!mustBeDecimalFormat) { |
| 1335 | char buffer[8]; |
| 1336 | int32_t count = desiredLocale.getKeywordValue("compat", buffer, sizeof(buffer), status); |
| 1337 | |
| 1338 | // if the locale has "@compat=host", create a host-specific NumberFormat |
| 1339 | if (U_SUCCESS(status) && count > 0 && uprv_strcmp(buffer, "host") == 0) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 1340 | UBool curr = true; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1341 | |
| 1342 | switch (style) { |
| 1343 | case UNUM_DECIMAL: |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 1344 | curr = false; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1345 | // fall-through |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 1346 | U_FALLTHROUGH; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1347 | |
| 1348 | case UNUM_CURRENCY: |
| 1349 | case UNUM_CURRENCY_ISO: // do not support plural formatting here |
| 1350 | case UNUM_CURRENCY_PLURAL: |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 1351 | case UNUM_CURRENCY_ACCOUNTING: |
| 1352 | case UNUM_CASH_CURRENCY: |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 1353 | case UNUM_CURRENCY_STANDARD: |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 1354 | { |
| 1355 | LocalPointer<Win32NumberFormat> f(new Win32NumberFormat(desiredLocale, curr, status), status); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1356 | if (U_SUCCESS(status)) { |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 1357 | return f.orphan(); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1358 | } |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 1359 | } |
| 1360 | break; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1361 | default: |
| 1362 | break; |
| 1363 | } |
| 1364 | } |
| 1365 | } |
| 1366 | #endif |
| 1367 | // Use numbering system cache hashtable |
| 1368 | umtx_initOnce(gNSCacheInitOnce, &nscacheInit); |
| 1369 | |
| 1370 | // Get cached numbering system |
| 1371 | LocalPointer<NumberingSystem> ownedNs; |
| 1372 | NumberingSystem *ns = NULL; |
| 1373 | if (NumberingSystem_cache != NULL) { |
| 1374 | // TODO: Bad hash key usage, see ticket #8504. |
| 1375 | int32_t hashKey = desiredLocale.hashCode(); |
| 1376 | |
Frank Tang | b869661 | 2019-10-25 14:58:21 -0700 | [diff] [blame] | 1377 | static UMutex nscacheMutex; |
| 1378 | Mutex lock(&nscacheMutex); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1379 | ns = (NumberingSystem *)uhash_iget(NumberingSystem_cache, hashKey); |
| 1380 | if (ns == NULL) { |
| 1381 | ns = NumberingSystem::createInstance(desiredLocale,status); |
| 1382 | uhash_iput(NumberingSystem_cache, hashKey, (void*)ns, &status); |
| 1383 | } |
| 1384 | } else { |
| 1385 | ownedNs.adoptInstead(NumberingSystem::createInstance(desiredLocale,status)); |
| 1386 | ns = ownedNs.getAlias(); |
| 1387 | } |
| 1388 | |
| 1389 | // check results of getting a numbering system |
| 1390 | if (U_FAILURE(status)) { |
| 1391 | return NULL; |
| 1392 | } |
| 1393 | |
| 1394 | if (mustBeDecimalFormat && ns->isAlgorithmic()) { |
| 1395 | status = U_UNSUPPORTED_ERROR; |
| 1396 | return NULL; |
| 1397 | } |
| 1398 | |
| 1399 | LocalPointer<DecimalFormatSymbols> symbolsToAdopt; |
| 1400 | UnicodeString pattern; |
| 1401 | LocalUResourceBundlePointer ownedResource(ures_open(NULL, desiredLocale.getName(), &status)); |
| 1402 | if (U_FAILURE(status)) { |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 1403 | return NULL; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1404 | } |
| 1405 | else { |
| 1406 | // Loads the decimal symbols of the desired locale. |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 1407 | symbolsToAdopt.adoptInsteadAndCheckErrorCode(new DecimalFormatSymbols(desiredLocale, status), status); |
| 1408 | if (U_FAILURE(status)) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1409 | return NULL; |
| 1410 | } |
| 1411 | |
Jungshik Shin | a9a2bd3 | 2018-07-07 03:36:01 -0700 | [diff] [blame] | 1412 | // Load the pattern from data using the common library function |
| 1413 | const char16_t* patternPtr = number::impl::utils::getPatternForStyle( |
| 1414 | desiredLocale, |
| 1415 | ns->getName(), |
| 1416 | gFormatCldrStyles[style], |
| 1417 | status); |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 1418 | pattern = UnicodeString(true, patternPtr, -1); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1419 | } |
| 1420 | if (U_FAILURE(status)) { |
| 1421 | return NULL; |
| 1422 | } |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 1423 | if(style==UNUM_CURRENCY || style == UNUM_CURRENCY_ISO || style == UNUM_CURRENCY_ACCOUNTING |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 1424 | || style == UNUM_CASH_CURRENCY || style == UNUM_CURRENCY_STANDARD){ |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1425 | const UChar* currPattern = symbolsToAdopt->getCurrencyPattern(); |
| 1426 | if(currPattern!=NULL){ |
| 1427 | pattern.setTo(currPattern, u_strlen(currPattern)); |
| 1428 | } |
| 1429 | } |
| 1430 | |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 1431 | LocalPointer<NumberFormat> f; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1432 | if (ns->isAlgorithmic()) { |
| 1433 | UnicodeString nsDesc; |
| 1434 | UnicodeString nsRuleSetGroup; |
| 1435 | UnicodeString nsRuleSetName; |
| 1436 | Locale nsLoc; |
| 1437 | URBNFRuleSetTag desiredRulesType = URBNF_NUMBERING_SYSTEM; |
| 1438 | |
| 1439 | nsDesc.setTo(ns->getDescription()); |
| 1440 | int32_t firstSlash = nsDesc.indexOf(gSlash); |
| 1441 | int32_t lastSlash = nsDesc.lastIndexOf(gSlash); |
| 1442 | if ( lastSlash > firstSlash ) { |
| 1443 | CharString nsLocID; |
| 1444 | |
| 1445 | nsLocID.appendInvariantChars(nsDesc.tempSubString(0, firstSlash), status); |
| 1446 | nsRuleSetGroup.setTo(nsDesc,firstSlash+1,lastSlash-firstSlash-1); |
| 1447 | nsRuleSetName.setTo(nsDesc,lastSlash+1); |
| 1448 | |
| 1449 | nsLoc = Locale::createFromName(nsLocID.data()); |
| 1450 | |
| 1451 | UnicodeString SpelloutRules = UNICODE_STRING_SIMPLE("SpelloutRules"); |
| 1452 | if ( nsRuleSetGroup.compare(SpelloutRules) == 0 ) { |
| 1453 | desiredRulesType = URBNF_SPELLOUT; |
| 1454 | } |
| 1455 | } else { |
| 1456 | nsLoc = desiredLocale; |
| 1457 | nsRuleSetName.setTo(nsDesc); |
| 1458 | } |
| 1459 | |
| 1460 | RuleBasedNumberFormat *r = new RuleBasedNumberFormat(desiredRulesType,nsLoc,status); |
| 1461 | if (r == NULL) { |
| 1462 | status = U_MEMORY_ALLOCATION_ERROR; |
| 1463 | return NULL; |
| 1464 | } |
| 1465 | r->setDefaultRuleSet(nsRuleSetName,status); |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 1466 | f.adoptInstead(r); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1467 | } else { |
| 1468 | // replace single currency sign in the pattern with double currency sign |
| 1469 | // if the style is UNUM_CURRENCY_ISO |
| 1470 | if (style == UNUM_CURRENCY_ISO) { |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 1471 | pattern.findAndReplace(UnicodeString(true, gSingleCurrencySign, 1), |
| 1472 | UnicodeString(true, gDoubleCurrencySign, 2)); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 1475 | // "new DecimalFormat()" does not adopt the symbols argument if its memory allocation fails. |
| 1476 | // So we can't use adoptInsteadAndCheckErrorCode as we need to know if the 'new' failed. |
| 1477 | DecimalFormatSymbols *syms = symbolsToAdopt.getAlias(); |
| 1478 | LocalPointer<DecimalFormat> df(new DecimalFormat(pattern, syms, style, status)); |
| 1479 | |
| 1480 | if (df.isValid()) { |
| 1481 | // if the DecimalFormat object was successfully new'ed, then it will own symbolsToAdopt, even if the status is a failure. |
| 1482 | symbolsToAdopt.orphan(); |
| 1483 | } |
| 1484 | else { |
| 1485 | status = U_MEMORY_ALLOCATION_ERROR; |
| 1486 | } |
| 1487 | |
| 1488 | if (U_FAILURE(status)) { |
| 1489 | return nullptr; |
| 1490 | } |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 1491 | |
| 1492 | // if it is cash currency style, setCurrencyUsage with usage |
| 1493 | if (style == UNUM_CASH_CURRENCY){ |
| 1494 | df->setCurrencyUsage(UCURR_USAGE_CASH, &status); |
| 1495 | } |
| 1496 | |
| 1497 | if (U_FAILURE(status)) { |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 1498 | return nullptr; |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 1499 | } |
| 1500 | |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 1501 | f.adoptInstead(df.orphan()); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | f->setLocaleIDs(ures_getLocaleByType(ownedResource.getAlias(), ULOC_VALID_LOCALE, &status), |
| 1505 | ures_getLocaleByType(ownedResource.getAlias(), ULOC_ACTUAL_LOCALE, &status)); |
| 1506 | if (U_FAILURE(status)) { |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1507 | return NULL; |
| 1508 | } |
Jungshik Shin | 42d5027 | 2018-10-24 01:22:09 -0700 | [diff] [blame] | 1509 | return f.orphan(); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
Jungshik Shin | b318966 | 2017-11-07 11:18:34 -0800 | [diff] [blame] | 1512 | /** |
| 1513 | * Get the rounding mode. |
| 1514 | * @return A rounding mode |
| 1515 | */ |
| 1516 | NumberFormat::ERoundingMode NumberFormat::getRoundingMode() const { |
| 1517 | // Default value. ICU4J throws an exception and we can't change this API. |
| 1518 | return NumberFormat::ERoundingMode::kRoundUnnecessary; |
| 1519 | } |
| 1520 | |
| 1521 | /** |
| 1522 | * Set the rounding mode. This has no effect unless the rounding |
| 1523 | * increment is greater than zero. |
| 1524 | * @param roundingMode A rounding mode |
| 1525 | */ |
| 1526 | void NumberFormat::setRoundingMode(NumberFormat::ERoundingMode /*roundingMode*/) { |
| 1527 | // No-op ICU4J throws an exception, and we can't change this API. |
| 1528 | } |
| 1529 | |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 1530 | U_NAMESPACE_END |
| 1531 | |
| 1532 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
| 1533 | |
| 1534 | //eof |