Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/intl/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 "number_stringbuilder.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 NumberStringBuilder 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 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.
145
     */
146
    static int32_t unescape(const UnicodeString& affixPattern, NumberStringBuilder& output,
147
                            int32_t position, const SymbolProvider& provider, UErrorCode& status);
148
149
    /**
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.
152
     *
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}.
156
     */
157
    static int32_t unescapedCodePointCount(const UnicodeString& affixPattern,
158
                                           const SymbolProvider& provider, UErrorCode& status);
159
160
    /**
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}.
163
     *
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.
167
     */
168
    static bool containsType(const UnicodeString& affixPattern, AffixPatternType type, UErrorCode& status);
169
170
    /**
171
     * Checks whether the specified affix pattern has any unquoted currency symbols ("¤").
172
     *
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.
175
     */
176
    static bool hasCurrencySymbols(const UnicodeString& affixPattern, UErrorCode& status);
177
178
    /**
179
     * Replaces all occurrences of tokens with the given type with the given replacement char.
180
     *
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.
185
     */
186
    static UnicodeString replaceType(const UnicodeString& affixPattern, AffixPatternType type,
187
                                     char16_t replacementChar, UErrorCode& status);
188
189
    /**
190
     * Returns whether the given affix pattern contains only symbols and ignorables as defined by the
191
     * given ignorables set.
192
     */
193
    static bool containsOnlySymbolsAndIgnorables(const UnicodeString& affixPattern,
194
                                                 const UnicodeSet& ignorables, UErrorCode& status);
195
196
    /**
197
     * Iterates over the affix pattern, calling the TokenConsumer for each token.
198
     */
199
    static void iterateWithConsumer(const UnicodeString& affixPattern, TokenConsumer& consumer,
200
                                    UErrorCode& status);
201
202
    /**
203
     * Returns the next token from the affix pattern.
204
     *
205
     * @param tag A bitmask used for keeping track of state from token to token. The initial value
206
     *     should be 0L.
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.
210
     * @see #hasNext
211
     */
212
    static AffixTag nextToken(AffixTag tag, const UnicodeString& patternString, UErrorCode& status);
213
214
    /**
215
     * Returns whether the affix pattern string has any more tokens to be retrieved from a call to
216
     * {@link #nextToken}.
217
     *
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.
221
     */
222
    static bool hasNext(const AffixTag& tag, const UnicodeString& string);
223
224
  private:
225
    /**
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.
229
     */
230
    static inline AffixTag makeTag(int32_t offset, AffixPatternType type, AffixPatternState state,
231
0
                                   UChar32 cp) {
232
0
        return {offset, cp, state, type};
233
0
    }
234
};
235
236
} // namespace impl
237
} // namespace number
238
U_NAMESPACE_END
239
240
241
#endif //__NUMBER_AFFIXUTILS_H__
242
243
#endif /* #if !UCONFIG_NO_FORMATTING */