dcfmtsym.h 20 KB

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