Coverage Report

Created: 2025-06-24 06:54

/src/icu/icu4c/source/i18n/numparse_compositions.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_compositions.h"
14
#include "string_segment.h"
15
#include "unicode/uniset.h"
16
17
using namespace icu;
18
using namespace icu::numparse;
19
using namespace icu::numparse::impl;
20
21
22
78.7k
bool SeriesMatcher::match(StringSegment& segment, ParsedNumber& result, UErrorCode& status) const {
23
78.7k
    ParsedNumber backup(result);
24
25
78.7k
    int32_t initialOffset = segment.getOffset();
26
78.7k
    bool maybeMore = true;
27
24.1M
    for (const auto* it = begin(); it < end();) {
28
24.1M
        const NumberParseMatcher* matcher = *it;
29
24.1M
        int matcherOffset = segment.getOffset();
30
24.1M
        if (segment.length() != 0) {
31
24.1M
            maybeMore = matcher->match(segment, result, status);
32
24.1M
        } else {
33
            // Nothing for this matcher to match; ask for more.
34
47
            maybeMore = true;
35
47
        }
36
37
24.1M
        bool success = (segment.getOffset() != matcherOffset);
38
24.1M
        bool isFlexible = matcher->isFlexible();
39
24.1M
        if (success && isFlexible) {
40
            // Match succeeded, and this is a flexible matcher. Re-run it.
41
24.1M
        } else if (success) {
42
            // Match succeeded, and this is NOT a flexible matcher. Proceed to the next matcher.
43
24.0M
            it++;
44
            // Small hack: if there is another matcher coming, do not accept trailing weak chars.
45
            // Needed for proper handling of currency spacing.
46
24.0M
            if (it < end() && segment.getOffset() != result.charEnd && result.charEnd > matcherOffset) {
47
1
                segment.setOffset(result.charEnd);
48
1
            }
49
24.0M
        } else if (isFlexible) {
50
            // Match failed, and this is a flexible matcher. Try again with the next matcher.
51
0
            it++;
52
39.7k
        } else {
53
            // Match failed, and this is NOT a flexible matcher. Exit.
54
39.7k
            segment.setOffset(initialOffset);
55
39.7k
            result = backup;
56
39.7k
            return maybeMore;
57
39.7k
        }
58
24.1M
    }
59
60
    // All matchers in the series succeeded.
61
39.0k
    return maybeMore;
62
78.7k
}
63
64
12.8M
bool SeriesMatcher::smokeTest(const StringSegment& segment) const {
65
    // NOTE: The range-based for loop calls the virtual begin() and end() methods.
66
    // NOTE: We only want the first element. Use the for loop for boundary checking.
67
12.8M
    for (const auto& matcher : *this) {
68
        // SeriesMatchers are never allowed to start with a Flexible matcher.
69
12.8M
        U_ASSERT(!matcher->isFlexible());
70
12.8M
        return matcher->smokeTest(segment);
71
12.8M
    }
72
0
    return false;
73
12.8M
}
74
75
37.6k
void SeriesMatcher::postProcess(ParsedNumber& result) const {
76
    // NOTE: The range-based for loop calls the virtual begin() and end() methods.
77
19.5M
    for (const auto* matcher : *this) {
78
19.5M
        matcher->postProcess(result);
79
19.5M
    }
80
37.6k
}
81
82
83
ArraySeriesMatcher::ArraySeriesMatcher()
84
2.78M
        : fMatchersLen(0) {
85
2.78M
}
86
87
ArraySeriesMatcher::ArraySeriesMatcher(MatcherArray& matchers, int32_t matchersLen)
88
163k
        : fMatchers(std::move(matchers)), fMatchersLen(matchersLen) {
89
163k
}
90
91
0
int32_t ArraySeriesMatcher::length() const {
92
0
    return fMatchersLen;
93
0
}
94
95
12.9M
const NumberParseMatcher* const* ArraySeriesMatcher::begin() const {
96
12.9M
    return fMatchers.getAlias();
97
12.9M
}
98
99
61.1M
const NumberParseMatcher* const* ArraySeriesMatcher::end() const {
100
61.1M
    return fMatchers.getAlias() + fMatchersLen;
101
61.1M
}
102
103
0
UnicodeString ArraySeriesMatcher::toString() const {
104
0
    return u"<ArraySeries>";
105
0
}
106
107
108
#endif /* #if !UCONFIG_NO_FORMATTING */