]> granicus.if.org Git - icu/commitdiff
ICU-12031 StandardPlural from UnicodeString
authorMarkus Scherer <markus.icu@gmail.com>
Wed, 16 Dec 2015 04:21:28 +0000 (04:21 +0000)
committerMarkus Scherer <markus.icu@gmail.com>
Wed, 16 Dec 2015 04:21:28 +0000 (04:21 +0000)
X-SVN-Rev: 38134

icu4c/source/i18n/standardplural.cpp
icu4c/source/i18n/standardplural.h

index 651a9bcc3ae97cf25a8604e1777d9a40e9c77aa6..34127a42c3cc3470a96cf84417b7a3f7d87214c1 100644 (file)
@@ -13,6 +13,7 @@
 
 #if !UCONFIG_NO_FORMATTING
 
+#include "unicode/unistr.h"
 #include "cstring.h"
 #include "standardplural.h"
 #include "uassert.h"
@@ -63,6 +64,42 @@ int32_t StandardPlural::indexOrNegativeFromString(const char *keyword) {
     return -1;
 }
 
+static const UChar gZero[] = { 0x7A, 0x65, 0x72, 0x6F };
+static const UChar gOne[] = { 0x6F, 0x6E, 0x65 };
+static const UChar gTwo[] = { 0x74, 0x77, 0x6F };
+static const UChar gFew[] = { 0x66, 0x65, 0x77 };
+static const UChar gMany[] = { 0x6D, 0x61, 0x6E, 0x79 };
+static const UChar gOther[] = { 0x6F, 0x74, 0x68, 0x65, 0x72 };
+
+int32_t StandardPlural::indexOrNegativeFromString(const UnicodeString &keyword) {
+    switch (keyword.length()) {
+    case 3:
+        if (keyword.compare(gOne, 3) == 0) {
+            return ONE;
+        } else if (keyword.compare(gTwo, 3) == 0) {
+            return TWO;
+        } else if (keyword.compare(gFew, 3) == 0) {
+            return FEW;
+        }
+        break;
+    case 4:
+        if (keyword.compare(gMany, 4) == 0) {
+            return MANY;
+        } else if (keyword.compare(gZero, 4) == 0) {
+            return ZERO;
+        }
+        break;
+    case 5:
+        if (keyword.compare(gOther, 5) == 0) {
+            return OTHER;
+        }
+        break;
+    default:
+        break;
+    }
+    return -1;
+}
+
 int32_t StandardPlural::indexFromString(const char *keyword, UErrorCode &errorCode) {
     if (U_FAILURE(errorCode)) { return OTHER; }
     int32_t i = indexOrNegativeFromString(keyword);
index 174f2a4a4aa974a343d2aecbfd1fbcb4d318301b..8a8de2188485ecc09bd9d9445ac650573175ba82 100644 (file)
@@ -18,6 +18,8 @@
 
 U_NAMESPACE_BEGIN
 
+class UnicodeString;
+
 /**
  * Standard CLDR plural form/category constants.
  * See http://www.unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules
@@ -47,6 +49,14 @@ public:
         return static_cast<Form>(indexOrOtherIndexFromString(keyword));
     }
 
+    /**
+     * @param keyword for example "few" or "other"
+     * @return the plural form corresponding to the keyword, or OTHER
+     */
+    static Form orOtherFromString(const UnicodeString &keyword) {
+        return static_cast<Form>(indexOrOtherIndexFromString(keyword));
+    }
+
     /**
      * Sets U_ILLEGAL_ARGUMENT_ERROR if the keyword is not a plural form.
      *
@@ -57,6 +67,16 @@ public:
         return static_cast<Form>(indexFromString(keyword, errorCode));
     }
 
+    /**
+     * Sets U_ILLEGAL_ARGUMENT_ERROR if the keyword is not a plural form.
+     *
+     * @param keyword for example "few" or "other"
+     * @return the plural form corresponding to the keyword
+     */
+    static Form fromString(const UnicodeString &keyword, UErrorCode &errorCode) {
+        return static_cast<Form>(indexFromString(keyword, errorCode));
+    }
+
     /**
      * @param keyword for example "few" or "other"
      * @return the index of the plural form corresponding to the keyword, or a negative value
@@ -65,13 +85,28 @@ public:
 
     /**
      * @param keyword for example "few" or "other"
-     * @return the index of the plural form corresponding to the keyword, or OTHER_INDEX
+     * @return the index of the plural form corresponding to the keyword, or a negative value
+     */
+    static int32_t indexOrNegativeFromString(const UnicodeString &keyword);
+
+    /**
+     * @param keyword for example "few" or "other"
+     * @return the index of the plural form corresponding to the keyword, or OTHER
      */
     static int32_t indexOrOtherIndexFromString(const char *keyword) {
         int32_t i = indexOrNegativeFromString(keyword);
         return i >= 0 ? i : OTHER;
     }
 
+    /**
+     * @param keyword for example "few" or "other"
+     * @return the index of the plural form corresponding to the keyword, or OTHER
+     */
+    static int32_t indexOrOtherIndexFromString(const UnicodeString &keyword) {
+        int32_t i = indexOrNegativeFromString(keyword);
+        return i >= 0 ? i : OTHER;
+    }
+
     /**
      * Sets U_ILLEGAL_ARGUMENT_ERROR if the keyword is not a plural form.
      *
@@ -79,6 +114,14 @@ public:
      * @return the index of the plural form corresponding to the keyword
      */
     static int32_t indexFromString(const char *keyword, UErrorCode &errorCode);
+
+    /**
+     * Sets U_ILLEGAL_ARGUMENT_ERROR if the keyword is not a plural form.
+     *
+     * @param keyword for example "few" or "other"
+     * @return the index of the plural form corresponding to the keyword
+     */
+    static int32_t indexFromString(const UnicodeString &keyword, UErrorCode &errorCode);
 };
 
 U_NAMESPACE_END