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 && !UPRV_INCOMPLETE_CPP11_SUPPORT
7
8#include "unicode/numberformatter.h"
9#include "number_types.h"
10#include "number_decimalquantity.h"
11
12using namespace icu;
13using namespace icu::number;
14using namespace icu::number::impl;
15
16IntegerWidth::IntegerWidth(int8_t minInt, int8_t maxInt) {
17    fUnion.minMaxInt.fMinInt = minInt;
18    fUnion.minMaxInt.fMaxInt = maxInt;
19}
20
21IntegerWidth IntegerWidth::zeroFillTo(int32_t minInt) {
22    if (minInt >= 0 && minInt <= kMaxIntFracSig) {
23        return {static_cast<int8_t>(minInt), -1};
24    } else {
25        return {U_NUMBER_DIGIT_WIDTH_OUTOFBOUNDS_ERROR};
26    }
27}
28
29IntegerWidth IntegerWidth::truncateAt(int32_t maxInt) {
30    if (fHasError) { return *this; }  // No-op on error
31    if (maxInt >= 0 && maxInt <= kMaxIntFracSig) {
32        return {fUnion.minMaxInt.fMinInt, static_cast<int8_t>(maxInt)};
33    } else {
34        return {U_NUMBER_DIGIT_WIDTH_OUTOFBOUNDS_ERROR};
35    }
36}
37
38void IntegerWidth::apply(impl::DecimalQuantity &quantity, UErrorCode &status) const {
39    if (fHasError) {
40        status = U_ILLEGAL_ARGUMENT_ERROR;
41    } else if (fUnion.minMaxInt.fMaxInt == -1) {
42        quantity.setIntegerLength(fUnion.minMaxInt.fMinInt, INT32_MAX);
43    } else {
44        quantity.setIntegerLength(fUnion.minMaxInt.fMinInt, fUnion.minMaxInt.fMaxInt);
45    }
46}
47
48#endif /* #if !UCONFIG_NO_FORMATTING */
49