/src/icu/source/i18n/number_integerwidth.cpp
Line | Count | Source (jump to first uncovered line) |
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 | 0 | IntegerWidth::IntegerWidth(digits_t minInt, digits_t maxInt, bool formatFailIfMoreThanMaxDigits) { |
17 | 0 | fUnion.minMaxInt.fMinInt = minInt; |
18 | 0 | fUnion.minMaxInt.fMaxInt = maxInt; |
19 | 0 | fUnion.minMaxInt.fFormatFailIfMoreThanMaxDigits = formatFailIfMoreThanMaxDigits; |
20 | 0 | } |
21 | | |
22 | 0 | IntegerWidth IntegerWidth::zeroFillTo(int32_t minInt) { |
23 | 0 | if (minInt >= 0 && minInt <= kMaxIntFracSig) { |
24 | 0 | return {static_cast<digits_t>(minInt), -1, false}; |
25 | 0 | } else { |
26 | 0 | return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR}; |
27 | 0 | } |
28 | 0 | } |
29 | | |
30 | 0 | IntegerWidth IntegerWidth::truncateAt(int32_t maxInt) { |
31 | 0 | if (fHasError) { return *this; } // No-op on error |
32 | 0 | digits_t minInt = fUnion.minMaxInt.fMinInt; |
33 | 0 | if (maxInt >= 0 && maxInt <= kMaxIntFracSig && minInt <= maxInt) { |
34 | 0 | return {minInt, static_cast<digits_t>(maxInt), false}; |
35 | 0 | } else if (maxInt == -1) { |
36 | 0 | return {minInt, -1, false}; |
37 | 0 | } else { |
38 | 0 | return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR}; |
39 | 0 | } |
40 | 0 | } |
41 | | |
42 | 0 | void IntegerWidth::apply(impl::DecimalQuantity& quantity, UErrorCode& status) const { |
43 | 0 | if (U_FAILURE(status)) { |
44 | 0 | return; |
45 | 0 | } |
46 | 0 | if (fHasError) { |
47 | 0 | status = U_ILLEGAL_ARGUMENT_ERROR; |
48 | 0 | } else if (fUnion.minMaxInt.fMaxInt == -1) { |
49 | 0 | quantity.setMinInteger(fUnion.minMaxInt.fMinInt); |
50 | 0 | } else { |
51 | | // Enforce the backwards-compatibility feature "FormatFailIfMoreThanMaxDigits" |
52 | 0 | if (fUnion.minMaxInt.fFormatFailIfMoreThanMaxDigits && |
53 | 0 | fUnion.minMaxInt.fMaxInt < quantity.getMagnitude()) { |
54 | 0 | status = U_ILLEGAL_ARGUMENT_ERROR; |
55 | 0 | } |
56 | 0 | quantity.setMinInteger(fUnion.minMaxInt.fMinInt); |
57 | 0 | quantity.applyMaxInteger(fUnion.minMaxInt.fMaxInt); |
58 | 0 | } |
59 | 0 | } |
60 | | |
61 | 0 | bool IntegerWidth::operator==(const IntegerWidth& other) const { |
62 | | // Private operator==; do error and bogus checking first! |
63 | 0 | U_ASSERT(!fHasError); |
64 | 0 | U_ASSERT(!other.fHasError); |
65 | 0 | U_ASSERT(!isBogus()); |
66 | 0 | U_ASSERT(!other.isBogus()); |
67 | 0 | return fUnion.minMaxInt.fMinInt == other.fUnion.minMaxInt.fMinInt && |
68 | 0 | fUnion.minMaxInt.fMaxInt == other.fUnion.minMaxInt.fMaxInt; |
69 | 0 | } |
70 | | |
71 | | #endif /* #if !UCONFIG_NO_FORMATTING */ |