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_currency.h"
15 #include "unicode/errorcode.h"
16 #include "numparse_utils.h"
19 using namespace icu::numparse
;
20 using namespace icu::numparse::impl
;
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());
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
);
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();
58 CombinedCurrencyMatcher::match(StringSegment
& segment
, ParsedNumber
& result
, UErrorCode
& status
) const {
59 if (result
.currencyCode
[0] != 0) {
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.
73 maybeMore
= maybeMore
|| overlap
== segment
.length();
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
);
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.
91 maybeMore
= maybeMore
|| overlap
== segment
.length();
97 bool CombinedCurrencyMatcher::matchCurrency(StringSegment
& segment
, ParsedNumber
& result
,
98 UErrorCode
& status
) const {
99 bool maybeMore
= false;
102 if (!fCurrency1
.isEmpty()) {
103 overlap1
= segment
.getCaseSensitivePrefixLength(fCurrency1
);
104 } else if (!fUseFullCurrencyData
&& (!fCurrencyTrails
|| result
.seenNumber())) { // Apple <rdar://problem/46915356><rdar://problem/51938595>
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
);
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
);
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
);
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();
138 // Try to parse the currency
139 ParsePosition
ppos(0);
140 int32_t partialMatchLen
= 0;
145 UCURR_SYMBOL_NAME
, // checks for both UCURR_SYMBOL_NAME and UCURR_LONG_NAME
149 maybeMore
= maybeMore
|| partialMatchLen
== segment
.length();
151 if (U_SUCCESS(status
) && ppos
.getIndex() != 0) {
153 // NOTE: The currency code should already be saved in the ParsedNumber.
154 segment
.adjustOffset(ppos
.getIndex());
155 result
.setCharsConsumed(segment
);
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();
168 maybeMore
= maybeMore
|| overlap
> 0;
170 if (longestFullMatch
> 0) {
171 utils::copyCurrencyCode(result
.currencyCode
, fCurrencyCode
);
172 segment
.adjustOffset(longestFullMatch
);
173 result
.setCharsConsumed(segment
);
182 bool CombinedCurrencyMatcher::smokeTest(const StringSegment
&) const {
183 // TODO: See constructor
185 //return segment.startsWith(fLeadCodePoints);
188 UnicodeString
CombinedCurrencyMatcher::toString() const {
189 return u
"<CombinedCurrencyMatcher>";
193 #endif /* #if !UCONFIG_NO_FORMATTING */