nfrule.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. * Copyright (C) 1997-2015, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. *******************************************************************************
  8. */
  9. #ifndef NFRULE_H
  10. #define NFRULE_H
  11. #include "unicode/rbnf.h"
  12. #if U_HAVE_RBNF
  13. #include "unicode/utypes.h"
  14. #include "unicode/uobject.h"
  15. #include "unicode/unistr.h"
  16. U_NAMESPACE_BEGIN
  17. class FieldPosition;
  18. class Formattable;
  19. class NFRuleList;
  20. class NFRuleSet;
  21. class NFSubstitution;
  22. class ParsePosition;
  23. class PluralFormat;
  24. class RuleBasedNumberFormat;
  25. class UnicodeString;
  26. class NFRule : public UMemory {
  27. public:
  28. enum ERuleType {
  29. kNoBase = 0,
  30. kNegativeNumberRule = -1,
  31. kImproperFractionRule = -2,
  32. kProperFractionRule = -3,
  33. kMasterRule = -4,
  34. kInfinityRule = -5,
  35. kNaNRule = -6,
  36. kOtherRule = -7
  37. };
  38. static void makeRules(UnicodeString& definition,
  39. NFRuleSet* ruleSet,
  40. const NFRule* predecessor,
  41. const RuleBasedNumberFormat* rbnf,
  42. NFRuleList& ruleList,
  43. UErrorCode& status);
  44. NFRule(const RuleBasedNumberFormat* rbnf, const UnicodeString &ruleText, UErrorCode &status);
  45. ~NFRule();
  46. UBool operator==(const NFRule& rhs) const;
  47. UBool operator!=(const NFRule& rhs) const { return !operator==(rhs); }
  48. ERuleType getType() const { return (ERuleType)(baseValue <= kNoBase ? (ERuleType)baseValue : kOtherRule); }
  49. void setType(ERuleType ruleType) { baseValue = (int32_t)ruleType; }
  50. int64_t getBaseValue() const { return baseValue; }
  51. void setBaseValue(int64_t value, UErrorCode& status);
  52. UChar getDecimalPoint() const { return decimalPoint; }
  53. int64_t getDivisor() const;
  54. void doFormat(int64_t number, UnicodeString& toAppendTo, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
  55. void doFormat(double number, UnicodeString& toAppendTo, int32_t pos, int32_t recursionCount, UErrorCode& status) const;
  56. UBool doParse(const UnicodeString& text,
  57. ParsePosition& pos,
  58. UBool isFractional,
  59. double upperBound,
  60. uint32_t nonNumericalExecutedRuleMask,
  61. Formattable& result) const;
  62. UBool shouldRollBack(int64_t number) const;
  63. void _appendRuleText(UnicodeString& result) const;
  64. int32_t findTextLenient(const UnicodeString& str, const UnicodeString& key,
  65. int32_t startingAt, int32_t* resultCount) const;
  66. void setDecimalFormatSymbols(const DecimalFormatSymbols &newSymbols, UErrorCode& status);
  67. private:
  68. void parseRuleDescriptor(UnicodeString& descriptor, UErrorCode& status);
  69. void extractSubstitutions(const NFRuleSet* ruleSet, const UnicodeString &ruleText, const NFRule* predecessor, UErrorCode& status);
  70. NFSubstitution* extractSubstitution(const NFRuleSet* ruleSet, const NFRule* predecessor, UErrorCode& status);
  71. int16_t expectedExponent() const;
  72. int32_t indexOfAnyRulePrefix() const;
  73. double matchToDelimiter(const UnicodeString& text, int32_t startPos, double baseValue,
  74. const UnicodeString& delimiter, ParsePosition& pp, const NFSubstitution* sub,
  75. uint32_t nonNumericalExecutedRuleMask,
  76. double upperBound) const;
  77. void stripPrefix(UnicodeString& text, const UnicodeString& prefix, ParsePosition& pp) const;
  78. int32_t prefixLength(const UnicodeString& str, const UnicodeString& prefix, UErrorCode& status) const;
  79. UBool allIgnorable(const UnicodeString& str, UErrorCode& status) const;
  80. int32_t findText(const UnicodeString& str, const UnicodeString& key,
  81. int32_t startingAt, int32_t* resultCount) const;
  82. private:
  83. int64_t baseValue;
  84. int32_t radix;
  85. int16_t exponent;
  86. UChar decimalPoint;
  87. UnicodeString fRuleText;
  88. NFSubstitution* sub1;
  89. NFSubstitution* sub2;
  90. const RuleBasedNumberFormat* formatter;
  91. const PluralFormat* rulePatternFormat;
  92. NFRule(const NFRule &other); // forbid copying of this class
  93. NFRule &operator=(const NFRule &other); // forbid copying of this class
  94. };
  95. U_NAMESPACE_END
  96. /* U_HAVE_RBNF */
  97. #endif
  98. // NFRULE_H
  99. #endif