intl/icu/source/i18n/unicode/rbnf.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial