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_PATTERNSTRING_H__
8 #define __NUMBER_PATTERNSTRING_H__
12 #include "unicode/unum.h"
13 #include "unicode/unistr.h"
14 #include "number_types.h"
15 #include "number_decimalquantity.h"
16 #include "number_decimfmtprops.h"
17 #include "number_affixutils.h"
19 U_NAMESPACE_BEGIN
namespace number
{
22 // Forward declaration
25 // Exported as U_I18N_API because it is a public member field of exported ParsedSubpatternInfo
26 struct U_I18N_API Endpoints
{
31 // Exported as U_I18N_API because it is a public member field of exported ParsedPatternInfo
32 struct U_I18N_API ParsedSubpatternInfo
{
33 uint64_t groupingSizes
= 0x0000ffffffff0000L
;
34 int32_t integerLeadingHashSigns
= 0;
35 int32_t integerTrailingHashSigns
= 0;
36 int32_t integerNumerals
= 0;
37 int32_t integerAtSigns
= 0;
38 int32_t integerTotal
= 0; // for convenience
39 int32_t fractionNumerals
= 0;
40 int32_t fractionHashSigns
= 0;
41 int32_t fractionTotal
= 0; // for convenience
42 bool hasDecimal
= false;
43 int32_t widthExceptAffixes
= 0;
44 // Note: NullableValue causes issues here with std::move.
45 bool hasPadding
= false;
46 UNumberFormatPadPosition paddingLocation
= UNUM_PAD_BEFORE_PREFIX
;
47 DecimalQuantity rounding
;
48 bool exponentHasPlusSign
= false;
49 int32_t exponentZeros
= 0;
50 bool hasPercentSign
= false;
51 bool hasPerMilleSign
= false;
52 bool hasCurrencySign
= false;
53 bool hasMinusSign
= false;
54 bool hasPlusSign
= false;
56 Endpoints prefixEndpoints
;
57 Endpoints suffixEndpoints
;
58 Endpoints paddingEndpoints
;
61 // Exported as U_I18N_API because it is needed for the unit test PatternStringTest
62 struct U_I18N_API ParsedPatternInfo
: public AffixPatternProvider
, public UMemory
{
63 UnicodeString pattern
;
64 ParsedSubpatternInfo positive
;
65 ParsedSubpatternInfo negative
;
68 : state(this->pattern
), currentSubpattern(nullptr) {}
70 ~ParsedPatternInfo() U_OVERRIDE
= default;
72 // Need to declare this explicitly because of the destructor
73 ParsedPatternInfo
& operator=(ParsedPatternInfo
&& src
) U_NOEXCEPT
= default;
75 static int32_t getLengthFromEndpoints(const Endpoints
& endpoints
);
77 char16_t charAt(int32_t flags
, int32_t index
) const U_OVERRIDE
;
79 int32_t length(int32_t flags
) const U_OVERRIDE
;
81 UnicodeString
getString(int32_t flags
) const U_OVERRIDE
;
83 bool positiveHasPlusSign() const U_OVERRIDE
;
85 bool hasNegativeSubpattern() const U_OVERRIDE
;
87 bool negativeHasMinusSign() const U_OVERRIDE
;
89 bool hasCurrencySign() const U_OVERRIDE
;
91 bool containsSymbolType(AffixPatternType type
, UErrorCode
& status
) const U_OVERRIDE
;
93 bool hasBody() const U_OVERRIDE
;
96 struct U_I18N_API ParserState
{
97 const UnicodeString
& pattern
; // reference to the parent
100 explicit ParserState(const UnicodeString
& _pattern
)
101 : pattern(_pattern
) {};
103 ParserState
& operator=(ParserState
&& src
) U_NOEXCEPT
{
104 // Leave pattern reference alone; it will continue to point to the same place in memory,
105 // which gets overwritten by ParsedPatternInfo's implicit move assignment.
114 // TODO: We don't currently do anything with the message string.
115 // This method is here as a shell for Java compatibility.
116 inline void toParseException(const char16_t* message
) { (void) message
; }
119 // NOTE: In Java, these are written as pure functions.
120 // In C++, they're written as methods.
121 // The behavior is the same.
123 // Mutable transient pointer:
124 ParsedSubpatternInfo
* currentSubpattern
;
126 // In Java, "negative == null" tells us whether or not we had a negative subpattern.
127 // In C++, we need to remember in another boolean.
128 bool fHasNegativeSubpattern
= false;
130 const Endpoints
& getEndpoints(int32_t flags
) const;
132 /** Run the recursive descent parser. */
133 void consumePattern(const UnicodeString
& patternString
, UErrorCode
& status
);
135 void consumeSubpattern(UErrorCode
& status
);
137 void consumePadding(PadPosition paddingLocation
, UErrorCode
& status
);
139 void consumeAffix(Endpoints
& endpoints
, UErrorCode
& status
);
141 void consumeLiteral(UErrorCode
& status
);
143 void consumeFormat(UErrorCode
& status
);
145 void consumeIntegerFormat(UErrorCode
& status
);
147 void consumeFractionFormat(UErrorCode
& status
);
149 void consumeExponent(UErrorCode
& status
);
151 friend class PatternParser
;
154 enum IgnoreRounding
{
155 IGNORE_ROUNDING_NEVER
= 0, IGNORE_ROUNDING_IF_CURRENCY
= 1, IGNORE_ROUNDING_ALWAYS
= 2
158 class U_I18N_API PatternParser
{
161 * Runs the recursive descent parser on the given pattern string, returning a data structure with raw information
162 * about the pattern string.
165 * To obtain a more useful form of the data, consider using {@link #parseToProperties} instead.
167 * TODO: Change argument type to const char16_t* instead of UnicodeString?
169 * @param patternString
170 * The LDML decimal format pattern (Excel-style pattern) to parse.
171 * @return The results of the parse.
173 static void parseToPatternInfo(const UnicodeString
& patternString
, ParsedPatternInfo
& patternInfo
,
177 * Parses a pattern string into a new property bag.
180 * The pattern string, like "#,##0.00"
181 * @param ignoreRounding
182 * Whether to leave out rounding information (minFrac, maxFrac, and rounding increment) when parsing the
183 * pattern. This may be desirable if a custom rounding mode, such as CurrencyUsage, is to be used
185 * @return A property bag object.
186 * @throws IllegalArgumentException
187 * If there is a syntax error in the pattern string.
189 static DecimalFormatProperties
parseToProperties(const UnicodeString
& pattern
,
190 IgnoreRounding ignoreRounding
, UErrorCode
& status
);
192 static DecimalFormatProperties
parseToProperties(const UnicodeString
& pattern
, UErrorCode
& status
);
195 * Parses a pattern string into an existing property bag. All properties that can be encoded into a pattern string
196 * will be overwritten with either their default value or with the value coming from the pattern string. Properties
197 * that cannot be encoded into a pattern string, such as rounding mode, are not modified.
200 * The pattern string, like "#,##0.00"
202 * The property bag object to overwrite.
203 * @param ignoreRounding
204 * See {@link #parseToProperties(String pattern, int ignoreRounding)}.
205 * @throws IllegalArgumentException
206 * If there was a syntax error in the pattern string.
208 static void parseToExistingProperties(const UnicodeString
& pattern
,
209 DecimalFormatProperties
& properties
,
210 IgnoreRounding ignoreRounding
, UErrorCode
& status
);
213 static void parseToExistingPropertiesImpl(const UnicodeString
& pattern
,
214 DecimalFormatProperties
& properties
,
215 IgnoreRounding ignoreRounding
, UErrorCode
& status
);
217 /** Finalizes the temporary data stored in the ParsedPatternInfo to the Properties. */
218 static void patternInfoToProperties(DecimalFormatProperties
& properties
,
219 ParsedPatternInfo
& patternInfo
, IgnoreRounding _ignoreRounding
,
223 class U_I18N_API PatternStringUtils
{
226 * Creates a pattern string from a property bag.
229 * Since pattern strings support only a subset of the functionality available in a property bag, a new property bag
230 * created from the string returned by this function may not be the same as the original property bag.
233 * The property bag to serialize.
234 * @return A pattern string approximately serializing the property bag.
236 static UnicodeString
propertiesToPatternString(const DecimalFormatProperties
& properties
,
241 * Converts a pattern between standard notation and localized notation. Localized notation means that instead of
242 * using generic placeholders in the pattern, you use the corresponding locale-specific characters instead. For
243 * example, in locale <em>fr-FR</em>, the period in the pattern "0.000" means "decimal" in standard notation (as it
244 * does in every other locale), but it means "grouping" in localized notation.
247 * A greedy string-substitution strategy is used to substitute locale symbols. If two symbols are ambiguous or have
248 * the same prefix, the result is not well-defined.
251 * Locale symbols are not allowed to contain the ASCII quote character.
254 * This method is provided for backwards compatibility and should not be used in any new code.
256 * TODO(C++): This method is not yet implemented.
259 * The pattern to convert.
261 * The symbols corresponding to the localized pattern.
263 * true to convert from standard to localized notation; false to convert from localized to standard
265 * @return The pattern expressed in the other notation.
267 static UnicodeString
convertLocalized(const UnicodeString
& input
, const DecimalFormatSymbols
& symbols
,
268 bool toLocalized
, UErrorCode
& status
);
271 * This method contains the heart of the logic for rendering LDML affix strings. It handles
272 * sign-always-shown resolution, whether to use the positive or negative subpattern, permille
273 * substitution, and plural forms for CurrencyPluralInfo.
275 static void patternInfoToStringBuilder(const AffixPatternProvider
& patternInfo
, bool isPrefix
,
276 int8_t signum
, UNumberSignDisplay signDisplay
,
277 StandardPlural::Form plural
, bool perMilleReplacesPercent
,
278 UnicodeString
& output
);
281 /** @return The number of chars inserted. */
282 static int escapePaddingString(UnicodeString input
, UnicodeString
& output
, int startIndex
,
287 } // namespace number
291 #endif //__NUMBER_PATTERNSTRING_H__
293 #endif /* #if !UCONFIG_NO_FORMATTING */