1 // Copyright (C) 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 10 #ifndef NFRULE_H 11 #define NFRULE_H 12 13 #include "unicode/rbnf.h" 14 15 #if U_HAVE_RBNF 16 17 #include "unicode/utypes.h" 18 #include "unicode/uobject.h" 19 #include "unicode/unistr.h" 20 #include "putilimp.h" 21 22 U_NAMESPACE_BEGIN 23 24 class FieldPosition; 25 class Formattable; 26 class NFRuleList; 27 class NFRuleSet; 28 class NFSubstitution; 29 class ParsePosition; 30 class PluralFormat; 31 class RuleBasedNumberFormat; 32 class UnicodeString; 33 34 class NFRule : public UMemory { 35 public: 36 37 enum ERuleType { 38 kNoBase = 0, 39 kNegativeNumberRule = -1, 40 kImproperFractionRule = -2, 41 kProperFractionRule = -3, 42 kMasterRule = -4, 43 kInfinityRule = -5, 44 kNaNRule = -6, 45 kOtherRule = -7 46 }; 47 48 static void makeRules(UnicodeString& definition, 49 NFRuleSet* ruleSet, 50 const NFRule* predecessor, 51 const RuleBasedNumberFormat* rbnf, 52 NFRuleList& ruleList, 53 UErrorCode& status); 54 55 NFRule(const RuleBasedNumberFormat* rbnf, const UnicodeString &ruleText, UErrorCode &status); 56 ~NFRule(); 57 58 UBool operator==(const NFRule& rhs) const; 59 UBool operator!=(const NFRule& rhs) const { return !operator==(rhs); } 60 getType()61 ERuleType getType() const { return (ERuleType)(baseValue <= kNoBase ? (ERuleType)baseValue : kOtherRule); } setType(ERuleType ruleType)62 void setType(ERuleType ruleType) { baseValue = (int32_t)ruleType; } 63 getBaseValue()64 int64_t getBaseValue() const { return baseValue; } 65 void setBaseValue(int64_t value, UErrorCode& status); 66 getDecimalPoint()67 UChar getDecimalPoint() const { return decimalPoint; } 68 getDivisor()69 double getDivisor() const { return uprv_pow(radix, exponent); } 70 71 void doFormat(int64_t number, UnicodeString& toAppendTo, int32_t pos, int32_t recursionCount, UErrorCode& status) const; 72 void doFormat(double number, UnicodeString& toAppendTo, int32_t pos, int32_t recursionCount, UErrorCode& status) const; 73 74 UBool doParse(const UnicodeString& text, 75 ParsePosition& pos, 76 UBool isFractional, 77 double upperBound, 78 Formattable& result) const; 79 80 UBool shouldRollBack(double number) const; 81 82 void _appendRuleText(UnicodeString& result) const; 83 84 int32_t findTextLenient(const UnicodeString& str, const UnicodeString& key, 85 int32_t startingAt, int32_t* resultCount) const; 86 87 void setDecimalFormatSymbols(const DecimalFormatSymbols &newSymbols, UErrorCode& status); 88 89 private: 90 void parseRuleDescriptor(UnicodeString& descriptor, UErrorCode& status); 91 void extractSubstitutions(const NFRuleSet* ruleSet, const UnicodeString &ruleText, const NFRule* predecessor, UErrorCode& status); 92 NFSubstitution* extractSubstitution(const NFRuleSet* ruleSet, const NFRule* predecessor, UErrorCode& status); 93 94 int16_t expectedExponent() const; 95 int32_t indexOfAnyRulePrefix() const; 96 double matchToDelimiter(const UnicodeString& text, int32_t startPos, double baseValue, 97 const UnicodeString& delimiter, ParsePosition& pp, const NFSubstitution* sub, 98 double upperBound) const; 99 void stripPrefix(UnicodeString& text, const UnicodeString& prefix, ParsePosition& pp) const; 100 101 int32_t prefixLength(const UnicodeString& str, const UnicodeString& prefix, UErrorCode& status) const; 102 UBool allIgnorable(const UnicodeString& str, UErrorCode& status) const; 103 int32_t findText(const UnicodeString& str, const UnicodeString& key, 104 int32_t startingAt, int32_t* resultCount) const; 105 106 private: 107 int64_t baseValue; 108 int32_t radix; 109 int16_t exponent; 110 UChar decimalPoint; 111 UnicodeString ruleText; 112 NFSubstitution* sub1; 113 NFSubstitution* sub2; 114 const RuleBasedNumberFormat* formatter; 115 const PluralFormat* rulePatternFormat; 116 117 NFRule(const NFRule &other); // forbid copying of this class 118 NFRule &operator=(const NFRule &other); // forbid copying of this class 119 }; 120 121 U_NAMESPACE_END 122 123 /* U_HAVE_RBNF */ 124 #endif 125 126 // NFRULE_H 127 #endif 128 129