/src/mozilla-central/intl/icu/source/i18n/standardplural.cpp
Line | Count | Source (jump to first uncovered line) |
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.cpp |
9 | | * |
10 | | * created on: 2015dec14 |
11 | | * created by: Markus W. Scherer |
12 | | */ |
13 | | |
14 | | #include "unicode/utypes.h" |
15 | | |
16 | | #if !UCONFIG_NO_FORMATTING |
17 | | |
18 | | #include "unicode/unistr.h" |
19 | | #include "cstring.h" |
20 | | #include "standardplural.h" |
21 | | #include "uassert.h" |
22 | | |
23 | | U_NAMESPACE_BEGIN |
24 | | |
25 | | static const char *gKeywords[StandardPlural::COUNT] = { |
26 | | "zero", "one", "two", "few", "many", "other" |
27 | | }; |
28 | | |
29 | 0 | const char *StandardPlural::getKeyword(Form p) { |
30 | 0 | U_ASSERT(ZERO <= p && p < COUNT); |
31 | 0 | return gKeywords[p]; |
32 | 0 | } |
33 | | |
34 | 0 | int32_t StandardPlural::indexOrNegativeFromString(const char *keyword) { |
35 | 0 | switch (*keyword++) { |
36 | 0 | case 'f': |
37 | 0 | if (uprv_strcmp(keyword, "ew") == 0) { |
38 | 0 | return FEW; |
39 | 0 | } |
40 | 0 | break; |
41 | 0 | case 'm': |
42 | 0 | if (uprv_strcmp(keyword, "any") == 0) { |
43 | 0 | return MANY; |
44 | 0 | } |
45 | 0 | break; |
46 | 0 | case 'o': |
47 | 0 | if (uprv_strcmp(keyword, "ther") == 0) { |
48 | 0 | return OTHER; |
49 | 0 | } else if (uprv_strcmp(keyword, "ne") == 0) { |
50 | 0 | return ONE; |
51 | 0 | } |
52 | 0 | break; |
53 | 0 | case 't': |
54 | 0 | if (uprv_strcmp(keyword, "wo") == 0) { |
55 | 0 | return TWO; |
56 | 0 | } |
57 | 0 | break; |
58 | 0 | case 'z': |
59 | 0 | if (uprv_strcmp(keyword, "ero") == 0) { |
60 | 0 | return ZERO; |
61 | 0 | } |
62 | 0 | break; |
63 | 0 | default: |
64 | 0 | break; |
65 | 0 | } |
66 | 0 | return -1; |
67 | 0 | } |
68 | | |
69 | | static const UChar gZero[] = { 0x7A, 0x65, 0x72, 0x6F }; |
70 | | static const UChar gOne[] = { 0x6F, 0x6E, 0x65 }; |
71 | | static const UChar gTwo[] = { 0x74, 0x77, 0x6F }; |
72 | | static const UChar gFew[] = { 0x66, 0x65, 0x77 }; |
73 | | static const UChar gMany[] = { 0x6D, 0x61, 0x6E, 0x79 }; |
74 | | static const UChar gOther[] = { 0x6F, 0x74, 0x68, 0x65, 0x72 }; |
75 | | |
76 | 0 | int32_t StandardPlural::indexOrNegativeFromString(const UnicodeString &keyword) { |
77 | 0 | switch (keyword.length()) { |
78 | 0 | case 3: |
79 | 0 | if (keyword.compare(gOne, 3) == 0) { |
80 | 0 | return ONE; |
81 | 0 | } else if (keyword.compare(gTwo, 3) == 0) { |
82 | 0 | return TWO; |
83 | 0 | } else if (keyword.compare(gFew, 3) == 0) { |
84 | 0 | return FEW; |
85 | 0 | } |
86 | 0 | break; |
87 | 0 | case 4: |
88 | 0 | if (keyword.compare(gMany, 4) == 0) { |
89 | 0 | return MANY; |
90 | 0 | } else if (keyword.compare(gZero, 4) == 0) { |
91 | 0 | return ZERO; |
92 | 0 | } |
93 | 0 | break; |
94 | 0 | case 5: |
95 | 0 | if (keyword.compare(gOther, 5) == 0) { |
96 | 0 | return OTHER; |
97 | 0 | } |
98 | 0 | break; |
99 | 0 | default: |
100 | 0 | break; |
101 | 0 | } |
102 | 0 | return -1; |
103 | 0 | } |
104 | | |
105 | 0 | int32_t StandardPlural::indexFromString(const char *keyword, UErrorCode &errorCode) { |
106 | 0 | if (U_FAILURE(errorCode)) { return OTHER; } |
107 | 0 | int32_t i = indexOrNegativeFromString(keyword); |
108 | 0 | if (i >= 0) { |
109 | 0 | return i; |
110 | 0 | } else { |
111 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
112 | 0 | return OTHER; |
113 | 0 | } |
114 | 0 | } |
115 | | |
116 | 0 | int32_t StandardPlural::indexFromString(const UnicodeString &keyword, UErrorCode &errorCode) { |
117 | 0 | if (U_FAILURE(errorCode)) { return OTHER; } |
118 | 0 | int32_t i = indexOrNegativeFromString(keyword); |
119 | 0 | if (i >= 0) { |
120 | 0 | return i; |
121 | 0 | } else { |
122 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
123 | 0 | return OTHER; |
124 | 0 | } |
125 | 0 | } |
126 | | |
127 | | U_NAMESPACE_END |
128 | | |
129 | | #endif // !UCONFIG_NO_FORMATTING |