1 // © 2018 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 __SOURCE_NUMBER_SKELETONS_H__
8 #define __SOURCE_NUMBER_SKELETONS_H__
10 #include "number_types.h"
11 #include "numparse_types.h"
12 #include "unicode/ucharstrie.h"
14 using icu::numparse::impl::StringSegment
;
16 U_NAMESPACE_BEGIN
namespace number
{
19 // Forward-declaration
20 struct SeenMacroProps
;
22 // namespace for enums and entrypoint functions
25 ///////////////////////////////////////////////////////////////////////////////////////
26 // NOTE: For an example of how to add a new stem to the number skeleton parser, see: //
27 // http://bugs.icu-project.org/trac/changeset/41193 //
28 ///////////////////////////////////////////////////////////////////////////////////////
31 * While parsing a skeleton, this enum records what type of option we expect to find next.
35 // Section 0: We expect whitespace or a stem, but not an option:
39 // Section 1: We might accept an option, but it is not required:
42 STATE_FRACTION_PRECISION
,
44 // Section 2: An option is required:
46 STATE_INCREMENT_PRECISION
,
48 STATE_PER_MEASURE_UNIT
,
51 STATE_NUMBERING_SYSTEM
,
56 * All possible stem literals have an entry in the StemEnum. The enum name is the kebab case stem
57 * string literal written in upper snake case.
60 * @see #SERIALIZED_STEM_TRIE
64 // Section 1: Stems that do not require an option:
74 STEM_PRECISION_INTEGER
,
75 STEM_PRECISION_UNLIMITED
,
76 STEM_PRECISION_CURRENCY_STANDARD
,
77 STEM_PRECISION_CURRENCY_CASH
,
78 STEM_ROUNDING_MODE_CEILING
,
79 STEM_ROUNDING_MODE_FLOOR
,
80 STEM_ROUNDING_MODE_DOWN
,
81 STEM_ROUNDING_MODE_UP
,
82 STEM_ROUNDING_MODE_HALF_EVEN
,
83 STEM_ROUNDING_MODE_HALF_DOWN
,
84 STEM_ROUNDING_MODE_HALF_UP
,
85 STEM_ROUNDING_MODE_UNNECESSARY
,
89 STEM_GROUP_ON_ALIGNED
,
92 STEM_UNIT_WIDTH_NARROW
,
93 STEM_UNIT_WIDTH_SHORT
,
94 STEM_UNIT_WIDTH_FULL_NAME
,
95 STEM_UNIT_WIDTH_ISO_CODE
,
96 STEM_UNIT_WIDTH_HIDDEN
,
100 STEM_SIGN_ACCOUNTING
,
101 STEM_SIGN_ACCOUNTING_ALWAYS
,
102 STEM_SIGN_EXCEPT_ZERO
,
103 STEM_SIGN_ACCOUNTING_EXCEPT_ZERO
,
107 // Section 2: Stems that DO require an option:
109 STEM_PRECISION_INCREMENT
,
111 STEM_PER_MEASURE_UNIT
,
114 STEM_NUMBERING_SYSTEM
,
119 * Creates a NumberFormatter corresponding to the given skeleton string.
121 * @param skeletonString
122 * A number skeleton string, possibly not in its shortest form.
123 * @return An UnlocalizedNumberFormatter with behavior defined by the given skeleton string.
125 UnlocalizedNumberFormatter
create(const UnicodeString
& skeletonString
, UErrorCode
& status
);
128 * Create a skeleton string corresponding to the given NumberFormatter.
131 * The NumberFormatter options object.
132 * @return A skeleton string in normalized form.
134 UnicodeString
generate(const MacroProps
& macros
, UErrorCode
& status
);
137 * Converts from a skeleton string to a MacroProps. This method contains the primary parse loop.
139 * Internal: use the create() endpoint instead of this function.
141 MacroProps
parseSkeleton(const UnicodeString
& skeletonString
, UErrorCode
& status
);
144 * Given that the current segment represents a stem, parse it and save the result.
146 * @return The next state after parsing this stem, corresponding to what subset of options to expect.
148 ParseState
parseStem(const StringSegment
& segment
, const UCharsTrie
& stemTrie
, SeenMacroProps
& seen
,
149 MacroProps
& macros
, UErrorCode
& status
);
152 * Given that the current segment represents an option, parse it and save the result.
154 * @return The next state after parsing this option, corresponding to what subset of options to
158 parseOption(ParseState stem
, const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
160 } // namespace skeleton
164 * Namespace for utility methods that convert from StemEnum to corresponding objects or enums. This
165 * applies to only the "Section 1" stems, those that are well-defined without an option.
167 namespace stem_to_object
{
169 Notation
notation(skeleton::StemEnum stem
);
171 MeasureUnit
unit(skeleton::StemEnum stem
);
173 Precision
precision(skeleton::StemEnum stem
);
175 UNumberFormatRoundingMode
roundingMode(skeleton::StemEnum stem
);
177 UGroupingStrategy
groupingStrategy(skeleton::StemEnum stem
);
179 UNumberUnitWidth
unitWidth(skeleton::StemEnum stem
);
181 UNumberSignDisplay
signDisplay(skeleton::StemEnum stem
);
183 UNumberDecimalSeparatorDisplay
decimalSeparatorDisplay(skeleton::StemEnum stem
);
185 } // namespace stem_to_object
188 * Namespace for utility methods that convert from enums to stem strings. More complex object conversions
189 * take place in the object_to_stem_string namespace.
191 namespace enum_to_stem_string
{
193 void roundingMode(UNumberFormatRoundingMode value
, UnicodeString
& sb
);
195 void groupingStrategy(UGroupingStrategy value
, UnicodeString
& sb
);
197 void unitWidth(UNumberUnitWidth value
, UnicodeString
& sb
);
199 void signDisplay(UNumberSignDisplay value
, UnicodeString
& sb
);
201 void decimalSeparatorDisplay(UNumberDecimalSeparatorDisplay value
, UnicodeString
& sb
);
203 } // namespace enum_to_stem_string
206 * Namespace for utility methods for processing stems and options that cannot be interpreted literally.
208 namespace blueprint_helpers
{
210 /** @return Whether we successfully found and parsed an exponent width option. */
211 bool parseExponentWidthOption(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
213 void generateExponentWidthOption(int32_t minExponentDigits
, UnicodeString
& sb
, UErrorCode
& status
);
215 /** @return Whether we successfully found and parsed an exponent sign option. */
216 bool parseExponentSignOption(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
218 void parseCurrencyOption(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
220 void generateCurrencyOption(const CurrencyUnit
& currency
, UnicodeString
& sb
, UErrorCode
& status
);
222 void parseMeasureUnitOption(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
224 void generateMeasureUnitOption(const MeasureUnit
& measureUnit
, UnicodeString
& sb
, UErrorCode
& status
);
226 void parseMeasurePerUnitOption(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
228 void parseFractionStem(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
230 void generateFractionStem(int32_t minFrac
, int32_t maxFrac
, UnicodeString
& sb
, UErrorCode
& status
);
232 void parseDigitsStem(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
234 void generateDigitsStem(int32_t minSig
, int32_t maxSig
, UnicodeString
& sb
, UErrorCode
& status
);
236 /** @return Whether we successfully found and parsed a frac-sig option. */
237 bool parseFracSigOption(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
239 void parseIncrementOption(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
242 generateIncrementOption(double increment
, int32_t trailingZeros
, UnicodeString
& sb
, UErrorCode
& status
);
244 void parseIntegerWidthOption(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
246 void generateIntegerWidthOption(int32_t minInt
, int32_t maxInt
, UnicodeString
& sb
, UErrorCode
& status
);
248 void parseNumberingSystemOption(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
250 void generateNumberingSystemOption(const NumberingSystem
& ns
, UnicodeString
& sb
, UErrorCode
& status
);
252 void parseScaleOption(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
254 void generateScaleOption(int32_t magnitude
, const DecNum
* arbitrary
, UnicodeString
& sb
,
257 } // namespace blueprint_helpers
260 * Class for utility methods for generating a token corresponding to each macro-prop. Each method
261 * returns whether or not a token was written to the string builder.
263 * This needs to be a class, not a namespace, so it can be friended.
265 class GeneratorHelpers
{
268 * Main skeleton generator function. Appends the normalized skeleton for the MacroProps to the given
271 * Internal: use the create() endpoint instead of this function.
273 static void generateSkeleton(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
276 static bool notation(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
278 static bool unit(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
280 static bool perUnit(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
282 static bool precision(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
284 static bool roundingMode(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
286 static bool grouping(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
288 static bool integerWidth(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
290 static bool symbols(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
292 static bool unitWidth(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
294 static bool sign(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
296 static bool decimal(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
298 static bool scale(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
303 * Struct for null-checking.
304 * In Java, we can just check the object reference. In C++, we need a different method.
306 struct SeenMacroProps
{
307 bool notation
= false;
309 bool perUnit
= false;
310 bool precision
= false;
311 bool roundingMode
= false;
312 bool grouper
= false;
314 bool integerWidth
= false;
315 bool symbols
= false;
316 bool unitWidth
= false;
318 bool decimal
= false;
323 } // namespace number
326 #endif //__SOURCE_NUMBER_SKELETONS_H__
327 #endif /* #if !UCONFIG_NO_FORMATTING */