1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
// © 2018 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
#include <_foundation_unicode/utypes.h>
#if !UCONFIG_NO_FORMATTING
#ifndef __NUMBER_MAPPER_H__
#define __NUMBER_MAPPER_H__
#if U_HAVE_ATOMICS
# include <atomic>
#endif
#include "number_types.h"
#include <_foundation_unicode/currpinf.h>
#include "standardplural.h"
#include "number_patternstring.h"
#include "number_currencysymbols.h"
#include "numparse_impl.h"
U_NAMESPACE_BEGIN
namespace number {
namespace impl {
class AutoAffixPatternProvider;
class CurrencyPluralInfoAffixProvider;
class PropertiesAffixPatternProvider : public AffixPatternProvider, public UMemory {
public:
bool isBogus() const {
return fBogus;
}
void setToBogus() {
fBogus = true;
}
void setTo(const DecimalFormatProperties& properties, UErrorCode& status);
// AffixPatternProvider Methods:
char16_t charAt(int32_t flags, int32_t i) const override;
int32_t length(int32_t flags) const override;
UnicodeString getString(int32_t flags) const override;
bool hasCurrencySign() const override;
bool positiveHasPlusSign() const override;
bool hasNegativeSubpattern() const override;
bool negativeHasMinusSign() const override;
bool containsSymbolType(AffixPatternType, UErrorCode&) const override;
bool hasBody() const override;
bool currencyAsDecimal() const override;
private:
UnicodeString posPrefix;
UnicodeString posSuffix;
UnicodeString negPrefix;
UnicodeString negSuffix;
bool isCurrencyPattern;
bool fCurrencyAsDecimal;
PropertiesAffixPatternProvider() = default; // puts instance in valid but undefined state
const UnicodeString& getStringInternal(int32_t flags) const;
bool fBogus{true};
friend class AutoAffixPatternProvider;
friend class CurrencyPluralInfoAffixProvider;
};
class CurrencyPluralInfoAffixProvider : public AffixPatternProvider, public UMemory {
public:
bool isBogus() const {
return fBogus;
}
void setToBogus() {
fBogus = true;
}
void setTo(const CurrencyPluralInfo& cpi, const DecimalFormatProperties& properties,
UErrorCode& status);
// AffixPatternProvider Methods:
char16_t charAt(int32_t flags, int32_t i) const override;
int32_t length(int32_t flags) const override;
UnicodeString getString(int32_t flags) const override;
bool hasCurrencySign() const override;
bool positiveHasPlusSign() const override;
bool hasNegativeSubpattern() const override;
bool negativeHasMinusSign() const override;
bool containsSymbolType(AffixPatternType, UErrorCode&) const override;
bool hasBody() const override;
bool currencyAsDecimal() const override;
private:
PropertiesAffixPatternProvider affixesByPlural[StandardPlural::COUNT];
CurrencyPluralInfoAffixProvider() = default;
bool fBogus{true};
friend class AutoAffixPatternProvider;
};
class AutoAffixPatternProvider {
public:
inline AutoAffixPatternProvider() = default;
inline AutoAffixPatternProvider(const DecimalFormatProperties& properties, UErrorCode& status) {
setTo(properties, status);
}
inline void setTo(const DecimalFormatProperties& properties, UErrorCode& status) {
if (properties.currencyPluralInfo.fPtr.isNull()) {
propertiesAPP.setTo(properties, status);
currencyPluralInfoAPP.setToBogus();
} else {
propertiesAPP.setToBogus();
currencyPluralInfoAPP.setTo(*properties.currencyPluralInfo.fPtr, properties, status);
}
}
inline void setTo(const AffixPatternProvider* provider, UErrorCode& status) {
if (auto ptr = dynamic_cast<const PropertiesAffixPatternProvider*>(provider)) {
propertiesAPP = *ptr;
} else if (auto ptr = dynamic_cast<const CurrencyPluralInfoAffixProvider*>(provider)) {
currencyPluralInfoAPP = *ptr;
} else {
status = U_INTERNAL_PROGRAM_ERROR;
}
}
inline const AffixPatternProvider& get() const {
if (!currencyPluralInfoAPP.isBogus()) {
return currencyPluralInfoAPP;
} else {
return propertiesAPP;
}
}
private:
PropertiesAffixPatternProvider propertiesAPP;
CurrencyPluralInfoAffixProvider currencyPluralInfoAPP;
};
/**
* A struct for ownership of a few objects needed for formatting.
*/
struct DecimalFormatWarehouse : public UMemory {
AutoAffixPatternProvider affixProvider;
LocalPointer<PluralRules> rules;
};
/**
* Internal fields for DecimalFormat.
* TODO: Make some of these fields by value instead of by LocalPointer?
*/
struct DecimalFormatFields : public UMemory {
DecimalFormatFields() {}
DecimalFormatFields(const DecimalFormatProperties& propsToCopy)
: properties(propsToCopy) {}
/** The property bag corresponding to user-specified settings and settings from the pattern string. */
DecimalFormatProperties properties;
/** The symbols for the current locale. */
LocalPointer<const DecimalFormatSymbols> symbols;
/**
* The pre-computed formatter object. Setters cause this to be re-computed atomically. The {@link
* #format} method uses the formatter directly without needing to synchronize.
*/
LocalizedNumberFormatter formatter;
/** The lazy-computed parser for .parse() */
#if U_HAVE_ATOMICS
std::atomic<::icu::numparse::impl::NumberParserImpl*> atomicParser = {};
#else
::icu::numparse::impl::NumberParserImpl* atomicParser = nullptr;
#endif
/** The lazy-computed parser for .parseCurrency() */
#if U_HAVE_ATOMICS
std::atomic<::icu::numparse::impl::NumberParserImpl*> atomicCurrencyParser = {};
#else
::icu::numparse::impl::NumberParserImpl* atomicCurrencyParser = {};
#endif
/** Small object ownership warehouse for the formatter and parser */
DecimalFormatWarehouse warehouse;
/** The effective properties as exported from the formatter object. Used by some getters. */
DecimalFormatProperties exportedProperties;
// Data for fastpath
bool canUseFastFormat = false;
struct FastFormatData {
char16_t cpZero;
char16_t cpGroupingSeparator;
char16_t cpMinusSign;
int8_t minInt;
int8_t maxInt;
} fastData;
};
/**
* Utilities for converting between a DecimalFormatProperties and a MacroProps.
*/
class NumberPropertyMapper {
public:
/** Convenience method to create a NumberFormatter directly from Properties. */
static UnlocalizedNumberFormatter create(const DecimalFormatProperties& properties,
const DecimalFormatSymbols& symbols,
DecimalFormatWarehouse& warehouse, UErrorCode& status);
/** Convenience method to create a NumberFormatter directly from Properties. */
static UnlocalizedNumberFormatter create(const DecimalFormatProperties& properties,
const DecimalFormatSymbols& symbols,
DecimalFormatWarehouse& warehouse,
DecimalFormatProperties& exportedProperties,
UErrorCode& status);
/**
* Creates a new {@link MacroProps} object based on the content of a {@link DecimalFormatProperties}
* object. In other words, maps Properties to MacroProps. This function is used by the
* JDK-compatibility API to call into the ICU 60 fluent number formatting pipeline.
*
* @param properties
* The property bag to be mapped.
* @param symbols
* The symbols associated with the property bag.
* @param exportedProperties
* A property bag in which to store validated properties. Used by some DecimalFormat
* getters.
* @return A new MacroProps containing all of the information in the Properties.
*/
static MacroProps oldToNew(const DecimalFormatProperties& properties,
const DecimalFormatSymbols& symbols, DecimalFormatWarehouse& warehouse,
DecimalFormatProperties* exportedProperties, UErrorCode& status);
};
} // namespace impl
} // namespace numparse
U_NAMESPACE_END
#endif //__NUMBER_MAPPER_H__
#endif /* #if !UCONFIG_NO_FORMATTING */
|