ICU-12031 port enum StandardPlural to C++

X-SVN-Rev: 38130
This commit is contained in:
Markus Scherer 2015-12-14 21:57:41 +00:00
parent 565abe4808
commit 4c96f9a866
8 changed files with 186 additions and 51 deletions

View 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 \

View File

@ -364,6 +364,7 @@
<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" />
@ -1278,6 +1279,7 @@
<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

View File

@ -282,6 +282,9 @@
<ClCompile Include="smpdtfst.cpp">
<Filter>formatting</Filter>
</ClCompile>
<ClCompile Include="standardplural.cpp">
<Filter>formatting</Filter>
</ClCompile>
<ClCompile Include="taiwncal.cpp">
<Filter>formatting</Filter>
</ClCompile>
@ -787,6 +790,9 @@
<ClInclude Include="smpdtfst.h">
<Filter>formatting</Filter>
</ClInclude>
<ClInclude Include="standardplural.h">
<Filter>formatting</Filter>
</ClInclude>
<ClInclude Include="taiwncal.h">
<Filter>formatting</Filter>
</ClInclude>

View File

@ -10,18 +10,19 @@
*/
#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"

View File

@ -21,46 +21,12 @@
#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;
}

View 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

View File

@ -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

View File

@ -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__