]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/i18n/unicode/plurrule.h
ICU-511.35.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / plurrule.h
index 7c05fecca9ef7315f665e5b39c82bff4c326cbff..cc29754fc664067589a168bdc2d5dab3efed10e4 100644 (file)
@@ -1,6 +1,6 @@
 /*
 *******************************************************************************
-* Copyright (C) 2008-2010, International Business Machines Corporation and
+* Copyright (C) 2008-2013, International Business Machines Corporation and
 * others. All Rights Reserved.
 *******************************************************************************
 *
 #if !UCONFIG_NO_FORMATTING
 
 #include "unicode/format.h"
+#include "unicode/upluralrules.h"
+
+/**
+ * Value returned by PluralRules::getUniqueKeywordValue() when there is no
+ * unique value to return.
+ * @stable ICU 4.8
+ */
+#define UPLRULES_NO_UNIQUE_VALUE ((double)-0.00123456777)
 
 U_NAMESPACE_BEGIN
 
 class Hashtable;
 class RuleChain;
 class RuleParser;
+class PluralKeywordEnumeration;
 
 /**
- * Defines rules for mapping positive long values onto a small set of
+ * Defines rules for mapping non-negative numeric values onto a small set of
  * keywords. Rules are constructed from a text description, consisting
  * of a series of keywords and conditions.  The {@link #select} method
  * examines each condition in order and returns the keyword for the
  * first condition that matches the number.  If none match,
  * default rule(other) is returned.
  *
+ * For more information, details, and tips for writing rules, see the
+ * LDML spec, C.11 Language Plural Rules:
+ * http://www.unicode.org/draft/reports/tr35/tr35.html#Language_Plural_Rules
+ *
  * Examples:<pre>
  *   "one: n is 1; few: n in 2..4"</pre>
  *  This defines two rules, for 'one' and 'few'.  The condition for
@@ -74,24 +87,28 @@ class RuleParser;
  * and_condition = relation ('and' relation)*
  * relation      = is_relation | in_relation | within_relation | 'n' <EOL>
  * is_relation   = expr 'is' ('not')? value
- * in_relation   = expr ('not')? 'in' range
+ * in_relation   = expr ('not')? 'in' range_list
  * within_relation = expr ('not')? 'within' range
  * expr          = 'n' ('mod' value)?
+ * range_list    = (range | value) (',' range_list)*
  * value         = digit+
  * digit         = 0|1|2|3|4|5|6|7|8|9
  * range         = value'..'value
  * \endcode
  * </pre></p>
  * <p>
- *  The difference between 'in' and 'within' is that 'in' only includes
- *  integers in the specified range, while 'within' includes all values.</p>
- *  <p>
- *  Keywords
- *  could be defined by users or from ICU locale data. There are 6
- *  predefined values in ICU - 'zero', 'one', 'two', 'few', 'many' and
- *  'other'. Callers need to check the value of keyword returned by
- *  {@link #select} method.
- *  </p>
+ * An "identifier" is a sequence of characters that do not have the
+ * Unicode Pattern_Syntax or Pattern_White_Space properties.
+ * <p>
+ * The difference between 'in' and 'within' is that 'in' only includes
+ * integers in the specified range, while 'within' includes all values.</p>
+ * <p>
+ * Keywords
+ * could be defined by users or from ICU locale data. There are 6
+ * predefined values in ICU - 'zero', 'one', 'two', 'few', 'many' and
+ * 'other'. Callers need to check the value of keyword returned by
+ * {@link #select} method.
+ * </p>
  *
  * Examples:<pre>
  * UnicodeString keyword = pl->select(number);
@@ -103,7 +120,7 @@ class RuleParser;
  * <strong>Note:</strong><br>
  *  <p>
  *   ICU defines plural rules for many locales based on CLDR <i>Language Plural Rules</i>.
- *   For these predefined rules, see CLDR page at 
+ *   For these predefined rules, see CLDR page at
  *    http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
  * </p>
  */
@@ -145,7 +162,7 @@ public:
 
     /**
      * Creates a PluralRules from a description if it is parsable, otherwise
-     * returns null.
+     * returns NULL.
      *
      * @param description rule description
      * @param status      Output param set to success/failure code on exit, which
@@ -167,8 +184,9 @@ public:
     static PluralRules* U_EXPORT2 createDefaultRules(UErrorCode& status);
 
     /**
-     * Provides access to the predefined <code>PluralRules</code> for a given
+     * Provides access to the predefined cardinal-number <code>PluralRules</code> for a given
      * locale.
+     * Same as forLocale(locale, UPLURAL_TYPE_CARDINAL, status).
      *
      * @param locale  The locale for which a <code>PluralRules</code> object is
      *                returned.
@@ -182,7 +200,27 @@ public:
      * @stable ICU 4.0
      */
     static PluralRules* U_EXPORT2 forLocale(const Locale& locale, UErrorCode& status);
-    
+
+#ifndef U_HIDE_DRAFT_API
+    /**
+     * Provides access to the predefined <code>PluralRules</code> for a given
+     * locale and the plural type.
+     *
+     * @param locale  The locale for which a <code>PluralRules</code> object is
+     *                returned.
+     * @param type    The plural type (e.g., cardinal or ordinal).
+     * @param status  Output param set to success/failure code on exit, which
+     *                must not indicate a failure before the function call.
+     * @return        The predefined <code>PluralRules</code> object pointer for
+     *                this locale. If there's no predefined rules for this locale,
+     *                the rules for the closest parent in the locale hierarchy
+     *                that has one will  be returned.  The final fallback always
+     *                returns the default 'other' rules.
+     * @draft ICU 50
+     */
+    static PluralRules* U_EXPORT2 forLocale(const Locale& locale, UPluralType type, UErrorCode& status);
+#endif /* U_HIDE_DRAFT_API */
+
     /**
      * Given a number, returns the keyword of the first rule that applies to
      * the number.  This function can be used with isKeyword* functions to
@@ -193,7 +231,7 @@ public:
      * @stable ICU 4.0
      */
     UnicodeString select(int32_t number) const;
-    
+
     /**
      * Given a number, returns the keyword of the first rule that applies to
      * the number.  This function can be used with isKeyword* functions to
@@ -217,6 +255,62 @@ public:
      */
     StringEnumeration* getKeywords(UErrorCode& status) const;
 
+    /**
+     * Returns a unique value for this keyword if it exists, else the constant
+     * UPLRULES_NO_UNIQUE_VALUE.
+     *
+     * @param keyword The keyword.
+     * @return        The unique value that generates the keyword, or
+     *                UPLRULES_NO_UNIQUE_VALUE if the keyword is undefined or there is no
+     *                unique value that generates this keyword.
+     * @stable ICU 4.8
+     */
+    double getUniqueKeywordValue(const UnicodeString& keyword);
+
+    /**
+     * Returns all the values for which select() would return the keyword.  If
+     * the keyword is unknown, returns no values, but this is not an error.  If
+     * the number of values is unlimited, returns no values and -1 as the
+     * count.
+     *
+     * The number of returned values is typically small.
+     *
+     * @param keyword      The keyword.
+     * @param dest         Array into which to put the returned values.  May
+     *                     be NULL if destCapacity is 0.
+     * @param destCapacity The capacity of the array, must be at least 0.
+     * @param status       The error code.
+     * @return             The count of values available, or -1.  This count
+     *                     can be larger than destCapacity, but no more than
+     *                     destCapacity values will be written.
+     * @stable ICU 4.8
+     */
+    int32_t getAllKeywordValues(const UnicodeString &keyword,
+                                double *dest, int32_t destCapacity,
+                                UErrorCode& status);
+
+    /**
+     * Returns sample values for which select() would return the keyword.  If
+     * the keyword is unknown, returns no values, but this is not an error.
+     *
+     * The number of returned values is typically small.
+     *
+     * @param keyword      The keyword.
+     * @param dest         Array into which to put the returned values.  May
+     *                     be NULL if destCapacity is 0.
+     * @param destCapacity The capacity of the array, must be at least 0.
+     * @param status       The error code.
+     * @return             The count of values written.
+     *                     If more than destCapacity samples are available, then
+     *                     only destCapacity are written, and destCapacity is returned as the count,
+     *                     rather than setting a U_BUFFER_OVERFLOW_ERROR.
+     *                     (The actual number of keyword values could be unlimited.)
+     * @stable ICU 4.8
+     */
+    int32_t getSamples(const UnicodeString &keyword,
+                       double *dest, int32_t destCapacity,
+                       UErrorCode& status);
+
     /**
      * Returns TRUE if the given keyword is defined in this
      * <code>PluralRules</code> object.
@@ -233,7 +327,6 @@ public:
      * Returns keyword for default plural form.
      *
      * @return         keyword for default plural form.
-     * @internal 4.0
      * @stable ICU 4.0
      */
     UnicodeString getKeywordOther() const;
@@ -276,10 +369,11 @@ public:
 
 
 private:
-    Hashtable       *fLocaleStringsHash;
-    UnicodeString   mLocaleName;
-    RuleChain       *mRules;
-    RuleParser      *mParser;
+    RuleChain  *mRules;
+    RuleParser *mParser;
+    double     *mSamples;
+    int32_t    *mSampleInfo;
+    int32_t    mSampleInfoCount;
 
     PluralRules();   // default constructor not implemented
     int32_t getRepeatLimit() const;
@@ -287,7 +381,16 @@ private:
     void getNextLocale(const UnicodeString& localeData, int32_t* curIndex, UnicodeString& localeName);
     void addRules(RuleChain& rules);
     int32_t getNumberValue(const UnicodeString& token) const;
-    UnicodeString getRuleFromResource(const Locale& locale, UErrorCode& status);
+    UnicodeString getRuleFromResource(const Locale& locale, UPluralType type, UErrorCode& status);
+
+    static const int32_t MAX_SAMPLES = 3;
+
+    int32_t getSamplesInternal(const UnicodeString &keyword, double *dest,
+                               int32_t destCapacity, UBool includeUnlimited,
+                               UErrorCode& status);
+    int32_t getKeywordIndex(const UnicodeString& keyword,
+                            UErrorCode& status) const;
+    void initSamples(UErrorCode& status);
 
 };