ICU-1097 use a char* for the currency code

X-SVN-Rev: 8625
This commit is contained in:
Alan Liu 2002-05-14 23:24:58 +00:00
parent 111446f691
commit d5c7b20828
5 changed files with 73 additions and 58 deletions

View file

@ -4,8 +4,8 @@
* Corporation and others. All Rights Reserved.
**********************************************************************
* $Source: /xsrl/Nsvn/icu/icu/source/i18n/Attic/currency.cpp,v $
* $Date: 2002/05/14 22:18:43 $
* $Revision: 1.2 $
* $Date: 2002/05/14 23:24:24 $
* $Revision: 1.3 $
**********************************************************************
*/
#include "unicode/currency.h"
@ -56,16 +56,12 @@ static const UCurrencyData DATA[] = {
* TEMPORARY Internal function to look up currency data.
*/
static int32_t
_findData(const UnicodeString& currency) {
_findData(const char* currency) {
// TODO Temporary implementation; Redo this
// Convert currency code to char*
char isoCode[4];
currency.extract(0, 3, isoCode, "");
// Start from element 1
for (int32_t i=1; i < (int32_t)ARRAY_SIZE(DATA); ++i) {
int32_t c = uprv_strcmp(DATA[i].code, isoCode);
int32_t c = uprv_stricmp(DATA[i].code, currency);
if (c == 0) {
return i;
} else if (c > 0) {
@ -79,8 +75,9 @@ _findData(const UnicodeString& currency) {
* Returns a currency object for the default currency in the given
* locale.
*/
UnicodeString
void
UCurrency::forLocale(const Locale& locale,
char* result,
UErrorCode& ec) {
// Look up the CurrencyElements resource for this locale.
// It contains: [0] = currency symbol, e.g. "$";
@ -88,9 +85,11 @@ UCurrency::forLocale(const Locale& locale,
// [2] = monetary decimal separator, e.g. ".".
if (U_SUCCESS(ec)) {
ResourceBundle rb((char*)0, locale, ec);
return rb.get("CurrencyElements", ec).getStringEx(1, ec);
UnicodeString s = rb.get("CurrencyElements", ec).getStringEx(1, ec);
s.extract(0, 3, result, "");
} else {
result[0] = 0;
}
return "";
}
/**
@ -99,7 +98,7 @@ UCurrency::forLocale(const Locale& locale,
* currency object in the en_US locale is "$".
*/
UnicodeString
UCurrency::getSymbol(const UnicodeString& currency,
UCurrency::getSymbol(const char* currency,
const Locale& locale) {
// Look up the Currencies resource for the given locale. The
// Currencies locale looks like this in the original C
@ -113,14 +112,10 @@ UCurrency::getSymbol(const UnicodeString& currency,
//|}
UErrorCode ec = U_ZERO_ERROR;
// Convert currency code to char*
char isoCode[4];
currency.extract(0, 3, isoCode, "");
ResourceBundle rb((char*)0, locale, ec);
UnicodeString result = rb.get("Currencies", ec).getStringEx(isoCode, ec);
if (U_SUCCESS(ec) && result.length() > 0) {
return result;
UnicodeString s = rb.get("Currencies", ec).getStringEx(currency, ec);
if (U_SUCCESS(ec) && s.length() > 0) {
return s;
}
// Since the Currencies resource is not fully populated yet,
@ -128,12 +123,13 @@ UCurrency::getSymbol(const UnicodeString& currency,
// resource.
ec = U_ZERO_ERROR;
ResourceBundle ce = rb.get("CurrencyElements", ec);
if (ce.getStringEx(1, ec) == currency) {
s = UnicodeString(currency, "");
if (ce.getStringEx(1, ec) == s) {
return ce.getStringEx((int32_t)0, ec);
}
// If we fail to find a match, use the full ISO code
return currency;
return s;
}
/**
@ -143,7 +139,7 @@ UCurrency::getSymbol(const UnicodeString& currency,
* displayed
*/
int32_t
UCurrency::getDefaultFractionDigits(const UnicodeString& currency) {
UCurrency::getDefaultFractionDigits(const char* currency) {
// TODO Temporary implementation; Redo this
return DATA[_findData(currency)].fractionDigits;
}
@ -154,7 +150,7 @@ UCurrency::getDefaultFractionDigits(const UnicodeString& currency) {
* @return the non-negative rounding increment, or 0.0 if none
*/
double
UCurrency::getRoundingIncrement(const UnicodeString& currency) {
UCurrency::getRoundingIncrement(const char* currency) {
// TODO Temporary implementation; Redo this
return DATA[_findData(currency)].rounding;
}

View file

@ -43,6 +43,7 @@
#include "unicode/resbund.h"
#include "unicode/uchar.h"
#include "cmemory.h"
#include "cstring.h"
#include "unicode/currency.h"
U_NAMESPACE_BEGIN
@ -234,7 +235,7 @@ DecimalFormat::construct(UErrorCode& status,
}
if (symbolsToAdopt == NULL) {
currency = UCurrency::forLocale(Locale::getDefault(), status);
UCurrency::forLocale(Locale::getDefault(), currency, status);
} else {
setCurrencyForSymbols();
}
@ -1438,9 +1439,9 @@ DecimalFormat::setCurrencyForSymbols() {
def.getSymbol(DecimalFormatSymbols::kCurrencySymbol) &&
fSymbols->getSymbol(DecimalFormatSymbols::kIntlCurrencySymbol) ==
def.getSymbol(DecimalFormatSymbols::kIntlCurrencySymbol)) {
currency = UCurrency::forLocale(fSymbols->getLocale(), ec);
UCurrency::forLocale(fSymbols->getLocale(), currency, ec);
} else {
currency.truncate(0); // Use DFS currency info
currency[0] = 0; // Use DFS currency info
}
}
@ -1937,7 +1938,7 @@ void DecimalFormat::expandAffix(const UnicodeString& pattern,
++i;
}
UnicodeString s;
if (currency.length() != 0) {
if (currency[0] != 0) {
s = intl ? currency
: UCurrency::getSymbol(currency, fSymbols->getLocale());
} else {
@ -2932,13 +2933,14 @@ void DecimalFormat::setMinimumFractionDigits(int32_t newValue) {
* null.
* @since ICU 2.2
*/
void DecimalFormat::setCurrency(const UnicodeString& theCurrency) {
void DecimalFormat::setCurrency(const char* theCurrency) {
// If we are a currency format, then modify our affixes to
// encode the currency symbol for the given currency in our
// locale, and adjust the decimal digits and rounding for the
// given currency.
currency = theCurrency;
uprv_strncpy(currency, theCurrency, 3);
currency[3] = 0;
if (fIsCurrencyFormat) {
setRoundingIncrement(UCurrency::getRoundingIncrement(currency));
@ -2960,7 +2962,7 @@ void DecimalFormat::setCurrency(const UnicodeString& theCurrency) {
* the standard ones for its locale.
* @since ICU 2.2
*/
UnicodeString DecimalFormat::getCurrency() const {
const char* DecimalFormat::getCurrency() const {
return currency;
}

View file

@ -4,7 +4,7 @@
* Corporation and others. All Rights Reserved.
**********************************************************************
* $Source: /xsrl/Nsvn/icu/icu/source/i18n/unicode/Attic/currency.h,v $
* $Revision: 1.2 $
* $Revision: 1.3 $
**********************************************************************
*/
#ifndef UCURRENCY_H
@ -34,39 +34,42 @@ class Locale;
*/
class U_I18N_API UCurrency {
// TODO:? Make the string a char* instead of a UnicodeString ?
public:
/**
* Returns a currency object for the default currency in the given
* Returns a currency code for the default currency in the given
* locale.
* @param locale the locale for which to retrieve a currency code
* @param result pointer to a buffer of at least 4 chars
* to receive a null-terminated 3-letter ISO 4217 code
* @param ec error code
*/
static UnicodeString forLocale(const Locale& locale,
UErrorCode& ec);
static void forLocale(const Locale& locale,
char* result,
UErrorCode& ec);
/**
* Returns the display string for this currency object in the
* Returns the display string for the given currency in the
* given locale. For example, the display string for the USD
* currency object in the en_US locale is "$".
*/
static UnicodeString getSymbol(const UnicodeString& currency,
static UnicodeString getSymbol(const char* currency,
const Locale& locale);
/**
* Returns the number of the number of fraction digits that should
* be displayed for this currency.
* be displayed for the given currency.
* @return a non-negative number of fraction digits to be
* displayed
*/
static int32_t getDefaultFractionDigits(const UnicodeString& currency);
static int32_t getDefaultFractionDigits(const char* currency);
/**
* Returns the rounding increment for this currency, or 0.0 if no
* rounding is done by this currency.
* Returns the rounding increment for the given currency, or 0.0 if no
* rounding is done by the currency.
* @return the non-negative rounding increment, or 0.0 if none
*/
static double getRoundingIncrement(const UnicodeString& currency);
static double getRoundingIncrement(const char* currency);
};
U_NAMESPACE_END

View file

@ -987,27 +987,26 @@ public:
virtual void setMinimumFractionDigits(int32_t newValue);
/**
* Sets the <tt>Currency</tt> object used to display currency
* Sets the currency used to display currency
* amounts. This takes effect immediately, if this format is a
* currency format. If this format is not a currency format, then
* the currency object is used if and when this object becomes a
* the currency is used if and when this object becomes a
* currency format through the application of a new pattern.
* @param theCurrency new currency object to use. Must not be
* null.
* @param theCurrency new currency to use
* @since ICU 2.2
*/
void setCurrency(const UnicodeString& theCurrency);
void setCurrency(const char* theCurrency);
/**
* Gets the <tt>Currency</tt> object used to display currency
* amounts. This will be null if a object is resurrected with a
* Gets the currency used to display currency
* amounts. This will be an empty string if a object is resurrected with a
* custom DecimalFormatSymbols object, or if the user sets a
* custom DecimalFormatSymbols object. A custom
* DecimalFormatSymbols object has currency symbols that are not
* the standard ones for its locale.
* @since ICU 2.2
*/
UnicodeString getCurrency() const;
const char* getCurrency() const;
/**
* The resource tags we use to retrieve decimal format data from
@ -1169,7 +1168,7 @@ private:
EPadPosition fPadPosition;
// ISO currency code
UnicodeString currency;
char currency[4];
// Constants for characters used in programmatic (unlocalized) patterns.
static const UChar kPatternZeroDigit;

View file

@ -492,11 +492,11 @@ void NumberFormatTest::TestCurrencyObject() {
void NumberFormatTest::expectCurrency(NumberFormat& nf, const Locale& locale,
double value, const UnicodeString& string) {
UErrorCode ec = U_ZERO_ERROR;
DecimalFormat& fmt = * (DecimalFormat*) &nf;
UnicodeString curr("<none>");
char curr[4] = {'-','-','-',0};
if (*locale.getLanguage() != 0) {
UErrorCode ec = U_ZERO_ERROR;
curr = UCurrency::forLocale(locale, ec);
UCurrency::forLocale(locale, curr, ec);
if (U_FAILURE(ec)) {
errln("FAIL: UCurrency::forLocale");
return;
@ -507,10 +507,25 @@ void NumberFormatTest::expectCurrency(NumberFormat& nf, const Locale& locale,
fmt.format(value, s);
s.findAndReplace((UChar32)0x00A0, (UChar32)0x0020);
if (s == string) {
logln((UnicodeString)"Ok: " + value + " x " + curr + " => " + prettify(s));
// Default display of the number yields "1234.5599999999999"
// instead of "1234.56". Use a formatter to fix this.
NumberFormat* f =
NumberFormat::createInstance(Locale::getUS(), ec);
UnicodeString v;
if (U_FAILURE(ec)) {
// Oops; bad formatter. Use default op+= display.
v = (UnicodeString)"" + value;
} else {
errln((UnicodeString)"FAIL: " + value + " x " + curr + " => " + prettify(s) +
f->setMaximumFractionDigits(4);
f->setGroupingUsed(FALSE);
f->format(value, v);
}
delete f;
if (s == string) {
logln((UnicodeString)"Ok: " + v + " x " + curr + " => " + prettify(s));
} else {
errln((UnicodeString)"FAIL: " + v + " x " + curr + " => " + prettify(s) +
", expected " + prettify(string));
}
}