plurrule_impl.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. * Copyright (C) 2007-2016, International Business Machines Corporation and
  6. * others. All Rights Reserved.
  7. *******************************************************************************
  8. *
  9. * File PLURRULE_IMPL.H
  10. *
  11. *******************************************************************************
  12. */
  13. #ifndef PLURRULE_IMPL
  14. #define PLURRULE_IMPL
  15. // Internal definitions for the PluralRules implementation.
  16. #include "unicode/utypes.h"
  17. #if !UCONFIG_NO_FORMATTING
  18. #include "unicode/format.h"
  19. #include "unicode/locid.h"
  20. #include "unicode/parseerr.h"
  21. #include "unicode/strenum.h"
  22. #include "unicode/ures.h"
  23. #include "uvector.h"
  24. #include "hash.h"
  25. #include "uassert.h"
  26. class PluralRulesTest;
  27. U_NAMESPACE_BEGIN
  28. class AndConstraint;
  29. class RuleChain;
  30. class DigitInterval;
  31. class PluralRules;
  32. class VisibleDigits;
  33. namespace pluralimpl {
  34. // TODO: Remove this and replace with u"" literals. Was for EBCDIC compatibility.
  35. static const UChar DOT = ((UChar) 0x002E);
  36. static const UChar SINGLE_QUOTE = ((UChar) 0x0027);
  37. static const UChar SLASH = ((UChar) 0x002F);
  38. static const UChar BACKSLASH = ((UChar) 0x005C);
  39. static const UChar SPACE = ((UChar) 0x0020);
  40. static const UChar EXCLAMATION = ((UChar) 0x0021);
  41. static const UChar QUOTATION_MARK = ((UChar) 0x0022);
  42. static const UChar NUMBER_SIGN = ((UChar) 0x0023);
  43. static const UChar PERCENT_SIGN = ((UChar) 0x0025);
  44. static const UChar ASTERISK = ((UChar) 0x002A);
  45. static const UChar COMMA = ((UChar) 0x002C);
  46. static const UChar HYPHEN = ((UChar) 0x002D);
  47. static const UChar U_ZERO = ((UChar) 0x0030);
  48. static const UChar U_ONE = ((UChar) 0x0031);
  49. static const UChar U_TWO = ((UChar) 0x0032);
  50. static const UChar U_THREE = ((UChar) 0x0033);
  51. static const UChar U_FOUR = ((UChar) 0x0034);
  52. static const UChar U_FIVE = ((UChar) 0x0035);
  53. static const UChar U_SIX = ((UChar) 0x0036);
  54. static const UChar U_SEVEN = ((UChar) 0x0037);
  55. static const UChar U_EIGHT = ((UChar) 0x0038);
  56. static const UChar U_NINE = ((UChar) 0x0039);
  57. static const UChar COLON = ((UChar) 0x003A);
  58. static const UChar SEMI_COLON = ((UChar) 0x003B);
  59. static const UChar EQUALS = ((UChar) 0x003D);
  60. static const UChar AT = ((UChar) 0x0040);
  61. static const UChar CAP_A = ((UChar) 0x0041);
  62. static const UChar CAP_B = ((UChar) 0x0042);
  63. static const UChar CAP_R = ((UChar) 0x0052);
  64. static const UChar CAP_Z = ((UChar) 0x005A);
  65. static const UChar LOWLINE = ((UChar) 0x005F);
  66. static const UChar LEFTBRACE = ((UChar) 0x007B);
  67. static const UChar RIGHTBRACE = ((UChar) 0x007D);
  68. static const UChar TILDE = ((UChar) 0x007E);
  69. static const UChar ELLIPSIS = ((UChar) 0x2026);
  70. static const UChar LOW_A = ((UChar) 0x0061);
  71. static const UChar LOW_B = ((UChar) 0x0062);
  72. static const UChar LOW_C = ((UChar) 0x0063);
  73. static const UChar LOW_D = ((UChar) 0x0064);
  74. static const UChar LOW_E = ((UChar) 0x0065);
  75. static const UChar LOW_F = ((UChar) 0x0066);
  76. static const UChar LOW_G = ((UChar) 0x0067);
  77. static const UChar LOW_H = ((UChar) 0x0068);
  78. static const UChar LOW_I = ((UChar) 0x0069);
  79. static const UChar LOW_J = ((UChar) 0x006a);
  80. static const UChar LOW_K = ((UChar) 0x006B);
  81. static const UChar LOW_L = ((UChar) 0x006C);
  82. static const UChar LOW_M = ((UChar) 0x006D);
  83. static const UChar LOW_N = ((UChar) 0x006E);
  84. static const UChar LOW_O = ((UChar) 0x006F);
  85. static const UChar LOW_P = ((UChar) 0x0070);
  86. static const UChar LOW_Q = ((UChar) 0x0071);
  87. static const UChar LOW_R = ((UChar) 0x0072);
  88. static const UChar LOW_S = ((UChar) 0x0073);
  89. static const UChar LOW_T = ((UChar) 0x0074);
  90. static const UChar LOW_U = ((UChar) 0x0075);
  91. static const UChar LOW_V = ((UChar) 0x0076);
  92. static const UChar LOW_W = ((UChar) 0x0077);
  93. static const UChar LOW_Y = ((UChar) 0x0079);
  94. static const UChar LOW_Z = ((UChar) 0x007A);
  95. }
  96. static const int32_t PLURAL_RANGE_HIGH = 0x7fffffff;
  97. enum tokenType {
  98. none,
  99. tNumber,
  100. tComma,
  101. tSemiColon,
  102. tSpace,
  103. tColon,
  104. tAt, // '@'
  105. tDot,
  106. tDot2,
  107. tEllipsis,
  108. tKeyword,
  109. tAnd,
  110. tOr,
  111. tMod, // 'mod' or '%'
  112. tNot, // 'not' only.
  113. tIn, // 'in' only.
  114. tEqual, // '=' only.
  115. tNotEqual, // '!='
  116. tTilde,
  117. tWithin,
  118. tIs,
  119. tVariableN,
  120. tVariableI,
  121. tVariableF,
  122. tVariableV,
  123. tVariableT,
  124. tDecimal,
  125. tInteger,
  126. tEOF
  127. };
  128. class PluralRuleParser: public UMemory {
  129. public:
  130. PluralRuleParser();
  131. virtual ~PluralRuleParser();
  132. void parse(const UnicodeString &rules, PluralRules *dest, UErrorCode &status);
  133. void getNextToken(UErrorCode &status);
  134. void checkSyntax(UErrorCode &status);
  135. static int32_t getNumberValue(const UnicodeString &token);
  136. private:
  137. static tokenType getKeyType(const UnicodeString& token, tokenType type);
  138. static tokenType charType(UChar ch);
  139. static UBool isValidKeyword(const UnicodeString& token);
  140. const UnicodeString *ruleSrc; // The rules string.
  141. int32_t ruleIndex; // String index in the input rules, the current parse position.
  142. UnicodeString token; // Token most recently scanned.
  143. tokenType type;
  144. tokenType prevType;
  145. // The items currently being parsed & built.
  146. // Note: currentChain may not be the last RuleChain in the
  147. // list because the "other" chain is forced to the end.
  148. AndConstraint *curAndConstraint;
  149. RuleChain *currentChain;
  150. int32_t rangeLowIdx; // Indices in the UVector of ranges of the
  151. int32_t rangeHiIdx; // low and hi values currently being parsed.
  152. enum EParseState {
  153. kKeyword,
  154. kExpr,
  155. kValue,
  156. kRangeList,
  157. kSamples
  158. };
  159. };
  160. enum PluralOperand {
  161. /**
  162. * The double value of the entire number.
  163. */
  164. PLURAL_OPERAND_N,
  165. /**
  166. * The integer value, with the fraction digits truncated off.
  167. */
  168. PLURAL_OPERAND_I,
  169. /**
  170. * All visible fraction digits as an integer, including trailing zeros.
  171. */
  172. PLURAL_OPERAND_F,
  173. /**
  174. * Visible fraction digits as an integer, not including trailing zeros.
  175. */
  176. PLURAL_OPERAND_T,
  177. /**
  178. * Number of visible fraction digits.
  179. */
  180. PLURAL_OPERAND_V,
  181. /**
  182. * Number of visible fraction digits, not including trailing zeros.
  183. */
  184. PLURAL_OPERAND_W,
  185. /**
  186. * Suppressed exponent for compact notation (exponent needed in
  187. * scientific notation with compact notation to approximate i).
  188. */
  189. PLURAL_OPERAND_E,
  190. /**
  191. * THIS OPERAND IS DEPRECATED AND HAS BEEN REMOVED FROM THE SPEC.
  192. *
  193. * <p>Returns the integer value, but will fail if the number has fraction digits.
  194. * That is, using "j" instead of "i" is like implicitly adding "v is 0".
  195. *
  196. * <p>For example, "j is 3" is equivalent to "i is 3 and v is 0": it matches
  197. * "3" but not "3.1" or "3.0".
  198. */
  199. PLURAL_OPERAND_J
  200. };
  201. /**
  202. * Converts from the tokenType enum to PluralOperand. Asserts that the given
  203. * tokenType can be mapped to a PluralOperand.
  204. */
  205. PluralOperand tokenTypeToPluralOperand(tokenType tt);
  206. /**
  207. * An interface to FixedDecimal, allowing for other implementations.
  208. * @internal
  209. */
  210. class U_I18N_API IFixedDecimal {
  211. public:
  212. virtual ~IFixedDecimal();
  213. /**
  214. * Returns the value corresponding to the specified operand (n, i, f, t, v, or w).
  215. * If the operand is 'n', returns a double; otherwise, returns an integer.
  216. */
  217. virtual double getPluralOperand(PluralOperand operand) const = 0;
  218. virtual bool isNaN() const = 0;
  219. virtual bool isInfinite() const = 0;
  220. /** Whether the number has no nonzero fraction digits. */
  221. virtual bool hasIntegerValue() const = 0;
  222. };
  223. /**
  224. * class FixedDecimal serves to communicate the properties
  225. * of a formatted number from a decimal formatter to PluralRules::select()
  226. *
  227. * see DecimalFormat::getFixedDecimal()
  228. * @internal
  229. */
  230. class U_I18N_API FixedDecimal: public IFixedDecimal, public UObject {
  231. public:
  232. /**
  233. * @param n the number, e.g. 12.345
  234. * @param v The number of visible fraction digits, e.g. 3
  235. * @param f The fraction digits, e.g. 345
  236. */
  237. FixedDecimal(double n, int32_t v, int64_t f);
  238. FixedDecimal(double n, int32_t);
  239. explicit FixedDecimal(double n);
  240. FixedDecimal();
  241. ~FixedDecimal() U_OVERRIDE;
  242. FixedDecimal(const UnicodeString &s, UErrorCode &ec);
  243. FixedDecimal(const FixedDecimal &other);
  244. double getPluralOperand(PluralOperand operand) const U_OVERRIDE;
  245. bool isNaN() const U_OVERRIDE;
  246. bool isInfinite() const U_OVERRIDE;
  247. bool hasIntegerValue() const U_OVERRIDE;
  248. bool isNanOrInfinity() const; // used in decimfmtimpl.cpp
  249. int32_t getVisibleFractionDigitCount() const;
  250. void init(double n, int32_t v, int64_t f);
  251. void init(double n);
  252. UBool quickInit(double n); // Try a fast-path only initialization,
  253. // return TRUE if successful.
  254. void adjustForMinFractionDigits(int32_t min);
  255. static int64_t getFractionalDigits(double n, int32_t v);
  256. static int32_t decimals(double n);
  257. double source;
  258. int32_t visibleDecimalDigitCount;
  259. int64_t decimalDigits;
  260. int64_t decimalDigitsWithoutTrailingZeros;
  261. int64_t intValue;
  262. UBool _hasIntegerValue;
  263. UBool isNegative;
  264. UBool _isNaN;
  265. UBool _isInfinite;
  266. };
  267. class AndConstraint : public UMemory {
  268. public:
  269. typedef enum RuleOp {
  270. NONE,
  271. MOD
  272. } RuleOp;
  273. RuleOp op = AndConstraint::NONE;
  274. int32_t opNum = -1; // for mod expressions, the right operand of the mod.
  275. int32_t value = -1; // valid for 'is' rules only.
  276. UVector32 *rangeList = nullptr; // for 'in', 'within' rules. Null otherwise.
  277. UBool negated = FALSE; // TRUE for negated rules.
  278. UBool integerOnly = FALSE; // TRUE for 'within' rules.
  279. tokenType digitsType = none; // n | i | v | f constraint.
  280. AndConstraint *next = nullptr;
  281. // Internal error status, used for errors that occur during the copy constructor.
  282. UErrorCode fInternalStatus = U_ZERO_ERROR;
  283. AndConstraint() = default;
  284. AndConstraint(const AndConstraint& other);
  285. virtual ~AndConstraint();
  286. AndConstraint* add(UErrorCode& status);
  287. // UBool isFulfilled(double number);
  288. UBool isFulfilled(const IFixedDecimal &number);
  289. };
  290. class OrConstraint : public UMemory {
  291. public:
  292. AndConstraint *childNode = nullptr;
  293. OrConstraint *next = nullptr;
  294. // Internal error status, used for errors that occur during the copy constructor.
  295. UErrorCode fInternalStatus = U_ZERO_ERROR;
  296. OrConstraint() = default;
  297. OrConstraint(const OrConstraint& other);
  298. virtual ~OrConstraint();
  299. AndConstraint* add(UErrorCode& status);
  300. // UBool isFulfilled(double number);
  301. UBool isFulfilled(const IFixedDecimal &number);
  302. };
  303. class RuleChain : public UMemory {
  304. public:
  305. UnicodeString fKeyword;
  306. RuleChain *fNext = nullptr;
  307. OrConstraint *ruleHeader = nullptr;
  308. UnicodeString fDecimalSamples; // Samples strings from rule source
  309. UnicodeString fIntegerSamples; // without @decimal or @integer, otherwise unprocessed.
  310. UBool fDecimalSamplesUnbounded = FALSE;
  311. UBool fIntegerSamplesUnbounded = FALSE;
  312. // Internal error status, used for errors that occur during the copy constructor.
  313. UErrorCode fInternalStatus = U_ZERO_ERROR;
  314. RuleChain() = default;
  315. RuleChain(const RuleChain& other);
  316. virtual ~RuleChain();
  317. UnicodeString select(const IFixedDecimal &number) const;
  318. void dumpRules(UnicodeString& result);
  319. UErrorCode getKeywords(int32_t maxArraySize, UnicodeString *keywords, int32_t& arraySize) const;
  320. UBool isKeyword(const UnicodeString& keyword) const;
  321. };
  322. class PluralKeywordEnumeration : public StringEnumeration {
  323. public:
  324. PluralKeywordEnumeration(RuleChain *header, UErrorCode& status);
  325. virtual ~PluralKeywordEnumeration();
  326. static UClassID U_EXPORT2 getStaticClassID(void);
  327. virtual UClassID getDynamicClassID(void) const;
  328. virtual const UnicodeString* snext(UErrorCode& status);
  329. virtual void reset(UErrorCode& status);
  330. virtual int32_t count(UErrorCode& status) const;
  331. private:
  332. int32_t pos;
  333. UVector fKeywordNames;
  334. };
  335. class U_I18N_API PluralAvailableLocalesEnumeration: public StringEnumeration {
  336. public:
  337. PluralAvailableLocalesEnumeration(UErrorCode &status);
  338. virtual ~PluralAvailableLocalesEnumeration();
  339. virtual const char* next(int32_t *resultLength, UErrorCode& status);
  340. virtual void reset(UErrorCode& status);
  341. virtual int32_t count(UErrorCode& status) const;
  342. private:
  343. UErrorCode fOpenStatus;
  344. UResourceBundle *fLocales = nullptr;
  345. UResourceBundle *fRes = nullptr;
  346. };
  347. U_NAMESPACE_END
  348. #endif /* #if !UCONFIG_NO_FORMATTING */
  349. #endif // _PLURRULE_IMPL
  350. //eof