Coverage Report

Created: 2025-06-13 06:38

/src/icu/icu4c/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
516k
IntegerWidth::IntegerWidth(digits_t minInt, digits_t maxInt, bool formatFailIfMoreThanMaxDigits) {
17
516k
    fUnion.minMaxInt.fMinInt = minInt;
18
516k
    fUnion.minMaxInt.fMaxInt = maxInt;
19
516k
    fUnion.minMaxInt.fFormatFailIfMoreThanMaxDigits = formatFailIfMoreThanMaxDigits;
20
516k
}
21
22
10.6k
IntegerWidth IntegerWidth::zeroFillTo(int32_t minInt) {
23
10.6k
    if (minInt >= 0 && minInt <= kMaxIntFracSig) {
24
10.5k
        return {static_cast<digits_t>(minInt), -1, false};
25
10.5k
    } else {
26
7
        return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
27
7
    }
28
10.6k
}
29
30
1.28k
IntegerWidth IntegerWidth::truncateAt(int32_t maxInt) {
31
1.28k
    if (fHasError) { return *this; }  // No-op on error
32
1.28k
    digits_t minInt = fUnion.minMaxInt.fMinInt;
33
1.28k
    if (maxInt >= 0 && maxInt <= kMaxIntFracSig && minInt <= maxInt) {
34
1.28k
        return {minInt, static_cast<digits_t>(maxInt), false};
35
1.28k
    } else if (maxInt == -1) {
36
0
        return {minInt, -1, false};
37
0
    } else {
38
0
        return {U_NUMBER_ARG_OUTOFBOUNDS_ERROR};
39
0
    }
40
1.28k
}
41
42
1.27M
void IntegerWidth::apply(impl::DecimalQuantity& quantity, UErrorCode& status) const {
43
1.27M
    if (U_FAILURE(status)) {
44
0
        return;
45
0
    }
46
1.27M
    if (fHasError) {
47
0
        status = U_ILLEGAL_ARGUMENT_ERROR;
48
1.27M
    } else if (fUnion.minMaxInt.fMaxInt == -1) {
49
1.27M
        quantity.increaseMinIntegerTo(fUnion.minMaxInt.fMinInt);
50
1.27M
    } 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.increaseMinIntegerTo(fUnion.minMaxInt.fMinInt);
57
0
        quantity.applyMaxInteger(fUnion.minMaxInt.fMaxInt);
58
0
    }
59
1.27M
}
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 */