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