number_decimfmtprops.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // © 2017 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. #include "unicode/utypes.h"
  4. #if !UCONFIG_NO_FORMATTING
  5. #ifndef __NUMBER_DECIMFMTPROPS_H__
  6. #define __NUMBER_DECIMFMTPROPS_H__
  7. #include "unicode/unistr.h"
  8. #include <cstdint>
  9. #include "unicode/plurrule.h"
  10. #include "unicode/currpinf.h"
  11. #include "unicode/unum.h"
  12. #include "unicode/localpointer.h"
  13. #include "number_types.h"
  14. U_NAMESPACE_BEGIN
  15. // Export an explicit template instantiation of the LocalPointer that is used as a
  16. // data member of CurrencyPluralInfoWrapper.
  17. // (When building DLLs for Windows this is required.)
  18. #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
  19. #if defined(_MSC_VER)
  20. // Ignore warning 4661 as LocalPointerBase does not use operator== or operator!=
  21. #pragma warning(push)
  22. #pragma warning(disable: 4661)
  23. #endif
  24. template class U_I18N_API LocalPointerBase<CurrencyPluralInfo>;
  25. template class U_I18N_API LocalPointer<CurrencyPluralInfo>;
  26. #if defined(_MSC_VER)
  27. #pragma warning(pop)
  28. #endif
  29. #endif
  30. namespace number {
  31. namespace impl {
  32. // Exported as U_I18N_API because it is a public member field of exported DecimalFormatProperties
  33. // Using this wrapper is rather unfortunate, but is needed on Windows platforms in order to allow
  34. // for DLL-exporting an fully specified template instantiation.
  35. class U_I18N_API CurrencyPluralInfoWrapper {
  36. public:
  37. LocalPointer<CurrencyPluralInfo> fPtr;
  38. CurrencyPluralInfoWrapper() = default;
  39. CurrencyPluralInfoWrapper(const CurrencyPluralInfoWrapper& other) {
  40. if (!other.fPtr.isNull()) {
  41. fPtr.adoptInstead(new CurrencyPluralInfo(*other.fPtr));
  42. }
  43. }
  44. CurrencyPluralInfoWrapper& operator=(const CurrencyPluralInfoWrapper& other) {
  45. if (!other.fPtr.isNull()) {
  46. fPtr.adoptInstead(new CurrencyPluralInfo(*other.fPtr));
  47. }
  48. return *this;
  49. }
  50. };
  51. /** Controls the set of rules for parsing a string from the old DecimalFormat API. */
  52. enum ParseMode {
  53. /**
  54. * Lenient mode should be used if you want to accept malformed user input. It will use heuristics
  55. * to attempt to parse through typographical errors in the string.
  56. */
  57. PARSE_MODE_LENIENT,
  58. /**
  59. * Strict mode should be used if you want to require that the input is well-formed. More
  60. * specifically, it differs from lenient mode in the following ways:
  61. *
  62. * <ul>
  63. * <li>Grouping widths must match the grouping settings. For example, "12,3,45" will fail if the
  64. * grouping width is 3, as in the pattern "#,##0".
  65. * <li>The string must contain a complete prefix and suffix. For example, if the pattern is
  66. * "{#};(#)", then "{123}" or "(123)" would match, but "{123", "123}", and "123" would all fail.
  67. * (The latter strings would be accepted in lenient mode.)
  68. * <li>Whitespace may not appear at arbitrary places in the string. In lenient mode, whitespace
  69. * is allowed to occur arbitrarily before and after prefixes and exponent separators.
  70. * <li>Leading grouping separators are not allowed, as in ",123".
  71. * <li>Minus and plus signs can only appear if specified in the pattern. In lenient mode, a plus
  72. * or minus sign can always precede a number.
  73. * <li>The set of characters that can be interpreted as a decimal or grouping separator is
  74. * smaller.
  75. * <li><strong>If currency parsing is enabled,</strong> currencies must only appear where
  76. * specified in either the current pattern string or in a valid pattern string for the current
  77. * locale. For example, if the pattern is "¤0.00", then "$1.23" would match, but "1.23$" would
  78. * fail to match.
  79. * </ul>
  80. */
  81. PARSE_MODE_STRICT,
  82. };
  83. // Exported as U_I18N_API because it is needed for the unit test PatternStringTest
  84. struct U_I18N_API DecimalFormatProperties : public UMemory {
  85. public:
  86. NullableValue<UNumberCompactStyle> compactStyle;
  87. NullableValue<CurrencyUnit> currency;
  88. CurrencyPluralInfoWrapper currencyPluralInfo;
  89. NullableValue<UCurrencyUsage> currencyUsage;
  90. bool decimalPatternMatchRequired;
  91. bool decimalSeparatorAlwaysShown;
  92. bool exponentSignAlwaysShown;
  93. bool formatFailIfMoreThanMaxDigits; // ICU4C-only
  94. int32_t formatWidth;
  95. int32_t groupingSize;
  96. bool groupingUsed;
  97. int32_t magnitudeMultiplier; // internal field like multiplierScale but separate to avoid conflict
  98. int32_t maximumFractionDigits;
  99. int32_t maximumIntegerDigits;
  100. int32_t maximumSignificantDigits;
  101. int32_t minimumExponentDigits;
  102. int32_t minimumFractionDigits;
  103. int32_t minimumGroupingDigits;
  104. int32_t minimumIntegerDigits;
  105. int32_t minimumSignificantDigits;
  106. int32_t multiplier;
  107. int32_t multiplierScale; // ICU4C-only
  108. UnicodeString negativePrefix;
  109. UnicodeString negativePrefixPattern;
  110. UnicodeString negativeSuffix;
  111. UnicodeString negativeSuffixPattern;
  112. NullableValue<PadPosition> padPosition;
  113. UnicodeString padString;
  114. bool parseCaseSensitive;
  115. bool parseIntegerOnly;
  116. NullableValue<ParseMode> parseMode;
  117. bool parseNoExponent;
  118. bool parseToBigDecimal; // TODO: Not needed in ICU4C?
  119. UNumberFormatAttributeValue parseAllInput; // ICU4C-only
  120. //PluralRules pluralRules;
  121. UnicodeString positivePrefix;
  122. UnicodeString positivePrefixPattern;
  123. UnicodeString positiveSuffix;
  124. UnicodeString positiveSuffixPattern;
  125. double roundingIncrement;
  126. NullableValue<RoundingMode> roundingMode;
  127. int32_t secondaryGroupingSize;
  128. bool signAlwaysShown;
  129. DecimalFormatProperties();
  130. inline bool operator==(const DecimalFormatProperties& other) const {
  131. return _equals(other, false);
  132. }
  133. void clear();
  134. /**
  135. * Checks for equality to the default DecimalFormatProperties, but ignores the prescribed set of
  136. * options for fast-path formatting.
  137. */
  138. bool equalsDefaultExceptFastFormat() const;
  139. /**
  140. * Returns the default DecimalFormatProperties instance.
  141. */
  142. static const DecimalFormatProperties& getDefault();
  143. private:
  144. bool _equals(const DecimalFormatProperties& other, bool ignoreForFastFormat) const;
  145. };
  146. } // namespace impl
  147. } // namespace number
  148. U_NAMESPACE_END
  149. #endif //__NUMBER_DECIMFMTPROPS_H__
  150. #endif /* #if !UCONFIG_NO_FORMATTING */