1*0e209d39SAndroid Build Coastguard Worker // © 2016 and later: Unicode, Inc. and others. 2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html 3*0e209d39SAndroid Build Coastguard Worker /* 4*0e209d39SAndroid Build Coastguard Worker ****************************************************************************** 5*0e209d39SAndroid Build Coastguard Worker * Copyright (C) 1997-2015, International Business Machines 6*0e209d39SAndroid Build Coastguard Worker * Corporation and others. All Rights Reserved. 7*0e209d39SAndroid Build Coastguard Worker ****************************************************************************** 8*0e209d39SAndroid Build Coastguard Worker * file name: nfsubs.h 9*0e209d39SAndroid Build Coastguard Worker * encoding: UTF-8 10*0e209d39SAndroid Build Coastguard Worker * tab size: 8 (not used) 11*0e209d39SAndroid Build Coastguard Worker * indentation:4 12*0e209d39SAndroid Build Coastguard Worker * 13*0e209d39SAndroid Build Coastguard Worker * Modification history 14*0e209d39SAndroid Build Coastguard Worker * Date Name Comments 15*0e209d39SAndroid Build Coastguard Worker * 10/11/2001 Doug Ported from ICU4J 16*0e209d39SAndroid Build Coastguard Worker */ 17*0e209d39SAndroid Build Coastguard Worker 18*0e209d39SAndroid Build Coastguard Worker #ifndef NFSUBS_H 19*0e209d39SAndroid Build Coastguard Worker #define NFSUBS_H 20*0e209d39SAndroid Build Coastguard Worker 21*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h" 22*0e209d39SAndroid Build Coastguard Worker #include "unicode/uobject.h" 23*0e209d39SAndroid Build Coastguard Worker #include "nfrule.h" 24*0e209d39SAndroid Build Coastguard Worker 25*0e209d39SAndroid Build Coastguard Worker #if U_HAVE_RBNF 26*0e209d39SAndroid Build Coastguard Worker 27*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h" 28*0e209d39SAndroid Build Coastguard Worker #include "unicode/decimfmt.h" 29*0e209d39SAndroid Build Coastguard Worker #include "nfrs.h" 30*0e209d39SAndroid Build Coastguard Worker #include <float.h> 31*0e209d39SAndroid Build Coastguard Worker 32*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN 33*0e209d39SAndroid Build Coastguard Worker 34*0e209d39SAndroid Build Coastguard Worker class NFSubstitution : public UObject { 35*0e209d39SAndroid Build Coastguard Worker int32_t pos; 36*0e209d39SAndroid Build Coastguard Worker const NFRuleSet* ruleSet; 37*0e209d39SAndroid Build Coastguard Worker DecimalFormat* numberFormat; 38*0e209d39SAndroid Build Coastguard Worker 39*0e209d39SAndroid Build Coastguard Worker protected: 40*0e209d39SAndroid Build Coastguard Worker NFSubstitution(int32_t pos, 41*0e209d39SAndroid Build Coastguard Worker const NFRuleSet* ruleSet, 42*0e209d39SAndroid Build Coastguard Worker const UnicodeString& description, 43*0e209d39SAndroid Build Coastguard Worker UErrorCode& status); 44*0e209d39SAndroid Build Coastguard Worker 45*0e209d39SAndroid Build Coastguard Worker /** 46*0e209d39SAndroid Build Coastguard Worker * Get the Ruleset of the object. 47*0e209d39SAndroid Build Coastguard Worker * @return the Ruleset of the object. 48*0e209d39SAndroid Build Coastguard Worker */ getRuleSet()49*0e209d39SAndroid Build Coastguard Worker const NFRuleSet* getRuleSet() const { return ruleSet; } 50*0e209d39SAndroid Build Coastguard Worker 51*0e209d39SAndroid Build Coastguard Worker /** 52*0e209d39SAndroid Build Coastguard Worker * get the NumberFormat of this object. 53*0e209d39SAndroid Build Coastguard Worker * @return the numberformat of this object. 54*0e209d39SAndroid Build Coastguard Worker */ getNumberFormat()55*0e209d39SAndroid Build Coastguard Worker const DecimalFormat* getNumberFormat() const { return numberFormat; } 56*0e209d39SAndroid Build Coastguard Worker 57*0e209d39SAndroid Build Coastguard Worker public: 58*0e209d39SAndroid Build Coastguard Worker static NFSubstitution* makeSubstitution(int32_t pos, 59*0e209d39SAndroid Build Coastguard Worker const NFRule* rule, 60*0e209d39SAndroid Build Coastguard Worker const NFRule* predecessor, 61*0e209d39SAndroid Build Coastguard Worker const NFRuleSet* ruleSet, 62*0e209d39SAndroid Build Coastguard Worker const RuleBasedNumberFormat* rbnf, 63*0e209d39SAndroid Build Coastguard Worker const UnicodeString& description, 64*0e209d39SAndroid Build Coastguard Worker UErrorCode& status); 65*0e209d39SAndroid Build Coastguard Worker 66*0e209d39SAndroid Build Coastguard Worker /** 67*0e209d39SAndroid Build Coastguard Worker * Destructor. 68*0e209d39SAndroid Build Coastguard Worker */ 69*0e209d39SAndroid Build Coastguard Worker virtual ~NFSubstitution(); 70*0e209d39SAndroid Build Coastguard Worker 71*0e209d39SAndroid Build Coastguard Worker /** 72*0e209d39SAndroid Build Coastguard Worker * Return true if the given Format objects are semantically equal. 73*0e209d39SAndroid Build Coastguard Worker * Objects of different subclasses are considered unequal. 74*0e209d39SAndroid Build Coastguard Worker * @param rhs the object to be compared with. 75*0e209d39SAndroid Build Coastguard Worker * @return true if the given Format objects are semantically equal. 76*0e209d39SAndroid Build Coastguard Worker */ 77*0e209d39SAndroid Build Coastguard Worker virtual bool operator==(const NFSubstitution& rhs) const; 78*0e209d39SAndroid Build Coastguard Worker 79*0e209d39SAndroid Build Coastguard Worker /** 80*0e209d39SAndroid Build Coastguard Worker * Return true if the given Format objects are semantically unequal. 81*0e209d39SAndroid Build Coastguard Worker * Objects of different subclasses are considered unequal. 82*0e209d39SAndroid Build Coastguard Worker * @param rhs the object to be compared with. 83*0e209d39SAndroid Build Coastguard Worker * @return true if the given Format objects are semantically unequal. 84*0e209d39SAndroid Build Coastguard Worker */ 85*0e209d39SAndroid Build Coastguard Worker bool operator!=(const NFSubstitution& rhs) const { return !operator==(rhs); } 86*0e209d39SAndroid Build Coastguard Worker 87*0e209d39SAndroid Build Coastguard Worker /** 88*0e209d39SAndroid Build Coastguard Worker * Sets the substitution's divisor. Used by NFRule.setBaseValue(). 89*0e209d39SAndroid Build Coastguard Worker * A no-op for all substitutions except multiplier and modulus 90*0e209d39SAndroid Build Coastguard Worker * substitutions. 91*0e209d39SAndroid Build Coastguard Worker * @param radix The radix of the divisor 92*0e209d39SAndroid Build Coastguard Worker * @param exponent The exponent of the divisor 93*0e209d39SAndroid Build Coastguard Worker */ 94*0e209d39SAndroid Build Coastguard Worker virtual void setDivisor(int32_t radix, int16_t exponent, UErrorCode& status); 95*0e209d39SAndroid Build Coastguard Worker 96*0e209d39SAndroid Build Coastguard Worker /** 97*0e209d39SAndroid Build Coastguard Worker * Replaces result with the string describing the substitution. 98*0e209d39SAndroid Build Coastguard Worker * @param result Output param which will receive the string. 99*0e209d39SAndroid Build Coastguard Worker */ 100*0e209d39SAndroid Build Coastguard Worker virtual void toString(UnicodeString& result) const; 101*0e209d39SAndroid Build Coastguard Worker 102*0e209d39SAndroid Build Coastguard Worker void setDecimalFormatSymbols(const DecimalFormatSymbols &newSymbols, UErrorCode& status); 103*0e209d39SAndroid Build Coastguard Worker 104*0e209d39SAndroid Build Coastguard Worker //----------------------------------------------------------------------- 105*0e209d39SAndroid Build Coastguard Worker // formatting 106*0e209d39SAndroid Build Coastguard Worker //----------------------------------------------------------------------- 107*0e209d39SAndroid Build Coastguard Worker 108*0e209d39SAndroid Build Coastguard Worker /** 109*0e209d39SAndroid Build Coastguard Worker * Performs a mathematical operation on the number, formats it using 110*0e209d39SAndroid Build Coastguard Worker * either ruleSet or decimalFormat, and inserts the result into 111*0e209d39SAndroid Build Coastguard Worker * toInsertInto. 112*0e209d39SAndroid Build Coastguard Worker * @param number The number being formatted. 113*0e209d39SAndroid Build Coastguard Worker * @param toInsertInto The string we insert the result into 114*0e209d39SAndroid Build Coastguard Worker * @param pos The position in toInsertInto where the owning rule's 115*0e209d39SAndroid Build Coastguard Worker * rule text begins (this value is added to this substitution's 116*0e209d39SAndroid Build Coastguard Worker * position to determine exactly where to insert the new text) 117*0e209d39SAndroid Build Coastguard Worker */ 118*0e209d39SAndroid Build Coastguard Worker virtual void doSubstitution(int64_t number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const; 119*0e209d39SAndroid Build Coastguard Worker 120*0e209d39SAndroid Build Coastguard Worker /** 121*0e209d39SAndroid Build Coastguard Worker * Performs a mathematical operation on the number, formats it using 122*0e209d39SAndroid Build Coastguard Worker * either ruleSet or decimalFormat, and inserts the result into 123*0e209d39SAndroid Build Coastguard Worker * toInsertInto. 124*0e209d39SAndroid Build Coastguard Worker * @param number The number being formatted. 125*0e209d39SAndroid Build Coastguard Worker * @param toInsertInto The string we insert the result into 126*0e209d39SAndroid Build Coastguard Worker * @param pos The position in toInsertInto where the owning rule's 127*0e209d39SAndroid Build Coastguard Worker * rule text begins (this value is added to this substitution's 128*0e209d39SAndroid Build Coastguard Worker * position to determine exactly where to insert the new text) 129*0e209d39SAndroid Build Coastguard Worker */ 130*0e209d39SAndroid Build Coastguard Worker virtual void doSubstitution(double number, UnicodeString& toInsertInto, int32_t pos, int32_t recursionCount, UErrorCode& status) const; 131*0e209d39SAndroid Build Coastguard Worker 132*0e209d39SAndroid Build Coastguard Worker protected: 133*0e209d39SAndroid Build Coastguard Worker /** 134*0e209d39SAndroid Build Coastguard Worker * Subclasses override this function to perform some kind of 135*0e209d39SAndroid Build Coastguard Worker * mathematical operation on the number. The result of this operation 136*0e209d39SAndroid Build Coastguard Worker * is formatted using the rule set or DecimalFormat that this 137*0e209d39SAndroid Build Coastguard Worker * substitution refers to, and the result is inserted into the result 138*0e209d39SAndroid Build Coastguard Worker * string. 139*0e209d39SAndroid Build Coastguard Worker * @param The number being formatted 140*0e209d39SAndroid Build Coastguard Worker * @return The result of performing the opreration on the number 141*0e209d39SAndroid Build Coastguard Worker */ 142*0e209d39SAndroid Build Coastguard Worker virtual int64_t transformNumber(int64_t number) const = 0; 143*0e209d39SAndroid Build Coastguard Worker 144*0e209d39SAndroid Build Coastguard Worker /** 145*0e209d39SAndroid Build Coastguard Worker * Subclasses override this function to perform some kind of 146*0e209d39SAndroid Build Coastguard Worker * mathematical operation on the number. The result of this operation 147*0e209d39SAndroid Build Coastguard Worker * is formatted using the rule set or DecimalFormat that this 148*0e209d39SAndroid Build Coastguard Worker * substitution refers to, and the result is inserted into the result 149*0e209d39SAndroid Build Coastguard Worker * string. 150*0e209d39SAndroid Build Coastguard Worker * @param The number being formatted 151*0e209d39SAndroid Build Coastguard Worker * @return The result of performing the opreration on the number 152*0e209d39SAndroid Build Coastguard Worker */ 153*0e209d39SAndroid Build Coastguard Worker virtual double transformNumber(double number) const = 0; 154*0e209d39SAndroid Build Coastguard Worker 155*0e209d39SAndroid Build Coastguard Worker public: 156*0e209d39SAndroid Build Coastguard Worker //----------------------------------------------------------------------- 157*0e209d39SAndroid Build Coastguard Worker // parsing 158*0e209d39SAndroid Build Coastguard Worker //----------------------------------------------------------------------- 159*0e209d39SAndroid Build Coastguard Worker 160*0e209d39SAndroid Build Coastguard Worker /** 161*0e209d39SAndroid Build Coastguard Worker * Parses a string using the rule set or DecimalFormat belonging 162*0e209d39SAndroid Build Coastguard Worker * to this substitution. If there's a match, a mathematical 163*0e209d39SAndroid Build Coastguard Worker * operation (the inverse of the one used in formatting) is 164*0e209d39SAndroid Build Coastguard Worker * performed on the result of the parse and the value passed in 165*0e209d39SAndroid Build Coastguard Worker * and returned as the result. The parse position is updated to 166*0e209d39SAndroid Build Coastguard Worker * point to the first unmatched character in the string. 167*0e209d39SAndroid Build Coastguard Worker * @param text The string to parse 168*0e209d39SAndroid Build Coastguard Worker * @param parsePosition On entry, ignored, but assumed to be 0. 169*0e209d39SAndroid Build Coastguard Worker * On exit, this is updated to point to the first unmatched 170*0e209d39SAndroid Build Coastguard Worker * character (or 0 if the substitution didn't match) 171*0e209d39SAndroid Build Coastguard Worker * @param baseValue A partial parse result that should be 172*0e209d39SAndroid Build Coastguard Worker * combined with the result of this parse 173*0e209d39SAndroid Build Coastguard Worker * @param upperBound When searching the rule set for a rule 174*0e209d39SAndroid Build Coastguard Worker * matching the string passed in, only rules with base values 175*0e209d39SAndroid Build Coastguard Worker * lower than this are considered 176*0e209d39SAndroid Build Coastguard Worker * @param lenientParse If true and matching against rules fails, 177*0e209d39SAndroid Build Coastguard Worker * the substitution will also try matching the text against 178*0e209d39SAndroid Build Coastguard Worker * numerals using a default-costructed NumberFormat. If false, 179*0e209d39SAndroid Build Coastguard Worker * no extra work is done. (This value is false whenever the 180*0e209d39SAndroid Build Coastguard Worker * formatter isn't in lenient-parse mode, but is also false 181*0e209d39SAndroid Build Coastguard Worker * under some conditions even when the formatter _is_ in 182*0e209d39SAndroid Build Coastguard Worker * lenient-parse mode.) 183*0e209d39SAndroid Build Coastguard Worker * @return If there's a match, this is the result of composing 184*0e209d39SAndroid Build Coastguard Worker * baseValue with whatever was returned from matching the 185*0e209d39SAndroid Build Coastguard Worker * characters. This will be either a Long or a Double. If there's 186*0e209d39SAndroid Build Coastguard Worker * no match this is new Long(0) (not null), and parsePosition 187*0e209d39SAndroid Build Coastguard Worker * is left unchanged. 188*0e209d39SAndroid Build Coastguard Worker */ 189*0e209d39SAndroid Build Coastguard Worker virtual UBool doParse(const UnicodeString& text, 190*0e209d39SAndroid Build Coastguard Worker ParsePosition& parsePosition, 191*0e209d39SAndroid Build Coastguard Worker double baseValue, 192*0e209d39SAndroid Build Coastguard Worker double upperBound, 193*0e209d39SAndroid Build Coastguard Worker UBool lenientParse, 194*0e209d39SAndroid Build Coastguard Worker uint32_t nonNumericalExecutedRuleMask, 195*0e209d39SAndroid Build Coastguard Worker Formattable& result) const; 196*0e209d39SAndroid Build Coastguard Worker 197*0e209d39SAndroid Build Coastguard Worker /** 198*0e209d39SAndroid Build Coastguard Worker * Derives a new value from the two values passed in. The two values 199*0e209d39SAndroid Build Coastguard Worker * are typically either the base values of two rules (the one containing 200*0e209d39SAndroid Build Coastguard Worker * the substitution and the one matching the substitution) or partial 201*0e209d39SAndroid Build Coastguard Worker * parse results derived in some other way. The operation is generally 202*0e209d39SAndroid Build Coastguard Worker * the inverse of the operation performed by transformNumber(). 203*0e209d39SAndroid Build Coastguard Worker * @param newRuleValue The value produced by matching this substitution 204*0e209d39SAndroid Build Coastguard Worker * @param oldRuleValue The value that was passed to the substitution 205*0e209d39SAndroid Build Coastguard Worker * by the rule that owns it 206*0e209d39SAndroid Build Coastguard Worker * @return A third value derived from the other two, representing a 207*0e209d39SAndroid Build Coastguard Worker * partial parse result 208*0e209d39SAndroid Build Coastguard Worker */ 209*0e209d39SAndroid Build Coastguard Worker virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const = 0; 210*0e209d39SAndroid Build Coastguard Worker 211*0e209d39SAndroid Build Coastguard Worker /** 212*0e209d39SAndroid Build Coastguard Worker * Calculates an upper bound when searching for a rule that matches 213*0e209d39SAndroid Build Coastguard Worker * this substitution. Rules with base values greater than or equal 214*0e209d39SAndroid Build Coastguard Worker * to upperBound are not considered. 215*0e209d39SAndroid Build Coastguard Worker * @param oldUpperBound The current upper-bound setting. The new 216*0e209d39SAndroid Build Coastguard Worker * upper bound can't be any higher. 217*0e209d39SAndroid Build Coastguard Worker * @return the upper bound when searching for a rule that matches 218*0e209d39SAndroid Build Coastguard Worker * this substitution. 219*0e209d39SAndroid Build Coastguard Worker */ 220*0e209d39SAndroid Build Coastguard Worker virtual double calcUpperBound(double oldUpperBound) const = 0; 221*0e209d39SAndroid Build Coastguard Worker 222*0e209d39SAndroid Build Coastguard Worker //----------------------------------------------------------------------- 223*0e209d39SAndroid Build Coastguard Worker // simple accessors 224*0e209d39SAndroid Build Coastguard Worker //----------------------------------------------------------------------- 225*0e209d39SAndroid Build Coastguard Worker 226*0e209d39SAndroid Build Coastguard Worker /** 227*0e209d39SAndroid Build Coastguard Worker * Returns the substitution's position in the rule that owns it. 228*0e209d39SAndroid Build Coastguard Worker * @return The substitution's position in the rule that owns it. 229*0e209d39SAndroid Build Coastguard Worker */ getPos()230*0e209d39SAndroid Build Coastguard Worker int32_t getPos() const { return pos; } 231*0e209d39SAndroid Build Coastguard Worker 232*0e209d39SAndroid Build Coastguard Worker /** 233*0e209d39SAndroid Build Coastguard Worker * Returns the character used in the textual representation of 234*0e209d39SAndroid Build Coastguard Worker * substitutions of this type. Used by toString(). 235*0e209d39SAndroid Build Coastguard Worker * @return This substitution's token character. 236*0e209d39SAndroid Build Coastguard Worker */ 237*0e209d39SAndroid Build Coastguard Worker virtual char16_t tokenChar() const = 0; 238*0e209d39SAndroid Build Coastguard Worker 239*0e209d39SAndroid Build Coastguard Worker /** 240*0e209d39SAndroid Build Coastguard Worker * Returns true if this is a modulus substitution. (We didn't do this 241*0e209d39SAndroid Build Coastguard Worker * with instanceof partially because it causes source files to 242*0e209d39SAndroid Build Coastguard Worker * proliferate and partially because we have to port this to C++.) 243*0e209d39SAndroid Build Coastguard Worker * @return true if this object is an instance of ModulusSubstitution 244*0e209d39SAndroid Build Coastguard Worker */ 245*0e209d39SAndroid Build Coastguard Worker virtual UBool isModulusSubstitution() const; 246*0e209d39SAndroid Build Coastguard Worker 247*0e209d39SAndroid Build Coastguard Worker private: 248*0e209d39SAndroid Build Coastguard Worker NFSubstitution(const NFSubstitution &other) = delete; // forbid copying of this class 249*0e209d39SAndroid Build Coastguard Worker NFSubstitution &operator=(const NFSubstitution &other) = delete; // forbid copying of this class 250*0e209d39SAndroid Build Coastguard Worker 251*0e209d39SAndroid Build Coastguard Worker public: 252*0e209d39SAndroid Build Coastguard Worker static UClassID getStaticClassID(); 253*0e209d39SAndroid Build Coastguard Worker virtual UClassID getDynamicClassID() const override; 254*0e209d39SAndroid Build Coastguard Worker }; 255*0e209d39SAndroid Build Coastguard Worker 256*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END 257*0e209d39SAndroid Build Coastguard Worker 258*0e209d39SAndroid Build Coastguard Worker /* U_HAVE_RBNF */ 259*0e209d39SAndroid Build Coastguard Worker #endif 260*0e209d39SAndroid Build Coastguard Worker 261*0e209d39SAndroid Build Coastguard Worker // NFSUBS_H 262*0e209d39SAndroid Build Coastguard Worker #endif 263