]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/dcfmtsym.cpp
ICU-551.24.tar.gz
[apple/icu.git] / icuSources / i18n / dcfmtsym.cpp
CommitLineData
b75a7d8f
A
1/*
2*******************************************************************************
b331163b 3* Copyright (C) 1997-2014, International Business Machines Corporation and
57a6839d 4* others. All Rights Reserved.
b75a7d8f
A
5*******************************************************************************
6*
7* File DCFMTSYM.CPP
8*
9* Modification History:
10*
11* Date Name Description
12* 02/19/97 aliu Converted from java.
13* 03/18/97 clhuang Implemented with C++ APIs.
14* 03/27/97 helena Updated to pass the simple test after code review.
15* 08/26/97 aliu Added currency/intl currency symbol support.
374ca955 16* 07/20/98 stephen Slightly modified initialization of monetarySeparator
b75a7d8f
A
17********************************************************************************
18*/
729e4ab9 19
b75a7d8f
A
20#include "unicode/utypes.h"
21
22#if !UCONFIG_NO_FORMATTING
23
24#include "unicode/dcfmtsym.h"
374ca955 25#include "unicode/ures.h"
b75a7d8f
A
26#include "unicode/decimfmt.h"
27#include "unicode/ucurr.h"
28#include "unicode/choicfmt.h"
729e4ab9
A
29#include "unicode/unistr.h"
30#include "unicode/numsys.h"
4388f060 31#include "unicode/unum.h"
57a6839d 32#include "unicode/utf16.h"
374ca955
A
33#include "ucurrimp.h"
34#include "cstring.h"
35#include "locbased.h"
73c04bcf 36#include "uresimp.h"
729e4ab9
A
37#include "ureslocs.h"
38
b75a7d8f
A
39// *****************************************************************************
40// class DecimalFormatSymbols
41// *****************************************************************************
729e4ab9 42
b75a7d8f
A
43U_NAMESPACE_BEGIN
44
374ca955 45UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DecimalFormatSymbols)
b75a7d8f 46
374ca955 47static const char gNumberElements[] = "NumberElements";
729e4ab9
A
48static const char gCurrencySpacingTag[] = "currencySpacing";
49static const char gBeforeCurrencyTag[] = "beforeCurrency";
50static const char gAfterCurrencyTag[] = "afterCurrency";
51static const char gCurrencyMatchTag[] = "currencyMatch";
52static const char gCurrencySudMatchTag[] = "surroundingMatch";
53static const char gCurrencyInsertBtnTag[] = "insertBetween";
54
b75a7d8f
A
55
56static const UChar INTL_CURRENCY_SYMBOL_STR[] = {0xa4, 0xa4, 0};
57
58// -------------------------------------
59// Initializes this with the decimal format symbols in the default locale.
729e4ab9 60
b75a7d8f 61DecimalFormatSymbols::DecimalFormatSymbols(UErrorCode& status)
374ca955
A
62 : UObject(),
63 locale()
b75a7d8f 64{
374ca955 65 initialize(locale, status, TRUE);
b75a7d8f 66}
729e4ab9 67
b75a7d8f
A
68// -------------------------------------
69// Initializes this with the decimal format symbols in the desired locale.
729e4ab9 70
b75a7d8f 71DecimalFormatSymbols::DecimalFormatSymbols(const Locale& loc, UErrorCode& status)
374ca955
A
72 : UObject(),
73 locale(loc)
b75a7d8f 74{
374ca955 75 initialize(locale, status);
b75a7d8f 76}
729e4ab9 77
57a6839d
A
78DecimalFormatSymbols::DecimalFormatSymbols()
79 : UObject(),
80 locale(Locale::getRoot()),
81 currPattern(NULL) {
82 *validLocale = *actualLocale = 0;
83 initialize();
84}
85
86DecimalFormatSymbols*
87DecimalFormatSymbols::createWithLastResortData(UErrorCode& status) {
88 if (U_FAILURE(status)) { return NULL; }
89 DecimalFormatSymbols* sym = new DecimalFormatSymbols();
90 if (sym == NULL) {
91 status = U_MEMORY_ALLOCATION_ERROR;
92 }
93 return sym;
94}
95
b75a7d8f
A
96// -------------------------------------
97
98DecimalFormatSymbols::~DecimalFormatSymbols()
99{
100}
101
102// -------------------------------------
103// copy constructor
104
105DecimalFormatSymbols::DecimalFormatSymbols(const DecimalFormatSymbols &source)
106 : UObject(source)
107{
374ca955 108 *this = source;
b75a7d8f
A
109}
110
111// -------------------------------------
112// assignment operator
113
114DecimalFormatSymbols&
115DecimalFormatSymbols::operator=(const DecimalFormatSymbols& rhs)
116{
374ca955
A
117 if (this != &rhs) {
118 for(int32_t i = 0; i < (int32_t)kFormatSymbolCount; ++i) {
b75a7d8f
A
119 // fastCopyFrom is safe, see docs on fSymbols
120 fSymbols[(ENumberFormatSymbol)i].fastCopyFrom(rhs.fSymbols[(ENumberFormatSymbol)i]);
121 }
4388f060 122 for(int32_t i = 0; i < (int32_t)UNUM_CURRENCY_SPACING_COUNT; ++i) {
729e4ab9
A
123 currencySpcBeforeSym[i].fastCopyFrom(rhs.currencySpcBeforeSym[i]);
124 currencySpcAfterSym[i].fastCopyFrom(rhs.currencySpcAfterSym[i]);
125 }
374ca955
A
126 locale = rhs.locale;
127 uprv_strcpy(validLocale, rhs.validLocale);
128 uprv_strcpy(actualLocale, rhs.actualLocale);
b75a7d8f
A
129 }
130 return *this;
131}
132
133// -------------------------------------
134
135UBool
136DecimalFormatSymbols::operator==(const DecimalFormatSymbols& that) const
137{
138 if (this == &that) {
139 return TRUE;
140 }
374ca955 141 for(int32_t i = 0; i < (int32_t)kFormatSymbolCount; ++i) {
b75a7d8f
A
142 if(fSymbols[(ENumberFormatSymbol)i] != that.fSymbols[(ENumberFormatSymbol)i]) {
143 return FALSE;
144 }
145 }
4388f060 146 for(int32_t i = 0; i < (int32_t)UNUM_CURRENCY_SPACING_COUNT; ++i) {
729e4ab9
A
147 if(currencySpcBeforeSym[i] != that.currencySpcBeforeSym[i]) {
148 return FALSE;
149 }
150 if(currencySpcAfterSym[i] != that.currencySpcAfterSym[i]) {
151 return FALSE;
152 }
153 }
374ca955
A
154 return locale == that.locale &&
155 uprv_strcmp(validLocale, that.validLocale) == 0 &&
156 uprv_strcmp(actualLocale, that.actualLocale) == 0;
b75a7d8f 157}
729e4ab9 158
b75a7d8f 159// -------------------------------------
729e4ab9 160
b75a7d8f 161void
729e4ab9 162DecimalFormatSymbols::initialize(const Locale& loc, UErrorCode& status, UBool useLastResortData)
b75a7d8f 163{
729e4ab9
A
164 static const char *gNumberElementKeys[kFormatSymbolCount] = {
165 "decimal",
166 "group",
167 "list",
168 "percentSign",
169 NULL, /* Native zero digit is deprecated from CLDR - get it from the numbering system */
170 NULL, /* Pattern digit character is deprecated from CLDR - use # by default always */
171 "minusSign",
172 "plusSign",
173 NULL, /* currency symbol - We don't really try to load this directly from CLDR until we know the currency */
174 NULL, /* intl currency symbol - We don't really try to load this directly from CLDR until we know the currency */
175 "currencyDecimal",
176 "exponential",
177 "perMille",
178 NULL, /* Escape padding character - not in CLDR */
179 "infinity",
180 "nan",
181 NULL, /* Significant digit symbol - not in CLDR */
182 "currencyGroup",
183 NULL, /* one digit - get it from the numbering system */
184 NULL, /* two digit - get it from the numbering system */
185 NULL, /* three digit - get it from the numbering system */
186 NULL, /* four digit - get it from the numbering system */
187 NULL, /* five digit - get it from the numbering system */
188 NULL, /* six digit - get it from the numbering system */
189 NULL, /* seven digit - get it from the numbering system */
190 NULL, /* eight digit - get it from the numbering system */
191 NULL, /* nine digit - get it from the numbering system */
b331163b 192 "superscriptingExponent", /* Multiplication (x) symbol for exponents */
729e4ab9
A
193 };
194
195 static const char *gLatn = "latn";
196 static const char *gSymbols = "symbols";
197 const char *nsName;
198 const UChar *sym = NULL;
199 int32_t len = 0;
200
374ca955 201 *validLocale = *actualLocale = 0;
73c04bcf 202 currPattern = NULL;
374ca955
A
203 if (U_FAILURE(status))
204 return;
729e4ab9 205
374ca955 206 const char* locStr = loc.getName();
57a6839d
A
207 LocalUResourceBundlePointer resource(ures_open(NULL, locStr, &status));
208 LocalUResourceBundlePointer numberElementsRes(
209 ures_getByKeyWithFallback(resource.getAlias(), gNumberElements, NULL, &status));
729e4ab9
A
210
211 if (U_FAILURE(status)) {
212 if ( useLastResortData ) {
4388f060 213 status = U_USING_DEFAULT_WARNING;
b75a7d8f
A
214 initialize();
215 }
729e4ab9 216 return;
57a6839d 217 }
729e4ab9 218
57a6839d
A
219 // First initialize all the symbols to the fallbacks for anything we can't find
220 initialize();
221
222 //
223 // Next get the numbering system for this locale and set zero digit
224 // and the digit string based on the numbering system for the locale
225 //
226
227 LocalPointer<NumberingSystem> ns(NumberingSystem::createInstance(loc, status));
228 if (U_SUCCESS(status) && ns->getRadix() == 10 && !ns->isAlgorithmic()) {
229 nsName = ns->getName();
230 UnicodeString digitString(ns->getDescription());
231 int32_t digitIndex = 0;
232 UChar32 digit = digitString.char32At(0);
233 fSymbols[kZeroDigitSymbol].setTo(digit);
234 for (int32_t i = kOneDigitSymbol; i <= kNineDigitSymbol; ++i) {
235 digitIndex += U16_LENGTH(digit);
236 digit = digitString.char32At(digitIndex);
237 fSymbols[i].setTo(digit);
374ca955 238 }
57a6839d
A
239 } else {
240 nsName = gLatn;
241 }
b75a7d8f 242
57a6839d
A
243 UBool isLatn = !uprv_strcmp(nsName,gLatn);
244
245 UErrorCode nlStatus = U_ZERO_ERROR;
246 LocalUResourceBundlePointer nonLatnSymbols;
247 if ( !isLatn ) {
248 nonLatnSymbols.adoptInstead(
249 ures_getByKeyWithFallback(numberElementsRes.getAlias(), nsName, NULL, &nlStatus));
250 ures_getByKeyWithFallback(nonLatnSymbols.getAlias(), gSymbols, nonLatnSymbols.getAlias(), &nlStatus);
251 }
729e4ab9 252
57a6839d
A
253 LocalUResourceBundlePointer latnSymbols(
254 ures_getByKeyWithFallback(numberElementsRes.getAlias(), gLatn, NULL, &status));
255 ures_getByKeyWithFallback(latnSymbols.getAlias(), gSymbols, latnSymbols.getAlias(), &status);
256
257 UBool kMonetaryDecimalSet = FALSE;
258 UBool kMonetaryGroupingSet = FALSE;
259 for(int32_t i = 0; i<kFormatSymbolCount; i++) {
260 if ( gNumberElementKeys[i] != NULL ) {
261 UErrorCode localStatus = U_ZERO_ERROR;
262 if ( !isLatn ) {
263 sym = ures_getStringByKeyWithFallback(nonLatnSymbols.getAlias(),
264 gNumberElementKeys[i], &len, &localStatus);
265 // If we can't find the symbol in the numbering system specific resources,
266 // use the "latn" numbering system as the fallback.
267 if ( U_FAILURE(localStatus) ) {
268 localStatus = U_ZERO_ERROR;
269 sym = ures_getStringByKeyWithFallback(latnSymbols.getAlias(),
270 gNumberElementKeys[i], &len, &localStatus);
374ca955 271 }
57a6839d
A
272 } else {
273 sym = ures_getStringByKeyWithFallback(latnSymbols.getAlias(),
274 gNumberElementKeys[i], &len, &localStatus);
b75a7d8f 275 }
729e4ab9 276
57a6839d
A
277 if ( U_SUCCESS(localStatus) ) {
278 setSymbol((ENumberFormatSymbol)i, UnicodeString(TRUE, sym, len));
279 if ( i == kMonetarySeparatorSymbol ) {
280 kMonetaryDecimalSet = TRUE;
281 } else if ( i == kMonetaryGroupingSeparatorSymbol ) {
282 kMonetaryGroupingSet = TRUE;
283 }
284 }
729e4ab9 285 }
57a6839d 286 }
729e4ab9 287
57a6839d
A
288 // If monetary decimal or grouping were not explicitly set, then set them to be the
289 // same as their non-monetary counterparts.
374ca955 290
57a6839d
A
291 if ( !kMonetaryDecimalSet ) {
292 setSymbol(kMonetarySeparatorSymbol,fSymbols[kDecimalSeparatorSymbol]);
293 }
294 if ( !kMonetaryGroupingSet ) {
295 setSymbol(kMonetaryGroupingSeparatorSymbol,fSymbols[kGroupingSeparatorSymbol]);
296 }
729e4ab9 297
57a6839d
A
298 // Obtain currency data from the currency API. This is strictly
299 // for backward compatibility; we don't use DecimalFormatSymbols
300 // for currency data anymore.
301 UErrorCode internalStatus = U_ZERO_ERROR; // don't propagate failures out
302 UChar curriso[4];
303 UnicodeString tempStr;
304 ucurr_forLocale(locStr, curriso, 4, &internalStatus);
305
306 uprv_getStaticCurrencyName(curriso, locStr, tempStr, internalStatus);
307 if (U_SUCCESS(internalStatus)) {
308 fSymbols[kIntlCurrencySymbol].setTo(curriso, -1);
309 fSymbols[kCurrencySymbol] = tempStr;
310 }
311 /* else use the default values. */
312
313 U_LOCALE_BASED(locBased, *this);
314 locBased.setLocaleIDs(ures_getLocaleByType(numberElementsRes.getAlias(),
315 ULOC_VALID_LOCALE, &status),
316 ures_getLocaleByType(numberElementsRes.getAlias(),
317 ULOC_ACTUAL_LOCALE, &status));
318
319 //load the currency data
320 UChar ucc[4]={0}; //Currency Codes are always 3 chars long
321 int32_t uccLen = 4;
322 const char* locName = loc.getName();
323 UErrorCode localStatus = U_ZERO_ERROR;
324 uccLen = ucurr_forLocale(locName, ucc, uccLen, &localStatus);
325
326 if(U_SUCCESS(localStatus) && uccLen > 0) {
327 char cc[4]={0};
328 u_UCharsToChars(ucc, cc, uccLen);
329 /* An explicit currency was requested */
330 LocalUResourceBundlePointer currencyResource(ures_open(U_ICUDATA_CURR, locStr, &localStatus));
331 LocalUResourceBundlePointer currency(
332 ures_getByKeyWithFallback(currencyResource.getAlias(), "Currencies", NULL, &localStatus));
333 ures_getByKeyWithFallback(currency.getAlias(), cc, currency.getAlias(), &localStatus);
334 if(U_SUCCESS(localStatus) && ures_getSize(currency.getAlias())>2) { // the length is 3 if more data is present
335 ures_getByIndex(currency.getAlias(), 2, currency.getAlias(), &localStatus);
336 int32_t currPatternLen = 0;
337 currPattern =
338 ures_getStringByIndex(currency.getAlias(), (int32_t)0, &currPatternLen, &localStatus);
339 UnicodeString decimalSep =
340 ures_getUnicodeStringByIndex(currency.getAlias(), (int32_t)1, &localStatus);
341 UnicodeString groupingSep =
342 ures_getUnicodeStringByIndex(currency.getAlias(), (int32_t)2, &localStatus);
343 if(U_SUCCESS(localStatus)){
344 fSymbols[kMonetaryGroupingSeparatorSymbol] = groupingSep;
345 fSymbols[kMonetarySeparatorSymbol] = decimalSep;
346 //pattern.setTo(TRUE, currPattern, currPatternLen);
347 status = localStatus;
73c04bcf 348 }
73c04bcf 349 }
57a6839d
A
350 /* else An explicit currency was requested and is unknown or locale data is malformed. */
351 /* ucurr_* API will get the correct value later on. */
352 }
353 // else ignore the error if no currency
354
355 // Currency Spacing.
356 localStatus = U_ZERO_ERROR;
357 LocalUResourceBundlePointer currencyResource(ures_open(U_ICUDATA_CURR, locStr, &localStatus));
358 LocalUResourceBundlePointer currencySpcRes(
359 ures_getByKeyWithFallback(currencyResource.getAlias(),
360 gCurrencySpacingTag, NULL, &localStatus));
361
362 if (localStatus == U_USING_FALLBACK_WARNING || U_SUCCESS(localStatus)) {
363 const char* keywords[UNUM_CURRENCY_SPACING_COUNT] = {
364 gCurrencyMatchTag, gCurrencySudMatchTag, gCurrencyInsertBtnTag
365 };
729e4ab9 366 localStatus = U_ZERO_ERROR;
57a6839d
A
367 LocalUResourceBundlePointer dataRes(
368 ures_getByKeyWithFallback(currencySpcRes.getAlias(),
369 gBeforeCurrencyTag, NULL, &localStatus));
729e4ab9 370 if (localStatus == U_USING_FALLBACK_WARNING || U_SUCCESS(localStatus)) {
729e4ab9 371 localStatus = U_ZERO_ERROR;
57a6839d
A
372 for (int32_t i = 0; i < UNUM_CURRENCY_SPACING_COUNT; i++) {
373 currencySpcBeforeSym[i] =
374 ures_getUnicodeStringByKey(dataRes.getAlias(), keywords[i], &localStatus);
729e4ab9 375 }
57a6839d
A
376 }
377 dataRes.adoptInstead(
378 ures_getByKeyWithFallback(currencySpcRes.getAlias(),
379 gAfterCurrencyTag, NULL, &localStatus));
380 if (localStatus == U_USING_FALLBACK_WARNING || U_SUCCESS(localStatus)) {
381 localStatus = U_ZERO_ERROR;
382 for (int32_t i = 0; i < UNUM_CURRENCY_SPACING_COUNT; i++) {
383 currencySpcAfterSym[i] =
384 ures_getUnicodeStringByKey(dataRes.getAlias(), keywords[i], &localStatus);
729e4ab9 385 }
374ca955
A
386 }
387 }
b75a7d8f
A
388}
389
b75a7d8f
A
390void
391DecimalFormatSymbols::initialize() {
392 /*
393 * These strings used to be in static arrays, but the HP/UX aCC compiler
394 * cannot initialize a static array with class constructors.
395 * markus 2000may25
396 */
397 fSymbols[kDecimalSeparatorSymbol] = (UChar)0x2e; // '.' decimal separator
398 fSymbols[kGroupingSeparatorSymbol].remove(); // group (thousands) separator
399 fSymbols[kPatternSeparatorSymbol] = (UChar)0x3b; // ';' pattern separator
400 fSymbols[kPercentSymbol] = (UChar)0x25; // '%' percent sign
401 fSymbols[kZeroDigitSymbol] = (UChar)0x30; // '0' native 0 digit
729e4ab9
A
402 fSymbols[kOneDigitSymbol] = (UChar)0x31; // '1' native 1 digit
403 fSymbols[kTwoDigitSymbol] = (UChar)0x32; // '2' native 2 digit
404 fSymbols[kThreeDigitSymbol] = (UChar)0x33; // '3' native 3 digit
405 fSymbols[kFourDigitSymbol] = (UChar)0x34; // '4' native 4 digit
406 fSymbols[kFiveDigitSymbol] = (UChar)0x35; // '5' native 5 digit
407 fSymbols[kSixDigitSymbol] = (UChar)0x36; // '6' native 6 digit
408 fSymbols[kSevenDigitSymbol] = (UChar)0x37; // '7' native 7 digit
409 fSymbols[kEightDigitSymbol] = (UChar)0x38; // '8' native 8 digit
410 fSymbols[kNineDigitSymbol] = (UChar)0x39; // '9' native 9 digit
b75a7d8f 411 fSymbols[kDigitSymbol] = (UChar)0x23; // '#' pattern digit
b75a7d8f 412 fSymbols[kPlusSignSymbol] = (UChar)0x002b; // '+' plus sign
374ca955 413 fSymbols[kMinusSignSymbol] = (UChar)0x2d; // '-' minus sign
b75a7d8f 414 fSymbols[kCurrencySymbol] = (UChar)0xa4; // 'OX' currency symbol
4388f060 415 fSymbols[kIntlCurrencySymbol].setTo(TRUE, INTL_CURRENCY_SYMBOL_STR, 2);
b75a7d8f
A
416 fSymbols[kMonetarySeparatorSymbol] = (UChar)0x2e; // '.' monetary decimal separator
417 fSymbols[kExponentialSymbol] = (UChar)0x45; // 'E' exponential
418 fSymbols[kPerMillSymbol] = (UChar)0x2030; // '%o' per mill
419 fSymbols[kPadEscapeSymbol] = (UChar)0x2a; // '*' pad escape symbol
420 fSymbols[kInfinitySymbol] = (UChar)0x221e; // 'oo' infinite
421 fSymbols[kNaNSymbol] = (UChar)0xfffd; // SUB NaN
374ca955 422 fSymbols[kSignificantDigitSymbol] = (UChar)0x0040; // '@' significant digit
729e4ab9 423 fSymbols[kMonetaryGroupingSeparatorSymbol].remove(); //
b331163b 424 fSymbols[kExponentMultiplicationSymbol] = (UChar)0xd7; // 'x' multiplication symbol for exponents
374ca955
A
425}
426
729e4ab9 427Locale
374ca955
A
428DecimalFormatSymbols::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
429 U_LOCALE_BASED(locBased, *this);
430 return locBased.getLocale(type, status);
b75a7d8f
A
431}
432
729e4ab9 433const UnicodeString&
4388f060 434DecimalFormatSymbols::getPatternForCurrencySpacing(UCurrencySpacing type,
729e4ab9
A
435 UBool beforeCurrency,
436 UErrorCode& status) const {
437 if (U_FAILURE(status)) {
438 return fNoSymbol; // always empty.
439 }
440 if (beforeCurrency) {
441 return currencySpcBeforeSym[(int32_t)type];
442 } else {
443 return currencySpcAfterSym[(int32_t)type];
444 }
445}
446
447void
4388f060 448DecimalFormatSymbols::setPatternForCurrencySpacing(UCurrencySpacing type,
729e4ab9
A
449 UBool beforeCurrency,
450 const UnicodeString& pattern) {
451 if (beforeCurrency) {
452 currencySpcBeforeSym[(int32_t)type] = pattern;
453 } else {
454 currencySpcAfterSym[(int32_t)type] = pattern;
455 }
456}
b75a7d8f
A
457U_NAMESPACE_END
458
459#endif /* #if !UCONFIG_NO_FORMATTING */
460
461//eof