dcfmtsym.h 16 KB

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