blob: 3bb8d107807967b52e330a8e66993b47f4a028f5 [file] [log] [blame]
Victor Chang73229502020-09-17 13:39:19 +01001// © 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"
Victor Changce4bf3c2021-01-19 16:34:24 +000012#include "bytesinkutil.h"
Victor Chang73229502020-09-17 13:39:19 +010013
14U_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
22template class U_I18N_API MaybeStackHeaderAndArray<decNumber, char, DECNUM_INITIAL_CAPACITY>;
23#endif
24
25namespace number {
26namespace impl {
27
28/** A very thin C++ wrapper around decNumber.h */
29// Exported as U_I18N_API for tests
30class 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 void toString(ByteSink& output, UErrorCode& status) const;
60
Victor Changce4bf3c2021-01-19 16:34:24 +000061 inline CharString toCharString(UErrorCode& status) const {
62 CharString cstr;
63 CharStringByteSink sink(&cstr);
64 toString(sink, status);
65 return cstr;
66 }
67
Victor Chang73229502020-09-17 13:39:19 +010068 inline const decNumber* getRawDecNumber() const {
69 return fData.getAlias();
70 }
71
72 private:
73 static constexpr int32_t kDefaultDigits = DECNUM_INITIAL_CAPACITY;
74 MaybeStackHeaderAndArray<decNumber, char, kDefaultDigits> fData;
75 decContext fContext;
76
77 void _setTo(const char* str, int32_t maxDigits, UErrorCode& status);
78};
79
80} // namespace impl
81} // namespace number
82
83U_NAMESPACE_END
84
85#endif // __NUMBER_DECNUM_H__
86
87#endif /* #if !UCONFIG_NO_FORMATTING */