| 1 | // © 2016 and later: Unicode, Inc. and others. |
| 2 | // License & terms of use: http://www.unicode.org/copyright.html |
| 3 | /* |
| 4 | ******************************************************************************* |
| 5 | * Copyright (C) 1997-2016, International Business Machines Corporation and |
| 6 | * others. All Rights Reserved. |
| 7 | ******************************************************************************* |
| 8 | * |
| 9 | * File DCFMTSYM.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. |
| 16 | * 03/27/97 helena Updated to pass the simple test after code review. |
| 17 | * 08/26/97 aliu Added currency/intl currency symbol support. |
| 18 | * 07/20/98 stephen Slightly modified initialization of monetarySeparator |
| 19 | ******************************************************************************** |
| 20 | */ |
| 21 | |
| 22 | #include "unicode/utypes.h" |
| 23 | |
| 24 | #if !UCONFIG_NO_FORMATTING |
| 25 | |
| 26 | #include "unicode/dcfmtsym.h" |
| 27 | #include "unicode/ures.h" |
| 28 | #include "unicode/decimfmt.h" |
| 29 | #include "unicode/ucurr.h" |
| 30 | #include "unicode/choicfmt.h" |
| 31 | #include "unicode/unistr.h" |
| 32 | #include "unicode/numsys.h" |
| 33 | #include "unicode/unum.h" |
| 34 | #include "unicode/utf16.h" |
| 35 | #include "ucurrimp.h" |
| 36 | #include "cstring.h" |
| 37 | #include "locbased.h" |
| 38 | #include "uresimp.h" |
| 39 | #include "ureslocs.h" |
| 40 | #include "charstr.h" |
| 41 | #include "uassert.h" |
| 42 | |
| 43 | // ***************************************************************************** |
| 44 | // class DecimalFormatSymbols |
| 45 | // ***************************************************************************** |
| 46 | |
| 47 | U_NAMESPACE_BEGIN |
| 48 | |
| 49 | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DecimalFormatSymbols) |
| 50 | |
| 51 | static const char gNumberElements[] = "NumberElements"; |
| 52 | static const char gCurrencySpacingTag[] = "currencySpacing"; |
| 53 | static const char gBeforeCurrencyTag[] = "beforeCurrency"; |
| 54 | static const char gAfterCurrencyTag[] = "afterCurrency"; |
| 55 | static const char gCurrencyMatchTag[] = "currencyMatch"; |
| 56 | static const char gCurrencySudMatchTag[] = "surroundingMatch"; |
| 57 | static const char gCurrencyInsertBtnTag[] = "insertBetween"; |
| 58 | static const char gLatn[] = "latn"; |
| 59 | static const char gSymbols[] = "symbols"; |
| 60 | static const char gNumberElementsLatnSymbols[] = "NumberElements/latn/symbols"; |
| 61 | |
| 62 | static const char16_t INTL_CURRENCY_SYMBOL_STR[] = {0xa4, 0xa4, 0}; |
| 63 | |
| 64 | // List of field names to be loaded from the data files. |
| 65 | // These are parallel with the enum ENumberFormatSymbol in unicode/dcfmtsym.h. |
| 66 | static const char *gNumberElementKeys[DecimalFormatSymbols::kFormatSymbolCount] = { |
| 67 | "decimal", |
| 68 | "group", |
| 69 | nullptr, /* #11897: the <list> symbol is NOT the pattern separator symbol */ |
| 70 | "percentSign", |
| 71 | nullptr, /* Native zero digit is deprecated from CLDR - get it from the numbering system */ |
| 72 | nullptr, /* Pattern digit character is deprecated from CLDR - use # by default always */ |
| 73 | "minusSign", |
| 74 | "plusSign", |
| 75 | nullptr, /* currency symbol - Wait until we know the currency before loading from CLDR */ |
| 76 | nullptr, /* intl currency symbol - Wait until we know the currency before loading from CLDR */ |
| 77 | "currencyDecimal", |
| 78 | "exponential", |
| 79 | "perMille", |
| 80 | nullptr, /* Escape padding character - not in CLDR */ |
| 81 | "infinity", |
| 82 | "nan", |
| 83 | nullptr, /* Significant digit symbol - not in CLDR */ |
| 84 | "currencyGroup", |
| 85 | nullptr, /* one digit - get it from the numbering system */ |
| 86 | nullptr, /* two digit - get it from the numbering system */ |
| 87 | nullptr, /* three digit - get it from the numbering system */ |
| 88 | nullptr, /* four digit - get it from the numbering system */ |
| 89 | nullptr, /* five digit - get it from the numbering system */ |
| 90 | nullptr, /* six digit - get it from the numbering system */ |
| 91 | nullptr, /* seven digit - get it from the numbering system */ |
| 92 | nullptr, /* eight digit - get it from the numbering system */ |
| 93 | nullptr, /* nine digit - get it from the numbering system */ |
| 94 | "superscriptingExponent", /* Multiplication (x) symbol for exponents */ |
| 95 | "approximatelySign" /* Approximately sign symbol */ |
| 96 | }; |
| 97 | |
| 98 | // ------------------------------------- |
| 99 | // Initializes this with the decimal format symbols in the default locale. |
| 100 | |
| 101 | DecimalFormatSymbols::DecimalFormatSymbols(UErrorCode& status) |
| 102 | : UObject(), locale() { |
| 103 | initialize(locale, status, true); |
| 104 | } |
| 105 | |
| 106 | // ------------------------------------- |
| 107 | // Initializes this with the decimal format symbols in the desired locale. |
| 108 | |
| 109 | DecimalFormatSymbols::DecimalFormatSymbols(const Locale& loc, UErrorCode& status) |
| 110 | : UObject(), locale(loc) { |
| 111 | initialize(locale, status); |
| 112 | } |
| 113 | |
| 114 | DecimalFormatSymbols::DecimalFormatSymbols(const Locale& loc, const NumberingSystem& ns, UErrorCode& status) |
| 115 | : UObject(), locale(loc) { |
| 116 | initialize(locale, status, false, &ns); |
| 117 | } |
| 118 | |
| 119 | DecimalFormatSymbols::DecimalFormatSymbols() |
| 120 | : UObject(), locale(Locale::getRoot()) { |
| 121 | initialize(); |
| 122 | } |
| 123 | |
| 124 | DecimalFormatSymbols* |
| 125 | DecimalFormatSymbols::createWithLastResortData(UErrorCode& status) { |
| 126 | if (U_FAILURE(status)) { return nullptr; } |
| 127 | DecimalFormatSymbols* sym = new DecimalFormatSymbols(); |
| 128 | if (sym == nullptr) { |
| 129 | status = U_MEMORY_ALLOCATION_ERROR; |
| 130 | } |
| 131 | return sym; |
| 132 | } |
| 133 | |
| 134 | // ------------------------------------- |
| 135 | |
| 136 | DecimalFormatSymbols::~DecimalFormatSymbols() |
| 137 | { |
| 138 | delete actualLocale; |
| 139 | delete validLocale; |
| 140 | } |
| 141 | |
| 142 | // ------------------------------------- |
| 143 | // copy constructor |
| 144 | |
| 145 | DecimalFormatSymbols::DecimalFormatSymbols(const DecimalFormatSymbols &source) |
| 146 | : UObject(source) |
| 147 | { |
| 148 | *this = source; |
| 149 | } |
| 150 | |
| 151 | // ------------------------------------- |
| 152 | // assignment operator |
| 153 | |
| 154 | DecimalFormatSymbols& |
| 155 | DecimalFormatSymbols::operator=(const DecimalFormatSymbols& rhs) |
| 156 | { |
| 157 | if (this != &rhs) { |
| 158 | for (int32_t i = 0; i < static_cast<int32_t>(kFormatSymbolCount); ++i) { |
| 159 | // fastCopyFrom is safe, see docs on fSymbols |
| 160 | fSymbols[static_cast<ENumberFormatSymbol>(i)].fastCopyFrom(rhs.fSymbols[static_cast<ENumberFormatSymbol>(i)]); |
| 161 | } |
| 162 | for (int32_t i = 0; i < static_cast<int32_t>(UNUM_CURRENCY_SPACING_COUNT); ++i) { |
| 163 | currencySpcBeforeSym[i].fastCopyFrom(rhs.currencySpcBeforeSym[i]); |
| 164 | currencySpcAfterSym[i].fastCopyFrom(rhs.currencySpcAfterSym[i]); |
| 165 | } |
| 166 | locale = rhs.locale; |
| 167 | |
| 168 | UErrorCode status = U_ZERO_ERROR; |
| 169 | U_LOCALE_BASED(locBased, *this); |
| 170 | locBased.setLocaleIDs(rhs.validLocale, rhs.actualLocale, status); |
| 171 | U_ASSERT(U_SUCCESS(status)); |
| 172 | |
| 173 | fIsCustomCurrencySymbol = rhs.fIsCustomCurrencySymbol; |
| 174 | fIsCustomIntlCurrencySymbol = rhs.fIsCustomIntlCurrencySymbol; |
| 175 | fCodePointZero = rhs.fCodePointZero; |
| 176 | currPattern = rhs.currPattern; |
| 177 | uprv_strcpy(nsName, rhs.nsName); |
| 178 | } |
| 179 | return *this; |
| 180 | } |
| 181 | |
| 182 | // ------------------------------------- |
| 183 | |
| 184 | bool |
| 185 | DecimalFormatSymbols::operator==(const DecimalFormatSymbols& that) const |
| 186 | { |
| 187 | if (this == &that) { |
| 188 | return true; |
| 189 | } |
| 190 | if (fIsCustomCurrencySymbol != that.fIsCustomCurrencySymbol) { |
| 191 | return false; |
| 192 | } |
| 193 | if (fIsCustomIntlCurrencySymbol != that.fIsCustomIntlCurrencySymbol) { |
| 194 | return false; |
| 195 | } |
| 196 | for (int32_t i = 0; i < static_cast<int32_t>(kFormatSymbolCount); ++i) { |
| 197 | if (fSymbols[static_cast<ENumberFormatSymbol>(i)] != that.fSymbols[static_cast<ENumberFormatSymbol>(i)]) { |
| 198 | return false; |
| 199 | } |
| 200 | } |
| 201 | for (int32_t i = 0; i < static_cast<int32_t>(UNUM_CURRENCY_SPACING_COUNT); ++i) { |
| 202 | if(currencySpcBeforeSym[i] != that.currencySpcBeforeSym[i]) { |
| 203 | return false; |
| 204 | } |
| 205 | if(currencySpcAfterSym[i] != that.currencySpcAfterSym[i]) { |
| 206 | return false; |
| 207 | } |
| 208 | } |
| 209 | // No need to check fCodePointZero since it is based on fSymbols |
| 210 | return locale == that.locale && |
| 211 | LocaleBased::equalIDs(actualLocale, that.actualLocale) && |
| 212 | LocaleBased::equalIDs(validLocale, that.validLocale); |
| 213 | } |
| 214 | |
| 215 | // ------------------------------------- |
| 216 | |
| 217 | namespace { |
| 218 | |
| 219 | /** |
| 220 | * Sink for enumerating all of the decimal format symbols (more specifically, anything |
| 221 | * under the "NumberElements.symbols" tree). |
| 222 | * |
| 223 | * More specific bundles (en_GB) are enumerated before their parents (en_001, en, root): |
| 224 | * Only store a value if it is still missing, that is, it has not been overridden. |
| 225 | */ |
| 226 | struct DecFmtSymDataSink : public ResourceSink { |
| 227 | |
| 228 | // Destination for data, modified via setters. |
| 229 | DecimalFormatSymbols& dfs; |
| 230 | // Boolean array of whether or not we have seen a particular symbol yet. |
| 231 | // Can't simply check fSymbols because it is pre-populated with defaults. |
| 232 | UBool seenSymbol[DecimalFormatSymbols::kFormatSymbolCount]; |
| 233 | |
| 234 | // Constructor/Destructor |
| 235 | DecFmtSymDataSink(DecimalFormatSymbols& _dfs) : dfs(_dfs) { |
| 236 | uprv_memset(seenSymbol, false, sizeof(seenSymbol)); |
| 237 | } |
| 238 | virtual ~DecFmtSymDataSink(); |
| 239 | |
| 240 | virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/, |
| 241 | UErrorCode &errorCode) override { |
| 242 | ResourceTable symbolsTable = value.getTable(errorCode); |
| 243 | if (U_FAILURE(errorCode)) { return; } |
| 244 | for (int32_t j = 0; symbolsTable.getKeyAndValue(j, key, value); ++j) { |
| 245 | for (int32_t i=0; i<DecimalFormatSymbols::kFormatSymbolCount; i++) { |
| 246 | if (gNumberElementKeys[i] != nullptr && uprv_strcmp(key, gNumberElementKeys[i]) == 0) { |
| 247 | if (!seenSymbol[i]) { |
| 248 | seenSymbol[i] = true; |
| 249 | dfs.setSymbol( |
| 250 | static_cast<DecimalFormatSymbols::ENumberFormatSymbol>(i), |
| 251 | value.getUnicodeString(errorCode)); |
| 252 | if (U_FAILURE(errorCode)) { return; } |
| 253 | } |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // Returns true if all the symbols have been seen. |
| 261 | UBool seenAll() { |
| 262 | for (int32_t i=0; i<DecimalFormatSymbols::kFormatSymbolCount; i++) { |
| 263 | if (!seenSymbol[i]) { |
| 264 | return false; |
| 265 | } |
| 266 | } |
| 267 | return true; |
| 268 | } |
| 269 | |
| 270 | // If monetary decimal or grouping were not explicitly set, then set them to be the |
| 271 | // same as their non-monetary counterparts. |
| 272 | void resolveMissingMonetarySeparators(const UnicodeString* fSymbols) { |
| 273 | if (!seenSymbol[DecimalFormatSymbols::kMonetarySeparatorSymbol]) { |
| 274 | dfs.setSymbol( |
| 275 | DecimalFormatSymbols::kMonetarySeparatorSymbol, |
| 276 | fSymbols[DecimalFormatSymbols::kDecimalSeparatorSymbol]); |
| 277 | } |
| 278 | if (!seenSymbol[DecimalFormatSymbols::kMonetaryGroupingSeparatorSymbol]) { |
| 279 | dfs.setSymbol( |
| 280 | DecimalFormatSymbols::kMonetaryGroupingSeparatorSymbol, |
| 281 | fSymbols[DecimalFormatSymbols::kGroupingSeparatorSymbol]); |
| 282 | } |
| 283 | } |
| 284 | }; |
| 285 | |
| 286 | struct CurrencySpacingSink : public ResourceSink { |
| 287 | DecimalFormatSymbols& dfs; |
| 288 | UBool hasBeforeCurrency; |
| 289 | UBool hasAfterCurrency; |
| 290 | |
| 291 | CurrencySpacingSink(DecimalFormatSymbols& _dfs) |
| 292 | : dfs(_dfs), hasBeforeCurrency(false), hasAfterCurrency(false) {} |
| 293 | virtual ~CurrencySpacingSink(); |
| 294 | |
| 295 | virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/, |
| 296 | UErrorCode &errorCode) override { |
| 297 | ResourceTable spacingTypesTable = value.getTable(errorCode); |
| 298 | for (int32_t i = 0; spacingTypesTable.getKeyAndValue(i, key, value); ++i) { |
| 299 | UBool beforeCurrency; |
| 300 | if (uprv_strcmp(key, gBeforeCurrencyTag) == 0) { |
| 301 | beforeCurrency = true; |
| 302 | hasBeforeCurrency = true; |
| 303 | } else if (uprv_strcmp(key, gAfterCurrencyTag) == 0) { |
| 304 | beforeCurrency = false; |
| 305 | hasAfterCurrency = true; |
| 306 | } else { |
| 307 | continue; |
| 308 | } |
| 309 | |
| 310 | ResourceTable patternsTable = value.getTable(errorCode); |
| 311 | for (int32_t j = 0; patternsTable.getKeyAndValue(j, key, value); ++j) { |
| 312 | UCurrencySpacing pattern; |
| 313 | if (uprv_strcmp(key, gCurrencyMatchTag) == 0) { |
| 314 | pattern = UNUM_CURRENCY_MATCH; |
| 315 | } else if (uprv_strcmp(key, gCurrencySudMatchTag) == 0) { |
| 316 | pattern = UNUM_CURRENCY_SURROUNDING_MATCH; |
| 317 | } else if (uprv_strcmp(key, gCurrencyInsertBtnTag) == 0) { |
| 318 | pattern = UNUM_CURRENCY_INSERT; |
| 319 | } else { |
| 320 | continue; |
| 321 | } |
| 322 | |
| 323 | const UnicodeString& current = dfs.getPatternForCurrencySpacing( |
| 324 | pattern, beforeCurrency, errorCode); |
| 325 | if (current.isEmpty()) { |
| 326 | dfs.setPatternForCurrencySpacing( |
| 327 | pattern, beforeCurrency, value.getUnicodeString(errorCode)); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | void resolveMissing() { |
| 334 | // For consistency with Java, this method overwrites everything with the defaults unless |
| 335 | // both beforeCurrency and afterCurrency were found in CLDR. |
| 336 | static const char* defaults[] = { "[:letter:]", "[:digit:]", " " }; |
| 337 | if (!hasBeforeCurrency || !hasAfterCurrency) { |
| 338 | for (int32_t pattern = 0; pattern < UNUM_CURRENCY_SPACING_COUNT; pattern++) { |
| 339 | dfs.setPatternForCurrencySpacing(static_cast<UCurrencySpacing>(pattern), |
| 340 | false, UnicodeString(defaults[pattern], -1, US_INV)); |
| 341 | } |
| 342 | for (int32_t pattern = 0; pattern < UNUM_CURRENCY_SPACING_COUNT; pattern++) { |
| 343 | dfs.setPatternForCurrencySpacing(static_cast<UCurrencySpacing>(pattern), |
| 344 | true, UnicodeString(defaults[pattern], -1, US_INV)); |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | }; |
| 349 | |
| 350 | // Virtual destructors must be defined out of line. |
| 351 | DecFmtSymDataSink::~DecFmtSymDataSink() {} |
| 352 | CurrencySpacingSink::~CurrencySpacingSink() {} |
| 353 | |
| 354 | } // namespace |
| 355 | |
| 356 | void |
| 357 | DecimalFormatSymbols::initialize(const Locale& loc, UErrorCode& status, |
| 358 | UBool useLastResortData, const NumberingSystem* ns) |
| 359 | { |
| 360 | if (U_FAILURE(status)) { return; } |
| 361 | |
| 362 | // First initialize all the symbols to the fallbacks for anything we can't find |
| 363 | initialize(); |
| 364 | |
| 365 | // |
| 366 | // Next get the numbering system for this locale and set zero digit |
| 367 | // and the digit string based on the numbering system for the locale |
| 368 | // |
| 369 | LocalPointer<NumberingSystem> nsLocal; |
| 370 | if (ns == nullptr) { |
| 371 | // Use the numbering system according to the locale. |
| 372 | // Save it into a LocalPointer so it gets cleaned up. |
| 373 | nsLocal.adoptInstead(NumberingSystem::createInstance(loc, status)); |
| 374 | ns = nsLocal.getAlias(); |
| 375 | } |
| 376 | const char *nsName; |
| 377 | if (U_SUCCESS(status) && ns->getRadix() == 10 && !ns->isAlgorithmic()) { |
| 378 | nsName = ns->getName(); |
| 379 | UnicodeString digitString(ns->getDescription()); |
| 380 | int32_t digitIndex = 0; |
| 381 | UChar32 digit = digitString.char32At(0); |
| 382 | fSymbols[kZeroDigitSymbol].setTo(digit); |
| 383 | for (int32_t i = kOneDigitSymbol; i <= kNineDigitSymbol; ++i) { |
| 384 | digitIndex += U16_LENGTH(digit); |
| 385 | digit = digitString.char32At(digitIndex); |
| 386 | fSymbols[i].setTo(digit); |
| 387 | } |
| 388 | } else { |
| 389 | nsName = gLatn; |
| 390 | } |
| 391 | uprv_strcpy(this->nsName, nsName); |
| 392 | |
| 393 | // Open resource bundles |
| 394 | const char* locStr = loc.getName(); |
| 395 | LocalUResourceBundlePointer resource(ures_open(nullptr, locStr, &status)); |
| 396 | LocalUResourceBundlePointer numberElementsRes( |
| 397 | ures_getByKeyWithFallback(resource.getAlias(), gNumberElements, nullptr, &status)); |
| 398 | |
| 399 | if (U_FAILURE(status)) { |
| 400 | if ( useLastResortData ) { |
| 401 | status = U_USING_DEFAULT_WARNING; |
| 402 | initialize(); |
| 403 | } |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | // Set locale IDs |
| 408 | // TODO: Is there a way to do this without depending on the resource bundle instance? |
| 409 | U_LOCALE_BASED(locBased, *this); |
| 410 | locBased.setLocaleIDs( |
| 411 | ures_getLocaleByType( |
| 412 | numberElementsRes.getAlias(), |
| 413 | ULOC_VALID_LOCALE, &status), |
| 414 | ures_getLocaleByType( |
| 415 | numberElementsRes.getAlias(), |
| 416 | ULOC_ACTUAL_LOCALE, &status), |
| 417 | status); |
| 418 | |
| 419 | // Now load the rest of the data from the data sink. |
| 420 | // Start with loading this nsName if it is not Latin. |
| 421 | DecFmtSymDataSink sink(*this); |
| 422 | if (uprv_strcmp(nsName, gLatn) != 0) { |
| 423 | CharString path; |
| 424 | path.append(gNumberElements, status) |
| 425 | .append('/', status) |
| 426 | .append(nsName, status) |
| 427 | .append('/', status) |
| 428 | .append(gSymbols, status); |
| 429 | ures_getAllItemsWithFallback(resource.getAlias(), path.data(), sink, status); |
| 430 | |
| 431 | // If no symbols exist for the given nsName and resource bundle, silently ignore |
| 432 | // and fall back to Latin. |
| 433 | if (status == U_MISSING_RESOURCE_ERROR) { |
| 434 | status = U_ZERO_ERROR; |
| 435 | } else if (U_FAILURE(status)) { |
| 436 | return; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | // Continue with Latin if necessary. |
| 441 | if (!sink.seenAll()) { |
| 442 | ures_getAllItemsWithFallback(resource.getAlias(), gNumberElementsLatnSymbols, sink, status); |
| 443 | if (U_FAILURE(status)) { return; } |
| 444 | } |
| 445 | |
| 446 | // Let the monetary number separators equal the default number separators if necessary. |
| 447 | sink.resolveMissingMonetarySeparators(fSymbols); |
| 448 | |
| 449 | // Resolve codePointZero |
| 450 | UChar32 tempCodePointZero = -1; |
| 451 | for (int32_t i=0; i<=9; i++) { |
| 452 | const UnicodeString& stringDigit = getConstDigitSymbol(i); |
| 453 | if (stringDigit.countChar32() != 1) { |
| 454 | tempCodePointZero = -1; |
| 455 | break; |
| 456 | } |
| 457 | UChar32 cp = stringDigit.char32At(0); |
| 458 | if (i == 0) { |
| 459 | tempCodePointZero = cp; |
| 460 | } else if (cp != tempCodePointZero + i) { |
| 461 | tempCodePointZero = -1; |
| 462 | break; |
| 463 | } |
| 464 | } |
| 465 | fCodePointZero = tempCodePointZero; |
| 466 | |
| 467 | // Get the default currency from the currency API. |
| 468 | UErrorCode internalStatus = U_ZERO_ERROR; // don't propagate failures out |
| 469 | char16_t curriso[4]; |
| 470 | UnicodeString tempStr; |
| 471 | int32_t currisoLength = ucurr_forLocale(locStr, curriso, UPRV_LENGTHOF(curriso), &internalStatus); |
| 472 | if (U_SUCCESS(internalStatus) && currisoLength == 3) { |
| 473 | setCurrency(curriso, status); |
| 474 | } else { |
| 475 | setCurrency(nullptr, status); |
| 476 | } |
| 477 | |
| 478 | // Currency Spacing. |
| 479 | LocalUResourceBundlePointer currencyResource(ures_open(U_ICUDATA_CURR, locStr, &status)); |
| 480 | CurrencySpacingSink currencySink(*this); |
| 481 | ures_getAllItemsWithFallback(currencyResource.getAlias(), gCurrencySpacingTag, currencySink, status); |
| 482 | currencySink.resolveMissing(); |
| 483 | if (U_FAILURE(status)) { return; } |
| 484 | } |
| 485 | |
| 486 | void |
| 487 | DecimalFormatSymbols::initialize() { |
| 488 | /* |
| 489 | * These strings used to be in static arrays, but the HP/UX aCC compiler |
| 490 | * cannot initialize a static array with class constructors. |
| 491 | * markus 2000may25 |
| 492 | */ |
| 493 | fSymbols[kDecimalSeparatorSymbol] = static_cast<char16_t>(0x2e); // '.' decimal separator |
| 494 | fSymbols[kGroupingSeparatorSymbol].remove(); // group (thousands) separator |
| 495 | fSymbols[kPatternSeparatorSymbol] = static_cast<char16_t>(0x3b); // ';' pattern separator |
| 496 | fSymbols[kPercentSymbol] = static_cast<char16_t>(0x25); // '%' percent sign |
| 497 | fSymbols[kZeroDigitSymbol] = static_cast<char16_t>(0x30); // '0' native 0 digit |
| 498 | fSymbols[kOneDigitSymbol] = static_cast<char16_t>(0x31); // '1' native 1 digit |
| 499 | fSymbols[kTwoDigitSymbol] = static_cast<char16_t>(0x32); // '2' native 2 digit |
| 500 | fSymbols[kThreeDigitSymbol] = static_cast<char16_t>(0x33); // '3' native 3 digit |
| 501 | fSymbols[kFourDigitSymbol] = static_cast<char16_t>(0x34); // '4' native 4 digit |
| 502 | fSymbols[kFiveDigitSymbol] = static_cast<char16_t>(0x35); // '5' native 5 digit |
| 503 | fSymbols[kSixDigitSymbol] = static_cast<char16_t>(0x36); // '6' native 6 digit |
| 504 | fSymbols[kSevenDigitSymbol] = static_cast<char16_t>(0x37); // '7' native 7 digit |
| 505 | fSymbols[kEightDigitSymbol] = static_cast<char16_t>(0x38); // '8' native 8 digit |
| 506 | fSymbols[kNineDigitSymbol] = static_cast<char16_t>(0x39); // '9' native 9 digit |
| 507 | fSymbols[kDigitSymbol] = static_cast<char16_t>(0x23); // '#' pattern digit |
| 508 | fSymbols[kPlusSignSymbol] = static_cast<char16_t>(0x002b); // '+' plus sign |
| 509 | fSymbols[kMinusSignSymbol] = static_cast<char16_t>(0x2d); // '-' minus sign |
| 510 | fSymbols[kCurrencySymbol] = static_cast<char16_t>(0xa4); // 'OX' currency symbol |
| 511 | fSymbols[kIntlCurrencySymbol].setTo(true, INTL_CURRENCY_SYMBOL_STR, 2); |
| 512 | fSymbols[kMonetarySeparatorSymbol] = static_cast<char16_t>(0x2e); // '.' monetary decimal separator |
| 513 | fSymbols[kExponentialSymbol] = static_cast<char16_t>(0x45); // 'E' exponential |
| 514 | fSymbols[kPerMillSymbol] = static_cast<char16_t>(0x2030); // '%o' per mill |
| 515 | fSymbols[kPadEscapeSymbol] = static_cast<char16_t>(0x2a); // '*' pad escape symbol |
| 516 | fSymbols[kInfinitySymbol] = static_cast<char16_t>(0x221e); // 'oo' infinite |
| 517 | fSymbols[kNaNSymbol] = static_cast<char16_t>(0xfffd); // SUB NaN |
| 518 | fSymbols[kSignificantDigitSymbol] = static_cast<char16_t>(0x0040); // '@' significant digit |
| 519 | fSymbols[kMonetaryGroupingSeparatorSymbol].remove(); // |
| 520 | fSymbols[kExponentMultiplicationSymbol] = static_cast<char16_t>(0xd7); // 'x' multiplication symbol for exponents |
| 521 | fSymbols[kApproximatelySignSymbol] = u'~'; // '~' approximately sign |
| 522 | fIsCustomCurrencySymbol = false; |
| 523 | fIsCustomIntlCurrencySymbol = false; |
| 524 | fCodePointZero = 0x30; |
| 525 | U_ASSERT(fCodePointZero == fSymbols[kZeroDigitSymbol].char32At(0)); |
| 526 | currPattern = nullptr; |
| 527 | nsName[0] = 0; |
| 528 | } |
| 529 | |
| 530 | void DecimalFormatSymbols::setCurrency(const char16_t* currency, UErrorCode& status) { |
| 531 | // TODO: If this method is made public: |
| 532 | // - Adopt ICU4J behavior of not allowing currency to be null. |
| 533 | // - Also verify that the length of currency is 3. |
| 534 | if (!currency) { |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | UnicodeString tempStr; |
| 539 | uprv_getStaticCurrencyName(currency, locale.getName(), tempStr, status); |
| 540 | if (U_SUCCESS(status)) { |
| 541 | fSymbols[kIntlCurrencySymbol].setTo(currency, 3); |
| 542 | fSymbols[kCurrencySymbol] = tempStr; |
| 543 | } |
| 544 | |
| 545 | char cc[4]={0}; |
| 546 | u_UCharsToChars(currency, cc, 3); |
| 547 | |
| 548 | /* An explicit currency was requested */ |
| 549 | // TODO(ICU-13297): Move this data loading logic into a centralized place |
| 550 | UErrorCode localStatus = U_ZERO_ERROR; |
| 551 | LocalUResourceBundlePointer rbTop(ures_open(U_ICUDATA_CURR, locale.getName(), &localStatus)); |
| 552 | LocalUResourceBundlePointer rb( |
| 553 | ures_getByKeyWithFallback(rbTop.getAlias(), "Currencies", nullptr, &localStatus)); |
| 554 | ures_getByKeyWithFallback(rb.getAlias(), cc, rb.getAlias(), &localStatus); |
| 555 | if(U_SUCCESS(localStatus) && ures_getSize(rb.getAlias())>2) { // the length is 3 if more data is present |
| 556 | ures_getByIndex(rb.getAlias(), 2, rb.getAlias(), &localStatus); |
| 557 | int32_t currPatternLen = 0; |
| 558 | currPattern = |
| 559 | ures_getStringByIndex(rb.getAlias(), static_cast<int32_t>(0), &currPatternLen, &localStatus); |
| 560 | UnicodeString decimalSep = |
| 561 | ures_getUnicodeStringByIndex(rb.getAlias(), static_cast<int32_t>(1), &localStatus); |
| 562 | UnicodeString groupingSep = |
| 563 | ures_getUnicodeStringByIndex(rb.getAlias(), static_cast<int32_t>(2), &localStatus); |
| 564 | if(U_SUCCESS(localStatus)){ |
| 565 | fSymbols[kMonetaryGroupingSeparatorSymbol] = groupingSep; |
| 566 | fSymbols[kMonetarySeparatorSymbol] = decimalSep; |
| 567 | //pattern.setTo(true, currPattern, currPatternLen); |
| 568 | } |
| 569 | } |
| 570 | /* else An explicit currency was requested and is unknown or locale data is malformed. */ |
| 571 | /* ucurr_* API will get the correct value later on. */ |
| 572 | } |
| 573 | |
| 574 | Locale |
| 575 | DecimalFormatSymbols::getLocale(ULocDataLocaleType type, UErrorCode& status) const { |
| 576 | return LocaleBased::getLocale(validLocale, actualLocale, type, status); |
| 577 | } |
| 578 | |
| 579 | const UnicodeString& |
| 580 | DecimalFormatSymbols::getPatternForCurrencySpacing(UCurrencySpacing type, |
| 581 | UBool beforeCurrency, |
| 582 | UErrorCode& status) const { |
| 583 | if (U_FAILURE(status)) { |
| 584 | return fNoSymbol; // always empty. |
| 585 | } |
| 586 | if (beforeCurrency) { |
| 587 | return currencySpcBeforeSym[static_cast<int32_t>(type)]; |
| 588 | } else { |
| 589 | return currencySpcAfterSym[static_cast<int32_t>(type)]; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | void |
| 594 | DecimalFormatSymbols::setPatternForCurrencySpacing(UCurrencySpacing type, |
| 595 | UBool beforeCurrency, |
| 596 | const UnicodeString& pattern) { |
| 597 | if (beforeCurrency) { |
| 598 | currencySpcBeforeSym[static_cast<int32_t>(type)] = pattern; |
| 599 | } else { |
| 600 | currencySpcAfterSym[static_cast<int32_t>(type)] = pattern; |
| 601 | } |
| 602 | } |
| 603 | U_NAMESPACE_END |
| 604 | |
| 605 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
| 606 | |
| 607 | //eof |