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