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