1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #include "unicode/utypes.h"
6 #if !UCONFIG_NO_FORMATTING
8 #include "unicode/numberformatter.h"
9 #include "number_types.h"
10 #include "number_decimalquantity.h"
13 using namespace icu::number
;
14 using namespace icu::number::impl
;
16 IntegerWidth::IntegerWidth(digits_t minInt
, digits_t maxInt
, bool formatFailIfMoreThanMaxDigits
) {
17 fUnion
.minMaxInt
.fMinInt
= minInt
;
18 fUnion
.minMaxInt
.fMaxInt
= maxInt
;
19 fUnion
.minMaxInt
.fFormatFailIfMoreThanMaxDigits
= formatFailIfMoreThanMaxDigits
;
22 IntegerWidth
IntegerWidth::zeroFillTo(int32_t minInt
) {
23 if (minInt
>= 0 && minInt
<= kMaxIntFracSig
) {
24 return {static_cast<digits_t
>(minInt
), -1, false};
26 return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR
};
30 IntegerWidth
IntegerWidth::truncateAt(int32_t maxInt
) {
31 if (fHasError
) { return *this; } // No-op on error
32 digits_t minInt
= fUnion
.minMaxInt
.fMinInt
;
33 if (maxInt
>= 0 && maxInt
<= kMaxIntFracSig
&& minInt
<= maxInt
) {
34 return {minInt
, static_cast<digits_t
>(maxInt
), false};
35 } else if (maxInt
== -1) {
36 return {minInt
, -1, false};
38 return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR
};
42 void IntegerWidth::apply(impl::DecimalQuantity
& quantity
, UErrorCode
& status
) const {
44 status
= U_ILLEGAL_ARGUMENT_ERROR
;
45 } else if (fUnion
.minMaxInt
.fMaxInt
== -1) {
46 quantity
.setMinInteger(fUnion
.minMaxInt
.fMinInt
);
48 // Enforce the backwards-compatibility feature "FormatFailIfMoreThanMaxDigits"
49 if (fUnion
.minMaxInt
.fFormatFailIfMoreThanMaxDigits
&&
50 fUnion
.minMaxInt
.fMaxInt
< quantity
.getMagnitude()) {
51 status
= U_ILLEGAL_ARGUMENT_ERROR
;
53 quantity
.setMinInteger(fUnion
.minMaxInt
.fMinInt
);
54 quantity
.applyMaxInteger(fUnion
.minMaxInt
.fMaxInt
);
58 bool IntegerWidth::operator==(const IntegerWidth
& other
) const {
59 // Private operator==; do error and bogus checking first!
61 U_ASSERT(!other
.fHasError
);
63 U_ASSERT(!other
.isBogus());
64 return fUnion
.minMaxInt
.fMinInt
== other
.fUnion
.minMaxInt
.fMinInt
&&
65 fUnion
.minMaxInt
.fMaxInt
== other
.fUnion
.minMaxInt
.fMaxInt
;
68 #endif /* #if !UCONFIG_NO_FORMATTING */