dcfmtsym.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. #ifndef U_HIDE_INTERNAL_API
  164. /** Approximately sign.
  165. * @internal
  166. */
  167. kApproximatelySignSymbol,
  168. #endif /* U_HIDE_INTERNAL_API */
  169. /** count symbol constants */
  170. kFormatSymbolCount = kExponentMultiplicationSymbol + 2
  171. };
  172. /**
  173. * Create a DecimalFormatSymbols object for the given locale.
  174. *
  175. * @param locale The locale to get symbols for.
  176. * @param status Input/output parameter, set to success or
  177. * failure code upon return.
  178. * @stable ICU 2.0
  179. */
  180. DecimalFormatSymbols(const Locale& locale, UErrorCode& status);
  181. /**
  182. * Creates a DecimalFormatSymbols instance for the given locale with digits and symbols
  183. * corresponding to the given NumberingSystem.
  184. *
  185. * This constructor behaves equivalently to the normal constructor called with a locale having a
  186. * "numbers=xxxx" keyword specifying the numbering system by name.
  187. *
  188. * In this constructor, the NumberingSystem argument will be used even if the locale has its own
  189. * "numbers=xxxx" keyword.
  190. *
  191. * @param locale The locale to get symbols for.
  192. * @param ns The numbering system.
  193. * @param status Input/output parameter, set to success or
  194. * failure code upon return.
  195. * @stable ICU 60
  196. */
  197. DecimalFormatSymbols(const Locale& locale, const NumberingSystem& ns, UErrorCode& status);
  198. /**
  199. * Create a DecimalFormatSymbols object for the default locale.
  200. * This constructor will not fail. If the resource file data is
  201. * not available, it will use hard-coded last-resort data and
  202. * set status to U_USING_FALLBACK_ERROR.
  203. *
  204. * @param status Input/output parameter, set to success or
  205. * failure code upon return.
  206. * @stable ICU 2.0
  207. */
  208. DecimalFormatSymbols(UErrorCode& status);
  209. /**
  210. * Creates a DecimalFormatSymbols object with last-resort data.
  211. * Intended for callers who cache the symbols data and
  212. * set all symbols on the resulting object.
  213. *
  214. * The last-resort symbols are similar to those for the root data,
  215. * except that the grouping separators are empty,
  216. * the NaN symbol is U+FFFD rather than "NaN",
  217. * and the CurrencySpacing patterns are empty.
  218. *
  219. * @param status Input/output parameter, set to success or
  220. * failure code upon return.
  221. * @return last-resort symbols
  222. * @stable ICU 52
  223. */
  224. static DecimalFormatSymbols* createWithLastResortData(UErrorCode& status);
  225. /**
  226. * Copy constructor.
  227. * @stable ICU 2.0
  228. */
  229. DecimalFormatSymbols(const DecimalFormatSymbols&);
  230. /**
  231. * Assignment operator.
  232. * @stable ICU 2.0
  233. */
  234. DecimalFormatSymbols& operator=(const DecimalFormatSymbols&);
  235. /**
  236. * Destructor.
  237. * @stable ICU 2.0
  238. */
  239. virtual ~DecimalFormatSymbols();
  240. /**
  241. * Return true if another object is semantically equal to this one.
  242. *
  243. * @param other the object to be compared with.
  244. * @return true if another object is semantically equal to this one.
  245. * @stable ICU 2.0
  246. */
  247. bool operator==(const DecimalFormatSymbols& other) const;
  248. /**
  249. * Return true if another object is semantically unequal to this one.
  250. *
  251. * @param other the object to be compared with.
  252. * @return true if another object is semantically unequal to this one.
  253. * @stable ICU 2.0
  254. */
  255. bool operator!=(const DecimalFormatSymbols& other) const { return !operator==(other); }
  256. /**
  257. * Get one of the format symbols by its enum constant.
  258. * Each symbol is stored as a string so that graphemes
  259. * (characters with modifier letters) can be used.
  260. *
  261. * @param symbol Constant to indicate a number format symbol.
  262. * @return the format symbols by the param 'symbol'
  263. * @stable ICU 2.0
  264. */
  265. inline UnicodeString getSymbol(ENumberFormatSymbol symbol) const;
  266. /**
  267. * Set one of the format symbols by its enum constant.
  268. * Each symbol is stored as a string so that graphemes
  269. * (characters with modifier letters) can be used.
  270. *
  271. * @param symbol Constant to indicate a number format symbol.
  272. * @param value value of the format symbol
  273. * @param propagateDigits If false, setting the zero digit will not automatically set 1-9.
  274. * The default behavior is to automatically set 1-9 if zero is being set and the value
  275. * it is being set to corresponds to a known Unicode zero digit.
  276. * @stable ICU 2.0
  277. */
  278. void setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propagateDigits);
  279. #ifndef U_HIDE_INTERNAL_API
  280. /**
  281. * Loads symbols for the specified currency into this instance.
  282. *
  283. * This method is internal. If you think it should be public, file a ticket.
  284. *
  285. * @internal
  286. */
  287. void setCurrency(const char16_t* currency, UErrorCode& status);
  288. #endif // U_HIDE_INTERNAL_API
  289. /**
  290. * Returns the locale for which this object was constructed.
  291. * @stable ICU 2.6
  292. */
  293. inline Locale getLocale() const;
  294. /**
  295. * Returns the locale for this object. Two flavors are available:
  296. * valid and actual locale.
  297. * @stable ICU 2.8
  298. */
  299. Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
  300. /**
  301. * Get pattern string for 'CurrencySpacing' that can be applied to
  302. * currency format.
  303. * This API gets the CurrencySpacing data from ResourceBundle. The pattern can
  304. * be empty if there is no data from current locale and its parent locales.
  305. *
  306. * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
  307. * @param beforeCurrency : true if the pattern is for before currency symbol.
  308. * false if the pattern is for after currency symbol.
  309. * @param status: Input/output parameter, set to success or
  310. * failure code upon return.
  311. * @return pattern string for currencyMatch, surroundingMatch or spaceInsert.
  312. * Return empty string if there is no data for this locale and its parent
  313. * locales.
  314. * @stable ICU 4.8
  315. */
  316. const UnicodeString& getPatternForCurrencySpacing(UCurrencySpacing type,
  317. UBool beforeCurrency,
  318. UErrorCode& status) const;
  319. /**
  320. * Set pattern string for 'CurrencySpacing' that can be applied to
  321. * currency format.
  322. *
  323. * @param type : UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH or UNUM_CURRENCY_INSERT.
  324. * @param beforeCurrency : true if the pattern is for before currency symbol.
  325. * false if the pattern is for after currency symbol.
  326. * @param pattern : pattern string to override current setting.
  327. * @stable ICU 4.8
  328. */
  329. void setPatternForCurrencySpacing(UCurrencySpacing type,
  330. UBool beforeCurrency,
  331. const UnicodeString& pattern);
  332. /**
  333. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  334. *
  335. * @stable ICU 2.2
  336. */
  337. virtual UClassID getDynamicClassID() const override;
  338. /**
  339. * ICU "poor man's RTTI", returns a UClassID for this class.
  340. *
  341. * @stable ICU 2.2
  342. */
  343. static UClassID U_EXPORT2 getStaticClassID();
  344. private:
  345. DecimalFormatSymbols();
  346. /**
  347. * Initializes the symbols from the LocaleElements resource bundle.
  348. * Note: The organization of LocaleElements badly needs to be
  349. * cleaned up.
  350. *
  351. * @param locale The locale to get symbols for.
  352. * @param success Input/output parameter, set to success or
  353. * failure code upon return.
  354. * @param useLastResortData determine if use last resort data
  355. * @param ns The NumberingSystem to use; otherwise, fall
  356. * back to the locale.
  357. */
  358. void initialize(const Locale& locale, UErrorCode& success,
  359. UBool useLastResortData = false, const NumberingSystem* ns = nullptr);
  360. /**
  361. * Initialize the symbols with default values.
  362. */
  363. void initialize();
  364. public:
  365. #ifndef U_HIDE_INTERNAL_API
  366. /**
  367. * @internal For ICU use only
  368. */
  369. inline UBool isCustomCurrencySymbol() const {
  370. return fIsCustomCurrencySymbol;
  371. }
  372. /**
  373. * @internal For ICU use only
  374. */
  375. inline UBool isCustomIntlCurrencySymbol() const {
  376. return fIsCustomIntlCurrencySymbol;
  377. }
  378. /**
  379. * @internal For ICU use only
  380. */
  381. inline UChar32 getCodePointZero() const {
  382. return fCodePointZero;
  383. }
  384. #endif /* U_HIDE_INTERNAL_API */
  385. /**
  386. * _Internal_ function - more efficient version of getSymbol,
  387. * returning a const reference to one of the symbol strings.
  388. * The returned reference becomes invalid when the symbol is changed
  389. * or when the DecimalFormatSymbols are destroyed.
  390. * Note: moved \#ifndef U_HIDE_INTERNAL_API after this, since this is needed for inline in DecimalFormat
  391. *
  392. * This is not currently stable API, but if you think it should be stable,
  393. * post a comment on the following ticket and the ICU team will take a look:
  394. * https://unicode-org.atlassian.net/browse/ICU-13580
  395. *
  396. * @param symbol Constant to indicate a number format symbol.
  397. * @return the format symbol by the param 'symbol'
  398. * @internal
  399. */
  400. inline const UnicodeString& getConstSymbol(ENumberFormatSymbol symbol) const;
  401. #ifndef U_HIDE_INTERNAL_API
  402. /**
  403. * Returns the const UnicodeString reference, like getConstSymbol,
  404. * corresponding to the digit with the given value. This is equivalent
  405. * to accessing the symbol from getConstSymbol with the corresponding
  406. * key, such as kZeroDigitSymbol or kOneDigitSymbol.
  407. *
  408. * This is not currently stable API, but if you think it should be stable,
  409. * post a comment on the following ticket and the ICU team will take a look:
  410. * https://unicode-org.atlassian.net/browse/ICU-13580
  411. *
  412. * @param digit The digit, an integer between 0 and 9 inclusive.
  413. * If outside the range 0 to 9, the zero digit is returned.
  414. * @return the format symbol for the given digit.
  415. * @internal This API is currently for ICU use only.
  416. */
  417. inline const UnicodeString& getConstDigitSymbol(int32_t digit) const;
  418. /**
  419. * Returns that pattern stored in currency info. Internal API for use by NumberFormat API.
  420. * @internal
  421. */
  422. inline const char16_t* getCurrencyPattern() const;
  423. /**
  424. * Returns the numbering system with which this DecimalFormatSymbols was initialized.
  425. * @internal
  426. */
  427. inline const char* getNumberingSystemName() const;
  428. #endif /* U_HIDE_INTERNAL_API */
  429. private:
  430. /**
  431. * Private symbol strings.
  432. * They are either loaded from a resource bundle or otherwise owned.
  433. * setSymbol() clones the symbol string.
  434. * Readonly aliases can only come from a resource bundle, so that we can always
  435. * use fastCopyFrom() with them.
  436. *
  437. * If DecimalFormatSymbols becomes subclassable and the status of fSymbols changes
  438. * from private to protected,
  439. * or when fSymbols can be set any other way that allows them to be readonly aliases
  440. * to non-resource bundle strings,
  441. * then regular UnicodeString copies must be used instead of fastCopyFrom().
  442. *
  443. */
  444. UnicodeString fSymbols[kFormatSymbolCount];
  445. /**
  446. * Non-symbol variable for getConstSymbol(). Always empty.
  447. */
  448. UnicodeString fNoSymbol;
  449. /**
  450. * Dealing with code points is faster than dealing with strings when formatting. Because of
  451. * this, we maintain a value containing the zero code point that is used whenever digitStrings
  452. * represents a sequence of ten code points in order.
  453. *
  454. * <p>If the value stored here is positive, it means that the code point stored in this value
  455. * corresponds to the digitStrings array, and codePointZero can be used instead of the
  456. * digitStrings array for the purposes of efficient formatting; if -1, then digitStrings does
  457. * *not* contain a sequence of code points, and it must be used directly.
  458. *
  459. * <p>It is assumed that codePointZero always shadows the value in digitStrings. codePointZero
  460. * should never be set directly; rather, it should be updated only when digitStrings mutates.
  461. * That is, the flow of information is digitStrings -> codePointZero, not the other way.
  462. */
  463. UChar32 fCodePointZero;
  464. Locale locale;
  465. char actualLocale[ULOC_FULLNAME_CAPACITY];
  466. char validLocale[ULOC_FULLNAME_CAPACITY];
  467. const char16_t* currPattern = nullptr;
  468. UnicodeString currencySpcBeforeSym[UNUM_CURRENCY_SPACING_COUNT];
  469. UnicodeString currencySpcAfterSym[UNUM_CURRENCY_SPACING_COUNT];
  470. UBool fIsCustomCurrencySymbol;
  471. UBool fIsCustomIntlCurrencySymbol;
  472. char nsName[kInternalNumSysNameCapacity+1] = {};
  473. };
  474. // -------------------------------------
  475. inline UnicodeString
  476. DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol) const {
  477. const UnicodeString *strPtr;
  478. if(symbol < kFormatSymbolCount) {
  479. strPtr = &fSymbols[symbol];
  480. } else {
  481. strPtr = &fNoSymbol;
  482. }
  483. return *strPtr;
  484. }
  485. // See comments above for this function. Not hidden with #ifdef U_HIDE_INTERNAL_API
  486. inline const UnicodeString &
  487. DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol) const {
  488. const UnicodeString *strPtr;
  489. if(symbol < kFormatSymbolCount) {
  490. strPtr = &fSymbols[symbol];
  491. } else {
  492. strPtr = &fNoSymbol;
  493. }
  494. return *strPtr;
  495. }
  496. #ifndef U_HIDE_INTERNAL_API
  497. inline const UnicodeString& DecimalFormatSymbols::getConstDigitSymbol(int32_t digit) const {
  498. if (digit < 0 || digit > 9) {
  499. digit = 0;
  500. }
  501. if (digit == 0) {
  502. return fSymbols[kZeroDigitSymbol];
  503. }
  504. ENumberFormatSymbol key = static_cast<ENumberFormatSymbol>(kOneDigitSymbol + digit - 1);
  505. return fSymbols[key];
  506. }
  507. #endif /* U_HIDE_INTERNAL_API */
  508. // -------------------------------------
  509. inline void
  510. DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propagateDigits = true) {
  511. if (symbol == kCurrencySymbol) {
  512. fIsCustomCurrencySymbol = true;
  513. }
  514. else if (symbol == kIntlCurrencySymbol) {
  515. fIsCustomIntlCurrencySymbol = true;
  516. }
  517. if(symbol<kFormatSymbolCount) {
  518. fSymbols[symbol]=value;
  519. }
  520. // If the zero digit is being set to a known zero digit according to Unicode,
  521. // then we automatically set the corresponding 1-9 digits
  522. // Also record updates to fCodePointZero. Be conservative if in doubt.
  523. if (symbol == kZeroDigitSymbol) {
  524. UChar32 sym = value.char32At(0);
  525. if ( propagateDigits && u_charDigitValue(sym) == 0 && value.countChar32() == 1 ) {
  526. fCodePointZero = sym;
  527. for ( int8_t i = 1 ; i<= 9 ; i++ ) {
  528. sym++;
  529. fSymbols[(int)kOneDigitSymbol+i-1] = UnicodeString(sym);
  530. }
  531. } else {
  532. fCodePointZero = -1;
  533. }
  534. } else if (symbol >= kOneDigitSymbol && symbol <= kNineDigitSymbol) {
  535. fCodePointZero = -1;
  536. }
  537. }
  538. // -------------------------------------
  539. inline Locale
  540. DecimalFormatSymbols::getLocale() const {
  541. return locale;
  542. }
  543. #ifndef U_HIDE_INTERNAL_API
  544. inline const char16_t*
  545. DecimalFormatSymbols::getCurrencyPattern() const {
  546. return currPattern;
  547. }
  548. inline const char*
  549. DecimalFormatSymbols::getNumberingSystemName() const {
  550. return nsName;
  551. }
  552. #endif /* U_HIDE_INTERNAL_API */
  553. U_NAMESPACE_END
  554. #endif /* #if !UCONFIG_NO_FORMATTING */
  555. #endif /* U_SHOW_CPLUSPLUS_API */
  556. #endif // _DCFMTSYM
  557. //eof