plurrule_impl.h 14 KB

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