tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

currunit.cpp (4429B)


      1 // © 2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html
      3 /*
      4 **********************************************************************
      5 * Copyright (c) 2004-2014, International Business Machines
      6 * Corporation and others.  All Rights Reserved.
      7 **********************************************************************
      8 * Author: Alan Liu
      9 * Created: April 26, 2004
     10 * Since: ICU 3.0
     11 **********************************************************************
     12 */
     13 #include "unicode/utypes.h"
     14 
     15 #if !UCONFIG_NO_FORMATTING
     16 
     17 #include "unicode/currunit.h"
     18 #include "unicode/ustring.h"
     19 #include "unicode/uchar.h"
     20 #include "cstring.h"
     21 #include "uinvchar.h"
     22 #include "charstr.h"
     23 #include "ustr_imp.h"
     24 #include "measunit_impl.h"
     25 
     26 U_NAMESPACE_BEGIN
     27 
     28 CurrencyUnit::CurrencyUnit(ConstChar16Ptr _isoCode, UErrorCode& ec) {
     29    // The constructor always leaves the CurrencyUnit in a valid state (with a 3-character currency code).
     30    // Note: in ICU4J Currency.getInstance(), we check string length for 3, but in ICU4C we allow a
     31    // non-NUL-terminated string to be passed as an argument, so it is not possible to check length.
     32    // However, we allow a NUL-terminated empty string, which should have the same behavior as nullptr.
     33    // Consider NUL-terminated strings of length 1 or 2 as invalid.
     34    bool useDefault = false;
     35    if (U_FAILURE(ec) || _isoCode == nullptr || _isoCode[0] == 0) {
     36        useDefault = true;
     37    } else if (_isoCode[1] == 0 || _isoCode[2] == 0) {
     38        useDefault = true;
     39        ec = U_ILLEGAL_ARGUMENT_ERROR;
     40    } else if (!uprv_isInvariantUString(_isoCode, 3)) {
     41        // TODO: Perform a more strict ASCII check like in ICU4J isAlpha3Code?
     42        useDefault = true;
     43        ec = U_INVARIANT_CONVERSION_ERROR;
     44    } else {
     45        for (int32_t i=0; i<3; i++) {
     46            isoCode[i] = u_asciiToUpper(_isoCode[i]);
     47        }
     48        isoCode[3] = 0;
     49    }
     50    if (useDefault) {
     51        uprv_memcpy(isoCode, kDefaultCurrency, sizeof(char16_t) * 4);
     52    }
     53    char simpleIsoCode[4];
     54    u_UCharsToChars(isoCode, simpleIsoCode, 4);
     55    initCurrency(simpleIsoCode);
     56 }
     57 
     58 CurrencyUnit::CurrencyUnit(StringPiece _isoCode, UErrorCode& ec) {
     59    // Note: unlike the old constructor, reject empty arguments with an error.
     60    char isoCodeBuffer[4];
     61    const char* isoCodeToUse;
     62    // uprv_memchr checks that the string contains no internal NULs
     63    if (_isoCode.length() != 3 || uprv_memchr(_isoCode.data(), 0, 3) != nullptr) {
     64        isoCodeToUse = kDefaultCurrency8;
     65        ec = U_ILLEGAL_ARGUMENT_ERROR;
     66    } else if (!uprv_isInvariantString(_isoCode.data(), 3)) {
     67        // TODO: Perform a more strict ASCII check like in ICU4J isAlpha3Code?
     68        isoCodeToUse = kDefaultCurrency8;
     69        ec = U_INVARIANT_CONVERSION_ERROR;
     70    } else {
     71        // Have to use isoCodeBuffer to ensure the string is NUL-terminated
     72        for (int32_t i=0; i<3; i++) {
     73            isoCodeBuffer[i] = uprv_toupper(_isoCode.data()[i]);
     74        }
     75        isoCodeBuffer[3] = 0;
     76        isoCodeToUse = isoCodeBuffer;
     77    }
     78    u_charsToUChars(isoCodeToUse, isoCode, 4);
     79    initCurrency(isoCodeToUse);
     80 }
     81 
     82 CurrencyUnit::CurrencyUnit(const CurrencyUnit& other) : MeasureUnit(other) {
     83    u_strcpy(isoCode, other.isoCode);
     84 }
     85 
     86 CurrencyUnit::CurrencyUnit(const MeasureUnit& other, UErrorCode& ec) : MeasureUnit(other) {
     87    // Make sure this is a currency.
     88    // OK to hard-code the string because we are comparing against another hard-coded string.
     89    if (uprv_strcmp("currency", getType()) != 0) {
     90        ec = U_ILLEGAL_ARGUMENT_ERROR;
     91        isoCode[0] = 0;
     92    } else {
     93        // Get the ISO Code from the subtype field.
     94        u_charsToUChars(getSubtype(), isoCode, 4);
     95        isoCode[3] = 0; // make 100% sure it is NUL-terminated
     96    }
     97 }
     98 
     99 CurrencyUnit::CurrencyUnit() : MeasureUnit() {
    100    u_strcpy(isoCode, kDefaultCurrency);
    101    char simpleIsoCode[4];
    102    u_UCharsToChars(isoCode, simpleIsoCode, 4);
    103    initCurrency(simpleIsoCode);
    104 }
    105 
    106 CurrencyUnit& CurrencyUnit::operator=(const CurrencyUnit& other) {
    107    if (this == &other) {
    108        return *this;
    109    }
    110    MeasureUnit::operator=(other);
    111    u_strcpy(isoCode, other.isoCode);
    112    return *this;
    113 }
    114 
    115 CurrencyUnit* CurrencyUnit::clone() const {
    116    return new CurrencyUnit(*this);
    117 }
    118 
    119 CurrencyUnit::~CurrencyUnit() {
    120 }
    121    
    122 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CurrencyUnit)
    123 
    124 U_NAMESPACE_END
    125 
    126 #endif // !UCONFIG_NO_FORMATTING