1 // © 2018 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 
8 // Allow implicit conversion from char16_t* to UnicodeString for this file:
9 // Helpful in toString methods and elsewhere.
10 #define UNISTR_FROM_STRING_EXPLICIT
11 
12 #include "number_decnum.h"
13 #include "number_types.h"
14 #include "number_multiplier.h"
15 #include "numparse_validators.h"
16 #include "number_utils.h"
17 #include "decNumber.h"
18 
19 using namespace icu;
20 using namespace icu::number;
21 using namespace icu::number::impl;
22 using namespace icu::numparse::impl;
23 
24 
Scale(int32_t magnitude,DecNum * arbitraryToAdopt)25 Scale::Scale(int32_t magnitude, DecNum* arbitraryToAdopt)
26         : fMagnitude(magnitude), fArbitrary(arbitraryToAdopt), fError(U_ZERO_ERROR) {
27     if (fArbitrary != nullptr) {
28         // Attempt to convert the DecNum to a magnitude multiplier.
29         fArbitrary->normalize();
30         if (fArbitrary->getRawDecNumber()->digits == 1 && fArbitrary->getRawDecNumber()->lsu[0] == 1 &&
31             !fArbitrary->isNegative()) {
32             // Success!
33             fMagnitude += fArbitrary->getRawDecNumber()->exponent;
34             delete fArbitrary;
35             fArbitrary = nullptr;
36         }
37     }
38 }
39 
Scale(const Scale & other)40 Scale::Scale(const Scale& other)
41         : fMagnitude(other.fMagnitude), fArbitrary(nullptr), fError(other.fError) {
42     if (other.fArbitrary != nullptr) {
43         UErrorCode localStatus = U_ZERO_ERROR;
44         fArbitrary = new DecNum(*other.fArbitrary, localStatus);
45     }
46 }
47 
operator =(const Scale & other)48 Scale& Scale::operator=(const Scale& other) {
49     if (this == &other) { return *this; }  // self-assignment: no-op
50     fMagnitude = other.fMagnitude;
51     if (other.fArbitrary != nullptr) {
52         UErrorCode localStatus = U_ZERO_ERROR;
53         fArbitrary = new DecNum(*other.fArbitrary, localStatus);
54     } else {
55         fArbitrary = nullptr;
56     }
57     fError = other.fError;
58     return *this;
59 }
60 
Scale(Scale && src)61 Scale::Scale(Scale&& src) U_NOEXCEPT
62         : fMagnitude(src.fMagnitude), fArbitrary(src.fArbitrary), fError(src.fError) {
63     // Take ownership away from src if necessary
64     src.fArbitrary = nullptr;
65 }
66 
operator =(Scale && src)67 Scale& Scale::operator=(Scale&& src) U_NOEXCEPT {
68     fMagnitude = src.fMagnitude;
69     if (fArbitrary != nullptr) {
70         delete fArbitrary;
71     }
72     fArbitrary = src.fArbitrary;
73     fError = src.fError;
74     // Take ownership away from src if necessary
75     src.fArbitrary = nullptr;
76     return *this;
77 }
78 
~Scale()79 Scale::~Scale() {
80     delete fArbitrary;
81 }
82 
83 
none()84 Scale Scale::none() {
85     return {0, nullptr};
86 }
87 
powerOfTen(int32_t power)88 Scale Scale::powerOfTen(int32_t power) {
89     return {power, nullptr};
90 }
91 
byDecimal(StringPiece multiplicand)92 Scale Scale::byDecimal(StringPiece multiplicand) {
93     UErrorCode localError = U_ZERO_ERROR;
94     LocalPointer<DecNum> decnum(new DecNum(), localError);
95     if (U_FAILURE(localError)) {
96         return {localError};
97     }
98     decnum->setTo(multiplicand, localError);
99     if (U_FAILURE(localError)) {
100         return {localError};
101     }
102     return {0, decnum.orphan()};
103 }
104 
byDouble(double multiplicand)105 Scale Scale::byDouble(double multiplicand) {
106     UErrorCode localError = U_ZERO_ERROR;
107     LocalPointer<DecNum> decnum(new DecNum(), localError);
108     if (U_FAILURE(localError)) {
109         return {localError};
110     }
111     decnum->setTo(multiplicand, localError);
112     if (U_FAILURE(localError)) {
113         return {localError};
114     }
115     return {0, decnum.orphan()};
116 }
117 
byDoubleAndPowerOfTen(double multiplicand,int32_t power)118 Scale Scale::byDoubleAndPowerOfTen(double multiplicand, int32_t power) {
119     UErrorCode localError = U_ZERO_ERROR;
120     LocalPointer<DecNum> decnum(new DecNum(), localError);
121     if (U_FAILURE(localError)) {
122         return {localError};
123     }
124     decnum->setTo(multiplicand, localError);
125     if (U_FAILURE(localError)) {
126         return {localError};
127     }
128     return {power, decnum.orphan()};
129 }
130 
applyTo(impl::DecimalQuantity & quantity) const131 void Scale::applyTo(impl::DecimalQuantity& quantity) const {
132     quantity.adjustMagnitude(fMagnitude);
133     if (fArbitrary != nullptr) {
134         UErrorCode localStatus = U_ZERO_ERROR;
135         quantity.multiplyBy(*fArbitrary, localStatus);
136     }
137 }
138 
applyReciprocalTo(impl::DecimalQuantity & quantity) const139 void Scale::applyReciprocalTo(impl::DecimalQuantity& quantity) const {
140     quantity.adjustMagnitude(-fMagnitude);
141     if (fArbitrary != nullptr) {
142         UErrorCode localStatus = U_ZERO_ERROR;
143         quantity.divideBy(*fArbitrary, localStatus);
144     }
145 }
146 
147 
148 void
setAndChain(const Scale & multiplier,const MicroPropsGenerator * parent)149 MultiplierFormatHandler::setAndChain(const Scale& multiplier, const MicroPropsGenerator* parent) {
150     fMultiplier = multiplier;
151     fParent = parent;
152 }
153 
processQuantity(DecimalQuantity & quantity,MicroProps & micros,UErrorCode & status) const154 void MultiplierFormatHandler::processQuantity(DecimalQuantity& quantity, MicroProps& micros,
155                                               UErrorCode& status) const {
156     fParent->processQuantity(quantity, micros, status);
157     fMultiplier.applyTo(quantity);
158 }
159 
160 #endif /* #if !UCONFIG_NO_FORMATTING */
161