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
, Field field
,
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.
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}.
158 static int32_t unescapedCodePointCount(const UnicodeString
& affixPattern
,
159 const SymbolProvider
& provider
, UErrorCode
& status
);
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}.
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.
169 static bool containsType(const UnicodeString
& affixPattern
, AffixPatternType type
, UErrorCode
& status
);
172 * Checks whether the specified affix pattern has any unquoted currency symbols ("¤").
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.
177 static bool hasCurrencySymbols(const UnicodeString
& affixPattern
, UErrorCode
& status
);
180 * Replaces all occurrences of tokens with the given type with the given replacement char.
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.
187 static UnicodeString
replaceType(const UnicodeString
& affixPattern
, AffixPatternType type
,
188 char16_t replacementChar
, UErrorCode
& status
);
191 * Returns whether the given affix pattern contains only symbols and ignorables as defined by the
192 * given ignorables set.
194 static bool containsOnlySymbolsAndIgnorables(const UnicodeString
& affixPattern
,
195 const UnicodeSet
& ignorables
, UErrorCode
& status
);
198 * Iterates over the affix pattern, calling the TokenConsumer for each token.
200 static void iterateWithConsumer(const UnicodeString
& affixPattern
, TokenConsumer
& consumer
,
204 * Returns the next token from the affix pattern.
206 * @param tag A bitmask used for keeping track of state from token to token. The initial value
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.
213 static AffixTag
nextToken(AffixTag tag
, const UnicodeString
& patternString
, UErrorCode
& status
);
216 * Returns whether the affix pattern string has any more tokens to be retrieved from a call to
217 * {@link #nextToken}.
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.
223 static bool hasNext(const AffixTag
& tag
, const UnicodeString
& string
);
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.
231 static inline AffixTag
makeTag(int32_t offset
, AffixPatternType type
, AffixPatternState state
,
233 return {offset
, cp
, state
, type
};
238 } // namespace number
242 #endif //__NUMBER_AFFIXUTILS_H__
244 #endif /* #if !UCONFIG_NO_FORMATTING */