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