| 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 (U_FAILURE(status)) { |
| 44 |
return; |
| 45 |
} |
| 46 |
if (fHasError) { |
| 47 |
status = U_ILLEGAL_ARGUMENT_ERROR; |
| 48 |
} else if (fUnion.minMaxInt.fMaxInt == -1) { |
| 49 |
quantity.increaseMinIntegerTo(fUnion.minMaxInt.fMinInt); |
| 50 |
} else { |
| 51 |
// Enforce the backwards-compatibility feature "FormatFailIfMoreThanMaxDigits" |
| 52 |
if (fUnion.minMaxInt.fFormatFailIfMoreThanMaxDigits && |
| 53 |
fUnion.minMaxInt.fMaxInt < quantity.getMagnitude()) { |
| 54 |
status = U_ILLEGAL_ARGUMENT_ERROR; |
| 55 |
} |
| 56 |
quantity.increaseMinIntegerTo(fUnion.minMaxInt.fMinInt); |
| 57 |
quantity.applyMaxInteger(fUnion.minMaxInt.fMaxInt); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
bool IntegerWidth::operator==(const IntegerWidth& other) const { |
| 62 |
// Private operator==; do error and bogus checking first! |
| 63 |
U_ASSERT(!fHasError); |
| 64 |
U_ASSERT(!other.fHasError); |
| 65 |
U_ASSERT(!isBogus()); |
| 66 |
U_ASSERT(!other.isBogus()); |
| 67 |
return fUnion.minMaxInt.fMinInt == other.fUnion.minMaxInt.fMinInt && |
| 68 |
fUnion.minMaxInt.fMaxInt == other.fUnion.minMaxInt.fMaxInt; |
| 69 |
} |
| 70 |
|
| 71 |
#endif /* #if !UCONFIG_NO_FORMATTING */ |