/src/icu/icu4c/source/i18n/dcfmtsym.cpp
Line | Count | Source (jump to first uncovered line) |
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 | 158k | : UObject(), locale() { |
103 | 158k | initialize(locale, status, true); |
104 | 158k | } |
105 | | |
106 | | // ------------------------------------- |
107 | | // Initializes this with the decimal format symbols in the desired locale. |
108 | | |
109 | | DecimalFormatSymbols::DecimalFormatSymbols(const Locale& loc, UErrorCode& status) |
110 | 123k | : UObject(), locale(loc) { |
111 | 123k | initialize(locale, status); |
112 | 123k | } |
113 | | |
114 | | DecimalFormatSymbols::DecimalFormatSymbols(const Locale& loc, const NumberingSystem& ns, UErrorCode& status) |
115 | 10.0k | : UObject(), locale(loc) { |
116 | 10.0k | initialize(locale, status, false, &ns); |
117 | 10.0k | } |
118 | | |
119 | | DecimalFormatSymbols::DecimalFormatSymbols() |
120 | 0 | : UObject(), locale(Locale::getRoot()) { |
121 | 0 | initialize(); |
122 | 0 | } |
123 | | |
124 | | DecimalFormatSymbols* |
125 | 0 | DecimalFormatSymbols::createWithLastResortData(UErrorCode& status) { |
126 | 0 | if (U_FAILURE(status)) { return nullptr; } |
127 | 0 | DecimalFormatSymbols* sym = new DecimalFormatSymbols(); |
128 | 0 | if (sym == nullptr) { |
129 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
130 | 0 | } |
131 | 0 | return sym; |
132 | 0 | } |
133 | | |
134 | | // ------------------------------------- |
135 | | |
136 | | DecimalFormatSymbols::~DecimalFormatSymbols() |
137 | 1.07M | { |
138 | 1.07M | delete actualLocale; |
139 | 1.07M | delete validLocale; |
140 | 1.07M | } |
141 | | |
142 | | // ------------------------------------- |
143 | | // copy constructor |
144 | | |
145 | | DecimalFormatSymbols::DecimalFormatSymbols(const DecimalFormatSymbols &source) |
146 | 783k | : UObject(source) |
147 | 783k | { |
148 | 783k | *this = source; |
149 | 783k | } |
150 | | |
151 | | // ------------------------------------- |
152 | | // assignment operator |
153 | | |
154 | | DecimalFormatSymbols& |
155 | | DecimalFormatSymbols::operator=(const DecimalFormatSymbols& rhs) |
156 | 783k | { |
157 | 783k | if (this != &rhs) { |
158 | 23.5M | for (int32_t i = 0; i < static_cast<int32_t>(kFormatSymbolCount); ++i) { |
159 | | // fastCopyFrom is safe, see docs on fSymbols |
160 | 22.7M | fSymbols[static_cast<ENumberFormatSymbol>(i)].fastCopyFrom(rhs.fSymbols[static_cast<ENumberFormatSymbol>(i)]); |
161 | 22.7M | } |
162 | 3.13M | for (int32_t i = 0; i < static_cast<int32_t>(UNUM_CURRENCY_SPACING_COUNT); ++i) { |
163 | 2.35M | currencySpcBeforeSym[i].fastCopyFrom(rhs.currencySpcBeforeSym[i]); |
164 | 2.35M | currencySpcAfterSym[i].fastCopyFrom(rhs.currencySpcAfterSym[i]); |
165 | 2.35M | } |
166 | 783k | locale = rhs.locale; |
167 | | |
168 | 783k | UErrorCode status = U_ZERO_ERROR; |
169 | 783k | U_LOCALE_BASED(locBased, *this); |
170 | 783k | locBased.setLocaleIDs(rhs.validLocale, rhs.actualLocale, status); |
171 | 783k | U_ASSERT(U_SUCCESS(status)); |
172 | | |
173 | 783k | fIsCustomCurrencySymbol = rhs.fIsCustomCurrencySymbol; |
174 | 783k | fIsCustomIntlCurrencySymbol = rhs.fIsCustomIntlCurrencySymbol; |
175 | 783k | fCodePointZero = rhs.fCodePointZero; |
176 | 783k | currPattern = rhs.currPattern; |
177 | 783k | uprv_strcpy(nsName, rhs.nsName); |
178 | 783k | } |
179 | 783k | return *this; |
180 | 783k | } |
181 | | |
182 | | // ------------------------------------- |
183 | | |
184 | | bool |
185 | | DecimalFormatSymbols::operator==(const DecimalFormatSymbols& that) const |
186 | 0 | { |
187 | 0 | if (this == &that) { |
188 | 0 | return true; |
189 | 0 | } |
190 | 0 | if (fIsCustomCurrencySymbol != that.fIsCustomCurrencySymbol) { |
191 | 0 | return false; |
192 | 0 | } |
193 | 0 | if (fIsCustomIntlCurrencySymbol != that.fIsCustomIntlCurrencySymbol) { |
194 | 0 | return false; |
195 | 0 | } |
196 | 0 | for (int32_t i = 0; i < static_cast<int32_t>(kFormatSymbolCount); ++i) { |
197 | 0 | if (fSymbols[static_cast<ENumberFormatSymbol>(i)] != that.fSymbols[static_cast<ENumberFormatSymbol>(i)]) { |
198 | 0 | return false; |
199 | 0 | } |
200 | 0 | } |
201 | 0 | for (int32_t i = 0; i < static_cast<int32_t>(UNUM_CURRENCY_SPACING_COUNT); ++i) { |
202 | 0 | if(currencySpcBeforeSym[i] != that.currencySpcBeforeSym[i]) { |
203 | 0 | return false; |
204 | 0 | } |
205 | 0 | if(currencySpcAfterSym[i] != that.currencySpcAfterSym[i]) { |
206 | 0 | return false; |
207 | 0 | } |
208 | 0 | } |
209 | | // No need to check fCodePointZero since it is based on fSymbols |
210 | 0 | return locale == that.locale && |
211 | 0 | LocaleBased::equalIDs(actualLocale, that.actualLocale) && |
212 | 0 | LocaleBased::equalIDs(validLocale, that.validLocale); |
213 | 0 | } |
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 | 289k | DecFmtSymDataSink(DecimalFormatSymbols& _dfs) : dfs(_dfs) { |
236 | 289k | uprv_memset(seenSymbol, false, sizeof(seenSymbol)); |
237 | 289k | } |
238 | | virtual ~DecFmtSymDataSink(); |
239 | | |
240 | | virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/, |
241 | 783k | UErrorCode &errorCode) override { |
242 | 783k | ResourceTable symbolsTable = value.getTable(errorCode); |
243 | 783k | if (U_FAILURE(errorCode)) { return; } |
244 | 7.62M | for (int32_t j = 0; symbolsTable.getKeyAndValue(j, key, value); ++j) { |
245 | 94.7M | for (int32_t i=0; i<DecimalFormatSymbols::kFormatSymbolCount; i++) { |
246 | 93.9M | if (gNumberElementKeys[i] != nullptr && uprv_strcmp(key, gNumberElementKeys[i]) == 0) { |
247 | 6.01M | if (!seenSymbol[i]) { |
248 | 3.18M | seenSymbol[i] = true; |
249 | 3.18M | dfs.setSymbol( |
250 | 3.18M | static_cast<DecimalFormatSymbols::ENumberFormatSymbol>(i), |
251 | 3.18M | value.getUnicodeString(errorCode)); |
252 | 3.18M | if (U_FAILURE(errorCode)) { return; } |
253 | 3.18M | } |
254 | 6.01M | break; |
255 | 6.01M | } |
256 | 93.9M | } |
257 | 6.84M | } |
258 | 783k | } |
259 | | |
260 | | // Returns true if all the symbols have been seen. |
261 | 289k | UBool seenAll() { |
262 | 317k | for (int32_t i=0; i<DecimalFormatSymbols::kFormatSymbolCount; i++) { |
263 | 317k | if (!seenSymbol[i]) { |
264 | 289k | return false; |
265 | 289k | } |
266 | 317k | } |
267 | 0 | return true; |
268 | 289k | } |
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 | 289k | void resolveMissingMonetarySeparators(const UnicodeString* fSymbols) { |
273 | 289k | if (!seenSymbol[DecimalFormatSymbols::kMonetarySeparatorSymbol]) { |
274 | 289k | dfs.setSymbol( |
275 | 289k | DecimalFormatSymbols::kMonetarySeparatorSymbol, |
276 | 289k | fSymbols[DecimalFormatSymbols::kDecimalSeparatorSymbol]); |
277 | 289k | } |
278 | 289k | if (!seenSymbol[DecimalFormatSymbols::kMonetaryGroupingSeparatorSymbol]) { |
279 | 289k | dfs.setSymbol( |
280 | 289k | DecimalFormatSymbols::kMonetaryGroupingSeparatorSymbol, |
281 | 289k | fSymbols[DecimalFormatSymbols::kGroupingSeparatorSymbol]); |
282 | 289k | } |
283 | 289k | } |
284 | | }; |
285 | | |
286 | | struct CurrencySpacingSink : public ResourceSink { |
287 | | DecimalFormatSymbols& dfs; |
288 | | UBool hasBeforeCurrency; |
289 | | UBool hasAfterCurrency; |
290 | | |
291 | | CurrencySpacingSink(DecimalFormatSymbols& _dfs) |
292 | 289k | : dfs(_dfs), hasBeforeCurrency(false), hasAfterCurrency(false) {} |
293 | | virtual ~CurrencySpacingSink(); |
294 | | |
295 | | virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/, |
296 | 289k | UErrorCode &errorCode) override { |
297 | 289k | ResourceTable spacingTypesTable = value.getTable(errorCode); |
298 | 869k | for (int32_t i = 0; spacingTypesTable.getKeyAndValue(i, key, value); ++i) { |
299 | 579k | UBool beforeCurrency; |
300 | 579k | if (uprv_strcmp(key, gBeforeCurrencyTag) == 0) { |
301 | 289k | beforeCurrency = true; |
302 | 289k | hasBeforeCurrency = true; |
303 | 289k | } else if (uprv_strcmp(key, gAfterCurrencyTag) == 0) { |
304 | 289k | beforeCurrency = false; |
305 | 289k | hasAfterCurrency = true; |
306 | 289k | } else { |
307 | 0 | continue; |
308 | 0 | } |
309 | | |
310 | 579k | ResourceTable patternsTable = value.getTable(errorCode); |
311 | 2.31M | for (int32_t j = 0; patternsTable.getKeyAndValue(j, key, value); ++j) { |
312 | 1.73M | UCurrencySpacing pattern; |
313 | 1.73M | if (uprv_strcmp(key, gCurrencyMatchTag) == 0) { |
314 | 579k | pattern = UNUM_CURRENCY_MATCH; |
315 | 1.15M | } else if (uprv_strcmp(key, gCurrencySudMatchTag) == 0) { |
316 | 579k | pattern = UNUM_CURRENCY_SURROUNDING_MATCH; |
317 | 579k | } else if (uprv_strcmp(key, gCurrencyInsertBtnTag) == 0) { |
318 | 579k | pattern = UNUM_CURRENCY_INSERT; |
319 | 579k | } else { |
320 | 0 | continue; |
321 | 0 | } |
322 | | |
323 | 1.73M | const UnicodeString& current = dfs.getPatternForCurrencySpacing( |
324 | 1.73M | pattern, beforeCurrency, errorCode); |
325 | 1.73M | if (current.isEmpty()) { |
326 | 1.73M | dfs.setPatternForCurrencySpacing( |
327 | 1.73M | pattern, beforeCurrency, value.getUnicodeString(errorCode)); |
328 | 1.73M | } |
329 | 1.73M | } |
330 | 579k | } |
331 | 289k | } |
332 | | |
333 | 289k | 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 | 289k | static const char* defaults[] = { "[:letter:]", "[:digit:]", " " }; |
337 | 289k | if (!hasBeforeCurrency || !hasAfterCurrency) { |
338 | 20 | for (int32_t pattern = 0; pattern < UNUM_CURRENCY_SPACING_COUNT; pattern++) { |
339 | 15 | dfs.setPatternForCurrencySpacing(static_cast<UCurrencySpacing>(pattern), |
340 | 15 | false, UnicodeString(defaults[pattern], -1, US_INV)); |
341 | 15 | } |
342 | 20 | for (int32_t pattern = 0; pattern < UNUM_CURRENCY_SPACING_COUNT; pattern++) { |
343 | 15 | dfs.setPatternForCurrencySpacing(static_cast<UCurrencySpacing>(pattern), |
344 | 15 | true, UnicodeString(defaults[pattern], -1, US_INV)); |
345 | 15 | } |
346 | 5 | } |
347 | 289k | } |
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 | 291k | { |
360 | 291k | if (U_FAILURE(status)) { return; } |
361 | | |
362 | | // First initialize all the symbols to the fallbacks for anything we can't find |
363 | 290k | 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 | 290k | LocalPointer<NumberingSystem> nsLocal; |
370 | 290k | if (ns == nullptr) { |
371 | | // Use the numbering system according to the locale. |
372 | | // Save it into a LocalPointer so it gets cleaned up. |
373 | 280k | nsLocal.adoptInstead(NumberingSystem::createInstance(loc, status)); |
374 | 280k | ns = nsLocal.getAlias(); |
375 | 280k | } |
376 | 290k | const char *nsName; |
377 | 290k | if (U_SUCCESS(status) && ns->getRadix() == 10 && !ns->isAlgorithmic()) { |
378 | 289k | nsName = ns->getName(); |
379 | 289k | UnicodeString digitString(ns->getDescription()); |
380 | 289k | int32_t digitIndex = 0; |
381 | 289k | UChar32 digit = digitString.char32At(0); |
382 | 289k | fSymbols[kZeroDigitSymbol].setTo(digit); |
383 | 2.89M | for (int32_t i = kOneDigitSymbol; i <= kNineDigitSymbol; ++i) { |
384 | 2.60M | digitIndex += U16_LENGTH(digit); |
385 | 2.60M | digit = digitString.char32At(digitIndex); |
386 | 2.60M | fSymbols[i].setTo(digit); |
387 | 2.60M | } |
388 | 289k | } else { |
389 | 896 | nsName = gLatn; |
390 | 896 | } |
391 | 290k | uprv_strcpy(this->nsName, nsName); |
392 | | |
393 | | // Open resource bundles |
394 | 290k | const char* locStr = loc.getName(); |
395 | 290k | LocalUResourceBundlePointer resource(ures_open(nullptr, locStr, &status)); |
396 | 290k | LocalUResourceBundlePointer numberElementsRes( |
397 | 290k | ures_getByKeyWithFallback(resource.getAlias(), gNumberElements, nullptr, &status)); |
398 | | |
399 | 290k | if (U_FAILURE(status)) { |
400 | 326 | if ( useLastResortData ) { |
401 | 0 | status = U_USING_DEFAULT_WARNING; |
402 | 0 | initialize(); |
403 | 0 | } |
404 | 326 | return; |
405 | 326 | } |
406 | | |
407 | | // Set locale IDs |
408 | | // TODO: Is there a way to do this without depending on the resource bundle instance? |
409 | 289k | U_LOCALE_BASED(locBased, *this); |
410 | 289k | locBased.setLocaleIDs( |
411 | 289k | ures_getLocaleByType( |
412 | 289k | numberElementsRes.getAlias(), |
413 | 289k | ULOC_VALID_LOCALE, &status), |
414 | 289k | ures_getLocaleByType( |
415 | 289k | numberElementsRes.getAlias(), |
416 | 289k | ULOC_ACTUAL_LOCALE, &status), |
417 | 289k | 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 | 289k | DecFmtSymDataSink sink(*this); |
422 | 289k | if (uprv_strcmp(nsName, gLatn) != 0) { |
423 | 13.8k | CharString path; |
424 | 13.8k | path.append(gNumberElements, status) |
425 | 13.8k | .append('/', status) |
426 | 13.8k | .append(nsName, status) |
427 | 13.8k | .append('/', status) |
428 | 13.8k | .append(gSymbols, status); |
429 | 13.8k | 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 | 13.8k | if (status == U_MISSING_RESOURCE_ERROR) { |
434 | 0 | status = U_ZERO_ERROR; |
435 | 13.8k | } else if (U_FAILURE(status)) { |
436 | 0 | return; |
437 | 0 | } |
438 | 13.8k | } |
439 | | |
440 | | // Continue with Latin if necessary. |
441 | 289k | if (!sink.seenAll()) { |
442 | 289k | ures_getAllItemsWithFallback(resource.getAlias(), gNumberElementsLatnSymbols, sink, status); |
443 | 289k | if (U_FAILURE(status)) { return; } |
444 | 289k | } |
445 | | |
446 | | // Let the monetary number separators equal the default number separators if necessary. |
447 | 289k | sink.resolveMissingMonetarySeparators(fSymbols); |
448 | | |
449 | | // Resolve codePointZero |
450 | 289k | UChar32 tempCodePointZero = -1; |
451 | 3.18M | for (int32_t i=0; i<=9; i++) { |
452 | 2.89M | const UnicodeString& stringDigit = getConstDigitSymbol(i); |
453 | 2.89M | if (stringDigit.countChar32() != 1) { |
454 | 0 | tempCodePointZero = -1; |
455 | 0 | break; |
456 | 0 | } |
457 | 2.89M | UChar32 cp = stringDigit.char32At(0); |
458 | 2.89M | if (i == 0) { |
459 | 289k | tempCodePointZero = cp; |
460 | 2.60M | } else if (cp != tempCodePointZero + i) { |
461 | 13 | tempCodePointZero = -1; |
462 | 13 | break; |
463 | 13 | } |
464 | 2.89M | } |
465 | 289k | fCodePointZero = tempCodePointZero; |
466 | | |
467 | | // Get the default currency from the currency API. |
468 | 289k | UErrorCode internalStatus = U_ZERO_ERROR; // don't propagate failures out |
469 | 289k | char16_t curriso[4]; |
470 | 289k | UnicodeString tempStr; |
471 | 289k | int32_t currisoLength = ucurr_forLocale(locStr, curriso, UPRV_LENGTHOF(curriso), &internalStatus); |
472 | 289k | if (U_SUCCESS(internalStatus) && currisoLength == 3) { |
473 | 246k | setCurrency(curriso, status); |
474 | 246k | } else { |
475 | 42.8k | setCurrency(nullptr, status); |
476 | 42.8k | } |
477 | | |
478 | | // Currency Spacing. |
479 | 289k | LocalUResourceBundlePointer currencyResource(ures_open(U_ICUDATA_CURR, locStr, &status)); |
480 | 289k | CurrencySpacingSink currencySink(*this); |
481 | 289k | ures_getAllItemsWithFallback(currencyResource.getAlias(), gCurrencySpacingTag, currencySink, status); |
482 | 289k | currencySink.resolveMissing(); |
483 | 289k | if (U_FAILURE(status)) { return; } |
484 | 289k | } |
485 | | |
486 | | void |
487 | 290k | 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 | 290k | fSymbols[kDecimalSeparatorSymbol] = static_cast<char16_t>(0x2e); // '.' decimal separator |
494 | 290k | fSymbols[kGroupingSeparatorSymbol].remove(); // group (thousands) separator |
495 | 290k | fSymbols[kPatternSeparatorSymbol] = static_cast<char16_t>(0x3b); // ';' pattern separator |
496 | 290k | fSymbols[kPercentSymbol] = static_cast<char16_t>(0x25); // '%' percent sign |
497 | 290k | fSymbols[kZeroDigitSymbol] = static_cast<char16_t>(0x30); // '0' native 0 digit |
498 | 290k | fSymbols[kOneDigitSymbol] = static_cast<char16_t>(0x31); // '1' native 1 digit |
499 | 290k | fSymbols[kTwoDigitSymbol] = static_cast<char16_t>(0x32); // '2' native 2 digit |
500 | 290k | fSymbols[kThreeDigitSymbol] = static_cast<char16_t>(0x33); // '3' native 3 digit |
501 | 290k | fSymbols[kFourDigitSymbol] = static_cast<char16_t>(0x34); // '4' native 4 digit |
502 | 290k | fSymbols[kFiveDigitSymbol] = static_cast<char16_t>(0x35); // '5' native 5 digit |
503 | 290k | fSymbols[kSixDigitSymbol] = static_cast<char16_t>(0x36); // '6' native 6 digit |
504 | 290k | fSymbols[kSevenDigitSymbol] = static_cast<char16_t>(0x37); // '7' native 7 digit |
505 | 290k | fSymbols[kEightDigitSymbol] = static_cast<char16_t>(0x38); // '8' native 8 digit |
506 | 290k | fSymbols[kNineDigitSymbol] = static_cast<char16_t>(0x39); // '9' native 9 digit |
507 | 290k | fSymbols[kDigitSymbol] = static_cast<char16_t>(0x23); // '#' pattern digit |
508 | 290k | fSymbols[kPlusSignSymbol] = static_cast<char16_t>(0x002b); // '+' plus sign |
509 | 290k | fSymbols[kMinusSignSymbol] = static_cast<char16_t>(0x2d); // '-' minus sign |
510 | 290k | fSymbols[kCurrencySymbol] = static_cast<char16_t>(0xa4); // 'OX' currency symbol |
511 | 290k | fSymbols[kIntlCurrencySymbol].setTo(true, INTL_CURRENCY_SYMBOL_STR, 2); |
512 | 290k | fSymbols[kMonetarySeparatorSymbol] = static_cast<char16_t>(0x2e); // '.' monetary decimal separator |
513 | 290k | fSymbols[kExponentialSymbol] = static_cast<char16_t>(0x45); // 'E' exponential |
514 | 290k | fSymbols[kPerMillSymbol] = static_cast<char16_t>(0x2030); // '%o' per mill |
515 | 290k | fSymbols[kPadEscapeSymbol] = static_cast<char16_t>(0x2a); // '*' pad escape symbol |
516 | 290k | fSymbols[kInfinitySymbol] = static_cast<char16_t>(0x221e); // 'oo' infinite |
517 | 290k | fSymbols[kNaNSymbol] = static_cast<char16_t>(0xfffd); // SUB NaN |
518 | 290k | fSymbols[kSignificantDigitSymbol] = static_cast<char16_t>(0x0040); // '@' significant digit |
519 | 290k | fSymbols[kMonetaryGroupingSeparatorSymbol].remove(); // |
520 | 290k | fSymbols[kExponentMultiplicationSymbol] = static_cast<char16_t>(0xd7); // 'x' multiplication symbol for exponents |
521 | 290k | fSymbols[kApproximatelySignSymbol] = u'~'; // '~' approximately sign |
522 | 290k | fIsCustomCurrencySymbol = false; |
523 | 290k | fIsCustomIntlCurrencySymbol = false; |
524 | 290k | fCodePointZero = 0x30; |
525 | 290k | U_ASSERT(fCodePointZero == fSymbols[kZeroDigitSymbol].char32At(0)); |
526 | 290k | currPattern = nullptr; |
527 | 290k | nsName[0] = 0; |
528 | 290k | } |
529 | | |
530 | 289k | 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 | 289k | if (!currency) { |
535 | 42.8k | return; |
536 | 42.8k | } |
537 | | |
538 | 246k | UnicodeString tempStr; |
539 | 246k | uprv_getStaticCurrencyName(currency, locale.getName(), tempStr, status); |
540 | 246k | if (U_SUCCESS(status)) { |
541 | 246k | fSymbols[kIntlCurrencySymbol].setTo(currency, 3); |
542 | 246k | fSymbols[kCurrencySymbol] = tempStr; |
543 | 246k | } |
544 | | |
545 | 246k | char cc[4]={0}; |
546 | 246k | 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 | 246k | UErrorCode localStatus = U_ZERO_ERROR; |
551 | 246k | LocalUResourceBundlePointer rbTop(ures_open(U_ICUDATA_CURR, locale.getName(), &localStatus)); |
552 | 246k | LocalUResourceBundlePointer rb( |
553 | 246k | ures_getByKeyWithFallback(rbTop.getAlias(), "Currencies", nullptr, &localStatus)); |
554 | 246k | ures_getByKeyWithFallback(rb.getAlias(), cc, rb.getAlias(), &localStatus); |
555 | 246k | if(U_SUCCESS(localStatus) && ures_getSize(rb.getAlias())>2) { // the length is 3 if more data is present |
556 | 2.47k | ures_getByIndex(rb.getAlias(), 2, rb.getAlias(), &localStatus); |
557 | 2.47k | int32_t currPatternLen = 0; |
558 | 2.47k | currPattern = |
559 | 2.47k | ures_getStringByIndex(rb.getAlias(), static_cast<int32_t>(0), &currPatternLen, &localStatus); |
560 | 2.47k | UnicodeString decimalSep = |
561 | 2.47k | ures_getUnicodeStringByIndex(rb.getAlias(), static_cast<int32_t>(1), &localStatus); |
562 | 2.47k | UnicodeString groupingSep = |
563 | 2.47k | ures_getUnicodeStringByIndex(rb.getAlias(), static_cast<int32_t>(2), &localStatus); |
564 | 2.47k | if(U_SUCCESS(localStatus)){ |
565 | 2.47k | fSymbols[kMonetaryGroupingSeparatorSymbol] = groupingSep; |
566 | 2.47k | fSymbols[kMonetarySeparatorSymbol] = decimalSep; |
567 | | //pattern.setTo(true, currPattern, currPatternLen); |
568 | 2.47k | } |
569 | 2.47k | } |
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 | 246k | } |
573 | | |
574 | | Locale |
575 | 0 | DecimalFormatSymbols::getLocale(ULocDataLocaleType type, UErrorCode& status) const { |
576 | 0 | return LocaleBased::getLocale(validLocale, actualLocale, type, status); |
577 | 0 | } |
578 | | |
579 | | const UnicodeString& |
580 | | DecimalFormatSymbols::getPatternForCurrencySpacing(UCurrencySpacing type, |
581 | | UBool beforeCurrency, |
582 | 1.85M | UErrorCode& status) const { |
583 | 1.85M | if (U_FAILURE(status)) { |
584 | 0 | return fNoSymbol; // always empty. |
585 | 0 | } |
586 | 1.85M | if (beforeCurrency) { |
587 | 925k | return currencySpcBeforeSym[static_cast<int32_t>(type)]; |
588 | 925k | } else { |
589 | 925k | return currencySpcAfterSym[static_cast<int32_t>(type)]; |
590 | 925k | } |
591 | 1.85M | } |
592 | | |
593 | | void |
594 | | DecimalFormatSymbols::setPatternForCurrencySpacing(UCurrencySpacing type, |
595 | | UBool beforeCurrency, |
596 | 1.73M | const UnicodeString& pattern) { |
597 | 1.73M | if (beforeCurrency) { |
598 | 869k | currencySpcBeforeSym[static_cast<int32_t>(type)] = pattern; |
599 | 869k | } else { |
600 | 869k | currencySpcAfterSym[static_cast<int32_t>(type)] = pattern; |
601 | 869k | } |
602 | 1.73M | } |
603 | | U_NAMESPACE_END |
604 | | |
605 | | #endif /* #if !UCONFIG_NO_FORMATTING */ |
606 | | |
607 | | //eof |