nfsubs.h 10 KB

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