dcfmtsym.h 17 KB

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