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 | |
8 | #include "unicode/numberformatter.h" |
9 | #include "number_types.h" |
10 | #include "number_decimalquantity.h" |
11 | |
12 | using namespace icu; |
13 | using namespace icu::number; |
14 | using namespace icu::number::impl; |
15 | |
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; |
20 | } |
21 | |
22 | IntegerWidth IntegerWidth::zeroFillTo(int32_t minInt) { |
23 | if (minInt >= 0 && minInt <= kMaxIntFracSig) { |
24 | return {static_cast<digits_t>(minInt), -1, false}; |
25 | } else { |
26 | return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR}; |
27 | } |
28 | } |
29 | |
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}; |
37 | } else { |
38 | return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR}; |
39 | } |
40 | } |
41 | |
42 | void IntegerWidth::apply(impl::DecimalQuantity& quantity, UErrorCode& status) const { |
43 | if (fHasError) { |
44 | status = U_ILLEGAL_ARGUMENT_ERROR; |
45 | } else if (fUnion.minMaxInt.fMaxInt == -1) { |
46 | quantity.setMinInteger(fUnion.minMaxInt.fMinInt); |
47 | } else { |
48 | // Enforce the backwards-compatibility feature "FormatFailIfMoreThanMaxDigits" |
49 | if (fUnion.minMaxInt.fFormatFailIfMoreThanMaxDigits && |
50 | fUnion.minMaxInt.fMaxInt < quantity.getMagnitude()) { |
51 | status = U_ILLEGAL_ARGUMENT_ERROR; |
52 | } |
53 | quantity.setMinInteger(fUnion.minMaxInt.fMinInt); |
54 | quantity.applyMaxInteger(fUnion.minMaxInt.fMaxInt); |
55 | } |
56 | } |
57 | |
58 | bool IntegerWidth::operator==(const IntegerWidth& other) const { |
59 | // Private operator==; do error and bogus checking first! |
60 | U_ASSERT(!fHasError); |
61 | U_ASSERT(!other.fHasError); |
62 | U_ASSERT(!isBogus()); |
63 | U_ASSERT(!other.isBogus()); |
64 | return fUnion.minMaxInt.fMinInt == other.fUnion.minMaxInt.fMinInt && |
65 | fUnion.minMaxInt.fMaxInt == other.fUnion.minMaxInt.fMaxInt; |
66 | } |
67 | |
68 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
69 | |