| 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_FORMATIMPL_H__ |
| 8 |
#define __NUMBER_FORMATIMPL_H__ |
| 9 |
|
| 10 |
#include "number_types.h" |
| 11 |
#include "formatted_string_builder.h" |
| 12 |
#include "number_patternstring.h" |
| 13 |
#include "number_usageprefs.h" |
| 14 |
#include "number_utils.h" |
| 15 |
#include "number_patternmodifier.h" |
| 16 |
#include "number_longnames.h" |
| 17 |
#include "number_compact.h" |
| 18 |
#include "number_microprops.h" |
| 19 |
#include "number_utypes.h" |
| 20 |
|
| 21 |
U_NAMESPACE_BEGIN |
| 22 |
namespace number::impl { |
| 23 |
|
| 24 |
/** |
| 25 |
* This is the "brain" of the number formatting pipeline. It ties all the pieces together, taking in a MacroProps and a |
| 26 |
* DecimalQuantity and outputting a properly formatted number string. |
| 27 |
*/ |
| 28 |
class NumberFormatterImpl : public UMemory { |
| 29 |
public: |
| 30 |
/** |
| 31 |
* Builds a "safe" MicroPropsGenerator, which is thread-safe and can be used repeatedly. |
| 32 |
* The caller owns the returned NumberFormatterImpl. |
| 33 |
*/ |
| 34 |
NumberFormatterImpl(const MacroProps ¯os, UErrorCode &status); |
| 35 |
|
| 36 |
/** |
| 37 |
* Default constructor; leaves the NumberFormatterImpl in an undefined state. |
| 38 |
* Takes an error code to prevent the method from being called accidentally. |
| 39 |
*/ |
| 40 |
NumberFormatterImpl(UErrorCode &) {} |
| 41 |
|
| 42 |
/** |
| 43 |
* Builds and evaluates an "unsafe" MicroPropsGenerator, which is cheaper but can be used only once. |
| 44 |
*/ |
| 45 |
static int32_t formatStatic(const MacroProps ¯os, UFormattedNumberData *results, |
| 46 |
UErrorCode &status); |
| 47 |
|
| 48 |
/** |
| 49 |
* Prints only the prefix and suffix; used for DecimalFormat getters. |
| 50 |
* |
| 51 |
* @return The index into the output at which the prefix ends and the suffix starts; in other words, |
| 52 |
* the prefix length. |
| 53 |
*/ |
| 54 |
static int32_t getPrefixSuffixStatic(const MacroProps& macros, Signum signum, |
| 55 |
StandardPlural::Form plural, FormattedStringBuilder& outString, |
| 56 |
UErrorCode& status); |
| 57 |
|
| 58 |
/** |
| 59 |
* Evaluates the "safe" MicroPropsGenerator created by "fromMacros". |
| 60 |
*/ |
| 61 |
int32_t format(UFormattedNumberData *results, UErrorCode &status) const; |
| 62 |
|
| 63 |
/** |
| 64 |
* Like format(), but saves the result into an output MicroProps without additional processing. |
| 65 |
*/ |
| 66 |
void preProcess(DecimalQuantity& inValue, MicroProps& microsOut, UErrorCode& status) const; |
| 67 |
|
| 68 |
/** |
| 69 |
* Like getPrefixSuffixStatic() but uses the safe compiled object. |
| 70 |
*/ |
| 71 |
int32_t getPrefixSuffix(Signum signum, StandardPlural::Form plural, FormattedStringBuilder& outString, |
| 72 |
UErrorCode& status) const; |
| 73 |
|
| 74 |
const MicroProps& getRawMicroProps() const { |
| 75 |
return fMicros; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Synthesizes the output string from a MicroProps and DecimalQuantity. |
| 80 |
* This method formats only the main number, not affixes. |
| 81 |
*/ |
| 82 |
static int32_t writeNumber( |
| 83 |
const SimpleMicroProps& micros, |
| 84 |
DecimalQuantity& quantity, |
| 85 |
FormattedStringBuilder& string, |
| 86 |
int32_t index, |
| 87 |
UErrorCode& status); |
| 88 |
|
| 89 |
/** |
| 90 |
* Adds the affixes. Intended to be called immediately after formatNumber. |
| 91 |
*/ |
| 92 |
static int32_t writeAffixes( |
| 93 |
const MicroProps& micros, |
| 94 |
FormattedStringBuilder& string, |
| 95 |
int32_t start, |
| 96 |
int32_t end, |
| 97 |
UErrorCode& status); |
| 98 |
|
| 99 |
private: |
| 100 |
// Head of the MicroPropsGenerator linked list. Subclasses' processQuantity |
| 101 |
// methods process this list in a parent-first order, such that the last |
| 102 |
// item added, which this points to, typically has its logic executed last. |
| 103 |
const MicroPropsGenerator *fMicroPropsGenerator = nullptr; |
| 104 |
|
| 105 |
// Tail of the list: |
| 106 |
MicroProps fMicros; |
| 107 |
|
| 108 |
// Other fields possibly used by the number formatting pipeline: |
| 109 |
// TODO: Convert more of these LocalPointers to value objects to reduce the number of news? |
| 110 |
LocalPointer<const UsagePrefsHandler> fUsagePrefsHandler; |
| 111 |
LocalPointer<const UnitConversionHandler> fUnitConversionHandler; |
| 112 |
LocalPointer<const DecimalFormatSymbols> fSymbols; |
| 113 |
LocalPointer<const PluralRules> fRules; |
| 114 |
LocalPointer<const ParsedPatternInfo> fPatternInfo; |
| 115 |
LocalPointer<const ScientificHandler> fScientificHandler; |
| 116 |
LocalPointer<MutablePatternModifier> fPatternModifier; |
| 117 |
LocalPointer<ImmutablePatternModifier> fImmutablePatternModifier; |
| 118 |
LocalPointer<LongNameHandler> fLongNameHandler; |
| 119 |
// TODO: use a common base class that enables fLongNameHandler, |
| 120 |
// fLongNameMultiplexer, and fMixedUnitLongNameHandler to be merged into one |
| 121 |
// member? |
| 122 |
LocalPointer<MixedUnitLongNameHandler> fMixedUnitLongNameHandler; |
| 123 |
LocalPointer<const LongNameMultiplexer> fLongNameMultiplexer; |
| 124 |
LocalPointer<const CompactHandler> fCompactHandler; |
| 125 |
|
| 126 |
NumberFormatterImpl(const MacroProps ¯os, bool safe, UErrorCode &status); |
| 127 |
|
| 128 |
MicroProps& preProcessUnsafe(DecimalQuantity &inValue, UErrorCode &status); |
| 129 |
|
| 130 |
int32_t getPrefixSuffixUnsafe(Signum signum, StandardPlural::Form plural, |
| 131 |
FormattedStringBuilder& outString, UErrorCode& status); |
| 132 |
|
| 133 |
/** |
| 134 |
* If rulesPtr is non-null, return it. Otherwise, return a PluralRules owned by this object for the |
| 135 |
* specified locale, creating it if necessary. |
| 136 |
*/ |
| 137 |
const PluralRules * |
| 138 |
resolvePluralRules(const PluralRules *rulesPtr, const Locale &locale, UErrorCode &status); |
| 139 |
|
| 140 |
/** |
| 141 |
* Synthesizes the MacroProps into a MicroPropsGenerator. All information, including the locale, is encoded into the |
| 142 |
* MicroPropsGenerator, except for the quantity itself, which is left abstract and must be provided to the returned |
| 143 |
* MicroPropsGenerator instance. |
| 144 |
* |
| 145 |
* @see MicroPropsGenerator |
| 146 |
* @param macros |
| 147 |
* The {@link MacroProps} to consume. This method does not mutate the MacroProps instance. |
| 148 |
* @param safe |
| 149 |
* If true, the returned MicroPropsGenerator will be thread-safe. If false, the returned value will |
| 150 |
* <em>not</em> be thread-safe, intended for a single "one-shot" use only. Building the thread-safe |
| 151 |
* object is more expensive. |
| 152 |
*/ |
| 153 |
const MicroPropsGenerator * |
| 154 |
macrosToMicroGenerator(const MacroProps ¯os, bool safe, UErrorCode &status); |
| 155 |
|
| 156 |
static int32_t |
| 157 |
writeIntegerDigits( |
| 158 |
const SimpleMicroProps& micros, |
| 159 |
DecimalQuantity &quantity, |
| 160 |
FormattedStringBuilder &string, |
| 161 |
int32_t index, |
| 162 |
UErrorCode &status); |
| 163 |
|
| 164 |
static int32_t |
| 165 |
writeFractionDigits( |
| 166 |
const SimpleMicroProps& micros, |
| 167 |
DecimalQuantity &quantity, |
| 168 |
FormattedStringBuilder &string, |
| 169 |
int32_t index, |
| 170 |
UErrorCode &status); |
| 171 |
}; |
| 172 |
|
| 173 |
} // namespace number::impl |
| 174 |
U_NAMESPACE_END |
| 175 |
|
| 176 |
|
| 177 |
#endif //__NUMBER_FORMATIMPL_H__ |
| 178 |
|
| 179 |
#endif /* #if !UCONFIG_NO_FORMATTING */ |