#include "unicode/currunit.h"
#include "unicode/ustring.h"
#include "cstring.h"
+#include "uinvchar.h"
static constexpr char16_t kDefaultCurrency[] = u"XXX";
CurrencyUnit::CurrencyUnit(ConstChar16Ptr _isoCode, UErrorCode& ec) {
// The constructor always leaves the CurrencyUnit in a valid state (with a 3-character currency code).
+ // Note: in ICU4J Currency.getInstance(), we check string length for 3, but in ICU4C we allow a
+ // non-NUL-terminated string to be passed as an argument, so it is not possible to check length.
+ const char16_t* isoCodeToUse;
if (U_FAILURE(ec) || _isoCode == nullptr) {
- u_strcpy(isoCode, kDefaultCurrency);
- } else if (u_strlen(_isoCode) != 3) {
- u_strcpy(isoCode, kDefaultCurrency);
- ec = U_ILLEGAL_ARGUMENT_ERROR;
+ isoCodeToUse = kDefaultCurrency;
+ } else if (!uprv_isInvariantUString(_isoCode, 3)) {
+ // TODO: Perform a more strict ASCII check like in ICU4J isAlpha3Code?
+ isoCodeToUse = kDefaultCurrency;
+ ec = U_INVARIANT_CONVERSION_ERROR;
} else {
- u_strcpy(isoCode, _isoCode);
+ isoCodeToUse = _isoCode;
}
+ u_strncpy(isoCode, isoCodeToUse, 3);
+ isoCode[3] = 0;
char simpleIsoCode[4];
u_UCharsToChars(isoCode, simpleIsoCode, 4);
initCurrency(simpleIsoCode);
void blueprint_helpers::parseCurrencyOption(const StringSegment& segment, MacroProps& macros,
UErrorCode& status) {
- // Can't use toTempUnicodeString() because getTerminatedBuffer is non-const
- const UChar* currencyCode = segment.toUnicodeString().getTerminatedBuffer();
+ // Unlike ICU4J, have to check length manually because ICU4C CurrencyUnit does not check it for us
+ if (segment.length() != 3) {
+ status = U_NUMBER_SKELETON_SYNTAX_ERROR;
+ return;
+ }
+ const UChar* currencyCode = segment.toTempUnicodeString().getBuffer();
UErrorCode localStatus = U_ZERO_ERROR;
CurrencyUnit currency(currencyCode, localStatus);
if (U_FAILURE(localStatus)) {
/**
* Construct an object with the given ISO currency code.
- * @param isoCode the 3-letter ISO 4217 currency code; must not be
- * NULL and must have length 3
+ * @param isoCode the 3-letter ISO 4217 currency code; must have
+ * length 3 and need not be NUL-terminated. If NULL, the currency
+ * is initialized to the unknown currency XXX.
* @param ec input-output error code. If the isoCode is invalid,
* then this will be set to a failing value.
* @stable ICU 3.0