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