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"
|
2019-02-16 01:32:50 +00:00
|
|
|
#include "unicode/numberformatter.h"
|
|
|
|
#include "number_longnames.h"
|
2020-03-24 07:03:31 +00:00
|
|
|
#include "number_utypes.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
|
|
|
|
|
2020-03-24 07:03:31 +00:00
|
|
|
using number::impl::UFormattedNumberData;
|
|
|
|
|
2017-09-29 23:04:34 +00:00
|
|
|
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
|
2019-05-28 22:41:00 +00:00
|
|
|
UnicodeString hourMinute;
|
2014-02-10 19:13:50 +00:00
|
|
|
|
|
|
|
// formats like M:ss
|
2019-05-28 22:41:00 +00:00
|
|
|
UnicodeString minuteSecond;
|
2014-02-10 19:13:50 +00:00
|
|
|
|
|
|
|
// formats like H:mm:ss
|
2019-05-28 22:41:00 +00:00
|
|
|
UnicodeString 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,
|
2019-05-28 22:41:00 +00:00
|
|
|
const UnicodeString &hms) :
|
|
|
|
hourMinute(hm),
|
|
|
|
minuteSecond(ms),
|
|
|
|
hourMinuteSecond(hms) {
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-02-16 01:32:50 +00:00
|
|
|
static UNumberUnitWidth getUnitWidth(UMeasureFormatWidth width) {
|
|
|
|
switch (width) {
|
|
|
|
case UMEASFMT_WIDTH_WIDE:
|
|
|
|
return UNUM_UNIT_WIDTH_FULL_NAME;
|
|
|
|
case UMEASFMT_WIDTH_NARROW:
|
|
|
|
case UMEASFMT_WIDTH_NUMERIC:
|
|
|
|
return UNUM_UNIT_WIDTH_NARROW;
|
|
|
|
case UMEASFMT_WIDTH_SHORT:
|
|
|
|
default:
|
|
|
|
return UNUM_UNIT_WIDTH_SHORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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];
|
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
|
|
|
|
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) {
|
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(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];
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
2019-05-28 22:41:00 +00:00
|
|
|
loadNumericDateFormatterPattern(resource, "hms", status));
|
2014-02-04 00:29:17 +00:00
|
|
|
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
|
|
|
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),
|
2018-08-11 01:05:58 +00:00
|
|
|
fWidth(w),
|
2014-02-10 19:13:50 +00:00
|
|
|
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),
|
2018-08-11 01:05:58 +00:00
|
|
|
fWidth(w),
|
2014-02-10 19:13:50 +00:00
|
|
|
listFormatter(NULL) {
|
|
|
|
initMeasureFormat(locale, w, nfToAdopt, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
MeasureFormat::MeasureFormat(const MeasureFormat &other) :
|
|
|
|
Format(other),
|
|
|
|
cache(other.cache),
|
|
|
|
numberFormat(other.numberFormat),
|
|
|
|
pluralRules(other.pluralRules),
|
2018-08-11 01:05:58 +00:00
|
|
|
fWidth(other.fWidth),
|
2014-02-10 19:13:50 +00:00
|
|
|
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);
|
2018-08-11 01:05:58 +00:00
|
|
|
fWidth = other.fWidth;
|
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),
|
2018-08-11 01:05:58 +00:00
|
|
|
fWidth(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
|
2018-08-11 01:05:58 +00:00
|
|
|
if (fWidth != rhs.fWidth) {
|
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
|
|
|
}
|
|
|
|
|
2019-08-22 00:13:18 +00:00
|
|
|
MeasureFormat *MeasureFormat::clone() const {
|
2014-02-04 00:29:17 +00:00
|
|
|
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;
|
|
|
|
}
|
2019-02-16 01:32:50 +00:00
|
|
|
auto* df = dynamic_cast<const DecimalFormat*>(&getNumberFormatInternal());
|
|
|
|
if (df == nullptr) {
|
|
|
|
// Don't know how to handle other types of NumberFormat
|
|
|
|
status = U_UNSUPPORTED_ERROR;
|
2014-09-10 20:06:52 +00:00
|
|
|
return appendTo;
|
|
|
|
}
|
2020-03-24 07:03:31 +00:00
|
|
|
UFormattedNumberData result;
|
2019-02-16 01:32:50 +00:00
|
|
|
if (auto* lnf = df->toNumberFormatter(status)) {
|
2020-03-24 07:03:31 +00:00
|
|
|
result.quantity.setToDouble(measure.getNumber().getDouble(status));
|
|
|
|
lnf->unit(measure.getUnit())
|
2019-02-16 01:32:50 +00:00
|
|
|
.perUnit(perUnit)
|
|
|
|
.unitWidth(getUnitWidth(fWidth))
|
2020-03-24 07:03:31 +00:00
|
|
|
.formatImpl(&result, status);
|
2014-09-10 20:06:52 +00:00
|
|
|
}
|
2019-02-16 01:32:50 +00:00
|
|
|
DecimalFormat::fieldPositionHelper(result, pos, appendTo.length(), status);
|
|
|
|
appendTo.append(result.toTempString(status));
|
2014-09-10 20:06:52 +00:00
|
|
|
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
|
|
|
}
|
2018-08-11 01:05:58 +00:00
|
|
|
if (fWidth == UMEASFMT_WIDTH_NUMERIC) {
|
2014-02-04 00:29:17 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-02-16 01:32:50 +00:00
|
|
|
UnicodeString MeasureFormat::getUnitDisplayName(const MeasureUnit& unit, UErrorCode& status) const {
|
|
|
|
return number::impl::LongNameHandler::getUnitDisplayName(
|
|
|
|
getLocale(status),
|
|
|
|
unit,
|
|
|
|
getUnitWidth(fWidth),
|
|
|
|
status);
|
2016-09-15 08:37:56 +00:00
|
|
|
}
|
|
|
|
|
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()) {
|
2019-02-16 01:32:50 +00:00
|
|
|
// TODO: Clean this up
|
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;
|
|
|
|
}
|
|
|
|
}
|
2018-08-11 01:05:58 +00:00
|
|
|
fWidth = w;
|
2014-02-10 19:13:50 +00:00
|
|
|
delete listFormatter;
|
|
|
|
listFormatter = ListFormatter::createInstance(
|
|
|
|
locale,
|
2018-08-11 01:05:58 +00:00
|
|
|
listStyles[getRegularWidth(fWidth)],
|
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;
|
|
|
|
}
|
2018-08-11 01:05:58 +00:00
|
|
|
initMeasureFormat(locale, fWidth, NULL, status);
|
2014-02-04 00:29:17 +00:00
|
|
|
return U_SUCCESS(status);
|
|
|
|
}
|
|
|
|
|
2019-02-16 01:32:50 +00:00
|
|
|
const NumberFormat &MeasureFormat::getNumberFormatInternal() const {
|
2014-02-10 19:13:50 +00:00
|
|
|
return **numberFormat;
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 01:32:50 +00:00
|
|
|
const NumberFormat &MeasureFormat::getCurrencyFormatInternal() const {
|
|
|
|
return *cache->getCurrencyFormat(UMEASFMT_WIDTH_NARROW);
|
|
|
|
}
|
|
|
|
|
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);
|
2018-08-11 01:05:58 +00:00
|
|
|
return cache->getCurrencyFormat(fWidth)->format(
|
2014-02-04 00:29:17 +00:00
|
|
|
new CurrencyAmount(amtNumber, isoCode, status),
|
|
|
|
appendTo,
|
|
|
|
pos,
|
|
|
|
status);
|
|
|
|
}
|
2019-02-16 01:32:50 +00:00
|
|
|
auto* df = dynamic_cast<const DecimalFormat*>(&nf);
|
|
|
|
if (df == nullptr) {
|
2019-08-29 07:02:17 +00:00
|
|
|
// Handle other types of NumberFormat using the ICU 63 code, modified to
|
|
|
|
// get the unitPattern from LongNameHandler and handle fallback to OTHER.
|
|
|
|
UnicodeString formattedNumber;
|
|
|
|
StandardPlural::Form pluralForm = QuantityFormatter::selectPlural(
|
|
|
|
amtNumber, nf, **pluralRules, formattedNumber, pos, status);
|
|
|
|
UnicodeString pattern = number::impl::LongNameHandler::getUnitPattern(getLocale(status),
|
|
|
|
amtUnit, getUnitWidth(fWidth), pluralForm, status);
|
|
|
|
// The above handles fallback from other widths to short, and from other plural forms to OTHER
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
SimpleFormatter formatter(pattern, 0, 1, status);
|
|
|
|
return QuantityFormatter::format(formatter, formattedNumber, appendTo, pos, status);
|
2019-02-16 01:32:50 +00:00
|
|
|
}
|
2020-03-24 07:03:31 +00:00
|
|
|
UFormattedNumberData result;
|
2019-02-16 01:32:50 +00:00
|
|
|
if (auto* lnf = df->toNumberFormatter(status)) {
|
2020-03-24 07:03:31 +00:00
|
|
|
result.quantity.setToDouble(amtNumber.getDouble(status));
|
|
|
|
lnf->unit(amtUnit)
|
2019-02-16 01:32:50 +00:00
|
|
|
.unitWidth(getUnitWidth(fWidth))
|
2020-03-24 07:03:31 +00:00
|
|
|
.formatImpl(&result, status);
|
2019-02-16 01:32:50 +00:00
|
|
|
}
|
|
|
|
DecimalFormat::fieldPositionHelper(result, pos, appendTo.length(), status);
|
|
|
|
appendTo.append(result.toTempString(status));
|
|
|
|
return appendTo;
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 22:41:00 +00:00
|
|
|
|
|
|
|
// Formats numeric time duration as 5:00:47 or 3:54.
|
2014-02-04 00:29:17 +00:00
|
|
|
UnicodeString &MeasureFormat::formatNumeric(
|
2019-05-28 22:41:00 +00:00
|
|
|
const Formattable *hms, // always length 3
|
|
|
|
int32_t bitMap, // 1=hour set, 2=minute set, 4=second set
|
2014-02-04 00:29:17 +00:00
|
|
|
UnicodeString &appendTo,
|
|
|
|
UErrorCode &status) const {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return appendTo;
|
|
|
|
}
|
2019-05-28 22:41:00 +00:00
|
|
|
|
|
|
|
UnicodeString pattern;
|
|
|
|
|
|
|
|
double hours = hms[0].getDouble(status);
|
|
|
|
double minutes = hms[1].getDouble(status);
|
|
|
|
double seconds = hms[2].getDouble(status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All possible combinations: "h", "m", "s", "hm", "hs", "ms", "hms"
|
|
|
|
if (bitMap == 5 || bitMap == 7) { // "hms" & "hs" (we add minutes if "hs")
|
|
|
|
pattern = cache->getNumericDateFormatters()->hourMinuteSecond;
|
|
|
|
hours = uprv_trunc(hours);
|
|
|
|
minutes = uprv_trunc(minutes);
|
|
|
|
} else if (bitMap == 3) { // "hm"
|
|
|
|
pattern = cache->getNumericDateFormatters()->hourMinute;
|
|
|
|
hours = uprv_trunc(hours);
|
|
|
|
} else if (bitMap == 6) { // "ms"
|
|
|
|
pattern = cache->getNumericDateFormatters()->minuteSecond;
|
|
|
|
minutes = uprv_trunc(minutes);
|
|
|
|
} else { // h m s, handled outside formatNumeric. No value is also an error.
|
2014-02-20 22:41:13 +00:00
|
|
|
status = U_INTERNAL_PROGRAM_ERROR;
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
|
2019-05-28 22:41:00 +00:00
|
|
|
const DecimalFormat *numberFormatter = dynamic_cast<const DecimalFormat*>(numberFormat->get());
|
|
|
|
if (!numberFormatter) {
|
|
|
|
status = U_INTERNAL_PROGRAM_ERROR;
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
number::LocalizedNumberFormatter numberFormatter2;
|
|
|
|
if (auto* lnf = numberFormatter->toNumberFormatter(status)) {
|
|
|
|
numberFormatter2 = lnf->integerWidth(number::IntegerWidth::zeroFillTo(2));
|
2014-02-04 00:29:17 +00:00
|
|
|
} else {
|
2019-05-28 22:41:00 +00:00
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
FormattedStringBuilder fsb;
|
|
|
|
|
|
|
|
UBool protect = FALSE;
|
|
|
|
const int32_t patternLength = pattern.length();
|
|
|
|
for (int32_t i = 0; i < patternLength; i++) {
|
|
|
|
char16_t c = pattern[i];
|
|
|
|
|
|
|
|
// Also set the proper field in this switch
|
|
|
|
// We don't use DateFormat.Field because this is not a date / time, is a duration.
|
|
|
|
double value = 0;
|
|
|
|
switch (c) {
|
|
|
|
case u'H': value = hours; break;
|
|
|
|
case u'm': value = minutes; break;
|
|
|
|
case u's': value = seconds; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is not enough info to add Field(s) for the unit because all we have are plain
|
|
|
|
// text patterns. For example in "21:51" there is no text for something like "hour",
|
|
|
|
// while in something like "21h51" there is ("h"). But we can't really tell...
|
|
|
|
switch (c) {
|
|
|
|
case u'H':
|
|
|
|
case u'm':
|
|
|
|
case u's':
|
|
|
|
if (protect) {
|
2019-11-20 02:22:20 +00:00
|
|
|
fsb.appendChar16(c, kUndefinedField, status);
|
2019-05-28 22:41:00 +00:00
|
|
|
} else {
|
|
|
|
UnicodeString tmp;
|
|
|
|
if ((i + 1 < patternLength) && pattern[i + 1] == c) { // doubled
|
|
|
|
tmp = numberFormatter2.formatDouble(value, status).toString(status);
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
numberFormatter->format(value, tmp, status);
|
|
|
|
}
|
|
|
|
// TODO: Use proper Field
|
2019-11-20 02:22:20 +00:00
|
|
|
fsb.append(tmp, kUndefinedField, status);
|
2019-05-28 22:41:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case u'\'':
|
|
|
|
// '' is escaped apostrophe
|
|
|
|
if ((i + 1 < patternLength) && pattern[i + 1] == c) {
|
2019-11-20 02:22:20 +00:00
|
|
|
fsb.appendChar16(c, kUndefinedField, status);
|
2019-05-28 22:41:00 +00:00
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
protect = !protect;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2019-11-20 02:22:20 +00:00
|
|
|
fsb.appendChar16(c, kUndefinedField, status);
|
2019-05-28 22:41:00 +00:00
|
|
|
}
|
2014-02-04 00:29:17 +00:00
|
|
|
}
|
2019-05-28 22:41:00 +00:00
|
|
|
|
|
|
|
appendTo.append(fsb.toTempUnicodeString());
|
|
|
|
|
2014-02-04 00:29:17 +00:00
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
2019-02-16 01:32:50 +00:00
|
|
|
LocalArray<UnicodeString> results(new UnicodeString[measureCount], status);
|
2014-02-04 00:29:17 +00:00
|
|
|
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)) {
|
|
|
|
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(
|
2019-02-16 01:32:50 +00:00
|
|
|
results.getAlias(),
|
2014-02-04 00:29:17 +00:00
|
|
|
measureCount,
|
|
|
|
appendTo,
|
|
|
|
fieldPositionFoundIndex,
|
|
|
|
offset,
|
|
|
|
status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return appendTo;
|
|
|
|
}
|
2019-02-16 01:32:50 +00:00
|
|
|
// Fix up FieldPosition indexes if our field is found.
|
2020-08-14 10:48:42 +00:00
|
|
|
if (fieldPositionFoundIndex != -1 && offset != -1) {
|
2014-02-04 00:29:17 +00:00
|
|
|
pos.setBeginIndex(fpos.getBeginIndex() + offset);
|
|
|
|
pos.setEndIndex(fpos.getEndIndex() + offset);
|
|
|
|
}
|
|
|
|
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) {
|
2019-02-16 01:32:50 +00:00
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
return nullptr;
|
2004-04-21 18:05:06 +00:00
|
|
|
}
|
2019-02-16 01:32:50 +00:00
|
|
|
LocalPointer<CurrencyFormat> fmt(new CurrencyFormat(locale, ec), ec);
|
|
|
|
return fmt.orphan();
|
2004-04-21 18:05:06 +00:00
|
|
|
}
|
|
|
|
|
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)) {
|
2019-02-16 01:32:50 +00:00
|
|
|
return nullptr;
|
2004-04-21 18:05:06 +00:00
|
|
|
}
|
|
|
|
return MeasureFormat::createCurrencyFormat(Locale::getDefault(), ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|