From d0c762e71cbf214a73c07f41726c6ac20b61e2ba Mon Sep 17 00:00:00 2001 From: Shane Carr Date: Tue, 12 Sep 2017 05:20:50 +0000 Subject: [PATCH] ICU-13351 Adding additional CurrencyUnit constructors and stabilizing API constract of MeasureUnit default constructor. X-SVN-Rev: 40379 --- icu4c/source/i18n/currunit.cpp | 24 ++++++++++++++++++++++-- icu4c/source/i18n/measunit.cpp | 7 ++++++- icu4c/source/i18n/unicode/currunit.h | 16 ++++++++++++++++ icu4c/source/i18n/unicode/measunit.h | 5 ++--- 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/icu4c/source/i18n/currunit.cpp b/icu4c/source/i18n/currunit.cpp index 197885452f..1750b94fab 100644 --- a/icu4c/source/i18n/currunit.cpp +++ b/icu4c/source/i18n/currunit.cpp @@ -16,6 +16,7 @@ #include "unicode/currunit.h" #include "unicode/ustring.h" +#include "cstring.h" U_NAMESPACE_BEGIN @@ -33,11 +34,30 @@ CurrencyUnit::CurrencyUnit(ConstChar16Ptr _isoCode, UErrorCode& ec) { } } -CurrencyUnit::CurrencyUnit(const CurrencyUnit& other) : - MeasureUnit(other) { +CurrencyUnit::CurrencyUnit(const CurrencyUnit& other) : MeasureUnit(other) { u_strcpy(isoCode, other.isoCode); } +CurrencyUnit::CurrencyUnit(const MeasureUnit& other, UErrorCode& ec) : MeasureUnit(other) { + // Make sure this is a currency. + // OK to hard-code the string because we are comparing against another hard-coded string. + if (uprv_strcmp("currency", getType()) != 0) { + ec = U_ILLEGAL_ARGUMENT_ERROR; + isoCode[0] = 0; + } else { + // Get the ISO Code from the subtype field. + u_charsToUChars(getSubtype(), isoCode, 4); + isoCode[3] = 0; // make 100% sure it is NUL-terminated + } +} + +CurrencyUnit::CurrencyUnit() : MeasureUnit() { + u_strcpy(isoCode, u"XXX"); + char simpleIsoCode[4]; + u_UCharsToChars(isoCode, simpleIsoCode, 4); + initCurrency(simpleIsoCode); +} + CurrencyUnit& CurrencyUnit::operator=(const CurrencyUnit& other) { if (this == &other) { return *this; diff --git a/icu4c/source/i18n/measunit.cpp b/icu4c/source/i18n/measunit.cpp index d1244ad353..e9e141789e 100644 --- a/icu4c/source/i18n/measunit.cpp +++ b/icu4c/source/i18n/measunit.cpp @@ -1079,7 +1079,12 @@ static int32_t binarySearch( } return -1; } - + +MeasureUnit::MeasureUnit() { + fCurrency[0] = 0; + initNoUnit("base"); +} + MeasureUnit::MeasureUnit(const MeasureUnit &other) : fTypeId(other.fTypeId), fSubTypeId(other.fSubTypeId) { uprv_strcpy(fCurrency, other.fCurrency); diff --git a/icu4c/source/i18n/unicode/currunit.h b/icu4c/source/i18n/unicode/currunit.h index fd0f9f2bcc..db36d2b40f 100644 --- a/icu4c/source/i18n/unicode/currunit.h +++ b/icu4c/source/i18n/unicode/currunit.h @@ -36,6 +36,12 @@ U_NAMESPACE_BEGIN */ class U_I18N_API CurrencyUnit: public MeasureUnit { public: + /** + * Default constructor. Initializes currency code to "XXX" (no currency). + * @draft ICU 60 + */ + CurrencyUnit(); + /** * Construct an object with the given ISO currency code. * @param isoCode the 3-letter ISO 4217 currency code; must not be @@ -52,6 +58,16 @@ class U_I18N_API CurrencyUnit: public MeasureUnit { */ CurrencyUnit(const CurrencyUnit& other); + /** + * Copy constructor from MeasureUnit. This constructor allows you to + * restore a CurrencyUnit that was sliced to MeasureUnit. + * + * @param measureUnit The MeasureUnit to copy from. + * @param ec Set to a failing value if the MeasureUnit is not a currency. + * @draft ICU 60 + */ + CurrencyUnit(const MeasureUnit& measureUnit, UErrorCode &ec); + /** * Assignment operator * @stable ICU 3.0 diff --git a/icu4c/source/i18n/unicode/measunit.h b/icu4c/source/i18n/unicode/measunit.h index 2204e987aa..afd560e30e 100644 --- a/icu4c/source/i18n/unicode/measunit.h +++ b/icu4c/source/i18n/unicode/measunit.h @@ -40,11 +40,10 @@ class U_I18N_API MeasureUnit: public UObject { /** * Default constructor. + * Populates the instance with the base dimensionless unit. * @stable ICU 3.0 */ - MeasureUnit() : fTypeId(0), fSubTypeId(0) { - fCurrency[0] = 0; - } + MeasureUnit(); /** * Copy constructor.