Logo Search packages:      
Sourcecode: icu version File versions

Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes

PluralRules Class Reference

#include <plurrule.h>

Inheritance diagram for PluralRules:
Inheritance graph
[legend]
Collaboration diagram for PluralRules:
Collaboration graph
[legend]

List of all members.

Public Member Functions

PluralRulesclone () const
int32_t getAllKeywordValues (const UnicodeString &keyword, double *dest, int32_t destCapacity, UErrorCode &status)
virtual UClassID getDynamicClassID () const
UnicodeString getKeywordOther () const
StringEnumerationgetKeywords (UErrorCode &status) const
int32_t getSamples (const UnicodeString &keyword, double *dest, int32_t destCapacity, UErrorCode &status)
double getUniqueKeywordValue (const UnicodeString &keyword)
UBool isKeyword (const UnicodeString &keyword) const
UBool operator!= (const PluralRules &other) const
PluralRulesoperator= (const PluralRules &)
virtual UBool operator== (const PluralRules &other) const
 PluralRules (const PluralRules &other)
 PluralRules (UErrorCode &status)
UnicodeString select (double number) const
UnicodeString select (int32_t number) const
virtual ~PluralRules ()

Static Public Member Functions

static PluralRules *U_EXPORT2 createDefaultRules (UErrorCode &status)
static PluralRules *U_EXPORT2 createRules (const UnicodeString &description, UErrorCode &status)
static PluralRules *U_EXPORT2 forLocale (const Locale &locale, UErrorCode &status)
static UClassID U_EXPORT2 getStaticClassID (void)
static void U_EXPORT2 operator delete (void *p) U_NO_THROW
static void U_EXPORT2 operator delete (void *, void *) U_NO_THROW
static void U_EXPORT2 operator delete[] (void *p) U_NO_THROW
static void *U_EXPORT2 operator new (size_t size) U_NO_THROW
static void *U_EXPORT2 operator new (size_t, void *ptr) U_NO_THROW
static void *U_EXPORT2 operator new[] (size_t size) U_NO_THROW

Private Member Functions

void addRules (RuleChain &rules)
int32_t getKeywordIndex (const UnicodeString &keyword, UErrorCode &status) const
void getNextLocale (const UnicodeString &localeData, int32_t *curIndex, UnicodeString &localeName)
int32_t getNumberValue (const UnicodeString &token) const
int32_t getRepeatLimit () const
UnicodeString getRuleFromResource (const Locale &locale, UErrorCode &status)
int32_t getSamplesInternal (const UnicodeString &keyword, double *dest, int32_t destCapacity, UBool includeUnlimited, UErrorCode &status)
void initSamples (UErrorCode &status)
void parseDescription (UnicodeString &ruleData, RuleChain &rules, UErrorCode &status)

Private Attributes

RuleParsermParser
RuleChainmRules
int32_t * mSampleInfo
int32_t mSampleInfoCount
double * mSamples

Static Private Attributes

static const int32_t MAX_SAMPLES = 3

Detailed Description

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 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:

   "one: n is 1; few: n in 2..4"

This defines two rules, for 'one' and 'few'. The condition for 'one' is "n is 1" which means that the number must be equal to 1 for this condition to pass. The condition for 'few' is "n in 2..4" which means that the number must be between 2 and 4 inclusive for this condition to pass. All other numbers are assigned the keyword "other" by the default rule.

    "zero: n is 0; one: n is 1; zero: n mod 100 in 1..19"

This illustrates that the same keyword can be defined multiple times. Each rule is examined in order, and the first keyword whose condition passes is the one returned. Also notes that a modulus is applied to n in the last rule. Thus its condition holds for 119, 219, 319...

    "one: n is 1; few: n mod 10 in 2..4 and n mod 100 not in 12..14"

This illustrates conjunction and negation. The condition for 'few' has two parts, both of which must be met: "n mod 10 in 2..4" and "n mod 100 not in 12..14". The first part applies a modulus to n before the test as in the previous example. The second part applies a different modulus and also uses negation, thus it matches all numbers _not_ in 12, 13, 14, 112, 113, 114, 212, 213, 214...

Syntax:

 
 rules         = rule (';' rule)*
 rule          = keyword ':' condition
 keyword       = <identifier>
 condition     = and_condition ('or' and_condition)*
 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_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

An "identifier" is a sequence of characters that do not have the Unicode Pattern_Syntax or Pattern_White_Space properties.

The difference between 'in' and 'within' is that 'in' only includes integers in the specified range, while 'within' includes all values.

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 select method.

Examples:

 UnicodeString keyword = pl->select(number);
 if (keyword== UnicodeString("one") {
     ...
 }
 else if ( ... )
 

Note:

ICU defines plural rules for many locales based on CLDR Language Plural Rules. For these predefined rules, see CLDR page at http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html

Definition at line 126 of file plurrule.h.


The documentation for this class was generated from the following files:

Generated by  Doxygen 1.6.0   Back to index