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