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
|
2004-04-21 18:05:06 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (c) 2004-2016, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
2004-04-21 18:05:06 +00:00
|
|
|
**********************************************************************
|
|
|
|
* Author: Alan Liu
|
|
|
|
* Created: April 20, 2004
|
|
|
|
* Since: ICU 3.0
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
2014-03-11 17:40:23 +00:00
|
|
|
#include "utypeinfo.h" // for 'typeid' to work
|
2004-04-21 18:05:06 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
|
|
|
#include "unicode/measfmt.h"
|
2014-02-04 00:29:17 +00:00
|
|
|
#include "unicode/numfmt.h"
|
2004-04-21 18:05:06 +00:00
|
|
|
#include "currfmt.h"
|
2014-02-04 00:29:17 +00:00
|
|
|
#include "unicode/localpointer.h"
|
2015-11-18 00:18:28 +00:00
|
|
|
#include "resource.h"
|
2016-01-23 05:20:23 +00:00
|
|
|
#include "unicode/simpleformatter.h"
|
2014-02-04 00:29:17 +00:00
|
|
|
#include "quantityformatter.h"
|
|
|
|
#include "unicode/plurrule.h"
|
|
|
|
#include "unicode/decimfmt.h"
|
|
|
|
#include "uresimp.h"
|
|
|
|
#include "unicode/ures.h"
|
2017-02-09 21:15:34 +00:00
|
|
|
#include "unicode/ustring.h"
|
2014-09-11 10:16:22 +00:00
|
|
|
#include "ureslocs.h"
|
2014-02-04 00:29:17 +00:00
|
|
|
#include "cstring.h"
|
|
|
|
#include "mutex.h"
|
|
|
|
#include "ucln_in.h"
|
|
|
|
#include "unicode/listformatter.h"
|
|
|
|
#include "charstr.h"
|
|
|
|
#include "unicode/putil.h"
|
|
|
|
#include "unicode/smpdtfmt.h"
|
2014-05-22 23:21:32 +00:00
|
|
|
#include "uassert.h"
|
2014-02-04 00:29:17 +00:00
|
|
|
|
2014-02-10 19:13:50 +00:00
|
|
|
#include "sharednumberformat.h"
|
|
|
|
#include "sharedpluralrules.h"
|
2015-12-16 04:41:33 +00:00
|
|
|
#include "standardplural.h"
|
2014-08-20 21:46:02 +00:00
|
|
|
#include "unifiedcache.h"
|
2014-02-04 00:29:17 +00:00
|
|
|
|
|
|
|
|
2004-04-21 18:05:06 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2017-09-29 23:04:34 +00:00
|
|
|
static constexpr int32_t PER_UNIT_INDEX = StandardPlural::COUNT;
|
|
|
|
static constexpr int32_t PATTERN_COUNT = PER_UNIT_INDEX + 1;
|
|
|
|
static constexpr int32_t MEAS_UNIT_COUNT = 138; // see assertion in MeasureFormatCacheData constructor
|
|
|
|
static constexpr int32_t WIDTH_INDEX_COUNT = UMEASFMT_WIDTH_NARROW + 1;
|
|
|
|
|
2014-03-11 17:40:23 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(MeasureFormat)
|
|
|
|
|
2014-02-10 19:13:50 +00:00
|
|
|
// Used to format durations like 5:47 or 21:35:42.
|
2014-02-04 00:29:17 +00:00
|
|
|
class NumericDateFormatters : public UMemory {
|
|
|
|
public:
|
2014-02-10 19:13:50 +00:00
|
|
|
// Formats like H:mm
|
2014-02-04 00:29:17 +00:00
|
|
|
SimpleDateFormat hourMinute;
|
2014-02-10 19:13:50 +00:00
|
|
|
|
|
|
|
// formats like M:ss
|
2014-02-04 00:29:17 +00:00
|
|
|
SimpleDateFormat minuteSecond;
|
2014-02-10 19:13:50 +00:00
|
|
|
|
|
|
|
// formats like H:mm:ss
|
2014-02-04 00:29:17 +00:00
|
|
|
SimpleDateFormat hourMinuteSecond;
|
2014-02-10 19:13:50 +00:00
|
|
|
|
|
|
|
// Constructor that takes the actual patterns for hour-minute,
|
|
|
|
// minute-second, and hour-minute-second respectively.
|
2014-02-04 00:29:17 +00:00
|
|
|
NumericDateFormatters(
|
|
|
|
const UnicodeString &hm,
|
|
|
|
const UnicodeString &ms,
|
|
|
|
const UnicodeString &hms,
|
|
|
|
UErrorCode &status) :
|
|
|
|
hourMinute(hm, status),
|
|
|
|
minuteSecond(ms, status),
|
|
|
|
hourMinuteSecond(hms, status) {
|
|
|
|
const TimeZone *gmt = TimeZone::getGMT();
|
|
|
|
hourMinute.setTimeZone(*gmt);
|
|
|
|
minuteSecond.setTimeZone(*gmt);
|
|
|
|
hourMinuteSecond.setTimeZone(*gmt);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
NumericDateFormatters(const NumericDateFormatters &other);
|
|
|
|
NumericDateFormatters &operator=(const NumericDateFormatters &other);
|
|
|
|
};
|
|
|
|
|
2015-11-19 22:33:25 +00:00
|
|
|
static UMeasureFormatWidth getRegularWidth(UMeasureFormatWidth width) {
|
|
|
|
if (width >= WIDTH_INDEX_COUNT) {
|
|
|
|
return UMEASFMT_WIDTH_NARROW;
|
|
|
|
}
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
2015-11-14 00:31:21 +00:00
|
|
|
/**
|
|
|
|
* Instances contain all MeasureFormat specific data for a particular locale.
|
|
|
|
* This data is cached. It is never copied, but is shared via shared pointers.
|
|
|
|
*
|
|
|
|
* Note: We might change the cache data to have an array[WIDTH_INDEX_COUNT] of
|
|
|
|
* complete sets of unit & per patterns,
|
|
|
|
* to correspond to the resource data and its aliases.
|
|
|
|
*
|
|
|
|
* TODO: Maybe store more sparsely in general, with pointers rather than potentially-empty objects.
|
|
|
|
*/
|
2014-02-10 19:13:50 +00:00
|
|
|
class MeasureFormatCacheData : public SharedObject {
|
2014-02-04 00:29:17 +00:00
|
|
|
public:
|
2015-12-16 04:41:33 +00:00
|
|
|
|
2015-11-14 00:31:21 +00:00
|
|
|
/**
|
|
|
|
* Redirection data from root-bundle, top-level sideways aliases.
|
|
|
|
* - UMEASFMT_WIDTH_COUNT: initial value, just fall back to root
|
|
|
|
* - UMEASFMT_WIDTH_WIDE/SHORT/NARROW: sideways alias for missing data
|
|
|
|
*/
|
|
|
|
UMeasureFormatWidth widthFallback[WIDTH_INDEX_COUNT];
|
2015-12-16 04:41:33 +00:00
|
|
|
/** Measure unit -> format width -> array of patterns ("{0} meters") (plurals + PER_UNIT_INDEX) */
|
2017-10-05 00:47:08 +00:00
|
|
|
SimpleFormatter* patterns[MEAS_UNIT_COUNT][WIDTH_INDEX_COUNT][PATTERN_COUNT];
|
|
|
|
const UChar* dnams[MEAS_UNIT_COUNT][WIDTH_INDEX_COUNT];
|
2016-01-23 05:20:23 +00:00
|
|
|
SimpleFormatter perFormatters[WIDTH_INDEX_COUNT];
|
2014-09-10 20:06:52 +00:00
|
|
|
|
2014-02-10 19:13:50 +00:00
|
|
|
MeasureFormatCacheData();
|
2015-12-16 04:41:33 +00:00
|
|
|
virtual ~MeasureFormatCacheData();
|
2015-11-12 22:09:04 +00:00
|
|
|
|
|
|
|
UBool hasPerFormatter(int32_t width) const {
|
|
|
|
// TODO: Create a more obvious way to test if the per-formatter has been set?
|
|
|
|
// Use pointers, check for NULL? Or add an isValid() method?
|
2016-01-23 05:20:23 +00:00
|
|
|
return perFormatters[width].getArgumentLimit() == 2;
|
2015-11-12 22:09:04 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 19:13:50 +00:00
|
|
|
void adoptCurrencyFormat(int32_t widthIndex, NumberFormat *nfToAdopt) {
|
|
|
|
delete currencyFormats[widthIndex];
|
|
|
|
currencyFormats[widthIndex] = nfToAdopt;
|
|
|
|
}
|
2015-11-19 22:33:25 +00:00
|
|
|
const NumberFormat *getCurrencyFormat(UMeasureFormatWidth width) const {
|
|
|
|
return currencyFormats[getRegularWidth(width)];
|
2014-02-10 19:13:50 +00:00
|
|
|
}
|
2014-02-16 02:10:51 +00:00
|
|
|
void adoptIntegerFormat(NumberFormat *nfToAdopt) {
|
|
|
|
delete integerFormat;
|
|
|
|
integerFormat = nfToAdopt;
|
|
|
|
}
|
|
|
|
const NumberFormat *getIntegerFormat() const {
|
|
|
|
return integerFormat;
|
|
|
|
}
|
2014-02-10 19:13:50 +00:00
|
|
|
void adoptNumericDateFormatters(NumericDateFormatters *formattersToAdopt) {
|
|
|
|
delete numericDateFormatters;
|
|
|
|
numericDateFormatters = formattersToAdopt;
|
|
|
|
}
|
|
|
|
const NumericDateFormatters *getNumericDateFormatters() const {
|
|
|
|
return numericDateFormatters;
|
|
|
|
}
|
2015-12-16 04:41:33 +00:00
|
|
|
|
2014-02-04 00:29:17 +00:00
|
|
|
private:
|
2017-10-05 00:47:08 +00:00
|
|
|
NumberFormat* currencyFormats[WIDTH_INDEX_COUNT];
|
|
|
|
NumberFormat* integerFormat;
|
|
|
|
NumericDateFormatters* numericDateFormatters;
|
2017-09-29 23:04:34 +00:00
|
|
|
|
2014-02-10 19:13:50 +00:00
|
|
|
MeasureFormatCacheData(const MeasureFormatCacheData &other);
|
|
|
|
MeasureFormatCacheData &operator=(const MeasureFormatCacheData &other);
|
2014-02-04 00:29:17 +00:00
|
|
|
};
|
|
|
|
|
2017-10-05 00:47:08 +00:00
|
|
|
MeasureFormatCacheData::MeasureFormatCacheData()
|
|
|
|
: integerFormat(nullptr), numericDateFormatters(nullptr) {
|
2017-09-29 23:04:34 +00:00
|
|
|
// Please update MEAS_UNIT_COUNT if it gets out of sync with the true count!
|
|
|
|
U_ASSERT(MEAS_UNIT_COUNT == MeasureUnit::getIndexCount());
|
|
|
|
|
2015-11-14 00:31:21 +00:00
|
|
|
for (int32_t i = 0; i < WIDTH_INDEX_COUNT; ++i) {
|
|
|
|
widthFallback[i] = UMEASFMT_WIDTH_COUNT;
|
|
|
|
}
|
2017-10-05 00:47:08 +00:00
|
|
|
memset(&patterns[0][0][0], 0, sizeof(patterns));
|
|
|
|
memset(&dnams[0][0], 0, sizeof(dnams));
|
|
|
|
memset(currencyFormats, 0, sizeof(currencyFormats));
|
2014-02-10 19:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MeasureFormatCacheData::~MeasureFormatCacheData() {
|
2014-08-28 22:13:45 +00:00
|
|
|
for (int32_t i = 0; i < UPRV_LENGTHOF(currencyFormats); ++i) {
|
2014-02-10 19:13:50 +00:00
|
|
|
delete currencyFormats[i];
|
|
|
|
}
|
2014-09-10 20:06:52 +00:00
|
|
|
for (int32_t i = 0; i < MEAS_UNIT_COUNT; ++i) {
|
|
|
|
for (int32_t j = 0; j < WIDTH_INDEX_COUNT; ++j) {
|
2015-12-31 21:12:07 +00:00
|
|
|
for (int32_t k = 0; k < PATTERN_COUNT; ++k) {
|
2015-12-16 04:41:33 +00:00
|
|
|
delete patterns[i][j][k];
|
|
|
|
}
|
2014-09-10 20:06:52 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-15 08:37:56 +00:00
|
|
|
// Note: the contents of 'dnams' are pointers into the resource bundle
|
2014-02-16 02:10:51 +00:00
|
|
|
delete integerFormat;
|
2014-02-10 19:13:50 +00:00
|
|
|
delete numericDateFormatters;
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static UBool isCurrency(const MeasureUnit &unit) {
|
|
|
|
return (uprv_strcmp(unit.getType(), "currency") == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static UBool getString(
|
|
|
|
const UResourceBundle *resource,
|
|
|
|
UnicodeString &result,
|
|
|
|
UErrorCode &status) {
|
|
|
|
int32_t len = 0;
|
|
|
|
const UChar *resStr = ures_getString(resource, &len, &status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
result.setTo(TRUE, resStr, len);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-11-12 22:09:04 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
static const UChar g_LOCALE_units[] = {
|
|
|
|
0x2F, 0x4C, 0x4F, 0x43, 0x41, 0x4C, 0x45, 0x2F,
|
|
|
|
0x75, 0x6E, 0x69, 0x74, 0x73
|
|
|
|
};
|
|
|
|
static const UChar gShort[] = { 0x53, 0x68, 0x6F, 0x72, 0x74 };
|
|
|
|
static const UChar gNarrow[] = { 0x4E, 0x61, 0x72, 0x72, 0x6F, 0x77 };
|
|
|
|
|
2015-11-16 23:50:58 +00:00
|
|
|
/**
|
|
|
|
* Sink for enumerating all of the measurement unit display names.
|
|
|
|
* Contains inner sink classes, each one corresponding to a type of resource table.
|
|
|
|
* The outer sink handles the top-level units, unitsNarrow, and unitsShort tables.
|
|
|
|
*
|
|
|
|
* More specific bundles (en_GB) are enumerated before their parents (en_001, en, root):
|
|
|
|
* Only store a value if it is still missing, that is, it has not been overridden.
|
|
|
|
*
|
|
|
|
* C++: Each inner sink class has a reference to the main outer sink.
|
|
|
|
* Java: Use non-static inner classes instead.
|
|
|
|
*/
|
2016-06-17 19:16:15 +00:00
|
|
|
struct UnitDataSink : public ResourceSink {
|
|
|
|
|
|
|
|
// Output data.
|
|
|
|
MeasureFormatCacheData &cacheData;
|
|
|
|
|
|
|
|
// Path to current data.
|
|
|
|
UMeasureFormatWidth width;
|
|
|
|
const char *type;
|
|
|
|
int32_t unitIndex;
|
|
|
|
|
|
|
|
UnitDataSink(MeasureFormatCacheData &outputData)
|
|
|
|
: cacheData(outputData),
|
|
|
|
width(UMEASFMT_WIDTH_COUNT), type(NULL), unitIndex(0) {}
|
|
|
|
~UnitDataSink();
|
|
|
|
|
|
|
|
void setFormatterIfAbsent(int32_t index, const ResourceValue &value,
|
|
|
|
int32_t minPlaceholders, UErrorCode &errorCode) {
|
2017-09-29 23:04:34 +00:00
|
|
|
U_ASSERT(unitIndex < MEAS_UNIT_COUNT);
|
|
|
|
U_ASSERT(width < WIDTH_INDEX_COUNT);
|
|
|
|
U_ASSERT(index < PATTERN_COUNT);
|
2016-09-15 08:37:56 +00:00
|
|
|
SimpleFormatter **patterns = &cacheData.patterns[unitIndex][width][0];
|
2016-06-17 19:16:15 +00:00
|
|
|
if (U_SUCCESS(errorCode) && patterns[index] == NULL) {
|
2016-09-15 08:37:56 +00:00
|
|
|
if (minPlaceholders >= 0) {
|
|
|
|
patterns[index] = new SimpleFormatter(
|
|
|
|
value.getUnicodeString(errorCode), minPlaceholders, 1, errorCode);
|
|
|
|
}
|
2015-12-16 04:41:33 +00:00
|
|
|
if (U_SUCCESS(errorCode) && patterns[index] == NULL) {
|
2016-06-17 19:16:15 +00:00
|
|
|
errorCode = U_MEMORY_ALLOCATION_ERROR;
|
2015-12-16 04:41:33 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-17 19:16:15 +00:00
|
|
|
}
|
2015-12-16 04:41:33 +00:00
|
|
|
|
2016-09-15 08:37:56 +00:00
|
|
|
void setDnamIfAbsent(const ResourceValue &value, UErrorCode& errorCode) {
|
2017-09-29 23:04:34 +00:00
|
|
|
U_ASSERT(unitIndex < MEAS_UNIT_COUNT);
|
|
|
|
U_ASSERT(width < WIDTH_INDEX_COUNT);
|
2016-09-15 08:37:56 +00:00
|
|
|
if (cacheData.dnams[unitIndex][width] == NULL) {
|
|
|
|
int32_t length;
|
|
|
|
cacheData.dnams[unitIndex][width] = value.getString(length, errorCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 19:16:15 +00:00
|
|
|
/**
|
|
|
|
* Consume a display pattern. For example,
|
|
|
|
* unitsShort/duration/hour contains other{"{0} hrs"}.
|
|
|
|
*/
|
|
|
|
void consumePattern(const char *key, const ResourceValue &value, UErrorCode &errorCode) {
|
|
|
|
if (U_FAILURE(errorCode)) { return; }
|
|
|
|
if (uprv_strcmp(key, "dnam") == 0) {
|
2016-09-15 08:37:56 +00:00
|
|
|
// The display name for the unit in the current width.
|
|
|
|
setDnamIfAbsent(value, errorCode);
|
2016-06-17 19:16:15 +00:00
|
|
|
} else if (uprv_strcmp(key, "per") == 0) {
|
|
|
|
// For example, "{0}/h".
|
2017-09-29 23:04:34 +00:00
|
|
|
setFormatterIfAbsent(PER_UNIT_INDEX, value, 1, errorCode);
|
2016-06-17 19:16:15 +00:00
|
|
|
} else {
|
|
|
|
// The key must be one of the plural form strings. For example:
|
|
|
|
// one{"{0} hr"}
|
|
|
|
// other{"{0} hrs"}
|
|
|
|
setFormatterIfAbsent(StandardPlural::indexFromString(key, errorCode), value, 0,
|
|
|
|
errorCode);
|
2015-11-16 23:50:58 +00:00
|
|
|
}
|
2016-06-17 19:16:15 +00:00
|
|
|
}
|
2015-11-16 23:50:58 +00:00
|
|
|
|
|
|
|
/**
|
2016-06-17 19:16:15 +00:00
|
|
|
* Consume a table of per-unit tables. For example,
|
2015-11-16 23:50:58 +00:00
|
|
|
* unitsShort/duration contains tables for duration-unit subtypes day & hour.
|
|
|
|
*/
|
2016-06-17 19:16:15 +00:00
|
|
|
void consumeSubtypeTable(const char *key, ResourceValue &value, UErrorCode &errorCode) {
|
|
|
|
if (U_FAILURE(errorCode)) { return; }
|
|
|
|
unitIndex = MeasureUnit::internalGetIndexForTypeAndSubtype(type, key);
|
|
|
|
if (unitIndex < 0) {
|
|
|
|
// TODO: How to handle unexpected data?
|
|
|
|
// See http://bugs.icu-project.org/trac/ticket/12597
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-23 16:20:59 +00:00
|
|
|
// We no longer handle units like "coordinate" here (which do not have plural variants)
|
|
|
|
if (value.getType() == URES_TABLE) {
|
2016-06-17 19:16:15 +00:00
|
|
|
// Units that have plural variants
|
|
|
|
ResourceTable patternTableTable = value.getTable(errorCode);
|
|
|
|
if (U_FAILURE(errorCode)) { return; }
|
|
|
|
for (int i = 0; patternTableTable.getKeyAndValue(i, key, value); ++i) {
|
|
|
|
consumePattern(key, value, errorCode);
|
2015-11-16 23:50:58 +00:00
|
|
|
}
|
2016-06-17 19:16:15 +00:00
|
|
|
} else {
|
|
|
|
// TODO: How to handle unexpected data?
|
|
|
|
// See http://bugs.icu-project.org/trac/ticket/12597
|
|
|
|
return;
|
2015-11-16 23:50:58 +00:00
|
|
|
}
|
2016-06-17 19:16:15 +00:00
|
|
|
}
|
2015-11-16 23:50:58 +00:00
|
|
|
|
|
|
|
/**
|
2016-06-17 19:16:15 +00:00
|
|
|
* Consume compound x-per-y display pattern. For example,
|
2015-11-16 23:50:58 +00:00
|
|
|
* unitsShort/compound/per may be "{0}/{1}".
|
|
|
|
*/
|
2016-06-17 19:16:15 +00:00
|
|
|
void consumeCompoundPattern(const char *key, const ResourceValue &value, UErrorCode &errorCode) {
|
|
|
|
if (U_SUCCESS(errorCode) && uprv_strcmp(key, "per") == 0) {
|
|
|
|
cacheData.perFormatters[width].
|
|
|
|
applyPatternMinMaxArguments(value.getUnicodeString(errorCode), 2, 2, errorCode);
|
2015-11-16 23:50:58 +00:00
|
|
|
}
|
2016-06-17 19:16:15 +00:00
|
|
|
}
|
2015-11-16 23:50:58 +00:00
|
|
|
|
|
|
|
/**
|
2016-06-17 19:16:15 +00:00
|
|
|
* Consume a table of unit type tables. For example,
|
2015-11-16 23:50:58 +00:00
|
|
|
* unitsShort contains tables for area & duration.
|
|
|
|
* It also contains a table for the compound/per pattern.
|
|
|
|
*/
|
2016-06-17 19:16:15 +00:00
|
|
|
void consumeUnitTypesTable(const char *key, ResourceValue &value, UErrorCode &errorCode) {
|
|
|
|
if (U_FAILURE(errorCode)) { return; }
|
|
|
|
if (uprv_strcmp(key, "currency") == 0) {
|
|
|
|
// Skip.
|
|
|
|
} else if (uprv_strcmp(key, "compound") == 0) {
|
|
|
|
if (!cacheData.hasPerFormatter(width)) {
|
|
|
|
ResourceTable compoundTable = value.getTable(errorCode);
|
|
|
|
if (U_FAILURE(errorCode)) { return; }
|
|
|
|
for (int i = 0; compoundTable.getKeyAndValue(i, key, value); ++i) {
|
|
|
|
consumeCompoundPattern(key, value, errorCode);
|
2015-11-16 23:50:58 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-23 16:20:59 +00:00
|
|
|
} else if (uprv_strcmp(key, "coordinate") == 0) {
|
|
|
|
// special handling but we need to determine what that is
|
2016-06-17 19:16:15 +00:00
|
|
|
} else {
|
|
|
|
type = key;
|
|
|
|
ResourceTable subtypeTable = value.getTable(errorCode);
|
|
|
|
if (U_FAILURE(errorCode)) { return; }
|
|
|
|
for (int i = 0; subtypeTable.getKeyAndValue(i, key, value); ++i) {
|
|
|
|
consumeSubtypeTable(key, value, errorCode);
|
|
|
|
}
|
2015-11-16 23:50:58 +00:00
|
|
|
}
|
2016-06-17 19:16:15 +00:00
|
|
|
}
|
2015-11-16 23:50:58 +00:00
|
|
|
|
2016-06-17 19:16:15 +00:00
|
|
|
void consumeAlias(const char *key, const ResourceValue &value, UErrorCode &errorCode) {
|
2015-11-16 23:50:58 +00:00
|
|
|
// Handle aliases like
|
|
|
|
// units:alias{"/LOCALE/unitsShort"}
|
|
|
|
// which should only occur in the root bundle.
|
|
|
|
UMeasureFormatWidth sourceWidth = widthFromKey(key);
|
|
|
|
if (sourceWidth == UMEASFMT_WIDTH_COUNT) {
|
|
|
|
// Alias from something we don't care about.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
UMeasureFormatWidth targetWidth = widthFromAlias(value, errorCode);
|
|
|
|
if (targetWidth == UMEASFMT_WIDTH_COUNT) {
|
|
|
|
// We do not recognize what to fall back to.
|
|
|
|
errorCode = U_INVALID_FORMAT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Check that we do not fall back to another fallback.
|
|
|
|
if (cacheData.widthFallback[targetWidth] != UMEASFMT_WIDTH_COUNT) {
|
|
|
|
errorCode = U_INVALID_FORMAT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cacheData.widthFallback[sourceWidth] = targetWidth;
|
|
|
|
}
|
2016-06-17 19:16:15 +00:00
|
|
|
|
|
|
|
void consumeTable(const char *key, ResourceValue &value, UErrorCode &errorCode) {
|
2015-11-16 23:50:58 +00:00
|
|
|
if (U_SUCCESS(errorCode) && (width = widthFromKey(key)) != UMEASFMT_WIDTH_COUNT) {
|
2016-06-17 19:16:15 +00:00
|
|
|
ResourceTable unitTypesTable = value.getTable(errorCode);
|
|
|
|
if (U_FAILURE(errorCode)) { return; }
|
|
|
|
for (int i = 0; unitTypesTable.getKeyAndValue(i, key, value); ++i) {
|
|
|
|
consumeUnitTypesTable(key, value, errorCode);
|
|
|
|
}
|
2015-11-16 23:50:58 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-12 22:09:04 +00:00
|
|
|
|
|
|
|
static UMeasureFormatWidth widthFromKey(const char *key) {
|
|
|
|
if (uprv_strncmp(key, "units", 5) == 0) {
|
|
|
|
key += 5;
|
|
|
|
if (*key == 0) {
|
|
|
|
return UMEASFMT_WIDTH_WIDE;
|
|
|
|
} else if (uprv_strcmp(key, "Short") == 0) {
|
|
|
|
return UMEASFMT_WIDTH_SHORT;
|
|
|
|
} else if (uprv_strcmp(key, "Narrow") == 0) {
|
|
|
|
return UMEASFMT_WIDTH_NARROW;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return UMEASFMT_WIDTH_COUNT;
|
|
|
|
}
|
|
|
|
|
2015-11-18 00:18:28 +00:00
|
|
|
static UMeasureFormatWidth widthFromAlias(const ResourceValue &value, UErrorCode &errorCode) {
|
2015-11-12 22:09:04 +00:00
|
|
|
int32_t length;
|
|
|
|
const UChar *s = value.getAliasString(length, errorCode);
|
|
|
|
// For example: "/LOCALE/unitsShort"
|
|
|
|
if (U_SUCCESS(errorCode) && length >= 13 && u_memcmp(s, g_LOCALE_units, 13) == 0) {
|
|
|
|
s += 13;
|
|
|
|
length -= 13;
|
|
|
|
if (*s == 0) {
|
|
|
|
return UMEASFMT_WIDTH_WIDE;
|
|
|
|
} else if (u_strCompare(s, length, gShort, 5, FALSE) == 0) {
|
|
|
|
return UMEASFMT_WIDTH_SHORT;
|
|
|
|
} else if (u_strCompare(s, length, gNarrow, 6, FALSE) == 0) {
|
|
|
|
return UMEASFMT_WIDTH_NARROW;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return UMEASFMT_WIDTH_COUNT;
|
|
|
|
}
|
|
|
|
|
2016-06-17 19:16:15 +00:00
|
|
|
virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/,
|
|
|
|
UErrorCode &errorCode) {
|
|
|
|
// Main entry point to sink
|
|
|
|
ResourceTable widthsTable = value.getTable(errorCode);
|
|
|
|
if (U_FAILURE(errorCode)) { return; }
|
|
|
|
for (int i = 0; widthsTable.getKeyAndValue(i, key, value); ++i) {
|
|
|
|
if (value.getType() == URES_ALIAS) {
|
|
|
|
consumeAlias(key, value, errorCode);
|
|
|
|
} else {
|
|
|
|
consumeTable(key, value, errorCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-12 22:09:04 +00:00
|
|
|
};
|
|
|
|
|
2015-11-16 23:50:58 +00:00
|
|
|
// Virtual destructors must be defined out of line.
|
2015-11-12 22:09:04 +00:00
|
|
|
UnitDataSink::~UnitDataSink() {}
|
|
|
|
|
|
|
|
} // namespace
|
2014-02-04 00:29:17 +00:00
|
|
|
|
2014-02-10 19:13:50 +00:00
|
|
|
static UBool loadMeasureUnitData(
|
2014-02-04 00:29:17 +00:00
|
|
|
const UResourceBundle *resource,
|
2014-02-10 19:13:50 +00:00
|
|
|
MeasureFormatCacheData &cacheData,
|
2014-02-04 00:29:17 +00:00
|
|
|
UErrorCode &status) {
|
2015-11-14 00:31:21 +00:00
|
|
|
UnitDataSink sink(cacheData);
|
2016-06-17 19:16:15 +00:00
|
|
|
ures_getAllItemsWithFallback(resource, "", sink, status);
|
2014-02-04 00:29:17 +00:00
|
|
|
return U_SUCCESS(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
static UnicodeString loadNumericDateFormatterPattern(
|
|
|
|
const UResourceBundle *resource,
|
|
|
|
const char *pattern,
|
|
|
|
UErrorCode &status) {
|
|
|
|
UnicodeString result;
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
CharString chs;
|
|
|
|
chs.append("durationUnits", status)
|
|
|
|
.append("/", status).append(pattern, status);
|
|
|
|
LocalUResourceBundlePointer patternBundle(
|
|
|
|
ures_getByKeyWithFallback(
|
|
|
|
resource,
|
|
|
|
chs.data(),
|
|
|
|
NULL,
|
|
|
|
&status));
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
getString(patternBundle.getAlias(), result, status);
|
|
|
|
// Replace 'h' with 'H'
|
|
|
|
int32_t len = result.length();
|
|
|
|
UChar *buffer = result.getBuffer(len);
|
|
|
|
for (int32_t i = 0; i < len; ++i) {
|
|
|
|
if (buffer[i] == 0x68) { // 'h'
|
|
|
|
buffer[i] = 0x48; // 'H'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.releaseBuffer(len);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NumericDateFormatters *loadNumericDateFormatters(
|
|
|
|
const UResourceBundle *resource,
|
|
|
|
UErrorCode &status) {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
NumericDateFormatters *result = new NumericDateFormatters(
|
|
|
|
loadNumericDateFormatterPattern(resource, "hm", status),
|
|
|
|
loadNumericDateFormatterPattern(resource, "ms", status),
|
|
|
|
loadNumericDateFormatterPattern(resource, "hms", status),
|
|
|
|
status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete result;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-08-20 21:46:02 +00:00
|
|
|
template<> U_I18N_API
|
|
|
|
const MeasureFormatCacheData *LocaleCacheKey<MeasureFormatCacheData>::createObject(
|
|
|
|
const void * /*unused*/, UErrorCode &status) const {
|
|
|
|
const char *localeId = fLoc.getName();
|
2014-09-11 10:16:22 +00:00
|
|
|
LocalUResourceBundlePointer unitsBundle(ures_open(U_ICUDATA_UNIT, localeId, &status));
|
2014-02-04 00:29:17 +00:00
|
|
|
static UNumberFormatStyle currencyStyles[] = {
|
|
|
|
UNUM_CURRENCY_PLURAL, UNUM_CURRENCY_ISO, UNUM_CURRENCY};
|
2014-11-20 00:27:17 +00:00
|
|
|
LocalPointer<MeasureFormatCacheData> result(new MeasureFormatCacheData(), status);
|
2014-02-04 00:29:17 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-02-10 19:13:50 +00:00
|
|
|
if (!loadMeasureUnitData(
|
2014-09-11 10:16:22 +00:00
|
|
|
unitsBundle.getAlias(),
|
2014-02-10 19:13:50 +00:00
|
|
|
*result,
|
2014-02-04 00:29:17 +00:00
|
|
|
status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-02-10 19:13:50 +00:00
|
|
|
result->adoptNumericDateFormatters(loadNumericDateFormatters(
|
2014-12-11 16:10:49 +00:00
|
|
|
unitsBundle.getAlias(), status));
|
2014-02-04 00:29:17 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-02-10 19:13:50 +00:00
|
|
|
for (int32_t i = 0; i < WIDTH_INDEX_COUNT; ++i) {
|
2016-06-17 19:16:15 +00:00
|
|
|
// NumberFormat::createInstance can erase warning codes from status, so pass it
|
|
|
|
// a separate status instance
|
|
|
|
UErrorCode localStatus = U_ZERO_ERROR;
|
2014-02-10 19:13:50 +00:00
|
|
|
result->adoptCurrencyFormat(i, NumberFormat::createInstance(
|
2016-06-17 19:16:15 +00:00
|
|
|
localeId, currencyStyles[i], localStatus));
|
|
|
|
if (localStatus != U_ZERO_ERROR) {
|
|
|
|
status = localStatus;
|
|
|
|
}
|
2014-02-04 00:29:17 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2014-02-16 02:10:51 +00:00
|
|
|
NumberFormat *inf = NumberFormat::createInstance(
|
|
|
|
localeId, UNUM_DECIMAL, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
inf->setMaximumFractionDigits(0);
|
|
|
|
DecimalFormat *decfmt = dynamic_cast<DecimalFormat *>(inf);
|
|
|
|
if (decfmt != NULL) {
|
|
|
|
decfmt->setRoundingMode(DecimalFormat::kRoundDown);
|
|
|
|
}
|
|
|
|
result->adoptIntegerFormat(inf);
|
2014-08-20 21:46:02 +00:00
|
|
|
result->addRef();
|
2014-02-04 00:29:17 +00:00
|
|
|
return result.orphan();
|
|
|
|
}
|
|
|
|
|
2014-03-12 00:12:32 +00:00
|
|
|
static UBool isTimeUnit(const MeasureUnit &mu, const char *tu) {
|
2014-03-12 18:48:27 +00:00
|
|
|
return uprv_strcmp(mu.getType(), "duration") == 0 &&
|
|
|
|
uprv_strcmp(mu.getSubtype(), tu) == 0;
|
2014-03-12 00:12:32 +00:00
|
|
|
}
|
|
|
|
|
2014-03-11 23:14:05 +00:00
|
|
|
// Converts a composite measure into hours-minutes-seconds and stores at hms
|
|
|
|
// array. [0] is hours; [1] is minutes; [2] is seconds. Returns a bit map of
|
|
|
|
// units found: 1=hours, 2=minutes, 4=seconds. For example, if measures
|
|
|
|
// contains hours-minutes, this function would return 3.
|
|
|
|
//
|
|
|
|
// If measures cannot be converted into hours, minutes, seconds or if amounts
|
|
|
|
// are negative, or if hours, minutes, seconds are out of order, returns 0.
|
2014-02-04 00:29:17 +00:00
|
|
|
static int32_t toHMS(
|
|
|
|
const Measure *measures,
|
|
|
|
int32_t measureCount,
|
|
|
|
Formattable *hms,
|
|
|
|
UErrorCode &status) {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int32_t result = 0;
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-03-11 17:40:23 +00:00
|
|
|
// We use copy constructor to ensure that both sides of equality operator
|
|
|
|
// are instances of MeasureUnit base class and not a subclass. Otherwise,
|
|
|
|
// operator== will immediately return false.
|
2014-02-04 00:29:17 +00:00
|
|
|
for (int32_t i = 0; i < measureCount; ++i) {
|
2014-03-12 00:12:32 +00:00
|
|
|
if (isTimeUnit(measures[i].getUnit(), "hour")) {
|
2014-02-20 22:41:13 +00:00
|
|
|
// hour must come first
|
|
|
|
if (result >= 1) {
|
2014-02-04 00:29:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
hms[0] = measures[i].getNumber();
|
2014-02-20 22:41:13 +00:00
|
|
|
if (hms[0].getDouble() < 0.0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-04 00:29:17 +00:00
|
|
|
result |= 1;
|
2014-03-12 00:12:32 +00:00
|
|
|
} else if (isTimeUnit(measures[i].getUnit(), "minute")) {
|
2014-02-20 22:41:13 +00:00
|
|
|
// minute must come after hour
|
|
|
|
if (result >= 2) {
|
2014-02-04 00:29:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
hms[1] = measures[i].getNumber();
|
2014-02-20 22:41:13 +00:00
|
|
|
if (hms[1].getDouble() < 0.0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-04 00:29:17 +00:00
|
|
|
result |= 2;
|
2014-03-12 00:12:32 +00:00
|
|
|
} else if (isTimeUnit(measures[i].getUnit(), "second")) {
|
2014-02-20 22:41:13 +00:00
|
|
|
// second must come after hour and minute
|
|
|
|
if (result >= 4) {
|
2014-02-04 00:29:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
hms[2] = measures[i].getNumber();
|
2014-02-20 22:41:13 +00:00
|
|
|
if (hms[2].getDouble() < 0.0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-04 00:29:17 +00:00
|
|
|
result |= 4;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-04-28 00:12:33 +00:00
|
|
|
|
2014-02-04 00:29:17 +00:00
|
|
|
MeasureFormat::MeasureFormat(
|
|
|
|
const Locale &locale, UMeasureFormatWidth w, UErrorCode &status)
|
2014-02-10 19:13:50 +00:00
|
|
|
: cache(NULL),
|
|
|
|
numberFormat(NULL),
|
|
|
|
pluralRules(NULL),
|
|
|
|
width(w),
|
|
|
|
listFormatter(NULL) {
|
|
|
|
initMeasureFormat(locale, w, NULL, status);
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MeasureFormat::MeasureFormat(
|
|
|
|
const Locale &locale,
|
|
|
|
UMeasureFormatWidth w,
|
|
|
|
NumberFormat *nfToAdopt,
|
|
|
|
UErrorCode &status)
|
2014-02-10 19:13:50 +00:00
|
|
|
: cache(NULL),
|
|
|
|
numberFormat(NULL),
|
|
|
|
pluralRules(NULL),
|
|
|
|
width(w),
|
|
|
|
listFormatter(NULL) {
|
|
|
|
initMeasureFormat(locale, w, nfToAdopt, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
MeasureFormat::MeasureFormat(const MeasureFormat &other) :
|
|
|
|
Format(other),
|
|
|
|
cache(other.cache),
|
|
|
|
numberFormat(other.numberFormat),
|
|
|
|
pluralRules(other.pluralRules),
|
|
|
|
width(other.width),
|
|
|
|
listFormatter(NULL) {
|
|
|
|
cache->addRef();
|
|
|
|
numberFormat->addRef();
|
|
|
|
pluralRules->addRef();
|
2015-12-16 22:29:21 +00:00
|
|
|
if (other.listFormatter != NULL) {
|
|
|
|
listFormatter = new ListFormatter(*other.listFormatter);
|
|
|
|
}
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MeasureFormat &MeasureFormat::operator=(const MeasureFormat &other) {
|
|
|
|
if (this == &other) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Format::operator=(other);
|
2014-02-10 19:13:50 +00:00
|
|
|
SharedObject::copyPtr(other.cache, cache);
|
|
|
|
SharedObject::copyPtr(other.numberFormat, numberFormat);
|
|
|
|
SharedObject::copyPtr(other.pluralRules, pluralRules);
|
2014-02-04 00:29:17 +00:00
|
|
|
width = other.width;
|
2014-02-10 19:13:50 +00:00
|
|
|
delete listFormatter;
|
2015-12-16 22:29:21 +00:00
|
|
|
if (other.listFormatter != NULL) {
|
|
|
|
listFormatter = new ListFormatter(*other.listFormatter);
|
|
|
|
} else {
|
|
|
|
listFormatter = NULL;
|
|
|
|
}
|
2014-02-04 00:29:17 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-02-10 19:13:50 +00:00
|
|
|
MeasureFormat::MeasureFormat() :
|
|
|
|
cache(NULL),
|
|
|
|
numberFormat(NULL),
|
|
|
|
pluralRules(NULL),
|
2015-06-09 19:07:53 +00:00
|
|
|
width(UMEASFMT_WIDTH_SHORT),
|
2014-02-10 19:13:50 +00:00
|
|
|
listFormatter(NULL) {
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MeasureFormat::~MeasureFormat() {
|
2014-02-10 19:13:50 +00:00
|
|
|
if (cache != NULL) {
|
|
|
|
cache->removeRef();
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
2014-02-10 19:13:50 +00:00
|
|
|
if (numberFormat != NULL) {
|
|
|
|
numberFormat->removeRef();
|
|
|
|
}
|
|
|
|
if (pluralRules != NULL) {
|
|
|
|
pluralRules->removeRef();
|
|
|
|
}
|
|
|
|
delete listFormatter;
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UBool MeasureFormat::operator==(const Format &other) const {
|
2014-03-11 17:40:23 +00:00
|
|
|
if (this == &other) { // Same object, equal
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if (!Format::operator==(other)) {
|
2014-02-04 00:29:17 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2014-03-11 17:40:23 +00:00
|
|
|
const MeasureFormat &rhs = static_cast<const MeasureFormat &>(other);
|
2014-02-10 19:13:50 +00:00
|
|
|
|
|
|
|
// Note: Since the ListFormatter depends only on Locale and width, we
|
|
|
|
// don't have to check it here.
|
|
|
|
|
2014-02-04 00:29:17 +00:00
|
|
|
// differing widths aren't equivalent
|
2014-03-11 17:40:23 +00:00
|
|
|
if (width != rhs.width) {
|
2014-02-04 00:29:17 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2014-02-10 19:13:50 +00:00
|
|
|
// Width the same check locales.
|
|
|
|
// We don't need to check locales if both objects have same cache.
|
2014-03-11 17:40:23 +00:00
|
|
|
if (cache != rhs.cache) {
|
2014-02-10 19:13:50 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
const char *localeId = getLocaleID(status);
|
2014-03-11 17:40:23 +00:00
|
|
|
const char *rhsLocaleId = rhs.getLocaleID(status);
|
2014-02-10 19:13:50 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
// On failure, assume not equal
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (uprv_strcmp(localeId, rhsLocaleId) != 0) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
2014-02-10 19:13:50 +00:00
|
|
|
// Locales same, check NumberFormat if shared data differs.
|
|
|
|
return (
|
2014-03-11 17:40:23 +00:00
|
|
|
numberFormat == rhs.numberFormat ||
|
|
|
|
**numberFormat == **rhs.numberFormat);
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Format *MeasureFormat::clone() const {
|
|
|
|
return new MeasureFormat(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString &MeasureFormat::format(
|
|
|
|
const Formattable &obj,
|
|
|
|
UnicodeString &appendTo,
|
|
|
|
FieldPosition &pos,
|
|
|
|
UErrorCode &status) const {
|
|
|
|
if (U_FAILURE(status)) return appendTo;
|
|
|
|
if (obj.getType() == Formattable::kObject) {
|
|
|
|
const UObject* formatObj = obj.getObject();
|
|
|
|
const Measure* amount = dynamic_cast<const Measure*>(formatObj);
|
|
|
|
if (amount != NULL) {
|
2014-02-16 02:10:51 +00:00
|
|
|
return formatMeasure(
|
|
|
|
*amount, **numberFormat, appendTo, pos, status);
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeasureFormat::parseObject(
|
|
|
|
const UnicodeString & /*source*/,
|
|
|
|
Formattable & /*result*/,
|
|
|
|
ParsePosition& /*pos*/) const {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-05 20:52:28 +00:00
|
|
|
UnicodeString &MeasureFormat::formatMeasurePerUnit(
|
|
|
|
const Measure &measure,
|
2014-09-10 20:06:52 +00:00
|
|
|
const MeasureUnit &perUnit,
|
|
|
|
UnicodeString &appendTo,
|
|
|
|
FieldPosition &pos,
|
|
|
|
UErrorCode &status) const {
|
2014-12-05 20:52:28 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return appendTo;
|
|
|
|
}
|
2017-12-22 00:02:01 +00:00
|
|
|
bool isResolved = false;
|
|
|
|
MeasureUnit resolvedUnit =
|
|
|
|
MeasureUnit::resolveUnitPerUnit(measure.getUnit(), perUnit, &isResolved);
|
|
|
|
if (isResolved) {
|
|
|
|
Measure newMeasure(measure.getNumber(), new MeasureUnit(resolvedUnit), status);
|
2014-12-05 20:52:28 +00:00
|
|
|
return formatMeasure(
|
|
|
|
newMeasure, **numberFormat, appendTo, pos, status);
|
|
|
|
}
|
2014-09-10 20:06:52 +00:00
|
|
|
FieldPosition fpos(pos.getField());
|
2014-12-05 20:52:28 +00:00
|
|
|
UnicodeString result;
|
|
|
|
int32_t offset = withPerUnitAndAppend(
|
|
|
|
formatMeasure(
|
|
|
|
measure, **numberFormat, result, fpos, status),
|
2014-09-10 20:06:52 +00:00
|
|
|
perUnit,
|
|
|
|
appendTo,
|
|
|
|
status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
if (fpos.getBeginIndex() != 0 || fpos.getEndIndex() != 0) {
|
|
|
|
pos.setBeginIndex(fpos.getBeginIndex() + offset);
|
|
|
|
pos.setEndIndex(fpos.getEndIndex() + offset);
|
|
|
|
}
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
|
2014-02-04 00:29:17 +00:00
|
|
|
UnicodeString &MeasureFormat::formatMeasures(
|
|
|
|
const Measure *measures,
|
|
|
|
int32_t measureCount,
|
|
|
|
UnicodeString &appendTo,
|
|
|
|
FieldPosition &pos,
|
|
|
|
UErrorCode &status) const {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
if (measureCount == 0) {
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
if (measureCount == 1) {
|
2014-02-16 02:10:51 +00:00
|
|
|
return formatMeasure(measures[0], **numberFormat, appendTo, pos, status);
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
|
|
|
if (width == UMEASFMT_WIDTH_NUMERIC) {
|
|
|
|
Formattable hms[3];
|
|
|
|
int32_t bitMap = toHMS(measures, measureCount, hms, status);
|
|
|
|
if (bitMap > 0) {
|
|
|
|
return formatNumeric(hms, bitMap, appendTo, status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pos.getField() != FieldPosition::DONT_CARE) {
|
|
|
|
return formatMeasuresSlowTrack(
|
2014-02-10 19:13:50 +00:00
|
|
|
measures, measureCount, appendTo, pos, status);
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
|
|
|
UnicodeString *results = new UnicodeString[measureCount];
|
|
|
|
if (results == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
for (int32_t i = 0; i < measureCount; ++i) {
|
2014-02-16 02:10:51 +00:00
|
|
|
const NumberFormat *nf = cache->getIntegerFormat();
|
|
|
|
if (i == measureCount - 1) {
|
|
|
|
nf = numberFormat->get();
|
|
|
|
}
|
|
|
|
formatMeasure(
|
|
|
|
measures[i],
|
|
|
|
*nf,
|
|
|
|
results[i],
|
|
|
|
pos,
|
|
|
|
status);
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
2014-02-10 19:13:50 +00:00
|
|
|
listFormatter->format(results, measureCount, appendTo, status);
|
2016-06-17 19:16:15 +00:00
|
|
|
delete [] results;
|
2014-02-04 00:29:17 +00:00
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
|
2016-09-15 08:37:56 +00:00
|
|
|
UnicodeString MeasureFormat::getUnitDisplayName(const MeasureUnit& unit, UErrorCode& /*status*/) const {
|
|
|
|
UMeasureFormatWidth width = getRegularWidth(this->width);
|
|
|
|
const UChar* const* styleToDnam = cache->dnams[unit.getIndex()];
|
|
|
|
const UChar* dnam = styleToDnam[width];
|
|
|
|
if (dnam == NULL) {
|
|
|
|
int32_t fallbackWidth = cache->widthFallback[width];
|
|
|
|
dnam = styleToDnam[fallbackWidth];
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString result;
|
|
|
|
if (dnam == NULL) {
|
|
|
|
result.setToBogus();
|
|
|
|
} else {
|
|
|
|
result.setTo(dnam, -1);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-02-04 00:29:17 +00:00
|
|
|
void MeasureFormat::initMeasureFormat(
|
2014-02-10 19:13:50 +00:00
|
|
|
const Locale &locale,
|
|
|
|
UMeasureFormatWidth w,
|
|
|
|
NumberFormat *nfToAdopt,
|
|
|
|
UErrorCode &status) {
|
|
|
|
static const char *listStyles[] = {"unit", "unit-short", "unit-narrow"};
|
2014-02-10 22:52:01 +00:00
|
|
|
LocalPointer<NumberFormat> nf(nfToAdopt);
|
2014-02-04 00:29:17 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const char *name = locale.getName();
|
|
|
|
setLocaleIDs(name, name);
|
|
|
|
|
2014-08-20 21:46:02 +00:00
|
|
|
UnifiedCache::getByLocale(locale, cache, status);
|
|
|
|
if (U_FAILURE(status)) {
|
2014-02-10 19:13:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-22 23:48:56 +00:00
|
|
|
const SharedPluralRules *pr = PluralRules::createSharedInstance(
|
|
|
|
locale, UPLURAL_TYPE_CARDINAL, status);
|
2014-02-04 00:29:17 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-05-22 23:48:56 +00:00
|
|
|
SharedObject::copyPtr(pr, pluralRules);
|
|
|
|
pr->removeRef();
|
2014-03-11 17:40:23 +00:00
|
|
|
if (nf.isNull()) {
|
2014-05-22 23:48:56 +00:00
|
|
|
const SharedNumberFormat *shared = NumberFormat::createSharedInstance(
|
|
|
|
locale, UNUM_DECIMAL, status);
|
2014-02-10 19:13:50 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-05-22 23:48:56 +00:00
|
|
|
SharedObject::copyPtr(shared, numberFormat);
|
|
|
|
shared->removeRef();
|
2014-02-10 19:13:50 +00:00
|
|
|
} else {
|
2014-02-10 22:52:01 +00:00
|
|
|
adoptNumberFormat(nf.orphan(), status);
|
2014-02-10 19:13:50 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
width = w;
|
|
|
|
delete listFormatter;
|
|
|
|
listFormatter = ListFormatter::createInstance(
|
|
|
|
locale,
|
2015-11-19 22:33:25 +00:00
|
|
|
listStyles[getRegularWidth(width)],
|
2014-02-10 19:13:50 +00:00
|
|
|
status);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeasureFormat::adoptNumberFormat(
|
|
|
|
NumberFormat *nfToAdopt, UErrorCode &status) {
|
2014-02-10 22:55:18 +00:00
|
|
|
LocalPointer<NumberFormat> nf(nfToAdopt);
|
2014-02-10 19:13:50 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2014-02-04 00:29:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-02-10 22:55:18 +00:00
|
|
|
SharedNumberFormat *shared = new SharedNumberFormat(nf.getAlias());
|
2014-02-10 19:13:50 +00:00
|
|
|
if (shared == NULL) {
|
2014-02-04 00:29:17 +00:00
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2014-02-10 22:55:18 +00:00
|
|
|
nf.orphan();
|
2014-02-10 19:13:50 +00:00
|
|
|
SharedObject::copyPtr(shared, numberFormat);
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UBool MeasureFormat::setMeasureFormatLocale(const Locale &locale, UErrorCode &status) {
|
|
|
|
if (U_FAILURE(status) || locale == getLocale(status)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-02-10 19:13:50 +00:00
|
|
|
initMeasureFormat(locale, width, NULL, status);
|
2014-02-04 00:29:17 +00:00
|
|
|
return U_SUCCESS(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
const NumberFormat &MeasureFormat::getNumberFormat() const {
|
2014-02-10 19:13:50 +00:00
|
|
|
return **numberFormat;
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const PluralRules &MeasureFormat::getPluralRules() const {
|
2014-02-10 19:13:50 +00:00
|
|
|
return **pluralRules;
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Locale MeasureFormat::getLocale(UErrorCode &status) const {
|
|
|
|
return Format::getLocale(ULOC_VALID_LOCALE, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *MeasureFormat::getLocaleID(UErrorCode &status) const {
|
|
|
|
return Format::getLocaleID(ULOC_VALID_LOCALE, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString &MeasureFormat::formatMeasure(
|
|
|
|
const Measure &measure,
|
2014-02-16 02:10:51 +00:00
|
|
|
const NumberFormat &nf,
|
2014-02-04 00:29:17 +00:00
|
|
|
UnicodeString &appendTo,
|
|
|
|
FieldPosition &pos,
|
|
|
|
UErrorCode &status) const {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
const Formattable& amtNumber = measure.getNumber();
|
|
|
|
const MeasureUnit& amtUnit = measure.getUnit();
|
|
|
|
if (isCurrency(amtUnit)) {
|
|
|
|
UChar isoCode[4];
|
|
|
|
u_charsToUChars(amtUnit.getSubtype(), isoCode, 4);
|
2015-11-19 22:33:25 +00:00
|
|
|
return cache->getCurrencyFormat(width)->format(
|
2014-02-04 00:29:17 +00:00
|
|
|
new CurrencyAmount(amtNumber, isoCode, status),
|
|
|
|
appendTo,
|
|
|
|
pos,
|
|
|
|
status);
|
|
|
|
}
|
2015-12-16 04:41:33 +00:00
|
|
|
UnicodeString formattedNumber;
|
|
|
|
StandardPlural::Form pluralForm = QuantityFormatter::selectPlural(
|
|
|
|
amtNumber, nf, **pluralRules, formattedNumber, pos, status);
|
2016-01-23 05:20:23 +00:00
|
|
|
const SimpleFormatter *formatter = getPluralFormatter(amtUnit, width, pluralForm, status);
|
2015-12-16 04:41:33 +00:00
|
|
|
return QuantityFormatter::format(*formatter, formattedNumber, appendTo, pos, status);
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
|
|
|
|
2014-03-11 23:14:05 +00:00
|
|
|
// Formats hours-minutes-seconds as 5:37:23 or similar.
|
2014-02-04 00:29:17 +00:00
|
|
|
UnicodeString &MeasureFormat::formatNumeric(
|
|
|
|
const Formattable *hms, // always length 3
|
|
|
|
int32_t bitMap, // 1=hourset, 2=minuteset, 4=secondset
|
|
|
|
UnicodeString &appendTo,
|
|
|
|
UErrorCode &status) const {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
UDate millis =
|
2014-02-20 22:41:13 +00:00
|
|
|
(UDate) (((uprv_trunc(hms[0].getDouble(status)) * 60.0
|
|
|
|
+ uprv_trunc(hms[1].getDouble(status))) * 60.0
|
|
|
|
+ uprv_trunc(hms[2].getDouble(status))) * 1000.0);
|
2014-02-04 00:29:17 +00:00
|
|
|
switch (bitMap) {
|
|
|
|
case 5: // hs
|
|
|
|
case 7: // hms
|
|
|
|
return formatNumeric(
|
|
|
|
millis,
|
2014-02-10 19:13:50 +00:00
|
|
|
cache->getNumericDateFormatters()->hourMinuteSecond,
|
2014-02-04 00:29:17 +00:00
|
|
|
UDAT_SECOND_FIELD,
|
|
|
|
hms[2],
|
|
|
|
appendTo,
|
|
|
|
status);
|
|
|
|
break;
|
|
|
|
case 6: // ms
|
|
|
|
return formatNumeric(
|
|
|
|
millis,
|
2014-02-10 19:13:50 +00:00
|
|
|
cache->getNumericDateFormatters()->minuteSecond,
|
2014-02-04 00:29:17 +00:00
|
|
|
UDAT_SECOND_FIELD,
|
|
|
|
hms[2],
|
|
|
|
appendTo,
|
|
|
|
status);
|
|
|
|
break;
|
|
|
|
case 3: // hm
|
|
|
|
return formatNumeric(
|
|
|
|
millis,
|
2014-02-10 19:13:50 +00:00
|
|
|
cache->getNumericDateFormatters()->hourMinute,
|
2014-02-04 00:29:17 +00:00
|
|
|
UDAT_MINUTE_FIELD,
|
|
|
|
hms[1],
|
|
|
|
appendTo,
|
|
|
|
status);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
status = U_INTERNAL_PROGRAM_ERROR;
|
|
|
|
return appendTo;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
|
2014-02-20 22:41:13 +00:00
|
|
|
static void appendRange(
|
|
|
|
const UnicodeString &src,
|
|
|
|
int32_t start,
|
|
|
|
int32_t end,
|
|
|
|
UnicodeString &dest) {
|
|
|
|
dest.append(src, start, end - start);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void appendRange(
|
|
|
|
const UnicodeString &src,
|
|
|
|
int32_t end,
|
|
|
|
UnicodeString &dest) {
|
|
|
|
dest.append(src, end, src.length() - end);
|
|
|
|
}
|
|
|
|
|
2014-03-11 23:14:05 +00:00
|
|
|
// Formats time like 5:37:23
|
2014-02-04 00:29:17 +00:00
|
|
|
UnicodeString &MeasureFormat::formatNumeric(
|
2014-02-20 22:41:13 +00:00
|
|
|
UDate date, // Time since epoch 1:30:00 would be 5400000
|
|
|
|
const DateFormat &dateFmt, // h:mm, m:ss, or h:mm:ss
|
2014-03-11 23:14:05 +00:00
|
|
|
UDateFormatField smallestField, // seconds in 5:37:23.5
|
|
|
|
const Formattable &smallestAmount, // 23.5 for 5:37:23.5
|
2014-02-04 00:29:17 +00:00
|
|
|
UnicodeString &appendTo,
|
|
|
|
UErrorCode &status) const {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return appendTo;
|
|
|
|
}
|
2014-02-20 22:41:13 +00:00
|
|
|
// Format the smallest amount with this object's NumberFormat
|
2014-02-04 00:29:17 +00:00
|
|
|
UnicodeString smallestAmountFormatted;
|
2014-02-20 22:41:13 +00:00
|
|
|
|
|
|
|
// We keep track of the integer part of smallest amount so that
|
|
|
|
// we can replace it later so that we get '0:00:09.3' instead of
|
|
|
|
// '0:00:9.3'
|
|
|
|
FieldPosition intFieldPosition(UNUM_INTEGER_FIELD);
|
2014-02-10 19:13:50 +00:00
|
|
|
(*numberFormat)->format(
|
2014-02-20 22:41:13 +00:00
|
|
|
smallestAmount, smallestAmountFormatted, intFieldPosition, status);
|
|
|
|
if (
|
|
|
|
intFieldPosition.getBeginIndex() == 0 &&
|
|
|
|
intFieldPosition.getEndIndex() == 0) {
|
|
|
|
status = U_INTERNAL_PROGRAM_ERROR;
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format time. draft becomes something like '5:30:45'
|
2018-03-01 00:58:47 +00:00
|
|
|
// #13606: DateFormat is not thread-safe, but MeasureFormat advertises itself as thread-safe.
|
2014-02-04 00:29:17 +00:00
|
|
|
FieldPosition smallestFieldPosition(smallestField);
|
|
|
|
UnicodeString draft;
|
2018-03-01 00:58:47 +00:00
|
|
|
static UMutex dateFmtMutex = U_MUTEX_INITIALIZER;
|
|
|
|
umtx_lock(&dateFmtMutex);
|
2014-02-04 00:29:17 +00:00
|
|
|
dateFmt.format(date, draft, smallestFieldPosition, status);
|
2018-03-01 00:58:47 +00:00
|
|
|
umtx_unlock(&dateFmtMutex);
|
2014-02-20 22:41:13 +00:00
|
|
|
|
|
|
|
// If we find field for smallest amount replace it with the formatted
|
|
|
|
// smallest amount from above taking care to replace the integer part
|
|
|
|
// with what is in original time. For example, If smallest amount
|
|
|
|
// is 9.35s and the formatted time is 0:00:09 then 9.35 becomes 09.35
|
|
|
|
// and replacing yields 0:00:09.35
|
2014-02-04 00:29:17 +00:00
|
|
|
if (smallestFieldPosition.getBeginIndex() != 0 ||
|
2014-02-20 22:41:13 +00:00
|
|
|
smallestFieldPosition.getEndIndex() != 0) {
|
|
|
|
appendRange(draft, 0, smallestFieldPosition.getBeginIndex(), appendTo);
|
|
|
|
appendRange(
|
|
|
|
smallestAmountFormatted,
|
|
|
|
0,
|
|
|
|
intFieldPosition.getBeginIndex(),
|
|
|
|
appendTo);
|
|
|
|
appendRange(
|
|
|
|
draft,
|
|
|
|
smallestFieldPosition.getBeginIndex(),
|
|
|
|
smallestFieldPosition.getEndIndex(),
|
|
|
|
appendTo);
|
|
|
|
appendRange(
|
|
|
|
smallestAmountFormatted,
|
|
|
|
intFieldPosition.getEndIndex(),
|
|
|
|
appendTo);
|
|
|
|
appendRange(
|
2014-02-04 00:29:17 +00:00
|
|
|
draft,
|
|
|
|
smallestFieldPosition.getEndIndex(),
|
2014-02-20 22:41:13 +00:00
|
|
|
appendTo);
|
2014-02-04 00:29:17 +00:00
|
|
|
} else {
|
|
|
|
appendTo.append(draft);
|
|
|
|
}
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
|
2016-01-23 05:20:23 +00:00
|
|
|
const SimpleFormatter *MeasureFormat::getFormatterOrNull(
|
2015-12-16 04:41:33 +00:00
|
|
|
const MeasureUnit &unit, UMeasureFormatWidth width, int32_t index) const {
|
2015-11-19 22:33:25 +00:00
|
|
|
width = getRegularWidth(width);
|
2017-09-29 23:04:34 +00:00
|
|
|
SimpleFormatter *const (*unitPatterns)[PATTERN_COUNT] = &cache->patterns[unit.getIndex()][0];
|
2015-12-16 04:41:33 +00:00
|
|
|
if (unitPatterns[width][index] != NULL) {
|
|
|
|
return unitPatterns[width][index];
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
2015-11-19 22:33:25 +00:00
|
|
|
int32_t fallbackWidth = cache->widthFallback[width];
|
2015-12-16 04:41:33 +00:00
|
|
|
if (fallbackWidth != UMEASFMT_WIDTH_COUNT && unitPatterns[fallbackWidth][index] != NULL) {
|
|
|
|
return unitPatterns[fallbackWidth][index];
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-01-23 05:20:23 +00:00
|
|
|
const SimpleFormatter *MeasureFormat::getFormatter(
|
2015-12-16 04:41:33 +00:00
|
|
|
const MeasureUnit &unit, UMeasureFormatWidth width, int32_t index,
|
|
|
|
UErrorCode &errorCode) const {
|
|
|
|
if (U_FAILURE(errorCode)) {
|
|
|
|
return NULL;
|
2014-09-10 20:06:52 +00:00
|
|
|
}
|
2016-01-23 05:20:23 +00:00
|
|
|
const SimpleFormatter *pattern = getFormatterOrNull(unit, width, index);
|
2015-12-16 04:41:33 +00:00
|
|
|
if (pattern == NULL) {
|
|
|
|
errorCode = U_MISSING_RESOURCE_ERROR;
|
2014-09-10 20:06:52 +00:00
|
|
|
}
|
2015-12-16 04:41:33 +00:00
|
|
|
return pattern;
|
|
|
|
}
|
|
|
|
|
2016-01-23 05:20:23 +00:00
|
|
|
const SimpleFormatter *MeasureFormat::getPluralFormatter(
|
2015-12-16 04:41:33 +00:00
|
|
|
const MeasureUnit &unit, UMeasureFormatWidth width, int32_t index,
|
|
|
|
UErrorCode &errorCode) const {
|
|
|
|
if (U_FAILURE(errorCode)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (index != StandardPlural::OTHER) {
|
2016-01-23 05:20:23 +00:00
|
|
|
const SimpleFormatter *pattern = getFormatterOrNull(unit, width, index);
|
2015-12-16 04:41:33 +00:00
|
|
|
if (pattern != NULL) {
|
|
|
|
return pattern;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return getFormatter(unit, width, StandardPlural::OTHER, errorCode);
|
2014-09-10 20:06:52 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 05:20:23 +00:00
|
|
|
const SimpleFormatter *MeasureFormat::getPerFormatter(
|
2015-11-19 22:33:25 +00:00
|
|
|
UMeasureFormatWidth width,
|
2014-09-10 20:06:52 +00:00
|
|
|
UErrorCode &status) const {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-11-19 22:33:25 +00:00
|
|
|
width = getRegularWidth(width);
|
2016-01-23 05:20:23 +00:00
|
|
|
const SimpleFormatter * perFormatters = cache->perFormatters;
|
|
|
|
if (perFormatters[width].getArgumentLimit() == 2) {
|
2015-11-19 22:33:25 +00:00
|
|
|
return &perFormatters[width];
|
2014-09-10 20:06:52 +00:00
|
|
|
}
|
2015-11-19 22:33:25 +00:00
|
|
|
int32_t fallbackWidth = cache->widthFallback[width];
|
2015-11-14 00:31:21 +00:00
|
|
|
if (fallbackWidth != UMEASFMT_WIDTH_COUNT &&
|
2016-01-23 05:20:23 +00:00
|
|
|
perFormatters[fallbackWidth].getArgumentLimit() == 2) {
|
2015-11-14 00:31:21 +00:00
|
|
|
return &perFormatters[fallbackWidth];
|
2014-09-10 20:06:52 +00:00
|
|
|
}
|
|
|
|
status = U_MISSING_RESOURCE_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-05 20:52:28 +00:00
|
|
|
int32_t MeasureFormat::withPerUnitAndAppend(
|
2014-09-10 20:06:52 +00:00
|
|
|
const UnicodeString &formatted,
|
|
|
|
const MeasureUnit &perUnit,
|
|
|
|
UnicodeString &appendTo,
|
|
|
|
UErrorCode &status) const {
|
|
|
|
int32_t offset = -1;
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return offset;
|
|
|
|
}
|
2017-09-29 23:04:34 +00:00
|
|
|
const SimpleFormatter *perUnitFormatter = getFormatterOrNull(perUnit, width, PER_UNIT_INDEX);
|
2014-09-10 20:06:52 +00:00
|
|
|
if (perUnitFormatter != NULL) {
|
|
|
|
const UnicodeString *params[] = {&formatted};
|
2014-12-05 20:52:28 +00:00
|
|
|
perUnitFormatter->formatAndAppend(
|
2014-09-10 20:06:52 +00:00
|
|
|
params,
|
|
|
|
UPRV_LENGTHOF(params),
|
|
|
|
appendTo,
|
|
|
|
&offset,
|
|
|
|
1,
|
|
|
|
status);
|
|
|
|
return offset;
|
|
|
|
}
|
2016-01-23 05:20:23 +00:00
|
|
|
const SimpleFormatter *perFormatter = getPerFormatter(width, status);
|
|
|
|
const SimpleFormatter *pattern =
|
2015-12-16 04:41:33 +00:00
|
|
|
getPluralFormatter(perUnit, width, StandardPlural::ONE, status);
|
2014-09-10 20:06:52 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return offset;
|
|
|
|
}
|
2016-01-23 05:20:23 +00:00
|
|
|
UnicodeString perUnitString = pattern->getTextWithNoArguments();
|
2015-12-16 04:41:33 +00:00
|
|
|
perUnitString.trim();
|
2014-09-10 20:06:52 +00:00
|
|
|
const UnicodeString *params[] = {&formatted, &perUnitString};
|
2014-12-05 20:52:28 +00:00
|
|
|
perFormatter->formatAndAppend(
|
2014-09-10 20:06:52 +00:00
|
|
|
params,
|
|
|
|
UPRV_LENGTHOF(params),
|
|
|
|
appendTo,
|
|
|
|
&offset,
|
|
|
|
1,
|
|
|
|
status);
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2014-02-04 00:29:17 +00:00
|
|
|
UnicodeString &MeasureFormat::formatMeasuresSlowTrack(
|
|
|
|
const Measure *measures,
|
|
|
|
int32_t measureCount,
|
|
|
|
UnicodeString& appendTo,
|
|
|
|
FieldPosition& pos,
|
|
|
|
UErrorCode& status) const {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
FieldPosition dontCare(FieldPosition::DONT_CARE);
|
|
|
|
FieldPosition fpos(pos.getField());
|
|
|
|
UnicodeString *results = new UnicodeString[measureCount];
|
|
|
|
int32_t fieldPositionFoundIndex = -1;
|
|
|
|
for (int32_t i = 0; i < measureCount; ++i) {
|
2014-02-16 02:10:51 +00:00
|
|
|
const NumberFormat *nf = cache->getIntegerFormat();
|
|
|
|
if (i == measureCount - 1) {
|
|
|
|
nf = numberFormat->get();
|
|
|
|
}
|
2014-02-04 00:29:17 +00:00
|
|
|
if (fieldPositionFoundIndex == -1) {
|
2014-02-16 02:10:51 +00:00
|
|
|
formatMeasure(measures[i], *nf, results[i], fpos, status);
|
2014-02-04 00:29:17 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete [] results;
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
if (fpos.getBeginIndex() != 0 || fpos.getEndIndex() != 0) {
|
|
|
|
fieldPositionFoundIndex = i;
|
|
|
|
}
|
|
|
|
} else {
|
2014-02-16 02:10:51 +00:00
|
|
|
formatMeasure(measures[i], *nf, results[i], dontCare, status);
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
int32_t offset;
|
2014-02-10 19:13:50 +00:00
|
|
|
listFormatter->format(
|
2014-02-04 00:29:17 +00:00
|
|
|
results,
|
|
|
|
measureCount,
|
|
|
|
appendTo,
|
|
|
|
fieldPositionFoundIndex,
|
|
|
|
offset,
|
|
|
|
status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete [] results;
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
if (offset != -1) {
|
|
|
|
pos.setBeginIndex(fpos.getBeginIndex() + offset);
|
|
|
|
pos.setEndIndex(fpos.getEndIndex() + offset);
|
|
|
|
}
|
|
|
|
delete [] results;
|
|
|
|
return appendTo;
|
|
|
|
}
|
2011-07-26 05:32:25 +00:00
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
MeasureFormat* U_EXPORT2 MeasureFormat::createCurrencyFormat(const Locale& locale,
|
2004-04-21 18:05:06 +00:00
|
|
|
UErrorCode& ec) {
|
|
|
|
CurrencyFormat* fmt = NULL;
|
|
|
|
if (U_SUCCESS(ec)) {
|
|
|
|
fmt = new CurrencyFormat(locale, ec);
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
delete fmt;
|
|
|
|
fmt = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt;
|
|
|
|
}
|
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
MeasureFormat* U_EXPORT2 MeasureFormat::createCurrencyFormat(UErrorCode& ec) {
|
2004-04-21 18:05:06 +00:00
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return MeasureFormat::createCurrencyFormat(Locale::getDefault(), ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|