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