]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/numparse_currency.cpp
ICU-62141.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / numparse_currency.cpp
CommitLineData
0f5d89e8
A
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
18using namespace icu;
19using namespace icu::numparse;
20using namespace icu::numparse::impl;
21
22
23CombinedCurrencyMatcher::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());
32
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);
39 }
40 }
41
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();
54}
55
56bool
57CombinedCurrencyMatcher::match(StringSegment& segment, ParsedNumber& result, UErrorCode& status) const {
58 if (result.currencyCode[0] != 0) {
59 return false;
60 }
61
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.
70 }
71 maybeMore = maybeMore || overlap == segment.length();
72 }
73
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);
78 return maybeMore;
79 }
80
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.
87 }
88 maybeMore = maybeMore || overlap == segment.length();
89 }
90
91 return maybeMore;
92}
93
94bool CombinedCurrencyMatcher::matchCurrency(StringSegment& segment, ParsedNumber& result,
95 UErrorCode& status) const {
96 bool maybeMore = false;
97
98 int32_t overlap1;
99 if (!fCurrency1.isEmpty()) {
100 overlap1 = segment.getCaseSensitivePrefixLength(fCurrency1);
101 } else {
102 overlap1 = -1;
103 }
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);
109 return maybeMore;
110 }
111
112 int32_t overlap2;
113 if (!fCurrency2.isEmpty()) {
114 overlap2 = segment.getCaseSensitivePrefixLength(fCurrency2);
115 } else {
116 overlap2 = -1;
117 }
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);
123 return maybeMore;
124 }
125
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();
130
131 // Try to parse the currency
132 ParsePosition ppos(0);
133 int32_t partialMatchLen = 0;
134 uprv_parseCurrency(
135 fLocaleName.data(),
136 segmentString,
137 ppos,
138 UCURR_SYMBOL_NAME, // checks for both UCURR_SYMBOL_NAME and UCURR_LONG_NAME
139 &partialMatchLen,
140 result.currencyCode,
141 status);
142 maybeMore = maybeMore || partialMatchLen == segment.length();
143
144 if (U_SUCCESS(status) && ppos.getIndex() != 0) {
145 // Complete match.
146 // NOTE: The currency code should already be saved in the ParsedNumber.
147 segment.adjustOffset(ppos.getIndex());
148 result.setCharsConsumed(segment);
149 return maybeMore;
150 }
151
152 } else {
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();
160 }
161 maybeMore = maybeMore || overlap > 0;
162 }
163 if (longestFullMatch > 0) {
164 utils::copyCurrencyCode(result.currencyCode, fCurrencyCode);
165 segment.adjustOffset(longestFullMatch);
166 result.setCharsConsumed(segment);
167 return maybeMore;
168 }
169 }
170
171 // No match found.
172 return maybeMore;
173}
174
175bool CombinedCurrencyMatcher::smokeTest(const StringSegment&) const {
176 // TODO: See constructor
177 return true;
178 //return segment.startsWith(fLeadCodePoints);
179}
180
181UnicodeString CombinedCurrencyMatcher::toString() const {
182 return u"<CombinedCurrencyMatcher>";
183}
184
185
186#endif /* #if !UCONFIG_NO_FORMATTING */