]> granicus.if.org Git - icu/commitdiff
ICU-12031 port enum StandardPlural to C++
authorMarkus Scherer <markus.icu@gmail.com>
Mon, 14 Dec 2015 21:57:41 +0000 (21:57 +0000)
committerMarkus Scherer <markus.icu@gmail.com>
Mon, 14 Dec 2015 21:57:41 +0000 (21:57 +0000)
X-SVN-Rev: 38130

icu4c/source/i18n/Makefile.in
icu4c/source/i18n/i18n.vcxproj
icu4c/source/i18n/i18n.vcxproj.filters
icu4c/source/i18n/plurrule_impl.h
icu4c/source/i18n/quantityformatter.cpp
icu4c/source/i18n/quantityformatter.h
icu4c/source/i18n/standardplural.cpp [new file with mode: 0644]
icu4c/source/i18n/standardplural.h [new file with mode: 0644]

index 53dd5fdf60ef535e40173b1e19c808f33e94570e..b59b2cea1498b8eec17fd024800458fbbdcf49ae 100644 (file)
@@ -88,7 +88,7 @@ regexcmp.o rematch.o repattrn.o regexst.o regextxt.o regeximp.o uregex.o uregexc
 ulocdata.o measfmt.o currfmt.o curramt.o currunit.o measure.o utmscale.o \
 csdetect.o csmatch.o csr2022.o csrecog.o csrmbcs.o csrsbcs.o csrucode.o csrutf8.o inputext.o \
 wintzimpl.o windtfmt.o winnmfmt.o basictz.o dtrule.o rbtz.o tzrule.o tztrans.o vtzone.o zonemeta.o \
-upluralrules.o plurrule.o plurfmt.o selfmt.o dtitvfmt.o dtitvinf.o udateintervalformat.o \
+standardplural.o upluralrules.o plurrule.o plurfmt.o selfmt.o dtitvfmt.o dtitvinf.o udateintervalformat.o \
 tmunit.o tmutamt.o tmutfmt.o currpinf.o \
 uspoof.o uspoof_impl.o uspoof_build.o uspoof_conf.o uspoof_wsconf.o decfmtst.o smpdtfst.o \
 ztrans.o zrule.o vzone.o fphdlimp.o fpositer.o ufieldpositer.o locdspnm.o \
index 8c586a9cb35f7366b31017c52eb44e31ff8cc771..b036d95e329b2f479a0f8963537e7a37332b733f 100644 (file)
     <ClCompile Include="scriptset.cpp" />
     <ClCompile Include="smpdtfmt.cpp" />
     <ClCompile Include="smpdtfst.cpp" />
+    <ClCompile Include="standardplural.cpp" />
     <ClCompile Include="taiwncal.cpp" />
     <ClCompile Include="timezone.cpp" />
     <ClCompile Include="tmunit.cpp" />
       <Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">..\..\include\unicode\%(Filename)%(Extension);%(Outputs)</Outputs>
     </CustomBuild>
     <ClInclude Include="smpdtfst.h" />
+    <ClInclude Include="standardplural.h" />
     <ClInclude Include="taiwncal.h" />
     <CustomBuild Include="unicode\timezone.h">
       <Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">copy "%(FullPath)" ..\..\include\unicode
index 35c3fe2317ceaca125517b89c04d86d63ee74262..9164f5a3ccac28eef4d64de6352e61c58a1ab458 100644 (file)
     <ClCompile Include="smpdtfst.cpp">
       <Filter>formatting</Filter>
     </ClCompile>
+    <ClCompile Include="standardplural.cpp">
+      <Filter>formatting</Filter>
+    </ClCompile>
     <ClCompile Include="taiwncal.cpp">
       <Filter>formatting</Filter>
     </ClCompile>
     <ClInclude Include="smpdtfst.h">
       <Filter>formatting</Filter>
     </ClInclude>
+    <ClInclude Include="standardplural.h">
+      <Filter>formatting</Filter>
+    </ClInclude>
     <ClInclude Include="taiwncal.h">
       <Filter>formatting</Filter>
     </ClInclude>
index 7416fa7f48407e5d86ac6530be64cf9a04b225a7..785600bff54cc9d171fcc0dbb7d366613eb6f3cb 100644 (file)
 */
 
 
-#ifndef PLURRULE_IMPLE
-#define PLURRULE_IMPLE
+#ifndef PLURRULE_IMPL
+#define PLURRULE_IMPL
 
 // Internal definitions for the PluralRules implementation.
 
+#include "unicode/utypes.h"
+
 #if !UCONFIG_NO_FORMATTING
 
 #include "unicode/format.h"
 #include "unicode/locid.h"
 #include "unicode/parseerr.h"
 #include "unicode/ures.h"
-#include "unicode/utypes.h"
 #include "uvector.h"
 #include "hash.h"
 
index 901b3f7edb2b4a36de5523d24d6eeed618413416..53ec2a83aca416ca0afbecf99abe6ea006e54126 100644 (file)
 #include "unicode/fmtable.h"
 #include "unicode/fieldpos.h"
 #include "resource.h"
+#include "standardplural.h"
 #include "visibledigits.h"
 #include "uassert.h"
 
 U_NAMESPACE_BEGIN
 
-/**
- * Plural forms in index order: "other", "zero", "one", "two", "few", "many"
- * "other" must be first.
- */
-static int32_t getPluralIndex(const char *pluralForm) {
-    switch (*pluralForm++) {
-    case 'f':
-        if (uprv_strcmp(pluralForm, "ew") == 0) {
-            return 4;
-        }
-    case 'm':
-        if (uprv_strcmp(pluralForm, "any") == 0) {
-            return 5;
-        }
-    case 'o':
-        if (uprv_strcmp(pluralForm, "ther") == 0) {
-            return 0;
-        } else if (uprv_strcmp(pluralForm, "ne") == 0) {
-            return 2;
-        }
-        break;
-    case 't':
-        if (uprv_strcmp(pluralForm, "wo") == 0) {
-            return 3;
-        }
-    case 'z':
-        if (uprv_strcmp(pluralForm, "ero") == 0) {
-            return 1;
-        }
-    default:
-        break;
-    }
-    return -1;
-}
-
 QuantityFormatter::QuantityFormatter() {
     for (int32_t i = 0; i < UPRV_LENGTHOF(formatters); ++i) {
         formatters[i] = NULL;
@@ -111,14 +77,10 @@ UBool QuantityFormatter::addIfAbsent(
         const UnicodeString *rawPattern,
         const ResourceValue *patternValue,
         UErrorCode &status) {
+    int32_t pluralIndex = StandardPlural::indexFromString(variant, status);
     if (U_FAILURE(status)) {
         return FALSE;
     }
-    int32_t pluralIndex = getPluralIndex(variant);
-    if (pluralIndex < 0) {
-        status = U_ILLEGAL_ARGUMENT_ERROR;
-        return FALSE;
-    }
     if (formatters[pluralIndex] != NULL) {
         return TRUE;
     }
@@ -139,19 +101,16 @@ UBool QuantityFormatter::addIfAbsent(
 }
 
 UBool QuantityFormatter::isValid() const {
-    return formatters[0] != NULL;
+    return formatters[StandardPlural::OTHER] != NULL;
 }
 
 const SimplePatternFormatter *QuantityFormatter::getByVariant(
         const char *variant) const {
     U_ASSERT(isValid());
-    int32_t pluralIndex = getPluralIndex(variant);
-    if (pluralIndex == -1) {
-        pluralIndex = 0;
-    }
+    int32_t pluralIndex = StandardPlural::indexOrOtherIndexFromString(variant);
     const SimplePatternFormatter *pattern = formatters[pluralIndex];
     if (pattern == NULL) {
-        pattern = formatters[0];
+        pattern = formatters[StandardPlural::OTHER];
     }
     return pattern;
 }
index 97bf91c40bfd6d90a51bb375e9ec84e21851b078..cbc7fd3ea38b29ba019e49f8da1f4c6547901a12 100644 (file)
@@ -15,6 +15,7 @@
 #if !UCONFIG_NO_FORMATTING
 
 #include "resource.h"
+#include "standardplural.h"
 
 U_NAMESPACE_BEGIN
 
@@ -130,7 +131,7 @@ private:
             const ResourceValue *patternValue,
             UErrorCode &status);
 
-    SimplePatternFormatter *formatters[6];
+    SimplePatternFormatter *formatters[StandardPlural::COUNT];
 };
 
 U_NAMESPACE_END
diff --git a/icu4c/source/i18n/standardplural.cpp b/icu4c/source/i18n/standardplural.cpp
new file mode 100644 (file)
index 0000000..651a9bc
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ *******************************************************************************
+ * Copyright (C) 2015, International Business Machines Corporation
+ * and others. All Rights Reserved.
+ *******************************************************************************
+ * standardplural.cpp
+ *
+ * created on: 2015dec14
+ * created by: Markus W. Scherer
+ */
+
+#include "unicode/utypes.h"
+
+#if !UCONFIG_NO_FORMATTING
+
+#include "cstring.h"
+#include "standardplural.h"
+#include "uassert.h"
+
+U_NAMESPACE_BEGIN
+
+static const char *gKeywords[StandardPlural::COUNT] = {
+    "zero", "one", "two", "few", "many", "other"
+};
+
+const char *StandardPlural::getKeyword(Form p) {
+    U_ASSERT(ZERO <= p && p < COUNT);
+    return gKeywords[p];
+}
+
+int32_t StandardPlural::indexOrNegativeFromString(const char *keyword) {
+    switch (*keyword++) {
+    case 'f':
+        if (uprv_strcmp(keyword, "ew") == 0) {
+            return FEW;
+        }
+        break;
+    case 'm':
+        if (uprv_strcmp(keyword, "any") == 0) {
+            return MANY;
+        }
+        break;
+    case 'o':
+        if (uprv_strcmp(keyword, "ther") == 0) {
+            return OTHER;
+        } else if (uprv_strcmp(keyword, "ne") == 0) {
+            return ONE;
+        }
+        break;
+    case 't':
+        if (uprv_strcmp(keyword, "wo") == 0) {
+            return TWO;
+        }
+        break;
+    case 'z':
+        if (uprv_strcmp(keyword, "ero") == 0) {
+            return ZERO;
+        }
+        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);
+    if (i >= 0) {
+        return i;
+    } else {
+        errorCode = U_ILLEGAL_ARGUMENT_ERROR;
+        return OTHER;
+    }
+}
+
+U_NAMESPACE_END
+
+#endif  // !UCONFIG_NO_FORMATTING
diff --git a/icu4c/source/i18n/standardplural.h b/icu4c/source/i18n/standardplural.h
new file mode 100644 (file)
index 0000000..174f2a4
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ *******************************************************************************
+ * Copyright (C) 2015, International Business Machines Corporation
+ * and others. All Rights Reserved.
+ *******************************************************************************
+ * standardplural.h
+ *
+ * created on: 2015dec14
+ * created by: Markus W. Scherer
+ */
+
+#ifndef __STANDARDPLURAL_H__
+#define __STANDARDPLURAL_H__
+
+#include "unicode/utypes.h"
+
+#if !UCONFIG_NO_FORMATTING
+
+U_NAMESPACE_BEGIN
+
+/**
+ * Standard CLDR plural form/category constants.
+ * See http://www.unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules
+ */
+class U_I18N_API StandardPlural {
+public:
+    enum Form {
+        ZERO,
+        ONE,
+        TWO,
+        FEW,
+        MANY,
+        OTHER,
+        COUNT
+    };
+
+    /**
+     * @return the lowercase CLDR keyword string for the plural form
+     */
+    static const char *getKeyword(Form p);
+
+    /**
+     * @param keyword for example "few" or "other"
+     * @return the plural form corresponding to the keyword, or OTHER
+     */
+    static Form orOtherFromString(const char *keyword) {
+        return static_cast<Form>(indexOrOtherIndexFromString(keyword));
+    }
+
+    /**
+     * 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 char *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
+     */
+    static int32_t indexOrNegativeFromString(const char *keyword);
+
+    /**
+     * @param keyword for example "few" or "other"
+     * @return the index of the plural form corresponding to the keyword, or OTHER_INDEX
+     */
+    static int32_t indexOrOtherIndexFromString(const char *keyword) {
+        int32_t i = indexOrNegativeFromString(keyword);
+        return i >= 0 ? i : OTHER;
+    }
+
+    /**
+     * 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 char *keyword, UErrorCode &errorCode);
+};
+
+U_NAMESPACE_END
+
+#endif  // !UCONFIG_NO_FORMATTING
+#endif  // __STANDARDPLURAL_H__