• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ********************************************************************************
5 *   Copyright (C) 1997-2013, International Business Machines
6 *   Corporation and others.  All Rights Reserved.
7 ********************************************************************************
8 *
9 * File CHOICFMT.H
10 *
11 * Modification History:
12 *
13 *   Date        Name        Description
14 *   02/19/97    aliu        Converted from java.
15 *   03/20/97    helena      Finished first cut of implementation and got rid
16 *                           of nextDouble/previousDouble and replaced with
17 *                           boolean array.
18 *   4/10/97     aliu        Clean up.  Modified to work on AIX.
19 *   8/6/97      nos         Removed overloaded constructor, member var 'buffer'.
20 *   07/22/98    stephen     Removed operator!= (implemented in Format)
21 ********************************************************************************
22 */
23 
24 #ifndef CHOICFMT_H
25 #define CHOICFMT_H
26 
27 #include "unicode/utypes.h"
28 
29 /**
30  * \file
31  * \brief C++ API: Choice Format.
32  */
33 
34 #if !UCONFIG_NO_FORMATTING
35 #ifndef U_HIDE_DEPRECATED_API
36 
37 #include "unicode/fieldpos.h"
38 #include "unicode/format.h"
39 #include "unicode/messagepattern.h"
40 #include "unicode/numfmt.h"
41 #include "unicode/unistr.h"
42 
43 U_NAMESPACE_BEGIN
44 
45 class MessageFormat;
46 
47 /**
48  * ChoiceFormat converts between ranges of numeric values and strings for those ranges.
49  * The strings must conform to the MessageFormat pattern syntax.
50  *
51  * <p><em><code>ChoiceFormat</code> is probably not what you need.
52  * Please use <code>MessageFormat</code>
53  * with <code>plural</code> arguments for proper plural selection,
54  * and <code>select</code> arguments for simple selection among a fixed set of choices!</em></p>
55  *
56  * <p>A <code>ChoiceFormat</code> splits
57  * the real number line \htmlonly<code>-&#x221E;</code> to
58  * <code>+&#x221E;</code>\endhtmlonly into two
59  * or more contiguous ranges. Each range is mapped to a
60  * string.</p>
61  *
62  * <p><code>ChoiceFormat</code> was originally intended
63  * for displaying grammatically correct
64  * plurals such as &quot;There is one file.&quot; vs. &quot;There are 2 files.&quot;
65  * <em>However,</em> plural rules for many languages
66  * are too complex for the capabilities of ChoiceFormat,
67  * and its requirement of specifying the precise rules for each message
68  * is unmanageable for translators.</p>
69  *
70  * <p>There are two methods of defining a <code>ChoiceFormat</code>; both
71  * are equivalent.  The first is by using a string pattern. This is the
72  * preferred method in most cases.  The second method is through direct
73  * specification of the arrays that logically make up the
74  * <code>ChoiceFormat</code>.</p>
75  *
76  * <p>Note: Typically, choice formatting is done (if done at all) via <code>MessageFormat</code>
77  * with a <code>choice</code> argument type,
78  * rather than using a stand-alone <code>ChoiceFormat</code>.</p>
79  *
80  * <h5>Patterns and Their Interpretation</h5>
81  *
82  * <p>The pattern string defines the range boundaries and the strings for each number range.
83  * Syntax:
84  * <pre>
85  * choiceStyle = number separator message ('|' number separator message)*
86  * number = normal_number | ['-'] \htmlonly&#x221E;\endhtmlonly (U+221E, infinity)
87  * normal_number = double value (unlocalized ASCII string)
88  * separator = less_than | less_than_or_equal
89  * less_than = '<'
90  * less_than_or_equal = '#' | \htmlonly&#x2264;\endhtmlonly (U+2264)
91  * message: see {@link MessageFormat}
92  * </pre>
93  * Pattern_White_Space between syntax elements is ignored, except
94  * around each range's sub-message.</p>
95  *
96  * <p>Each numeric sub-range extends from the current range's number
97  * to the next range's number.
98  * The number itself is included in its range if a <code>less_than_or_equal</code> sign is used,
99  * and excluded from its range (and instead included in the previous range)
100  * if a <code>less_than</code> sign is used.</p>
101  *
102  * <p>When a <code>ChoiceFormat</code> is constructed from
103  * arrays of numbers, closure flags and strings,
104  * they are interpreted just like
105  * the sequence of <code>(number separator string)</code> in an equivalent pattern string.
106  * <code>closure[i]==TRUE</code> corresponds to a <code>less_than</code> separator sign.
107  * The equivalent pattern string will be constructed automatically.</p>
108  *
109  * <p>During formatting, a number is mapped to the first range
110  * where the number is not greater than the range's upper limit.
111  * That range's message string is returned. A NaN maps to the very first range.</p>
112  *
113  * <p>During parsing, a range is selected for the longest match of
114  * any range's message. That range's number is returned, ignoring the separator/closure.
115  * Only a simple string match is performed, without parsing of arguments that
116  * might be specified in the message strings.</p>
117  *
118  * <p>Note that the first range's number is ignored in formatting
119  * but may be returned from parsing.</p>
120  *
121  * <h5>Examples</h5>
122  *
123  * <p>Here is an example of two arrays that map the number
124  * <code>1..7</code> to the English day of the week abbreviations
125  * <code>Sun..Sat</code>. No closures array is given; this is the same as
126  * specifying all closures to be <code>FALSE</code>.</p>
127  *
128  * <pre>    {1,2,3,4,5,6,7},
129  *     {&quot;Sun&quot;,&quot;Mon&quot;,&quot;Tue&quot;,&quot;Wed&quot;,&quot;Thur&quot;,&quot;Fri&quot;,&quot;Sat&quot;}</pre>
130  *
131  * <p>Here is an example that maps the ranges [-Inf, 1), [1, 1], and (1,
132  * +Inf] to three strings. That is, the number line is split into three
133  * ranges: x &lt; 1.0, x = 1.0, and x &gt; 1.0.
134  * (The round parentheses in the notation above indicate an exclusive boundary,
135  * like the turned bracket in European notation: [-Inf, 1) == [-Inf, 1[  )</p>
136  *
137  * <pre>    {0, 1, 1},
138  *     {FALSE, FALSE, TRUE},
139  *     {&quot;no files&quot;, &quot;one file&quot;, &quot;many files&quot;}</pre>
140  *
141  * <p>Here is an example that shows formatting and parsing: </p>
142  *
143  * \code
144  *   #include <unicode/choicfmt.h>
145  *   #include <unicode/unistr.h>
146  *   #include <iostream.h>
147  *
148  *   int main(int argc, char *argv[]) {
149  *       double limits[] = {1,2,3,4,5,6,7};
150  *       UnicodeString monthNames[] = {
151  *           "Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
152  *       ChoiceFormat fmt(limits, monthNames, 7);
153  *       UnicodeString str;
154  *       char buf[256];
155  *       for (double x = 1.0; x <= 8.0; x += 1.0) {
156  *           fmt.format(x, str);
157  *           str.extract(0, str.length(), buf, 256, "");
158  *           str.truncate(0);
159  *           cout << x << " -> "
160  *                << buf << endl;
161  *       }
162  *       cout << endl;
163  *       return 0;
164  *   }
165  * \endcode
166  *
167  * <p><em>User subclasses are not supported.</em> While clients may write
168  * subclasses, such code will not necessarily work and will not be
169  * guaranteed to work stably from release to release.
170  *
171  * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
172  */
173 class U_I18N_API ChoiceFormat: public NumberFormat {
174 public:
175     /**
176      * Constructs a new ChoiceFormat from the pattern string.
177      *
178      * @param pattern   Pattern used to construct object.
179      * @param status    Output param to receive success code.  If the
180      *                  pattern cannot be parsed, set to failure code.
181      * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
182      */
183     ChoiceFormat(const UnicodeString& pattern,
184                  UErrorCode& status);
185 
186 
187     /**
188      * Constructs a new ChoiceFormat with the given limits and message strings.
189      * All closure flags default to <code>FALSE</code>,
190      * equivalent to <code>less_than_or_equal</code> separators.
191      *
192      * Copies the limits and formats instead of adopting them.
193      *
194      * @param limits    Array of limit values.
195      * @param formats   Array of formats.
196      * @param count     Size of 'limits' and 'formats' arrays.
197      * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
198      */
199     ChoiceFormat(const double* limits,
200                  const UnicodeString* formats,
201                  int32_t count );
202 
203     /**
204      * Constructs a new ChoiceFormat with the given limits, closure flags and message strings.
205      *
206      * Copies the limits and formats instead of adopting them.
207      *
208      * @param limits Array of limit values
209      * @param closures Array of booleans specifying whether each
210      * element of 'limits' is open or closed.  If FALSE, then the
211      * corresponding limit number is a member of its range.
212      * If TRUE, then the limit number belongs to the previous range it.
213      * @param formats Array of formats
214      * @param count Size of 'limits', 'closures', and 'formats' arrays
215      * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
216      */
217     ChoiceFormat(const double* limits,
218                  const UBool* closures,
219                  const UnicodeString* formats,
220                  int32_t count);
221 
222     /**
223      * Copy constructor.
224      *
225      * @param that   ChoiceFormat object to be copied from
226      * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
227      */
228     ChoiceFormat(const ChoiceFormat& that);
229 
230     /**
231      * Assignment operator.
232      *
233      * @param that   ChoiceFormat object to be copied
234      * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
235      */
236     const ChoiceFormat& operator=(const ChoiceFormat& that);
237 
238     /**
239      * Destructor.
240      * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
241      */
242     virtual ~ChoiceFormat();
243 
244     /**
245      * Clones this Format object. The caller owns the
246      * result and must delete it when done.
247      *
248      * @return a copy of this object
249      * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
250      */
251     virtual Format* clone(void) const;
252 
253     /**
254      * Returns true if the given Format objects are semantically equal.
255      * Objects of different subclasses are considered unequal.
256      *
257      * @param other    ChoiceFormat object to be compared
258      * @return         true if other is the same as this.
259      * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
260      */
261     virtual UBool operator==(const Format& other) const;
262 
263     /**
264      * Sets the pattern.
265      * @param pattern   The pattern to be applied.
266      * @param status    Output param set to success/failure code on
267      *                  exit. If the pattern is invalid, this will be
268      *                  set to a failure result.
269      * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
270      */
271     virtual void applyPattern(const UnicodeString& pattern,
272                               UErrorCode& status);
273 
274     /**
275      * Sets the pattern.
276      * @param pattern    The pattern to be applied.
277      * @param parseError Struct to receive information on position
278      *                   of error if an error is encountered
279      * @param status     Output param set to success/failure code on
280      *                   exit. If the pattern is invalid, this will be
281      *                   set to a failure result.
282      * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
283      */
284     virtual void applyPattern(const UnicodeString& pattern,
285                              UParseError& parseError,
286                              UErrorCode& status);
287     /**
288      * Gets the pattern.
289      *
290      * @param pattern    Output param which will receive the pattern
291      *                   Previous contents are deleted.
292      * @return    A reference to 'pattern'
293      * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
294      */
295     virtual UnicodeString& toPattern(UnicodeString &pattern) const;
296 
297     /**
298      * Sets the choices to be used in formatting.
299      * For details see the constructor with the same parameter list.
300      *
301      * @param limitsToCopy      Contains the top value that you want
302      *                          parsed with that format,and should be in
303      *                          ascending sorted order. When formatting X,
304      *                          the choice will be the i, where limit[i]
305      *                          &lt;= X &lt; limit[i+1].
306      * @param formatsToCopy     The format strings you want to use for each limit.
307      * @param count             The size of the above arrays.
308      * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
309      */
310     virtual void setChoices(const double* limitsToCopy,
311                             const UnicodeString* formatsToCopy,
312                             int32_t count );
313 
314     /**
315      * Sets the choices to be used in formatting.
316      * For details see the constructor with the same parameter list.
317      *
318      * @param limits Array of limits
319      * @param closures Array of limit booleans
320      * @param formats Array of format string
321      * @param count The size of the above arrays
322      * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
323      */
324     virtual void setChoices(const double* limits,
325                             const UBool* closures,
326                             const UnicodeString* formats,
327                             int32_t count);
328 
329     /**
330      * Returns NULL and 0.
331      * Before ICU 4.8, this used to return the choice limits array.
332      *
333      * @param count Will be set to 0.
334      * @return NULL
335      * @deprecated ICU 4.8 Use the MessagePattern class to analyze a ChoiceFormat pattern.
336      */
337     virtual const double* getLimits(int32_t& count) const;
338 
339     /**
340      * Returns NULL and 0.
341      * Before ICU 4.8, this used to return the limit booleans array.
342      *
343      * @param count Will be set to 0.
344      * @return NULL
345      * @deprecated ICU 4.8 Use the MessagePattern class to analyze a ChoiceFormat pattern.
346      */
347     virtual const UBool* getClosures(int32_t& count) const;
348 
349     /**
350      * Returns NULL and 0.
351      * Before ICU 4.8, this used to return the array of choice strings.
352      *
353      * @param count Will be set to 0.
354      * @return NULL
355      * @deprecated ICU 4.8 Use the MessagePattern class to analyze a ChoiceFormat pattern.
356      */
357     virtual const UnicodeString* getFormats(int32_t& count) const;
358 
359 
360     using NumberFormat::format;
361 
362     /**
363      * Formats a double number using this object's choices.
364      *
365      * @param number    The value to be formatted.
366      * @param appendTo  Output parameter to receive result.
367      *                  Result is appended to existing contents.
368      * @param pos       On input: an alignment field, if desired.
369      *                  On output: the offsets of the alignment field.
370      * @return          Reference to 'appendTo' parameter.
371      * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
372      */
373     virtual UnicodeString& format(double number,
374                                   UnicodeString& appendTo,
375                                   FieldPosition& pos) const;
376     /**
377      * Formats an int32_t number using this object's choices.
378      *
379      * @param number    The value to be formatted.
380      * @param appendTo  Output parameter to receive result.
381      *                  Result is appended to existing contents.
382      * @param pos       On input: an alignment field, if desired.
383      *                  On output: the offsets of the alignment field.
384      * @return          Reference to 'appendTo' parameter.
385      * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
386      */
387     virtual UnicodeString& format(int32_t number,
388                                   UnicodeString& appendTo,
389                                   FieldPosition& pos) const;
390 
391     /**
392      * Formats an int64_t number using this object's choices.
393      *
394      * @param number    The value to be formatted.
395      * @param appendTo  Output parameter to receive result.
396      *                  Result is appended to existing contents.
397      * @param pos       On input: an alignment field, if desired.
398      *                  On output: the offsets of the alignment field.
399      * @return          Reference to 'appendTo' parameter.
400      * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
401      */
402     virtual UnicodeString& format(int64_t number,
403                                   UnicodeString& appendTo,
404                                   FieldPosition& pos) const;
405 
406     /**
407      * Formats an array of objects using this object's choices.
408      *
409      * @param objs      The array of objects to be formatted.
410      * @param cnt       The size of objs.
411      * @param appendTo  Output parameter to receive result.
412      *                  Result is appended to existing contents.
413      * @param pos       On input: an alignment field, if desired.
414      *                  On output: the offsets of the alignment field.
415      * @param success   Output param set to success/failure code on
416      *                  exit.
417      * @return          Reference to 'appendTo' parameter.
418      * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
419      */
420     virtual UnicodeString& format(const Formattable* objs,
421                                   int32_t cnt,
422                                   UnicodeString& appendTo,
423                                   FieldPosition& pos,
424                                   UErrorCode& success) const;
425 
426    using NumberFormat::parse;
427 
428    /**
429     * Looks for the longest match of any message string on the input text and,
430     * if there is a match, sets the result object to the corresponding range's number.
431     *
432     * If no string matches, then the parsePosition is unchanged.
433     *
434     * @param text           The text to be parsed.
435     * @param result         Formattable to be set to the parse result.
436     *                       If parse fails, return contents are undefined.
437     * @param parsePosition  The position to start parsing at on input.
438     *                       On output, moved to after the last successfully
439     *                       parse character. On parse failure, does not change.
440      * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
441     */
442     virtual void parse(const UnicodeString& text,
443                        Formattable& result,
444                        ParsePosition& parsePosition) const;
445 
446     /**
447      * Returns a unique class ID POLYMORPHICALLY. Part of ICU's "poor man's RTTI".
448      *
449      * @return          The class ID for this object. All objects of a
450      *                  given class have the same class ID.  Objects of
451      *                  other classes have different class IDs.
452      * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
453      */
454     virtual UClassID getDynamicClassID(void) const;
455 
456     /**
457      * Returns the class ID for this class.  This is useful only for
458      * comparing to a return value from getDynamicClassID().  For example:
459      * <pre>
460      * .       Base* polymorphic_pointer = createPolymorphicObject();
461      * .       if (polymorphic_pointer->getDynamicClassID() ==
462      * .           Derived::getStaticClassID()) ...
463      * </pre>
464      * @return          The class ID for all objects of this class.
465      * @deprecated ICU 49 Use MessageFormat instead, with plural and select arguments.
466      */
467     static UClassID U_EXPORT2 getStaticClassID(void);
468 
469 private:
470     /**
471      * Converts a double value to a string.
472      * @param value the double number to be converted.
473      * @param string the result string.
474      * @return the converted string.
475      */
476     static UnicodeString& dtos(double value, UnicodeString& string);
477 
478     ChoiceFormat(); // default constructor not implemented
479 
480     /**
481      * Construct a new ChoiceFormat with the limits and the corresponding formats
482      * based on the pattern.
483      *
484      * @param newPattern   Pattern used to construct object.
485      * @param parseError   Struct to receive information on position
486      *                     of error if an error is encountered.
487      * @param status       Output param to receive success code.  If the
488      *                     pattern cannot be parsed, set to failure code.
489      */
490     ChoiceFormat(const UnicodeString& newPattern,
491                  UParseError& parseError,
492                  UErrorCode& status);
493 
494     friend class MessageFormat;
495 
496     virtual void setChoices(const double* limits,
497                             const UBool* closures,
498                             const UnicodeString* formats,
499                             int32_t count,
500                             UErrorCode &errorCode);
501 
502     /**
503      * Finds the ChoiceFormat sub-message for the given number.
504      * @param pattern A MessagePattern.
505      * @param partIndex the index of the first ChoiceFormat argument style part.
506      * @param number a number to be mapped to one of the ChoiceFormat argument's intervals
507      * @return the sub-message start part index.
508      */
509     static int32_t findSubMessage(const MessagePattern &pattern, int32_t partIndex, double number);
510 
511     static double parseArgument(
512             const MessagePattern &pattern, int32_t partIndex,
513             const UnicodeString &source, ParsePosition &pos);
514 
515     /**
516      * Matches the pattern string from the end of the partIndex to
517      * the beginning of the limitPartIndex,
518      * including all syntax except SKIP_SYNTAX,
519      * against the source string starting at sourceOffset.
520      * If they match, returns the length of the source string match.
521      * Otherwise returns -1.
522      */
523     static int32_t matchStringUntilLimitPart(
524             const MessagePattern &pattern, int32_t partIndex, int32_t limitPartIndex,
525             const UnicodeString &source, int32_t sourceOffset);
526 
527     /**
528      * Some of the ChoiceFormat constructors do not have a UErrorCode paramater.
529      * We need _some_ way to provide one for the MessagePattern constructor.
530      * Alternatively, the MessagePattern could be a pointer field, but that is
531      * not nice either.
532      */
533     UErrorCode constructorErrorCode;
534 
535     /**
536      * The MessagePattern which contains the parsed structure of the pattern string.
537      *
538      * Starting with ICU 4.8, the MessagePattern contains a sequence of
539      * numeric/selector/message parts corresponding to the parsed pattern.
540      * For details see the MessagePattern class API docs.
541      */
542     MessagePattern msgPattern;
543 
544     /**
545      * Docs & fields from before ICU 4.8, before MessagePattern was used.
546      * Commented out, and left only for explanation of semantics.
547      * --------
548      * Each ChoiceFormat divides the range -Inf..+Inf into fCount
549      * intervals.  The intervals are:
550      *
551      *         0: fChoiceLimits[0]..fChoiceLimits[1]
552      *         1: fChoiceLimits[1]..fChoiceLimits[2]
553      *        ...
554      *  fCount-2: fChoiceLimits[fCount-2]..fChoiceLimits[fCount-1]
555      *  fCount-1: fChoiceLimits[fCount-1]..+Inf
556      *
557      * Interval 0 is special; during formatting (mapping numbers to
558      * strings), it also contains all numbers less than
559      * fChoiceLimits[0], as well as NaN values.
560      *
561      * Interval i maps to and from string fChoiceFormats[i].  When
562      * parsing (mapping strings to numbers), then intervals map to
563      * their lower limit, that is, interval i maps to fChoiceLimit[i].
564      *
565      * The intervals may be closed, half open, or open.  This affects
566      * formatting but does not affect parsing.  Interval i is affected
567      * by fClosures[i] and fClosures[i+1].  If fClosures[i]
568      * is FALSE, then the value fChoiceLimits[i] is in interval i.
569      * That is, intervals i and i are:
570      *
571      *  i-1:                 ... x < fChoiceLimits[i]
572      *    i: fChoiceLimits[i] <= x ...
573      *
574      * If fClosures[i] is TRUE, then the value fChoiceLimits[i] is
575      * in interval i-1.  That is, intervals i-1 and i are:
576      *
577      *  i-1:                ... x <= fChoiceLimits[i]
578      *    i: fChoiceLimits[i] < x ...
579      *
580      * Because of the nature of interval 0, fClosures[0] has no
581      * effect.
582      */
583     // double*         fChoiceLimits;
584     // UBool*          fClosures;
585     // UnicodeString*  fChoiceFormats;
586     // int32_t         fCount;
587 };
588 
589 
590 U_NAMESPACE_END
591 
592 #endif  // U_HIDE_DEPRECATED_API
593 #endif /* #if !UCONFIG_NO_FORMATTING */
594 
595 #endif // CHOICFMT_H
596 //eof
597