1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4  *******************************************************************************
5  * Copyright (C) 2015, International Business Machines Corporation
6  * and others. All Rights Reserved.
7  *******************************************************************************
8  * standardplural.h
9  *
10  * created on: 2015dec14
11  * created by: Markus W. Scherer
12  */
13 
14 #ifndef __STANDARDPLURAL_H__
15 #define __STANDARDPLURAL_H__
16 
17 #include "unicode/utypes.h"
18 
19 #if !UCONFIG_NO_FORMATTING
20 
21 U_NAMESPACE_BEGIN
22 
23 class UnicodeString;
24 
25 /**
26  * Standard CLDR plural form/category constants.
27  * See http://www.unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules
28  */
29 class U_I18N_API StandardPlural {
30 public:
31     enum Form {
32         ZERO,
33         ONE,
34         TWO,
35         FEW,
36         MANY,
37         OTHER,
38         EQ_0,
39         EQ_1,
40         COUNT
41     };
42 
43     /**
44      * @return the lowercase CLDR keyword string for the plural form
45      */
46     static const char *getKeyword(Form p);
47 
48     /**
49      * @param keyword for example "few" or "other"
50      * @return the plural form corresponding to the keyword, or OTHER
51      */
orOtherFromString(const char * keyword)52     static Form orOtherFromString(const char *keyword) {
53         return static_cast<Form>(indexOrOtherIndexFromString(keyword));
54     }
55 
56     /**
57      * @param keyword for example "few" or "other"
58      * @return the plural form corresponding to the keyword, or OTHER
59      */
orOtherFromString(const UnicodeString & keyword)60     static Form orOtherFromString(const UnicodeString &keyword) {
61         return static_cast<Form>(indexOrOtherIndexFromString(keyword));
62     }
63 
64     /**
65      * Sets U_ILLEGAL_ARGUMENT_ERROR if the keyword is not a plural form.
66      *
67      * @param keyword for example "few" or "other"
68      * @return the plural form corresponding to the keyword
69      */
fromString(const char * keyword,UErrorCode & errorCode)70     static Form fromString(const char *keyword, UErrorCode &errorCode) {
71         return static_cast<Form>(indexFromString(keyword, errorCode));
72     }
73 
74     /**
75      * Sets U_ILLEGAL_ARGUMENT_ERROR if the keyword is not a plural form.
76      *
77      * @param keyword for example "few" or "other"
78      * @return the plural form corresponding to the keyword
79      */
fromString(const UnicodeString & keyword,UErrorCode & errorCode)80     static Form fromString(const UnicodeString &keyword, UErrorCode &errorCode) {
81         return static_cast<Form>(indexFromString(keyword, errorCode));
82     }
83 
84     /**
85      * @param keyword for example "few" or "other"
86      * @return the index of the plural form corresponding to the keyword, or a negative value
87      */
88     static int32_t indexOrNegativeFromString(const char *keyword);
89 
90     /**
91      * @param keyword for example "few" or "other"
92      * @return the index of the plural form corresponding to the keyword, or a negative value
93      */
94     static int32_t indexOrNegativeFromString(const UnicodeString &keyword);
95 
96     /**
97      * @param keyword for example "few" or "other"
98      * @return the index of the plural form corresponding to the keyword, or OTHER
99      */
indexOrOtherIndexFromString(const char * keyword)100     static int32_t indexOrOtherIndexFromString(const char *keyword) {
101         int32_t i = indexOrNegativeFromString(keyword);
102         return i >= 0 ? i : OTHER;
103     }
104 
105     /**
106      * @param keyword for example "few" or "other"
107      * @return the index of the plural form corresponding to the keyword, or OTHER
108      */
indexOrOtherIndexFromString(const UnicodeString & keyword)109     static int32_t indexOrOtherIndexFromString(const UnicodeString &keyword) {
110         int32_t i = indexOrNegativeFromString(keyword);
111         return i >= 0 ? i : OTHER;
112     }
113 
114     /**
115      * Sets U_ILLEGAL_ARGUMENT_ERROR if the keyword is not a plural form.
116      *
117      * @param keyword for example "few" or "other"
118      * @return the index of the plural form corresponding to the keyword
119      */
120     static int32_t indexFromString(const char *keyword, UErrorCode &errorCode);
121 
122     /**
123      * Sets U_ILLEGAL_ARGUMENT_ERROR if the keyword is not a plural form.
124      *
125      * @param keyword for example "few" or "other"
126      * @return the index of the plural form corresponding to the keyword
127      */
128     static int32_t indexFromString(const UnicodeString &keyword, UErrorCode &errorCode);
129 };
130 
131 U_NAMESPACE_END
132 
133 #endif  // !UCONFIG_NO_FORMATTING
134 #endif  // __STANDARDPLURAL_H__
135