/src/icu/source/i18n/number_affixutils.h
Line  | Count  | Source (jump to first uncovered line)  | 
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_AFFIXUTILS_H__  | 
8  |  | #define __NUMBER_AFFIXUTILS_H__  | 
9  |  |  | 
10  |  | #include <cstdint>  | 
11  |  | #include "number_types.h"  | 
12  |  | #include "unicode/stringpiece.h"  | 
13  |  | #include "unicode/unistr.h"  | 
14  |  | #include "formatted_string_builder.h"  | 
15  |  | #include "unicode/uniset.h"  | 
16  |  |  | 
17  |  | U_NAMESPACE_BEGIN namespace number { | 
18  |  | namespace impl { | 
19  |  |  | 
20  |  | enum AffixPatternState { | 
21  |  |     STATE_BASE = 0,  | 
22  |  |     STATE_FIRST_QUOTE = 1,  | 
23  |  |     STATE_INSIDE_QUOTE = 2,  | 
24  |  |     STATE_AFTER_QUOTE = 3,  | 
25  |  |     STATE_FIRST_CURR = 4,  | 
26  |  |     STATE_SECOND_CURR = 5,  | 
27  |  |     STATE_THIRD_CURR = 6,  | 
28  |  |     STATE_FOURTH_CURR = 7,  | 
29  |  |     STATE_FIFTH_CURR = 8,  | 
30  |  |     STATE_OVERFLOW_CURR = 9  | 
31  |  | };  | 
32  |  |  | 
33  |  | // enum AffixPatternType defined in internals.h  | 
34  |  |  | 
35  |  | struct AffixTag { | 
36  |  |     int32_t offset;  | 
37  |  |     UChar32 codePoint;  | 
38  |  |     AffixPatternState state;  | 
39  |  |     AffixPatternType type;  | 
40  |  |  | 
41  |  |     AffixTag()  | 
42  | 0  |             : offset(0), state(STATE_BASE) {} | 
43  |  |  | 
44  |  |     AffixTag(int32_t offset)  | 
45  | 0  |             : offset(offset) {} | 
46  |  |  | 
47  |  |     AffixTag(int32_t offset, UChar32 codePoint, AffixPatternState state, AffixPatternType type)  | 
48  | 0  |             : offset(offset), codePoint(codePoint), state(state), type(type) {} | 
49  |  | };  | 
50  |  |  | 
51  |  | class TokenConsumer { | 
52  |  |   public:  | 
53  |  |     virtual ~TokenConsumer();  | 
54  |  |  | 
55  |  |     virtual void consumeToken(AffixPatternType type, UChar32 cp, UErrorCode& status) = 0;  | 
56  |  | };  | 
57  |  |  | 
58  |  | // Exported as U_I18N_API because it is a base class for other exported types  | 
59  |  | class U_I18N_API SymbolProvider { | 
60  |  |   public:  | 
61  |  |     virtual ~SymbolProvider();  | 
62  |  |  | 
63  |  |     // TODO: Could this be more efficient if it returned by reference?  | 
64  |  |     virtual UnicodeString getSymbol(AffixPatternType type) const = 0;  | 
65  |  | };  | 
66  |  |  | 
67  |  | /**  | 
68  |  |  * Performs manipulations on affix patterns: the prefix and suffix strings associated with a decimal  | 
69  |  |  * format pattern. For example:  | 
70  |  |  *  | 
71  |  |  * <table>  | 
72  |  |  * <tr><th>Affix Pattern</th><th>Example Unescaped (Formatted) String</th></tr>  | 
73  |  |  * <tr><td>abc</td><td>abc</td></tr>  | 
74  |  |  * <tr><td>ab-</td><td>ab−</td></tr>  | 
75  |  |  * <tr><td>ab'-'</td><td>ab-</td></tr>  | 
76  |  |  * <tr><td>ab''</td><td>ab'</td></tr>  | 
77  |  |  * </table>  | 
78  |  |  *  | 
79  |  |  * To manually iterate over tokens in a literal string, use the following pattern, which is designed  | 
80  |  |  * to be efficient.  | 
81  |  |  *  | 
82  |  |  * <pre>  | 
83  |  |  * long tag = 0L;  | 
84  |  |  * while (AffixPatternUtils.hasNext(tag, patternString)) { | 
85  |  |  *   tag = AffixPatternUtils.nextToken(tag, patternString);  | 
86  |  |  *   int typeOrCp = AffixPatternUtils.getTypeOrCp(tag);  | 
87  |  |  *   switch (typeOrCp) { | 
88  |  |  *     case AffixPatternUtils.TYPE_MINUS_SIGN:  | 
89  |  |  *       // Current token is a minus sign.  | 
90  |  |  *       break;  | 
91  |  |  *     case AffixPatternUtils.TYPE_PLUS_SIGN:  | 
92  |  |  *       // Current token is a plus sign.  | 
93  |  |  *       break;  | 
94  |  |  *     case AffixPatternUtils.TYPE_PERCENT:  | 
95  |  |  *       // Current token is a percent sign.  | 
96  |  |  *       break;  | 
97  |  |  *     // ... other types ...  | 
98  |  |  *     default:  | 
99  |  |  *       // Current token is an arbitrary code point.  | 
100  |  |  *       // The variable typeOrCp is the code point.  | 
101  |  |  *       break;  | 
102  |  |  *   }  | 
103  |  |  * }  | 
104  |  |  * </pre>  | 
105  |  |  */  | 
106  |  | class U_I18N_API AffixUtils { | 
107  |  |  | 
108  |  |   public:  | 
109  |  |  | 
110  |  |     /**  | 
111  |  |      * Estimates the number of code points present in an unescaped version of the affix pattern string  | 
112  |  |      * (one that would be returned by {@link #unescape}), assuming that all interpolated symbols | 
113  |  |      * consume one code point and that currencies consume as many code points as their symbol width.  | 
114  |  |      * Used for computing padding width.  | 
115  |  |      *  | 
116  |  |      * @param patternString The original string whose width will be estimated.  | 
117  |  |      * @return The length of the unescaped string.  | 
118  |  |      */  | 
119  |  |     static int32_t estimateLength(const UnicodeString& patternString, UErrorCode& status);  | 
120  |  |  | 
121  |  |     /**  | 
122  |  |      * Takes a string and escapes (quotes) characters that have special meaning in the affix pattern  | 
123  |  |      * syntax. This function does not reverse-lookup symbols.  | 
124  |  |      *  | 
125  |  |      * <p>Example input: "-$x"; example output: "'-'$x"  | 
126  |  |      *  | 
127  |  |      * @param input The string to be escaped.  | 
128  |  |      * @return The resulting UnicodeString.  | 
129  |  |      */  | 
130  |  |     static UnicodeString escape(const UnicodeString& input);  | 
131  |  |  | 
132  |  |     static Field getFieldForType(AffixPatternType type);  | 
133  |  |  | 
134  |  |     /**  | 
135  |  |      * Executes the unescape state machine. Replaces the unquoted characters "-", "+", "%", "‰", and  | 
136  |  |      * "¤" with the corresponding symbols provided by the {@link SymbolProvider}, and inserts the | 
137  |  |      * result into the FormattedStringBuilder at the requested location.  | 
138  |  |      *  | 
139  |  |      * <p>Example input: "'-'¤x"; example output: "-$x"  | 
140  |  |      *  | 
141  |  |      * @param affixPattern The original string to be unescaped.  | 
142  |  |      * @param output The FormattedStringBuilder to mutate with the result.  | 
143  |  |      * @param position The index into the FormattedStringBuilder to insert the string.  | 
144  |  |      * @param provider An object to generate locale symbols.  | 
145  |  |      */  | 
146  |  |     static int32_t unescape(const UnicodeString& affixPattern, FormattedStringBuilder& output,  | 
147  |  |                             int32_t position, const SymbolProvider& provider, Field field,  | 
148  |  |                             UErrorCode& status);  | 
149  |  |  | 
150  |  |     /**  | 
151  |  |    * Sames as {@link #unescape}, but only calculates the code point count.  More efficient than {@link #unescape} | 
152  |  |    * if you only need the length but not the string itself.  | 
153  |  |      *  | 
154  |  |      * @param affixPattern The original string to be unescaped.  | 
155  |  |      * @param provider An object to generate locale symbols.  | 
156  |  |      * @return The same return value as if you called {@link #unescape}. | 
157  |  |      */  | 
158  |  |     static int32_t unescapedCodePointCount(const UnicodeString& affixPattern,  | 
159  |  |                                            const SymbolProvider& provider, UErrorCode& status);  | 
160  |  |  | 
161  |  |     /**  | 
162  |  |      * Checks whether the given affix pattern contains at least one token of the given type, which is  | 
163  |  |      * one of the constants "TYPE_" in {@link AffixPatternUtils}. | 
164  |  |      *  | 
165  |  |      * @param affixPattern The affix pattern to check.  | 
166  |  |      * @param type The token type.  | 
167  |  |      * @return true if the affix pattern contains the given token type; false otherwise.  | 
168  |  |      */  | 
169  |  |     static bool containsType(const UnicodeString& affixPattern, AffixPatternType type, UErrorCode& status);  | 
170  |  |  | 
171  |  |     /**  | 
172  |  |      * Checks whether the specified affix pattern has any unquoted currency symbols ("¤"). | 
173  |  |      *  | 
174  |  |      * @param affixPattern The string to check for currency symbols.  | 
175  |  |      * @return true if the literal has at least one unquoted currency symbol; false otherwise.  | 
176  |  |      */  | 
177  |  |     static bool hasCurrencySymbols(const UnicodeString& affixPattern, UErrorCode& status);  | 
178  |  |  | 
179  |  |     /**  | 
180  |  |      * Replaces all occurrences of tokens with the given type with the given replacement char.  | 
181  |  |      *  | 
182  |  |      * @param affixPattern The source affix pattern (does not get modified).  | 
183  |  |      * @param type The token type.  | 
184  |  |      * @param replacementChar The char to substitute in place of chars of the given token type.  | 
185  |  |      * @return A string containing the new affix pattern.  | 
186  |  |      */  | 
187  |  |     static UnicodeString replaceType(const UnicodeString& affixPattern, AffixPatternType type,  | 
188  |  |                                      char16_t replacementChar, UErrorCode& status);  | 
189  |  |  | 
190  |  |     /**  | 
191  |  |      * Returns whether the given affix pattern contains only symbols and ignorables as defined by the  | 
192  |  |      * given ignorables set.  | 
193  |  |      */  | 
194  |  |     static bool containsOnlySymbolsAndIgnorables(const UnicodeString& affixPattern,  | 
195  |  |                                                  const UnicodeSet& ignorables, UErrorCode& status);  | 
196  |  |  | 
197  |  |     /**  | 
198  |  |      * Iterates over the affix pattern, calling the TokenConsumer for each token.  | 
199  |  |      */  | 
200  |  |     static void iterateWithConsumer(const UnicodeString& affixPattern, TokenConsumer& consumer,  | 
201  |  |                                     UErrorCode& status);  | 
202  |  |  | 
203  |  |     /**  | 
204  |  |      * Returns the next token from the affix pattern.  | 
205  |  |      *  | 
206  |  |      * @param tag A bitmask used for keeping track of state from token to token. The initial value  | 
207  |  |      *     should be 0L.  | 
208  |  |      * @param patternString The affix pattern.  | 
209  |  |      * @return The bitmask tag to pass to the next call of this method to retrieve the following token  | 
210  |  |      *     (never negative), or -1 if there were no more tokens in the affix pattern.  | 
211  |  |      * @see #hasNext  | 
212  |  |      */  | 
213  |  |     static AffixTag nextToken(AffixTag tag, const UnicodeString& patternString, UErrorCode& status);  | 
214  |  |  | 
215  |  |     /**  | 
216  |  |      * Returns whether the affix pattern string has any more tokens to be retrieved from a call to  | 
217  |  |      * {@link #nextToken}. | 
218  |  |      *  | 
219  |  |      * @param tag The bitmask tag of the previous token, as returned by {@link #nextToken}. | 
220  |  |      * @param string The affix pattern.  | 
221  |  |      * @return true if there are more tokens to consume; false otherwise.  | 
222  |  |      */  | 
223  |  |     static bool hasNext(const AffixTag& tag, const UnicodeString& string);  | 
224  |  |  | 
225  |  |   private:  | 
226  |  |     /**  | 
227  |  |      * Encodes the given values into a tag struct.  | 
228  |  |      * The order of the arguments is consistent with Java, but the order of the stored  | 
229  |  |      * fields is not necessarily the same.  | 
230  |  |      */  | 
231  |  |     static inline AffixTag makeTag(int32_t offset, AffixPatternType type, AffixPatternState state,  | 
232  | 0  |                                    UChar32 cp) { | 
233  | 0  |         return {offset, cp, state, type}; | 
234  | 0  |     }  | 
235  |  | };  | 
236  |  |  | 
237  |  | } // namespace impl  | 
238  |  | } // namespace number  | 
239  |  | U_NAMESPACE_END  | 
240  |  |  | 
241  |  |  | 
242  |  | #endif //__NUMBER_AFFIXUTILS_H__  | 
243  |  |  | 
244  |  | #endif /* #if !UCONFIG_NO_FORMATTING */  |