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 namespace number { 22 namespace 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 */ NumberFormatterImpl(UErrorCode &)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 getRawMicroProps()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(const MicroProps& micros, DecimalQuantity& quantity, 83 FormattedStringBuilder& string, int32_t index, UErrorCode& status); 84 85 /** 86 * Adds the affixes. Intended to be called immediately after formatNumber. 87 */ 88 static int32_t writeAffixes(const MicroProps& micros, FormattedStringBuilder& string, int32_t start, 89 int32_t end, UErrorCode& status); 90 91 private: 92 // Head of the MicroPropsGenerator linked list. Subclasses' processQuantity 93 // methods process this list in a parent-first order, such that the last 94 // item added, which this points to, typically has its logic executed last. 95 const MicroPropsGenerator *fMicroPropsGenerator = nullptr; 96 97 // Tail of the list: 98 MicroProps fMicros; 99 100 // Other fields possibly used by the number formatting pipeline: 101 // TODO: Convert more of these LocalPointers to value objects to reduce the number of news? 102 LocalPointer<const UsagePrefsHandler> fUsagePrefsHandler; 103 LocalPointer<const UnitConversionHandler> fUnitConversionHandler; 104 LocalPointer<const DecimalFormatSymbols> fSymbols; 105 LocalPointer<const PluralRules> fRules; 106 LocalPointer<const ParsedPatternInfo> fPatternInfo; 107 LocalPointer<const ScientificHandler> fScientificHandler; 108 LocalPointer<MutablePatternModifier> fPatternModifier; 109 LocalPointer<ImmutablePatternModifier> fImmutablePatternModifier; 110 LocalPointer<LongNameHandler> fLongNameHandler; 111 // TODO: use a common base class that enables fLongNameHandler, 112 // fLongNameMultiplexer, and fMixedUnitLongNameHandler to be merged into one 113 // member? 114 LocalPointer<MixedUnitLongNameHandler> fMixedUnitLongNameHandler; 115 LocalPointer<const LongNameMultiplexer> fLongNameMultiplexer; 116 LocalPointer<const CompactHandler> fCompactHandler; 117 118 NumberFormatterImpl(const MacroProps ¯os, bool safe, UErrorCode &status); 119 120 MicroProps& preProcessUnsafe(DecimalQuantity &inValue, UErrorCode &status); 121 122 int32_t getPrefixSuffixUnsafe(Signum signum, StandardPlural::Form plural, 123 FormattedStringBuilder& outString, UErrorCode& status); 124 125 /** 126 * If rulesPtr is non-null, return it. Otherwise, return a PluralRules owned by this object for the 127 * specified locale, creating it if necessary. 128 */ 129 const PluralRules * 130 resolvePluralRules(const PluralRules *rulesPtr, const Locale &locale, UErrorCode &status); 131 132 /** 133 * Synthesizes the MacroProps into a MicroPropsGenerator. All information, including the locale, is encoded into the 134 * MicroPropsGenerator, except for the quantity itself, which is left abstract and must be provided to the returned 135 * MicroPropsGenerator instance. 136 * 137 * @see MicroPropsGenerator 138 * @param macros 139 * The {@link MacroProps} to consume. This method does not mutate the MacroProps instance. 140 * @param safe 141 * If true, the returned MicroPropsGenerator will be thread-safe. If false, the returned value will 142 * <em>not</em> be thread-safe, intended for a single "one-shot" use only. Building the thread-safe 143 * object is more expensive. 144 */ 145 const MicroPropsGenerator * 146 macrosToMicroGenerator(const MacroProps ¯os, bool safe, UErrorCode &status); 147 148 static int32_t 149 writeIntegerDigits(const MicroProps µs, DecimalQuantity &quantity, FormattedStringBuilder &string, 150 int32_t index, UErrorCode &status); 151 152 static int32_t 153 writeFractionDigits(const MicroProps µs, DecimalQuantity &quantity, FormattedStringBuilder &string, 154 int32_t index, UErrorCode &status); 155 }; 156 157 } // namespace impl 158 } // namespace number 159 U_NAMESPACE_END 160 161 162 #endif //__NUMBER_FORMATIMPL_H__ 163 164 #endif /* #if !UCONFIG_NO_FORMATTING */ 165