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