number_affixutils.h 8.8 KB

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