scuffed-code/icu4c/source/i18n/unum.cpp

772 lines
22 KiB
C++
Raw Normal View History

1999-08-16 21:50:52 +00:00
/*
*******************************************************************************
* Copyright (C) 1996-2004, International Business Machines
* Corporation and others. All Rights Reserved.
*******************************************************************************
1999-08-16 21:50:52 +00:00
* Modification History:
*
* Date Name Description
* 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes
*******************************************************************************
*/
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/unum.h"
1999-08-16 21:50:52 +00:00
#include "unicode/uloc.h"
#include "unicode/numfmt.h"
#include "unicode/decimfmt.h"
#include "unicode/rbnf.h"
#include "unicode/ustring.h"
#include "unicode/fmtable.h"
#include "unicode/dcfmtsym.h"
#include "uassert.h"
#include "cpputils.h"
1999-08-16 21:50:52 +00:00
U_NAMESPACE_USE
1999-08-16 21:50:52 +00:00
U_CAPI UNumberFormat* U_EXPORT2
unum_open( UNumberFormatStyle style,
const UChar* pattern,
int32_t patternLength,
const char* locale,
UParseError* parseErr,
UErrorCode* status)
{
if(U_FAILURE(*status))
{
return 0;
}
UNumberFormat *retVal = 0;
switch(style) {
case UNUM_DECIMAL:
if(locale == 0)
retVal = (UNumberFormat*)NumberFormat::createInstance(*status);
else
retVal = (UNumberFormat*)NumberFormat::createInstance(Locale(locale),
*status);
break;
case UNUM_CURRENCY:
if(locale == 0)
retVal = (UNumberFormat*)NumberFormat::createCurrencyInstance(*status);
else
retVal = (UNumberFormat*)NumberFormat::createCurrencyInstance(Locale(locale),
*status);
break;
case UNUM_PERCENT:
if(locale == 0)
retVal = (UNumberFormat*)NumberFormat::createPercentInstance(*status);
else
retVal = (UNumberFormat*)NumberFormat::createPercentInstance(Locale(locale),
*status);
break;
case UNUM_SCIENTIFIC:
if(locale == 0)
retVal = (UNumberFormat*)NumberFormat::createScientificInstance(*status);
else
retVal = (UNumberFormat*)NumberFormat::createScientificInstance(Locale(locale),
*status);
break;
case UNUM_PATTERN_DECIMAL: {
UParseError tErr;
/* UnicodeString can handle the case when patternLength = -1. */
const UnicodeString pat(pattern, patternLength);
DecimalFormatSymbols *syms = 0;
if(parseErr==NULL){
parseErr = &tErr;
}
if(locale == 0)
syms = new DecimalFormatSymbols(*status);
else
syms = new DecimalFormatSymbols(Locale(locale), *status);
if(syms == 0) {
*status = U_MEMORY_ALLOCATION_ERROR;
return 0;
}
retVal = (UNumberFormat*)new DecimalFormat(pat, syms, *parseErr, *status);
if(retVal == 0) {
delete syms;
}
} break;
#if U_HAVE_RBNF
case UNUM_PATTERN_RULEBASED: {
UParseError tErr;
/* UnicodeString can handle the case when patternLength = -1. */
const UnicodeString pat(pattern, patternLength);
if(parseErr==NULL){
parseErr = &tErr;
}
retVal = (UNumberFormat*)new RuleBasedNumberFormat(pat, Locale(locale), *parseErr, *status);
} break;
case UNUM_SPELLOUT:
retVal = (UNumberFormat*)new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale(locale), *status);
break;
case UNUM_ORDINAL:
retVal = (UNumberFormat*)new RuleBasedNumberFormat(URBNF_ORDINAL, Locale(locale), *status);
break;
case UNUM_DURATION:
retVal = (UNumberFormat*)new RuleBasedNumberFormat(URBNF_DURATION, Locale(locale), *status);
break;
#endif
default:
*status = U_UNSUPPORTED_ERROR;
return 0;
}
if(retVal == 0) {
*status = U_MEMORY_ALLOCATION_ERROR;
}
return retVal;
1999-08-16 21:50:52 +00:00
}
U_CAPI void U_EXPORT2
1999-08-16 21:50:52 +00:00
unum_close(UNumberFormat* fmt)
{
delete (NumberFormat*) fmt;
1999-08-16 21:50:52 +00:00
}
U_CAPI UNumberFormat* U_EXPORT2
1999-08-16 21:50:52 +00:00
unum_clone(const UNumberFormat *fmt,
UErrorCode *status)
{
if(U_FAILURE(*status))
return 0;
Format *res = 0;
if (((const NumberFormat*)fmt)->getDynamicClassID() == DecimalFormat::getStaticClassID()) {
res = ((const DecimalFormat*)fmt)->clone();
} else {
U_ASSERT(((const NumberFormat*)fmt)->getDynamicClassID() == RuleBasedNumberFormat::getStaticClassID());
res = ((const RuleBasedNumberFormat*)fmt)->clone();
}
if(res == 0) {
*status = U_MEMORY_ALLOCATION_ERROR;
return 0;
}
return (UNumberFormat*) res;
1999-08-16 21:50:52 +00:00
}
U_CAPI int32_t U_EXPORT2
1999-08-16 21:50:52 +00:00
unum_format( const UNumberFormat* fmt,
int32_t number,
1999-08-16 21:50:52 +00:00
UChar* result,
int32_t resultLength,
1999-08-16 21:50:52 +00:00
UFieldPosition *pos,
UErrorCode* status)
{
return unum_formatInt64(fmt, number, result, resultLength, pos, status);
}
U_CAPI int32_t U_EXPORT2
unum_formatInt64(const UNumberFormat* fmt,
int64_t number,
UChar* result,
int32_t resultLength,
UFieldPosition *pos,
UErrorCode* status)
1999-08-16 21:50:52 +00:00
{
if(U_FAILURE(*status))
return -1;
UnicodeString res;
if(!(result==NULL && resultLength==0)) {
// NULL destination for pure preflighting: empty dummy string
// otherwise, alias the destination buffer
res.setTo(result, 0, resultLength);
}
FieldPosition fp;
if(pos != 0)
fp.setField(pos->field);
((NumberFormat*)fmt)->format(number, res, fp);
if(pos != 0) {
pos->beginIndex = fp.getBeginIndex();
pos->endIndex = fp.getEndIndex();
}
return res.extract(result, resultLength, *status);
1999-08-16 21:50:52 +00:00
}
U_CAPI int32_t U_EXPORT2
1999-08-16 21:50:52 +00:00
unum_formatDouble( const UNumberFormat* fmt,
double number,
UChar* result,
int32_t resultLength,
UFieldPosition *pos, /* 0 if ignore */
UErrorCode* status)
{
if(U_FAILURE(*status)) return -1;
1999-08-16 21:50:52 +00:00
UnicodeString res;
if(!(result==NULL && resultLength==0)) {
// NULL destination for pure preflighting: empty dummy string
// otherwise, alias the destination buffer
res.setTo(result, 0, resultLength);
}
1999-08-16 21:50:52 +00:00
FieldPosition fp;
if(pos != 0)
fp.setField(pos->field);
((NumberFormat*)fmt)->format(number, res, fp);
if(pos != 0) {
pos->beginIndex = fp.getBeginIndex();
pos->endIndex = fp.getEndIndex();
}
return res.extract(result, resultLength, *status);
1999-08-16 21:50:52 +00:00
}
static void
parseRes(Formattable& res,
const UNumberFormat* fmt,
1999-08-16 21:50:52 +00:00
const UChar* text,
int32_t textLength,
int32_t *parsePos /* 0 = start */,
UErrorCode *status)
{
if(U_FAILURE(*status))
return;
int32_t len = (textLength == -1 ? u_strlen(text) : textLength);
const UnicodeString src((UChar*)text, len, len);
ParsePosition pp;
if(parsePos != 0)
pp.setIndex(*parsePos);
((NumberFormat*)fmt)->parse(src, res, pp);
if(parsePos != 0) {
if(pp.getErrorIndex() == -1)
*parsePos = pp.getIndex();
else {
*parsePos = pp.getErrorIndex();
*status = U_PARSE_ERROR;
}
1999-08-16 21:50:52 +00:00
}
}
U_CAPI int32_t U_EXPORT2
unum_parse( const UNumberFormat* fmt,
const UChar* text,
int32_t textLength,
int32_t *parsePos /* 0 = start */,
UErrorCode *status)
{
Formattable res;
parseRes(res, fmt, text, textLength, parsePos, status);
return res.getLong(*status);
}
U_CAPI int64_t U_EXPORT2
unum_parseInt64( const UNumberFormat* fmt,
const UChar* text,
int32_t textLength,
int32_t *parsePos /* 0 = start */,
UErrorCode *status)
{
Formattable res;
parseRes(res, fmt, text, textLength, parsePos, status);
return res.getInt64(*status);
1999-08-16 21:50:52 +00:00
}
U_CAPI double U_EXPORT2
1999-08-16 21:50:52 +00:00
unum_parseDouble( const UNumberFormat* fmt,
const UChar* text,
int32_t textLength,
int32_t *parsePos /* 0 = start */,
UErrorCode *status)
{
Formattable res;
parseRes(res, fmt, text, textLength, parsePos, status);
return res.getDouble(*status);
1999-08-16 21:50:52 +00:00
}
U_CAPI const char* U_EXPORT2
1999-08-16 21:50:52 +00:00
unum_getAvailable(int32_t index)
{
return uloc_getAvailable(index);
1999-08-16 21:50:52 +00:00
}
U_CAPI int32_t U_EXPORT2
1999-08-16 21:50:52 +00:00
unum_countAvailable()
{
return uloc_countAvailable();
1999-08-16 21:50:52 +00:00
}
U_CAPI int32_t U_EXPORT2
1999-08-16 21:50:52 +00:00
unum_getAttribute(const UNumberFormat* fmt,
UNumberFormatAttribute attr)
{
if (((const NumberFormat*)fmt)->getDynamicClassID() == DecimalFormat::getStaticClassID()) {
const DecimalFormat* df = (const DecimalFormat*) fmt;
switch(attr) {
case UNUM_PARSE_INT_ONLY:
return df->isParseIntegerOnly();
case UNUM_GROUPING_USED:
return df->isGroupingUsed();
case UNUM_DECIMAL_ALWAYS_SHOWN:
return df->isDecimalSeparatorAlwaysShown();
case UNUM_MAX_INTEGER_DIGITS:
return df->getMaximumIntegerDigits();
case UNUM_MIN_INTEGER_DIGITS:
return df->getMinimumIntegerDigits();
case UNUM_INTEGER_DIGITS:
// TBD: what should this return?
return df->getMinimumIntegerDigits();
case UNUM_MAX_FRACTION_DIGITS:
return df->getMaximumFractionDigits();
case UNUM_MIN_FRACTION_DIGITS:
return df->getMinimumFractionDigits();
case UNUM_FRACTION_DIGITS:
// TBD: what should this return?
return df->getMinimumFractionDigits();
case UNUM_MAX_SIGNIFICANT_DIGITS:
return df->getMaximumSignificantDigits();
case UNUM_MIN_SIGNIFICANT_DIGITS:
return df->getMinimumSignificantDigits();
case UNUM_MULTIPLIER:
return df->getMultiplier();
case UNUM_GROUPING_SIZE:
return df->getGroupingSize();
case UNUM_ROUNDING_MODE:
return ((DecimalFormat*)df)->getRoundingMode(); // TODO remove cast!
case UNUM_FORMAT_WIDTH:
return ((DecimalFormat*)df)->getFormatWidth(); // TODO remove cast!
/** The position at which padding will take place. */
case UNUM_PADDING_POSITION:
return ((DecimalFormat*)df)->getPadPosition(); // TODO remove cast!
default:
break;
}
} else {
U_ASSERT(((const NumberFormat*)fmt)->getDynamicClassID() == RuleBasedNumberFormat::getStaticClassID());
if (attr == UNUM_LENIENT_PARSE) {
return ((const RuleBasedNumberFormat*)fmt)->isLenient();
}
}
return -1;
1999-08-16 21:50:52 +00:00
}
U_CAPI void U_EXPORT2
1999-08-16 21:50:52 +00:00
unum_setAttribute( UNumberFormat* fmt,
UNumberFormatAttribute attr,
int32_t newValue)
{
if (((NumberFormat*)fmt)->getDynamicClassID() == DecimalFormat::getStaticClassID()) {
DecimalFormat* df = (DecimalFormat*) fmt;
switch(attr) {
case UNUM_PARSE_INT_ONLY:
df->setParseIntegerOnly((UBool)newValue);
break;
case UNUM_GROUPING_USED:
df->setGroupingUsed((UBool)newValue);
break;
case UNUM_DECIMAL_ALWAYS_SHOWN:
df->setDecimalSeparatorAlwaysShown((UBool)newValue);
break;
case UNUM_MAX_INTEGER_DIGITS:
df->setMaximumIntegerDigits(newValue);
break;
case UNUM_MIN_INTEGER_DIGITS:
df->setMinimumIntegerDigits(newValue);
break;
case UNUM_INTEGER_DIGITS:
df->setMinimumIntegerDigits(newValue);
df->setMaximumIntegerDigits(newValue);
break;
case UNUM_MAX_FRACTION_DIGITS:
df->setMaximumFractionDigits(newValue);
break;
case UNUM_MIN_FRACTION_DIGITS:
df->setMinimumFractionDigits(newValue);
break;
case UNUM_FRACTION_DIGITS:
df->setMinimumFractionDigits(newValue);
df->setMaximumFractionDigits(newValue);
break;
case UNUM_MAX_SIGNIFICANT_DIGITS:
df->setMaximumSignificantDigits(newValue);
break;
case UNUM_MIN_SIGNIFICANT_DIGITS:
df->setMinimumSignificantDigits(newValue);
break;
case UNUM_MULTIPLIER:
df->setMultiplier(newValue);
break;
case UNUM_GROUPING_SIZE:
df->setGroupingSize(newValue);
break;
case UNUM_ROUNDING_MODE:
df->setRoundingMode((DecimalFormat::ERoundingMode)newValue);
break;
case UNUM_FORMAT_WIDTH:
df->setFormatWidth(newValue);
break;
case UNUM_PADDING_POSITION:
/** The position at which padding will take place. */
df->setPadPosition((DecimalFormat::EPadPosition)newValue);
break;
case UNUM_SECONDARY_GROUPING_SIZE:
df->setSecondaryGroupingSize(newValue);
break;
default:
/* Shouldn't get here anyway */
break;
}
} else {
U_ASSERT(((NumberFormat*)fmt)->getDynamicClassID() == RuleBasedNumberFormat::getStaticClassID());
if (attr == UNUM_LENIENT_PARSE) {
((RuleBasedNumberFormat*)fmt)->setLenient((UBool)newValue);
}
}
1999-08-16 21:50:52 +00:00
}
U_CAPI double U_EXPORT2
1999-08-16 21:50:52 +00:00
unum_getDoubleAttribute(const UNumberFormat* fmt,
UNumberFormatAttribute attr)
{
if (((const NumberFormat*)fmt)->getDynamicClassID() == DecimalFormat::getStaticClassID() &&
attr == UNUM_ROUNDING_INCREMENT) {
return ((DecimalFormat*)fmt)->getRoundingIncrement(); // TODO: cast to const DecimalFormat*!
} else {
return -1.0;
}
1999-08-16 21:50:52 +00:00
}
U_CAPI void U_EXPORT2
1999-08-16 21:50:52 +00:00
unum_setDoubleAttribute( UNumberFormat* fmt,
UNumberFormatAttribute attr,
double newValue)
{
if (((NumberFormat*)fmt)->getDynamicClassID() == DecimalFormat::getStaticClassID() &&
attr == UNUM_ROUNDING_INCREMENT) {
((DecimalFormat*)fmt)->setRoundingIncrement(newValue);
}
1999-08-16 21:50:52 +00:00
}
U_CAPI int32_t U_EXPORT2
unum_getTextAttribute(const UNumberFormat* fmt,
1999-08-16 21:50:52 +00:00
UNumberFormatTextAttribute tag,
UChar* result,
int32_t resultLength,
UErrorCode* status)
1999-08-16 21:50:52 +00:00
{
if(U_FAILURE(*status))
return -1;
UnicodeString res;
if(!(result==NULL && resultLength==0)) {
// NULL destination for pure preflighting: empty dummy string
// otherwise, alias the destination buffer
res.setTo(result, 0, resultLength);
}
if (((const NumberFormat*)fmt)->getDynamicClassID() == DecimalFormat::getStaticClassID()) {
const DecimalFormat* df = (const DecimalFormat*) fmt;
switch(tag) {
case UNUM_POSITIVE_PREFIX:
df->getPositivePrefix(res);
break;
case UNUM_POSITIVE_SUFFIX:
df->getPositiveSuffix(res);
break;
case UNUM_NEGATIVE_PREFIX:
df->getNegativePrefix(res);
break;
case UNUM_NEGATIVE_SUFFIX:
df->getNegativeSuffix(res);
break;
case UNUM_PADDING_CHARACTER:
res = ((DecimalFormat*) df)->getPadCharacterString(); // TODO: remove cast!
break;
case UNUM_CURRENCY_CODE:
res = UnicodeString(df->getCurrency());
break;
default:
*status = U_UNSUPPORTED_ERROR;
return -1;
}
} else {
U_ASSERT(((const NumberFormat*)fmt)->getDynamicClassID() == RuleBasedNumberFormat::getStaticClassID());
const RuleBasedNumberFormat* rbnf = (const RuleBasedNumberFormat*)fmt;
if (tag == UNUM_DEFAULT_RULESET) {
res = ((RuleBasedNumberFormat*)rbnf)->getDefaultRuleSetName(); // TODO: remove cast!
} else if (tag == UNUM_PUBLIC_RULESETS) {
int32_t count = rbnf->getNumberOfRuleSetNames();
for (int i = 0; i < count; ++i) {
res += rbnf->getRuleSetName(i);
res += (UChar)0x003b; // semicolon
}
} else {
*status = U_UNSUPPORTED_ERROR;
return -1;
}
}
return res.extract(result, resultLength, *status);
1999-08-16 21:50:52 +00:00
}
U_CAPI void U_EXPORT2
1999-08-16 21:50:52 +00:00
unum_setTextAttribute( UNumberFormat* fmt,
UNumberFormatTextAttribute tag,
const UChar* newValue,
int32_t newValueLength,
UErrorCode *status)
{
if(U_FAILURE(*status))
return;
int32_t len = (newValueLength == -1 ? u_strlen(newValue) : newValueLength);
const UnicodeString val((UChar*)newValue, len, len);
if (((NumberFormat*)fmt)->getDynamicClassID() == DecimalFormat::getStaticClassID()) {
DecimalFormat* df = (DecimalFormat*) fmt;
switch(tag) {
case UNUM_POSITIVE_PREFIX:
df->setPositivePrefix(val);
break;
case UNUM_POSITIVE_SUFFIX:
df->setPositiveSuffix(val);
break;
case UNUM_NEGATIVE_PREFIX:
df->setNegativePrefix(val);
break;
case UNUM_NEGATIVE_SUFFIX:
df->setNegativeSuffix(val);
break;
case UNUM_PADDING_CHARACTER:
df->setPadCharacter(*newValue);
break;
case UNUM_CURRENCY_CODE:
df->setCurrency(newValue, *status);
break;
default:
*status = U_UNSUPPORTED_ERROR;
break;
}
} else {
U_ASSERT(((NumberFormat*)fmt)->getDynamicClassID() == RuleBasedNumberFormat::getStaticClassID());
if (tag == UNUM_DEFAULT_RULESET) {
((RuleBasedNumberFormat*)fmt)->setDefaultRuleSet(newValue, *status);
} else {
*status = U_UNSUPPORTED_ERROR;
}
}
1999-08-16 21:50:52 +00:00
}
U_CAPI int32_t U_EXPORT2
1999-08-16 21:50:52 +00:00
unum_toPattern( const UNumberFormat* fmt,
UBool isPatternLocalized,
1999-08-16 21:50:52 +00:00
UChar* result,
int32_t resultLength,
UErrorCode* status)
{
if(U_FAILURE(*status))
return -1;
UnicodeString pat;
if(!(result==NULL && resultLength==0)) {
// NULL destination for pure preflighting: empty dummy string
// otherwise, alias the destination buffer
pat.setTo(result, 0, resultLength);
}
if (((const NumberFormat*)fmt)->getDynamicClassID() == DecimalFormat::getStaticClassID()) {
const DecimalFormat* df = (const DecimalFormat*) fmt;
if(isPatternLocalized)
df->toLocalizedPattern(pat);
else
df->toPattern(pat);
} else {
U_ASSERT(((const NumberFormat*)fmt)->getDynamicClassID() == RuleBasedNumberFormat::getStaticClassID());
pat = ((const RuleBasedNumberFormat*)fmt)->getRules();
}
return pat.extract(result, resultLength, *status);
1999-08-16 21:50:52 +00:00
}
// TODO: first parameter should be const!
U_CAPI int32_t U_EXPORT2
unum_getSymbol(const UNumberFormat *fmt,
UNumberFormatSymbol symbol,
UChar *buffer,
int32_t size,
UErrorCode *status)
{
if(status==NULL || U_FAILURE(*status)) {
return 0;
}
if(fmt==NULL || (uint16_t)symbol>=UNUM_FORMAT_SYMBOL_COUNT) {
*status=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
if (((const NumberFormat*)fmt)->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
*status = U_UNSUPPORTED_ERROR;
return 0;
}
return ((const DecimalFormat *)fmt)->
getDecimalFormatSymbols()->
getConstSymbol((DecimalFormatSymbols::ENumberFormatSymbol)symbol).
extract(buffer, size, *status);
}
U_CAPI void U_EXPORT2
unum_setSymbol(UNumberFormat *fmt,
UNumberFormatSymbol symbol,
const UChar *value,
int32_t length,
UErrorCode *status)
{
if(status==NULL || U_FAILURE(*status)) {
return;
}
if(fmt==NULL || (uint16_t)symbol>=UNUM_FORMAT_SYMBOL_COUNT || value==NULL || length<-1) {
*status=U_ILLEGAL_ARGUMENT_ERROR;
return;
}
if (((NumberFormat*)fmt)->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
*status = U_UNSUPPORTED_ERROR;
return;
}
DecimalFormatSymbols symbols(*((DecimalFormat *)fmt)->getDecimalFormatSymbols());
symbols.setSymbol((DecimalFormatSymbols::ENumberFormatSymbol)symbol,
UnicodeString(value, length)); /* UnicodeString can handle the case when length = -1. */
((DecimalFormat *)fmt)->setDecimalFormatSymbols(symbols);
}
U_CAPI void U_EXPORT2
unum_applyPattern( UNumberFormat *format,
UBool localized,
const UChar *pattern,
int32_t patternLength,
UParseError *parseError,
UErrorCode* status)
{
UErrorCode tStatus = U_ZERO_ERROR;
UParseError tParseError;
if(parseError == NULL){
parseError = &tParseError;
}
if(status==NULL){
status = &tStatus;
}
int32_t len = (patternLength == -1 ? u_strlen(pattern) : patternLength);
const UnicodeString pat((UChar*)pattern, len, len);
// Verify if the object passed is a DecimalFormat object
if (((NumberFormat*)format)->getDynamicClassID() == DecimalFormat::getStaticClassID()) {
if(localized) {
((DecimalFormat*)format)->applyLocalizedPattern(pat,*parseError, *status);
} else {
((DecimalFormat*)format)->applyPattern(pat,*parseError, *status);
}
} else {
*status = U_UNSUPPORTED_ERROR;
return;
}
}
U_CAPI const char* U_EXPORT2
unum_getLocaleByType(const UNumberFormat *fmt,
ULocDataLocaleType type,
UErrorCode* status)
{
if (fmt == NULL) {
if (U_SUCCESS(*status)) {
*status = U_ILLEGAL_ARGUMENT_ERROR;
}
return NULL;
}
return ((const Format*)fmt)->getLocaleID(type, *status);
}
#endif /* #if !UCONFIG_NO_FORMATTING */