Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/unicode/plurrule.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // © 2016 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 /*
0004 *******************************************************************************
0005 * Copyright (C) 2008-2015, International Business Machines Corporation and
0006 * others. All Rights Reserved.
0007 *******************************************************************************
0008 *
0009 *
0010 * File PLURRULE.H
0011 *
0012 * Modification History:*
0013 *   Date        Name        Description
0014 *
0015 ********************************************************************************
0016 */
0017 
0018 #ifndef PLURRULE
0019 #define PLURRULE
0020 
0021 #include "unicode/utypes.h"
0022 
0023 #if U_SHOW_CPLUSPLUS_API
0024 
0025 /**
0026  * \file
0027  * \brief C++ API: PluralRules object
0028  */
0029 
0030 #if !UCONFIG_NO_FORMATTING
0031 
0032 #include "unicode/format.h"
0033 #include "unicode/upluralrules.h"
0034 #ifndef U_HIDE_INTERNAL_API
0035 #include "unicode/numfmt.h"
0036 #endif  /* U_HIDE_INTERNAL_API */
0037 
0038 /**
0039  * Value returned by PluralRules::getUniqueKeywordValue() when there is no
0040  * unique value to return.
0041  * @stable ICU 4.8
0042  */
0043 #define UPLRULES_NO_UNIQUE_VALUE ((double)-0.00123456777)
0044 
0045 U_NAMESPACE_BEGIN
0046 
0047 class Hashtable;
0048 class IFixedDecimal;
0049 class FixedDecimal;
0050 class RuleChain;
0051 class PluralRuleParser;
0052 class PluralKeywordEnumeration;
0053 class AndConstraint;
0054 class SharedPluralRules;
0055 class StandardPluralRanges;
0056 
0057 namespace number {
0058 class FormattedNumber;
0059 class FormattedNumberRange;
0060 namespace impl {
0061 class UFormattedNumberRangeData;
0062 class DecimalQuantity;
0063 class DecNum;
0064 }
0065 }
0066 
0067 #ifndef U_HIDE_INTERNAL_API
0068 using icu::number::impl::DecimalQuantity;
0069 #endif  /* U_HIDE_INTERNAL_API */
0070 
0071 /**
0072  * Defines rules for mapping non-negative numeric values onto a small set of
0073  * keywords. Rules are constructed from a text description, consisting
0074  * of a series of keywords and conditions.  The {@link #select} method
0075  * examines each condition in order and returns the keyword for the
0076  * first condition that matches the number.  If none match,
0077  * default rule(other) is returned.
0078  *
0079  * For more information, details, and tips for writing rules, see the
0080  * LDML spec, Part 3.5 Language Plural Rules:
0081  * https://www.unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules
0082  *
0083  * Examples:<pre>
0084  *   "one: n is 1; few: n in 2..4"</pre>
0085  *  This defines two rules, for 'one' and 'few'.  The condition for
0086  *  'one' is "n is 1" which means that the number must be equal to
0087  *  1 for this condition to pass.  The condition for 'few' is
0088  *  "n in 2..4" which means that the number must be between 2 and
0089  *  4 inclusive for this condition to pass.  All other numbers
0090  *  are assigned the keyword "other" by the default rule.
0091  *  </p><pre>
0092  *    "zero: n is 0; one: n is 1; zero: n mod 100 in 1..19"</pre>
0093  *  This illustrates that the same keyword can be defined multiple times.
0094  *  Each rule is examined in order, and the first keyword whose condition
0095  *  passes is the one returned.  Also notes that a modulus is applied
0096  *  to n in the last rule.  Thus its condition holds for 119, 219, 319...
0097  *  </p><pre>
0098  *    "one: n is 1; few: n mod 10 in 2..4 and n mod 100 not in 12..14"</pre>
0099  *  This illustrates conjunction and negation.  The condition for 'few'
0100  *  has two parts, both of which must be met: "n mod 10 in 2..4" and
0101  *  "n mod 100 not in 12..14".  The first part applies a modulus to n
0102  *  before the test as in the previous example.  The second part applies
0103  *  a different modulus and also uses negation, thus it matches all
0104  *  numbers _not_ in 12, 13, 14, 112, 113, 114, 212, 213, 214...
0105  *  </p>
0106  *  <p>
0107  * Syntax:<pre>
0108  * \code
0109  * rules         = rule (';' rule)*
0110  * rule          = keyword ':' condition
0111  * keyword       = <identifier>
0112  * condition     = and_condition ('or' and_condition)*
0113  * and_condition = relation ('and' relation)*
0114  * relation      = is_relation | in_relation | within_relation | 'n' <EOL>
0115  * is_relation   = expr 'is' ('not')? value
0116  * in_relation   = expr ('not')? 'in' range_list
0117  * within_relation = expr ('not')? 'within' range
0118  * expr          = ('n' | 'i' | 'f' | 'v' | 'j') ('mod' value)?
0119  * range_list    = (range | value) (',' range_list)*
0120  * value         = digit+  ('.' digit+)?
0121  * digit         = 0|1|2|3|4|5|6|7|8|9
0122  * range         = value'..'value
0123  * \endcode
0124  * </pre></p>
0125  * <p>
0126  * <p>
0127  * The i, f, and v values are defined as follows:
0128  * </p>
0129  * <ul>
0130  * <li>i to be the integer digits.</li>
0131  * <li>f to be the visible fractional digits, as an integer.</li>
0132  * <li>v to be the number of visible fraction digits.</li>
0133  * <li>j is defined to only match integers. That is j is 3 fails if v != 0 (eg for 3.1 or 3.0).</li>
0134  * </ul>
0135  * <p>
0136  * Examples are in the following table:
0137  * </p>
0138  * <table border='1' style="border-collapse:collapse">
0139  * <tr>
0140  * <th>n</th>
0141  * <th>i</th>
0142  * <th>f</th>
0143  * <th>v</th>
0144  * </tr>
0145  * <tr>
0146  * <td>1.0</td>
0147  * <td>1</td>
0148  * <td align="right">0</td>
0149  * <td>1</td>
0150  * </tr>
0151  * <tr>
0152  * <td>1.00</td>
0153  * <td>1</td>
0154  * <td align="right">0</td>
0155  * <td>2</td>
0156  * </tr>
0157  * <tr>
0158  * <td>1.3</td>
0159  * <td>1</td>
0160  * <td align="right">3</td>
0161  * <td>1</td>
0162  * </tr>
0163  * <tr>
0164  * <td>1.03</td>
0165  * <td>1</td>
0166  * <td align="right">3</td>
0167  * <td>2</td>
0168  * </tr>
0169  * <tr>
0170  * <td>1.23</td>
0171  * <td>1</td>
0172  * <td align="right">23</td>
0173  * <td>2</td>
0174  * </tr>
0175  * </table>
0176  * <p>
0177  * The difference between 'in' and 'within' is that 'in' only includes integers in the specified range, while 'within'
0178  * includes all values. Using 'within' with a range_list consisting entirely of values is the same as using 'in' (it's
0179  * not an error).
0180  * </p>
0181 
0182  * An "identifier" is a sequence of characters that do not have the
0183  * Unicode Pattern_Syntax or Pattern_White_Space properties.
0184  * <p>
0185  * The difference between 'in' and 'within' is that 'in' only includes
0186  * integers in the specified range, while 'within' includes all values.
0187  * Using 'within' with a range_list consisting entirely of values is the
0188  * same as using 'in' (it's not an error).
0189  *</p>
0190  * <p>
0191  * Keywords
0192  * could be defined by users or from ICU locale data. There are 6
0193  * predefined values in ICU - 'zero', 'one', 'two', 'few', 'many' and
0194  * 'other'. Callers need to check the value of keyword returned by
0195  * {@link #select} method.
0196  * </p>
0197  *
0198  * Examples:<pre>
0199  * UnicodeString keyword = pl->select(number);
0200  * if (keyword== UnicodeString("one") {
0201  *     ...
0202  * }
0203  * else if ( ... )
0204  * </pre>
0205  * <strong>Note:</strong><br>
0206  *  <p>
0207  *   ICU defines plural rules for many locales based on CLDR <i>Language Plural Rules</i>.
0208  *   For these predefined rules, see CLDR page at
0209  *   https://unicode-org.github.io/cldr-staging/charts/latest/supplemental/language_plural_rules.html
0210  * </p>
0211  */
0212 class U_I18N_API PluralRules : public UObject {
0213 public:
0214 
0215     /**
0216      * Constructor.
0217      * @param status  Output param set to success/failure code on exit, which
0218      *                must not indicate a failure before the function call.
0219      *
0220      * @stable ICU 4.0
0221      */
0222     PluralRules(UErrorCode& status);
0223 
0224     /**
0225      * Copy constructor.
0226      * @stable ICU 4.0
0227      */
0228     PluralRules(const PluralRules& other);
0229 
0230     /**
0231      * Destructor.
0232      * @stable ICU 4.0
0233      */
0234     virtual ~PluralRules();
0235 
0236     /**
0237      * Clone
0238      * @stable ICU 4.0
0239      */
0240     PluralRules* clone() const;
0241 
0242     /**
0243       * Assignment operator.
0244       * @stable ICU 4.0
0245       */
0246     PluralRules& operator=(const PluralRules&);
0247 
0248     /**
0249      * Creates a PluralRules from a description if it is parsable, otherwise
0250      * returns nullptr.
0251      *
0252      * @param description rule description
0253      * @param status      Output param set to success/failure code on exit, which
0254      *                    must not indicate a failure before the function call.
0255      * @return            new PluralRules pointer. nullptr if there is an error.
0256      * @stable ICU 4.0
0257      */
0258     static PluralRules* U_EXPORT2 createRules(const UnicodeString& description,
0259                                               UErrorCode& status);
0260 
0261     /**
0262      * The default rules that accept any number.
0263      *
0264      * @param status  Output param set to success/failure code on exit, which
0265      *                must not indicate a failure before the function call.
0266      * @return        new PluralRules pointer. nullptr if there is an error.
0267      * @stable ICU 4.0
0268      */
0269     static PluralRules* U_EXPORT2 createDefaultRules(UErrorCode& status);
0270 
0271     /**
0272      * Provides access to the predefined cardinal-number <code>PluralRules</code> for a given
0273      * locale.
0274      * Same as forLocale(locale, UPLURAL_TYPE_CARDINAL, status).
0275      *
0276      * @param locale  The locale for which a <code>PluralRules</code> object is
0277      *                returned.
0278      * @param status  Output param set to success/failure code on exit, which
0279      *                must not indicate a failure before the function call.
0280      * @return        The predefined <code>PluralRules</code> object pointer for
0281      *                this locale. If there's no predefined rules for this locale,
0282      *                the rules for the closest parent in the locale hierarchy
0283      *                that has one will  be returned.  The final fallback always
0284      *                returns the default 'other' rules.
0285      * @stable ICU 4.0
0286      */
0287     static PluralRules* U_EXPORT2 forLocale(const Locale& locale, UErrorCode& status);
0288 
0289     /**
0290      * Provides access to the predefined <code>PluralRules</code> for a given
0291      * locale and the plural type.
0292      *
0293      * @param locale  The locale for which a <code>PluralRules</code> object is
0294      *                returned.
0295      * @param type    The plural type (e.g., cardinal or ordinal).
0296      * @param status  Output param set to success/failure code on exit, which
0297      *                must not indicate a failure before the function call.
0298      * @return        The predefined <code>PluralRules</code> object pointer for
0299      *                this locale. If there's no predefined rules for this locale,
0300      *                the rules for the closest parent in the locale hierarchy
0301      *                that has one will  be returned.  The final fallback always
0302      *                returns the default 'other' rules.
0303      * @stable ICU 50
0304      */
0305     static PluralRules* U_EXPORT2 forLocale(const Locale& locale, UPluralType type, UErrorCode& status);
0306 
0307 #ifndef U_HIDE_INTERNAL_API
0308     /**
0309      * Return a StringEnumeration over the locales for which there is plurals data.
0310      * @return a StringEnumeration over the locales available.
0311      * @internal
0312      */
0313     static StringEnumeration* U_EXPORT2 getAvailableLocales(UErrorCode &status);
0314 
0315     /**
0316      * For ICU use only.
0317      * creates a  SharedPluralRules object
0318      * @internal
0319      */
0320     static PluralRules* U_EXPORT2 internalForLocale(const Locale& locale, UPluralType type, UErrorCode& status);
0321 
0322     /**
0323      * For ICU use only.
0324      * Returns handle to the shared, cached PluralRules instance.
0325      * Caller must call removeRef() on returned value once it is done with
0326      * the shared instance.
0327      * @internal
0328      */
0329     static const SharedPluralRules* U_EXPORT2 createSharedInstance(
0330             const Locale& locale, UPluralType type, UErrorCode& status);
0331 
0332 
0333 #endif  /* U_HIDE_INTERNAL_API */
0334 
0335     /**
0336      * Given an integer, returns the keyword of the first rule
0337      * that applies to  the number.  This function can be used with
0338      * isKeyword* functions to determine the keyword for default plural rules.
0339      *
0340      * @param number  The number for which the rule has to be determined.
0341      * @return        The keyword of the selected rule.
0342      * @stable ICU 4.0
0343      */
0344     UnicodeString select(int32_t number) const;
0345 
0346     /**
0347      * Given a floating-point number, returns the keyword of the first rule
0348      * that applies to  the number.  This function can be used with
0349      * isKeyword* functions to determine the keyword for default plural rules.
0350      *
0351      * @param number  The number for which the rule has to be determined.
0352      * @return        The keyword of the selected rule.
0353      * @stable ICU 4.0
0354      */
0355     UnicodeString select(double number) const;
0356 
0357     /**
0358      * Given a formatted number, returns the keyword of the first rule
0359      * that applies to  the number.  This function can be used with
0360      * isKeyword* functions to determine the keyword for default plural rules.
0361      *
0362      * A FormattedNumber allows you to specify an exponent or trailing zeros,
0363      * which can affect the plural category. To get a FormattedNumber, see
0364      * NumberFormatter.
0365      *
0366      * @param number  The number for which the rule has to be determined.
0367      * @param status  Set if an error occurs while selecting plural keyword.
0368      *                This could happen if the FormattedNumber is invalid.
0369      * @return        The keyword of the selected rule.
0370      * @stable ICU 64
0371      */
0372     UnicodeString select(const number::FormattedNumber& number, UErrorCode& status) const;
0373 
0374     /**
0375      * Given a formatted number range, returns the overall plural form of the
0376      * range. For example, "3-5" returns "other" in English.
0377      *
0378      * To get a FormattedNumberRange, see NumberRangeFormatter.
0379      * 
0380      * This method only works if PluralRules was created with a locale. If it was created
0381      * from PluralRules::createRules(), this method sets status code U_UNSUPPORTED_ERROR.
0382      * 
0383      * @param range  The number range onto which the rules will be applied.
0384      * @param status Set if an error occurs while selecting plural keyword.
0385      *               This could happen if the FormattedNumberRange is invalid,
0386      *               or if plural ranges data is unavailable.
0387      * @return       The keyword of the selected rule.
0388      * @stable ICU 68
0389      */
0390     UnicodeString select(const number::FormattedNumberRange& range, UErrorCode& status) const;
0391 
0392 #ifndef U_HIDE_INTERNAL_API
0393     /**
0394      * @internal
0395      */
0396     UnicodeString select(const IFixedDecimal &number) const;
0397     /**
0398      * @internal
0399      */
0400     UnicodeString select(const number::impl::UFormattedNumberRangeData* urange, UErrorCode& status) const;
0401 #endif  /* U_HIDE_INTERNAL_API */
0402 
0403     /**
0404      * Returns a list of all rule keywords used in this <code>PluralRules</code>
0405      * object.  The rule 'other' is always present by default.
0406      *
0407      * @param status Output param set to success/failure code on exit, which
0408      *               must not indicate a failure before the function call.
0409      * @return       StringEnumeration with the keywords.
0410      *               The caller must delete the object.
0411      * @stable ICU 4.0
0412      */
0413     StringEnumeration* getKeywords(UErrorCode& status) const;
0414 
0415 #ifndef U_HIDE_DEPRECATED_API
0416     /**
0417      * Deprecated Function, does not return useful results.
0418      *
0419      * Originally intended to return a unique value for this keyword if it exists,
0420      * else the constant UPLRULES_NO_UNIQUE_VALUE.
0421      *
0422      * @param keyword The keyword.
0423      * @return        Stub deprecated function returns UPLRULES_NO_UNIQUE_VALUE always.
0424      * @deprecated ICU 55
0425      */
0426     double getUniqueKeywordValue(const UnicodeString& keyword);
0427 
0428     /**
0429      * Deprecated Function, does not produce useful results.
0430      *
0431      * Originally intended to return all the values for which select() would return the keyword.
0432      * If the keyword is unknown, returns no values, but this is not an error.  If
0433      * the number of values is unlimited, returns no values and -1 as the
0434      * count.
0435      *
0436      * The number of returned values is typically small.
0437      *
0438      * @param keyword      The keyword.
0439      * @param dest         Array into which to put the returned values.  May
0440      *                     be nullptr if destCapacity is 0.
0441      * @param destCapacity The capacity of the array, must be at least 0.
0442      * @param status       The error code. Deprecated function, always sets U_UNSUPPORTED_ERROR.
0443      * @return             The count of values available, or -1.  This count
0444      *                     can be larger than destCapacity, but no more than
0445      *                     destCapacity values will be written.
0446      * @deprecated ICU 55
0447      */
0448     int32_t getAllKeywordValues(const UnicodeString &keyword,
0449                                 double *dest, int32_t destCapacity,
0450                                 UErrorCode& status);
0451 #endif  /* U_HIDE_DEPRECATED_API */
0452 
0453     /**
0454      * Returns sample values for which select() would return the keyword.  If
0455      * the keyword is unknown, returns no values, but this is not an error.
0456      *
0457      * The number of returned values is typically small.
0458      *
0459      * @param keyword      The keyword.
0460      * @param dest         Array into which to put the returned values.  May
0461      *                     be nullptr if destCapacity is 0.
0462      * @param destCapacity The capacity of the array, must be at least 0.
0463      * @param status       The error code.
0464      * @return             The count of values written.
0465      *                     If more than destCapacity samples are available, then
0466      *                     only destCapacity are written, and destCapacity is returned as the count,
0467      *                     rather than setting a U_BUFFER_OVERFLOW_ERROR.
0468      *                     (The actual number of keyword values could be unlimited.)
0469      * @stable ICU 4.8
0470      */
0471     int32_t getSamples(const UnicodeString &keyword,
0472                        double *dest, int32_t destCapacity,
0473                        UErrorCode& status);
0474 
0475 #ifndef U_HIDE_INTERNAL_API
0476     /**
0477      * Internal-only function that returns DecimalQuantitys instead of doubles.
0478      *
0479      * Returns sample values for which select() would return the keyword.  If
0480      * the keyword is unknown, returns no values, but this is not an error.
0481      *
0482      * The number of returned values is typically small.
0483      *
0484      * @param keyword      The keyword.
0485      * @param dest         Array into which to put the returned values.  May
0486      *                     be nullptr if destCapacity is 0.
0487      * @param destCapacity The capacity of the array, must be at least 0.
0488      * @param status       The error code.
0489      * @return             The count of values written.
0490      *                     If more than destCapacity samples are available, then
0491      *                     only destCapacity are written, and destCapacity is returned as the count,
0492      *                     rather than setting a U_BUFFER_OVERFLOW_ERROR.
0493      *                     (The actual number of keyword values could be unlimited.)
0494      * @internal
0495      */
0496     int32_t getSamples(const UnicodeString &keyword,
0497                        DecimalQuantity *dest, int32_t destCapacity,
0498                        UErrorCode& status);
0499 #endif  /* U_HIDE_INTERNAL_API */
0500 
0501     /**
0502      * Returns true if the given keyword is defined in this
0503      * <code>PluralRules</code> object.
0504      *
0505      * @param keyword  the input keyword.
0506      * @return         true if the input keyword is defined.
0507      *                 Otherwise, return false.
0508      * @stable ICU 4.0
0509      */
0510     UBool isKeyword(const UnicodeString& keyword) const;
0511 
0512 
0513     /**
0514      * Returns keyword for default plural form.
0515      *
0516      * @return         keyword for default plural form.
0517      * @stable ICU 4.0
0518      */
0519     UnicodeString getKeywordOther() const;
0520 
0521 #ifndef U_HIDE_INTERNAL_API
0522     /**
0523      *
0524      * @internal
0525      */
0526      UnicodeString getRules() const;
0527 #endif  /* U_HIDE_INTERNAL_API */
0528 
0529     /**
0530      * Compares the equality of two PluralRules objects.
0531      *
0532      * @param other The other PluralRules object to be compared with.
0533      * @return      true if the given PluralRules is the same as this
0534      *              PluralRules; false otherwise.
0535      * @stable ICU 4.0
0536      */
0537     virtual bool operator==(const PluralRules& other) const;
0538 
0539     /**
0540      * Compares the inequality of two PluralRules objects.
0541      *
0542      * @param other The PluralRules object to be compared with.
0543      * @return      true if the given PluralRules is not the same as this
0544      *              PluralRules; false otherwise.
0545      * @stable ICU 4.0
0546      */
0547     bool operator!=(const PluralRules& other) const  {return !operator==(other);}
0548 
0549 
0550     /**
0551      * ICU "poor man's RTTI", returns a UClassID for this class.
0552      *
0553      * @stable ICU 4.0
0554      *
0555     */
0556     static UClassID U_EXPORT2 getStaticClassID(void);
0557 
0558     /**
0559      * ICU "poor man's RTTI", returns a UClassID for the actual class.
0560      *
0561      * @stable ICU 4.0
0562      */
0563     virtual UClassID getDynamicClassID() const override;
0564 
0565 
0566 private:
0567     RuleChain  *mRules;
0568     StandardPluralRanges *mStandardPluralRanges;
0569 
0570     PluralRules() = delete;   // default constructor not implemented
0571     UnicodeString   getRuleFromResource(const Locale& locale, UPluralType type, UErrorCode& status);
0572     RuleChain      *rulesForKeyword(const UnicodeString &keyword) const;
0573     PluralRules    *clone(UErrorCode& status) const;
0574 
0575     /**
0576     * An internal status variable used to indicate that the object is in an 'invalid' state.
0577     * Used by copy constructor, the assignment operator and the clone method.
0578     */
0579     UErrorCode mInternalStatus;
0580 
0581     friend class PluralRuleParser;
0582 };
0583 
0584 U_NAMESPACE_END
0585 
0586 #endif /* #if !UCONFIG_NO_FORMATTING */
0587 
0588 #endif /* U_SHOW_CPLUSPLUS_API */
0589 
0590 #endif // _PLURRULE
0591 //eof