2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2008-09-19 19:05:37 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 2008-2015, Google, International Business Machines Corporation
|
|
|
|
* and others. All Rights Reserved.
|
2008-09-19 19:05:37 +00:00
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/tmutfmt.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
2013-10-10 21:52:15 +00:00
|
|
|
#include "unicode/decimfmt.h"
|
2015-12-14 04:41:52 +00:00
|
|
|
#include "unicode/localpointer.h"
|
2013-10-10 21:52:15 +00:00
|
|
|
#include "plurrule_impl.h"
|
2012-06-25 16:34:19 +00:00
|
|
|
#include "uvector.h"
|
2011-06-03 05:23:57 +00:00
|
|
|
#include "charstr.h"
|
2008-09-19 19:05:37 +00:00
|
|
|
#include "cmemory.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
#include "hash.h"
|
|
|
|
#include "uresimp.h"
|
2014-09-11 10:16:22 +00:00
|
|
|
#include "ureslocs.h"
|
2008-09-19 19:05:37 +00:00
|
|
|
#include "unicode/msgfmt.h"
|
2011-11-14 19:32:51 +00:00
|
|
|
#include "uassert.h"
|
2008-09-19 19:05:37 +00:00
|
|
|
|
|
|
|
#define LEFT_CURLY_BRACKET ((UChar)0x007B)
|
|
|
|
#define RIGHT_CURLY_BRACKET ((UChar)0x007D)
|
|
|
|
#define SPACE ((UChar)0x0020)
|
|
|
|
#define DIGIT_ZERO ((UChar)0x0030)
|
|
|
|
#define LOW_S ((UChar)0x0073)
|
|
|
|
#define LOW_M ((UChar)0x006D)
|
|
|
|
#define LOW_I ((UChar)0x0069)
|
|
|
|
#define LOW_N ((UChar)0x006E)
|
|
|
|
#define LOW_H ((UChar)0x0068)
|
|
|
|
#define LOW_W ((UChar)0x0077)
|
|
|
|
#define LOW_D ((UChar)0x0064)
|
|
|
|
#define LOW_Y ((UChar)0x0079)
|
|
|
|
#define LOW_Z ((UChar)0x007A)
|
|
|
|
#define LOW_E ((UChar)0x0065)
|
|
|
|
#define LOW_R ((UChar)0x0072)
|
|
|
|
#define LOW_O ((UChar)0x006F)
|
|
|
|
#define LOW_N ((UChar)0x006E)
|
|
|
|
#define LOW_T ((UChar)0x0074)
|
|
|
|
|
|
|
|
|
2008-10-22 06:06:56 +00:00
|
|
|
//TODO: define in compile time
|
2008-09-19 19:05:37 +00:00
|
|
|
//#define TMUTFMT_DEBUG 1
|
|
|
|
|
|
|
|
#ifdef TMUTFMT_DEBUG
|
|
|
|
#include <iostream>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TimeUnitFormat)
|
|
|
|
|
|
|
|
static const char gUnitsTag[] = "units";
|
2009-04-17 18:25:16 +00:00
|
|
|
static const char gShortUnitsTag[] = "unitsShort";
|
2008-09-19 19:05:37 +00:00
|
|
|
static const char gTimeUnitYear[] = "year";
|
|
|
|
static const char gTimeUnitMonth[] = "month";
|
|
|
|
static const char gTimeUnitDay[] = "day";
|
|
|
|
static const char gTimeUnitWeek[] = "week";
|
|
|
|
static const char gTimeUnitHour[] = "hour";
|
|
|
|
static const char gTimeUnitMinute[] = "minute";
|
|
|
|
static const char gTimeUnitSecond[] = "second";
|
|
|
|
static const char gPluralCountOther[] = "other";
|
|
|
|
|
|
|
|
static const UChar DEFAULT_PATTERN_FOR_SECOND[] = {LEFT_CURLY_BRACKET, DIGIT_ZERO, RIGHT_CURLY_BRACKET, SPACE, LOW_S, 0};
|
|
|
|
static const UChar DEFAULT_PATTERN_FOR_MINUTE[] = {LEFT_CURLY_BRACKET, DIGIT_ZERO, RIGHT_CURLY_BRACKET, SPACE, LOW_M, LOW_I, LOW_N, 0};
|
|
|
|
static const UChar DEFAULT_PATTERN_FOR_HOUR[] = {LEFT_CURLY_BRACKET, DIGIT_ZERO, RIGHT_CURLY_BRACKET, SPACE, LOW_H, 0};
|
|
|
|
static const UChar DEFAULT_PATTERN_FOR_WEEK[] = {LEFT_CURLY_BRACKET, DIGIT_ZERO, RIGHT_CURLY_BRACKET, SPACE, LOW_W, 0};
|
|
|
|
static const UChar DEFAULT_PATTERN_FOR_DAY[] = {LEFT_CURLY_BRACKET, DIGIT_ZERO, RIGHT_CURLY_BRACKET, SPACE, LOW_D, 0};
|
|
|
|
static const UChar DEFAULT_PATTERN_FOR_MONTH[] = {LEFT_CURLY_BRACKET, DIGIT_ZERO, RIGHT_CURLY_BRACKET, SPACE, LOW_M, 0};
|
|
|
|
static const UChar DEFAULT_PATTERN_FOR_YEAR[] = {LEFT_CURLY_BRACKET, DIGIT_ZERO, RIGHT_CURLY_BRACKET, SPACE, LOW_Y, 0};
|
|
|
|
|
|
|
|
static const UChar PLURAL_COUNT_ZERO[] = {LOW_Z, LOW_E, LOW_R, LOW_O, 0};
|
|
|
|
static const UChar PLURAL_COUNT_ONE[] = {LOW_O, LOW_N, LOW_E, 0};
|
|
|
|
static const UChar PLURAL_COUNT_TWO[] = {LOW_T, LOW_W, LOW_O, 0};
|
|
|
|
|
2014-02-04 00:29:17 +00:00
|
|
|
TimeUnitFormat::TimeUnitFormat(UErrorCode& status) {
|
2014-02-10 19:13:50 +00:00
|
|
|
initMeasureFormat(Locale::getDefault(), UMEASFMT_WIDTH_WIDE, NULL, status);
|
2014-02-04 00:29:17 +00:00
|
|
|
create(UTMUTFMT_FULL_STYLE, status);
|
2008-09-19 19:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-04 00:29:17 +00:00
|
|
|
TimeUnitFormat::TimeUnitFormat(const Locale& locale, UErrorCode& status) {
|
2014-02-10 19:13:50 +00:00
|
|
|
initMeasureFormat(locale, UMEASFMT_WIDTH_WIDE, NULL, status);
|
2014-02-04 00:29:17 +00:00
|
|
|
create(UTMUTFMT_FULL_STYLE, status);
|
2008-09-19 19:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-04 00:29:17 +00:00
|
|
|
TimeUnitFormat::TimeUnitFormat(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status) {
|
|
|
|
switch (style) {
|
|
|
|
case UTMUTFMT_FULL_STYLE:
|
2014-02-10 19:13:50 +00:00
|
|
|
initMeasureFormat(locale, UMEASFMT_WIDTH_WIDE, NULL, status);
|
2014-02-04 00:29:17 +00:00
|
|
|
break;
|
|
|
|
case UTMUTFMT_ABBREVIATED_STYLE:
|
2014-02-10 19:13:50 +00:00
|
|
|
initMeasureFormat(locale, UMEASFMT_WIDTH_SHORT, NULL, status);
|
2014-02-04 00:29:17 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-02-10 19:13:50 +00:00
|
|
|
initMeasureFormat(locale, UMEASFMT_WIDTH_WIDE, NULL, status);
|
2014-02-04 00:29:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
create(style, status);
|
2008-09-19 19:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TimeUnitFormat::TimeUnitFormat(const TimeUnitFormat& other)
|
|
|
|
: MeasureFormat(other),
|
2014-02-04 00:29:17 +00:00
|
|
|
fStyle(other.fStyle)
|
2013-08-29 23:45:48 +00:00
|
|
|
{
|
2008-09-19 19:05:37 +00:00
|
|
|
for (TimeUnit::UTimeUnitFields i = TimeUnit::UTIMEUNIT_YEAR;
|
|
|
|
i < TimeUnit::UTIMEUNIT_FIELD_COUNT;
|
|
|
|
i = (TimeUnit::UTimeUnitFields)(i+1)) {
|
2014-02-04 00:29:17 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
fTimeUnitToCountToPatterns[i] = initHash(status);
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
copyHash(other.fTimeUnitToCountToPatterns[i], fTimeUnitToCountToPatterns[i], status);
|
|
|
|
} else {
|
|
|
|
delete fTimeUnitToCountToPatterns[i];
|
|
|
|
fTimeUnitToCountToPatterns[i] = NULL;
|
|
|
|
}
|
2015-12-14 05:10:38 +00:00
|
|
|
}
|
2008-09-19 19:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TimeUnitFormat::~TimeUnitFormat() {
|
|
|
|
for (TimeUnit::UTimeUnitFields i = TimeUnit::UTIMEUNIT_YEAR;
|
|
|
|
i < TimeUnit::UTIMEUNIT_FIELD_COUNT;
|
|
|
|
i = (TimeUnit::UTimeUnitFields)(i+1)) {
|
|
|
|
deleteHash(fTimeUnitToCountToPatterns[i]);
|
|
|
|
fTimeUnitToCountToPatterns[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-22 00:13:18 +00:00
|
|
|
TimeUnitFormat*
|
|
|
|
TimeUnitFormat::clone() const {
|
2008-09-19 19:05:37 +00:00
|
|
|
return new TimeUnitFormat(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-14 05:10:38 +00:00
|
|
|
TimeUnitFormat&
|
2008-09-19 19:05:37 +00:00
|
|
|
TimeUnitFormat::operator=(const TimeUnitFormat& other) {
|
|
|
|
if (this == &other) {
|
|
|
|
return *this;
|
|
|
|
}
|
2014-02-04 00:29:17 +00:00
|
|
|
MeasureFormat::operator=(other);
|
2008-09-19 19:05:37 +00:00
|
|
|
for (TimeUnit::UTimeUnitFields i = TimeUnit::UTIMEUNIT_YEAR;
|
|
|
|
i < TimeUnit::UTIMEUNIT_FIELD_COUNT;
|
|
|
|
i = (TimeUnit::UTimeUnitFields)(i+1)) {
|
|
|
|
deleteHash(fTimeUnitToCountToPatterns[i]);
|
|
|
|
fTimeUnitToCountToPatterns[i] = NULL;
|
|
|
|
}
|
|
|
|
for (TimeUnit::UTimeUnitFields i = TimeUnit::UTIMEUNIT_YEAR;
|
|
|
|
i < TimeUnit::UTIMEUNIT_FIELD_COUNT;
|
|
|
|
i = (TimeUnit::UTimeUnitFields)(i+1)) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
fTimeUnitToCountToPatterns[i] = initHash(status);
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
copyHash(other.fTimeUnitToCountToPatterns[i], fTimeUnitToCountToPatterns[i], status);
|
|
|
|
} else {
|
|
|
|
delete fTimeUnitToCountToPatterns[i];
|
|
|
|
fTimeUnitToCountToPatterns[i] = NULL;
|
|
|
|
}
|
2015-12-14 05:10:38 +00:00
|
|
|
}
|
2008-09-19 19:05:37 +00:00
|
|
|
fStyle = other.fStyle;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-12-14 05:10:38 +00:00
|
|
|
void
|
|
|
|
TimeUnitFormat::parseObject(const UnicodeString& source,
|
2008-09-19 19:05:37 +00:00
|
|
|
Formattable& result,
|
|
|
|
ParsePosition& pos) const {
|
2015-12-14 05:10:38 +00:00
|
|
|
Formattable resultNumber(0.0);
|
2008-09-19 19:05:37 +00:00
|
|
|
UBool withNumberFormat = false;
|
|
|
|
TimeUnit::UTimeUnitFields resultTimeUnit = TimeUnit::UTIMEUNIT_FIELD_COUNT;
|
|
|
|
int32_t oldPos = pos.getIndex();
|
|
|
|
int32_t newPos = -1;
|
|
|
|
int32_t longestParseDistance = 0;
|
|
|
|
UnicodeString* countOfLongestMatch = NULL;
|
|
|
|
#ifdef TMUTFMT_DEBUG
|
|
|
|
char res[1000];
|
|
|
|
source.extract(0, source.length(), res, "UTF-8");
|
2015-12-14 05:10:38 +00:00
|
|
|
std::cout << "parse source: " << res << "\n";
|
2008-09-19 19:05:37 +00:00
|
|
|
#endif
|
|
|
|
// parse by iterating through all available patterns
|
|
|
|
// and looking for the longest match.
|
|
|
|
for (TimeUnit::UTimeUnitFields i = TimeUnit::UTIMEUNIT_YEAR;
|
|
|
|
i < TimeUnit::UTIMEUNIT_FIELD_COUNT;
|
|
|
|
i = (TimeUnit::UTimeUnitFields)(i+1)) {
|
|
|
|
Hashtable* countToPatterns = fTimeUnitToCountToPatterns[i];
|
2014-12-09 23:54:56 +00:00
|
|
|
int32_t elemPos = UHASH_FIRST;
|
2008-09-19 19:05:37 +00:00
|
|
|
const UHashElement* elem = NULL;
|
|
|
|
while ((elem = countToPatterns->nextElement(elemPos)) != NULL){
|
|
|
|
const UHashTok keyTok = elem->key;
|
|
|
|
UnicodeString* count = (UnicodeString*)keyTok.pointer;
|
|
|
|
#ifdef TMUTFMT_DEBUG
|
|
|
|
count->extract(0, count->length(), res, "UTF-8");
|
2015-12-14 05:10:38 +00:00
|
|
|
std::cout << "parse plural count: " << res << "\n";
|
2008-09-19 19:05:37 +00:00
|
|
|
#endif
|
|
|
|
const UHashTok valueTok = elem->value;
|
|
|
|
// the value is a pair of MessageFormat*
|
|
|
|
MessageFormat** patterns = (MessageFormat**)valueTok.pointer;
|
2011-04-28 16:42:58 +00:00
|
|
|
for (UTimeUnitFormatStyle style = UTMUTFMT_FULL_STYLE; style < UTMUTFMT_FORMAT_STYLE_COUNT;
|
|
|
|
style = (UTimeUnitFormatStyle)(style + 1)) {
|
2008-09-19 19:05:37 +00:00
|
|
|
MessageFormat* pattern = patterns[style];
|
|
|
|
pos.setErrorIndex(-1);
|
|
|
|
pos.setIndex(oldPos);
|
|
|
|
// see if we can parse
|
|
|
|
Formattable parsed;
|
|
|
|
pattern->parseObject(source, parsed, pos);
|
|
|
|
if (pos.getErrorIndex() != -1 || pos.getIndex() == oldPos) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#ifdef TMUTFMT_DEBUG
|
|
|
|
std::cout << "parsed.getType: " << parsed.getType() << "\n";
|
|
|
|
#endif
|
2013-10-10 21:52:15 +00:00
|
|
|
Formattable tmpNumber(0.0);
|
2008-09-19 19:05:37 +00:00
|
|
|
if (pattern->getArgTypeCount() != 0) {
|
|
|
|
Formattable& temp = parsed[0];
|
2013-10-10 21:52:15 +00:00
|
|
|
if (temp.getType() == Formattable::kString) {
|
|
|
|
UnicodeString tmpString;
|
|
|
|
UErrorCode pStatus = U_ZERO_ERROR;
|
2019-02-16 01:32:50 +00:00
|
|
|
getNumberFormatInternal().parse(temp.getString(tmpString), tmpNumber, pStatus);
|
2013-10-10 21:52:15 +00:00
|
|
|
if (U_FAILURE(pStatus)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (temp.isNumeric()) {
|
|
|
|
tmpNumber = temp;
|
2008-09-19 19:05:37 +00:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int32_t parseDistance = pos.getIndex() - oldPos;
|
|
|
|
if (parseDistance > longestParseDistance) {
|
|
|
|
if (pattern->getArgTypeCount() != 0) {
|
|
|
|
resultNumber = tmpNumber;
|
|
|
|
withNumberFormat = true;
|
|
|
|
} else {
|
|
|
|
withNumberFormat = false;
|
|
|
|
}
|
|
|
|
resultTimeUnit = i;
|
|
|
|
newPos = pos.getIndex();
|
|
|
|
longestParseDistance = parseDistance;
|
|
|
|
countOfLongestMatch = count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* After find the longest match, parse the number.
|
|
|
|
* Result number could be null for the pattern without number pattern.
|
|
|
|
* such as unit pattern in Arabic.
|
|
|
|
* When result number is null, use plural rule to set the number.
|
|
|
|
*/
|
|
|
|
if (withNumberFormat == false && longestParseDistance != 0) {
|
|
|
|
// set the number using plurrual count
|
2011-07-07 18:46:19 +00:00
|
|
|
if (0 == countOfLongestMatch->compare(PLURAL_COUNT_ZERO, 4)) {
|
2013-10-10 21:52:15 +00:00
|
|
|
resultNumber = Formattable(0.0);
|
2011-07-07 18:46:19 +00:00
|
|
|
} else if (0 == countOfLongestMatch->compare(PLURAL_COUNT_ONE, 3)) {
|
2013-10-10 21:52:15 +00:00
|
|
|
resultNumber = Formattable(1.0);
|
2011-07-07 18:46:19 +00:00
|
|
|
} else if (0 == countOfLongestMatch->compare(PLURAL_COUNT_TWO, 3)) {
|
2013-10-10 21:52:15 +00:00
|
|
|
resultNumber = Formattable(2.0);
|
2008-09-19 19:05:37 +00:00
|
|
|
} else {
|
|
|
|
// should not happen.
|
|
|
|
// TODO: how to handle?
|
2013-10-10 21:52:15 +00:00
|
|
|
resultNumber = Formattable(3.0);
|
2008-09-19 19:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (longestParseDistance == 0) {
|
|
|
|
pos.setIndex(oldPos);
|
|
|
|
pos.setErrorIndex(0);
|
|
|
|
} else {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2015-12-14 04:41:52 +00:00
|
|
|
LocalPointer<TimeUnitAmount> tmutamt(new TimeUnitAmount(resultNumber, resultTimeUnit, status), status);
|
2008-09-19 19:05:37 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
2015-12-14 04:41:52 +00:00
|
|
|
result.adoptObject(tmutamt.orphan());
|
2008-09-19 19:05:37 +00:00
|
|
|
pos.setIndex(newPos);
|
|
|
|
pos.setErrorIndex(-1);
|
|
|
|
} else {
|
|
|
|
pos.setIndex(oldPos);
|
|
|
|
pos.setErrorIndex(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-02-04 00:29:17 +00:00
|
|
|
TimeUnitFormat::create(UTimeUnitFormatStyle style, UErrorCode& status) {
|
2014-03-13 21:28:24 +00:00
|
|
|
// fTimeUnitToCountToPatterns[] must have its elements initialized to NULL first
|
|
|
|
// before checking for failure status.
|
|
|
|
for (TimeUnit::UTimeUnitFields i = TimeUnit::UTIMEUNIT_YEAR;
|
|
|
|
i < TimeUnit::UTIMEUNIT_FIELD_COUNT;
|
|
|
|
i = (TimeUnit::UTimeUnitFields)(i+1)) {
|
|
|
|
fTimeUnitToCountToPatterns[i] = NULL;
|
|
|
|
}
|
|
|
|
|
2008-09-19 19:05:37 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-07-25 16:36:25 +00:00
|
|
|
if (style < UTMUTFMT_FULL_STYLE || style >= UTMUTFMT_FORMAT_STYLE_COUNT) {
|
2008-09-19 19:05:37 +00:00
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fStyle = style;
|
2014-02-04 00:29:17 +00:00
|
|
|
|
2008-09-19 19:05:37 +00:00
|
|
|
//TODO: format() and parseObj() are const member functions,
|
|
|
|
//so, can not do lazy initialization in C++.
|
|
|
|
//setup has to be done in constructors.
|
|
|
|
//and here, the behavior is not consistent with Java.
|
|
|
|
//In Java, create an empty instance does not setup locale as
|
|
|
|
//default locale. If it followed by setNumberFormat(),
|
|
|
|
//in format(), the locale will set up as the locale in fNumberFormat.
|
2015-12-14 05:10:38 +00:00
|
|
|
//But in C++, this sets the locale as the default locale.
|
2008-09-19 19:05:37 +00:00
|
|
|
setup(status);
|
|
|
|
}
|
|
|
|
|
2015-12-14 05:10:38 +00:00
|
|
|
void
|
2008-09-19 19:05:37 +00:00
|
|
|
TimeUnitFormat::setup(UErrorCode& err) {
|
|
|
|
initDataMembers(err);
|
2012-06-08 19:02:03 +00:00
|
|
|
|
2022-09-08 21:15:13 +00:00
|
|
|
UVector pluralCounts(nullptr, uhash_compareUnicodeString, 6, err);
|
2015-12-14 04:41:52 +00:00
|
|
|
LocalPointer<StringEnumeration> keywords(getPluralRules().getKeywords(err), err);
|
2012-06-08 19:02:03 +00:00
|
|
|
if (U_FAILURE(err)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
UnicodeString* pluralCount;
|
|
|
|
while ((pluralCount = const_cast<UnicodeString*>(keywords->snext(err))) != NULL) {
|
|
|
|
pluralCounts.addElement(pluralCount, err);
|
|
|
|
}
|
|
|
|
readFromCurrentLocale(UTMUTFMT_FULL_STYLE, gUnitsTag, pluralCounts, err);
|
2011-04-28 16:42:58 +00:00
|
|
|
checkConsistency(UTMUTFMT_FULL_STYLE, gUnitsTag, err);
|
2012-06-08 19:02:03 +00:00
|
|
|
readFromCurrentLocale(UTMUTFMT_ABBREVIATED_STYLE, gShortUnitsTag, pluralCounts, err);
|
2011-04-28 16:42:58 +00:00
|
|
|
checkConsistency(UTMUTFMT_ABBREVIATED_STYLE, gShortUnitsTag, err);
|
2008-09-19 19:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
TimeUnitFormat::initDataMembers(UErrorCode& err){
|
|
|
|
if (U_FAILURE(err)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (TimeUnit::UTimeUnitFields i = TimeUnit::UTIMEUNIT_YEAR;
|
|
|
|
i < TimeUnit::UTIMEUNIT_FIELD_COUNT;
|
|
|
|
i = (TimeUnit::UTimeUnitFields)(i+1)) {
|
|
|
|
deleteHash(fTimeUnitToCountToPatterns[i]);
|
|
|
|
fTimeUnitToCountToPatterns[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-24 00:51:49 +00:00
|
|
|
struct TimeUnitFormatReadSink : public ResourceSink {
|
|
|
|
TimeUnitFormat *timeUnitFormatObj;
|
|
|
|
const UVector &pluralCounts;
|
|
|
|
UTimeUnitFormatStyle style;
|
|
|
|
UBool beenHere;
|
|
|
|
|
|
|
|
TimeUnitFormatReadSink(TimeUnitFormat *timeUnitFormatObj,
|
|
|
|
const UVector &pluralCounts, UTimeUnitFormatStyle style) :
|
|
|
|
timeUnitFormatObj(timeUnitFormatObj), pluralCounts(pluralCounts),
|
2022-09-08 21:15:13 +00:00
|
|
|
style(style), beenHere(false){}
|
2016-06-24 00:51:49 +00:00
|
|
|
|
|
|
|
virtual ~TimeUnitFormatReadSink();
|
|
|
|
|
2022-09-08 21:15:13 +00:00
|
|
|
virtual void put(const char *key, ResourceValue &value, UBool, UErrorCode &errorCode) override {
|
2016-06-24 00:51:49 +00:00
|
|
|
// Skip all put() calls except the first one -- discard all fallback data.
|
|
|
|
if (beenHere) {
|
|
|
|
return;
|
|
|
|
} else {
|
2022-09-08 21:15:13 +00:00
|
|
|
beenHere = true;
|
2016-06-24 00:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ResourceTable units = value.getTable(errorCode);
|
|
|
|
if (U_FAILURE(errorCode)) { return; }
|
|
|
|
|
|
|
|
for (int32_t i = 0; units.getKeyAndValue(i, key, value); ++i) {
|
|
|
|
const char* timeUnitName = key;
|
|
|
|
if (timeUnitName == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeUnit::UTimeUnitFields timeUnitField = TimeUnit::UTIMEUNIT_FIELD_COUNT;
|
|
|
|
if ( uprv_strcmp(timeUnitName, gTimeUnitYear) == 0 ) {
|
|
|
|
timeUnitField = TimeUnit::UTIMEUNIT_YEAR;
|
|
|
|
} else if ( uprv_strcmp(timeUnitName, gTimeUnitMonth) == 0 ) {
|
|
|
|
timeUnitField = TimeUnit::UTIMEUNIT_MONTH;
|
|
|
|
} else if ( uprv_strcmp(timeUnitName, gTimeUnitDay) == 0 ) {
|
|
|
|
timeUnitField = TimeUnit::UTIMEUNIT_DAY;
|
|
|
|
} else if ( uprv_strcmp(timeUnitName, gTimeUnitHour) == 0 ) {
|
|
|
|
timeUnitField = TimeUnit::UTIMEUNIT_HOUR;
|
|
|
|
} else if ( uprv_strcmp(timeUnitName, gTimeUnitMinute) == 0 ) {
|
|
|
|
timeUnitField = TimeUnit::UTIMEUNIT_MINUTE;
|
|
|
|
} else if ( uprv_strcmp(timeUnitName, gTimeUnitSecond) == 0 ) {
|
|
|
|
timeUnitField = TimeUnit::UTIMEUNIT_SECOND;
|
|
|
|
} else if ( uprv_strcmp(timeUnitName, gTimeUnitWeek) == 0 ) {
|
|
|
|
timeUnitField = TimeUnit::UTIMEUNIT_WEEK;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
LocalPointer<Hashtable> localCountToPatterns;
|
|
|
|
Hashtable *countToPatterns =
|
|
|
|
timeUnitFormatObj->fTimeUnitToCountToPatterns[timeUnitField];
|
|
|
|
if (countToPatterns == NULL) {
|
|
|
|
localCountToPatterns.adoptInsteadAndCheckErrorCode(
|
|
|
|
timeUnitFormatObj->initHash(errorCode), errorCode);
|
|
|
|
countToPatterns = localCountToPatterns.getAlias();
|
|
|
|
if (U_FAILURE(errorCode)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ResourceTable countsToPatternTable = value.getTable(errorCode);
|
|
|
|
if (U_FAILURE(errorCode)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (int32_t j = 0; countsToPatternTable.getKeyAndValue(j, key, value); ++j) {
|
|
|
|
errorCode = U_ZERO_ERROR;
|
|
|
|
UnicodeString pattern = value.getUnicodeString(errorCode);
|
|
|
|
if (U_FAILURE(errorCode)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
UnicodeString pluralCountUniStr(key, -1, US_INV);
|
|
|
|
if (!pluralCounts.contains(&pluralCountUniStr)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
LocalPointer<MessageFormat> messageFormat(new MessageFormat(
|
|
|
|
pattern, timeUnitFormatObj->getLocale(errorCode), errorCode), errorCode);
|
|
|
|
if (U_FAILURE(errorCode)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MessageFormat** formatters =
|
|
|
|
(MessageFormat**)countToPatterns->get(pluralCountUniStr);
|
|
|
|
if (formatters == NULL) {
|
|
|
|
LocalMemory<MessageFormat *> localFormatters(
|
|
|
|
(MessageFormat **)uprv_malloc(UTMUTFMT_FORMAT_STYLE_COUNT*sizeof(MessageFormat*)));
|
|
|
|
if (localFormatters.isNull()) {
|
|
|
|
errorCode = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
localFormatters[UTMUTFMT_FULL_STYLE] = NULL;
|
|
|
|
localFormatters[UTMUTFMT_ABBREVIATED_STYLE] = NULL;
|
|
|
|
countToPatterns->put(pluralCountUniStr, localFormatters.getAlias(), errorCode);
|
|
|
|
if (U_FAILURE(errorCode)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
formatters = localFormatters.orphan();
|
|
|
|
}
|
|
|
|
formatters[style] = messageFormat.orphan();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeUnitFormatObj->fTimeUnitToCountToPatterns[timeUnitField] == NULL) {
|
|
|
|
timeUnitFormatObj->fTimeUnitToCountToPatterns[timeUnitField] = localCountToPatterns.orphan();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
TimeUnitFormatReadSink::~TimeUnitFormatReadSink() {}
|
|
|
|
|
2008-09-19 19:05:37 +00:00
|
|
|
void
|
2012-06-08 19:02:03 +00:00
|
|
|
TimeUnitFormat::readFromCurrentLocale(UTimeUnitFormatStyle style, const char* key,
|
|
|
|
const UVector& pluralCounts, UErrorCode& err) {
|
2008-09-19 19:05:37 +00:00
|
|
|
if (U_FAILURE(err)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// fill timeUnitToCountToPatterns from resource file
|
|
|
|
// err is used to indicate wrong status except missing resource.
|
|
|
|
// status is an error code used in resource lookup.
|
|
|
|
// status does not affect "err".
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2015-12-14 04:41:52 +00:00
|
|
|
LocalUResourceBundlePointer rb(ures_open(U_ICUDATA_UNIT, getLocaleID(status), &status));
|
|
|
|
|
|
|
|
LocalUResourceBundlePointer unitsRes(ures_getByKey(rb.getAlias(), key, NULL, &status));
|
|
|
|
ures_getByKey(unitsRes.getAlias(), "duration", unitsRes.getAlias(), &status);
|
2008-09-19 19:05:37 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-24 00:51:49 +00:00
|
|
|
TimeUnitFormatReadSink sink(this, pluralCounts, style);
|
|
|
|
ures_getAllItemsWithFallback(unitsRes.getAlias(), "", sink, status);
|
|
|
|
}
|
2008-09-19 19:05:37 +00:00
|
|
|
|
2015-12-14 05:10:38 +00:00
|
|
|
void
|
2011-04-28 16:42:58 +00:00
|
|
|
TimeUnitFormat::checkConsistency(UTimeUnitFormatStyle style, const char* key, UErrorCode& err) {
|
2008-09-19 19:05:37 +00:00
|
|
|
if (U_FAILURE(err)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// there should be patterns for each plural rule in each time unit.
|
2015-12-14 05:10:38 +00:00
|
|
|
// For each time unit,
|
2008-09-19 19:05:37 +00:00
|
|
|
// for each plural rule, following is unit pattern fall-back rule:
|
|
|
|
// ( for example: "one" hour )
|
|
|
|
// look for its unit pattern in its locale tree.
|
|
|
|
// if pattern is not found in its own locale, such as de_DE,
|
|
|
|
// look for the pattern in its parent, such as de,
|
|
|
|
// keep looking till found or till root.
|
|
|
|
// if the pattern is not found in root either,
|
|
|
|
// fallback to plural count "other",
|
|
|
|
// look for the pattern of "other" in the locale tree:
|
|
|
|
// "de_DE" to "de" to "root".
|
2015-12-14 05:10:38 +00:00
|
|
|
// If not found, fall back to value of
|
|
|
|
// static variable DEFAULT_PATTERN_FOR_xxx, such as "{0} h".
|
2008-09-19 19:05:37 +00:00
|
|
|
//
|
|
|
|
// Following is consistency check to create pattern for each
|
|
|
|
// plural rule in each time unit using above fall-back rule.
|
|
|
|
//
|
2015-12-14 04:41:52 +00:00
|
|
|
LocalPointer<StringEnumeration> keywords(
|
|
|
|
getPluralRules().getKeywords(err), err);
|
|
|
|
const UnicodeString* pluralCount;
|
|
|
|
while (U_SUCCESS(err) && (pluralCount = keywords->snext(err)) != NULL) {
|
|
|
|
for (int32_t i = 0; i < TimeUnit::UTIMEUNIT_FIELD_COUNT; ++i) {
|
2015-12-14 05:10:38 +00:00
|
|
|
// for each time unit,
|
2015-12-14 04:41:52 +00:00
|
|
|
// get all the patterns for each plural rule in this locale.
|
|
|
|
Hashtable* countToPatterns = fTimeUnitToCountToPatterns[i];
|
|
|
|
if ( countToPatterns == NULL ) {
|
|
|
|
fTimeUnitToCountToPatterns[i] = countToPatterns = initHash(err);
|
|
|
|
if (U_FAILURE(err)) {
|
|
|
|
return;
|
2008-09-19 19:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-14 04:41:52 +00:00
|
|
|
MessageFormat** formatters = (MessageFormat**)countToPatterns->get(*pluralCount);
|
|
|
|
if( formatters == NULL || formatters[style] == NULL ) {
|
|
|
|
// look through parents
|
|
|
|
const char* localeName = getLocaleID(err);
|
|
|
|
CharString pluralCountChars;
|
|
|
|
pluralCountChars.appendInvariantChars(*pluralCount, err);
|
|
|
|
searchInLocaleChain(style, key, localeName,
|
2015-12-14 05:10:38 +00:00
|
|
|
(TimeUnit::UTimeUnitFields)i,
|
|
|
|
*pluralCount, pluralCountChars.data(),
|
2015-12-14 04:41:52 +00:00
|
|
|
countToPatterns, err);
|
|
|
|
}
|
2015-12-14 05:10:38 +00:00
|
|
|
// TODO: what to do with U_FAILURE(err) at this point.
|
2015-12-14 04:41:52 +00:00
|
|
|
// As is, the outer loop continues to run, but does nothing.
|
2008-09-19 19:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// srcPluralCount is the original plural count on which the pattern is
|
|
|
|
// searched for.
|
|
|
|
// searchPluralCount is the fallback plural count.
|
|
|
|
// For example, to search for pattern for ""one" hour",
|
|
|
|
// "one" is the srcPluralCount,
|
2015-12-14 05:10:38 +00:00
|
|
|
// if the pattern is not found even in root, fallback to
|
|
|
|
// using patterns of plural count "other",
|
2008-09-19 19:05:37 +00:00
|
|
|
// then, "other" is the searchPluralCount.
|
2015-12-14 05:10:38 +00:00
|
|
|
void
|
2011-04-28 16:42:58 +00:00
|
|
|
TimeUnitFormat::searchInLocaleChain(UTimeUnitFormatStyle style, const char* key, const char* localeName,
|
2008-09-19 19:05:37 +00:00
|
|
|
TimeUnit::UTimeUnitFields srcTimeUnitField,
|
2011-06-03 05:23:57 +00:00
|
|
|
const UnicodeString& srcPluralCount,
|
2015-12-14 05:10:38 +00:00
|
|
|
const char* searchPluralCount,
|
2008-09-19 19:05:37 +00:00
|
|
|
Hashtable* countToPatterns,
|
|
|
|
UErrorCode& err) {
|
|
|
|
if (U_FAILURE(err)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
char parentLocale[ULOC_FULLNAME_CAPACITY];
|
2010-10-19 08:28:02 +00:00
|
|
|
uprv_strcpy(parentLocale, localeName);
|
2008-09-19 19:05:37 +00:00
|
|
|
int32_t locNameLen;
|
2011-11-14 19:32:51 +00:00
|
|
|
U_ASSERT(countToPatterns != NULL);
|
2008-09-19 19:05:37 +00:00
|
|
|
while ((locNameLen = uloc_getParent(parentLocale, parentLocale,
|
|
|
|
ULOC_FULLNAME_CAPACITY, &status)) >= 0){
|
|
|
|
// look for pattern for srcPluralCount in locale tree
|
2015-12-14 04:41:52 +00:00
|
|
|
LocalUResourceBundlePointer rb(ures_open(U_ICUDATA_UNIT, parentLocale, &status));
|
|
|
|
LocalUResourceBundlePointer unitsRes(ures_getByKey(rb.getAlias(), key, NULL, &status));
|
2008-09-19 19:05:37 +00:00
|
|
|
const char* timeUnitName = getTimeUnitName(srcTimeUnitField, status);
|
2015-12-14 04:41:52 +00:00
|
|
|
LocalUResourceBundlePointer countsToPatternRB(ures_getByKey(unitsRes.getAlias(), timeUnitName, NULL, &status));
|
2008-09-19 19:05:37 +00:00
|
|
|
const UChar* pattern;
|
|
|
|
int32_t ptLength;
|
2015-12-14 04:41:52 +00:00
|
|
|
pattern = ures_getStringByKeyWithFallback(countsToPatternRB.getAlias(), searchPluralCount, &ptLength, &status);
|
2008-09-19 19:05:37 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
//found
|
2015-12-14 04:41:52 +00:00
|
|
|
LocalPointer<MessageFormat> messageFormat(
|
2022-09-08 21:15:13 +00:00
|
|
|
new MessageFormat(UnicodeString(true, pattern, ptLength), getLocale(err), err), err);
|
2015-12-14 04:41:52 +00:00
|
|
|
if (U_FAILURE(err)) {
|
|
|
|
return;
|
2008-09-19 19:05:37 +00:00
|
|
|
}
|
2015-12-14 04:41:52 +00:00
|
|
|
MessageFormat** formatters = (MessageFormat**)countToPatterns->get(srcPluralCount);
|
|
|
|
if (formatters == NULL) {
|
|
|
|
LocalMemory<MessageFormat *> localFormatters(
|
|
|
|
(MessageFormat**)uprv_malloc(UTMUTFMT_FORMAT_STYLE_COUNT*sizeof(MessageFormat*)));
|
|
|
|
formatters = localFormatters.getAlias();
|
|
|
|
localFormatters[UTMUTFMT_FULL_STYLE] = NULL;
|
|
|
|
localFormatters[UTMUTFMT_ABBREVIATED_STYLE] = NULL;
|
|
|
|
countToPatterns->put(srcPluralCount, localFormatters.orphan(), err);
|
|
|
|
if (U_FAILURE(err)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-12-14 05:10:38 +00:00
|
|
|
}
|
2015-12-14 04:41:52 +00:00
|
|
|
//delete formatters[style];
|
|
|
|
formatters[style] = messageFormat.orphan();
|
2008-09-19 19:05:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-10-19 08:28:02 +00:00
|
|
|
status = U_ZERO_ERROR;
|
2015-12-14 04:41:52 +00:00
|
|
|
if (locNameLen == 0) {
|
2008-09-19 19:05:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-10-19 08:28:02 +00:00
|
|
|
|
|
|
|
// if no unitsShort resource was found even after fallback to root locale
|
|
|
|
// then search the units resource fallback from the current level to root
|
|
|
|
if ( locNameLen == 0 && uprv_strcmp(key, gShortUnitsTag) == 0) {
|
|
|
|
#ifdef TMUTFMT_DEBUG
|
|
|
|
std::cout << "loop into searchInLocaleChain since Short-Long-Alternative \n";
|
|
|
|
#endif
|
2015-12-14 04:41:52 +00:00
|
|
|
CharString pLocale(localeName, -1, err);
|
2010-10-19 08:28:02 +00:00
|
|
|
// Add an underscore at the tail of locale name,
|
|
|
|
// so that searchInLocaleChain will check the current locale before falling back
|
2015-12-14 04:41:52 +00:00
|
|
|
pLocale.append('_', err);
|
|
|
|
searchInLocaleChain(style, gUnitsTag, pLocale.data(), srcTimeUnitField, srcPluralCount,
|
2010-10-19 08:28:02 +00:00
|
|
|
searchPluralCount, countToPatterns, err);
|
2015-12-14 04:41:52 +00:00
|
|
|
if (U_FAILURE(err)) {
|
|
|
|
return;
|
|
|
|
}
|
2011-11-14 19:32:51 +00:00
|
|
|
MessageFormat** formatters = (MessageFormat**)countToPatterns->get(srcPluralCount);
|
|
|
|
if (formatters != NULL && formatters[style] != NULL) {
|
|
|
|
return;
|
2010-10-19 08:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-19 19:05:37 +00:00
|
|
|
// if not found the pattern for this plural count at all,
|
|
|
|
// fall-back to plural count "other"
|
|
|
|
if ( uprv_strcmp(searchPluralCount, gPluralCountOther) == 0 ) {
|
|
|
|
// set default fall back the same as the resource in root
|
2015-12-14 04:41:52 +00:00
|
|
|
LocalPointer<MessageFormat> messageFormat;
|
2011-07-07 18:46:19 +00:00
|
|
|
const UChar *pattern = NULL;
|
2008-09-19 19:05:37 +00:00
|
|
|
if ( srcTimeUnitField == TimeUnit::UTIMEUNIT_SECOND ) {
|
2011-07-07 18:46:19 +00:00
|
|
|
pattern = DEFAULT_PATTERN_FOR_SECOND;
|
2008-09-19 19:05:37 +00:00
|
|
|
} else if ( srcTimeUnitField == TimeUnit::UTIMEUNIT_MINUTE ) {
|
2011-07-07 18:46:19 +00:00
|
|
|
pattern = DEFAULT_PATTERN_FOR_MINUTE;
|
2008-09-19 19:05:37 +00:00
|
|
|
} else if ( srcTimeUnitField == TimeUnit::UTIMEUNIT_HOUR ) {
|
2011-07-07 18:46:19 +00:00
|
|
|
pattern = DEFAULT_PATTERN_FOR_HOUR;
|
2008-09-19 19:05:37 +00:00
|
|
|
} else if ( srcTimeUnitField == TimeUnit::UTIMEUNIT_WEEK ) {
|
2011-07-07 18:46:19 +00:00
|
|
|
pattern = DEFAULT_PATTERN_FOR_WEEK;
|
2008-09-19 19:05:37 +00:00
|
|
|
} else if ( srcTimeUnitField == TimeUnit::UTIMEUNIT_DAY ) {
|
2011-07-07 18:46:19 +00:00
|
|
|
pattern = DEFAULT_PATTERN_FOR_DAY;
|
2008-09-19 19:05:37 +00:00
|
|
|
} else if ( srcTimeUnitField == TimeUnit::UTIMEUNIT_MONTH ) {
|
2011-07-07 18:46:19 +00:00
|
|
|
pattern = DEFAULT_PATTERN_FOR_MONTH;
|
2008-09-19 19:05:37 +00:00
|
|
|
} else if ( srcTimeUnitField == TimeUnit::UTIMEUNIT_YEAR ) {
|
2011-07-07 18:46:19 +00:00
|
|
|
pattern = DEFAULT_PATTERN_FOR_YEAR;
|
|
|
|
}
|
|
|
|
if (pattern != NULL) {
|
2015-12-14 04:41:52 +00:00
|
|
|
messageFormat.adoptInsteadAndCheckErrorCode(
|
2022-09-08 21:15:13 +00:00
|
|
|
new MessageFormat(UnicodeString(true, pattern, -1), getLocale(err), err), err);
|
2008-09-19 19:05:37 +00:00
|
|
|
}
|
2015-12-14 04:41:52 +00:00
|
|
|
if (U_FAILURE(err)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MessageFormat** formatters = (MessageFormat**)countToPatterns->get(srcPluralCount);
|
|
|
|
if (formatters == NULL) {
|
|
|
|
LocalMemory<MessageFormat *> localFormatters (
|
|
|
|
(MessageFormat**)uprv_malloc(UTMUTFMT_FORMAT_STYLE_COUNT*sizeof(MessageFormat*)));
|
|
|
|
if (localFormatters.isNull()) {
|
|
|
|
err = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
2008-09-19 19:05:37 +00:00
|
|
|
}
|
2015-12-14 04:41:52 +00:00
|
|
|
formatters = localFormatters.getAlias();
|
|
|
|
formatters[UTMUTFMT_FULL_STYLE] = NULL;
|
|
|
|
formatters[UTMUTFMT_ABBREVIATED_STYLE] = NULL;
|
|
|
|
countToPatterns->put(srcPluralCount, localFormatters.orphan(), err);
|
|
|
|
}
|
|
|
|
if (U_SUCCESS(err)) {
|
|
|
|
//delete formatters[style];
|
|
|
|
formatters[style] = messageFormat.orphan();
|
2008-09-19 19:05:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// fall back to rule "other", and search in parents
|
2015-12-14 05:10:38 +00:00
|
|
|
searchInLocaleChain(style, key, localeName, srcTimeUnitField, srcPluralCount,
|
2008-09-19 19:05:37 +00:00
|
|
|
gPluralCountOther, countToPatterns, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-14 05:10:38 +00:00
|
|
|
void
|
2008-09-19 19:05:37 +00:00
|
|
|
TimeUnitFormat::setLocale(const Locale& locale, UErrorCode& status) {
|
2014-02-04 00:29:17 +00:00
|
|
|
if (setMeasureFormatLocale(locale, status)) {
|
2008-09-19 19:05:37 +00:00
|
|
|
setup(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-14 05:10:38 +00:00
|
|
|
void
|
2008-09-19 19:05:37 +00:00
|
|
|
TimeUnitFormat::setNumberFormat(const NumberFormat& format, UErrorCode& status){
|
2014-02-04 00:29:17 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2008-09-19 19:05:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-08-23 03:00:25 +00:00
|
|
|
adoptNumberFormat(format.clone(), status);
|
2008-09-19 19:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
TimeUnitFormat::deleteHash(Hashtable* htable) {
|
2014-12-09 23:54:56 +00:00
|
|
|
int32_t pos = UHASH_FIRST;
|
2008-09-19 19:05:37 +00:00
|
|
|
const UHashElement* element = NULL;
|
|
|
|
if ( htable ) {
|
|
|
|
while ( (element = htable->nextElement(pos)) != NULL ) {
|
|
|
|
const UHashTok valueTok = element->value;
|
|
|
|
const MessageFormat** value = (const MessageFormat**)valueTok.pointer;
|
2011-04-28 16:42:58 +00:00
|
|
|
delete value[UTMUTFMT_FULL_STYLE];
|
|
|
|
delete value[UTMUTFMT_ABBREVIATED_STYLE];
|
2008-09-19 19:05:37 +00:00
|
|
|
//delete[] value;
|
|
|
|
uprv_free(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete htable;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
TimeUnitFormat::copyHash(const Hashtable* source, Hashtable* target, UErrorCode& status) {
|
|
|
|
if ( U_FAILURE(status) ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-12-09 23:54:56 +00:00
|
|
|
int32_t pos = UHASH_FIRST;
|
2008-09-19 19:05:37 +00:00
|
|
|
const UHashElement* element = NULL;
|
|
|
|
if ( source ) {
|
|
|
|
while ( (element = source->nextElement(pos)) != NULL ) {
|
|
|
|
const UHashTok keyTok = element->key;
|
|
|
|
const UnicodeString* key = (UnicodeString*)keyTok.pointer;
|
|
|
|
const UHashTok valueTok = element->value;
|
|
|
|
const MessageFormat** value = (const MessageFormat**)valueTok.pointer;
|
2011-04-28 16:42:58 +00:00
|
|
|
MessageFormat** newVal = (MessageFormat**)uprv_malloc(UTMUTFMT_FORMAT_STYLE_COUNT*sizeof(MessageFormat*));
|
2019-08-23 03:00:25 +00:00
|
|
|
newVal[0] = value[0]->clone();
|
|
|
|
newVal[1] = value[1]->clone();
|
2008-09-19 19:05:37 +00:00
|
|
|
target->put(UnicodeString(*key), newVal, status);
|
|
|
|
if ( U_FAILURE(status) ) {
|
|
|
|
delete newVal[0];
|
|
|
|
delete newVal[1];
|
2010-09-27 23:52:31 +00:00
|
|
|
uprv_free(newVal);
|
2008-09-19 19:05:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-14 05:10:38 +00:00
|
|
|
U_CDECL_BEGIN
|
2008-09-19 19:05:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* set hash table value comparator
|
|
|
|
*
|
|
|
|
* @param val1 one value in comparison
|
|
|
|
* @param val2 the other value in comparison
|
2022-09-08 21:15:13 +00:00
|
|
|
* @return true if 2 values are the same, false otherwise
|
2008-09-19 19:05:37 +00:00
|
|
|
*/
|
2010-11-05 23:43:11 +00:00
|
|
|
static UBool U_CALLCONV tmutfmtHashTableValueComparator(UHashTok val1, UHashTok val2);
|
2008-09-19 19:05:37 +00:00
|
|
|
|
2010-11-05 23:43:11 +00:00
|
|
|
static UBool
|
2009-07-10 18:58:35 +00:00
|
|
|
U_CALLCONV tmutfmtHashTableValueComparator(UHashTok val1, UHashTok val2) {
|
2008-09-19 19:05:37 +00:00
|
|
|
const MessageFormat** pattern1 = (const MessageFormat**)val1.pointer;
|
|
|
|
const MessageFormat** pattern2 = (const MessageFormat**)val2.pointer;
|
|
|
|
return *pattern1[0] == *pattern2[0] && *pattern1[1] == *pattern2[1];
|
|
|
|
}
|
|
|
|
|
2009-12-14 05:57:34 +00:00
|
|
|
U_CDECL_END
|
|
|
|
|
2008-09-19 19:05:37 +00:00
|
|
|
Hashtable*
|
|
|
|
TimeUnitFormat::initHash(UErrorCode& status) {
|
|
|
|
if ( U_FAILURE(status) ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Hashtable* hTable;
|
2022-09-08 21:15:13 +00:00
|
|
|
if ( (hTable = new Hashtable(true, status)) == NULL ) {
|
2008-09-19 19:05:37 +00:00
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-03-01 20:55:43 +00:00
|
|
|
if ( U_FAILURE(status) ) {
|
2015-12-14 05:10:38 +00:00
|
|
|
delete hTable;
|
2011-03-01 20:55:43 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-02-16 23:43:22 +00:00
|
|
|
hTable->setValueComparator(tmutfmtHashTableValueComparator);
|
2008-09-19 19:05:37 +00:00
|
|
|
return hTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char*
|
2015-12-14 05:10:38 +00:00
|
|
|
TimeUnitFormat::getTimeUnitName(TimeUnit::UTimeUnitFields unitField,
|
2008-09-19 19:05:37 +00:00
|
|
|
UErrorCode& status) {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
switch (unitField) {
|
|
|
|
case TimeUnit::UTIMEUNIT_YEAR:
|
|
|
|
return gTimeUnitYear;
|
|
|
|
case TimeUnit::UTIMEUNIT_MONTH:
|
|
|
|
return gTimeUnitMonth;
|
|
|
|
case TimeUnit::UTIMEUNIT_DAY:
|
|
|
|
return gTimeUnitDay;
|
|
|
|
case TimeUnit::UTIMEUNIT_WEEK:
|
|
|
|
return gTimeUnitWeek;
|
|
|
|
case TimeUnit::UTIMEUNIT_HOUR:
|
|
|
|
return gTimeUnitHour;
|
|
|
|
case TimeUnit::UTIMEUNIT_MINUTE:
|
|
|
|
return gTimeUnitMinute;
|
|
|
|
case TimeUnit::UTIMEUNIT_SECOND:
|
|
|
|
return gTimeUnitSecond;
|
|
|
|
default:
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif
|