Debian Codegraph @

intl/icu/source/i18n/unicode/dcfmtsym.h

raw
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
6 * Corporation and others. All Rights Reserved.
7 ********************************************************************************
8 *
9 * File DCFMTSYM.H
10 *
11 * Modification History:
12 *
13 * Date Name Description
14 * 02/19/97 aliu Converted from java.
15 * 03/18/97 clhuang Updated per C++ implementation.
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/22/98 stephen Changed to match C++ style
19 * currencySymbol -> fCurrencySymbol
20 * Constants changed from CAPS to kCaps
21 * 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes
22 * 09/22/00 grhoten Marked deprecation tags with a pointer to replacement
23 * functions.
24 ********************************************************************************
25 */
26
27 #ifndef DCFMTSYM_H
28 #define DCFMTSYM_H
29
30 #include "unicode/utypes.h"
31
32 #if U_SHOW_CPLUSPLUS_API
33
34 #if !UCONFIG_NO_FORMATTING
35
36 #include "unicode/uchar.h"
37 #include "unicode/uobject.h"
38 #include "unicode/locid.h"
39 #include "unicode/numsys.h"
40 #include "unicode/unum.h"
41 #include "unicode/unistr.h"
42
43 /**
44 * \file
45 * \brief C++ API: Symbols for formatting numbers.
46 */
47
48
49 U_NAMESPACE_BEGIN
50
51 class CharString;
52 /**
53 * This class represents the set of symbols needed by DecimalFormat
54 * to format numbers. DecimalFormat creates for itself an instance of
55 * DecimalFormatSymbols from its locale data. If you need to change any
56 * of these symbols, you can get the DecimalFormatSymbols object from
57 * your DecimalFormat and modify it.
58 * <P>
59 * Here are the special characters used in the parts of the
60 * subpattern, with notes on their usage.
61 * <pre>
62 * \code
63 * Symbol Meaning
64 * 0 a digit
65 * # a digit, zero shows as absent
66 * . placeholder for decimal separator
67 * , placeholder for grouping separator.
68 * ; separates formats.
69 * - default negative prefix.
70 * % divide by 100 and show as percentage
71 * X any other characters can be used in the prefix or suffix
72 * ' used to quote special characters in a prefix or suffix.
73 * \endcode
74 * </pre>
75 * [Notes]
76 * <P>
77 * If there is no explicit negative subpattern, - is prefixed to the
78 * positive form. That is, "0.00" alone is equivalent to "0.00;-0.00".
79 * <P>
80 * The grouping separator is commonly used for thousands, but in some
81 * countries for ten-thousands. The interval is a constant number of
82 * digits between the grouping characters, such as 100,000,000 or 1,0000,0000.
83 * If you supply a pattern with multiple grouping characters, the interval
84 * between the last one and the end of the integer is the one that is
85 * used. So "#,##,###,####" == "######,####" == "##,####,####".
86 */
87 class U_I18N_API DecimalFormatSymbols : public UObject {
88 public:
89 /**
90 * Constants for specifying a number format symbol.
91 * @stable ICU 2.0
92 */
93 enum ENumberFormatSymbol {
94 /** The decimal separator */
95 kDecimalSeparatorSymbol,
96 /** The grouping separator */
97 kGroupingSeparatorSymbol,
98 /** The pattern separator */
99 kPatternSeparatorSymbol,
100 /** The percent sign */
101 kPercentSymbol,
102 /** Zero*/
103 kZeroDigitSymbol,
104 /** Character representing a digit in the pattern */
105 kDigitSymbol,
106 /** The minus sign */
107 kMinusSignSymbol,
108 /** The plus sign */
109 kPlusSignSymbol,
110 /** The currency symbol */
111 kCurrencySymbol,
112 /** The international currency symbol */
113 kIntlCurrencySymbol,
114 /** The monetary separator */
115 kMonetarySeparatorSymbol,
116 /** The exponential symbol */
117 kExponentialSymbol,
118 /** Per mill symbol - replaces kPermillSymbol */
119 kPerMillSymbol,
120 /** Escape padding character */
121 kPadEscapeSymbol,
122 /** Infinity symbol */
123 kInfinitySymbol,
124 /** Nan symbol */
125 kNaNSymbol,
126 /** Significant digit symbol
127 * @stable ICU 3.0 */
128 kSignificantDigitSymbol,
129 /** The monetary grouping separator
130 * @stable ICU 3.6
131 */
132 kMonetaryGroupingSeparatorSymbol,
133 /** One
134 * @stable ICU 4.6
135 */
136 kOneDigitSymbol,
137 /** Two
138 * @stable ICU 4.6
139 */
140 kTwoDigitSymbol,
141 /** Three
142 * @stable ICU 4.6
143 */
144 kThreeDigitSymbol,
145 /** Four
146 * @stable ICU 4.6
147 */
148 kFourDigitSymbol,
149 /** Five
150 * @stable ICU 4.6
151 */
152 kFiveDigitSymbol,
153 /** Six
154 * @stable ICU 4.6
155 */
156 kSixDigitSymbol,
157 /** Seven
158 * @stable ICU 4.6
159 */
160 kSevenDigitSymbol,
161 /** Eight
162 * @stable ICU 4.6
163 */
164 kEightDigitSymbol,
165 /** Nine
166 * @stable ICU 4.6
167 */
168 kNineDigitSymbol,
169 /** Multiplication sign.
170 * @stable ICU 54
171 */
172 kExponentMultiplicationSymbol,
173 #ifndef U_HIDE_INTERNAL_API
174 /** Approximately sign.
175 * @internal
176 */
177 kApproximatelySignSymbol,
178 #endif /* U_HIDE_INTERNAL_API */
179 /** count symbol constants */
180 kFormatSymbolCount = kExponentMultiplicationSymbol + 2
181 };
182
183 /**
184 * Create a DecimalFormatSymbols object for the given locale.
185 *
186 * @param locale The locale to get symbols for.
187 * @param status Input/output parameter, set to success or
188 * failure code upon return.
189 * @stable ICU 2.0
190 */
191 DecimalFormatSymbols(const Locale& locale, UErrorCode& status);
192
193 /**
194 * Creates a DecimalFormatSymbols instance for the given locale with digits and symbols
195 * corresponding to the given NumberingSystem.
196 *
197 * This constructor behaves equivalently to the normal constructor called with a locale having a
198 * "numbers=xxxx" keyword specifying the numbering system by name.
199 *
200 * In this constructor, the NumberingSystem argument will be used even if the locale has its own
201 * "numbers=xxxx" keyword.
202 *
203 * @param locale The locale to get symbols for.
204 * @param ns The numbering system.
205 * @param status Input/output parameter, set to success or
206 * failure code upon return.
207 * @stable ICU 60
208 */
209 DecimalFormatSymbols(const Locale& locale, const NumberingSystem& ns, UErrorCode& status);
210
211 /**
212 * Create a DecimalFormatSymbols object for the default locale.
213 * This constructor will not fail. If the resource file data is
214 * not available, it will use hard-coded last-resort data and
215 * set status to U_USING_FALLBACK_ERROR.
216 *
217 * @param status Input/output parameter, set to success or
218 * failure code upon return.
219 * @stable ICU 2.0
220 */
221 DecimalFormatSymbols(UErrorCode& status);
222
223 /**
224 * Creates a DecimalFormatSymbols object with last-resort data.
225 * Intended for callers who cache the symbols data and
226 * set all symbols on the resulting object.
227 *
228 * The last-resort symbols are similar to those for the root data,
229 * except that the grouping separators are empty,
230 * the NaN symbol is U+FFFD rather than "NaN",
231 * and the CurrencySpacing patterns are empty.
232 *
233 * @param status Input/output parameter, set to success or
234 * failure code upon return.
235 * @return last-resort symbols
236 * @stable ICU 52
237 */
238 static DecimalFormatSymbols* createWithLastResortData(UErrorCode& status);
239
240 /**
241 * Copy constructor.
242 * @stable ICU 2.0
243 */
244 DecimalFormatSymbols(const DecimalFormatSymbols&);
245
246 /**
247 * Assignment operator.
248 * @stable ICU 2.0
249 */
250 DecimalFormatSymbols& operator=(const DecimalFormatSymbols&);
251
252 /**
253 * Destructor.
254 * @stable ICU 2.0
255 */
256 virtual ~DecimalFormatSymbols();
257
258 /**
259 * Return true if another object is semantically equal to this one.
260 *
261 * @param other the object to be compared with.
262 * @return true if another object is semantically equal to this one.
263 * @stable ICU 2.0
264 */
265 bool operator==(const DecimalFormatSymbols& other) const;
266
267 /**
268 * Return true if another object is semantically unequal to this one.
269 *
270 * @param other the object to be compared with.
271 * @return true if another object is semantically unequal to this one.
272 * @stable ICU 2.0
273 */
274 bool operator!=(const DecimalFormatSymbols& other) const { return !operator==(other); }
275
276 /**
277 * Get one of the format symbols by its enum constant.
278 * Each symbol is stored as a string so that graphemes
279 * (characters with modifier letters) can be used.
280 *
281 * @param symbol Constant to indicate a number format symbol.
282 * @return the format symbols by the param 'symbol'
283 * @stable ICU 2.0
284 */
285 inline UnicodeString getSymbol(ENumberFormatSymbol symbol) const;
286
287 /**
288 * Set one of the format symbols by its enum constant.
289 * Each symbol is stored as a string so that graphemes
290 * (characters with modifier letters) can be used.
291 *
292 * @param symbol Constant to indicate a number format symbol.
293 * @param value value of the format symbol
294 * @param propagateDigits If false, setting the zero digit will not automatically set 1-9.
295 * The default behavior is to automatically set 1-9 if zero is being set and the value
296 * it is being set to corresponds to a known Unicode zero digit.
297 * @stable ICU 2.0
298 */
299 void setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propagateDigits);
300
301 #ifndef U_HIDE_INTERNAL_API
302 /**
303 * Loads symbols for the specified currency into this instance.
304 *
305 * This method is internal. If you think it should be public, file a ticket.
306 *
307 * @internal
308 */
309 void setCurrency(const char16_t* currency, UErrorCode& status);
310 #endif // U_HIDE_INTERNAL_API
311
312 /**
313 * Returns the locale for which this object was constructed.
314 * @stable ICU 2.6
315 */
316 inline Locale getLocale() const;
317
318 /**
319 * Returns the locale for this object. Two flavors are available:
320 * valid and actual locale.
321 * @stable ICU 2.8
322 */
323 Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
324
325 /**
326 * Get pattern string for 'CurrencySpacing' that can be applied to
327 * currency format.
328 * This API gets the CurrencySpacing data from ResourceBundle. The pattern can
329 * be empty if there is no data from current locale and its parent locales.
330 *
331 * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
332 * @param beforeCurrency : true if the pattern is for before currency symbol.
333 * false if the pattern is for after currency symbol.
334 * @param status: Input/output parameter, set to success or
335 * failure code upon return.
336 * @return pattern string for currencyMatch, surroundingMatch or spaceInsert.
337 * Return empty string if there is no data for this locale and its parent
338 * locales.
339 * @stable ICU 4.8
340 */
341 const UnicodeString& getPatternForCurrencySpacing(UCurrencySpacing type,
342 UBool beforeCurrency,
343 UErrorCode& status) const;
344 /**
345 * Set pattern string for 'CurrencySpacing' that can be applied to
346 * currency format.
347 *
348 * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
349 * @param beforeCurrency : true if the pattern is for before currency symbol.
350 * false if the pattern is for after currency symbol.
351 * @param pattern : pattern string to override current setting.
352 * @stable ICU 4.8
353 */
354 void setPatternForCurrencySpacing(UCurrencySpacing type,
355 UBool beforeCurrency,
356 const UnicodeString& pattern);
357
358 /**
359 * ICU "poor man's RTTI", returns a UClassID for the actual class.
360 *
361 * @stable ICU 2.2
362 */
363 virtual UClassID getDynamicClassID() const override;
364
365 /**
366 * ICU "poor man's RTTI", returns a UClassID for this class.
367 *
368 * @stable ICU 2.2
369 */
370 static UClassID U_EXPORT2 getStaticClassID();
371
372 private:
373 DecimalFormatSymbols();
374
375 /**
376 * Initializes the symbols from the LocaleElements resource bundle.
377 * Note: The organization of LocaleElements badly needs to be
378 * cleaned up.
379 *
380 * @param locale The locale to get symbols for.
381 * @param success Input/output parameter, set to success or
382 * failure code upon return.
383 * @param useLastResortData determine if use last resort data
384 * @param ns The NumberingSystem to use; otherwise, fall
385 * back to the locale.
386 */
387 void initialize(const Locale& locale, UErrorCode& success,
388 UBool useLastResortData = false, const NumberingSystem* ns = nullptr);
389
390 /**
391 * Initialize the symbols with default values.
392 */
393 void initialize();
394
395 public:
396
397 #ifndef U_HIDE_INTERNAL_API
398 /**
399 * @internal For ICU use only
400 */
401 inline UBool isCustomCurrencySymbol() const {
402 return fIsCustomCurrencySymbol;
403 }
404
405 /**
406 * @internal For ICU use only
407 */
408 inline UBool isCustomIntlCurrencySymbol() const {
409 return fIsCustomIntlCurrencySymbol;
410 }
411
412 /**
413 * @internal For ICU use only
414 */
415 inline UChar32 getCodePointZero() const {
416 return fCodePointZero;
417 }
418 #endif /* U_HIDE_INTERNAL_API */
419
420 /**
421 * _Internal_ function - more efficient version of getSymbol,
422 * returning a const reference to one of the symbol strings.
423 * The returned reference becomes invalid when the symbol is changed
424 * or when the DecimalFormatSymbols are destroyed.
425 * Note: moved \#ifndef U_HIDE_INTERNAL_API after this, since this is needed for inline in DecimalFormat
426 *
427 * This is not currently stable API, but if you think it should be stable,
428 * post a comment on the following ticket and the ICU team will take a look:
429 * https://unicode-org.atlassian.net/browse/ICU-13580
430 *
431 * @param symbol Constant to indicate a number format symbol.
432 * @return the format symbol by the param 'symbol'
433 * @internal
434 */
435 inline const UnicodeString& getConstSymbol(ENumberFormatSymbol symbol) const;
436
437 #ifndef U_HIDE_INTERNAL_API
438 /**
439 * Returns the const UnicodeString reference, like getConstSymbol,
440 * corresponding to the digit with the given value. This is equivalent
441 * to accessing the symbol from getConstSymbol with the corresponding
442 * key, such as kZeroDigitSymbol or kOneDigitSymbol.
443 *
444 * This is not currently stable API, but if you think it should be stable,
445 * post a comment on the following ticket and the ICU team will take a look:
446 * https://unicode-org.atlassian.net/browse/ICU-13580
447 *
448 * @param digit The digit, an integer between 0 and 9 inclusive.
449 * If outside the range 0 to 9, the zero digit is returned.
450 * @return the format symbol for the given digit.
451 * @internal This API is currently for ICU use only.
452 */
453 inline const UnicodeString& getConstDigitSymbol(int32_t digit) const;
454
455 /**
456 * Returns that pattern stored in currency info. Internal API for use by NumberFormat API.
457 * @internal
458 */
459 inline const char16_t* getCurrencyPattern() const;
460
461 /**
462 * Returns the numbering system with which this DecimalFormatSymbols was initialized.
463 * @internal
464 */
465 inline const char* getNumberingSystemName() const;
466 #endif /* U_HIDE_INTERNAL_API */
467
468 private:
469 /**
470 * Private symbol strings.
471 * They are either loaded from a resource bundle or otherwise owned.
472 * setSymbol() clones the symbol string.
473 * Readonly aliases can only come from a resource bundle, so that we can always
474 * use fastCopyFrom() with them.
475 *
476 * If DecimalFormatSymbols becomes subclassable and the status of fSymbols changes
477 * from private to protected,
478 * or when fSymbols can be set any other way that allows them to be readonly aliases
479 * to non-resource bundle strings,
480 * then regular UnicodeString copies must be used instead of fastCopyFrom().
481 *
482 */
483 UnicodeString fSymbols[kFormatSymbolCount];
484
485 /**
486 * Non-symbol variable for getConstSymbol(). Always empty.
487 */
488 UnicodeString fNoSymbol;
489
490 /**
491 * Dealing with code points is faster than dealing with strings when formatting. Because of
492 * this, we maintain a value containing the zero code point that is used whenever digitStrings
493 * represents a sequence of ten code points in order.
494 *
495 * <p>If the value stored here is positive, it means that the code point stored in this value
496 * corresponds to the digitStrings array, and codePointZero can be used instead of the
497 * digitStrings array for the purposes of efficient formatting; if -1, then digitStrings does
498 * *not* contain a sequence of code points, and it must be used directly.
499 *
500 * <p>It is assumed that codePointZero always shadows the value in digitStrings. codePointZero
501 * should never be set directly; rather, it should be updated only when digitStrings mutates.
502 * That is, the flow of information is digitStrings -> codePointZero, not the other way.
503 */
504 UChar32 fCodePointZero;
505
506 Locale locale;
507
508 CharString* actualLocale = nullptr;
509 CharString* validLocale = nullptr;
510 const char16_t* currPattern = nullptr;
511
512 UnicodeString currencySpcBeforeSym[UNUM_CURRENCY_SPACING_COUNT];
513 UnicodeString currencySpcAfterSym[UNUM_CURRENCY_SPACING_COUNT];
514 UBool fIsCustomCurrencySymbol;
515 UBool fIsCustomIntlCurrencySymbol;
516 char nsName[kInternalNumSysNameCapacity+1] = {};
517 };
518
519 // -------------------------------------
520
521 inline UnicodeString
522 DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol) const {
523 const UnicodeString *strPtr;
524 if(symbol < kFormatSymbolCount) {
525 strPtr = &fSymbols[symbol];
526 } else {
527 strPtr = &fNoSymbol;
528 }
529 return *strPtr;
530 }
531
532 // See comments above for this function. Not hidden with #ifdef U_HIDE_INTERNAL_API
533 inline const UnicodeString &
534 DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol) const {
535 const UnicodeString *strPtr;
536 if(symbol < kFormatSymbolCount) {
537 strPtr = &fSymbols[symbol];
538 } else {
539 strPtr = &fNoSymbol;
540 }
541 return *strPtr;
542 }
543
544 #ifndef U_HIDE_INTERNAL_API
545 inline const UnicodeString& DecimalFormatSymbols::getConstDigitSymbol(int32_t digit) const {
546 if (digit < 0 || digit > 9) {
547 digit = 0;
548 }
549 if (digit == 0) {
550 return fSymbols[kZeroDigitSymbol];
551 }
552 ENumberFormatSymbol key = static_cast<ENumberFormatSymbol>(kOneDigitSymbol + digit - 1);
553 return fSymbols[key];
554 }
555 #endif /* U_HIDE_INTERNAL_API */
556
557 // -------------------------------------
558
559 inline void
560 DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propagateDigits = true) {
561 if (symbol == kCurrencySymbol) {
562 fIsCustomCurrencySymbol = true;
563 }
564 else if (symbol == kIntlCurrencySymbol) {
565 fIsCustomIntlCurrencySymbol = true;
566 }
567 if(symbol<kFormatSymbolCount) {
568 fSymbols[symbol]=value;
569 }
570
571 // If the zero digit is being set to a known zero digit according to Unicode,
572 // then we automatically set the corresponding 1-9 digits
573 // Also record updates to fCodePointZero. Be conservative if in doubt.
574 if (symbol == kZeroDigitSymbol) {
575 UChar32 sym = value.char32At(0);
576 if ( propagateDigits && u_charDigitValue(sym) == 0 && value.countChar32() == 1 ) {
577 fCodePointZero = sym;
578 for ( int8_t i = 1 ; i<= 9 ; i++ ) {
579 sym++;
580 fSymbols[static_cast<int>(kOneDigitSymbol) + i - 1] = UnicodeString(sym);
581 }
582 } else {
583 fCodePointZero = -1;
584 }
585 } else if (symbol >= kOneDigitSymbol && symbol <= kNineDigitSymbol) {
586 fCodePointZero = -1;
587 }
588 }
589
590 // -------------------------------------
591
592 inline Locale
593 DecimalFormatSymbols::getLocale() const {
594 return locale;
595 }
596
597 #ifndef U_HIDE_INTERNAL_API
598 inline const char16_t*
599 DecimalFormatSymbols::getCurrencyPattern() const {
600 return currPattern;
601 }
602 inline const char*
603 DecimalFormatSymbols::getNumberingSystemName() const {
604 return nsName;
605 }
606 #endif /* U_HIDE_INTERNAL_API */
607
608 U_NAMESPACE_END
609
610 #endif /* #if !UCONFIG_NO_FORMATTING */
611
612 #endif /* U_SHOW_CPLUSPLUS_API */
613
614 #endif // _DCFMTSYM
615 //eof