2 *******************************************************************************
3 * Copyright (C) 1997-2003, International Business Machines Corporation and others.
5 *******************************************************************************
11 #include "unicode/utypes.h"
13 #if defined(U_INT64_T_UNAVAILABLE) || UCONFIG_NO_FORMATTING
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"
29 /** Tags for the predefined rulesets. */
30 enum URBNFRuleSetTag
{
37 #if UCONFIG_NO_COLLATION
42 * \brief C++ API: RuleBasedNumberFormat
44 * <h2> Rule Based Number Format C++ API </h2>
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 * "twenty-five thousand three hundred seventy-six" or "vingt-cinq mille trois
49 * cents soixante-seize" or
50 * "fünfundzwanzigtausenddreihundertsechsundsiebzig"), 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 "1:02:10").</p>
54 * <p>The resources contain three predefined formatters for each locale: spellout, which
55 * spells out a value in words (123 is "one hundred twenty-three"); ordinal, which
56 * appends an ordinal suffix to the end of a numeral (123 is "123rd"); and
57 * duration, which shows a duration in seconds as hours, minutes, and seconds (123 is
58 * "2:03"). The client can also define more specialized <tt>RuleBasedNumberFormat</tt>s
59 * by supplying programmer-defined rule sets.</p>
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
68 * <pre>zero; one; two; three; four; five; six; seven; eight; nine;
69 * ten; eleven; twelve; thirteen; fourteen; fifteen; sixteen; seventeen; eighteen; nineteen;</pre>
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>
74 * <pre> 20: twenty[->>];
75 * 30: thirty[->>];
76 * 40: forty[->>];
77 * 50: fifty[->>];
78 * 60: sixty[->>];
79 * 70: seventy[->>];
80 * 80: eighty[->>];
81 * 90: ninety[->>];</pre>
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 * ">>" 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 ">>" 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 "twenty-four," not "twenty four").</p>
92 * <p>For even larger numbers, we can actually look up several parts of the number in the
95 * <pre>100: << hundred[ >>];</pre>
97 * <p>The "<<" represents a new kind of substitution. The << 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 "<<" was. Notice also that the meaning of
100 * >> 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 <<
105 * substitution, and the remainder is used to fill in the >> 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>
111 * <p>This rule covers values up to 999, at which point we add another rule:</p>
113 * <pre>1000: << thousand[ >>];</pre>
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>
119 * <pre> 1,000,000: << million[ >>];
120 * 1,000,000,000: << billion[ >>];
121 * 1,000,000,000,000: << trillion[ >>];
122 * 1,000,000,000,000,000: OUT OF RANGE!;</pre>
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 * "overflow rule," 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>
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>
135 * <table border="0" width="100%">
137 * <td><< thousand >></strong></td>
138 * <td>[the rule whose base value is 1,000 is applicable to 25,340]</td>
141 * <td><strong>twenty->></strong> thousand >></td>
142 * <td>[25,340 over 1,000 is 25. The rule for 20 applies.]</td>
145 * <td>twenty-<strong>five</strong> thousand >></td>
146 * <td>[25 mod 10 is 5. The rule for 5 is "five."</td>
149 * <td>twenty-five thousand <strong><< hundred >></strong></td>
150 * <td>[25,340 mod 1,000 is 340. The rule for 100 applies.]</td>
153 * <td>twenty-five thousand <strong>three</strong> hundred >></td>
154 * <td>[340 over 100 is 3. The rule for 3 is "three."]</td>
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>
163 * <p>The above syntax suffices only to format positive integers. To format negative numbers,
164 * we add a special rule:</p>
166 * <pre>-x: minus >>;</pre>
168 * <p>This is called a <em>negative-number rule,</em> and is identified by "-x"
169 * where the base value would be. This rule is used to format all negative numbers. the
170 * >> token here means "find the number's absolute value, format it with these
171 * rules, and put the result here."</p>
173 * <p>We also add a special rule called a <em>fraction rule </em>for numbers with fractional
176 * <pre>x.x: << point >>;</pre>
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 << token refers to
180 * the number's integral part, and the >> 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 "one hundred twenty-three point four five six").</p>
184 * <p>To see how this rule syntax is applied to various languages, examine the resource data.</p>
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>
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>
202 * <p>The user can also specify a special "rule set" 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 '&', have no particular meaning when appearing outside
208 * of the <tt>lenient-parse</tt> rule set.</p>
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>
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>
218 * <table border="0" width="100%">
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>
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>
232 * <td><em>bv</em>>:</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 > 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>
240 * <td><em>bv</em>/<em>rad</em>>:</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 > 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>
249 * <td>The rule is a negative-number rule.</td>
253 * <td>The rule is an <em>improper fraction rule.</em></td>
257 * <td>The rule is a <em>proper fraction rule.</em></td>
261 * <td>The rule is a <em>master rule.</em></td>
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. In a fraction rule set, the base value is the same as the preceding rule's
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>
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:
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. (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
287 * <li>If the number has a fractional part and is between 0 and 1, use the proper fraction
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>
295 * <p>If the rule set is a fraction rule set, do the following:
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
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>
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>
327 * <p>The meanings of the substitution token characters are as follows:</p>
329 * <table border="0" width="100%">
332 * <td>in normal rule</td>
333 * <td>Divide the number by the rule's divisor and format the remainder</td>
337 * <td>in negative-number rule</td>
338 * <td>Find the absolute value of the number and format the result</td>
342 * <td>in fraction or master rule</td>
343 * <td>Isolate the number's fractional part and format it.</td>
347 * <td>in rule in fraction rule set</td>
348 * <td>Not allowed.</td>
351 * <td>>>></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>
359 * <td>in all other rules</td>
360 * <td>Not allowed.</td>
364 * <td>in normal rule</td>
365 * <td>Divide the number by the rule's divisor and format the quotient</td>
369 * <td>in negative-number rule</td>
370 * <td>Not allowed.</td>
374 * <td>in fraction or master rule</td>
375 * <td>Isolate the number's integral part and format it.</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>
384 * <td>in all rule sets</td>
385 * <td>Format the number unchanged</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>
394 * <td>in negative-number rule</td>
395 * <td>Not allowed.</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>
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>
411 * <td>in proper-fraction rule</td>
412 * <td>Not allowed.</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>
421 * <p>The substitution descriptor (i.e., the text between the token characters) may take one
422 * of three forms:</p>
424 * <table border="0" width="100%">
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>
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. The pattern must begin with 0 or #.</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 >> 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 << substitution in a rule in a
443 * fraction rule set, format the result using the default rule set for this formatter.</li>
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>
457 * <p>See the resource data and the demo program for annotated examples of real rule sets
458 * using these features.</p>
460 * @author Richard Gillam
465 class U_I18N_API RuleBasedNumberFormat
: public NumberFormat
{
468 //-----------------------------------------------------------------------
470 //-----------------------------------------------------------------------
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
480 * @param locale A locale, that governs which characters are used for
481 * formatting values in numerals, and which characters are equivalent in
483 * @param perror The parse error if an error was encountered.
484 * @param status The status indicating whether the constructor succeeded.
487 RuleBasedNumberFormat(const UnicodeString
& rules
, const Locale
& locale
,
488 UParseError
& perror
, UErrorCode
& status
);
491 * Creates a RuleBasedNumberFormat from a predefined ruleset. The selector
492 * code choosed among three possible predefined formats: spellout, ordinal,
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.
503 RuleBasedNumberFormat(URBNFRuleSetTag tag
, const Locale
& locale
, UErrorCode
& status
);
505 //-----------------------------------------------------------------------
507 //-----------------------------------------------------------------------
511 * @param rhs the object to be copied from.
514 RuleBasedNumberFormat(const RuleBasedNumberFormat
& rhs
);
517 * Assignment operator
518 * @param rhs the object to be copied from.
521 RuleBasedNumberFormat
& operator=(const RuleBasedNumberFormat
& rhs
);
524 * Release memory allocated for a RuleBasedNumberFormat when you are finished with it.
527 virtual ~RuleBasedNumberFormat();
530 * Clone this object polymorphically. The caller is responsible
531 * for deleting the result when done.
532 * @return A copy of the object.
535 virtual Format
* clone(void) const;
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.
544 virtual UBool
operator==(const Format
& other
) const;
546 //-----------------------------------------------------------------------
547 // public API functions
548 //-----------------------------------------------------------------------
551 * return the rules that were provided to the RuleBasedNumberFormat.
552 * @return the result String that was passed in
555 virtual UnicodeString
getRules() const;
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.
564 virtual UnicodeString
getRuleSetName(int32_t index
) const;
567 * Return the number of public rule set names.
568 * @return the number of public rule set names.
571 virtual int32_t getNumberOfRuleSetNames() const;
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.
581 virtual UnicodeString
& format(int32_t number
,
582 UnicodeString
& toAppendTo
,
583 FieldPosition
& pos
) const;
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.
593 virtual UnicodeString
& format(int64_t number
,
594 UnicodeString
& toAppendTo
,
595 FieldPosition
& pos
) const;
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.
604 virtual UnicodeString
& format(double number
,
605 UnicodeString
& toAppendTo
,
606 FieldPosition
& pos
) const;
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.
619 virtual UnicodeString
& format(int32_t number
,
620 const UnicodeString
& ruleSetName
,
621 UnicodeString
& toAppendTo
,
623 UErrorCode
& status
) const;
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.
635 virtual UnicodeString
& format(int64_t number
,
636 const UnicodeString
& ruleSetName
,
637 UnicodeString
& toAppendTo
,
639 UErrorCode
& status
) const;
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.
651 virtual UnicodeString
& format(double number
,
652 const UnicodeString
& ruleSetName
,
653 UnicodeString
& toAppendTo
,
655 UErrorCode
& status
) const;
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.
666 virtual UnicodeString
& format(const Formattable
& obj
,
667 UnicodeString
& toAppendTo
,
669 UErrorCode
& status
) const;
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'.
678 UnicodeString
& format(const Formattable
& obj
,
679 UnicodeString
& result
,
680 UErrorCode
& status
) const;
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'.
689 UnicodeString
& format(double number
,
690 UnicodeString
& output
) const;
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'.
699 UnicodeString
& format(int32_t number
,
700 UnicodeString
& output
) const;
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
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
716 virtual void parse(const UnicodeString
& text
,
718 ParsePosition
& parsePosition
) const;
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.
728 virtual inline void parse(const UnicodeString
& text
,
730 UErrorCode
& status
) const;
732 #if !UCONFIG_NO_COLLATION
735 * Turns lenient parse mode on and off.
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.
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"
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).
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".
763 * @param enabled If true, turns lenient-parse mode on; if false, turns it off.
764 * @see RuleBasedCollator
767 virtual void setLenient(UBool enabled
);
770 * Returns true if lenient-parse mode is turned on. Lenient parsing is off
772 * @return true if lenient-parse mode is turned on.
773 * @see #setLenientParseMode
776 virtual inline UBool
isLenient(void) const;
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.
788 virtual void setDefaultRuleSet(const UnicodeString
& ruleSetName
, UErrorCode
& status
);
791 RuleBasedNumberFormat(); // default constructor not implemented
793 void init(const UnicodeString
& rules
, UParseError
& perror
, UErrorCode
& status
);
795 void stripWhitespace(UnicodeString
& src
);
796 void initDefaultRuleSet();
797 void format(double number
, NFRuleSet
& ruleSet
);
798 NFRuleSet
* findRuleSet(const UnicodeString
& name
, UErrorCode
& status
) const;
801 friend class NFSubstitution
;
803 friend class FractionalPartSubstitution
;
805 inline NFRuleSet
* getDefaultRuleSet() const;
806 Collator
* getCollator() const;
807 DecimalFormatSymbols
* getDecimalFormatSymbols() const;
810 static const char fgClassID
;
813 static UClassID
getStaticClassID(void) { return (UClassID
)&fgClassID
; }
814 virtual UClassID
getDynamicClassID(void) const { return getStaticClassID(); }
817 NFRuleSet
**ruleSets
;
818 NFRuleSet
*defaultRuleSet
;
821 DecimalFormatSymbols
* decimalFormatSymbols
;
823 UnicodeString
* lenientParseRules
;
828 inline UnicodeString
&
829 RuleBasedNumberFormat::format(const Formattable
& obj
,
830 UnicodeString
& result
,
831 UErrorCode
& status
) const
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
);
841 inline UnicodeString
&
842 RuleBasedNumberFormat::format(double number
, UnicodeString
& output
) const {
843 FieldPosition
pos(0);
844 return format(number
, output
, pos
);
847 inline UnicodeString
&
848 RuleBasedNumberFormat::format(int32_t number
, UnicodeString
& output
) const {
849 FieldPosition
pos(0);
850 return format(number
, output
, pos
);
854 RuleBasedNumberFormat::parse(const UnicodeString
& text
, Formattable
& result
, UErrorCode
& status
) const
856 NumberFormat::parse(text
, result
, status
);
859 #if !UCONFIG_NO_COLLATION
862 RuleBasedNumberFormat::isLenient(void) const {
869 RuleBasedNumberFormat::getDefaultRuleSet() const {
870 return defaultRuleSet
;