• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "static_unicode_sets.h"
13 #include "umutex.h"
14 #include "ucln_cmn.h"
15 #include "unicode/uniset.h"
16 #include "uresimp.h"
17 #include "cstring.h"
18 #include "uassert.h"
19 
20 using namespace icu;
21 using namespace icu::unisets;
22 
23 
24 namespace {
25 
26 UnicodeSet* gUnicodeSets[COUNT] = {};
27 
28 // Save the empty instance in static memory to have well-defined behavior if a
29 // regular UnicodeSet cannot be allocated.
30 alignas(UnicodeSet)
31 char gEmptyUnicodeSet[sizeof(UnicodeSet)];
32 
33 // Whether the gEmptyUnicodeSet is initialized and ready to use.
34 UBool gEmptyUnicodeSetInitialized = FALSE;
35 
getImpl(Key key)36 inline UnicodeSet* getImpl(Key key) {
37     UnicodeSet* candidate = gUnicodeSets[key];
38     if (candidate == nullptr) {
39         return reinterpret_cast<UnicodeSet*>(gEmptyUnicodeSet);
40     }
41     return candidate;
42 }
43 
computeUnion(Key k1,Key k2)44 UnicodeSet* computeUnion(Key k1, Key k2) {
45     UnicodeSet* result = new UnicodeSet();
46     if (result == nullptr) {
47         return nullptr;
48     }
49     result->addAll(*getImpl(k1));
50     result->addAll(*getImpl(k2));
51     result->freeze();
52     return result;
53 }
54 
computeUnion(Key k1,Key k2,Key k3)55 UnicodeSet* computeUnion(Key k1, Key k2, Key k3) {
56     UnicodeSet* result = new UnicodeSet();
57     if (result == nullptr) {
58         return nullptr;
59     }
60     result->addAll(*getImpl(k1));
61     result->addAll(*getImpl(k2));
62     result->addAll(*getImpl(k3));
63     result->freeze();
64     return result;
65 }
66 
67 
saveSet(Key key,const UnicodeString & unicodeSetPattern,UErrorCode & status)68 void saveSet(Key key, const UnicodeString& unicodeSetPattern, UErrorCode& status) {
69     // assert unicodeSets.get(key) == null;
70     gUnicodeSets[key] = new UnicodeSet(unicodeSetPattern, status);
71 }
72 
73 class ParseDataSink : public ResourceSink {
74   public:
put(const char * key,ResourceValue & value,UBool,UErrorCode & status)75     void put(const char* key, ResourceValue& value, UBool /*noFallback*/, UErrorCode& status) U_OVERRIDE {
76         ResourceTable contextsTable = value.getTable(status);
77         if (U_FAILURE(status)) { return; }
78         for (int i = 0; contextsTable.getKeyAndValue(i, key, value); i++) {
79             if (uprv_strcmp(key, "date") == 0) {
80                 // ignore
81             } else {
82                 ResourceTable strictnessTable = value.getTable(status);
83                 if (U_FAILURE(status)) { return; }
84                 for (int j = 0; strictnessTable.getKeyAndValue(j, key, value); j++) {
85                     bool isLenient = (uprv_strcmp(key, "lenient") == 0);
86                     ResourceArray array = value.getArray(status);
87                     if (U_FAILURE(status)) { return; }
88                     for (int k = 0; k < array.getSize(); k++) {
89                         array.getValue(k, value);
90                         UnicodeString str = value.getUnicodeString(status);
91                         if (U_FAILURE(status)) { return; }
92                         // There is both lenient and strict data for comma/period,
93                         // but not for any of the other symbols.
94                         if (str.indexOf(u'.') != -1) {
95                             saveSet(isLenient ? PERIOD : STRICT_PERIOD, str, status);
96                         } else if (str.indexOf(u',') != -1) {
97                             saveSet(isLenient ? COMMA : STRICT_COMMA, str, status);
98                         } else if (str.indexOf(u'+') != -1) {
99                             saveSet(PLUS_SIGN, str, status);
100                         } else if (str.indexOf(u'‒') != -1) {
101                             saveSet(MINUS_SIGN, str, status);
102                         } else if (str.indexOf(u'$') != -1) {
103                             saveSet(DOLLAR_SIGN, str, status);
104                         } else if (str.indexOf(u'£') != -1) {
105                             saveSet(POUND_SIGN, str, status);
106                         } else if (str.indexOf(u'₨') != -1) {
107                             saveSet(RUPEE_SIGN, str, status);
108                         }
109                         if (U_FAILURE(status)) { return; }
110                     }
111                 }
112             }
113         }
114     }
115 };
116 
117 
118 icu::UInitOnce gNumberParseUniSetsInitOnce = U_INITONCE_INITIALIZER;
119 
cleanupNumberParseUniSets()120 UBool U_CALLCONV cleanupNumberParseUniSets() {
121     if (gEmptyUnicodeSetInitialized) {
122         reinterpret_cast<UnicodeSet*>(gEmptyUnicodeSet)->~UnicodeSet();
123         gEmptyUnicodeSetInitialized = FALSE;
124     }
125     for (int32_t i = 0; i < COUNT; i++) {
126         delete gUnicodeSets[i];
127         gUnicodeSets[i] = nullptr;
128     }
129     gNumberParseUniSetsInitOnce.reset();
130     return TRUE;
131 }
132 
initNumberParseUniSets(UErrorCode & status)133 void U_CALLCONV initNumberParseUniSets(UErrorCode& status) {
134     ucln_common_registerCleanup(UCLN_COMMON_NUMPARSE_UNISETS, cleanupNumberParseUniSets);
135 
136     // Initialize the empty instance for well-defined fallback behavior
137     new(gEmptyUnicodeSet) UnicodeSet();
138     reinterpret_cast<UnicodeSet*>(gEmptyUnicodeSet)->freeze();
139     gEmptyUnicodeSetInitialized = TRUE;
140 
141     // These sets were decided after discussion with icu-design@. See tickets #13084 and #13309.
142     // Zs+TAB is "horizontal whitespace" according to UTS #18 (blank property).
143     gUnicodeSets[DEFAULT_IGNORABLES] = new UnicodeSet(
144             u"[[:Zs:][\\u0009][:Bidi_Control:][:Variation_Selector:]]", status);
145     gUnicodeSets[STRICT_IGNORABLES] = new UnicodeSet(u"[[:Bidi_Control:]]", status);
146 
147     LocalUResourceBundlePointer rb(ures_open(nullptr, "root", &status));
148     if (U_FAILURE(status)) { return; }
149     ParseDataSink sink;
150     ures_getAllItemsWithFallback(rb.getAlias(), "parse", sink, status);
151     if (U_FAILURE(status)) { return; }
152 
153     // NOTE: It is OK for these assertions to fail if there was a no-data build.
154     U_ASSERT(gUnicodeSets[COMMA] != nullptr);
155     U_ASSERT(gUnicodeSets[STRICT_COMMA] != nullptr);
156     U_ASSERT(gUnicodeSets[PERIOD] != nullptr);
157     U_ASSERT(gUnicodeSets[STRICT_PERIOD] != nullptr);
158 
159     gUnicodeSets[OTHER_GROUPING_SEPARATORS] = new UnicodeSet(
160             u"['٬‘’'\\u0020\\u00A0\\u2000-\\u200A\\u202F\\u205F\\u3000]", status);
161     gUnicodeSets[ALL_SEPARATORS] = computeUnion(COMMA, PERIOD, OTHER_GROUPING_SEPARATORS);
162     gUnicodeSets[STRICT_ALL_SEPARATORS] = computeUnion(
163             STRICT_COMMA, STRICT_PERIOD, OTHER_GROUPING_SEPARATORS);
164 
165     U_ASSERT(gUnicodeSets[MINUS_SIGN] != nullptr);
166     U_ASSERT(gUnicodeSets[PLUS_SIGN] != nullptr);
167 
168     gUnicodeSets[PERCENT_SIGN] = new UnicodeSet(u"[%٪]", status);
169     gUnicodeSets[PERMILLE_SIGN] = new UnicodeSet(u"[‰؉]", status);
170     gUnicodeSets[INFINITY_KEY] = new UnicodeSet(u"[∞]", status);
171 
172     U_ASSERT(gUnicodeSets[DOLLAR_SIGN] != nullptr);
173     U_ASSERT(gUnicodeSets[POUND_SIGN] != nullptr);
174     U_ASSERT(gUnicodeSets[RUPEE_SIGN] != nullptr);
175     gUnicodeSets[YEN_SIGN] = new UnicodeSet(u"[¥\\uffe5]", status);
176 
177     gUnicodeSets[DIGITS] = new UnicodeSet(u"[:digit:]", status);
178 
179     gUnicodeSets[DIGITS_OR_ALL_SEPARATORS] = computeUnion(DIGITS, ALL_SEPARATORS);
180     gUnicodeSets[DIGITS_OR_STRICT_ALL_SEPARATORS] = computeUnion(DIGITS, STRICT_ALL_SEPARATORS);
181 
182     for (auto* uniset : gUnicodeSets) {
183         if (uniset != nullptr) {
184             uniset->freeze();
185         }
186     }
187 }
188 
189 }
190 
get(Key key)191 const UnicodeSet* unisets::get(Key key) {
192     UErrorCode localStatus = U_ZERO_ERROR;
193     umtx_initOnce(gNumberParseUniSetsInitOnce, &initNumberParseUniSets, localStatus);
194     if (U_FAILURE(localStatus)) {
195         return reinterpret_cast<UnicodeSet*>(gEmptyUnicodeSet);
196     }
197     return getImpl(key);
198 }
199 
chooseFrom(UnicodeString str,Key key1)200 Key unisets::chooseFrom(UnicodeString str, Key key1) {
201     return get(key1)->contains(str) ? key1 : NONE;
202 }
203 
chooseFrom(UnicodeString str,Key key1,Key key2)204 Key unisets::chooseFrom(UnicodeString str, Key key1, Key key2) {
205     return get(key1)->contains(str) ? key1 : chooseFrom(str, key2);
206 }
207 
208 //Key unisets::chooseCurrency(UnicodeString str) {
209 //    if (get(DOLLAR_SIGN)->contains(str)) {
210 //        return DOLLAR_SIGN;
211 //    } else if (get(POUND_SIGN)->contains(str)) {
212 //        return POUND_SIGN;
213 //    } else if (get(RUPEE_SIGN)->contains(str)) {
214 //        return RUPEE_SIGN;
215 //    } else if (get(YEN_SIGN)->contains(str)) {
216 //        return YEN_SIGN;
217 //    } else {
218 //        return NONE;
219 //    }
220 //}
221 
222 
223 #endif /* #if !UCONFIG_NO_FORMATTING */
224