• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 U_NAMESPACE_BEGIN
22 
23 static const char *gKeywords[StandardPlural::COUNT] = {
24     "zero", "one", "two", "few", "many", "other"
25 };
26 
getKeyword(Form p)27 const char *StandardPlural::getKeyword(Form p) {
28     U_ASSERT(ZERO <= p && p < COUNT);
29     return gKeywords[p];
30 }
31 
indexOrNegativeFromString(const char * keyword)32 int32_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 
67 static const UChar gZero[] = { 0x7A, 0x65, 0x72, 0x6F };
68 static const UChar gOne[] = { 0x6F, 0x6E, 0x65 };
69 static const UChar gTwo[] = { 0x74, 0x77, 0x6F };
70 static const UChar gFew[] = { 0x66, 0x65, 0x77 };
71 static const UChar gMany[] = { 0x6D, 0x61, 0x6E, 0x79 };
72 static const UChar gOther[] = { 0x6F, 0x74, 0x68, 0x65, 0x72 };
73 
indexOrNegativeFromString(const UnicodeString & keyword)74 int32_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 
indexFromString(const char * keyword,UErrorCode & errorCode)103 int32_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 
indexFromString(const UnicodeString & keyword,UErrorCode & errorCode)114 int32_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 
125 U_NAMESPACE_END
126 
127 #endif  // !UCONFIG_NO_FORMATTING
128