]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/number_formatimpl.h
ICU-62123.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / number_formatimpl.h
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 "number_stringbuilder.h"
12 #include "number_patternstring.h"
13 #include "number_utils.h"
14 #include "number_patternmodifier.h"
15 #include "number_longnames.h"
16 #include "number_compact.h"
17 #include "number_microprops.h"
18
19 U_NAMESPACE_BEGIN namespace number {
20 namespace impl {
21
22 /**
23 * This is the "brain" of the number formatting pipeline. It ties all the pieces together, taking in a MacroProps and a
24 * DecimalQuantity and outputting a properly formatted number string.
25 */
26 class NumberFormatterImpl : public UMemory {
27 public:
28 /**
29 * Builds a "safe" MicroPropsGenerator, which is thread-safe and can be used repeatedly.
30 * The caller owns the returned NumberFormatterImpl.
31 */
32 static NumberFormatterImpl *fromMacros(const MacroProps &macros, UErrorCode &status);
33
34 /**
35 * Builds and evaluates an "unsafe" MicroPropsGenerator, which is cheaper but can be used only once.
36 */
37 static void
38 applyStatic(const MacroProps &macros, DecimalQuantity &inValue, NumberStringBuilder &outString,
39 UErrorCode &status);
40
41 /**
42 * Prints only the prefix and suffix; used for DecimalFormat getters.
43 *
44 * @return The index into the output at which the prefix ends and the suffix starts; in other words,
45 * the prefix length.
46 */
47 static int32_t getPrefixSuffixStatic(const MacroProps& macros, int8_t signum,
48 StandardPlural::Form plural, NumberStringBuilder& outString,
49 UErrorCode& status);
50
51 /**
52 * Evaluates the "safe" MicroPropsGenerator created by "fromMacros".
53 */
54 void apply(DecimalQuantity& inValue, NumberStringBuilder& outString, UErrorCode& status) const;
55
56 /**
57 * Like getPrefixSuffixStatic() but uses the safe compiled object.
58 */
59 int32_t getPrefixSuffix(int8_t signum, StandardPlural::Form plural, NumberStringBuilder& outString,
60 UErrorCode& status) const;
61
62 private:
63 // Head of the MicroPropsGenerator linked list:
64 const MicroPropsGenerator *fMicroPropsGenerator = nullptr;
65
66 // Tail of the list:
67 MicroProps fMicros;
68
69 // Other fields possibly used by the number formatting pipeline:
70 // TODO: Convert more of these LocalPointers to value objects to reduce the number of news?
71 LocalPointer<const DecimalFormatSymbols> fSymbols;
72 LocalPointer<const PluralRules> fRules;
73 LocalPointer<const ParsedPatternInfo> fPatternInfo;
74 LocalPointer<const ScientificHandler> fScientificHandler;
75 LocalPointer<MutablePatternModifier> fPatternModifier;
76 LocalPointer<const ImmutablePatternModifier> fImmutablePatternModifier;
77 LocalPointer<const LongNameHandler> fLongNameHandler;
78 LocalPointer<const CompactHandler> fCompactHandler;
79
80 // Value objects possibly used by the number formatting pipeline:
81 struct Warehouse {
82 CurrencySymbols fCurrencySymbols;
83 } fWarehouse;
84
85
86 NumberFormatterImpl(const MacroProps &macros, bool safe, UErrorCode &status);
87
88 void applyUnsafe(DecimalQuantity &inValue, NumberStringBuilder &outString, UErrorCode &status);
89
90 int32_t getPrefixSuffixUnsafe(int8_t signum, StandardPlural::Form plural,
91 NumberStringBuilder& outString, UErrorCode& status);
92
93 /**
94 * If rulesPtr is non-null, return it. Otherwise, return a PluralRules owned by this object for the
95 * specified locale, creating it if necessary.
96 */
97 const PluralRules *
98 resolvePluralRules(const PluralRules *rulesPtr, const Locale &locale, UErrorCode &status);
99
100 /**
101 * Synthesizes the MacroProps into a MicroPropsGenerator. All information, including the locale, is encoded into the
102 * MicroPropsGenerator, except for the quantity itself, which is left abstract and must be provided to the returned
103 * MicroPropsGenerator instance.
104 *
105 * @see MicroPropsGenerator
106 * @param macros
107 * The {@link MacroProps} to consume. This method does not mutate the MacroProps instance.
108 * @param safe
109 * If true, the returned MicroPropsGenerator will be thread-safe. If false, the returned value will
110 * <em>not</em> be thread-safe, intended for a single "one-shot" use only. Building the thread-safe
111 * object is more expensive.
112 */
113 const MicroPropsGenerator *
114 macrosToMicroGenerator(const MacroProps &macros, bool safe, UErrorCode &status);
115
116 /**
117 * Synthesizes the output string from a MicroProps and DecimalQuantity.
118 *
119 * @param micros
120 * The MicroProps after the quantity has been consumed. Will not be mutated.
121 * @param quantity
122 * The DecimalQuantity to be rendered. May be mutated.
123 * @param string
124 * The output string. Will be mutated.
125 */
126 static int32_t
127 microsToString(const MicroProps &micros, DecimalQuantity &quantity, NumberStringBuilder &string,
128 UErrorCode &status);
129
130 static int32_t
131 writeNumber(const MicroProps &micros, DecimalQuantity &quantity, NumberStringBuilder &string,
132 UErrorCode &status);
133
134 static int32_t
135 writeIntegerDigits(const MicroProps &micros, DecimalQuantity &quantity, NumberStringBuilder &string,
136 UErrorCode &status);
137
138 static int32_t
139 writeFractionDigits(const MicroProps &micros, DecimalQuantity &quantity, NumberStringBuilder &string,
140 UErrorCode &status);
141 };
142
143 } // namespace impl
144 } // namespace number
145 U_NAMESPACE_END
146
147
148 #endif //__NUMBER_FORMATIMPL_H__
149
150 #endif /* #if !UCONFIG_NO_FORMATTING */