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"
13 #include "string_segment.h"
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(
126 const UnicodeString
& skeletonString
, UParseError
* perror
, UErrorCode
& status
);
129 * Create a skeleton string corresponding to the given NumberFormatter.
132 * The NumberFormatter options object.
133 * @return A skeleton string in normalized form.
135 UnicodeString
generate(const MacroProps
& macros
, UErrorCode
& status
);
138 * Converts from a skeleton string to a MacroProps. This method contains the primary parse loop.
140 * Internal: use the create() endpoint instead of this function.
142 MacroProps
parseSkeleton(const UnicodeString
& skeletonString
, int32_t& errOffset
, UErrorCode
& status
);
145 * Given that the current segment represents a stem, parse it and save the result.
147 * @return The next state after parsing this stem, corresponding to what subset of options to expect.
149 ParseState
parseStem(const StringSegment
& segment
, const UCharsTrie
& stemTrie
, SeenMacroProps
& seen
,
150 MacroProps
& macros
, UErrorCode
& status
);
153 * Given that the current segment represents an option, parse it and save the result.
155 * @return The next state after parsing this option, corresponding to what subset of options to
159 parseOption(ParseState stem
, const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
161 } // namespace skeleton
165 * Namespace for utility methods that convert from StemEnum to corresponding objects or enums. This
166 * applies to only the "Section 1" stems, those that are well-defined without an option.
168 namespace stem_to_object
{
170 Notation
notation(skeleton::StemEnum stem
);
172 MeasureUnit
unit(skeleton::StemEnum stem
);
174 Precision
precision(skeleton::StemEnum stem
);
176 UNumberFormatRoundingMode
roundingMode(skeleton::StemEnum stem
);
178 UNumberGroupingStrategy
groupingStrategy(skeleton::StemEnum stem
);
180 UNumberUnitWidth
unitWidth(skeleton::StemEnum stem
);
182 UNumberSignDisplay
signDisplay(skeleton::StemEnum stem
);
184 UNumberDecimalSeparatorDisplay
decimalSeparatorDisplay(skeleton::StemEnum stem
);
186 } // namespace stem_to_object
189 * Namespace for utility methods that convert from enums to stem strings. More complex object conversions
190 * take place in the object_to_stem_string namespace.
192 namespace enum_to_stem_string
{
194 void roundingMode(UNumberFormatRoundingMode value
, UnicodeString
& sb
);
196 void groupingStrategy(UNumberGroupingStrategy value
, UnicodeString
& sb
);
198 void unitWidth(UNumberUnitWidth value
, UnicodeString
& sb
);
200 void signDisplay(UNumberSignDisplay value
, UnicodeString
& sb
);
202 void decimalSeparatorDisplay(UNumberDecimalSeparatorDisplay value
, UnicodeString
& sb
);
204 } // namespace enum_to_stem_string
207 * Namespace for utility methods for processing stems and options that cannot be interpreted literally.
209 namespace blueprint_helpers
{
211 /** @return Whether we successfully found and parsed an exponent width option. */
212 bool parseExponentWidthOption(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
214 void generateExponentWidthOption(int32_t minExponentDigits
, UnicodeString
& sb
, UErrorCode
& status
);
216 /** @return Whether we successfully found and parsed an exponent sign option. */
217 bool parseExponentSignOption(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
219 void parseCurrencyOption(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
221 void generateCurrencyOption(const CurrencyUnit
& currency
, UnicodeString
& sb
, UErrorCode
& status
);
223 void parseMeasureUnitOption(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
225 void generateMeasureUnitOption(const MeasureUnit
& measureUnit
, UnicodeString
& sb
, UErrorCode
& status
);
227 void parseMeasurePerUnitOption(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
229 void parseFractionStem(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
231 void generateFractionStem(int32_t minFrac
, int32_t maxFrac
, UnicodeString
& sb
, UErrorCode
& status
);
233 void parseDigitsStem(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
235 void generateDigitsStem(int32_t minSig
, int32_t maxSig
, UnicodeString
& sb
, UErrorCode
& status
);
237 /** @return Whether we successfully found and parsed a frac-sig option. */
238 bool parseFracSigOption(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
240 void parseIncrementOption(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
243 generateIncrementOption(double increment
, int32_t trailingZeros
, UnicodeString
& sb
, UErrorCode
& status
);
245 void parseIntegerWidthOption(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
247 void generateIntegerWidthOption(int32_t minInt
, int32_t maxInt
, UnicodeString
& sb
, UErrorCode
& status
);
249 void parseNumberingSystemOption(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
251 void generateNumberingSystemOption(const NumberingSystem
& ns
, UnicodeString
& sb
, UErrorCode
& status
);
253 void parseScaleOption(const StringSegment
& segment
, MacroProps
& macros
, UErrorCode
& status
);
255 void generateScaleOption(int32_t magnitude
, const DecNum
* arbitrary
, UnicodeString
& sb
,
258 } // namespace blueprint_helpers
261 * Class for utility methods for generating a token corresponding to each macro-prop. Each method
262 * returns whether or not a token was written to the string builder.
264 * This needs to be a class, not a namespace, so it can be friended.
266 class GeneratorHelpers
{
269 * Main skeleton generator function. Appends the normalized skeleton for the MacroProps to the given
272 * Internal: use the create() endpoint instead of this function.
274 static void generateSkeleton(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
277 static bool notation(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
279 static bool unit(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
281 static bool perUnit(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
283 static bool precision(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
285 static bool roundingMode(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
287 static bool grouping(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
289 static bool integerWidth(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
291 static bool symbols(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
293 static bool unitWidth(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
295 static bool sign(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
297 static bool decimal(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
299 static bool scale(const MacroProps
& macros
, UnicodeString
& sb
, UErrorCode
& status
);
304 * Struct for null-checking.
305 * In Java, we can just check the object reference. In C++, we need a different method.
307 struct SeenMacroProps
{
308 bool notation
= false;
310 bool perUnit
= false;
311 bool precision
= false;
312 bool roundingMode
= false;
313 bool grouper
= false;
315 bool integerWidth
= false;
316 bool symbols
= false;
317 bool unitWidth
= false;
319 bool decimal
= false;
324 } // namespace number
327 #endif //__SOURCE_NUMBER_SKELETONS_H__
328 #endif /* #if !UCONFIG_NO_FORMATTING */