Coverage Report

Created: 2022-11-20 06:20

/src/icu/icu4c/source/i18n/numparse_scientific.cpp
Line
Count
Source (jump to first uncovered line)
1
// © 2018 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
// Allow implicit conversion from char16_t* to UnicodeString for this file:
9
// Helpful in toString methods and elsewhere.
10
#define UNISTR_FROM_STRING_EXPLICIT
11
12
#include "numparse_types.h"
13
#include "numparse_scientific.h"
14
#include "static_unicode_sets.h"
15
#include "string_segment.h"
16
17
using namespace icu;
18
using namespace icu::numparse;
19
using namespace icu::numparse::impl;
20
21
22
namespace {
23
24
3.91k
inline const UnicodeSet& minusSignSet() {
25
3.91k
    return *unisets::get(unisets::MINUS_SIGN);
26
3.91k
}
27
28
3.61k
inline const UnicodeSet& plusSignSet() {
29
3.61k
    return *unisets::get(unisets::PLUS_SIGN);
30
3.61k
}
31
32
} // namespace
33
34
35
ScientificMatcher::ScientificMatcher(const DecimalFormatSymbols& dfs, const Grouper& grouper)
36
        : fExponentSeparatorString(dfs.getConstSymbol(DecimalFormatSymbols::kExponentialSymbol)),
37
          fExponentMatcher(dfs, grouper, PARSE_FLAG_INTEGER_ONLY | PARSE_FLAG_GROUPING_DISABLED),
38
2.90k
          fIgnorablesMatcher(PARSE_FLAG_STRICT_IGNORABLES) {
39
40
2.90k
    const UnicodeString& minusSign = dfs.getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol);
41
2.90k
    if (minusSignSet().contains(minusSign)) {
42
2.58k
        fCustomMinusSign.setToBogus();
43
2.58k
    } else {
44
314
        fCustomMinusSign = minusSign;
45
314
    }
46
47
2.90k
    const UnicodeString& plusSign = dfs.getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol);
48
2.90k
    if (plusSignSet().contains(plusSign)) {
49
2.58k
        fCustomPlusSign.setToBogus();
50
2.58k
    } else {
51
314
        fCustomPlusSign = plusSign;
52
314
    }
53
2.90k
}
54
55
1.03k
bool ScientificMatcher::match(StringSegment& segment, ParsedNumber& result, UErrorCode& status) const {
56
    // Only accept scientific notation after the mantissa.
57
1.03k
    if (!result.seenNumber()) {
58
5
        return false;
59
5
    }
60
61
    // Only accept one exponent per string.
62
1.02k
    if (0 != (result.flags & FLAG_HAS_EXPONENT)) {
63
4
        return false;
64
4
    }
65
66
    // First match the scientific separator, and then match another number after it.
67
    // NOTE: This is guarded by the smoke test; no need to check fExponentSeparatorString length again.
68
1.02k
    int32_t initialOffset = segment.getOffset();
69
1.02k
    int32_t overlap = segment.getCommonPrefixLength(fExponentSeparatorString);
70
1.02k
    if (overlap == fExponentSeparatorString.length()) {
71
        // Full exponent separator match.
72
73
        // First attempt to get a code point, returning true if we can't get one.
74
1.01k
        if (segment.length() == overlap) {
75
3
            return true;
76
3
        }
77
1.01k
        segment.adjustOffset(overlap);
78
79
        // Allow ignorables before the sign.
80
        // Note: call site is guarded by the segment.length() check above.
81
        // Note: the ignorables matcher should not touch the result.
82
1.01k
        fIgnorablesMatcher.match(segment, result, status);
83
1.01k
        if (segment.length() == 0) {
84
1
            segment.setOffset(initialOffset);
85
1
            return true;
86
1
        }
87
88
        // Allow a sign, and then try to match digits.
89
1.01k
        int8_t exponentSign = 1;
90
1.01k
        if (segment.startsWith(minusSignSet())) {
91
301
            exponentSign = -1;
92
301
            segment.adjustOffsetByCodePoint();
93
711
        } else if (segment.startsWith(plusSignSet())) {
94
7
            segment.adjustOffsetByCodePoint();
95
704
        } else if (segment.startsWith(fCustomMinusSign)) {
96
2
            overlap = segment.getCommonPrefixLength(fCustomMinusSign);
97
2
            if (overlap != fCustomMinusSign.length()) {
98
                // Partial custom sign match
99
1
                segment.setOffset(initialOffset);
100
1
                return true;
101
1
            }
102
1
            exponentSign = -1;
103
1
            segment.adjustOffset(overlap);
104
702
        } else if (segment.startsWith(fCustomPlusSign)) {
105
0
            overlap = segment.getCommonPrefixLength(fCustomPlusSign);
106
0
            if (overlap != fCustomPlusSign.length()) {
107
                // Partial custom sign match
108
0
                segment.setOffset(initialOffset);
109
0
                return true;
110
0
            }
111
0
            segment.adjustOffset(overlap);
112
0
        }
113
114
        // Return true if the segment is empty.
115
1.01k
        if (segment.length() == 0) {
116
9
            segment.setOffset(initialOffset);
117
9
            return true;
118
9
        }
119
120
        // Allow ignorables after the sign.
121
        // Note: call site is guarded by the segment.length() check above.
122
        // Note: the ignorables matcher should not touch the result.
123
1.00k
        fIgnorablesMatcher.match(segment, result, status);
124
1.00k
        if (segment.length() == 0) {
125
1
            segment.setOffset(initialOffset);
126
1
            return true;
127
1
        }
128
129
        // We are supposed to accept E0 after NaN, so we need to make sure result.quantity is available.
130
1.00k
        bool wasBogus = result.quantity.bogus;
131
1.00k
        result.quantity.bogus = false;
132
1.00k
        int digitsOffset = segment.getOffset();
133
1.00k
        bool digitsReturnValue = fExponentMatcher.match(segment, result, exponentSign, status);
134
1.00k
        result.quantity.bogus = wasBogus;
135
136
1.00k
        if (segment.getOffset() != digitsOffset) {
137
            // At least one exponent digit was matched.
138
955
            result.flags |= FLAG_HAS_EXPONENT;
139
955
        } else {
140
            // No exponent digits were matched
141
46
            segment.setOffset(initialOffset);
142
46
        }
143
1.00k
        return digitsReturnValue;
144
145
1.00k
    } else if (overlap == segment.length()) {
146
        // Partial exponent separator match
147
1
        return true;
148
1
    }
149
150
    // No match
151
5
    return false;
152
1.02k
}
153
154
1.94k
bool ScientificMatcher::smokeTest(const StringSegment& segment) const {
155
1.94k
    return segment.startsWith(fExponentSeparatorString);
156
1.94k
}
157
158
0
UnicodeString ScientificMatcher::toString() const {
159
0
    return u"<Scientific>";
160
0
}
161
162
163
#endif /* #if !UCONFIG_NO_FORMATTING */