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