1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/i18n/nfsubs.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,266 @@ 1.4 +/* 1.5 +****************************************************************************** 1.6 +* Copyright (C) 1997-2007, International Business Machines 1.7 +* Corporation and others. All Rights Reserved. 1.8 +****************************************************************************** 1.9 +* file name: nfsubs.h 1.10 +* encoding: US-ASCII 1.11 +* tab size: 8 (not used) 1.12 +* indentation:4 1.13 +* 1.14 +* Modification history 1.15 +* Date Name Comments 1.16 +* 10/11/2001 Doug Ported from ICU4J 1.17 +*/ 1.18 + 1.19 +#ifndef NFSUBS_H 1.20 +#define NFSUBS_H 1.21 + 1.22 +#include "unicode/utypes.h" 1.23 +#include "unicode/uobject.h" 1.24 +#include "nfrule.h" 1.25 + 1.26 +#if U_HAVE_RBNF 1.27 + 1.28 +#include "unicode/utypes.h" 1.29 +#include "unicode/decimfmt.h" 1.30 +#include "nfrs.h" 1.31 +#include <float.h> 1.32 + 1.33 +U_NAMESPACE_BEGIN 1.34 + 1.35 +class NFSubstitution : public UObject { 1.36 + int32_t pos; 1.37 + const NFRuleSet* ruleSet; 1.38 + const DecimalFormat* numberFormat; 1.39 + 1.40 +protected: 1.41 + NFSubstitution(int32_t pos, 1.42 + const NFRuleSet* ruleSet, 1.43 + const RuleBasedNumberFormat* rbnf, 1.44 + const UnicodeString& description, 1.45 + UErrorCode& status); 1.46 + 1.47 + /** 1.48 + * Get the Ruleset of the object. 1.49 + * @return the Ruleset of the object. 1.50 + */ 1.51 + const NFRuleSet* getRuleSet() const { return ruleSet; } 1.52 + 1.53 + /** 1.54 + * get the NumberFormat of this object. 1.55 + * @return the numberformat of this object. 1.56 + */ 1.57 + const DecimalFormat* getNumberFormat() const { return numberFormat; } 1.58 + 1.59 +public: 1.60 + static NFSubstitution* makeSubstitution(int32_t pos, 1.61 + const NFRule* rule, 1.62 + const NFRule* predecessor, 1.63 + const NFRuleSet* ruleSet, 1.64 + const RuleBasedNumberFormat* rbnf, 1.65 + const UnicodeString& description, 1.66 + UErrorCode& status); 1.67 + 1.68 + /** 1.69 + * Destructor. 1.70 + */ 1.71 + virtual ~NFSubstitution(); 1.72 + 1.73 + /** 1.74 + * Return true if the given Format objects are semantically equal. 1.75 + * Objects of different subclasses are considered unequal. 1.76 + * @param rhs the object to be compared with. 1.77 + * @return true if the given Format objects are semantically equal. 1.78 + */ 1.79 + virtual UBool operator==(const NFSubstitution& rhs) const; 1.80 + 1.81 + /** 1.82 + * Return true if the given Format objects are semantically unequal. 1.83 + * Objects of different subclasses are considered unequal. 1.84 + * @param rhs the object to be compared with. 1.85 + * @return true if the given Format objects are semantically unequal. 1.86 + */ 1.87 + UBool operator!=(const NFSubstitution& rhs) const { return !operator==(rhs); } 1.88 + 1.89 + /** 1.90 + * Sets the substitution's divisor. Used by NFRule.setBaseValue(). 1.91 + * A no-op for all substitutions except multiplier and modulus 1.92 + * substitutions. 1.93 + * @param radix The radix of the divisor 1.94 + * @param exponent The exponent of the divisor 1.95 + */ 1.96 + virtual void setDivisor(int32_t radix, int32_t exponent, UErrorCode& status); 1.97 + 1.98 + /** 1.99 + * Replaces result with the string describing the substitution. 1.100 + * @param result Output param which will receive the string. 1.101 + */ 1.102 + virtual void toString(UnicodeString& result) const; 1.103 + 1.104 + //----------------------------------------------------------------------- 1.105 + // formatting 1.106 + //----------------------------------------------------------------------- 1.107 + 1.108 + /** 1.109 + * Performs a mathematical operation on the number, formats it using 1.110 + * either ruleSet or decimalFormat, and inserts the result into 1.111 + * toInsertInto. 1.112 + * @param number The number being formatted. 1.113 + * @param toInsertInto The string we insert the result into 1.114 + * @param pos The position in toInsertInto where the owning rule's 1.115 + * rule text begins (this value is added to this substitution's 1.116 + * position to determine exactly where to insert the new text) 1.117 + */ 1.118 + virtual void doSubstitution(int64_t number, UnicodeString& toInsertInto, int32_t pos) const; 1.119 + 1.120 + /** 1.121 + * Performs a mathematical operation on the number, formats it using 1.122 + * either ruleSet or decimalFormat, and inserts the result into 1.123 + * toInsertInto. 1.124 + * @param number The number being formatted. 1.125 + * @param toInsertInto The string we insert the result into 1.126 + * @param pos The position in toInsertInto where the owning rule's 1.127 + * rule text begins (this value is added to this substitution's 1.128 + * position to determine exactly where to insert the new text) 1.129 + */ 1.130 + virtual void doSubstitution(double number, UnicodeString& toInsertInto, int32_t pos) const; 1.131 + 1.132 +protected: 1.133 + /** 1.134 + * Subclasses override this function to perform some kind of 1.135 + * mathematical operation on the number. The result of this operation 1.136 + * is formatted using the rule set or DecimalFormat that this 1.137 + * substitution refers to, and the result is inserted into the result 1.138 + * string. 1.139 + * @param The number being formatted 1.140 + * @return The result of performing the opreration on the number 1.141 + */ 1.142 + virtual int64_t transformNumber(int64_t number) const = 0; 1.143 + 1.144 + /** 1.145 + * Subclasses override this function to perform some kind of 1.146 + * mathematical operation on the number. The result of this operation 1.147 + * is formatted using the rule set or DecimalFormat that this 1.148 + * substitution refers to, and the result is inserted into the result 1.149 + * string. 1.150 + * @param The number being formatted 1.151 + * @return The result of performing the opreration on the number 1.152 + */ 1.153 + virtual double transformNumber(double number) const = 0; 1.154 + 1.155 +public: 1.156 + //----------------------------------------------------------------------- 1.157 + // parsing 1.158 + //----------------------------------------------------------------------- 1.159 + 1.160 + /** 1.161 + * Parses a string using the rule set or DecimalFormat belonging 1.162 + * to this substitution. If there's a match, a mathematical 1.163 + * operation (the inverse of the one used in formatting) is 1.164 + * performed on the result of the parse and the value passed in 1.165 + * and returned as the result. The parse position is updated to 1.166 + * point to the first unmatched character in the string. 1.167 + * @param text The string to parse 1.168 + * @param parsePosition On entry, ignored, but assumed to be 0. 1.169 + * On exit, this is updated to point to the first unmatched 1.170 + * character (or 0 if the substitution didn't match) 1.171 + * @param baseValue A partial parse result that should be 1.172 + * combined with the result of this parse 1.173 + * @param upperBound When searching the rule set for a rule 1.174 + * matching the string passed in, only rules with base values 1.175 + * lower than this are considered 1.176 + * @param lenientParse If true and matching against rules fails, 1.177 + * the substitution will also try matching the text against 1.178 + * numerals using a default-costructed NumberFormat. If false, 1.179 + * no extra work is done. (This value is false whenever the 1.180 + * formatter isn't in lenient-parse mode, but is also false 1.181 + * under some conditions even when the formatter _is_ in 1.182 + * lenient-parse mode.) 1.183 + * @return If there's a match, this is the result of composing 1.184 + * baseValue with whatever was returned from matching the 1.185 + * characters. This will be either a Long or a Double. If there's 1.186 + * no match this is new Long(0) (not null), and parsePosition 1.187 + * is left unchanged. 1.188 + */ 1.189 + virtual UBool doParse(const UnicodeString& text, 1.190 + ParsePosition& parsePosition, 1.191 + double baseValue, 1.192 + double upperBound, 1.193 + UBool lenientParse, 1.194 + Formattable& result) const; 1.195 + 1.196 + /** 1.197 + * Derives a new value from the two values passed in. The two values 1.198 + * are typically either the base values of two rules (the one containing 1.199 + * the substitution and the one matching the substitution) or partial 1.200 + * parse results derived in some other way. The operation is generally 1.201 + * the inverse of the operation performed by transformNumber(). 1.202 + * @param newRuleValue The value produced by matching this substitution 1.203 + * @param oldRuleValue The value that was passed to the substitution 1.204 + * by the rule that owns it 1.205 + * @return A third value derived from the other two, representing a 1.206 + * partial parse result 1.207 + */ 1.208 + virtual double composeRuleValue(double newRuleValue, double oldRuleValue) const = 0; 1.209 + 1.210 + /** 1.211 + * Calculates an upper bound when searching for a rule that matches 1.212 + * this substitution. Rules with base values greater than or equal 1.213 + * to upperBound are not considered. 1.214 + * @param oldUpperBound The current upper-bound setting. The new 1.215 + * upper bound can't be any higher. 1.216 + * @return the upper bound when searching for a rule that matches 1.217 + * this substitution. 1.218 + */ 1.219 + virtual double calcUpperBound(double oldUpperBound) const = 0; 1.220 + 1.221 + //----------------------------------------------------------------------- 1.222 + // simple accessors 1.223 + //----------------------------------------------------------------------- 1.224 + 1.225 + /** 1.226 + * Returns the substitution's position in the rule that owns it. 1.227 + * @return The substitution's position in the rule that owns it. 1.228 + */ 1.229 + int32_t getPos() const { return pos; } 1.230 + 1.231 + /** 1.232 + * Returns the character used in the textual representation of 1.233 + * substitutions of this type. Used by toString(). 1.234 + * @return This substitution's token character. 1.235 + */ 1.236 + virtual UChar tokenChar() const = 0; 1.237 + 1.238 + /** 1.239 + * Returns true if this is a null substitution. (We didn't do this 1.240 + * with instanceof partially because it causes source files to 1.241 + * proliferate and partially because we have to port this to C++.) 1.242 + * @return true if this object is an instance of NullSubstitution 1.243 + */ 1.244 + virtual UBool isNullSubstitution() const; 1.245 + 1.246 + /** 1.247 + * Returns true if this is a modulus substitution. (We didn't do this 1.248 + * with instanceof partially because it causes source files to 1.249 + * proliferate and partially because we have to port this to C++.) 1.250 + * @return true if this object is an instance of ModulusSubstitution 1.251 + */ 1.252 + virtual UBool isModulusSubstitution() const; 1.253 + 1.254 +private: 1.255 + NFSubstitution(const NFSubstitution &other); // forbid copying of this class 1.256 + NFSubstitution &operator=(const NFSubstitution &other); // forbid copying of this class 1.257 + 1.258 +public: 1.259 + static UClassID getStaticClassID(void); 1.260 + virtual UClassID getDynamicClassID(void) const; 1.261 +}; 1.262 + 1.263 +U_NAMESPACE_END 1.264 + 1.265 +/* U_HAVE_RBNF */ 1.266 +#endif 1.267 + 1.268 +// NFSUBS_H 1.269 +#endif