rbnf.h 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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 Corporation and others.
  6. * All Rights Reserved.
  7. *******************************************************************************
  8. */
  9. #ifndef RBNF_H
  10. #define RBNF_H
  11. #include "unicode/utypes.h"
  12. #if U_SHOW_CPLUSPLUS_API
  13. /**
  14. * \file
  15. * \brief C++ API: Rule Based Number Format
  16. */
  17. /**
  18. * \def U_HAVE_RBNF
  19. * This will be 0 if RBNF support is not included in ICU
  20. * and 1 if it is.
  21. *
  22. * @stable ICU 2.4
  23. */
  24. #if UCONFIG_NO_FORMATTING
  25. #define U_HAVE_RBNF 0
  26. #else
  27. #define U_HAVE_RBNF 1
  28. #include "unicode/dcfmtsym.h"
  29. #include "unicode/fmtable.h"
  30. #include "unicode/locid.h"
  31. #include "unicode/numfmt.h"
  32. #include "unicode/unistr.h"
  33. #include "unicode/strenum.h"
  34. #include "unicode/brkiter.h"
  35. #include "unicode/upluralrules.h"
  36. U_NAMESPACE_BEGIN
  37. class NFRule;
  38. class NFRuleSet;
  39. class LocalizationInfo;
  40. class PluralFormat;
  41. class RuleBasedCollator;
  42. /**
  43. * Tags for the predefined rulesets.
  44. *
  45. * @stable ICU 2.2
  46. */
  47. enum URBNFRuleSetTag {
  48. /**
  49. * Requests predefined ruleset for spelling out numeric values in words.
  50. * @stable ICU 2.2
  51. */
  52. URBNF_SPELLOUT,
  53. /**
  54. * Requests predefined ruleset for the ordinal form of a number.
  55. * @stable ICU 2.2
  56. */
  57. URBNF_ORDINAL,
  58. /**
  59. * Requests predefined ruleset for formatting a value as a duration in hours, minutes, and seconds.
  60. * @stable ICU 2.2
  61. */
  62. URBNF_DURATION,
  63. /**
  64. * Requests predefined ruleset for various non-place-value numbering systems.
  65. * WARNING: The same resource contains rule sets for a variety of different numbering systems.
  66. * You need to call setDefaultRuleSet() on the formatter to choose the actual numbering system.
  67. * @stable ICU 2.2
  68. */
  69. URBNF_NUMBERING_SYSTEM,
  70. #ifndef U_HIDE_DEPRECATED_API
  71. /**
  72. * One more than the highest normal URBNFRuleSetTag value.
  73. * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
  74. */
  75. URBNF_COUNT
  76. #endif // U_HIDE_DEPRECATED_API
  77. };
  78. /**
  79. * The RuleBasedNumberFormat class formats numbers according to a set of rules. This number formatter is
  80. * typically used for spelling out numeric values in words (e.g., 25,3476 as
  81. * "twenty-five thousand three hundred seventy-six" or "vingt-cinq mille trois
  82. * cents soixante-seize" or
  83. * "fünfundzwanzigtausenddreihundertsechsundsiebzig"), but can also be used for
  84. * other complicated formatting tasks, such as formatting a number of seconds as hours,
  85. * minutes and seconds (e.g., 3,730 as "1:02:10").
  86. *
  87. * <p>The resources contain three predefined formatters for each locale: spellout, which
  88. * spells out a value in words (123 is &quot;one hundred twenty-three&quot;); ordinal, which
  89. * appends an ordinal suffix to the end of a numeral (123 is &quot;123rd&quot;); and
  90. * duration, which shows a duration in seconds as hours, minutes, and seconds (123 is
  91. * &quot;2:03&quot;).&nbsp; The client can also define more specialized <tt>RuleBasedNumberFormat</tt>s
  92. * by supplying programmer-defined rule sets.</p>
  93. *
  94. * <p>The behavior of a <tt>RuleBasedNumberFormat</tt> is specified by a textual description
  95. * that is either passed to the constructor as a <tt>String</tt> or loaded from a resource
  96. * bundle. In its simplest form, the description consists of a semicolon-delimited list of <em>rules.</em>
  97. * Each rule has a string of output text and a value or range of values it is applicable to.
  98. * In a typical spellout rule set, the first twenty rules are the words for the numbers from
  99. * 0 to 19:</p>
  100. *
  101. * <pre>zero; one; two; three; four; five; six; seven; eight; nine;
  102. * ten; eleven; twelve; thirteen; fourteen; fifteen; sixteen; seventeen; eighteen; nineteen;</pre>
  103. *
  104. * <p>For larger numbers, we can use the preceding set of rules to format the ones place, and
  105. * we only have to supply the words for the multiples of 10:</p>
  106. *
  107. * <pre> 20: twenty[-&gt;&gt;];
  108. * 30: thirty[-&gt;&gt;];
  109. * 40: forty[-&gt;&gt;];
  110. * 50: fifty[-&gt;&gt;];
  111. * 60: sixty[-&gt;&gt;];
  112. * 70: seventy[-&gt;&gt;];
  113. * 80: eighty[-&gt;&gt;];
  114. * 90: ninety[-&gt;&gt;];</pre>
  115. *
  116. * <p>In these rules, the <em>base value</em> is spelled out explicitly and set off from the
  117. * rule's output text with a colon. The rules are in a sorted list, and a rule is applicable
  118. * to all numbers from its own base value to one less than the next rule's base value. The
  119. * &quot;&gt;&gt;&quot; token is called a <em>substitution</em> and tells the formatter to
  120. * isolate the number's ones digit, format it using this same set of rules, and place the
  121. * result at the position of the &quot;&gt;&gt;&quot; token. Text in brackets is omitted if
  122. * the number being formatted is an even multiple of 10 (the hyphen is a literal hyphen; 24
  123. * is &quot;twenty-four,&quot; not &quot;twenty four&quot;).</p>
  124. *
  125. * <p>For even larger numbers, we can actually look up several parts of the number in the
  126. * list:</p>
  127. *
  128. * <pre>100: &lt;&lt; hundred[ &gt;&gt;];</pre>
  129. *
  130. * <p>The &quot;&lt;&lt;&quot; represents a new kind of substitution. The &lt;&lt; isolates
  131. * the hundreds digit (and any digits to its left), formats it using this same rule set, and
  132. * places the result where the &quot;&lt;&lt;&quot; was. Notice also that the meaning of
  133. * &gt;&gt; has changed: it now refers to both the tens and the ones digits. The meaning of
  134. * both substitutions depends on the rule's base value. The base value determines the rule's <em>divisor,</em>
  135. * which is the highest power of 10 that is less than or equal to the base value (the user
  136. * can change this). To fill in the substitutions, the formatter divides the number being
  137. * formatted by the divisor. The integral quotient is used to fill in the &lt;&lt;
  138. * substitution, and the remainder is used to fill in the &gt;&gt; substitution. The meaning
  139. * of the brackets changes similarly: text in brackets is omitted if the value being
  140. * formatted is an even multiple of the rule's divisor. The rules are applied recursively, so
  141. * if a substitution is filled in with text that includes another substitution, that
  142. * substitution is also filled in.</p>
  143. *
  144. * <p>This rule covers values up to 999, at which point we add another rule:</p>
  145. *
  146. * <pre>1000: &lt;&lt; thousand[ &gt;&gt;];</pre>
  147. *
  148. * <p>Again, the meanings of the brackets and substitution tokens shift because the rule's
  149. * base value is a higher power of 10, changing the rule's divisor. This rule can actually be
  150. * used all the way up to 999,999. This allows us to finish out the rules as follows:</p>
  151. *
  152. * <pre> 1,000,000: &lt;&lt; million[ &gt;&gt;];
  153. * 1,000,000,000: &lt;&lt; billion[ &gt;&gt;];
  154. * 1,000,000,000,000: &lt;&lt; trillion[ &gt;&gt;];
  155. * 1,000,000,000,000,000: OUT OF RANGE!;</pre>
  156. *
  157. * <p>Commas, periods, and spaces can be used in the base values to improve legibility and
  158. * are ignored by the rule parser. The last rule in the list is customarily treated as an
  159. * &quot;overflow rule,&quot; applying to everything from its base value on up, and often (as
  160. * in this example) being used to print out an error message or default representation.
  161. * Notice also that the size of the major groupings in large numbers is controlled by the
  162. * spacing of the rules: because in English we group numbers by thousand, the higher rules
  163. * are separated from each other by a factor of 1,000.</p>
  164. *
  165. * <p>To see how these rules actually work in practice, consider the following example:
  166. * Formatting 25,430 with this rule set would work like this:</p>
  167. *
  168. * <table border="0" width="100%">
  169. * <tr>
  170. * <td><strong>&lt;&lt; thousand &gt;&gt;</strong></td>
  171. * <td>[the rule whose base value is 1,000 is applicable to 25,340]</td>
  172. * </tr>
  173. * <tr>
  174. * <td><strong>twenty-&gt;&gt;</strong> thousand &gt;&gt;</td>
  175. * <td>[25,340 over 1,000 is 25. The rule for 20 applies.]</td>
  176. * </tr>
  177. * <tr>
  178. * <td>twenty-<strong>five</strong> thousand &gt;&gt;</td>
  179. * <td>[25 mod 10 is 5. The rule for 5 is &quot;five.&quot;</td>
  180. * </tr>
  181. * <tr>
  182. * <td>twenty-five thousand <strong>&lt;&lt; hundred &gt;&gt;</strong></td>
  183. * <td>[25,340 mod 1,000 is 340. The rule for 100 applies.]</td>
  184. * </tr>
  185. * <tr>
  186. * <td>twenty-five thousand <strong>three</strong> hundred &gt;&gt;</td>
  187. * <td>[340 over 100 is 3. The rule for 3 is &quot;three.&quot;]</td>
  188. * </tr>
  189. * <tr>
  190. * <td>twenty-five thousand three hundred <strong>forty</strong></td>
  191. * <td>[340 mod 100 is 40. The rule for 40 applies. Since 40 divides
  192. * evenly by 10, the hyphen and substitution in the brackets are omitted.]</td>
  193. * </tr>
  194. * </table>
  195. *
  196. * <p>The above syntax suffices only to format positive integers. To format negative numbers,
  197. * we add a special rule:</p>
  198. *
  199. * <pre>-x: minus &gt;&gt;;</pre>
  200. *
  201. * <p>This is called a <em>negative-number rule,</em> and is identified by &quot;-x&quot;
  202. * where the base value would be. This rule is used to format all negative numbers. the
  203. * &gt;&gt; token here means &quot;find the number's absolute value, format it with these
  204. * rules, and put the result here.&quot;</p>
  205. *
  206. * <p>We also add a special rule called a <em>fraction rule </em>for numbers with fractional
  207. * parts:</p>
  208. *
  209. * <pre>x.x: &lt;&lt; point &gt;&gt;;</pre>
  210. *
  211. * <p>This rule is used for all positive non-integers (negative non-integers pass through the
  212. * negative-number rule first and then through this rule). Here, the &lt;&lt; token refers to
  213. * the number's integral part, and the &gt;&gt; to the number's fractional part. The
  214. * fractional part is formatted as a series of single-digit numbers (e.g., 123.456 would be
  215. * formatted as &quot;one hundred twenty-three point four five six&quot;).</p>
  216. *
  217. * <p>To see how this rule syntax is applied to various languages, examine the resource data.</p>
  218. *
  219. * <p>There is actually much more flexibility built into the rule language than the
  220. * description above shows. A formatter may own multiple rule sets, which can be selected by
  221. * the caller, and which can use each other to fill in their substitutions. Substitutions can
  222. * also be filled in with digits, using a DecimalFormat object. There is syntax that can be
  223. * used to alter a rule's divisor in various ways. And there is provision for much more
  224. * flexible fraction handling. A complete description of the rule syntax follows:</p>
  225. *
  226. * <hr>
  227. *
  228. * <p>The description of a <tt>RuleBasedNumberFormat</tt>'s behavior consists of one or more <em>rule
  229. * sets.</em> Each rule set consists of a name, a colon, and a list of <em>rules.</em> A rule
  230. * set name must begin with a % sign. Rule sets with names that begin with a single % sign
  231. * are <em>public:</em> the caller can specify that they be used to format and parse numbers.
  232. * Rule sets with names that begin with %% are <em>private:</em> they exist only for the use
  233. * of other rule sets. If a formatter only has one rule set, the name may be omitted.</p>
  234. *
  235. * <p>The user can also specify a special &quot;rule set&quot; named <tt>%%lenient-parse</tt>.
  236. * The body of <tt>%%lenient-parse</tt> isn't a set of number-formatting rules, but a <tt>RuleBasedCollator</tt>
  237. * description which is used to define equivalences for lenient parsing. For more information
  238. * on the syntax, see <tt>RuleBasedCollator</tt>. For more information on lenient parsing,
  239. * see <tt>setLenientParse()</tt>. <em>Note:</em> symbols that have syntactic meaning
  240. * in collation rules, such as '&amp;', have no particular meaning when appearing outside
  241. * of the <tt>lenient-parse</tt> rule set.</p>
  242. *
  243. * <p>The body of a rule set consists of an ordered, semicolon-delimited list of <em>rules.</em>
  244. * Internally, every rule has a base value, a divisor, rule text, and zero, one, or two <em>substitutions.</em>
  245. * These parameters are controlled by the description syntax, which consists of a <em>rule
  246. * descriptor,</em> a colon, and a <em>rule body.</em></p>
  247. *
  248. * <p>A rule descriptor can take one of the following forms (text in <em>italics</em> is the
  249. * name of a token):</p>
  250. *
  251. * <table border="0" width="100%">
  252. * <tr>
  253. * <td><em>bv</em>:</td>
  254. * <td><em>bv</em> specifies the rule's base value. <em>bv</em> is a decimal
  255. * number expressed using ASCII digits. <em>bv</em> may contain spaces, period, and commas,
  256. * which are ignored. The rule's divisor is the highest power of 10 less than or equal to
  257. * the base value.</td>
  258. * </tr>
  259. * <tr>
  260. * <td><em>bv</em>/<em>rad</em>:</td>
  261. * <td><em>bv</em> specifies the rule's base value. The rule's divisor is the
  262. * highest power of <em>rad</em> less than or equal to the base value.</td>
  263. * </tr>
  264. * <tr>
  265. * <td><em>bv</em>&gt;:</td>
  266. * <td><em>bv</em> specifies the rule's base value. To calculate the divisor,
  267. * let the radix be 10, and the exponent be the highest exponent of the radix that yields a
  268. * result less than or equal to the base value. Every &gt; character after the base value
  269. * decreases the exponent by 1. If the exponent is positive or 0, the divisor is the radix
  270. * raised to the power of the exponent; otherwise, the divisor is 1.</td>
  271. * </tr>
  272. * <tr>
  273. * <td><em>bv</em>/<em>rad</em>&gt;:</td>
  274. * <td><em>bv</em> specifies the rule's base value. To calculate the divisor,
  275. * let the radix be <em>rad</em>, and the exponent be the highest exponent of the radix that
  276. * yields a result less than or equal to the base value. Every &gt; character after the radix
  277. * decreases the exponent by 1. If the exponent is positive or 0, the divisor is the radix
  278. * raised to the power of the exponent; otherwise, the divisor is 1.</td>
  279. * </tr>
  280. * <tr>
  281. * <td>-x:</td>
  282. * <td>The rule is a negative-number rule.</td>
  283. * </tr>
  284. * <tr>
  285. * <td>x.x:</td>
  286. * <td>The rule is an <em>improper fraction rule</em>. If the full stop in
  287. * the middle of the rule name is replaced with the decimal point
  288. * that is used in the language or DecimalFormatSymbols, then that rule will
  289. * have precedence when formatting and parsing this rule. For example, some
  290. * languages use the comma, and can thus be written as x,x instead. For example,
  291. * you can use "x.x: &lt;&lt; point &gt;&gt;;x,x: &lt;&lt; comma &gt;&gt;;" to
  292. * handle the decimal point that matches the language's natural spelling of
  293. * the punctuation of either the full stop or comma.</td>
  294. * </tr>
  295. * <tr>
  296. * <td>0.x:</td>
  297. * <td>The rule is a <em>proper fraction rule</em>. If the full stop in
  298. * the middle of the rule name is replaced with the decimal point
  299. * that is used in the language or DecimalFormatSymbols, then that rule will
  300. * have precedence when formatting and parsing this rule. For example, some
  301. * languages use the comma, and can thus be written as 0,x instead. For example,
  302. * you can use "0.x: point &gt;&gt;;0,x: comma &gt;&gt;;" to
  303. * handle the decimal point that matches the language's natural spelling of
  304. * the punctuation of either the full stop or comma.</td>
  305. * </tr>
  306. * <tr>
  307. * <td>x.0:</td>
  308. * <td>The rule is a <em>default rule</em>. If the full stop in
  309. * the middle of the rule name is replaced with the decimal point
  310. * that is used in the language or DecimalFormatSymbols, then that rule will
  311. * have precedence when formatting and parsing this rule. For example, some
  312. * languages use the comma, and can thus be written as x,0 instead. For example,
  313. * you can use "x.0: &lt;&lt; point;x,0: &lt;&lt; comma;" to
  314. * handle the decimal point that matches the language's natural spelling of
  315. * the punctuation of either the full stop or comma.</td>
  316. * </tr>
  317. * <tr>
  318. * <td>Inf:</td>
  319. * <td>The rule for infinity.</td>
  320. * </tr>
  321. * <tr>
  322. * <td>NaN:</td>
  323. * <td>The rule for an IEEE 754 NaN (not a number).</td>
  324. * </tr>
  325. * <tr>
  326. * <td><em>nothing</em></td>
  327. * <td>If the rule's rule descriptor is left out, the base value is one plus the
  328. * preceding rule's base value (or zero if this is the first rule in the list) in a normal
  329. * rule set.&nbsp; In a fraction rule set, the base value is the same as the preceding rule's
  330. * base value.</td>
  331. * </tr>
  332. * </table>
  333. *
  334. * <p>A rule set may be either a regular rule set or a <em>fraction rule set,</em> depending
  335. * on whether it is used to format a number's integral part (or the whole number) or a
  336. * number's fractional part. Using a rule set to format a rule's fractional part makes it a
  337. * fraction rule set.</p>
  338. *
  339. * <p>Which rule is used to format a number is defined according to one of the following
  340. * algorithms: If the rule set is a regular rule set, do the following:
  341. *
  342. * <ul>
  343. * <li>If the rule set includes a default rule (and the number was passed in as a <tt>double</tt>),
  344. * use the default rule.&nbsp; (If the number being formatted was passed in as a <tt>long</tt>,
  345. * the default rule is ignored.)</li>
  346. * <li>If the number is negative, use the negative-number rule.</li>
  347. * <li>If the number has a fractional part and is greater than 1, use the improper fraction
  348. * rule.</li>
  349. * <li>If the number has a fractional part and is between 0 and 1, use the proper fraction
  350. * rule.</li>
  351. * <li>Binary-search the rule list for the rule with the highest base value less than or equal
  352. * to the number. If that rule has two substitutions, its base value is not an even multiple
  353. * of its divisor, and the number <em>is</em> an even multiple of the rule's divisor, use the
  354. * rule that precedes it in the rule list. Otherwise, use the rule itself.</li>
  355. * </ul>
  356. *
  357. * <p>If the rule set is a fraction rule set, do the following:
  358. *
  359. * <ul>
  360. * <li>Ignore negative-number and fraction rules.</li>
  361. * <li>For each rule in the list, multiply the number being formatted (which will always be
  362. * between 0 and 1) by the rule's base value. Keep track of the distance between the result
  363. * the nearest integer.</li>
  364. * <li>Use the rule that produced the result closest to zero in the above calculation. In the
  365. * event of a tie or a direct hit, use the first matching rule encountered. (The idea here is
  366. * to try each rule's base value as a possible denominator of a fraction. Whichever
  367. * denominator produces the fraction closest in value to the number being formatted wins.) If
  368. * the rule following the matching rule has the same base value, use it if the numerator of
  369. * the fraction is anything other than 1; if the numerator is 1, use the original matching
  370. * rule. (This is to allow singular and plural forms of the rule text without a lot of extra
  371. * hassle.)</li>
  372. * </ul>
  373. *
  374. * <p>A rule's body consists of a string of characters terminated by a semicolon. The rule
  375. * may include zero, one, or two <em>substitution tokens,</em> and a range of text in
  376. * brackets. The brackets denote optional text (and may also include one or both
  377. * substitutions). The exact meanings of the substitution tokens, and under what conditions
  378. * optional text is omitted, depend on the syntax of the substitution token and the context.
  379. * The rest of the text in a rule body is literal text that is output when the rule matches
  380. * the number being formatted.</p>
  381. *
  382. * <p>A substitution token begins and ends with a <em>token character.</em> The token
  383. * character and the context together specify a mathematical operation to be performed on the
  384. * number being formatted. An optional <em>substitution descriptor </em>specifies how the
  385. * value resulting from that operation is used to fill in the substitution. The position of
  386. * the substitution token in the rule body specifies the location of the resultant text in
  387. * the original rule text.</p>
  388. *
  389. * <p>The meanings of the substitution token characters are as follows:</p>
  390. *
  391. * <table border="0" width="100%">
  392. * <tr>
  393. * <td>&gt;&gt;</td>
  394. * <td>in normal rule</td>
  395. * <td>Divide the number by the rule's divisor and format the remainder</td>
  396. * </tr>
  397. * <tr>
  398. * <td></td>
  399. * <td>in negative-number rule</td>
  400. * <td>Find the absolute value of the number and format the result</td>
  401. * </tr>
  402. * <tr>
  403. * <td></td>
  404. * <td>in fraction or default rule</td>
  405. * <td>Isolate the number's fractional part and format it.</td>
  406. * </tr>
  407. * <tr>
  408. * <td></td>
  409. * <td>in rule in fraction rule set</td>
  410. * <td>Not allowed.</td>
  411. * </tr>
  412. * <tr>
  413. * <td>&gt;&gt;&gt;</td>
  414. * <td>in normal rule</td>
  415. * <td>Divide the number by the rule's divisor and format the remainder,
  416. * but bypass the normal rule-selection process and just use the
  417. * rule that precedes this one in this rule list.</td>
  418. * </tr>
  419. * <tr>
  420. * <td></td>
  421. * <td>in all other rules</td>
  422. * <td>Not allowed.</td>
  423. * </tr>
  424. * <tr>
  425. * <td>&lt;&lt;</td>
  426. * <td>in normal rule</td>
  427. * <td>Divide the number by the rule's divisor and format the quotient</td>
  428. * </tr>
  429. * <tr>
  430. * <td></td>
  431. * <td>in negative-number rule</td>
  432. * <td>Not allowed.</td>
  433. * </tr>
  434. * <tr>
  435. * <td></td>
  436. * <td>in fraction or default rule</td>
  437. * <td>Isolate the number's integral part and format it.</td>
  438. * </tr>
  439. * <tr>
  440. * <td></td>
  441. * <td>in rule in fraction rule set</td>
  442. * <td>Multiply the number by the rule's base value and format the result.</td>
  443. * </tr>
  444. * <tr>
  445. * <td>==</td>
  446. * <td>in all rule sets</td>
  447. * <td>Format the number unchanged</td>
  448. * </tr>
  449. * <tr>
  450. * <td>[]</td>
  451. * <td>in normal rule</td>
  452. * <td>Omit the optional text if the number is an even multiple of the rule's divisor</td>
  453. * </tr>
  454. * <tr>
  455. * <td></td>
  456. * <td>in negative-number rule</td>
  457. * <td>Not allowed.</td>
  458. * </tr>
  459. * <tr>
  460. * <td></td>
  461. * <td>in improper-fraction rule</td>
  462. * <td>Omit the optional text if the number is between 0 and 1 (same as specifying both an
  463. * x.x rule and a 0.x rule)</td>
  464. * </tr>
  465. * <tr>
  466. * <td></td>
  467. * <td>in default rule</td>
  468. * <td>Omit the optional text if the number is an integer (same as specifying both an x.x
  469. * rule and an x.0 rule)</td>
  470. * </tr>
  471. * <tr>
  472. * <td></td>
  473. * <td>in proper-fraction rule</td>
  474. * <td>Not allowed.</td>
  475. * </tr>
  476. * <tr>
  477. * <td></td>
  478. * <td>in rule in fraction rule set</td>
  479. * <td>Omit the optional text if multiplying the number by the rule's base value yields 1.</td>
  480. * </tr>
  481. * <tr>
  482. * <td width="37">$(cardinal,<i>plural syntax</i>)$</td>
  483. * <td width="23"></td>
  484. * <td width="165" valign="top">in all rule sets</td>
  485. * <td>This provides the ability to choose a word based on the number divided by the radix to the power of the
  486. * exponent of the base value for the specified locale, which is normally equivalent to the &lt;&lt; value.
  487. * This uses the cardinal plural rules from PluralFormat. All strings used in the plural format are treated
  488. * as the same base value for parsing.</td>
  489. * </tr>
  490. * <tr>
  491. * <td width="37">$(ordinal,<i>plural syntax</i>)$</td>
  492. * <td width="23"></td>
  493. * <td width="165" valign="top">in all rule sets</td>
  494. * <td>This provides the ability to choose a word based on the number divided by the radix to the power of the
  495. * exponent of the base value for the specified locale, which is normally equivalent to the &lt;&lt; value.
  496. * This uses the ordinal plural rules from PluralFormat. All strings used in the plural format are treated
  497. * as the same base value for parsing.</td>
  498. * </tr>
  499. * </table>
  500. *
  501. * <p>The substitution descriptor (i.e., the text between the token characters) may take one
  502. * of three forms:</p>
  503. *
  504. * <table border="0" width="100%">
  505. * <tr>
  506. * <td>a rule set name</td>
  507. * <td>Perform the mathematical operation on the number, and format the result using the
  508. * named rule set.</td>
  509. * </tr>
  510. * <tr>
  511. * <td>a DecimalFormat pattern</td>
  512. * <td>Perform the mathematical operation on the number, and format the result using a
  513. * DecimalFormat with the specified pattern.&nbsp; The pattern must begin with 0 or #.</td>
  514. * </tr>
  515. * <tr>
  516. * <td>nothing</td>
  517. * <td>Perform the mathematical operation on the number, and format the result using the rule
  518. * set containing the current rule, except:
  519. * <ul>
  520. * <li>You can't have an empty substitution descriptor with a == substitution.</li>
  521. * <li>If you omit the substitution descriptor in a &gt;&gt; substitution in a fraction rule,
  522. * format the result one digit at a time using the rule set containing the current rule.</li>
  523. * <li>If you omit the substitution descriptor in a &lt;&lt; substitution in a rule in a
  524. * fraction rule set, format the result using the default rule set for this formatter.</li>
  525. * </ul>
  526. * </td>
  527. * </tr>
  528. * </table>
  529. *
  530. * <p>Whitespace is ignored between a rule set name and a rule set body, between a rule
  531. * descriptor and a rule body, or between rules. If a rule body begins with an apostrophe,
  532. * the apostrophe is ignored, but all text after it becomes significant (this is how you can
  533. * have a rule's rule text begin with whitespace). There is no escape function: the semicolon
  534. * is not allowed in rule set names or in rule text, and the colon is not allowed in rule set
  535. * names. The characters beginning a substitution token are always treated as the beginning
  536. * of a substitution token.</p>
  537. *
  538. * <p>See the resource data and the demo program for annotated examples of real rule sets
  539. * using these features.</p>
  540. *
  541. * <p><em>User subclasses are not supported.</em> While clients may write
  542. * subclasses, such code will not necessarily work and will not be
  543. * guaranteed to work stably from release to release.
  544. *
  545. * <p><b>Localizations</b></p>
  546. * <p>Constructors are available that allow the specification of localizations for the
  547. * public rule sets (and also allow more control over what public rule sets are available).
  548. * Localization data is represented as a textual description. The description represents
  549. * an array of arrays of string. The first element is an array of the public rule set names,
  550. * each of these must be one of the public rule set names that appear in the rules. Only
  551. * names in this array will be treated as public rule set names by the API. Each subsequent
  552. * element is an array of localizations of these names. The first element of one of these
  553. * subarrays is the locale name, and the remaining elements are localizations of the
  554. * public rule set names, in the same order as they were listed in the first array.</p>
  555. * <p>In the syntax, angle brackets '<', '>' are used to delimit the arrays, and comma ',' is used
  556. * to separate elements of an array. Whitespace is ignored, unless quoted.</p>
  557. * <p>For example:<pre>
  558. * < < %foo, %bar, %baz >,
  559. * < en, Foo, Bar, Baz >,
  560. * < fr, 'le Foo', 'le Bar', 'le Baz' >
  561. * < zh, \\u7532, \\u4e59, \\u4e19 > >
  562. * </pre></p>
  563. * @author Richard Gillam
  564. * @see NumberFormat
  565. * @see DecimalFormat
  566. * @see PluralFormat
  567. * @see PluralRules
  568. * @stable ICU 2.0
  569. */
  570. class U_I18N_API RuleBasedNumberFormat : public NumberFormat {
  571. public:
  572. //-----------------------------------------------------------------------
  573. // constructors
  574. //-----------------------------------------------------------------------
  575. /**
  576. * Creates a RuleBasedNumberFormat that behaves according to the description
  577. * passed in. The formatter uses the default locale.
  578. * @param rules A description of the formatter's desired behavior.
  579. * See the class documentation for a complete explanation of the description
  580. * syntax.
  581. * @param perror The parse error if an error was encountered.
  582. * @param status The status indicating whether the constructor succeeded.
  583. * @stable ICU 3.2
  584. */
  585. RuleBasedNumberFormat(const UnicodeString& rules, UParseError& perror, UErrorCode& status);
  586. /**
  587. * Creates a RuleBasedNumberFormat that behaves according to the description
  588. * passed in. The formatter uses the default locale.
  589. * <p>
  590. * The localizations data provides information about the public
  591. * rule sets and their localized display names for different
  592. * locales. The first element in the list is an array of the names
  593. * of the public rule sets. The first element in this array is
  594. * the initial default ruleset. The remaining elements in the
  595. * list are arrays of localizations of the names of the public
  596. * rule sets. Each of these is one longer than the initial array,
  597. * with the first String being the ULocale ID, and the remaining
  598. * Strings being the localizations of the rule set names, in the
  599. * same order as the initial array. Arrays are nullptr-terminated.
  600. * @param rules A description of the formatter's desired behavior.
  601. * See the class documentation for a complete explanation of the description
  602. * syntax.
  603. * @param localizations the localization information.
  604. * names in the description. These will be copied by the constructor.
  605. * @param perror The parse error if an error was encountered.
  606. * @param status The status indicating whether the constructor succeeded.
  607. * @stable ICU 3.2
  608. */
  609. RuleBasedNumberFormat(const UnicodeString& rules, const UnicodeString& localizations,
  610. UParseError& perror, UErrorCode& status);
  611. /**
  612. * Creates a RuleBasedNumberFormat that behaves according to the rules
  613. * passed in. The formatter uses the specified locale to determine the
  614. * characters to use when formatting numerals, and to define equivalences
  615. * for lenient parsing.
  616. * @param rules The formatter rules.
  617. * See the class documentation for a complete explanation of the rule
  618. * syntax.
  619. * @param locale A locale that governs which characters are used for
  620. * formatting values in numerals and which characters are equivalent in
  621. * lenient parsing.
  622. * @param perror The parse error if an error was encountered.
  623. * @param status The status indicating whether the constructor succeeded.
  624. * @stable ICU 2.0
  625. */
  626. RuleBasedNumberFormat(const UnicodeString& rules, const Locale& locale,
  627. UParseError& perror, UErrorCode& status);
  628. /**
  629. * Creates a RuleBasedNumberFormat that behaves according to the description
  630. * passed in. The formatter uses the default locale.
  631. * <p>
  632. * The localizations data provides information about the public
  633. * rule sets and their localized display names for different
  634. * locales. The first element in the list is an array of the names
  635. * of the public rule sets. The first element in this array is
  636. * the initial default ruleset. The remaining elements in the
  637. * list are arrays of localizations of the names of the public
  638. * rule sets. Each of these is one longer than the initial array,
  639. * with the first String being the ULocale ID, and the remaining
  640. * Strings being the localizations of the rule set names, in the
  641. * same order as the initial array. Arrays are nullptr-terminated.
  642. * @param rules A description of the formatter's desired behavior.
  643. * See the class documentation for a complete explanation of the description
  644. * syntax.
  645. * @param localizations a list of localizations for the rule set
  646. * names in the description. These will be copied by the constructor.
  647. * @param locale A locale that governs which characters are used for
  648. * formatting values in numerals and which characters are equivalent in
  649. * lenient parsing.
  650. * @param perror The parse error if an error was encountered.
  651. * @param status The status indicating whether the constructor succeeded.
  652. * @stable ICU 3.2
  653. */
  654. RuleBasedNumberFormat(const UnicodeString& rules, const UnicodeString& localizations,
  655. const Locale& locale, UParseError& perror, UErrorCode& status);
  656. /**
  657. * Creates a RuleBasedNumberFormat from a predefined ruleset. The selector
  658. * code chose among three possible predefined formats: spellout, ordinal,
  659. * and duration.
  660. * @param tag A selector code specifying which kind of formatter to create for that
  661. * locale. There are four legal values: URBNF_SPELLOUT, which creates a formatter that
  662. * spells out a value in words in the desired language, URBNF_ORDINAL, which attaches
  663. * an ordinal suffix from the desired language to the end of a number (e.g. "123rd"),
  664. * URBNF_DURATION, which formats a duration in seconds as hours, minutes, and seconds always rounding down,
  665. * and URBNF_NUMBERING_SYSTEM, which is used to invoke rules for alternate numbering
  666. * systems such as the Hebrew numbering system, or for Roman Numerals, etc.
  667. * NOTE: If you use URBNF_NUMBERING_SYSTEM, you must also call setDefaultRuleSet() to
  668. * specify the exact numbering system you want to use. If you want the default numbering system
  669. * for the locale, call NumberFormat::createInstance() instead of creating a RuleBasedNumberFormat directly.
  670. * @param locale The locale for the formatter.
  671. * @param status The status indicating whether the constructor succeeded.
  672. * @stable ICU 2.0
  673. */
  674. RuleBasedNumberFormat(URBNFRuleSetTag tag, const Locale& locale, UErrorCode& status);
  675. //-----------------------------------------------------------------------
  676. // boilerplate
  677. //-----------------------------------------------------------------------
  678. /**
  679. * Copy constructor
  680. * @param rhs the object to be copied from.
  681. * @stable ICU 2.6
  682. */
  683. RuleBasedNumberFormat(const RuleBasedNumberFormat& rhs);
  684. /**
  685. * Assignment operator
  686. * @param rhs the object to be copied from.
  687. * @stable ICU 2.6
  688. */
  689. RuleBasedNumberFormat& operator=(const RuleBasedNumberFormat& rhs);
  690. /**
  691. * Release memory allocated for a RuleBasedNumberFormat when you are finished with it.
  692. * @stable ICU 2.6
  693. */
  694. virtual ~RuleBasedNumberFormat();
  695. /**
  696. * Clone this object polymorphically. The caller is responsible
  697. * for deleting the result when done.
  698. * @return A copy of the object.
  699. * @stable ICU 2.6
  700. */
  701. virtual RuleBasedNumberFormat* clone() const override;
  702. /**
  703. * Return true if the given Format objects are semantically equal.
  704. * Objects of different subclasses are considered unequal.
  705. * @param other the object to be compared with.
  706. * @return true if the given Format objects are semantically equal.
  707. * @stable ICU 2.6
  708. */
  709. virtual bool operator==(const Format& other) const override;
  710. //-----------------------------------------------------------------------
  711. // public API functions
  712. //-----------------------------------------------------------------------
  713. /**
  714. * return the rules that were provided to the RuleBasedNumberFormat.
  715. * @return the result String that was passed in
  716. * @stable ICU 2.0
  717. */
  718. virtual UnicodeString getRules() const;
  719. /**
  720. * Return the number of public rule set names.
  721. * @return the number of public rule set names.
  722. * @stable ICU 2.0
  723. */
  724. virtual int32_t getNumberOfRuleSetNames() const;
  725. /**
  726. * Return the name of the index'th public ruleSet. If index is not valid,
  727. * the function returns null.
  728. * @param index the index of the ruleset
  729. * @return the name of the index'th public ruleSet.
  730. * @stable ICU 2.0
  731. */
  732. virtual UnicodeString getRuleSetName(int32_t index) const;
  733. /**
  734. * Return the number of locales for which we have localized rule set display names.
  735. * @return the number of locales for which we have localized rule set display names.
  736. * @stable ICU 3.2
  737. */
  738. virtual int32_t getNumberOfRuleSetDisplayNameLocales(void) const;
  739. /**
  740. * Return the index'th display name locale.
  741. * @param index the index of the locale
  742. * @param status set to a failure code when this function fails
  743. * @return the locale
  744. * @see #getNumberOfRuleSetDisplayNameLocales
  745. * @stable ICU 3.2
  746. */
  747. virtual Locale getRuleSetDisplayNameLocale(int32_t index, UErrorCode& status) const;
  748. /**
  749. * Return the rule set display names for the provided locale. These are in the same order
  750. * as those returned by getRuleSetName. The locale is matched against the locales for
  751. * which there is display name data, using normal fallback rules. If no locale matches,
  752. * the default display names are returned. (These are the internal rule set names minus
  753. * the leading '%'.)
  754. * @param index the index of the rule set
  755. * @param locale the locale (returned by getRuleSetDisplayNameLocales) for which the localized
  756. * display name is desired
  757. * @return the display name for the given index, which might be bogus if there is an error
  758. * @see #getRuleSetName
  759. * @stable ICU 3.2
  760. */
  761. virtual UnicodeString getRuleSetDisplayName(int32_t index,
  762. const Locale& locale = Locale::getDefault());
  763. /**
  764. * Return the rule set display name for the provided rule set and locale.
  765. * The locale is matched against the locales for which there is display name data, using
  766. * normal fallback rules. If no locale matches, the default display name is returned.
  767. * @return the display name for the rule set
  768. * @stable ICU 3.2
  769. * @see #getRuleSetDisplayName
  770. */
  771. virtual UnicodeString getRuleSetDisplayName(const UnicodeString& ruleSetName,
  772. const Locale& locale = Locale::getDefault());
  773. using NumberFormat::format;
  774. /**
  775. * Formats the specified 32-bit number using the default ruleset.
  776. * @param number The number to format.
  777. * @param toAppendTo the string that will hold the (appended) result
  778. * @param pos the fieldposition
  779. * @return A textual representation of the number.
  780. * @stable ICU 2.0
  781. */
  782. virtual UnicodeString& format(int32_t number,
  783. UnicodeString& toAppendTo,
  784. FieldPosition& pos) const override;
  785. /**
  786. * Formats the specified 64-bit number using the default ruleset.
  787. * @param number The number to format.
  788. * @param toAppendTo the string that will hold the (appended) result
  789. * @param pos the fieldposition
  790. * @return A textual representation of the number.
  791. * @stable ICU 2.1
  792. */
  793. virtual UnicodeString& format(int64_t number,
  794. UnicodeString& toAppendTo,
  795. FieldPosition& pos) const override;
  796. /**
  797. * Formats the specified number using the default ruleset.
  798. * @param number The number to format.
  799. * @param toAppendTo the string that will hold the (appended) result
  800. * @param pos the fieldposition
  801. * @return A textual representation of the number.
  802. * @stable ICU 2.0
  803. */
  804. virtual UnicodeString& format(double number,
  805. UnicodeString& toAppendTo,
  806. FieldPosition& pos) const override;
  807. /**
  808. * Formats the specified number using the named ruleset.
  809. * @param number The number to format.
  810. * @param ruleSetName The name of the rule set to format the number with.
  811. * This must be the name of a valid public rule set for this formatter.
  812. * @param toAppendTo the string that will hold the (appended) result
  813. * @param pos the fieldposition
  814. * @param status the status
  815. * @return A textual representation of the number.
  816. * @stable ICU 2.0
  817. */
  818. virtual UnicodeString& format(int32_t number,
  819. const UnicodeString& ruleSetName,
  820. UnicodeString& toAppendTo,
  821. FieldPosition& pos,
  822. UErrorCode& status) const;
  823. /**
  824. * Formats the specified 64-bit number using the named ruleset.
  825. * @param number The number to format.
  826. * @param ruleSetName The name of the rule set to format the number with.
  827. * This must be the name of a valid public rule set for this formatter.
  828. * @param toAppendTo the string that will hold the (appended) result
  829. * @param pos the fieldposition
  830. * @param status the status
  831. * @return A textual representation of the number.
  832. * @stable ICU 2.1
  833. */
  834. virtual UnicodeString& format(int64_t number,
  835. const UnicodeString& ruleSetName,
  836. UnicodeString& toAppendTo,
  837. FieldPosition& pos,
  838. UErrorCode& status) const;
  839. /**
  840. * Formats the specified number using the named ruleset.
  841. * @param number The number to format.
  842. * @param ruleSetName The name of the rule set to format the number with.
  843. * This must be the name of a valid public rule set for this formatter.
  844. * @param toAppendTo the string that will hold the (appended) result
  845. * @param pos the fieldposition
  846. * @param status the status
  847. * @return A textual representation of the number.
  848. * @stable ICU 2.0
  849. */
  850. virtual UnicodeString& format(double number,
  851. const UnicodeString& ruleSetName,
  852. UnicodeString& toAppendTo,
  853. FieldPosition& pos,
  854. UErrorCode& status) const;
  855. protected:
  856. /**
  857. * Format a decimal number.
  858. * The number is a DigitList wrapper onto a floating point decimal number.
  859. * The default implementation in NumberFormat converts the decimal number
  860. * to a double and formats that. Subclasses of NumberFormat that want
  861. * to specifically handle big decimal numbers must override this method.
  862. * class DecimalFormat does so.
  863. *
  864. * @param number The number, a DigitList format Decimal Floating Point.
  865. * @param appendTo Output parameter to receive result.
  866. * Result is appended to existing contents.
  867. * @param pos On input: an alignment field, if desired.
  868. * On output: the offsets of the alignment field.
  869. * @param status Output param filled with success/failure status.
  870. * @return Reference to 'appendTo' parameter.
  871. * @internal
  872. */
  873. virtual UnicodeString& format(const number::impl::DecimalQuantity &number,
  874. UnicodeString& appendTo,
  875. FieldPosition& pos,
  876. UErrorCode& status) const override;
  877. public:
  878. using NumberFormat::parse;
  879. /**
  880. * Parses the specified string, beginning at the specified position, according
  881. * to this formatter's rules. This will match the string against all of the
  882. * formatter's public rule sets and return the value corresponding to the longest
  883. * parseable substring. This function's behavior is affected by the lenient
  884. * parse mode.
  885. * @param text The string to parse
  886. * @param result the result of the parse, either a double or a long.
  887. * @param parsePosition On entry, contains the position of the first character
  888. * in "text" to examine. On exit, has been updated to contain the position
  889. * of the first character in "text" that wasn't consumed by the parse.
  890. * @see #setLenient
  891. * @stable ICU 2.0
  892. */
  893. virtual void parse(const UnicodeString& text,
  894. Formattable& result,
  895. ParsePosition& parsePosition) const override;
  896. #if !UCONFIG_NO_COLLATION
  897. /**
  898. * Turns lenient parse mode on and off.
  899. *
  900. * When in lenient parse mode, the formatter uses a Collator for parsing the text.
  901. * Only primary differences are treated as significant. This means that case
  902. * differences, accent differences, alternate spellings of the same letter
  903. * (e.g., ae and a-umlaut in German), ignorable characters, etc. are ignored in
  904. * matching the text. In many cases, numerals will be accepted in place of words
  905. * or phrases as well.
  906. *
  907. * For example, all of the following will correctly parse as 255 in English in
  908. * lenient-parse mode:
  909. * <br>"two hundred fifty-five"
  910. * <br>"two hundred fifty five"
  911. * <br>"TWO HUNDRED FIFTY-FIVE"
  912. * <br>"twohundredfiftyfive"
  913. * <br>"2 hundred fifty-5"
  914. *
  915. * The Collator used is determined by the locale that was
  916. * passed to this object on construction. The description passed to this object
  917. * on construction may supply additional collation rules that are appended to the
  918. * end of the default collator for the locale, enabling additional equivalences
  919. * (such as adding more ignorable characters or permitting spelled-out version of
  920. * symbols; see the demo program for examples).
  921. *
  922. * It's important to emphasize that even strict parsing is relatively lenient: it
  923. * will accept some text that it won't produce as output. In English, for example,
  924. * it will correctly parse "two hundred zero" and "fifteen hundred".
  925. *
  926. * @param enabled If true, turns lenient-parse mode on; if false, turns it off.
  927. * @see RuleBasedCollator
  928. * @stable ICU 2.0
  929. */
  930. virtual void setLenient(UBool enabled) override;
  931. /**
  932. * Returns true if lenient-parse mode is turned on. Lenient parsing is off
  933. * by default.
  934. * @return true if lenient-parse mode is turned on.
  935. * @see #setLenient
  936. * @stable ICU 2.0
  937. */
  938. virtual inline UBool isLenient(void) const override;
  939. #endif
  940. /**
  941. * Override the default rule set to use. If ruleSetName is null, reset
  942. * to the initial default rule set. If the rule set is not a public rule set name,
  943. * U_ILLEGAL_ARGUMENT_ERROR is returned in status.
  944. * @param ruleSetName the name of the rule set, or null to reset the initial default.
  945. * @param status set to failure code when a problem occurs.
  946. * @stable ICU 2.6
  947. */
  948. virtual void setDefaultRuleSet(const UnicodeString& ruleSetName, UErrorCode& status);
  949. /**
  950. * Return the name of the current default rule set. If the current rule set is
  951. * not public, returns a bogus (and empty) UnicodeString.
  952. * @return the name of the current default rule set
  953. * @stable ICU 3.0
  954. */
  955. virtual UnicodeString getDefaultRuleSetName() const;
  956. /**
  957. * Set a particular UDisplayContext value in the formatter, such as
  958. * UDISPCTX_CAPITALIZATION_FOR_STANDALONE. Note: For getContext, see
  959. * NumberFormat.
  960. * @param value The UDisplayContext value to set.
  961. * @param status Input/output status. If at entry this indicates a failure
  962. * status, the function will do nothing; otherwise this will be
  963. * updated with any new status from the function.
  964. * @stable ICU 53
  965. */
  966. virtual void setContext(UDisplayContext value, UErrorCode& status) override;
  967. /**
  968. * Get the rounding mode.
  969. * @return A rounding mode
  970. * @stable ICU 60
  971. */
  972. virtual ERoundingMode getRoundingMode(void) const override;
  973. /**
  974. * Set the rounding mode.
  975. * @param roundingMode A rounding mode
  976. * @stable ICU 60
  977. */
  978. virtual void setRoundingMode(ERoundingMode roundingMode) override;
  979. public:
  980. /**
  981. * ICU "poor man's RTTI", returns a UClassID for this class.
  982. *
  983. * @stable ICU 2.8
  984. */
  985. static UClassID U_EXPORT2 getStaticClassID(void);
  986. /**
  987. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  988. *
  989. * @stable ICU 2.8
  990. */
  991. virtual UClassID getDynamicClassID(void) const override;
  992. /**
  993. * Sets the decimal format symbols, which is generally not changed
  994. * by the programmer or user. The formatter takes ownership of
  995. * symbolsToAdopt; the client must not delete it.
  996. *
  997. * @param symbolsToAdopt DecimalFormatSymbols to be adopted.
  998. * @stable ICU 49
  999. */
  1000. virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt);
  1001. /**
  1002. * Sets the decimal format symbols, which is generally not changed
  1003. * by the programmer or user. A clone of the symbols is created and
  1004. * the symbols is _not_ adopted; the client is still responsible for
  1005. * deleting it.
  1006. *
  1007. * @param symbols DecimalFormatSymbols.
  1008. * @stable ICU 49
  1009. */
  1010. virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols);
  1011. private:
  1012. RuleBasedNumberFormat() = delete; // default constructor not implemented
  1013. // this will ref the localizations if they are not nullptr
  1014. // caller must deref to get adoption
  1015. RuleBasedNumberFormat(const UnicodeString& description, LocalizationInfo* localizations,
  1016. const Locale& locale, UParseError& perror, UErrorCode& status);
  1017. void init(const UnicodeString& rules, LocalizationInfo* localizations, UParseError& perror, UErrorCode& status);
  1018. void initCapitalizationContextInfo(const Locale& thelocale);
  1019. void dispose();
  1020. void stripWhitespace(UnicodeString& src);
  1021. void initDefaultRuleSet();
  1022. NFRuleSet* findRuleSet(const UnicodeString& name, UErrorCode& status) const;
  1023. /* friend access */
  1024. friend class NFSubstitution;
  1025. friend class NFRule;
  1026. friend class NFRuleSet;
  1027. friend class FractionalPartSubstitution;
  1028. inline NFRuleSet * getDefaultRuleSet() const;
  1029. const RuleBasedCollator * getCollator() const;
  1030. DecimalFormatSymbols * initializeDecimalFormatSymbols(UErrorCode &status);
  1031. const DecimalFormatSymbols * getDecimalFormatSymbols() const;
  1032. NFRule * initializeDefaultInfinityRule(UErrorCode &status);
  1033. const NFRule * getDefaultInfinityRule() const;
  1034. NFRule * initializeDefaultNaNRule(UErrorCode &status);
  1035. const NFRule * getDefaultNaNRule() const;
  1036. PluralFormat *createPluralFormat(UPluralType pluralType, const UnicodeString &pattern, UErrorCode& status) const;
  1037. UnicodeString& adjustForCapitalizationContext(int32_t startPos, UnicodeString& currentResult, UErrorCode& status) const;
  1038. UnicodeString& format(int64_t number, NFRuleSet *ruleSet, UnicodeString& toAppendTo, UErrorCode& status) const;
  1039. void format(double number, NFRuleSet& rs, UnicodeString& toAppendTo, UErrorCode& status) const;
  1040. private:
  1041. NFRuleSet **fRuleSets;
  1042. UnicodeString* ruleSetDescriptions;
  1043. int32_t numRuleSets;
  1044. NFRuleSet *defaultRuleSet;
  1045. Locale locale;
  1046. RuleBasedCollator* collator;
  1047. DecimalFormatSymbols* decimalFormatSymbols;
  1048. NFRule *defaultInfinityRule;
  1049. NFRule *defaultNaNRule;
  1050. ERoundingMode fRoundingMode;
  1051. UBool lenient;
  1052. UnicodeString* lenientParseRules;
  1053. LocalizationInfo* localizations;
  1054. UnicodeString originalDescription;
  1055. UBool capitalizationInfoSet;
  1056. UBool capitalizationForUIListMenu;
  1057. UBool capitalizationForStandAlone;
  1058. BreakIterator* capitalizationBrkIter;
  1059. };
  1060. // ---------------
  1061. #if !UCONFIG_NO_COLLATION
  1062. inline UBool
  1063. RuleBasedNumberFormat::isLenient(void) const {
  1064. return lenient;
  1065. }
  1066. #endif
  1067. inline NFRuleSet*
  1068. RuleBasedNumberFormat::getDefaultRuleSet() const {
  1069. return defaultRuleSet;
  1070. }
  1071. U_NAMESPACE_END
  1072. /* U_HAVE_RBNF */
  1073. #endif
  1074. #endif /* U_SHOW_CPLUSPLUS_API */
  1075. /* RBNF_H */
  1076. #endif