rbnf.h 51 KB

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