/src/mozilla-central/intl/icu/source/i18n/numparse_symbols.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_symbols.h" |
14 | | #include "numparse_utils.h" |
15 | | |
16 | | using namespace icu; |
17 | | using namespace icu::numparse; |
18 | | using namespace icu::numparse::impl; |
19 | | |
20 | | |
21 | 0 | SymbolMatcher::SymbolMatcher(const UnicodeString& symbolString, unisets::Key key) { |
22 | 0 | fUniSet = unisets::get(key); |
23 | 0 | if (fUniSet->contains(symbolString)) { |
24 | 0 | fString.setToBogus(); |
25 | 0 | } else { |
26 | 0 | fString = symbolString; |
27 | 0 | } |
28 | 0 | } |
29 | | |
30 | 0 | const UnicodeSet* SymbolMatcher::getSet() const { |
31 | 0 | return fUniSet; |
32 | 0 | } |
33 | | |
34 | 0 | bool SymbolMatcher::match(StringSegment& segment, ParsedNumber& result, UErrorCode&) const { |
35 | 0 | // Smoke test first; this matcher might be disabled. |
36 | 0 | if (isDisabled(result)) { |
37 | 0 | return false; |
38 | 0 | } |
39 | 0 | |
40 | 0 | // Test the string first in order to consume trailing chars greedily. |
41 | 0 | int overlap = 0; |
42 | 0 | if (!fString.isEmpty()) { |
43 | 0 | overlap = segment.getCommonPrefixLength(fString); |
44 | 0 | if (overlap == fString.length()) { |
45 | 0 | segment.adjustOffset(fString.length()); |
46 | 0 | accept(segment, result); |
47 | 0 | return false; |
48 | 0 | } |
49 | 0 | } |
50 | 0 | |
51 | 0 | int cp = segment.getCodePoint(); |
52 | 0 | if (cp != -1 && fUniSet->contains(cp)) { |
53 | 0 | segment.adjustOffset(U16_LENGTH(cp)); |
54 | 0 | accept(segment, result); |
55 | 0 | return false; |
56 | 0 | } |
57 | 0 |
|
58 | 0 | return overlap == segment.length(); |
59 | 0 | } |
60 | | |
61 | 0 | bool SymbolMatcher::smokeTest(const StringSegment& segment) const { |
62 | 0 | return segment.startsWith(*fUniSet) || segment.startsWith(fString); |
63 | 0 | } |
64 | | |
65 | 0 | UnicodeString SymbolMatcher::toString() const { |
66 | 0 | // TODO: Customize output for each symbol |
67 | 0 | return u"<Symbol>"; |
68 | 0 | } |
69 | | |
70 | | |
71 | | IgnorablesMatcher::IgnorablesMatcher(unisets::Key key) |
72 | 0 | : SymbolMatcher({}, key) { |
73 | 0 | } |
74 | | |
75 | 0 | bool IgnorablesMatcher::isFlexible() const { |
76 | 0 | return true; |
77 | 0 | } |
78 | | |
79 | 0 | UnicodeString IgnorablesMatcher::toString() const { |
80 | 0 | return u"<Ignorables>"; |
81 | 0 | } |
82 | | |
83 | 0 | bool IgnorablesMatcher::isDisabled(const ParsedNumber&) const { |
84 | 0 | return false; |
85 | 0 | } |
86 | | |
87 | 0 | void IgnorablesMatcher::accept(StringSegment&, ParsedNumber&) const { |
88 | 0 | // No-op |
89 | 0 | } |
90 | | |
91 | | |
92 | | InfinityMatcher::InfinityMatcher(const DecimalFormatSymbols& dfs) |
93 | 0 | : SymbolMatcher(dfs.getConstSymbol(DecimalFormatSymbols::kInfinitySymbol), unisets::INFINITY_KEY) { |
94 | 0 | } |
95 | | |
96 | 0 | bool InfinityMatcher::isDisabled(const ParsedNumber& result) const { |
97 | 0 | return 0 != (result.flags & FLAG_INFINITY); |
98 | 0 | } |
99 | | |
100 | 0 | void InfinityMatcher::accept(StringSegment& segment, ParsedNumber& result) const { |
101 | 0 | result.flags |= FLAG_INFINITY; |
102 | 0 | result.setCharsConsumed(segment); |
103 | 0 | } |
104 | | |
105 | | |
106 | | MinusSignMatcher::MinusSignMatcher(const DecimalFormatSymbols& dfs, bool allowTrailing) |
107 | | : SymbolMatcher(dfs.getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol), unisets::MINUS_SIGN), |
108 | 0 | fAllowTrailing(allowTrailing) { |
109 | 0 | } |
110 | | |
111 | 0 | bool MinusSignMatcher::isDisabled(const ParsedNumber& result) const { |
112 | 0 | return !fAllowTrailing && result.seenNumber(); |
113 | 0 | } |
114 | | |
115 | 0 | void MinusSignMatcher::accept(StringSegment& segment, ParsedNumber& result) const { |
116 | 0 | result.flags |= FLAG_NEGATIVE; |
117 | 0 | result.setCharsConsumed(segment); |
118 | 0 | } |
119 | | |
120 | | |
121 | | NanMatcher::NanMatcher(const DecimalFormatSymbols& dfs) |
122 | 0 | : SymbolMatcher(dfs.getConstSymbol(DecimalFormatSymbols::kNaNSymbol), unisets::EMPTY) { |
123 | 0 | } |
124 | | |
125 | 0 | bool NanMatcher::isDisabled(const ParsedNumber& result) const { |
126 | 0 | return result.seenNumber(); |
127 | 0 | } |
128 | | |
129 | 0 | void NanMatcher::accept(StringSegment& segment, ParsedNumber& result) const { |
130 | 0 | result.flags |= FLAG_NAN; |
131 | 0 | result.setCharsConsumed(segment); |
132 | 0 | } |
133 | | |
134 | | |
135 | | PaddingMatcher::PaddingMatcher(const UnicodeString& padString) |
136 | 0 | : SymbolMatcher(padString, unisets::EMPTY) {} |
137 | | |
138 | 0 | bool PaddingMatcher::isFlexible() const { |
139 | 0 | return true; |
140 | 0 | } |
141 | | |
142 | 0 | bool PaddingMatcher::isDisabled(const ParsedNumber&) const { |
143 | 0 | return false; |
144 | 0 | } |
145 | | |
146 | 0 | void PaddingMatcher::accept(StringSegment&, ParsedNumber&) const { |
147 | 0 | // No-op |
148 | 0 | } |
149 | | |
150 | | |
151 | | PercentMatcher::PercentMatcher(const DecimalFormatSymbols& dfs) |
152 | 0 | : SymbolMatcher(dfs.getConstSymbol(DecimalFormatSymbols::kPercentSymbol), unisets::PERCENT_SIGN) { |
153 | 0 | } |
154 | | |
155 | 0 | bool PercentMatcher::isDisabled(const ParsedNumber& result) const { |
156 | 0 | return 0 != (result.flags & FLAG_PERCENT); |
157 | 0 | } |
158 | | |
159 | 0 | void PercentMatcher::accept(StringSegment& segment, ParsedNumber& result) const { |
160 | 0 | result.flags |= FLAG_PERCENT; |
161 | 0 | result.setCharsConsumed(segment); |
162 | 0 | } |
163 | | |
164 | | |
165 | | PermilleMatcher::PermilleMatcher(const DecimalFormatSymbols& dfs) |
166 | 0 | : SymbolMatcher(dfs.getConstSymbol(DecimalFormatSymbols::kPerMillSymbol), unisets::PERMILLE_SIGN) { |
167 | 0 | } |
168 | | |
169 | 0 | bool PermilleMatcher::isDisabled(const ParsedNumber& result) const { |
170 | 0 | return 0 != (result.flags & FLAG_PERMILLE); |
171 | 0 | } |
172 | | |
173 | 0 | void PermilleMatcher::accept(StringSegment& segment, ParsedNumber& result) const { |
174 | 0 | result.flags |= FLAG_PERMILLE; |
175 | 0 | result.setCharsConsumed(segment); |
176 | 0 | } |
177 | | |
178 | | |
179 | | PlusSignMatcher::PlusSignMatcher(const DecimalFormatSymbols& dfs, bool allowTrailing) |
180 | | : SymbolMatcher(dfs.getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol), unisets::PLUS_SIGN), |
181 | 0 | fAllowTrailing(allowTrailing) { |
182 | 0 | } |
183 | | |
184 | 0 | bool PlusSignMatcher::isDisabled(const ParsedNumber& result) const { |
185 | 0 | return !fAllowTrailing && result.seenNumber(); |
186 | 0 | } |
187 | | |
188 | 0 | void PlusSignMatcher::accept(StringSegment& segment, ParsedNumber& result) const { |
189 | 0 | result.setCharsConsumed(segment); |
190 | 0 | } |
191 | | |
192 | | |
193 | | #endif /* #if !UCONFIG_NO_FORMATTING */ |