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

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial