1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #include "unicode/utypes.h"
6 #if !UCONFIG_NO_FORMATTING
7 #ifndef __NUMBER_FORMATIMPL_H__
8 #define __NUMBER_FORMATIMPL_H__
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"
19 U_NAMESPACE_BEGIN
namespace number
{
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.
26 class NumberFormatterImpl
: public UMemory
{
29 * Builds a "safe" MicroPropsGenerator, which is thread-safe and can be used repeatedly.
30 * The caller owns the returned NumberFormatterImpl.
32 static NumberFormatterImpl
*fromMacros(const MacroProps
¯os
, UErrorCode
&status
);
35 * Builds and evaluates an "unsafe" MicroPropsGenerator, which is cheaper but can be used only once.
38 applyStatic(const MacroProps
¯os
, DecimalQuantity
&inValue
, NumberStringBuilder
&outString
,
42 * Prints only the prefix and suffix; used for DecimalFormat getters.
44 * @return The index into the output at which the prefix ends and the suffix starts; in other words,
47 static int32_t getPrefixSuffixStatic(const MacroProps
& macros
, int8_t signum
,
48 StandardPlural::Form plural
, NumberStringBuilder
& outString
,
52 * Evaluates the "safe" MicroPropsGenerator created by "fromMacros".
54 void apply(DecimalQuantity
& inValue
, NumberStringBuilder
& outString
, UErrorCode
& status
) const;
57 * Like getPrefixSuffixStatic() but uses the safe compiled object.
59 int32_t getPrefixSuffix(int8_t signum
, StandardPlural::Form plural
, NumberStringBuilder
& outString
,
60 UErrorCode
& status
) const;
63 // Head of the MicroPropsGenerator linked list:
64 const MicroPropsGenerator
*fMicroPropsGenerator
= nullptr;
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
;
80 // Value objects possibly used by the number formatting pipeline:
82 CurrencySymbols fCurrencySymbols
;
86 NumberFormatterImpl(const MacroProps
¯os
, bool safe
, UErrorCode
&status
);
88 void applyUnsafe(DecimalQuantity
&inValue
, NumberStringBuilder
&outString
, UErrorCode
&status
);
90 int32_t getPrefixSuffixUnsafe(int8_t signum
, StandardPlural::Form plural
,
91 NumberStringBuilder
& outString
, UErrorCode
& status
);
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.
98 resolvePluralRules(const PluralRules
*rulesPtr
, const Locale
&locale
, UErrorCode
&status
);
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.
105 * @see MicroPropsGenerator
107 * The {@link MacroProps} to consume. This method does not mutate the MacroProps instance.
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.
113 const MicroPropsGenerator
*
114 macrosToMicroGenerator(const MacroProps
¯os
, bool safe
, UErrorCode
&status
);
117 * Synthesizes the output string from a MicroProps and DecimalQuantity.
120 * The MicroProps after the quantity has been consumed. Will not be mutated.
122 * The DecimalQuantity to be rendered. May be mutated.
124 * The output string. Will be mutated.
127 microsToString(const MicroProps
µs
, DecimalQuantity
&quantity
, NumberStringBuilder
&string
,
131 writeNumber(const MicroProps
µs
, DecimalQuantity
&quantity
, NumberStringBuilder
&string
,
135 writeIntegerDigits(const MicroProps
µs
, DecimalQuantity
&quantity
, NumberStringBuilder
&string
,
139 writeFractionDigits(const MicroProps
µs
, DecimalQuantity
&quantity
, NumberStringBuilder
&string
,
144 } // namespace number
148 #endif //__NUMBER_FORMATIMPL_H__
150 #endif /* #if !UCONFIG_NO_FORMATTING */