Coverage Report

Created: 2018-09-25 14:53

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