• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PATTERNSTRING_H__
8 #define __NUMBER_PATTERNSTRING_H__
9 
10 
11 #include <cstdint>
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"
18 
19 U_NAMESPACE_BEGIN namespace number {
20 namespace impl {
21 
22 // Forward declaration
23 class PatternParser;
24 
25 // Note: the order of fields in this enum matters for parsing.
26 enum PatternSignType {
27     /** Render using normal positive subpattern rules */
28     PATTERN_SIGN_TYPE_POS,
29     /** Render using rules to force the display of a plus sign */
30     PATTERN_SIGN_TYPE_POS_SIGN,
31     /** Render using negative subpattern rules */
32     PATTERN_SIGN_TYPE_NEG,
33     /** Count for looping over the possibilities */
34     PATTERN_SIGN_TYPE_COUNT
35 };
36 
37 // Exported as U_I18N_API because it is a public member field of exported ParsedSubpatternInfo
38 struct U_I18N_API Endpoints {
39     int32_t start = 0;
40     int32_t end = 0;
41 };
42 
43 // Exported as U_I18N_API because it is a public member field of exported ParsedPatternInfo
44 struct U_I18N_API ParsedSubpatternInfo {
45     uint64_t groupingSizes = 0x0000ffffffff0000L;
46     int32_t integerLeadingHashSigns = 0;
47     int32_t integerTrailingHashSigns = 0;
48     int32_t integerNumerals = 0;
49     int32_t integerAtSigns = 0;
50     int32_t integerTotal = 0; // for convenience
51     int32_t fractionNumerals = 0;
52     int32_t fractionHashSigns = 0;
53     int32_t fractionTotal = 0; // for convenience
54     bool hasDecimal = false;
55     int32_t widthExceptAffixes = 0;
56     // Note: NullableValue causes issues here with std::move.
57     bool hasPadding = false;
58     UNumberFormatPadPosition paddingLocation = UNUM_PAD_BEFORE_PREFIX;
59     DecimalQuantity rounding;
60     bool exponentHasPlusSign = false;
61     int32_t exponentZeros = 0;
62     bool hasPercentSign = false;
63     bool hasPerMilleSign = false;
64     bool hasCurrencySign = false;
65     bool hasCurrencyDecimal = false;
66     bool hasMinusSign = false;
67     bool hasPlusSign = false;
68 
69     Endpoints prefixEndpoints;
70     Endpoints suffixEndpoints;
71     Endpoints paddingEndpoints;
72 };
73 
74 // Exported as U_I18N_API because it is needed for the unit test PatternStringTest
75 struct U_I18N_API ParsedPatternInfo : public AffixPatternProvider, public UMemory {
76     UnicodeString pattern;
77     ParsedSubpatternInfo positive;
78     ParsedSubpatternInfo negative;
79 
ParsedPatternInfoParsedPatternInfo80     ParsedPatternInfo()
81             : state(this->pattern), currentSubpattern(nullptr) {}
82 
83     ~ParsedPatternInfo() override = default;
84 
85     // Need to declare this explicitly because of the destructor
86     ParsedPatternInfo& operator=(ParsedPatternInfo&& src) noexcept = default;
87 
88     static int32_t getLengthFromEndpoints(const Endpoints& endpoints);
89 
90     char16_t charAt(int32_t flags, int32_t index) const override;
91 
92     int32_t length(int32_t flags) const override;
93 
94     UnicodeString getString(int32_t flags) const override;
95 
96     bool positiveHasPlusSign() const override;
97 
98     bool hasNegativeSubpattern() const override;
99 
100     bool negativeHasMinusSign() const override;
101 
102     bool hasCurrencySign() const override;
103 
104     bool containsSymbolType(AffixPatternType type, UErrorCode& status) const override;
105 
106     bool hasBody() const override;
107 
108     bool currencyAsDecimal() const override;
109 
110   private:
111     struct U_I18N_API ParserState {
112         const UnicodeString& pattern; // reference to the parent
113         int32_t offset = 0;
114 
ParserStateParsedPatternInfo::ParserState115         explicit ParserState(const UnicodeString& _pattern)
116                 : pattern(_pattern) {}
117 
118         ParserState& operator=(ParserState&& src) noexcept {
119             // Leave pattern reference alone; it will continue to point to the same place in memory,
120             // which gets overwritten by ParsedPatternInfo's implicit move assignment.
121             offset = src.offset;
122             return *this;
123         }
124 
125         /** Returns the next code point, or -1 if string is too short. */
126         UChar32 peek();
127 
128         /** Returns the code point after the next code point, or -1 if string is too short. */
129         UChar32 peek2();
130 
131         /** Returns the next code point and then steps forward. */
132         UChar32 next();
133 
134         // TODO: We don't currently do anything with the message string.
135         // This method is here as a shell for Java compatibility.
toParseExceptionParsedPatternInfo::ParserState136         inline void toParseException(const char16_t* message) { (void) message; }
137     } state;
138 
139     // NOTE: In Java, these are written as pure functions.
140     // In C++, they're written as methods.
141     // The behavior is the same.
142 
143     // Mutable transient pointer:
144     ParsedSubpatternInfo* currentSubpattern;
145 
146     // In Java, "negative == null" tells us whether or not we had a negative subpattern.
147     // In C++, we need to remember in another boolean.
148     bool fHasNegativeSubpattern = false;
149 
150     const Endpoints& getEndpoints(int32_t flags) const;
151 
152     /** Run the recursive descent parser. */
153     void consumePattern(const UnicodeString& patternString, UErrorCode& status);
154 
155     void consumeSubpattern(UErrorCode& status);
156 
157     void consumePadding(PadPosition paddingLocation, UErrorCode& status);
158 
159     void consumeAffix(Endpoints& endpoints, UErrorCode& status);
160 
161     void consumeLiteral(UErrorCode& status);
162 
163     void consumeFormat(UErrorCode& status);
164 
165     void consumeIntegerFormat(UErrorCode& status);
166 
167     void consumeFractionFormat(UErrorCode& status);
168 
169     void consumeExponent(UErrorCode& status);
170 
171     friend class PatternParser;
172 };
173 
174 enum IgnoreRounding {
175     IGNORE_ROUNDING_NEVER = 0, IGNORE_ROUNDING_IF_CURRENCY = 1, IGNORE_ROUNDING_ALWAYS = 2
176 };
177 
178 class U_I18N_API PatternParser {
179   public:
180     /**
181      * Runs the recursive descent parser on the given pattern string, returning a data structure with raw information
182      * about the pattern string.
183      *
184      * <p>
185      * To obtain a more useful form of the data, consider using {@link #parseToProperties} instead.
186      *
187      * TODO: Change argument type to const char16_t* instead of UnicodeString?
188      *
189      * @param patternString
190      *            The LDML decimal format pattern (Excel-style pattern) to parse.
191      * @return The results of the parse.
192      */
193     static void parseToPatternInfo(const UnicodeString& patternString, ParsedPatternInfo& patternInfo,
194                                    UErrorCode& status);
195 
196     /**
197      * Parses a pattern string into a new property bag.
198      *
199      * @param pattern
200      *            The pattern string, like "#,##0.00"
201      * @param ignoreRounding
202      *            Whether to leave out rounding information (minFrac, maxFrac, and rounding increment) when parsing the
203      *            pattern. This may be desirable if a custom rounding mode, such as CurrencyUsage, is to be used
204      *            instead.
205      * @return A property bag object.
206      * @throws IllegalArgumentException
207      *             If there is a syntax error in the pattern string.
208      */
209     static DecimalFormatProperties parseToProperties(const UnicodeString& pattern,
210                                                      IgnoreRounding ignoreRounding, UErrorCode& status);
211 
212     static DecimalFormatProperties parseToProperties(const UnicodeString& pattern, UErrorCode& status);
213 
214     /**
215      * Parses a pattern string into an existing property bag. All properties that can be encoded into a pattern string
216      * will be overwritten with either their default value or with the value coming from the pattern string. Properties
217      * that cannot be encoded into a pattern string, such as rounding mode, are not modified.
218      *
219      * @param pattern
220      *            The pattern string, like "#,##0.00"
221      * @param properties
222      *            The property bag object to overwrite.
223      * @param ignoreRounding
224      *            See {@link #parseToProperties(String pattern, int ignoreRounding)}.
225      * @throws IllegalArgumentException
226      *             If there was a syntax error in the pattern string.
227      */
228     static void parseToExistingProperties(const UnicodeString& pattern,
229                                           DecimalFormatProperties& properties,
230                                           IgnoreRounding ignoreRounding, UErrorCode& status);
231 
232   private:
233     static void parseToExistingPropertiesImpl(const UnicodeString& pattern,
234                                               DecimalFormatProperties& properties,
235                                               IgnoreRounding ignoreRounding, UErrorCode& status);
236 
237     /** Finalizes the temporary data stored in the ParsedPatternInfo to the Properties. */
238     static void patternInfoToProperties(DecimalFormatProperties& properties,
239                                         ParsedPatternInfo& patternInfo, IgnoreRounding _ignoreRounding,
240                                         UErrorCode& status);
241 };
242 
243 class U_I18N_API PatternStringUtils {
244   public:
245     /**
246      * Determine whether a given roundingIncrement should be ignored for formatting
247      * based on the current maxFrac value (maximum fraction digits). For example a
248      * roundingIncrement of 0.01 should be ignored if maxFrac is 1, but not if maxFrac
249      * is 2 or more. Note that roundingIncrements are rounded up in significance, so
250      * a roundingIncrement of 0.006 is treated like 0.01 for this determination, i.e.
251      * it should not be ignored if maxFrac is 2 or more (but a roundingIncrement of
252      * 0.005 is treated like 0.001 for significance).
253      *
254      * This test is needed for both NumberPropertyMapper::oldToNew and
255      * PatternStringUtils::propertiesToPatternString. In Java it cannot be
256      * exported by NumberPropertyMapper (package private) so it is in
257      * PatternStringUtils, do the same in C.
258      *
259      * @param roundIncr
260      *            The roundingIncrement to be checked. Must be non-zero.
261      * @param maxFrac
262      *            The current maximum fraction digits value.
263      * @return true if roundIncr should be ignored for formatting.
264      */
265      static bool ignoreRoundingIncrement(double roundIncr, int32_t maxFrac);
266 
267     /**
268      * Creates a pattern string from a property bag.
269      *
270      * <p>
271      * Since pattern strings support only a subset of the functionality available in a property bag, a new property bag
272      * created from the string returned by this function may not be the same as the original property bag.
273      *
274      * @param properties
275      *            The property bag to serialize.
276      * @return A pattern string approximately serializing the property bag.
277      */
278     static UnicodeString propertiesToPatternString(const DecimalFormatProperties& properties,
279                                                    UErrorCode& status);
280 
281 
282     /**
283      * Converts a pattern between standard notation and localized notation. Localized notation means that instead of
284      * using generic placeholders in the pattern, you use the corresponding locale-specific characters instead. For
285      * example, in locale <em>fr-FR</em>, the period in the pattern "0.000" means "decimal" in standard notation (as it
286      * does in every other locale), but it means "grouping" in localized notation.
287      *
288      * <p>
289      * A greedy string-substitution strategy is used to substitute locale symbols. If two symbols are ambiguous or have
290      * the same prefix, the result is not well-defined.
291      *
292      * <p>
293      * Locale symbols are not allowed to contain the ASCII quote character.
294      *
295      * <p>
296      * This method is provided for backwards compatibility and should not be used in any new code.
297      *
298      * TODO(C++): This method is not yet implemented.
299      *
300      * @param input
301      *            The pattern to convert.
302      * @param symbols
303      *            The symbols corresponding to the localized pattern.
304      * @param toLocalized
305      *            true to convert from standard to localized notation; false to convert from localized to standard
306      *            notation.
307      * @return The pattern expressed in the other notation.
308      */
309     static UnicodeString convertLocalized(const UnicodeString& input, const DecimalFormatSymbols& symbols,
310                                           bool toLocalized, UErrorCode& status);
311 
312     /**
313      * This method contains the heart of the logic for rendering LDML affix strings. It handles
314      * sign-always-shown resolution, whether to use the positive or negative subpattern, permille
315      * substitution, and plural forms for CurrencyPluralInfo.
316      */
317     static void patternInfoToStringBuilder(const AffixPatternProvider& patternInfo, bool isPrefix,
318                                            PatternSignType patternSignType,
319                                            bool approximately,
320                                            StandardPlural::Form plural,
321                                            bool perMilleReplacesPercent,
322                                            bool dropCurrencySymbols,
323                                            UnicodeString& output);
324 
325     static PatternSignType resolveSignDisplay(UNumberSignDisplay signDisplay, Signum signum);
326 
327   private:
328     /** @return The number of chars inserted. */
329     static int escapePaddingString(UnicodeString input, UnicodeString& output, int startIndex,
330                                    UErrorCode& status);
331 };
332 
333 } // namespace impl
334 } // namespace number
335 U_NAMESPACE_END
336 
337 
338 #endif //__NUMBER_PATTERNSTRING_H__
339 
340 #endif /* #if !UCONFIG_NO_FORMATTING */
341