2014-01-29 05:08:55 +00:00
|
|
|
/*
|
|
|
|
******************************************************************************
|
|
|
|
* Copyright (C) 2014, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
******************************************************************************
|
|
|
|
* quantityformatter.cpp
|
|
|
|
*/
|
|
|
|
#include "quantityformatter.h"
|
2014-01-29 22:57:06 +00:00
|
|
|
#include "simplepatternformatter.h"
|
2014-01-29 05:08:55 +00:00
|
|
|
#include "uassert.h"
|
|
|
|
#include "unicode/unistr.h"
|
|
|
|
#include "unicode/decimfmt.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
#include "plurrule_impl.h"
|
|
|
|
#include "unicode/plurrule.h"
|
|
|
|
#include "charstr.h"
|
|
|
|
#include "unicode/fmtable.h"
|
2014-02-04 00:29:17 +00:00
|
|
|
#include "unicode/fieldpos.h"
|
2014-01-29 05:08:55 +00:00
|
|
|
|
|
|
|
#define LENGTHOF(array) (int32_t)(sizeof(array) / sizeof((array)[0]))
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
// other must always be first.
|
|
|
|
static const char * const gPluralForms[] = {
|
|
|
|
"other", "zero", "one", "two", "few", "many"};
|
|
|
|
|
|
|
|
static int32_t getPluralIndex(const char *pluralForm) {
|
|
|
|
int32_t len = LENGTHOF(gPluralForms);
|
|
|
|
for (int32_t i = 0; i < len; ++i) {
|
|
|
|
if (uprv_strcmp(pluralForm, gPluralForms[i]) == 0) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
QuantityFormatter::QuantityFormatter() {
|
2014-01-29 22:57:06 +00:00
|
|
|
for (int32_t i = 0; i < LENGTHOF(formatters); ++i) {
|
|
|
|
formatters[i] = NULL;
|
2014-01-29 05:08:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QuantityFormatter::QuantityFormatter(const QuantityFormatter &other) {
|
2014-01-29 22:57:06 +00:00
|
|
|
for (int32_t i = 0; i < LENGTHOF(formatters); ++i) {
|
|
|
|
if (other.formatters[i] == NULL) {
|
|
|
|
formatters[i] = NULL;
|
2014-01-29 05:08:55 +00:00
|
|
|
} else {
|
2014-01-29 22:57:06 +00:00
|
|
|
formatters[i] = new SimplePatternFormatter(*other.formatters[i]);
|
2014-01-29 05:08:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QuantityFormatter &QuantityFormatter::operator=(
|
|
|
|
const QuantityFormatter& other) {
|
|
|
|
if (this == &other) {
|
|
|
|
return *this;
|
|
|
|
}
|
2014-01-29 22:57:06 +00:00
|
|
|
for (int32_t i = 0; i < LENGTHOF(formatters); ++i) {
|
|
|
|
delete formatters[i];
|
|
|
|
if (other.formatters[i] == NULL) {
|
|
|
|
formatters[i] = NULL;
|
2014-01-29 05:08:55 +00:00
|
|
|
} else {
|
2014-01-29 22:57:06 +00:00
|
|
|
formatters[i] = new SimplePatternFormatter(*other.formatters[i]);
|
2014-01-29 05:08:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
QuantityFormatter::~QuantityFormatter() {
|
2014-01-29 22:57:06 +00:00
|
|
|
for (int32_t i = 0; i < LENGTHOF(formatters); ++i) {
|
|
|
|
delete formatters[i];
|
2014-01-29 05:08:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuantityFormatter::reset() {
|
2014-01-29 22:57:06 +00:00
|
|
|
for (int32_t i = 0; i < LENGTHOF(formatters); ++i) {
|
|
|
|
delete formatters[i];
|
|
|
|
formatters[i] = NULL;
|
2014-01-29 05:08:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool QuantityFormatter::add(
|
|
|
|
const char *variant,
|
|
|
|
const UnicodeString &rawPattern,
|
|
|
|
UErrorCode &status) {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
int32_t pluralIndex = getPluralIndex(variant);
|
|
|
|
if (pluralIndex == -1) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-01-29 22:57:06 +00:00
|
|
|
SimplePatternFormatter *newFmt =
|
|
|
|
new SimplePatternFormatter(rawPattern);
|
|
|
|
if (newFmt == NULL) {
|
2014-01-29 05:08:55 +00:00
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-01-29 22:57:06 +00:00
|
|
|
if (newFmt->getPlaceholderCount() > 1) {
|
|
|
|
delete newFmt;
|
2014-01-29 05:08:55 +00:00
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-01-29 22:57:06 +00:00
|
|
|
delete formatters[pluralIndex];
|
|
|
|
formatters[pluralIndex] = newFmt;
|
2014-01-29 05:08:55 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-02-04 00:29:17 +00:00
|
|
|
UBool QuantityFormatter::isValid() const {
|
|
|
|
return formatters[0] != NULL;
|
|
|
|
}
|
|
|
|
|
2014-01-29 05:08:55 +00:00
|
|
|
UnicodeString &QuantityFormatter::format(
|
|
|
|
const Formattable& quantity,
|
|
|
|
const NumberFormat &fmt,
|
|
|
|
const PluralRules &rules,
|
|
|
|
UnicodeString &appendTo,
|
2014-02-04 00:29:17 +00:00
|
|
|
FieldPosition &pos,
|
2014-01-29 05:08:55 +00:00
|
|
|
UErrorCode &status) const {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
UnicodeString count;
|
|
|
|
const DecimalFormat *decFmt = dynamic_cast<const DecimalFormat *>(&fmt);
|
|
|
|
if (decFmt != NULL) {
|
|
|
|
FixedDecimal fd = decFmt->getFixedDecimal(quantity, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
count = rules.select(fd);
|
|
|
|
} else {
|
|
|
|
if (quantity.getType() == Formattable::kDouble) {
|
|
|
|
count = rules.select(quantity.getDouble());
|
|
|
|
} else if (quantity.getType() == Formattable::kLong) {
|
|
|
|
count = rules.select(quantity.getLong());
|
|
|
|
} else if (quantity.getType() == Formattable::kInt64) {
|
|
|
|
count = rules.select((double) quantity.getInt64());
|
|
|
|
} else {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CharString buffer;
|
|
|
|
buffer.appendInvariantChars(count, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
int32_t pluralIndex = getPluralIndex(buffer.data());
|
|
|
|
if (pluralIndex == -1) {
|
|
|
|
pluralIndex = 0;
|
|
|
|
}
|
2014-01-29 22:57:06 +00:00
|
|
|
const SimplePatternFormatter *pattern = formatters[pluralIndex];
|
2014-01-29 05:08:55 +00:00
|
|
|
if (pattern == NULL) {
|
2014-01-29 22:57:06 +00:00
|
|
|
pattern = formatters[0];
|
2014-01-29 05:08:55 +00:00
|
|
|
}
|
|
|
|
if (pattern == NULL) {
|
|
|
|
status = U_INVALID_STATE_ERROR;
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
UnicodeString formattedNumber;
|
2014-02-04 00:29:17 +00:00
|
|
|
FieldPosition fpos(pos.getField());
|
|
|
|
fmt.format(quantity, formattedNumber, fpos, status);
|
|
|
|
const UnicodeString *params[1] = {&formattedNumber};
|
|
|
|
int32_t offsets[1];
|
|
|
|
pattern->format(params, LENGTHOF(params), appendTo, offsets, LENGTHOF(offsets), status);
|
|
|
|
if (offsets[0] != -1) {
|
|
|
|
if (fpos.getBeginIndex() != 0 || fpos.getEndIndex() != 0) {
|
|
|
|
pos.setBeginIndex(fpos.getBeginIndex() + offsets[0]);
|
|
|
|
pos.setEndIndex(fpos.getEndIndex() + offsets[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return appendTo;
|
2014-01-29 05:08:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|