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