]> granicus.if.org Git - icu/commitdiff
ICU-13351 Adding additional CurrencyUnit constructors and stabilizing API constract...
authorShane Carr <shane@unicode.org>
Tue, 12 Sep 2017 05:20:50 +0000 (05:20 +0000)
committerShane Carr <shane@unicode.org>
Tue, 12 Sep 2017 05:20:50 +0000 (05:20 +0000)
X-SVN-Rev: 40379

icu4c/source/i18n/currunit.cpp
icu4c/source/i18n/measunit.cpp
icu4c/source/i18n/unicode/currunit.h
icu4c/source/i18n/unicode/measunit.h

index 197885452f5e181b0dd9410c597c6a8bc844b922..1750b94fab411b790227b0d0a1ec7d45839f645e 100644 (file)
@@ -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;
index d1244ad3535a47493a4d5af99ad09a2a934457d0..e9e141789ea757e4f7f0488b818693d7727132f4 100644 (file)
@@ -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);
index fd0f9f2bcce085982be4ec26ccd4390385d754ed..db36d2b40fa65cfdc12245a8c9cff2f791aff8b7 100644 (file)
@@ -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
index 2204e987aaf33f36d571028b230c05b60ca950dc..afd560e30e0977004d3410fca8111ec0af0b6e5d 100644 (file)
@@ -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.