1*0e209d39SAndroid Build Coastguard Worker // © 2017 and later: Unicode, Inc. and others. 2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html 3*0e209d39SAndroid Build Coastguard Worker 4*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h" 5*0e209d39SAndroid Build Coastguard Worker 6*0e209d39SAndroid Build Coastguard Worker #if !UCONFIG_NO_FORMATTING 7*0e209d39SAndroid Build Coastguard Worker #ifndef __NUMBER_DECNUM_H__ 8*0e209d39SAndroid Build Coastguard Worker #define __NUMBER_DECNUM_H__ 9*0e209d39SAndroid Build Coastguard Worker 10*0e209d39SAndroid Build Coastguard Worker #include "decNumber.h" 11*0e209d39SAndroid Build Coastguard Worker #include "charstr.h" 12*0e209d39SAndroid Build Coastguard Worker #include "bytesinkutil.h" 13*0e209d39SAndroid Build Coastguard Worker 14*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN 15*0e209d39SAndroid Build Coastguard Worker 16*0e209d39SAndroid Build Coastguard Worker #define DECNUM_INITIAL_CAPACITY 34 17*0e209d39SAndroid Build Coastguard Worker 18*0e209d39SAndroid Build Coastguard Worker // Export an explicit template instantiation of the MaybeStackHeaderAndArray that is used as a data member of DecNum. 19*0e209d39SAndroid Build Coastguard Worker // When building DLLs for Windows this is required even though no direct access to the MaybeStackHeaderAndArray leaks out of the i18n library. 20*0e209d39SAndroid Build Coastguard Worker // (See digitlst.h, pluralaffix.h, datefmt.h, and others for similar examples.) 21*0e209d39SAndroid Build Coastguard Worker #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN 22*0e209d39SAndroid Build Coastguard Worker template class U_I18N_API MaybeStackHeaderAndArray<decNumber, char, DECNUM_INITIAL_CAPACITY>; 23*0e209d39SAndroid Build Coastguard Worker #endif 24*0e209d39SAndroid Build Coastguard Worker 25*0e209d39SAndroid Build Coastguard Worker namespace number::impl { 26*0e209d39SAndroid Build Coastguard Worker 27*0e209d39SAndroid Build Coastguard Worker /** A very thin C++ wrapper around decNumber.h */ 28*0e209d39SAndroid Build Coastguard Worker // Exported as U_I18N_API for tests 29*0e209d39SAndroid Build Coastguard Worker class U_I18N_API DecNum : public UMemory { 30*0e209d39SAndroid Build Coastguard Worker public: 31*0e209d39SAndroid Build Coastguard Worker DecNum(); // leaves object in valid but undefined state 32*0e209d39SAndroid Build Coastguard Worker 33*0e209d39SAndroid Build Coastguard Worker // Copy-like constructor; use the default move operators. 34*0e209d39SAndroid Build Coastguard Worker DecNum(const DecNum& other, UErrorCode& status); 35*0e209d39SAndroid Build Coastguard Worker 36*0e209d39SAndroid Build Coastguard Worker /** Sets the decNumber to the StringPiece. */ 37*0e209d39SAndroid Build Coastguard Worker void setTo(StringPiece str, UErrorCode& status); 38*0e209d39SAndroid Build Coastguard Worker 39*0e209d39SAndroid Build Coastguard Worker /** Sets the decNumber to the NUL-terminated char string. */ 40*0e209d39SAndroid Build Coastguard Worker void setTo(const char* str, UErrorCode& status); 41*0e209d39SAndroid Build Coastguard Worker 42*0e209d39SAndroid Build Coastguard Worker /** Uses double_conversion to set this decNumber to the given double. */ 43*0e209d39SAndroid Build Coastguard Worker void setTo(double d, UErrorCode& status); 44*0e209d39SAndroid Build Coastguard Worker 45*0e209d39SAndroid Build Coastguard Worker /** Sets the decNumber to the BCD representation. */ 46*0e209d39SAndroid Build Coastguard Worker void setTo(const uint8_t* bcd, int32_t length, int32_t scale, bool isNegative, UErrorCode& status); 47*0e209d39SAndroid Build Coastguard Worker 48*0e209d39SAndroid Build Coastguard Worker void normalize(); 49*0e209d39SAndroid Build Coastguard Worker 50*0e209d39SAndroid Build Coastguard Worker void multiplyBy(const DecNum& rhs, UErrorCode& status); 51*0e209d39SAndroid Build Coastguard Worker 52*0e209d39SAndroid Build Coastguard Worker void divideBy(const DecNum& rhs, UErrorCode& status); 53*0e209d39SAndroid Build Coastguard Worker 54*0e209d39SAndroid Build Coastguard Worker bool isNegative() const; 55*0e209d39SAndroid Build Coastguard Worker 56*0e209d39SAndroid Build Coastguard Worker bool isZero() const; 57*0e209d39SAndroid Build Coastguard Worker 58*0e209d39SAndroid Build Coastguard Worker /** Is infinity or NaN */ 59*0e209d39SAndroid Build Coastguard Worker bool isSpecial() const; 60*0e209d39SAndroid Build Coastguard Worker 61*0e209d39SAndroid Build Coastguard Worker bool isInfinity() const; 62*0e209d39SAndroid Build Coastguard Worker 63*0e209d39SAndroid Build Coastguard Worker bool isNaN() const; 64*0e209d39SAndroid Build Coastguard Worker 65*0e209d39SAndroid Build Coastguard Worker void toString(ByteSink& output, UErrorCode& status) const; 66*0e209d39SAndroid Build Coastguard Worker toCharString(UErrorCode & status)67*0e209d39SAndroid Build Coastguard Worker inline CharString toCharString(UErrorCode& status) const { 68*0e209d39SAndroid Build Coastguard Worker CharString cstr; 69*0e209d39SAndroid Build Coastguard Worker CharStringByteSink sink(&cstr); 70*0e209d39SAndroid Build Coastguard Worker toString(sink, status); 71*0e209d39SAndroid Build Coastguard Worker return cstr; 72*0e209d39SAndroid Build Coastguard Worker } 73*0e209d39SAndroid Build Coastguard Worker getRawDecNumber()74*0e209d39SAndroid Build Coastguard Worker inline const decNumber* getRawDecNumber() const { 75*0e209d39SAndroid Build Coastguard Worker return fData.getAlias(); 76*0e209d39SAndroid Build Coastguard Worker } 77*0e209d39SAndroid Build Coastguard Worker 78*0e209d39SAndroid Build Coastguard Worker private: 79*0e209d39SAndroid Build Coastguard Worker static constexpr int32_t kDefaultDigits = DECNUM_INITIAL_CAPACITY; 80*0e209d39SAndroid Build Coastguard Worker MaybeStackHeaderAndArray<decNumber, char, kDefaultDigits> fData; 81*0e209d39SAndroid Build Coastguard Worker decContext fContext; 82*0e209d39SAndroid Build Coastguard Worker 83*0e209d39SAndroid Build Coastguard Worker void _setTo(const char* str, int32_t maxDigits, UErrorCode& status); 84*0e209d39SAndroid Build Coastguard Worker }; 85*0e209d39SAndroid Build Coastguard Worker 86*0e209d39SAndroid Build Coastguard Worker } // namespace number::impl 87*0e209d39SAndroid Build Coastguard Worker 88*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END 89*0e209d39SAndroid Build Coastguard Worker 90*0e209d39SAndroid Build Coastguard Worker #endif // __NUMBER_DECNUM_H__ 91*0e209d39SAndroid Build Coastguard Worker 92*0e209d39SAndroid Build Coastguard Worker #endif /* #if !UCONFIG_NO_FORMATTING */ 93