#include "unicode/ustring.h"
#include "cstring.h"
#include "uinvchar.h"
+#include "charstr.h"
static constexpr char16_t kDefaultCurrency[] = u"XXX";
+static constexpr char kDefaultCurrency8[] = "XXX";
U_NAMESPACE_BEGIN
initCurrency(simpleIsoCode);
}
+CurrencyUnit::CurrencyUnit(StringPiece _isoCode, UErrorCode& ec) {
+ // Note: unlike the old constructor, reject empty arguments with an error.
+ char isoCodeBuffer[4];
+ const char* isoCodeToUse;
+ // uprv_memchr checks that the string contains no internal NULs
+ if (_isoCode.length() != 3 || uprv_memchr(_isoCode.data(), 0, 3) != nullptr) {
+ isoCodeToUse = kDefaultCurrency8;
+ ec = U_ILLEGAL_ARGUMENT_ERROR;
+ } else if (!uprv_isInvariantString(_isoCode.data(), 3)) {
+ // TODO: Perform a more strict ASCII check like in ICU4J isAlpha3Code?
+ isoCodeToUse = kDefaultCurrency8;
+ ec = U_INVARIANT_CONVERSION_ERROR;
+ } else {
+ // Have to use isoCodeBuffer to ensure the string is NUL-terminated
+ uprv_strncpy(isoCodeBuffer, _isoCode.data(), 3);
+ isoCodeBuffer[3] = 0;
+ isoCodeToUse = isoCodeBuffer;
+ }
+ // TODO: Perform uppercasing here like in ICU4J Currency.getInstance()?
+ u_charsToUChars(isoCodeToUse, isoCode, 3);
+ isoCode[3] = 0;
+ initCurrency(isoCodeToUse);
+}
+
CurrencyUnit::CurrencyUnit(const CurrencyUnit& other) : MeasureUnit(other) {
u_strcpy(isoCode, other.isoCode);
}
/**
* Construct an object with the given ISO currency code.
+ *
* @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.
*/
CurrencyUnit(ConstChar16Ptr isoCode, UErrorCode &ec);
+ /**
+ * Construct an object with the given ISO currency code.
+ *
+ * @param isoCode the 3-letter ISO 4217 currency code; must have
+ * length 3. If invalid, the currency is initialized to XXX.
+ * @param ec input-output error code. If the isoCode is invalid,
+ * then this will be set to a failing value.
+ * @draft ICU 64
+ */
+ CurrencyUnit(StringPiece isoCode, UErrorCode &ec);
+
/**
* Copy constructor
* @stable ICU 3.0
static const UChar BAD2[] = u"??A";
static const UChar XXX[] = u"XXX";
static const char XXX8[] = "XXX";
+ static const UChar INV[] = u"{$%";
+ static const char INV8[] = "{$%";
+ static const UChar ZZZ[] = u"zz";
+ static const char ZZZ8[] = "zz";
+
+ UChar* EUR = (UChar*) malloc(6);
+ EUR[0] = u'E';
+ EUR[1] = u'U';
+ EUR[2] = u'R';
+ char* EUR8 = (char*) malloc(3);
+ EUR8[0] = 'E';
+ EUR8[1] = 'U';
+ EUR8[2] = 'R';
+
CurrencyUnit cu(USD, ec);
assertSuccess("CurrencyUnit", ec);
assertEquals("getISOCurrency()", USD, cu.getISOCurrency());
assertEquals("getSubtype()", USD8, cu.getSubtype());
+ CurrencyUnit inv(INV, ec);
+ assertEquals("non-invariant", U_INVARIANT_CONVERSION_ERROR, ec);
+ assertEquals("non-invariant", XXX, inv.getISOCurrency());
+ ec = U_ZERO_ERROR;
+
+ CurrencyUnit zzz(ZZZ, ec);
+ assertEquals("too short", U_ILLEGAL_ARGUMENT_ERROR, ec);
+ assertEquals("too short", XXX, zzz.getISOCurrency());
+ ec = U_ZERO_ERROR;
+
+ CurrencyUnit eur(EUR, ec);
+ assertEquals("non-nul-terminated", u"EUR", eur.getISOCurrency());
+ assertEquals("non-nul-terminated", "EUR", eur.getSubtype());
+
+ // Test StringPiece constructor
+ CurrencyUnit cu8(USD8, ec);
+ assertEquals("StringPiece constructor", USD, cu8.getISOCurrency());
+
+ CurrencyUnit inv8(INV8, ec);
+ assertEquals("non-invariant 8", U_INVARIANT_CONVERSION_ERROR, ec);
+ assertEquals("non-invariant 8", XXX, inv8.getISOCurrency());
+ ec = U_ZERO_ERROR;
+
+ CurrencyUnit zzz8(ZZZ8, ec);
+ assertEquals("too short 8", U_ILLEGAL_ARGUMENT_ERROR, ec);
+ assertEquals("too short 8", XXX, zzz8.getISOCurrency());
+ ec = U_ZERO_ERROR;
+
+ CurrencyUnit zzz8b({ZZZ8, 3}, ec);
+ assertEquals("too short 8b", U_ILLEGAL_ARGUMENT_ERROR, ec);
+ assertEquals("too short 8b", XXX, zzz8b.getISOCurrency());
+ ec = U_ZERO_ERROR;
+
+ CurrencyUnit eur8({EUR8, 3}, ec);
+ assertEquals("non-nul-terminated 8", u"EUR", eur8.getISOCurrency());
+ assertEquals("non-nul-terminated 8", "EUR", eur8.getSubtype());
+
CurrencyUnit cu2(cu);
if (!(cu2 == cu)){
errln("CurrencyUnit copy constructed object should be same");
CurrencyUnit failure(*meter, ec);
assertEquals("Copying from meter should fail", ec, U_ILLEGAL_ARGUMENT_ERROR);
assertEquals("Copying should not give uninitialized ISO code", u"", failure.getISOCurrency());
+
+ uprv_free(EUR);
+ uprv_free(EUR8);
}
void NumberFormatTest::TestCurrencyAmount(void){