2017-09-27 00:25:20 +00:00
|
|
|
|
// © 2017 and later: Unicode, Inc. and others.
|
|
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
|
|
|
|
|
2017-10-04 22:51:06 +00:00
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
2018-04-23 23:02:26 +00:00
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
2017-09-27 05:31:57 +00:00
|
|
|
|
#ifndef __NUMBER_AFFIXUTILS_H__
|
|
|
|
|
#define __NUMBER_AFFIXUTILS_H__
|
2017-09-27 00:25:20 +00:00
|
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include "number_types.h"
|
|
|
|
|
#include "unicode/stringpiece.h"
|
|
|
|
|
#include "unicode/unistr.h"
|
|
|
|
|
#include "number_stringbuilder.h"
|
2018-04-24 01:25:05 +00:00
|
|
|
|
#include "unicode/uniset.h"
|
2017-09-27 00:25:20 +00:00
|
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN namespace number {
|
|
|
|
|
namespace impl {
|
|
|
|
|
|
|
|
|
|
enum AffixPatternState {
|
|
|
|
|
STATE_BASE = 0,
|
|
|
|
|
STATE_FIRST_QUOTE = 1,
|
|
|
|
|
STATE_INSIDE_QUOTE = 2,
|
|
|
|
|
STATE_AFTER_QUOTE = 3,
|
|
|
|
|
STATE_FIRST_CURR = 4,
|
|
|
|
|
STATE_SECOND_CURR = 5,
|
|
|
|
|
STATE_THIRD_CURR = 6,
|
|
|
|
|
STATE_FOURTH_CURR = 7,
|
|
|
|
|
STATE_FIFTH_CURR = 8,
|
|
|
|
|
STATE_OVERFLOW_CURR = 9
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// enum AffixPatternType defined in internals.h
|
|
|
|
|
|
|
|
|
|
struct AffixTag {
|
|
|
|
|
int32_t offset;
|
|
|
|
|
UChar32 codePoint;
|
|
|
|
|
AffixPatternState state;
|
|
|
|
|
AffixPatternType type;
|
|
|
|
|
|
2018-02-10 14:29:26 +00:00
|
|
|
|
AffixTag()
|
|
|
|
|
: offset(0), state(STATE_BASE) {}
|
2017-09-27 03:02:02 +00:00
|
|
|
|
|
2018-02-10 14:29:26 +00:00
|
|
|
|
AffixTag(int32_t offset)
|
|
|
|
|
: offset(offset) {}
|
2017-09-27 00:25:20 +00:00
|
|
|
|
|
|
|
|
|
AffixTag(int32_t offset, UChar32 codePoint, AffixPatternState state, AffixPatternType type)
|
2018-02-10 14:29:26 +00:00
|
|
|
|
: offset(offset), codePoint(codePoint), state(state), type(type) {}
|
2017-09-27 00:25:20 +00:00
|
|
|
|
};
|
|
|
|
|
|
2018-02-10 10:01:46 +00:00
|
|
|
|
class TokenConsumer {
|
|
|
|
|
public:
|
2018-06-13 17:00:44 +00:00
|
|
|
|
virtual ~TokenConsumer();
|
|
|
|
|
|
2018-02-10 10:01:46 +00:00
|
|
|
|
virtual void consumeToken(AffixPatternType type, UChar32 cp, UErrorCode& status) = 0;
|
|
|
|
|
};
|
|
|
|
|
|
2017-09-28 22:53:31 +00:00
|
|
|
|
// Exported as U_I18N_API because it is a base class for other exported types
|
|
|
|
|
class U_I18N_API SymbolProvider {
|
2017-09-27 00:25:20 +00:00
|
|
|
|
public:
|
2018-05-03 09:13:46 +00:00
|
|
|
|
virtual ~SymbolProvider();
|
2017-10-19 23:47:20 +00:00
|
|
|
|
|
2017-09-27 00:25:20 +00:00
|
|
|
|
// TODO: Could this be more efficient if it returned by reference?
|
|
|
|
|
virtual UnicodeString getSymbol(AffixPatternType type) const = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Performs manipulations on affix patterns: the prefix and suffix strings associated with a decimal
|
|
|
|
|
* format pattern. For example:
|
|
|
|
|
*
|
|
|
|
|
* <table>
|
|
|
|
|
* <tr><th>Affix Pattern</th><th>Example Unescaped (Formatted) String</th></tr>
|
|
|
|
|
* <tr><td>abc</td><td>abc</td></tr>
|
|
|
|
|
* <tr><td>ab-</td><td>ab−</td></tr>
|
|
|
|
|
* <tr><td>ab'-'</td><td>ab-</td></tr>
|
|
|
|
|
* <tr><td>ab''</td><td>ab'</td></tr>
|
|
|
|
|
* </table>
|
|
|
|
|
*
|
|
|
|
|
* To manually iterate over tokens in a literal string, use the following pattern, which is designed
|
|
|
|
|
* to be efficient.
|
|
|
|
|
*
|
|
|
|
|
* <pre>
|
|
|
|
|
* long tag = 0L;
|
|
|
|
|
* while (AffixPatternUtils.hasNext(tag, patternString)) {
|
|
|
|
|
* tag = AffixPatternUtils.nextToken(tag, patternString);
|
|
|
|
|
* int typeOrCp = AffixPatternUtils.getTypeOrCp(tag);
|
|
|
|
|
* switch (typeOrCp) {
|
|
|
|
|
* case AffixPatternUtils.TYPE_MINUS_SIGN:
|
|
|
|
|
* // Current token is a minus sign.
|
|
|
|
|
* break;
|
|
|
|
|
* case AffixPatternUtils.TYPE_PLUS_SIGN:
|
|
|
|
|
* // Current token is a plus sign.
|
|
|
|
|
* break;
|
|
|
|
|
* case AffixPatternUtils.TYPE_PERCENT:
|
|
|
|
|
* // Current token is a percent sign.
|
|
|
|
|
* break;
|
|
|
|
|
* // ... other types ...
|
|
|
|
|
* default:
|
|
|
|
|
* // Current token is an arbitrary code point.
|
|
|
|
|
* // The variable typeOrCp is the code point.
|
|
|
|
|
* break;
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* </pre>
|
|
|
|
|
*/
|
2017-09-27 18:50:44 +00:00
|
|
|
|
class U_I18N_API AffixUtils {
|
2017-09-27 00:25:20 +00:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Estimates the number of code points present in an unescaped version of the affix pattern string
|
|
|
|
|
* (one that would be returned by {@link #unescape}), assuming that all interpolated symbols
|
|
|
|
|
* consume one code point and that currencies consume as many code points as their symbol width.
|
|
|
|
|
* Used for computing padding width.
|
|
|
|
|
*
|
|
|
|
|
* @param patternString The original string whose width will be estimated.
|
|
|
|
|
* @return The length of the unescaped string.
|
|
|
|
|
*/
|
2018-04-21 09:08:53 +00:00
|
|
|
|
static int32_t estimateLength(const UnicodeString& patternString, UErrorCode& status);
|
2017-09-27 00:25:20 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Takes a string and escapes (quotes) characters that have special meaning in the affix pattern
|
|
|
|
|
* syntax. This function does not reverse-lookup symbols.
|
|
|
|
|
*
|
|
|
|
|
* <p>Example input: "-$x"; example output: "'-'$x"
|
|
|
|
|
*
|
|
|
|
|
* @param input The string to be escaped.
|
|
|
|
|
* @return The resulting UnicodeString.
|
|
|
|
|
*/
|
2018-04-21 09:08:53 +00:00
|
|
|
|
static UnicodeString escape(const UnicodeString& input);
|
2017-09-27 00:25:20 +00:00
|
|
|
|
|
|
|
|
|
static Field getFieldForType(AffixPatternType type);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Executes the unescape state machine. Replaces the unquoted characters "-", "+", "%", "‰", and
|
|
|
|
|
* "¤" with the corresponding symbols provided by the {@link SymbolProvider}, and inserts the
|
|
|
|
|
* result into the NumberStringBuilder at the requested location.
|
|
|
|
|
*
|
|
|
|
|
* <p>Example input: "'-'¤x"; example output: "-$x"
|
|
|
|
|
*
|
|
|
|
|
* @param affixPattern The original string to be unescaped.
|
|
|
|
|
* @param output The NumberStringBuilder to mutate with the result.
|
2017-10-06 22:44:25 +00:00
|
|
|
|
* @param position The index into the NumberStringBuilder to insert the string.
|
2017-09-27 00:25:20 +00:00
|
|
|
|
* @param provider An object to generate locale symbols.
|
|
|
|
|
*/
|
2018-04-21 09:08:53 +00:00
|
|
|
|
static int32_t unescape(const UnicodeString& affixPattern, NumberStringBuilder& output,
|
2018-02-10 14:29:26 +00:00
|
|
|
|
int32_t position, const SymbolProvider& provider, UErrorCode& status);
|
2017-09-27 00:25:20 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sames as {@link #unescape}, but only calculates the code point count. More efficient than {@link #unescape}
|
|
|
|
|
* if you only need the length but not the string itself.
|
|
|
|
|
*
|
|
|
|
|
* @param affixPattern The original string to be unescaped.
|
|
|
|
|
* @param provider An object to generate locale symbols.
|
|
|
|
|
* @return The same return value as if you called {@link #unescape}.
|
|
|
|
|
*/
|
2018-04-21 09:08:53 +00:00
|
|
|
|
static int32_t unescapedCodePointCount(const UnicodeString& affixPattern,
|
2018-02-10 14:29:26 +00:00
|
|
|
|
const SymbolProvider& provider, UErrorCode& status);
|
2017-09-27 00:25:20 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks whether the given affix pattern contains at least one token of the given type, which is
|
|
|
|
|
* one of the constants "TYPE_" in {@link AffixPatternUtils}.
|
|
|
|
|
*
|
|
|
|
|
* @param affixPattern The affix pattern to check.
|
|
|
|
|
* @param type The token type.
|
|
|
|
|
* @return true if the affix pattern contains the given token type; false otherwise.
|
|
|
|
|
*/
|
2018-04-21 09:08:53 +00:00
|
|
|
|
static bool containsType(const UnicodeString& affixPattern, AffixPatternType type, UErrorCode& status);
|
2017-09-27 00:25:20 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks whether the specified affix pattern has any unquoted currency symbols ("¤").
|
|
|
|
|
*
|
|
|
|
|
* @param affixPattern The string to check for currency symbols.
|
|
|
|
|
* @return true if the literal has at least one unquoted currency symbol; false otherwise.
|
|
|
|
|
*/
|
2018-04-21 09:08:53 +00:00
|
|
|
|
static bool hasCurrencySymbols(const UnicodeString& affixPattern, UErrorCode& status);
|
2017-09-27 00:25:20 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Replaces all occurrences of tokens with the given type with the given replacement char.
|
|
|
|
|
*
|
|
|
|
|
* @param affixPattern The source affix pattern (does not get modified).
|
|
|
|
|
* @param type The token type.
|
|
|
|
|
* @param replacementChar The char to substitute in place of chars of the given token type.
|
|
|
|
|
* @return A string containing the new affix pattern.
|
|
|
|
|
*/
|
2018-04-21 09:08:53 +00:00
|
|
|
|
static UnicodeString replaceType(const UnicodeString& affixPattern, AffixPatternType type,
|
2018-02-10 14:29:26 +00:00
|
|
|
|
char16_t replacementChar, UErrorCode& status);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns whether the given affix pattern contains only symbols and ignorables as defined by the
|
|
|
|
|
* given ignorables set.
|
|
|
|
|
*/
|
2018-04-21 09:08:53 +00:00
|
|
|
|
static bool containsOnlySymbolsAndIgnorables(const UnicodeString& affixPattern,
|
2018-02-10 14:29:26 +00:00
|
|
|
|
const UnicodeSet& ignorables, UErrorCode& status);
|
2017-09-27 00:25:20 +00:00
|
|
|
|
|
2018-02-10 10:01:46 +00:00
|
|
|
|
/**
|
|
|
|
|
* Iterates over the affix pattern, calling the TokenConsumer for each token.
|
|
|
|
|
*/
|
2018-04-21 09:08:53 +00:00
|
|
|
|
static void iterateWithConsumer(const UnicodeString& affixPattern, TokenConsumer& consumer,
|
2018-02-10 10:01:46 +00:00
|
|
|
|
UErrorCode& status);
|
|
|
|
|
|
2017-09-27 00:25:20 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the next token from the affix pattern.
|
|
|
|
|
*
|
|
|
|
|
* @param tag A bitmask used for keeping track of state from token to token. The initial value
|
|
|
|
|
* should be 0L.
|
|
|
|
|
* @param patternString The affix pattern.
|
|
|
|
|
* @return The bitmask tag to pass to the next call of this method to retrieve the following token
|
|
|
|
|
* (never negative), or -1 if there were no more tokens in the affix pattern.
|
|
|
|
|
* @see #hasNext
|
|
|
|
|
*/
|
2018-04-21 09:08:53 +00:00
|
|
|
|
static AffixTag nextToken(AffixTag tag, const UnicodeString& patternString, UErrorCode& status);
|
2017-09-27 00:25:20 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns whether the affix pattern string has any more tokens to be retrieved from a call to
|
|
|
|
|
* {@link #nextToken}.
|
|
|
|
|
*
|
|
|
|
|
* @param tag The bitmask tag of the previous token, as returned by {@link #nextToken}.
|
|
|
|
|
* @param string The affix pattern.
|
|
|
|
|
* @return true if there are more tokens to consume; false otherwise.
|
|
|
|
|
*/
|
2018-04-21 09:08:53 +00:00
|
|
|
|
static bool hasNext(const AffixTag& tag, const UnicodeString& string);
|
2017-09-27 00:25:20 +00:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
/**
|
|
|
|
|
* Encodes the given values into a tag struct.
|
|
|
|
|
* The order of the arguments is consistent with Java, but the order of the stored
|
|
|
|
|
* fields is not necessarily the same.
|
|
|
|
|
*/
|
2018-02-10 14:29:26 +00:00
|
|
|
|
static inline AffixTag makeTag(int32_t offset, AffixPatternType type, AffixPatternState state,
|
|
|
|
|
UChar32 cp) {
|
2017-09-27 00:25:20 +00:00
|
|
|
|
return {offset, cp, state, type};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace impl
|
|
|
|
|
} // namespace number
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
|
|
|
2017-09-27 05:31:57 +00:00
|
|
|
|
#endif //__NUMBER_AFFIXUTILS_H__
|
|
|
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|