rbnf.h 44 KB

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