1 // Copyright (C) 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 const char *StandardPlural::getKeyword(Form p) { 30 U_ASSERT(ZERO <= p && p < COUNT); 31 return gKeywords[p]; 32 } 33 34 int32_t StandardPlural::indexOrNegativeFromString(const char *keyword) { 35 switch (*keyword++) { 36 case 'f': 37 if (uprv_strcmp(keyword, "ew") == 0) { 38 return FEW; 39 } 40 break; 41 case 'm': 42 if (uprv_strcmp(keyword, "any") == 0) { 43 return MANY; 44 } 45 break; 46 case 'o': 47 if (uprv_strcmp(keyword, "ther") == 0) { 48 return OTHER; 49 } else if (uprv_strcmp(keyword, "ne") == 0) { 50 return ONE; 51 } 52 break; 53 case 't': 54 if (uprv_strcmp(keyword, "wo") == 0) { 55 return TWO; 56 } 57 break; 58 case 'z': 59 if (uprv_strcmp(keyword, "ero") == 0) { 60 return ZERO; 61 } 62 break; 63 default: 64 break; 65 } 66 return -1; 67 } 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 int32_t StandardPlural::indexOrNegativeFromString(const UnicodeString &keyword) { 77 switch (keyword.length()) { 78 case 3: 79 if (keyword.compare(gOne, 3) == 0) { 80 return ONE; 81 } else if (keyword.compare(gTwo, 3) == 0) { 82 return TWO; 83 } else if (keyword.compare(gFew, 3) == 0) { 84 return FEW; 85 } 86 break; 87 case 4: 88 if (keyword.compare(gMany, 4) == 0) { 89 return MANY; 90 } else if (keyword.compare(gZero, 4) == 0) { 91 return ZERO; 92 } 93 break; 94 case 5: 95 if (keyword.compare(gOther, 5) == 0) { 96 return OTHER; 97 } 98 break; 99 default: 100 break; 101 } 102 return -1; 103 } 104 105 int32_t StandardPlural::indexFromString(const char *keyword, UErrorCode &errorCode) { 106 if (U_FAILURE(errorCode)) { return OTHER; } 107 int32_t i = indexOrNegativeFromString(keyword); 108 if (i >= 0) { 109 return i; 110 } else { 111 errorCode = U_ILLEGAL_ARGUMENT_ERROR; 112 return OTHER; 113 } 114 } 115 116 int32_t StandardPlural::indexFromString(const UnicodeString &keyword, UErrorCode &errorCode) { 117 if (U_FAILURE(errorCode)) { return OTHER; } 118 int32_t i = indexOrNegativeFromString(keyword); 119 if (i >= 0) { 120 return i; 121 } else { 122 errorCode = U_ILLEGAL_ARGUMENT_ERROR; 123 return OTHER; 124 } 125 } 126 127 U_NAMESPACE_END 128 129 #endif // !UCONFIG_NO_FORMATTING 130