intl/icu/source/i18n/unicode/plurfmt.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) 2007-2013, International Business Machines Corporation and
michael@0 4 * others. All Rights Reserved.
michael@0 5 *******************************************************************************
michael@0 6 *
michael@0 7
michael@0 8 * File PLURFMT.H
michael@0 9 ********************************************************************************
michael@0 10 */
michael@0 11
michael@0 12 #ifndef PLURFMT
michael@0 13 #define PLURFMT
michael@0 14
michael@0 15 #include "unicode/utypes.h"
michael@0 16
michael@0 17 /**
michael@0 18 * \file
michael@0 19 * \brief C++ API: PluralFormat object
michael@0 20 */
michael@0 21
michael@0 22 #if !UCONFIG_NO_FORMATTING
michael@0 23
michael@0 24 #include "unicode/messagepattern.h"
michael@0 25 #include "unicode/numfmt.h"
michael@0 26 #include "unicode/plurrule.h"
michael@0 27
michael@0 28 U_NAMESPACE_BEGIN
michael@0 29
michael@0 30 class Hashtable;
michael@0 31
michael@0 32 /**
michael@0 33 * <p>
michael@0 34 * <code>PluralFormat</code> supports the creation of internationalized
michael@0 35 * messages with plural inflection. It is based on <i>plural
michael@0 36 * selection</i>, i.e. the caller specifies messages for each
michael@0 37 * plural case that can appear in the user's language and the
michael@0 38 * <code>PluralFormat</code> selects the appropriate message based on
michael@0 39 * the number.
michael@0 40 * </p>
michael@0 41 * <h4>The Problem of Plural Forms in Internationalized Messages</h4>
michael@0 42 * <p>
michael@0 43 * Different languages have different ways to inflect
michael@0 44 * plurals. Creating internationalized messages that include plural
michael@0 45 * forms is only feasible when the framework is able to handle plural
michael@0 46 * forms of <i>all</i> languages correctly. <code>ChoiceFormat</code>
michael@0 47 * doesn't handle this well, because it attaches a number interval to
michael@0 48 * each message and selects the message whose interval contains a
michael@0 49 * given number. This can only handle a finite number of
michael@0 50 * intervals. But in some languages, like Polish, one plural case
michael@0 51 * applies to infinitely many intervals (e.g., the plural case applies to
michael@0 52 * numbers ending with 2, 3, or 4 except those ending with 12, 13, or
michael@0 53 * 14). Thus <code>ChoiceFormat</code> is not adequate.
michael@0 54 * </p><p>
michael@0 55 * <code>PluralFormat</code> deals with this by breaking the problem
michael@0 56 * into two parts:
michael@0 57 * <ul>
michael@0 58 * <li>It uses <code>PluralRules</code> that can define more complex
michael@0 59 * conditions for a plural case than just a single interval. These plural
michael@0 60 * rules define both what plural cases exist in a language, and to
michael@0 61 * which numbers these cases apply.
michael@0 62 * <li>It provides predefined plural rules for many languages. Thus, the programmer
michael@0 63 * need not worry about the plural cases of a language and
michael@0 64 * does not have to define the plural cases; they can simply
michael@0 65 * use the predefined keywords. The whole plural formatting of messages can
michael@0 66 * be done using localized patterns from resource bundles. For predefined plural
michael@0 67 * rules, see the CLDR <i>Language Plural Rules</i> page at
michael@0 68 * http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
michael@0 69 * </ul>
michael@0 70 * </p>
michael@0 71 * <h4>Usage of <code>PluralFormat</code></h4>
michael@0 72 * <p>Note: Typically, plural formatting is done via <code>MessageFormat</code>
michael@0 73 * with a <code>plural</code> argument type,
michael@0 74 * rather than using a stand-alone <code>PluralFormat</code>.
michael@0 75 * </p><p>
michael@0 76 * This discussion assumes that you use <code>PluralFormat</code> with
michael@0 77 * a predefined set of plural rules. You can create one using one of
michael@0 78 * the constructors that takes a <code>locale</code> object. To
michael@0 79 * specify the message pattern, you can either pass it to the
michael@0 80 * constructor or set it explicitly using the
michael@0 81 * <code>applyPattern()</code> method. The <code>format()</code>
michael@0 82 * method takes a number object and selects the message of the
michael@0 83 * matching plural case. This message will be returned.
michael@0 84 * </p>
michael@0 85 * <h5>Patterns and Their Interpretation</h5>
michael@0 86 * <p>
michael@0 87 * The pattern text defines the message output for each plural case of the
michael@0 88 * specified locale. Syntax:
michael@0 89 * <pre>
michael@0 90 * pluralStyle = [offsetValue] (selector '{' message '}')+
michael@0 91 * offsetValue = "offset:" number
michael@0 92 * selector = explicitValue | keyword
michael@0 93 * explicitValue = '=' number // adjacent, no white space in between
michael@0 94 * keyword = [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+
michael@0 95 * message: see {@link MessageFormat}
michael@0 96 * </pre>
michael@0 97 * Pattern_White_Space between syntax elements is ignored, except
michael@0 98 * between the {curly braces} and their sub-message,
michael@0 99 * and between the '=' and the number of an explicitValue.
michael@0 100 *
michael@0 101 * </p><p>
michael@0 102 * There are 6 predefined casekeyword in CLDR/ICU - 'zero', 'one', 'two', 'few', 'many' and
michael@0 103 * 'other'. You always have to define a message text for the default plural case
michael@0 104 * <code>other</code> which is contained in every rule set.
michael@0 105 * If you do not specify a message text for a particular plural case, the
michael@0 106 * message text of the plural case <code>other</code> gets assigned to this
michael@0 107 * plural case.
michael@0 108 * </p><p>
michael@0 109 * When formatting, the input number is first matched against the explicitValue clauses.
michael@0 110 * If there is no exact-number match, then a keyword is selected by calling
michael@0 111 * the <code>PluralRules</code> with the input number <em>minus the offset</em>.
michael@0 112 * (The offset defaults to 0 if it is omitted from the pattern string.)
michael@0 113 * If there is no clause with that keyword, then the "other" clauses is returned.
michael@0 114 * </p><p>
michael@0 115 * An unquoted pound sign (<code>#</code>) in the selected sub-message
michael@0 116 * itself (i.e., outside of arguments nested in the sub-message)
michael@0 117 * is replaced by the input number minus the offset.
michael@0 118 * The number-minus-offset value is formatted using a
michael@0 119 * <code>NumberFormat</code> for the <code>PluralFormat</code>'s locale. If you
michael@0 120 * need special number formatting, you have to use a <code>MessageFormat</code>
michael@0 121 * and explicitly specify a <code>NumberFormat</code> argument.
michael@0 122 * <strong>Note:</strong> That argument is formatting without subtracting the offset!
michael@0 123 * If you need a custom format and have a non-zero offset, then you need to pass the
michael@0 124 * number-minus-offset value as a separate parameter.
michael@0 125 * </p>
michael@0 126 * For a usage example, see the {@link MessageFormat} class documentation.
michael@0 127 *
michael@0 128 * <h4>Defining Custom Plural Rules</h4>
michael@0 129 * <p>If you need to use <code>PluralFormat</code> with custom rules, you can
michael@0 130 * create a <code>PluralRules</code> object and pass it to
michael@0 131 * <code>PluralFormat</code>'s constructor. If you also specify a locale in this
michael@0 132 * constructor, this locale will be used to format the number in the message
michael@0 133 * texts.
michael@0 134 * </p><p>
michael@0 135 * For more information about <code>PluralRules</code>, see
michael@0 136 * {@link PluralRules}.
michael@0 137 * </p>
michael@0 138 *
michael@0 139 * ported from Java
michael@0 140 * @stable ICU 4.0
michael@0 141 */
michael@0 142
michael@0 143 class U_I18N_API PluralFormat : public Format {
michael@0 144 public:
michael@0 145
michael@0 146 /**
michael@0 147 * Creates a new cardinal-number <code>PluralFormat</code> for the default locale.
michael@0 148 * This locale will be used to get the set of plural rules and for standard
michael@0 149 * number formatting.
michael@0 150 * @param status output param set to success/failure code on exit, which
michael@0 151 * must not indicate a failure before the function call.
michael@0 152 * @stable ICU 4.0
michael@0 153 */
michael@0 154 PluralFormat(UErrorCode& status);
michael@0 155
michael@0 156 /**
michael@0 157 * Creates a new cardinal-number <code>PluralFormat</code> for a given locale.
michael@0 158 * @param locale the <code>PluralFormat</code> will be configured with
michael@0 159 * rules for this locale. This locale will also be used for
michael@0 160 * standard number formatting.
michael@0 161 * @param status output param set to success/failure code on exit, which
michael@0 162 * must not indicate a failure before the function call.
michael@0 163 * @stable ICU 4.0
michael@0 164 */
michael@0 165 PluralFormat(const Locale& locale, UErrorCode& status);
michael@0 166
michael@0 167 /**
michael@0 168 * Creates a new <code>PluralFormat</code> for a given set of rules.
michael@0 169 * The standard number formatting will be done using the default locale.
michael@0 170 * @param rules defines the behavior of the <code>PluralFormat</code>
michael@0 171 * object.
michael@0 172 * @param status output param set to success/failure code on exit, which
michael@0 173 * must not indicate a failure before the function call.
michael@0 174 * @stable ICU 4.0
michael@0 175 */
michael@0 176 PluralFormat(const PluralRules& rules, UErrorCode& status);
michael@0 177
michael@0 178 /**
michael@0 179 * Creates a new <code>PluralFormat</code> for a given set of rules.
michael@0 180 * The standard number formatting will be done using the given locale.
michael@0 181 * @param locale the default number formatting will be done using this
michael@0 182 * locale.
michael@0 183 * @param rules defines the behavior of the <code>PluralFormat</code>
michael@0 184 * object.
michael@0 185 * @param status output param set to success/failure code on exit, which
michael@0 186 * must not indicate a failure before the function call.
michael@0 187 * @stable ICU 4.0
michael@0 188 * <p>
michael@0 189 * <h4>Sample code</h4>
michael@0 190 * \snippet samples/plurfmtsample/plurfmtsample.cpp PluralFormatExample1
michael@0 191 * \snippet samples/plurfmtsample/plurfmtsample.cpp PluralFormatExample
michael@0 192 * <p>
michael@0 193 */
michael@0 194 PluralFormat(const Locale& locale, const PluralRules& rules, UErrorCode& status);
michael@0 195
michael@0 196 /**
michael@0 197 * Creates a new <code>PluralFormat</code> for the plural type.
michael@0 198 * The standard number formatting will be done using the given locale.
michael@0 199 * @param locale the default number formatting will be done using this
michael@0 200 * locale.
michael@0 201 * @param type The plural type (e.g., cardinal or ordinal).
michael@0 202 * @param status output param set to success/failure code on exit, which
michael@0 203 * must not indicate a failure before the function call.
michael@0 204 * @stable ICU 50
michael@0 205 */
michael@0 206 PluralFormat(const Locale& locale, UPluralType type, UErrorCode& status);
michael@0 207
michael@0 208 /**
michael@0 209 * Creates a new cardinal-number <code>PluralFormat</code> for a given pattern string.
michael@0 210 * The default locale will be used to get the set of plural rules and for
michael@0 211 * standard number formatting.
michael@0 212 * @param pattern the pattern for this <code>PluralFormat</code>.
michael@0 213 * errors are returned to status if the pattern is invalid.
michael@0 214 * @param status output param set to success/failure code on exit, which
michael@0 215 * must not indicate a failure before the function call.
michael@0 216 * @stable ICU 4.0
michael@0 217 */
michael@0 218 PluralFormat(const UnicodeString& pattern, UErrorCode& status);
michael@0 219
michael@0 220 /**
michael@0 221 * Creates a new cardinal-number <code>PluralFormat</code> for a given pattern string and
michael@0 222 * locale.
michael@0 223 * The locale will be used to get the set of plural rules and for
michael@0 224 * standard number formatting.
michael@0 225 * @param locale the <code>PluralFormat</code> will be configured with
michael@0 226 * rules for this locale. This locale will also be used for
michael@0 227 * standard number formatting.
michael@0 228 * @param pattern the pattern for this <code>PluralFormat</code>.
michael@0 229 * errors are returned to status if the pattern is invalid.
michael@0 230 * @param status output param set to success/failure code on exit, which
michael@0 231 * must not indicate a failure before the function call.
michael@0 232 * @stable ICU 4.0
michael@0 233 */
michael@0 234 PluralFormat(const Locale& locale, const UnicodeString& pattern, UErrorCode& status);
michael@0 235
michael@0 236 /**
michael@0 237 * Creates a new <code>PluralFormat</code> for a given set of rules, a
michael@0 238 * pattern and a locale.
michael@0 239 * @param rules defines the behavior of the <code>PluralFormat</code>
michael@0 240 * object.
michael@0 241 * @param pattern the pattern for this <code>PluralFormat</code>.
michael@0 242 * errors are returned to status if the pattern is invalid.
michael@0 243 * @param status output param set to success/failure code on exit, which
michael@0 244 * must not indicate a failure before the function call.
michael@0 245 * @stable ICU 4.0
michael@0 246 */
michael@0 247 PluralFormat(const PluralRules& rules,
michael@0 248 const UnicodeString& pattern,
michael@0 249 UErrorCode& status);
michael@0 250
michael@0 251 /**
michael@0 252 * Creates a new <code>PluralFormat</code> for a given set of rules, a
michael@0 253 * pattern and a locale.
michael@0 254 * @param locale the <code>PluralFormat</code> will be configured with
michael@0 255 * rules for this locale. This locale will also be used for
michael@0 256 * standard number formatting.
michael@0 257 * @param rules defines the behavior of the <code>PluralFormat</code>
michael@0 258 * object.
michael@0 259 * @param pattern the pattern for this <code>PluralFormat</code>.
michael@0 260 * errors are returned to status if the pattern is invalid.
michael@0 261 * @param status output param set to success/failure code on exit, which
michael@0 262 * must not indicate a failure before the function call.
michael@0 263 * @stable ICU 4.0
michael@0 264 */
michael@0 265 PluralFormat(const Locale& locale,
michael@0 266 const PluralRules& rules,
michael@0 267 const UnicodeString& pattern,
michael@0 268 UErrorCode& status);
michael@0 269
michael@0 270 /**
michael@0 271 * Creates a new <code>PluralFormat</code> for a plural type, a
michael@0 272 * pattern and a locale.
michael@0 273 * @param locale the <code>PluralFormat</code> will be configured with
michael@0 274 * rules for this locale. This locale will also be used for
michael@0 275 * standard number formatting.
michael@0 276 * @param type The plural type (e.g., cardinal or ordinal).
michael@0 277 * @param pattern the pattern for this <code>PluralFormat</code>.
michael@0 278 * errors are returned to status if the pattern is invalid.
michael@0 279 * @param status output param set to success/failure code on exit, which
michael@0 280 * must not indicate a failure before the function call.
michael@0 281 * @stable ICU 50
michael@0 282 */
michael@0 283 PluralFormat(const Locale& locale,
michael@0 284 UPluralType type,
michael@0 285 const UnicodeString& pattern,
michael@0 286 UErrorCode& status);
michael@0 287
michael@0 288 /**
michael@0 289 * copy constructor.
michael@0 290 * @stable ICU 4.0
michael@0 291 */
michael@0 292 PluralFormat(const PluralFormat& other);
michael@0 293
michael@0 294 /**
michael@0 295 * Destructor.
michael@0 296 * @stable ICU 4.0
michael@0 297 */
michael@0 298 virtual ~PluralFormat();
michael@0 299
michael@0 300 /**
michael@0 301 * Sets the pattern used by this plural format.
michael@0 302 * The method parses the pattern and creates a map of format strings
michael@0 303 * for the plural rules.
michael@0 304 * Patterns and their interpretation are specified in the class description.
michael@0 305 *
michael@0 306 * @param pattern the pattern for this plural format
michael@0 307 * errors are returned to status if the pattern is invalid.
michael@0 308 * @param status output param set to success/failure code on exit, which
michael@0 309 * must not indicate a failure before the function call.
michael@0 310 * @stable ICU 4.0
michael@0 311 */
michael@0 312 void applyPattern(const UnicodeString& pattern, UErrorCode& status);
michael@0 313
michael@0 314
michael@0 315 using Format::format;
michael@0 316
michael@0 317 /**
michael@0 318 * Formats a plural message for a given number.
michael@0 319 *
michael@0 320 * @param number a number for which the plural message should be formatted
michael@0 321 * for. If no pattern has been applied to this
michael@0 322 * <code>PluralFormat</code> object yet, the formatted number
michael@0 323 * will be returned.
michael@0 324 * @param status output param set to success/failure code on exit, which
michael@0 325 * must not indicate a failure before the function call.
michael@0 326 * @return the string containing the formatted plural message.
michael@0 327 * @stable ICU 4.0
michael@0 328 */
michael@0 329 UnicodeString format(int32_t number, UErrorCode& status) const;
michael@0 330
michael@0 331 /**
michael@0 332 * Formats a plural message for a given number.
michael@0 333 *
michael@0 334 * @param number a number for which the plural message should be formatted
michael@0 335 * for. If no pattern has been applied to this
michael@0 336 * PluralFormat object yet, the formatted number
michael@0 337 * will be returned.
michael@0 338 * @param status output param set to success or failure code on exit, which
michael@0 339 * must not indicate a failure before the function call.
michael@0 340 * @return the string containing the formatted plural message.
michael@0 341 * @stable ICU 4.0
michael@0 342 */
michael@0 343 UnicodeString format(double number, UErrorCode& status) const;
michael@0 344
michael@0 345 /**
michael@0 346 * Formats a plural message for a given number.
michael@0 347 *
michael@0 348 * @param number a number for which the plural message should be formatted
michael@0 349 * for. If no pattern has been applied to this
michael@0 350 * <code>PluralFormat</code> object yet, the formatted number
michael@0 351 * will be returned.
michael@0 352 * @param appendTo output parameter to receive result.
michael@0 353 * result is appended to existing contents.
michael@0 354 * @param pos On input: an alignment field, if desired.
michael@0 355 * On output: the offsets of the alignment field.
michael@0 356 * @param status output param set to success/failure code on exit, which
michael@0 357 * must not indicate a failure before the function call.
michael@0 358 * @return the string containing the formatted plural message.
michael@0 359 * @stable ICU 4.0
michael@0 360 */
michael@0 361 UnicodeString& format(int32_t number,
michael@0 362 UnicodeString& appendTo,
michael@0 363 FieldPosition& pos,
michael@0 364 UErrorCode& status) const;
michael@0 365
michael@0 366 /**
michael@0 367 * Formats a plural message for a given number.
michael@0 368 *
michael@0 369 * @param number a number for which the plural message should be formatted
michael@0 370 * for. If no pattern has been applied to this
michael@0 371 * PluralFormat object yet, the formatted number
michael@0 372 * will be returned.
michael@0 373 * @param appendTo output parameter to receive result.
michael@0 374 * result is appended to existing contents.
michael@0 375 * @param pos On input: an alignment field, if desired.
michael@0 376 * On output: the offsets of the alignment field.
michael@0 377 * @param status output param set to success/failure code on exit, which
michael@0 378 * must not indicate a failure before the function call.
michael@0 379 * @return the string containing the formatted plural message.
michael@0 380 * @stable ICU 4.0
michael@0 381 */
michael@0 382 UnicodeString& format(double number,
michael@0 383 UnicodeString& appendTo,
michael@0 384 FieldPosition& pos,
michael@0 385 UErrorCode& status) const;
michael@0 386
michael@0 387 #ifndef U_HIDE_DEPRECATED_API
michael@0 388 /**
michael@0 389 * Sets the locale used by this <code>PluraFormat</code> object.
michael@0 390 * Note: Calling this method resets this <code>PluraFormat</code> object,
michael@0 391 * i.e., a pattern that was applied previously will be removed,
michael@0 392 * and the NumberFormat is set to the default number format for
michael@0 393 * the locale. The resulting format behaves the same as one
michael@0 394 * constructed from {@link #PluralFormat(const Locale& locale, UPluralType type, UErrorCode& status)}
michael@0 395 * with UPLURAL_TYPE_CARDINAL.
michael@0 396 * @param locale the <code>locale</code> to use to configure the formatter.
michael@0 397 * @param status output param set to success/failure code on exit, which
michael@0 398 * must not indicate a failure before the function call.
michael@0 399 * @deprecated ICU 50 This method clears the pattern and might create
michael@0 400 * a different kind of PluralRules instance;
michael@0 401 * use one of the constructors to create a new instance instead.
michael@0 402 */
michael@0 403 void setLocale(const Locale& locale, UErrorCode& status);
michael@0 404 #endif /* U_HIDE_DEPRECATED_API */
michael@0 405
michael@0 406 /**
michael@0 407 * Sets the number format used by this formatter. You only need to
michael@0 408 * call this if you want a different number format than the default
michael@0 409 * formatter for the locale.
michael@0 410 * @param format the number format to use.
michael@0 411 * @param status output param set to success/failure code on exit, which
michael@0 412 * must not indicate a failure before the function call.
michael@0 413 * @stable ICU 4.0
michael@0 414 */
michael@0 415 void setNumberFormat(const NumberFormat* format, UErrorCode& status);
michael@0 416
michael@0 417 /**
michael@0 418 * Assignment operator
michael@0 419 *
michael@0 420 * @param other the PluralFormat object to copy from.
michael@0 421 * @stable ICU 4.0
michael@0 422 */
michael@0 423 PluralFormat& operator=(const PluralFormat& other);
michael@0 424
michael@0 425 /**
michael@0 426 * Return true if another object is semantically equal to this one.
michael@0 427 *
michael@0 428 * @param other the PluralFormat object to be compared with.
michael@0 429 * @return true if other is semantically equal to this.
michael@0 430 * @stable ICU 4.0
michael@0 431 */
michael@0 432 virtual UBool operator==(const Format& other) const;
michael@0 433
michael@0 434 /**
michael@0 435 * Return true if another object is semantically unequal to this one.
michael@0 436 *
michael@0 437 * @param other the PluralFormat object to be compared with.
michael@0 438 * @return true if other is semantically unequal to this.
michael@0 439 * @stable ICU 4.0
michael@0 440 */
michael@0 441 virtual UBool operator!=(const Format& other) const;
michael@0 442
michael@0 443 /**
michael@0 444 * Clones this Format object polymorphically. The caller owns the
michael@0 445 * result and should delete it when done.
michael@0 446 * @stable ICU 4.0
michael@0 447 */
michael@0 448 virtual Format* clone(void) const;
michael@0 449
michael@0 450 /**
michael@0 451 * Formats a plural message for a number taken from a Formattable object.
michael@0 452 *
michael@0 453 * @param obj The object containing a number for which the
michael@0 454 * plural message should be formatted.
michael@0 455 * The object must be of a numeric type.
michael@0 456 * @param appendTo output parameter to receive result.
michael@0 457 * Result is appended to existing contents.
michael@0 458 * @param pos On input: an alignment field, if desired.
michael@0 459 * On output: the offsets of the alignment field.
michael@0 460 * @param status output param filled with success/failure status.
michael@0 461 * @return Reference to 'appendTo' parameter.
michael@0 462 * @stable ICU 4.0
michael@0 463 */
michael@0 464 UnicodeString& format(const Formattable& obj,
michael@0 465 UnicodeString& appendTo,
michael@0 466 FieldPosition& pos,
michael@0 467 UErrorCode& status) const;
michael@0 468
michael@0 469 /**
michael@0 470 * Returns the pattern from applyPattern() or constructor().
michael@0 471 *
michael@0 472 * @param appendTo output parameter to receive result.
michael@0 473 * Result is appended to existing contents.
michael@0 474 * @return the UnicodeString with inserted pattern.
michael@0 475 * @stable ICU 4.0
michael@0 476 */
michael@0 477 UnicodeString& toPattern(UnicodeString& appendTo);
michael@0 478
michael@0 479 /**
michael@0 480 * This method is not yet supported by <code>PluralFormat</code>.
michael@0 481 * <P>
michael@0 482 * Before calling, set parse_pos.index to the offset you want to start
michael@0 483 * parsing at in the source. After calling, parse_pos.index is the end of
michael@0 484 * the text you parsed. If error occurs, index is unchanged.
michael@0 485 * <P>
michael@0 486 * When parsing, leading whitespace is discarded (with a successful parse),
michael@0 487 * while trailing whitespace is left as is.
michael@0 488 * <P>
michael@0 489 * See Format::parseObject() for more.
michael@0 490 *
michael@0 491 * @param source The string to be parsed into an object.
michael@0 492 * @param result Formattable to be set to the parse result.
michael@0 493 * If parse fails, return contents are undefined.
michael@0 494 * @param parse_pos The position to start parsing at. Upon return
michael@0 495 * this param is set to the position after the
michael@0 496 * last character successfully parsed. If the
michael@0 497 * source is not parsed successfully, this param
michael@0 498 * will remain unchanged.
michael@0 499 * @stable ICU 4.0
michael@0 500 */
michael@0 501 virtual void parseObject(const UnicodeString& source,
michael@0 502 Formattable& result,
michael@0 503 ParsePosition& parse_pos) const;
michael@0 504
michael@0 505 /**
michael@0 506 * ICU "poor man's RTTI", returns a UClassID for this class.
michael@0 507 *
michael@0 508 * @stable ICU 4.0
michael@0 509 *
michael@0 510 */
michael@0 511 static UClassID U_EXPORT2 getStaticClassID(void);
michael@0 512
michael@0 513 /**
michael@0 514 * ICU "poor man's RTTI", returns a UClassID for the actual class.
michael@0 515 *
michael@0 516 * @stable ICU 4.0
michael@0 517 */
michael@0 518 virtual UClassID getDynamicClassID() const;
michael@0 519
michael@0 520 #if (defined(__xlC__) && (__xlC__ < 0x0C00)) || (U_PLATFORM == U_PF_OS390) || (U_PLATFORM ==U_PF_OS400)
michael@0 521 // Work around a compiler bug on xlC 11.1 on AIX 7.1 that would
michael@0 522 // prevent PluralSelectorAdapter from implementing private PluralSelector.
michael@0 523 // xlC error message:
michael@0 524 // 1540-0300 (S) The "private" member "class icu_49::PluralFormat::PluralSelector" cannot be accessed.
michael@0 525 public:
michael@0 526 #else
michael@0 527 private:
michael@0 528 #endif
michael@0 529 /**
michael@0 530 * @internal
michael@0 531 */
michael@0 532 class U_I18N_API PluralSelector : public UMemory {
michael@0 533 public:
michael@0 534 virtual ~PluralSelector();
michael@0 535 /**
michael@0 536 * Given a number, returns the appropriate PluralFormat keyword.
michael@0 537 *
michael@0 538 * @param context worker object for the selector.
michael@0 539 * @param number The number to be plural-formatted.
michael@0 540 * @param ec Error code.
michael@0 541 * @return The selected PluralFormat keyword.
michael@0 542 * @internal
michael@0 543 */
michael@0 544 virtual UnicodeString select(void *context, double number, UErrorCode& ec) const = 0;
michael@0 545 };
michael@0 546
michael@0 547 /**
michael@0 548 * @internal
michael@0 549 */
michael@0 550 class U_I18N_API PluralSelectorAdapter : public PluralSelector {
michael@0 551 public:
michael@0 552 PluralSelectorAdapter() : pluralRules(NULL) {
michael@0 553 }
michael@0 554
michael@0 555 virtual ~PluralSelectorAdapter();
michael@0 556
michael@0 557 virtual UnicodeString select(void *context, double number, UErrorCode& /*ec*/) const; /**< @internal */
michael@0 558
michael@0 559 void reset();
michael@0 560
michael@0 561 PluralRules* pluralRules;
michael@0 562 };
michael@0 563
michael@0 564 #if defined(__xlC__)
michael@0 565 // End of xlC bug workaround, keep remaining definitions private.
michael@0 566 private:
michael@0 567 #endif
michael@0 568 Locale locale;
michael@0 569 MessagePattern msgPattern;
michael@0 570 NumberFormat* numberFormat;
michael@0 571 double offset;
michael@0 572 PluralSelectorAdapter pluralRulesWrapper;
michael@0 573
michael@0 574 PluralFormat(); // default constructor not implemented
michael@0 575 void init(const PluralRules* rules, UPluralType type, UErrorCode& status);
michael@0 576 /**
michael@0 577 * Copies dynamically allocated values (pointer fields).
michael@0 578 * Others are copied using their copy constructors and assignment operators.
michael@0 579 */
michael@0 580 void copyObjects(const PluralFormat& other);
michael@0 581
michael@0 582 UnicodeString& format(const Formattable& numberObject, double number,
michael@0 583 UnicodeString& appendTo,
michael@0 584 FieldPosition& pos,
michael@0 585 UErrorCode& status) const; /**< @internal */
michael@0 586
michael@0 587 /**
michael@0 588 * Finds the PluralFormat sub-message for the given number, or the "other" sub-message.
michael@0 589 * @param pattern A MessagePattern.
michael@0 590 * @param partIndex the index of the first PluralFormat argument style part.
michael@0 591 * @param selector the PluralSelector for mapping the number (minus offset) to a keyword.
michael@0 592 * @param context worker object for the selector.
michael@0 593 * @param number a number to be matched to one of the PluralFormat argument's explicit values,
michael@0 594 * or mapped via the PluralSelector.
michael@0 595 * @param ec ICU error code.
michael@0 596 * @return the sub-message start part index.
michael@0 597 */
michael@0 598 static int32_t findSubMessage(
michael@0 599 const MessagePattern& pattern, int32_t partIndex,
michael@0 600 const PluralSelector& selector, void *context, double number, UErrorCode& ec); /**< @internal */
michael@0 601
michael@0 602 friend class MessageFormat;
michael@0 603 };
michael@0 604
michael@0 605 U_NAMESPACE_END
michael@0 606
michael@0 607 #endif /* #if !UCONFIG_NO_FORMATTING */
michael@0 608
michael@0 609 #endif // _PLURFMT
michael@0 610 //eof

mercurial