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
21U_NAMESPACE_BEGIN
22
23class 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 */
29class U_I18N_API StandardPlural {
30public:
31 enum Form {
32 ZERO,
33 ONE,
34 TWO,
35 FEW,
36 MANY,
37 OTHER,
38 COUNT
39 };
40
41 /**
42 * @return the lowercase CLDR keyword string for the plural form
43 */
44 static const char *getKeyword(Form p);
45
46 /**
47 * @param keyword for example "few" or "other"
48 * @return the plural form corresponding to the keyword, or OTHER
49 */
50 static Form orOtherFromString(const char *keyword) {
51 return static_cast<Form>(indexOrOtherIndexFromString(keyword));
52 }
53
54 /**
55 * @param keyword for example "few" or "other"
56 * @return the plural form corresponding to the keyword, or OTHER
57 */
58 static Form orOtherFromString(const UnicodeString &keyword) {
59 return static_cast<Form>(indexOrOtherIndexFromString(keyword));
60 }
61
62 /**
63 * Sets U_ILLEGAL_ARGUMENT_ERROR if the keyword is not a plural form.
64 *
65 * @param keyword for example "few" or "other"
66 * @return the plural form corresponding to the keyword
67 */
68 static Form fromString(const char *keyword, UErrorCode &errorCode) {
69 return static_cast<Form>(indexFromString(keyword, errorCode));
70 }
71
72 /**
73 * Sets U_ILLEGAL_ARGUMENT_ERROR if the keyword is not a plural form.
74 *
75 * @param keyword for example "few" or "other"
76 * @return the plural form corresponding to the keyword
77 */
78 static Form fromString(const UnicodeString &keyword, UErrorCode &errorCode) {
79 return static_cast<Form>(indexFromString(keyword, errorCode));
80 }
81
82 /**
83 * @param keyword for example "few" or "other"
84 * @return the index of the plural form corresponding to the keyword, or a negative value
85 */
86 static int32_t indexOrNegativeFromString(const char *keyword);
87
88 /**
89 * @param keyword for example "few" or "other"
90 * @return the index of the plural form corresponding to the keyword, or a negative value
91 */
92 static int32_t indexOrNegativeFromString(const UnicodeString &keyword);
93
94 /**
95 * @param keyword for example "few" or "other"
96 * @return the index of the plural form corresponding to the keyword, or OTHER
97 */
98 static int32_t indexOrOtherIndexFromString(const char *keyword) {
99 int32_t i = indexOrNegativeFromString(keyword);
100 return i >= 0 ? i : OTHER;
101 }
102
103 /**
104 * @param keyword for example "few" or "other"
105 * @return the index of the plural form corresponding to the keyword, or OTHER
106 */
107 static int32_t indexOrOtherIndexFromString(const UnicodeString &keyword) {
108 int32_t i = indexOrNegativeFromString(keyword);
109 return i >= 0 ? i : OTHER;
110 }
111
112 /**
113 * Sets U_ILLEGAL_ARGUMENT_ERROR if the keyword is not a plural form.
114 *
115 * @param keyword for example "few" or "other"
116 * @return the index of the plural form corresponding to the keyword
117 */
118 static int32_t indexFromString(const char *keyword, UErrorCode &errorCode);
119
120 /**
121 * Sets U_ILLEGAL_ARGUMENT_ERROR if the keyword is not a plural form.
122 *
123 * @param keyword for example "few" or "other"
124 * @return the index of the plural form corresponding to the keyword
125 */
126 static int32_t indexFromString(const UnicodeString &keyword, UErrorCode &errorCode);
127};
128
129U_NAMESPACE_END
130
131#endif // !UCONFIG_NO_FORMATTING
132#endif // __STANDARDPLURAL_H__
133