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_AFFIXUTILS_H__
8 #define __NUMBER_AFFIXUTILS_H__
11 #include "number_types.h"
12 #include "unicode/stringpiece.h"
13 #include "unicode/unistr.h"
14 #include "number_stringbuilder.h"
15 #include "unicode/uniset.h"
17 U_NAMESPACE_BEGIN
namespace number
{
20 enum AffixPatternState
{
22 STATE_FIRST_QUOTE
= 1,
23 STATE_INSIDE_QUOTE
= 2,
24 STATE_AFTER_QUOTE
= 3,
26 STATE_SECOND_CURR
= 5,
28 STATE_FOURTH_CURR
= 7,
30 STATE_OVERFLOW_CURR
= 9
33 // enum AffixPatternType defined in internals.h
38 AffixPatternState state
;
39 AffixPatternType type
;
42 : offset(0), state(STATE_BASE
) {}
44 AffixTag(int32_t offset
)
47 AffixTag(int32_t offset
, UChar32 codePoint
, AffixPatternState state
, AffixPatternType type
)
48 : offset(offset
), codePoint(codePoint
), state(state
), type(type
) {}
53 virtual ~TokenConsumer();
55 virtual void consumeToken(AffixPatternType type
, UChar32 cp
, UErrorCode
& status
) = 0;
58 // Exported as U_I18N_API because it is a base class for other exported types
59 class U_I18N_API SymbolProvider
{
61 virtual ~SymbolProvider();
63 // TODO: Could this be more efficient if it returned by reference?
64 virtual UnicodeString
getSymbol(AffixPatternType type
) const = 0;
68 * Performs manipulations on affix patterns: the prefix and suffix strings associated with a decimal
69 * format pattern. For example:
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>
79 * To manually iterate over tokens in a literal string, use the following pattern, which is designed
84 * while (AffixPatternUtils.hasNext(tag, patternString)) {
85 * tag = AffixPatternUtils.nextToken(tag, patternString);
86 * int typeOrCp = AffixPatternUtils.getTypeOrCp(tag);
88 * case AffixPatternUtils.TYPE_MINUS_SIGN:
89 * // Current token is a minus sign.
91 * case AffixPatternUtils.TYPE_PLUS_SIGN:
92 * // Current token is a plus sign.
94 * case AffixPatternUtils.TYPE_PERCENT:
95 * // Current token is a percent sign.
97 * // ... other types ...
99 * // Current token is an arbitrary code point.
100 * // The variable typeOrCp is the code point.
106 class U_I18N_API AffixUtils
{
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.
116 * @param patternString The original string whose width will be estimated.
117 * @return The length of the unescaped string.
119 static int32_t estimateLength(const UnicodeString
& patternString
, UErrorCode
& status
);
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.
125 * <p>Example input: "-$x"; example output: "'-'$x"
127 * @param input The string to be escaped.
128 * @return The resulting UnicodeString.
130 static UnicodeString
escape(const UnicodeString
& input
);
132 static Field
getFieldForType(AffixPatternType type
);
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 NumberStringBuilder at the requested location.
139 * <p>Example input: "'-'¤x"; example output: "-$x"
141 * @param affixPattern The original string to be unescaped.
142 * @param output The NumberStringBuilder to mutate with the result.
143 * @param position The index into the NumberStringBuilder to insert the string.
144 * @param provider An object to generate locale symbols.
146 static int32_t unescape(const UnicodeString
& affixPattern
, NumberStringBuilder
& output
,
147 int32_t position
, const SymbolProvider
& provider
, UErrorCode
& status
);
150 * Sames as {@link #unescape}, but only calculates the code point count. More efficient than {@link #unescape}
151 * if you only need the length but not the string itself.
153 * @param affixPattern The original string to be unescaped.
154 * @param provider An object to generate locale symbols.
155 * @return The same return value as if you called {@link #unescape}.
157 static int32_t unescapedCodePointCount(const UnicodeString
& affixPattern
,
158 const SymbolProvider
& provider
, UErrorCode
& status
);
161 * Checks whether the given affix pattern contains at least one token of the given type, which is
162 * one of the constants "TYPE_" in {@link AffixPatternUtils}.
164 * @param affixPattern The affix pattern to check.
165 * @param type The token type.
166 * @return true if the affix pattern contains the given token type; false otherwise.
168 static bool containsType(const UnicodeString
& affixPattern
, AffixPatternType type
, UErrorCode
& status
);
171 * Checks whether the specified affix pattern has any unquoted currency symbols ("¤").
173 * @param affixPattern The string to check for currency symbols.
174 * @return true if the literal has at least one unquoted currency symbol; false otherwise.
176 static bool hasCurrencySymbols(const UnicodeString
& affixPattern
, UErrorCode
& status
);
179 * Replaces all occurrences of tokens with the given type with the given replacement char.
181 * @param affixPattern The source affix pattern (does not get modified).
182 * @param type The token type.
183 * @param replacementChar The char to substitute in place of chars of the given token type.
184 * @return A string containing the new affix pattern.
186 static UnicodeString
replaceType(const UnicodeString
& affixPattern
, AffixPatternType type
,
187 char16_t replacementChar
, UErrorCode
& status
);
190 * Returns whether the given affix pattern contains only symbols and ignorables as defined by the
191 * given ignorables set.
193 static bool containsOnlySymbolsAndIgnorables(const UnicodeString
& affixPattern
,
194 const UnicodeSet
& ignorables
, UErrorCode
& status
);
197 * Iterates over the affix pattern, calling the TokenConsumer for each token.
199 static void iterateWithConsumer(const UnicodeString
& affixPattern
, TokenConsumer
& consumer
,
203 * Returns the next token from the affix pattern.
205 * @param tag A bitmask used for keeping track of state from token to token. The initial value
207 * @param patternString The affix pattern.
208 * @return The bitmask tag to pass to the next call of this method to retrieve the following token
209 * (never negative), or -1 if there were no more tokens in the affix pattern.
212 static AffixTag
nextToken(AffixTag tag
, const UnicodeString
& patternString
, UErrorCode
& status
);
215 * Returns whether the affix pattern string has any more tokens to be retrieved from a call to
216 * {@link #nextToken}.
218 * @param tag The bitmask tag of the previous token, as returned by {@link #nextToken}.
219 * @param string The affix pattern.
220 * @return true if there are more tokens to consume; false otherwise.
222 static bool hasNext(const AffixTag
& tag
, const UnicodeString
& string
);
226 * Encodes the given values into a tag struct.
227 * The order of the arguments is consistent with Java, but the order of the stored
228 * fields is not necessarily the same.
230 static inline AffixTag
makeTag(int32_t offset
, AffixPatternType type
, AffixPatternState state
,
232 return {offset
, cp
, state
, type
};
237 } // namespace number
241 #endif //__NUMBER_AFFIXUTILS_H__
243 #endif /* #if !UCONFIG_NO_FORMATTING */