• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
26 namespace impl {
27 
28 /** A very thin C++ wrapper around decNumber.h */
29 // Exported as U_I18N_API for tests
30 class U_I18N_API DecNum : public UMemory {
31   public:
32     DecNum();  // leaves object in valid but undefined state
33 
34     // Copy-like constructor; use the default move operators.
35     DecNum(const DecNum& other, UErrorCode& status);
36 
37     /** Sets the decNumber to the StringPiece. */
38     void setTo(StringPiece str, UErrorCode& status);
39 
40     /** Sets the decNumber to the NUL-terminated char string. */
41     void setTo(const char* str, UErrorCode& status);
42 
43     /** Uses double_conversion to set this decNumber to the given double. */
44     void setTo(double d, UErrorCode& status);
45 
46     /** Sets the decNumber to the BCD representation. */
47     void setTo(const uint8_t* bcd, int32_t length, int32_t scale, bool isNegative, UErrorCode& status);
48 
49     void normalize();
50 
51     void multiplyBy(const DecNum& rhs, UErrorCode& status);
52 
53     void divideBy(const DecNum& rhs, UErrorCode& status);
54 
55     bool isNegative() const;
56 
57     bool isZero() const;
58 
59     /** Is infinity or NaN */
60     bool isSpecial() const;
61 
62     bool isInfinity() const;
63 
64     bool isNaN() const;
65 
66     void toString(ByteSink& output, UErrorCode& status) const;
67 
toCharString(UErrorCode & status)68     inline CharString toCharString(UErrorCode& status) const {
69       CharString cstr;
70       CharStringByteSink sink(&cstr);
71       toString(sink, status);
72       return cstr;
73     }
74 
getRawDecNumber()75     inline const decNumber* getRawDecNumber() const {
76         return fData.getAlias();
77     }
78 
79   private:
80     static constexpr int32_t kDefaultDigits = DECNUM_INITIAL_CAPACITY;
81     MaybeStackHeaderAndArray<decNumber, char, kDefaultDigits> fData;
82     decContext fContext;
83 
84     void _setTo(const char* str, int32_t maxDigits, UErrorCode& status);
85 };
86 
87 } // namespace impl
88 } // namespace number
89 
90 U_NAMESPACE_END
91 
92 #endif // __NUMBER_DECNUM_H__
93 
94 #endif /* #if !UCONFIG_NO_FORMATTING */
95