dcfmtsym.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. ********************************************************************************
  3. * Copyright (C) 1997-2015, 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. #ifndef U_HIDE_DRAFT_API
  159. /** Multiplication sign.
  160. * @draft ICU 54
  161. */
  162. kExponentMultiplicationSymbol,
  163. #endif /* U_HIDE_DRAFT_API */
  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. /**
  332. * _Internal_ function - more efficient version of getSymbol,
  333. * returning a const reference to one of the symbol strings.
  334. * The returned reference becomes invalid when the symbol is changed
  335. * or when the DecimalFormatSymbols are destroyed.
  336. * ### TODO markus 2002oct11: Consider proposing getConstSymbol() to be really public.
  337. * Note: moved #ifndef U_HIDE_INTERNAL_API after this, since this is needed for inline in DecimalFormat
  338. *
  339. * @param symbol Constant to indicate a number format symbol.
  340. * @return the format symbol by the param 'symbol'
  341. * @internal
  342. */
  343. inline const UnicodeString &getConstSymbol(ENumberFormatSymbol symbol) const;
  344. #ifndef U_HIDE_INTERNAL_API
  345. /**
  346. * Returns that pattern stored in currecy info. Internal API for use by NumberFormat API.
  347. * @internal
  348. */
  349. inline const UChar* getCurrencyPattern(void) const;
  350. #endif /* U_HIDE_INTERNAL_API */
  351. private:
  352. /**
  353. * Private symbol strings.
  354. * They are either loaded from a resource bundle or otherwise owned.
  355. * setSymbol() clones the symbol string.
  356. * Readonly aliases can only come from a resource bundle, so that we can always
  357. * use fastCopyFrom() with them.
  358. *
  359. * If DecimalFormatSymbols becomes subclassable and the status of fSymbols changes
  360. * from private to protected,
  361. * or when fSymbols can be set any other way that allows them to be readonly aliases
  362. * to non-resource bundle strings,
  363. * then regular UnicodeString copies must be used instead of fastCopyFrom().
  364. *
  365. * @internal
  366. */
  367. UnicodeString fSymbols[kFormatSymbolCount];
  368. /**
  369. * Non-symbol variable for getConstSymbol(). Always empty.
  370. * @internal
  371. */
  372. UnicodeString fNoSymbol;
  373. Locale locale;
  374. char actualLocale[ULOC_FULLNAME_CAPACITY];
  375. char validLocale[ULOC_FULLNAME_CAPACITY];
  376. const UChar* currPattern;
  377. UnicodeString currencySpcBeforeSym[UNUM_CURRENCY_SPACING_COUNT];
  378. UnicodeString currencySpcAfterSym[UNUM_CURRENCY_SPACING_COUNT];
  379. };
  380. // -------------------------------------
  381. inline UnicodeString
  382. DecimalFormatSymbols::getSymbol(ENumberFormatSymbol symbol) const {
  383. const UnicodeString *strPtr;
  384. if(symbol < kFormatSymbolCount) {
  385. strPtr = &fSymbols[symbol];
  386. } else {
  387. strPtr = &fNoSymbol;
  388. }
  389. return *strPtr;
  390. }
  391. //#ifndef U_HIDE_INTERNAL_API
  392. // See comments above for this function. Not hidden.
  393. inline const UnicodeString &
  394. DecimalFormatSymbols::getConstSymbol(ENumberFormatSymbol symbol) const {
  395. const UnicodeString *strPtr;
  396. if(symbol < kFormatSymbolCount) {
  397. strPtr = &fSymbols[symbol];
  398. } else {
  399. strPtr = &fNoSymbol;
  400. }
  401. return *strPtr;
  402. }
  403. //#endif /* U_HIDE_INTERNAL_API */
  404. // -------------------------------------
  405. inline void
  406. DecimalFormatSymbols::setSymbol(ENumberFormatSymbol symbol, const UnicodeString &value, const UBool propogateDigits = TRUE) {
  407. if(symbol<kFormatSymbolCount) {
  408. fSymbols[symbol]=value;
  409. }
  410. // If the zero digit is being set to a known zero digit according to Unicode,
  411. // then we automatically set the corresponding 1-9 digits
  412. if ( propogateDigits && symbol == kZeroDigitSymbol && value.countChar32() == 1 ) {
  413. UChar32 sym = value.char32At(0);
  414. if ( u_charDigitValue(sym) == 0 ) {
  415. for ( int8_t i = 1 ; i<= 9 ; i++ ) {
  416. sym++;
  417. fSymbols[(int)kOneDigitSymbol+i-1] = UnicodeString(sym);
  418. }
  419. }
  420. }
  421. }
  422. // -------------------------------------
  423. inline Locale
  424. DecimalFormatSymbols::getLocale() const {
  425. return locale;
  426. }
  427. #ifndef U_HIDE_INTERNAL_API
  428. inline const UChar*
  429. DecimalFormatSymbols::getCurrencyPattern() const {
  430. return currPattern;
  431. }
  432. #endif /* U_HIDE_INTERNAL_API */
  433. U_NAMESPACE_END
  434. #endif /* #if !UCONFIG_NO_FORMATTING */
  435. #endif // _DCFMTSYM
  436. //eof