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