~ubuntu-branches/ubuntu/quantal/icu/quantal

« back to all changes in this revision

Viewing changes to source/i18n/unicode/rbnf.h

  • Committer: Package Import Robot
  • Author(s): Yves Arrouye
  • Date: 2002-03-03 15:31:13 UTC
  • Revision ID: package-import@ubuntu.com-20020303153113-3ssceqlq45xbmbnc
Tags: upstream-2.0-2.1pre20020303
ImportĀ upstreamĀ versionĀ 2.0-2.1pre20020303

Show diffs side-by-side

added added

removed removed

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