]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/numparse_compositions.cpp
1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #include "unicode/utypes.h"
6 #if !UCONFIG_NO_FORMATTING
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
12 #include "numparse_types.h"
13 #include "numparse_compositions.h"
14 #include "unicode/uniset.h"
17 using namespace icu::numparse
;
18 using namespace icu::numparse::impl
;
21 bool SeriesMatcher::match(StringSegment
& segment
, ParsedNumber
& result
, UErrorCode
& status
) const {
22 ParsedNumber
backup(result
);
24 int32_t initialOffset
= segment
.getOffset();
25 bool maybeMore
= true;
26 for (auto* it
= begin(); it
< end();) {
27 const NumberParseMatcher
* matcher
= *it
;
28 int matcherOffset
= segment
.getOffset();
29 if (segment
.length() != 0) {
30 maybeMore
= matcher
->match(segment
, result
, status
);
32 // Nothing for this matcher to match; ask for more.
36 bool success
= (segment
.getOffset() != matcherOffset
);
37 bool isFlexible
= matcher
->isFlexible();
38 if (success
&& isFlexible
) {
39 // Match succeeded, and this is a flexible matcher. Re-run it.
41 // Match succeeded, and this is NOT a flexible matcher. Proceed to the next matcher.
43 // Small hack: if there is another matcher coming, do not accept trailing weak chars.
44 // Needed for proper handling of currency spacing.
45 if (it
< end() && segment
.getOffset() != result
.charEnd
&& result
.charEnd
> matcherOffset
) {
46 segment
.setOffset(result
.charEnd
);
48 } else if (isFlexible
) {
49 // Match failed, and this is a flexible matcher. Try again with the next matcher.
52 // Match failed, and this is NOT a flexible matcher. Exit.
53 segment
.setOffset(initialOffset
);
59 // All matchers in the series succeeded.
63 bool SeriesMatcher::smokeTest(const StringSegment
& segment
) const {
64 // NOTE: The range-based for loop calls the virtual begin() and end() methods.
65 // NOTE: We only want the first element. Use the for loop for boundary checking.
66 for (auto& matcher
: *this) {
67 // SeriesMatchers are never allowed to start with a Flexible matcher.
68 U_ASSERT(!matcher
->isFlexible());
69 return matcher
->smokeTest(segment
);
74 void SeriesMatcher::postProcess(ParsedNumber
& result
) const {
75 // NOTE: The range-based for loop calls the virtual begin() and end() methods.
76 for (auto* matcher
: *this) {
77 matcher
->postProcess(result
);
82 ArraySeriesMatcher::ArraySeriesMatcher()
86 ArraySeriesMatcher::ArraySeriesMatcher(MatcherArray
& matchers
, int32_t matchersLen
)
87 : fMatchers(std::move(matchers
)), fMatchersLen(matchersLen
) {
90 int32_t ArraySeriesMatcher::length() const {
94 const NumberParseMatcher
* const* ArraySeriesMatcher::begin() const {
95 return fMatchers
.getAlias();
98 const NumberParseMatcher
* const* ArraySeriesMatcher::end() const {
99 return fMatchers
.getAlias() + fMatchersLen
;
102 UnicodeString
ArraySeriesMatcher::toString() const {
103 return u
"<ArraySeries>";
107 #endif /* #if !UCONFIG_NO_FORMATTING */