ICU-7387 Changed TimeUnitFormat::EStyle should bto C enum.

X-SVN-Rev: 29916
This commit is contained in:
Claire Ho 2011-04-28 16:42:58 +00:00
parent 6c49e812c0
commit 83d13b22aa
3 changed files with 59 additions and 58 deletions

View File

@ -77,18 +77,18 @@ static const UChar PLURAL_COUNT_TWO[] = {LOW_T, LOW_W, LOW_O, 0};
TimeUnitFormat::TimeUnitFormat(UErrorCode& status) TimeUnitFormat::TimeUnitFormat(UErrorCode& status)
: fNumberFormat(NULL), : fNumberFormat(NULL),
fPluralRules(NULL) { fPluralRules(NULL) {
create(Locale::getDefault(), kFull, status); create(Locale::getDefault(), UTMUTFMT_FULL_STYLE, status);
} }
TimeUnitFormat::TimeUnitFormat(const Locale& locale, UErrorCode& status) TimeUnitFormat::TimeUnitFormat(const Locale& locale, UErrorCode& status)
: fNumberFormat(NULL), : fNumberFormat(NULL),
fPluralRules(NULL) { fPluralRules(NULL) {
create(locale, kFull, status); create(locale, UTMUTFMT_FULL_STYLE, status);
} }
TimeUnitFormat::TimeUnitFormat(const Locale& locale, EStyle style, UErrorCode& status) TimeUnitFormat::TimeUnitFormat(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status)
: fNumberFormat(NULL), : fNumberFormat(NULL),
fPluralRules(NULL) { fPluralRules(NULL) {
create(locale, style, status); create(locale, style, status);
@ -99,7 +99,7 @@ TimeUnitFormat::TimeUnitFormat(const TimeUnitFormat& other)
: MeasureFormat(other), : MeasureFormat(other),
fNumberFormat(NULL), fNumberFormat(NULL),
fPluralRules(NULL), fPluralRules(NULL),
fStyle(kFull) fStyle(UTMUTFMT_FULL_STYLE)
{ {
for (TimeUnit::UTimeUnitFields i = TimeUnit::UTIMEUNIT_YEAR; for (TimeUnit::UTimeUnitFields i = TimeUnit::UTIMEUNIT_YEAR;
i < TimeUnit::UTIMEUNIT_FIELD_COUNT; i < TimeUnit::UTIMEUNIT_FIELD_COUNT;
@ -266,7 +266,8 @@ TimeUnitFormat::parseObject(const UnicodeString& source,
const UHashTok valueTok = elem->value; const UHashTok valueTok = elem->value;
// the value is a pair of MessageFormat* // the value is a pair of MessageFormat*
MessageFormat** patterns = (MessageFormat**)valueTok.pointer; MessageFormat** patterns = (MessageFormat**)valueTok.pointer;
for (EStyle style = kFull; style < kTotal; style = (EStyle)(style + 1)) { for (UTimeUnitFormatStyle style = UTMUTFMT_FULL_STYLE; style < UTMUTFMT_FORMAT_STYLE_COUNT;
style = (UTimeUnitFormatStyle)(style + 1)) {
MessageFormat* pattern = patterns[style]; MessageFormat* pattern = patterns[style];
pos.setErrorIndex(-1); pos.setErrorIndex(-1);
pos.setIndex(oldPos); pos.setIndex(oldPos);
@ -354,11 +355,11 @@ TimeUnitFormat::parseObject(const UnicodeString& source,
void void
TimeUnitFormat::create(const Locale& locale, EStyle style, UErrorCode& status) { TimeUnitFormat::create(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status) {
if (U_FAILURE(status)) { if (U_FAILURE(status)) {
return; return;
} }
if (style < kFull || style > kAbbreviate) { if (style < UTMUTFMT_FULL_STYLE || style > UTMUTFMT_ABBREVIATED_STYLE) {
status = U_ILLEGAL_ARGUMENT_ERROR; status = U_ILLEGAL_ARGUMENT_ERROR;
return; return;
} }
@ -383,10 +384,10 @@ TimeUnitFormat::create(const Locale& locale, EStyle style, UErrorCode& status) {
void void
TimeUnitFormat::setup(UErrorCode& err) { TimeUnitFormat::setup(UErrorCode& err) {
initDataMembers(err); initDataMembers(err);
readFromCurrentLocale(kFull, gUnitsTag, err); readFromCurrentLocale(UTMUTFMT_FULL_STYLE, gUnitsTag, err);
checkConsistency(kFull, gUnitsTag, err); checkConsistency(UTMUTFMT_FULL_STYLE, gUnitsTag, err);
readFromCurrentLocale(kAbbreviate, gShortUnitsTag, err); readFromCurrentLocale(UTMUTFMT_ABBREVIATED_STYLE, gShortUnitsTag, err);
checkConsistency(kAbbreviate, gShortUnitsTag, err); checkConsistency(UTMUTFMT_ABBREVIATED_STYLE, gShortUnitsTag, err);
} }
@ -412,7 +413,7 @@ TimeUnitFormat::initDataMembers(UErrorCode& err){
void void
TimeUnitFormat::readFromCurrentLocale(EStyle style, const char* key, UErrorCode& err) { TimeUnitFormat::readFromCurrentLocale(UTimeUnitFormatStyle style, const char* key, UErrorCode& err) {
if (U_FAILURE(err)) { if (U_FAILURE(err)) {
return; return;
} }
@ -496,9 +497,9 @@ TimeUnitFormat::readFromCurrentLocale(EStyle style, const char* key, UErrorCode&
} }
MessageFormat** formatters = (MessageFormat**)countToPatterns->get(pluralCount); MessageFormat** formatters = (MessageFormat**)countToPatterns->get(pluralCount);
if (formatters == NULL) { if (formatters == NULL) {
formatters = (MessageFormat**)uprv_malloc(kTotal*sizeof(MessageFormat*)); formatters = (MessageFormat**)uprv_malloc(UTMUTFMT_FORMAT_STYLE_COUNT*sizeof(MessageFormat*));
formatters[kFull] = NULL; formatters[UTMUTFMT_FULL_STYLE] = NULL;
formatters[kAbbreviate] = NULL; formatters[UTMUTFMT_ABBREVIATED_STYLE] = NULL;
countToPatterns->put(pluralCount, formatters, err); countToPatterns->put(pluralCount, formatters, err);
if (U_FAILURE(err)) { if (U_FAILURE(err)) {
uprv_free(formatters); uprv_free(formatters);
@ -532,7 +533,7 @@ TimeUnitFormat::readFromCurrentLocale(EStyle style, const char* key, UErrorCode&
void void
TimeUnitFormat::checkConsistency(EStyle style, const char* key, UErrorCode& err) { TimeUnitFormat::checkConsistency(UTimeUnitFormatStyle style, const char* key, UErrorCode& err) {
if (U_FAILURE(err)) { if (U_FAILURE(err)) {
return; return;
} }
@ -598,7 +599,7 @@ TimeUnitFormat::checkConsistency(EStyle style, const char* key, UErrorCode& err)
// using patterns of plural count "other", // using patterns of plural count "other",
// then, "other" is the searchPluralCount. // then, "other" is the searchPluralCount.
void void
TimeUnitFormat::searchInLocaleChain(EStyle style, const char* key, const char* localeName, TimeUnitFormat::searchInLocaleChain(UTimeUnitFormatStyle style, const char* key, const char* localeName,
TimeUnit::UTimeUnitFields srcTimeUnitField, TimeUnit::UTimeUnitFields srcTimeUnitField,
const char* srcPluralCount, const char* srcPluralCount,
const char* searchPluralCount, const char* searchPluralCount,
@ -631,9 +632,9 @@ TimeUnitFormat::searchInLocaleChain(EStyle style, const char* key, const char* l
} }
MessageFormat** formatters = (MessageFormat**)countToPatterns->get(srcPluralCount); MessageFormat** formatters = (MessageFormat**)countToPatterns->get(srcPluralCount);
if (formatters == NULL) { if (formatters == NULL) {
formatters = (MessageFormat**)uprv_malloc(kTotal*sizeof(MessageFormat*)); formatters = (MessageFormat**)uprv_malloc(UTMUTFMT_FORMAT_STYLE_COUNT*sizeof(MessageFormat*));
formatters[kFull] = NULL; formatters[UTMUTFMT_FULL_STYLE] = NULL;
formatters[kAbbreviate] = NULL; formatters[UTMUTFMT_ABBREVIATED_STYLE] = NULL;
countToPatterns->put(srcPluralCount, formatters, err); countToPatterns->put(srcPluralCount, formatters, err);
if (U_FAILURE(err)) { if (U_FAILURE(err)) {
uprv_free(formatters); uprv_free(formatters);
@ -706,9 +707,9 @@ TimeUnitFormat::searchInLocaleChain(EStyle style, const char* key, const char* l
} }
MessageFormat** formatters = (MessageFormat**)countToPatterns->get(srcPluralCount); MessageFormat** formatters = (MessageFormat**)countToPatterns->get(srcPluralCount);
if (formatters == NULL) { if (formatters == NULL) {
formatters = (MessageFormat**)uprv_malloc(kTotal*sizeof(MessageFormat*)); formatters = (MessageFormat**)uprv_malloc(UTMUTFMT_FORMAT_STYLE_COUNT*sizeof(MessageFormat*));
formatters[kFull] = NULL; formatters[UTMUTFMT_FULL_STYLE] = NULL;
formatters[kAbbreviate] = NULL; formatters[UTMUTFMT_ABBREVIATED_STYLE] = NULL;
countToPatterns->put(srcPluralCount, formatters, err); countToPatterns->put(srcPluralCount, formatters, err);
if (U_FAILURE(err)) { if (U_FAILURE(err)) {
uprv_free(formatters); uprv_free(formatters);
@ -754,8 +755,8 @@ TimeUnitFormat::setNumberFormat(const NumberFormat& format, UErrorCode& status){
while ((elem = fTimeUnitToCountToPatterns[i]->nextElement(pos)) != NULL){ while ((elem = fTimeUnitToCountToPatterns[i]->nextElement(pos)) != NULL){
const UHashTok keyTok = elem->value; const UHashTok keyTok = elem->value;
MessageFormat** pattern = (MessageFormat**)keyTok.pointer; MessageFormat** pattern = (MessageFormat**)keyTok.pointer;
pattern[kFull]->setFormat(0, format); pattern[UTMUTFMT_FULL_STYLE]->setFormat(0, format);
pattern[kAbbreviate]->setFormat(0, format); pattern[UTMUTFMT_ABBREVIATED_STYLE]->setFormat(0, format);
} }
} }
} }
@ -769,8 +770,8 @@ TimeUnitFormat::deleteHash(Hashtable* htable) {
while ( (element = htable->nextElement(pos)) != NULL ) { while ( (element = htable->nextElement(pos)) != NULL ) {
const UHashTok valueTok = element->value; const UHashTok valueTok = element->value;
const MessageFormat** value = (const MessageFormat**)valueTok.pointer; const MessageFormat** value = (const MessageFormat**)valueTok.pointer;
delete value[kFull]; delete value[UTMUTFMT_FULL_STYLE];
delete value[kAbbreviate]; delete value[UTMUTFMT_ABBREVIATED_STYLE];
//delete[] value; //delete[] value;
uprv_free(value); uprv_free(value);
} }
@ -792,7 +793,7 @@ TimeUnitFormat::copyHash(const Hashtable* source, Hashtable* target, UErrorCode&
const UnicodeString* key = (UnicodeString*)keyTok.pointer; const UnicodeString* key = (UnicodeString*)keyTok.pointer;
const UHashTok valueTok = element->value; const UHashTok valueTok = element->value;
const MessageFormat** value = (const MessageFormat**)valueTok.pointer; const MessageFormat** value = (const MessageFormat**)valueTok.pointer;
MessageFormat** newVal = (MessageFormat**)uprv_malloc(kTotal*sizeof(MessageFormat*)); MessageFormat** newVal = (MessageFormat**)uprv_malloc(UTMUTFMT_FORMAT_STYLE_COUNT*sizeof(MessageFormat*));
newVal[0] = (MessageFormat*)value[0]->clone(); newVal[0] = (MessageFormat*)value[0]->clone();
newVal[1] = (MessageFormat*)value[1]->clone(); newVal[1] = (MessageFormat*)value[1]->clone();
target->put(UnicodeString(*key), newVal, status); target->put(UnicodeString(*key), newVal, status);

View File

@ -1,6 +1,6 @@
/* /*
******************************************************************************* *******************************************************************************
* Copyright (C) 2010, Google, International Business Machines Corporation and * * Copyright (C) 2011, Google, International Business Machines Corporation and *
* others. All Rights Reserved. * * others. All Rights Reserved. *
******************************************************************************* *******************************************************************************
*/ */
@ -31,6 +31,19 @@
union UHashTok; union UHashTok;
/**
* Constants for various styles.
* There are 2 styles: full name and abbreviated name.
* For example, for English, the full name for hour duration is "3 hours",
* and the abbreviated name is "3 hrs".
* @draft ICU 4.8
*/
enum UTimeUnitFormatStyle {
UTMUTFMT_FULL_STYLE,
UTMUTFMT_ABBREVIATED_STYLE,
UTMUTFMT_FORMAT_STYLE_COUNT
};
U_NAMESPACE_BEGIN U_NAMESPACE_BEGIN
class Hashtable; class Hashtable;
@ -69,19 +82,6 @@ class Hashtable;
class U_I18N_API TimeUnitFormat: public MeasureFormat { class U_I18N_API TimeUnitFormat: public MeasureFormat {
public: public:
/**
* Constants for various styles.
* There are 2 styles: full name and abbreviated name.
* For example, for English, the full name for hour duration is "3 hours",
* and the abbreviated name is "3 hrs".
* @draft ICU 4.2
*/
enum EStyle {
kFull = 0,
kAbbreviate = 1,
kTotal = kAbbreviate + 1
};
/** /**
* Create TimeUnitFormat with default locale, and full name style. * Create TimeUnitFormat with default locale, and full name style.
* Use setLocale and/or setFormat to modify. * Use setLocale and/or setFormat to modify.
@ -99,7 +99,7 @@ public:
* Create TimeUnitFormat given locale and style. * Create TimeUnitFormat given locale and style.
* @draft ICU 4.2 * @draft ICU 4.2
*/ */
TimeUnitFormat(const Locale& locale, EStyle style, UErrorCode& status); TimeUnitFormat(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status);
/** /**
* Copy constructor. * Copy constructor.
@ -219,9 +219,9 @@ private:
Locale fLocale; Locale fLocale;
Hashtable* fTimeUnitToCountToPatterns[TimeUnit::UTIMEUNIT_FIELD_COUNT]; Hashtable* fTimeUnitToCountToPatterns[TimeUnit::UTIMEUNIT_FIELD_COUNT];
PluralRules* fPluralRules; PluralRules* fPluralRules;
EStyle fStyle; UTimeUnitFormatStyle fStyle;
void create(const Locale& locale, EStyle style, UErrorCode& status); void create(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status);
// it might actually be simpler to make them Decimal Formats later. // it might actually be simpler to make them Decimal Formats later.
// initialize all private data members // initialize all private data members
@ -231,14 +231,14 @@ private:
void initDataMembers(UErrorCode& status); void initDataMembers(UErrorCode& status);
// initialize fTimeUnitToCountToPatterns from current locale's resource. // initialize fTimeUnitToCountToPatterns from current locale's resource.
void readFromCurrentLocale(EStyle style, const char* key, UErrorCode& status); void readFromCurrentLocale(UTimeUnitFormatStyle style, const char* key, UErrorCode& status);
// check completeness of fTimeUnitToCountToPatterns against all time units, // check completeness of fTimeUnitToCountToPatterns against all time units,
// and all plural rules, fill in fallback as necessary. // and all plural rules, fill in fallback as necessary.
void checkConsistency(EStyle style, const char* key, UErrorCode& status); void checkConsistency(UTimeUnitFormatStyle style, const char* key, UErrorCode& status);
// fill in fTimeUnitToCountToPatterns from locale fall-back chain // fill in fTimeUnitToCountToPatterns from locale fall-back chain
void searchInLocaleChain(EStyle style, const char* key, const char* localeName, void searchInLocaleChain(UTimeUnitFormatStyle style, const char* key, const char* localeName,
TimeUnit::UTimeUnitFields field, const char*, TimeUnit::UTimeUnitFields field, const char*,
const char*, Hashtable*, UErrorCode&); const char*, Hashtable*, UErrorCode&);

View File

@ -1,5 +1,5 @@
/******************************************************************** /********************************************************************
* Copyright (c) 2008-2010, International Business Machines Corporation and * Copyright (c) 2008-2011, International Business Machines Corporation and
* others. All Rights Reserved. * others. All Rights Reserved.
********************************************************************/ ********************************************************************/
@ -41,15 +41,15 @@ void TimeUnitTest::testBasic() {
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
Locale loc(locales[locIndex]); Locale loc(locales[locIndex]);
TimeUnitFormat** formats = new TimeUnitFormat*[2]; TimeUnitFormat** formats = new TimeUnitFormat*[2];
formats[TimeUnitFormat::kFull] = new TimeUnitFormat(loc, status); formats[UTMUTFMT_FULL_STYLE] = new TimeUnitFormat(loc, status);
if (!assertSuccess("TimeUnitFormat(full)", status, TRUE)) return; if (!assertSuccess("TimeUnitFormat(full)", status, TRUE)) return;
formats[TimeUnitFormat::kAbbreviate] = new TimeUnitFormat(loc, TimeUnitFormat::kAbbreviate, status); formats[UTMUTFMT_ABBREVIATED_STYLE] = new TimeUnitFormat(loc, UTMUTFMT_ABBREVIATED_STYLE, status);
if (!assertSuccess("TimeUnitFormat(short)", status)) return; if (!assertSuccess("TimeUnitFormat(short)", status)) return;
#ifdef TUFMTTS_DEBUG #ifdef TUFMTTS_DEBUG
std::cout << "locale: " << locales[locIndex] << "\n"; std::cout << "locale: " << locales[locIndex] << "\n";
#endif #endif
for (int style = TimeUnitFormat::kFull; for (int style = UTMUTFMT_FULL_STYLE;
style <= TimeUnitFormat::kAbbreviate; style <= UTMUTFMT_ABBREVIATED_STYLE;
++style) { ++style) {
for (TimeUnit::UTimeUnitFields j = TimeUnit::UTIMEUNIT_YEAR; for (TimeUnit::UTimeUnitFields j = TimeUnit::UTIMEUNIT_YEAR;
j < TimeUnit::UTIMEUNIT_FIELD_COUNT; j < TimeUnit::UTIMEUNIT_FIELD_COUNT;
@ -90,8 +90,8 @@ void TimeUnitTest::testBasic() {
} }
} }
} }
delete formats[TimeUnitFormat::kFull]; delete formats[UTMUTFMT_FULL_STYLE];
delete formats[TimeUnitFormat::kAbbreviate]; delete formats[UTMUTFMT_ABBREVIATED_STYLE];
delete[] formats; delete[] formats;
} }
} }
@ -186,11 +186,11 @@ void TimeUnitTest::testAPI() {
delete tmf_en; delete tmf_en;
TimeUnitFormat* en_long = new TimeUnitFormat(Locale("en"), TimeUnitFormat::kFull, status); TimeUnitFormat* en_long = new TimeUnitFormat(Locale("en"), UTMUTFMT_FULL_STYLE, status);
if (!assertSuccess("TimeUnitFormat(en...)", status)) return; if (!assertSuccess("TimeUnitFormat(en...)", status)) return;
delete en_long; delete en_long;
TimeUnitFormat* en_short = new TimeUnitFormat(Locale("en"), TimeUnitFormat::kAbbreviate, status); TimeUnitFormat* en_short = new TimeUnitFormat(Locale("en"), UTMUTFMT_ABBREVIATED_STYLE, status);
if (!assertSuccess("TimeUnitFormat(en...)", status)) return; if (!assertSuccess("TimeUnitFormat(en...)", status)) return;
delete en_short; delete en_short;
@ -213,7 +213,7 @@ void TimeUnitTest::testGreek() {
const char* locales[] = {"el-GR", "el"}; const char* locales[] = {"el-GR", "el"};
TimeUnit::UTimeUnitFields tunits[] = {TimeUnit::UTIMEUNIT_SECOND, TimeUnit::UTIMEUNIT_MINUTE, TimeUnit::UTIMEUNIT_HOUR, TimeUnit::UTIMEUNIT_DAY, TimeUnit::UTIMEUNIT_MONTH, TimeUnit::UTIMEUNIT_YEAR}; TimeUnit::UTimeUnitFields tunits[] = {TimeUnit::UTIMEUNIT_SECOND, TimeUnit::UTIMEUNIT_MINUTE, TimeUnit::UTIMEUNIT_HOUR, TimeUnit::UTIMEUNIT_DAY, TimeUnit::UTIMEUNIT_MONTH, TimeUnit::UTIMEUNIT_YEAR};
TimeUnitFormat::EStyle styles[] = {TimeUnitFormat::kFull, TimeUnitFormat::kAbbreviate}; UTimeUnitFormatStyle styles[] = {UTMUTFMT_FULL_STYLE, UTMUTFMT_ABBREVIATED_STYLE};
const int numbers[] = {1, 7}; const int numbers[] = {1, 7};
const UChar oneSecond[] = {0x0031, 0x0020, 0x03b4, 0x03b5, 0x03c5, 0x03c4, 0x03b5, 0x03c1, 0x03cc, 0x03bb, 0x03b5, 0x03c0, 0x03c4, 0x03bf, 0}; const UChar oneSecond[] = {0x0031, 0x0020, 0x03b4, 0x03b5, 0x03c5, 0x03c4, 0x03b5, 0x03c1, 0x03cc, 0x03bb, 0x03b5, 0x03c0, 0x03c4, 0x03bf, 0};