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 afterPrefixInsert(dfs
.getPatternForCurrencySpacing(UNUM_CURRENCY_INSERT
, false, status
)),
29 beforeSuffixInsert(dfs
.getPatternForCurrencySpacing(UNUM_CURRENCY_INSERT
, true, status
)),
30 fLocaleName(dfs
.getLocale().getName(), -1, status
) {
31 utils::copyCurrencyCode(fCurrencyCode
, currencySymbols
.getIsoCode());
33 // Pre-load the long names for the current locale and currency
34 // if we are parsing without the full currency data.
35 if (!fUseFullCurrencyData
) {
36 for (int32_t i
=0; i
<StandardPlural::COUNT
; i
++) {
37 auto plural
= static_cast<StandardPlural::Form
>(i
);
38 fLocalLongNames
[i
] = currencySymbols
.getPluralName(plural
, status
);
42 // TODO: Figure out how to make this faster and re-enable.
43 // Computing the "lead code points" set for fastpathing is too slow to use in production.
44 // See http://bugs.icu-project.org/trac/ticket/13584
45 // // Compute the full set of characters that could be the first in a currency to allow for
46 // // efficient smoke test.
47 // fLeadCodePoints.add(fCurrency1.char32At(0));
48 // fLeadCodePoints.add(fCurrency2.char32At(0));
49 // fLeadCodePoints.add(beforeSuffixInsert.char32At(0));
50 // uprv_currencyLeads(fLocaleName.data(), fLeadCodePoints, status);
51 // // Always apply case mapping closure for currencies
52 // fLeadCodePoints.closeOver(USET_ADD_CASE_MAPPINGS);
53 // fLeadCodePoints.freeze();
57 CombinedCurrencyMatcher::match(StringSegment
& segment
, ParsedNumber
& result
, UErrorCode
& status
) const {
58 if (result
.currencyCode
[0] != 0) {
62 // Try to match a currency spacing separator.
63 int32_t initialOffset
= segment
.getOffset();
64 bool maybeMore
= false;
65 if (result
.seenNumber() && !beforeSuffixInsert
.isEmpty()) {
66 int32_t overlap
= segment
.getCommonPrefixLength(beforeSuffixInsert
);
67 if (overlap
== beforeSuffixInsert
.length()) {
68 segment
.adjustOffset(overlap
);
69 // Note: let currency spacing be a weak match. Don't update chars consumed.
71 maybeMore
= maybeMore
|| overlap
== segment
.length();
74 // Match the currency string, and reset if we didn't find one.
75 maybeMore
= maybeMore
|| matchCurrency(segment
, result
, status
);
76 if (result
.currencyCode
[0] == 0) {
77 segment
.setOffset(initialOffset
);
81 // Try to match a currency spacing separator.
82 if (!result
.seenNumber() && !afterPrefixInsert
.isEmpty()) {
83 int32_t overlap
= segment
.getCommonPrefixLength(afterPrefixInsert
);
84 if (overlap
== afterPrefixInsert
.length()) {
85 segment
.adjustOffset(overlap
);
86 // Note: let currency spacing be a weak match. Don't update chars consumed.
88 maybeMore
= maybeMore
|| overlap
== segment
.length();
94 bool CombinedCurrencyMatcher::matchCurrency(StringSegment
& segment
, ParsedNumber
& result
,
95 UErrorCode
& status
) const {
96 bool maybeMore
= false;
99 if (!fCurrency1
.isEmpty()) {
100 overlap1
= segment
.getCaseSensitivePrefixLength(fCurrency1
);
104 maybeMore
= maybeMore
|| overlap1
== segment
.length();
105 if (overlap1
== fCurrency1
.length()) {
106 utils::copyCurrencyCode(result
.currencyCode
, fCurrencyCode
);
107 segment
.adjustOffset(overlap1
);
108 result
.setCharsConsumed(segment
);
113 if (!fCurrency2
.isEmpty()) {
114 overlap2
= segment
.getCaseSensitivePrefixLength(fCurrency2
);
118 maybeMore
= maybeMore
|| overlap2
== segment
.length();
119 if (overlap2
== fCurrency2
.length()) {
120 utils::copyCurrencyCode(result
.currencyCode
, fCurrencyCode
);
121 segment
.adjustOffset(overlap2
);
122 result
.setCharsConsumed(segment
);
126 if (fUseFullCurrencyData
) {
127 // Use the full currency data.
128 // NOTE: This call site should be improved with #13584.
129 const UnicodeString segmentString
= segment
.toTempUnicodeString();
131 // Try to parse the currency
132 ParsePosition
ppos(0);
133 int32_t partialMatchLen
= 0;
138 UCURR_SYMBOL_NAME
, // checks for both UCURR_SYMBOL_NAME and UCURR_LONG_NAME
142 maybeMore
= maybeMore
|| partialMatchLen
== segment
.length();
144 if (U_SUCCESS(status
) && ppos
.getIndex() != 0) {
146 // NOTE: The currency code should already be saved in the ParsedNumber.
147 segment
.adjustOffset(ppos
.getIndex());
148 result
.setCharsConsumed(segment
);
153 // Use the locale long names.
154 int32_t longestFullMatch
= 0;
155 for (int32_t i
=0; i
<StandardPlural::COUNT
; i
++) {
156 const UnicodeString
& name
= fLocalLongNames
[i
];
157 int32_t overlap
= segment
.getCommonPrefixLength(name
);
158 if (overlap
== name
.length() && name
.length() > longestFullMatch
) {
159 longestFullMatch
= name
.length();
161 maybeMore
= maybeMore
|| overlap
> 0;
163 if (longestFullMatch
> 0) {
164 utils::copyCurrencyCode(result
.currencyCode
, fCurrencyCode
);
165 segment
.adjustOffset(longestFullMatch
);
166 result
.setCharsConsumed(segment
);
175 bool CombinedCurrencyMatcher::smokeTest(const StringSegment
&) const {
176 // TODO: See constructor
178 //return segment.startsWith(fLeadCodePoints);
181 UnicodeString
CombinedCurrencyMatcher::toString() const {
182 return u
"<CombinedCurrencyMatcher>";
186 #endif /* #if !UCONFIG_NO_FORMATTING */