]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/numparse_currency.cpp
ICU-64243.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / numparse_currency.cpp
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_currency.h"
14 #include "ucurrimp.h"
15 #include "unicode/errorcode.h"
16 #include "numparse_utils.h"
17
18 using namespace icu;
19 using namespace icu::numparse;
20 using namespace icu::numparse::impl;
21
22
23 CombinedCurrencyMatcher::CombinedCurrencyMatcher(const CurrencySymbols& currencySymbols, const DecimalFormatSymbols& dfs,
24 parse_flags_t parseFlags, UErrorCode& status)
25 : fCurrency1(currencySymbols.getCurrencySymbol(status)),
26 fCurrency2(currencySymbols.getIntlCurrencySymbol(status)),
27 fUseFullCurrencyData(0 == (parseFlags & PARSE_FLAG_NO_FOREIGN_CURRENCY)),
28 fCurrencyTrails(0 != (parseFlags & PARSE_FLAG_HAS_TRAIL_CURRENCY)), // Apple <rdar://problem/51938595>
29 afterPrefixInsert(dfs.getPatternForCurrencySpacing(UNUM_CURRENCY_INSERT, false, status)),
30 beforeSuffixInsert(dfs.getPatternForCurrencySpacing(UNUM_CURRENCY_INSERT, true, status)),
31 fLocaleName(dfs.getLocale().getName(), -1, status) {
32 utils::copyCurrencyCode(fCurrencyCode, currencySymbols.getIsoCode());
33
34 // Pre-load the long names for the current locale and currency
35 // if we are parsing without the full currency data.
36 if (!fUseFullCurrencyData) {
37 for (int32_t i=0; i<StandardPlural::COUNT; i++) {
38 auto plural = static_cast<StandardPlural::Form>(i);
39 fLocalLongNames[i] = currencySymbols.getPluralName(plural, status);
40 }
41 }
42
43 // TODO: Figure out how to make this faster and re-enable.
44 // Computing the "lead code points" set for fastpathing is too slow to use in production.
45 // See http://bugs.icu-project.org/trac/ticket/13584
46 // // Compute the full set of characters that could be the first in a currency to allow for
47 // // efficient smoke test.
48 // fLeadCodePoints.add(fCurrency1.char32At(0));
49 // fLeadCodePoints.add(fCurrency2.char32At(0));
50 // fLeadCodePoints.add(beforeSuffixInsert.char32At(0));
51 // uprv_currencyLeads(fLocaleName.data(), fLeadCodePoints, status);
52 // // Always apply case mapping closure for currencies
53 // fLeadCodePoints.closeOver(USET_ADD_CASE_MAPPINGS);
54 // fLeadCodePoints.freeze();
55 }
56
57 bool
58 CombinedCurrencyMatcher::match(StringSegment& segment, ParsedNumber& result, UErrorCode& status) const {
59 if (result.currencyCode[0] != 0) {
60 return false;
61 }
62
63 // Try to match a currency spacing separator.
64 int32_t initialOffset = segment.getOffset();
65 bool maybeMore = false;
66 if (result.seenNumber() && !beforeSuffixInsert.isEmpty()
67 && segment.length() != 0) { // Apple <rdar://problem/51938595>
68 int32_t overlap = segment.getCommonPrefixLength(beforeSuffixInsert);
69 if (overlap == beforeSuffixInsert.length()) {
70 segment.adjustOffset(overlap);
71 // Note: let currency spacing be a weak match. Don't update chars consumed.
72 }
73 maybeMore = maybeMore || overlap == segment.length();
74 }
75
76 // Match the currency string, and reset if we didn't find one.
77 maybeMore = maybeMore || matchCurrency(segment, result, status);
78 if (result.currencyCode[0] == 0) {
79 segment.setOffset(initialOffset);
80 return maybeMore;
81 }
82
83 // Try to match a currency spacing separator.
84 if (!result.seenNumber() && !afterPrefixInsert.isEmpty()
85 && segment.length() != 0) { // Apple <rdar://problem/51938595>
86 int32_t overlap = segment.getCommonPrefixLength(afterPrefixInsert);
87 if (overlap == afterPrefixInsert.length()) {
88 segment.adjustOffset(overlap);
89 // Note: let currency spacing be a weak match. Don't update chars consumed.
90 }
91 maybeMore = maybeMore || overlap == segment.length();
92 }
93
94 return maybeMore;
95 }
96
97 bool CombinedCurrencyMatcher::matchCurrency(StringSegment& segment, ParsedNumber& result,
98 UErrorCode& status) const {
99 bool maybeMore = false;
100
101 int32_t overlap1;
102 if (!fCurrency1.isEmpty()) {
103 overlap1 = segment.getCaseSensitivePrefixLength(fCurrency1);
104 } else if (!fUseFullCurrencyData && (!fCurrencyTrails || result.seenNumber())) { // Apple <rdar://problem/46915356><rdar://problem/51938595>
105 overlap1 = 0;
106 } else {
107 overlap1 = -1;
108 }
109 maybeMore = maybeMore || overlap1 == segment.length();
110 if (overlap1 == fCurrency1.length()) {
111 utils::copyCurrencyCode(result.currencyCode, fCurrencyCode);
112 segment.adjustOffset(overlap1);
113 result.setCharsConsumed(segment);
114 return maybeMore;
115 }
116
117 int32_t overlap2;
118 if (!fCurrency2.isEmpty()) {
119 // ISO codes should be accepted case-insensitive.
120 // https://unicode-org.atlassian.net/browse/ICU-13696
121 overlap2 = segment.getCommonPrefixLength(fCurrency2);
122 } else {
123 overlap2 = -1;
124 }
125 maybeMore = maybeMore || overlap2 == segment.length();
126 if (overlap2 == fCurrency2.length()) {
127 utils::copyCurrencyCode(result.currencyCode, fCurrencyCode);
128 segment.adjustOffset(overlap2);
129 result.setCharsConsumed(segment);
130 return maybeMore;
131 }
132
133 if (fUseFullCurrencyData) {
134 // Use the full currency data.
135 // NOTE: This call site should be improved with #13584.
136 const UnicodeString segmentString = segment.toTempUnicodeString();
137
138 // Try to parse the currency
139 ParsePosition ppos(0);
140 int32_t partialMatchLen = 0;
141 uprv_parseCurrency(
142 fLocaleName.data(),
143 segmentString,
144 ppos,
145 UCURR_SYMBOL_NAME, // checks for both UCURR_SYMBOL_NAME and UCURR_LONG_NAME
146 &partialMatchLen,
147 result.currencyCode,
148 status);
149 maybeMore = maybeMore || partialMatchLen == segment.length();
150
151 if (U_SUCCESS(status) && ppos.getIndex() != 0) {
152 // Complete match.
153 // NOTE: The currency code should already be saved in the ParsedNumber.
154 segment.adjustOffset(ppos.getIndex());
155 result.setCharsConsumed(segment);
156 return maybeMore;
157 }
158
159 } else {
160 // Use the locale long names.
161 int32_t longestFullMatch = 0;
162 for (int32_t i=0; i<StandardPlural::COUNT; i++) {
163 const UnicodeString& name = fLocalLongNames[i];
164 int32_t overlap = segment.getCommonPrefixLength(name);
165 if (overlap == name.length() && name.length() > longestFullMatch) {
166 longestFullMatch = name.length();
167 }
168 maybeMore = maybeMore || overlap > 0;
169 }
170 if (longestFullMatch > 0) {
171 utils::copyCurrencyCode(result.currencyCode, fCurrencyCode);
172 segment.adjustOffset(longestFullMatch);
173 result.setCharsConsumed(segment);
174 return maybeMore;
175 }
176 }
177
178 // No match found.
179 return maybeMore;
180 }
181
182 bool CombinedCurrencyMatcher::smokeTest(const StringSegment&) const {
183 // TODO: See constructor
184 return true;
185 //return segment.startsWith(fLeadCodePoints);
186 }
187
188 UnicodeString CombinedCurrencyMatcher::toString() const {
189 return u"<CombinedCurrencyMatcher>";
190 }
191
192
193 #endif /* #if !UCONFIG_NO_FORMATTING */