| 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_PATTERNMODIFIER_H__ |
| 8 |
#define __NUMBER_PATTERNMODIFIER_H__ |
| 9 |
|
| 10 |
#include "standardplural.h" |
| 11 |
#include "unicode/numberformatter.h" |
| 12 |
#include "number_patternstring.h" |
| 13 |
#include "number_types.h" |
| 14 |
#include "number_modifiers.h" |
| 15 |
#include "number_utils.h" |
| 16 |
#include "number_currencysymbols.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 AdoptingModifierStore. |
| 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<number::impl::AdoptingModifierStore>; |
| 25 |
template class U_I18N_API LocalPointer<number::impl::AdoptingModifierStore>; |
| 26 |
#endif |
| 27 |
|
| 28 |
namespace number::impl { |
| 29 |
|
| 30 |
// Forward declaration |
| 31 |
class MutablePatternModifier; |
| 32 |
|
| 33 |
// Exported as U_I18N_API because it is needed for the unit test PatternModifierTest |
| 34 |
class U_I18N_API ImmutablePatternModifier : public MicroPropsGenerator, public UMemory { |
| 35 |
public: |
| 36 |
~ImmutablePatternModifier() override = default; |
| 37 |
|
| 38 |
void processQuantity(DecimalQuantity&, MicroProps& micros, UErrorCode& status) const override; |
| 39 |
|
| 40 |
void applyToMicros(MicroProps& micros, const DecimalQuantity& quantity, UErrorCode& status) const; |
| 41 |
|
| 42 |
const Modifier* getModifier(Signum signum, StandardPlural::Form plural) const; |
| 43 |
|
| 44 |
// Non-const method: |
| 45 |
void addToChain(const MicroPropsGenerator* parent); |
| 46 |
|
| 47 |
private: |
| 48 |
ImmutablePatternModifier(AdoptingModifierStore* pm, const PluralRules* rules); |
| 49 |
|
| 50 |
const LocalPointer<AdoptingModifierStore> pm; |
| 51 |
const PluralRules* rules; |
| 52 |
const MicroPropsGenerator* parent; |
| 53 |
|
| 54 |
friend class MutablePatternModifier; |
| 55 |
}; |
| 56 |
|
| 57 |
/** |
| 58 |
* This class is a {@link Modifier} that wraps a decimal format pattern. It applies the pattern's affixes in |
| 59 |
* {@link Modifier#apply}. |
| 60 |
* |
| 61 |
* <p> |
| 62 |
* In addition to being a Modifier, this class contains the business logic for substituting the correct locale symbols |
| 63 |
* into the affixes of the decimal format pattern. |
| 64 |
* |
| 65 |
* <p> |
| 66 |
* In order to use this class, create a new instance and call the following four setters: {@link #setPatternInfo}, |
| 67 |
* {@link #setPatternAttributes}, {@link #setSymbols}, and {@link #setNumberProperties}. After calling these four |
| 68 |
* setters, the instance will be ready for use as a Modifier. |
| 69 |
* |
| 70 |
* <p> |
| 71 |
* This is a MUTABLE, NON-THREAD-SAFE class designed for performance. Do NOT save references to this or attempt to use |
| 72 |
* it from multiple threads! Instead, you can obtain a safe, immutable decimal format pattern modifier by calling |
| 73 |
* {@link MutablePatternModifier#createImmutable}, in effect treating this instance as a builder for the immutable |
| 74 |
* variant. |
| 75 |
*/ |
| 76 |
class U_I18N_API MutablePatternModifier |
| 77 |
: public MicroPropsGenerator, |
| 78 |
public Modifier, |
| 79 |
public SymbolProvider, |
| 80 |
public UMemory { |
| 81 |
public: |
| 82 |
|
| 83 |
~MutablePatternModifier() override = default; |
| 84 |
|
| 85 |
/** |
| 86 |
* @param isStrong |
| 87 |
* Whether the modifier should be considered strong. For more information, see |
| 88 |
* {@link Modifier#isStrong()}. Most of the time, decimal format pattern modifiers should be considered |
| 89 |
* as non-strong. |
| 90 |
*/ |
| 91 |
explicit MutablePatternModifier(bool isStrong); |
| 92 |
|
| 93 |
/** |
| 94 |
* Sets a reference to the parsed decimal format pattern, usually obtained from |
| 95 |
* {@link PatternStringParser#parseToPatternInfo(String)}, but any implementation of {@link AffixPatternProvider} is |
| 96 |
* accepted. |
| 97 |
* |
| 98 |
* @param field |
| 99 |
* Which field to use for literal characters in the pattern. |
| 100 |
*/ |
| 101 |
void setPatternInfo(const AffixPatternProvider *patternInfo, Field field); |
| 102 |
|
| 103 |
/** |
| 104 |
* Sets attributes that imply changes to the literal interpretation of the pattern string affixes. |
| 105 |
* |
| 106 |
* @param signDisplay |
| 107 |
* Whether to force a plus sign on positive numbers. |
| 108 |
* @param perMille |
| 109 |
* Whether to substitute the percent sign in the pattern with a permille sign. |
| 110 |
* @param approximately |
| 111 |
* Whether to prepend approximately to the sign |
| 112 |
*/ |
| 113 |
void setPatternAttributes(UNumberSignDisplay signDisplay, bool perMille, bool approximately); |
| 114 |
|
| 115 |
/** |
| 116 |
* Sets locale-specific details that affect the symbols substituted into the pattern string affixes. |
| 117 |
* |
| 118 |
* @param symbols |
| 119 |
* The desired instance of DecimalFormatSymbols. |
| 120 |
* @param currency |
| 121 |
* The currency to be used when substituting currency values into the affixes. |
| 122 |
* @param unitWidth |
| 123 |
* The width used to render currencies. |
| 124 |
* @param rules |
| 125 |
* Required if the triple currency sign, "¤¤¤", appears in the pattern, which can be determined from the |
| 126 |
* convenience method {@link #needsPlurals()}. |
| 127 |
* @param status |
| 128 |
* Set if an error occurs while loading currency data. |
| 129 |
*/ |
| 130 |
void setSymbols(const DecimalFormatSymbols* symbols, const CurrencyUnit& currency, |
| 131 |
UNumberUnitWidth unitWidth, const PluralRules* rules, UErrorCode& status); |
| 132 |
|
| 133 |
/** |
| 134 |
* Sets attributes of the current number being processed. |
| 135 |
* |
| 136 |
* @param signum |
| 137 |
* -1 if negative; +1 if positive; or 0 if zero. |
| 138 |
* @param plural |
| 139 |
* The plural form of the number, required only if the pattern contains the triple |
| 140 |
* currency sign, "¤¤¤" (and as indicated by {@link #needsPlurals()}). |
| 141 |
*/ |
| 142 |
void setNumberProperties(Signum signum, StandardPlural::Form plural); |
| 143 |
|
| 144 |
/** |
| 145 |
* Returns true if the pattern represented by this MurkyModifier requires a plural keyword in order to localize. |
| 146 |
* This is currently true only if there is a currency long name placeholder in the pattern ("¤¤¤"). |
| 147 |
*/ |
| 148 |
bool needsPlurals() const; |
| 149 |
|
| 150 |
/** Creates a quantity-dependent Modifier for the specified plural form. */ |
| 151 |
AdoptingSignumModifierStore createImmutableForPlural(StandardPlural::Form plural, UErrorCode& status); |
| 152 |
|
| 153 |
/** |
| 154 |
* Creates a new quantity-dependent Modifier that behaves the same as the current instance, but which is immutable |
| 155 |
* and can be saved for future use. The number properties in the current instance are mutated; all other properties |
| 156 |
* are left untouched. |
| 157 |
* |
| 158 |
* <p> |
| 159 |
* The resulting modifier cannot be used in a QuantityChain. |
| 160 |
* |
| 161 |
* <p> |
| 162 |
* CREATES A NEW HEAP OBJECT; THE CALLER GETS OWNERSHIP. |
| 163 |
* |
| 164 |
* @return An immutable that supports both positive and negative numbers. |
| 165 |
*/ |
| 166 |
ImmutablePatternModifier *createImmutable(UErrorCode &status); |
| 167 |
|
| 168 |
MicroPropsGenerator &addToChain(const MicroPropsGenerator *parent); |
| 169 |
|
| 170 |
void processQuantity(DecimalQuantity &, MicroProps µs, UErrorCode &status) const override; |
| 171 |
|
| 172 |
int32_t apply(FormattedStringBuilder &output, int32_t leftIndex, int32_t rightIndex, |
| 173 |
UErrorCode &status) const override; |
| 174 |
|
| 175 |
int32_t getPrefixLength() const override; |
| 176 |
|
| 177 |
int32_t getCodePointCount() const override; |
| 178 |
|
| 179 |
bool isStrong() const override; |
| 180 |
|
| 181 |
bool containsField(Field field) const override; |
| 182 |
|
| 183 |
void getParameters(Parameters& output) const override; |
| 184 |
|
| 185 |
bool strictEquals(const Modifier& other) const override; |
| 186 |
|
| 187 |
/** |
| 188 |
* Returns the string that substitutes a given symbol type in a pattern. |
| 189 |
*/ |
| 190 |
UnicodeString getSymbol(AffixPatternType type) const override; |
| 191 |
|
| 192 |
/** |
| 193 |
* Returns the currency symbol for the unit width specified in setSymbols() |
| 194 |
*/ |
| 195 |
UnicodeString getCurrencySymbolForUnitWidth(UErrorCode& status) const; |
| 196 |
|
| 197 |
UnicodeString toUnicodeString() const; |
| 198 |
|
| 199 |
private: |
| 200 |
// Modifier details (initialized in constructor) |
| 201 |
const bool fStrong; |
| 202 |
|
| 203 |
// Pattern details (initialized in setPatternInfo and setPatternAttributes) |
| 204 |
const AffixPatternProvider *fPatternInfo; |
| 205 |
Field fField; |
| 206 |
UNumberSignDisplay fSignDisplay; |
| 207 |
bool fPerMilleReplacesPercent; |
| 208 |
bool fApproximately; |
| 209 |
|
| 210 |
// Symbol details (initialized in setSymbols) |
| 211 |
const DecimalFormatSymbols *fSymbols; |
| 212 |
UNumberUnitWidth fUnitWidth; |
| 213 |
CurrencySymbols fCurrencySymbols; |
| 214 |
const PluralRules *fRules; |
| 215 |
|
| 216 |
// Number details (initialized in setNumberProperties) |
| 217 |
Signum fSignum; |
| 218 |
StandardPlural::Form fPlural; |
| 219 |
|
| 220 |
// QuantityChain details (initialized in addToChain) |
| 221 |
const MicroPropsGenerator *fParent; |
| 222 |
|
| 223 |
// Transient fields for rendering |
| 224 |
UnicodeString currentAffix; |
| 225 |
|
| 226 |
/** |
| 227 |
* Uses the current properties to create a single {@link ConstantMultiFieldModifier} with currency spacing support |
| 228 |
* if required. |
| 229 |
* |
| 230 |
* <p> |
| 231 |
* CREATES A NEW HEAP OBJECT; THE CALLER GETS OWNERSHIP. |
| 232 |
* |
| 233 |
* @param a |
| 234 |
* A working FormattedStringBuilder object; passed from the outside to prevent the need to create many new |
| 235 |
* instances if this method is called in a loop. |
| 236 |
* @param b |
| 237 |
* Another working FormattedStringBuilder object. |
| 238 |
* @return The constant modifier object. |
| 239 |
*/ |
| 240 |
ConstantMultiFieldModifier *createConstantModifier(UErrorCode &status); |
| 241 |
|
| 242 |
int32_t insertPrefix(FormattedStringBuilder &sb, int position, UErrorCode &status); |
| 243 |
|
| 244 |
int32_t insertSuffix(FormattedStringBuilder &sb, int position, UErrorCode &status); |
| 245 |
|
| 246 |
void prepareAffix(bool isPrefix); |
| 247 |
}; |
| 248 |
|
| 249 |
} // namespace number::impl |
| 250 |
|
| 251 |
U_NAMESPACE_END |
| 252 |
|
| 253 |
#endif //__NUMBER_PATTERNMODIFIER_H__ |
| 254 |
|
| 255 |
#endif /* #if !UCONFIG_NO_FORMATTING */ |