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