/src/icu/icu4c/source/common/characterproperties.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // © 2018 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | |
4 | | // characterproperties.cpp |
5 | | // created: 2018sep03 Markus W. Scherer |
6 | | |
7 | | #include "unicode/utypes.h" |
8 | | #include "unicode/localpointer.h" |
9 | | #include "unicode/uchar.h" |
10 | | #include "unicode/ucpmap.h" |
11 | | #include "unicode/ucptrie.h" |
12 | | #include "unicode/umutablecptrie.h" |
13 | | #include "unicode/uniset.h" |
14 | | #include "unicode/uscript.h" |
15 | | #include "unicode/uset.h" |
16 | | #include "cmemory.h" |
17 | | #include "emojiprops.h" |
18 | | #include "mutex.h" |
19 | | #include "normalizer2impl.h" |
20 | | #include "uassert.h" |
21 | | #include "ubidi_props.h" |
22 | | #include "ucase.h" |
23 | | #include "ucln_cmn.h" |
24 | | #include "umutex.h" |
25 | | #include "uprops.h" |
26 | | |
27 | | using icu::LocalPointer; |
28 | | #if !UCONFIG_NO_NORMALIZATION |
29 | | using icu::Normalizer2Factory; |
30 | | using icu::Normalizer2Impl; |
31 | | #endif |
32 | | using icu::UInitOnce; |
33 | | using icu::UnicodeSet; |
34 | | |
35 | | namespace { |
36 | | |
37 | | UBool U_CALLCONV characterproperties_cleanup(); |
38 | | |
39 | | constexpr int32_t NUM_INCLUSIONS = UPROPS_SRC_COUNT + (UCHAR_INT_LIMIT - UCHAR_INT_START); |
40 | | |
41 | | struct Inclusion { |
42 | | UnicodeSet *fSet = nullptr; |
43 | | UInitOnce fInitOnce {}; |
44 | | }; |
45 | | Inclusion gInclusions[NUM_INCLUSIONS]; // cached getInclusions() |
46 | | |
47 | | UnicodeSet *sets[UCHAR_BINARY_LIMIT] = {}; |
48 | | |
49 | | UCPMap *maps[UCHAR_INT_LIMIT - UCHAR_INT_START] = {}; |
50 | | |
51 | | icu::UMutex cpMutex; |
52 | | |
53 | | //---------------------------------------------------------------- |
54 | | // Inclusions list |
55 | | //---------------------------------------------------------------- |
56 | | |
57 | | // USetAdder implementation |
58 | | // Does not use uset.h to reduce code dependencies |
59 | | void U_CALLCONV |
60 | 13.1k | _set_add(USet *set, UChar32 c) { |
61 | 13.1k | reinterpret_cast<UnicodeSet*>(set)->add(c); |
62 | 13.1k | } |
63 | | |
64 | | void U_CALLCONV |
65 | 0 | _set_addRange(USet *set, UChar32 start, UChar32 end) { |
66 | 0 | reinterpret_cast<UnicodeSet*>(set)->add(start, end); |
67 | 0 | } |
68 | | |
69 | | void U_CALLCONV |
70 | 0 | _set_addString(USet *set, const char16_t *str, int32_t length) { |
71 | 0 | reinterpret_cast<UnicodeSet*>(set)->add(icu::UnicodeString(static_cast<UBool>(length < 0), str, length)); |
72 | 0 | } |
73 | | |
74 | 0 | UBool U_CALLCONV characterproperties_cleanup() { |
75 | 0 | for (Inclusion &in: gInclusions) { |
76 | 0 | delete in.fSet; |
77 | 0 | in.fSet = nullptr; |
78 | 0 | in.fInitOnce.reset(); |
79 | 0 | } |
80 | 0 | for (int32_t i = 0; i < UPRV_LENGTHOF(sets); ++i) { |
81 | 0 | delete sets[i]; |
82 | 0 | sets[i] = nullptr; |
83 | 0 | } |
84 | 0 | for (int32_t i = 0; i < UPRV_LENGTHOF(maps); ++i) { |
85 | 0 | ucptrie_close(reinterpret_cast<UCPTrie *>(maps[i])); |
86 | 0 | maps[i] = nullptr; |
87 | 0 | } |
88 | 0 | return true; |
89 | 0 | } |
90 | | |
91 | 2 | void U_CALLCONV initInclusion(UPropertySource src, UErrorCode &errorCode) { |
92 | | // This function is invoked only via umtx_initOnce(). |
93 | 2 | U_ASSERT(0 <= src && src < UPROPS_SRC_COUNT); |
94 | 2 | if (src == UPROPS_SRC_NONE) { |
95 | 0 | errorCode = U_INTERNAL_PROGRAM_ERROR; |
96 | 0 | return; |
97 | 0 | } |
98 | 2 | U_ASSERT(gInclusions[src].fSet == nullptr); |
99 | | |
100 | 2 | LocalPointer<UnicodeSet> incl(new UnicodeSet()); |
101 | 2 | if (incl.isNull()) { |
102 | 0 | errorCode = U_MEMORY_ALLOCATION_ERROR; |
103 | 0 | return; |
104 | 0 | } |
105 | 2 | USetAdder sa = { |
106 | 2 | reinterpret_cast<USet*>(incl.getAlias()), |
107 | 2 | _set_add, |
108 | 2 | _set_addRange, |
109 | 2 | _set_addString, |
110 | 2 | nullptr, // don't need remove() |
111 | 2 | nullptr // don't need removeRange() |
112 | 2 | }; |
113 | | |
114 | 2 | switch(src) { |
115 | 1 | case UPROPS_SRC_CHAR: |
116 | 1 | uchar_addPropertyStarts(&sa, &errorCode); |
117 | 1 | break; |
118 | 1 | case UPROPS_SRC_PROPSVEC: |
119 | 1 | upropsvec_addPropertyStarts(&sa, &errorCode); |
120 | 1 | break; |
121 | 0 | case UPROPS_SRC_CHAR_AND_PROPSVEC: |
122 | 0 | uchar_addPropertyStarts(&sa, &errorCode); |
123 | 0 | upropsvec_addPropertyStarts(&sa, &errorCode); |
124 | 0 | break; |
125 | 0 | #if !UCONFIG_NO_NORMALIZATION |
126 | 0 | case UPROPS_SRC_CASE_AND_NORM: { |
127 | 0 | const Normalizer2Impl *impl=Normalizer2Factory::getNFCImpl(errorCode); |
128 | 0 | if(U_SUCCESS(errorCode)) { |
129 | 0 | impl->addPropertyStarts(&sa, errorCode); |
130 | 0 | } |
131 | 0 | ucase_addPropertyStarts(&sa, &errorCode); |
132 | 0 | break; |
133 | 0 | } |
134 | 0 | case UPROPS_SRC_NFC: { |
135 | 0 | const Normalizer2Impl *impl=Normalizer2Factory::getNFCImpl(errorCode); |
136 | 0 | if(U_SUCCESS(errorCode)) { |
137 | 0 | impl->addPropertyStarts(&sa, errorCode); |
138 | 0 | } |
139 | 0 | break; |
140 | 0 | } |
141 | 0 | case UPROPS_SRC_NFKC: { |
142 | 0 | const Normalizer2Impl *impl=Normalizer2Factory::getNFKCImpl(errorCode); |
143 | 0 | if(U_SUCCESS(errorCode)) { |
144 | 0 | impl->addPropertyStarts(&sa, errorCode); |
145 | 0 | } |
146 | 0 | break; |
147 | 0 | } |
148 | 0 | case UPROPS_SRC_NFKC_CF: { |
149 | 0 | const Normalizer2Impl *impl=Normalizer2Factory::getNFKC_CFImpl(errorCode); |
150 | 0 | if(U_SUCCESS(errorCode)) { |
151 | 0 | impl->addPropertyStarts(&sa, errorCode); |
152 | 0 | } |
153 | 0 | break; |
154 | 0 | } |
155 | 0 | case UPROPS_SRC_NFC_CANON_ITER: { |
156 | 0 | const Normalizer2Impl *impl=Normalizer2Factory::getNFCImpl(errorCode); |
157 | 0 | if(U_SUCCESS(errorCode)) { |
158 | 0 | impl->addCanonIterPropertyStarts(&sa, errorCode); |
159 | 0 | } |
160 | 0 | break; |
161 | 0 | } |
162 | 0 | #endif |
163 | 0 | case UPROPS_SRC_CASE: |
164 | 0 | ucase_addPropertyStarts(&sa, &errorCode); |
165 | 0 | break; |
166 | 0 | case UPROPS_SRC_BIDI: |
167 | 0 | ubidi_addPropertyStarts(&sa, &errorCode); |
168 | 0 | break; |
169 | 0 | case UPROPS_SRC_INPC: |
170 | 0 | case UPROPS_SRC_INSC: |
171 | 0 | case UPROPS_SRC_VO: |
172 | 0 | uprops_addPropertyStarts(src, &sa, &errorCode); |
173 | 0 | break; |
174 | 0 | case UPROPS_SRC_EMOJI: { |
175 | 0 | const icu::EmojiProps *ep = icu::EmojiProps::getSingleton(errorCode); |
176 | 0 | if (U_SUCCESS(errorCode)) { |
177 | 0 | ep->addPropertyStarts(&sa, errorCode); |
178 | 0 | } |
179 | 0 | break; |
180 | 0 | } |
181 | 0 | case UPROPS_SRC_IDSU: |
182 | | // New in Unicode 15.1 for just two characters. |
183 | 0 | sa.add(sa.set, 0x2FFE); |
184 | 0 | sa.add(sa.set, 0x2FFF + 1); |
185 | 0 | break; |
186 | 0 | case UPROPS_SRC_ID_COMPAT_MATH: |
187 | 0 | case UPROPS_SRC_MCM: |
188 | 0 | uprops_addPropertyStarts(src, &sa, &errorCode); |
189 | 0 | break; |
190 | 0 | case UPROPS_SRC_BLOCK: |
191 | 0 | ublock_addPropertyStarts(&sa, errorCode); |
192 | 0 | break; |
193 | 0 | default: |
194 | 0 | errorCode = U_INTERNAL_PROGRAM_ERROR; |
195 | 0 | break; |
196 | 2 | } |
197 | | |
198 | 2 | if (U_FAILURE(errorCode)) { |
199 | 0 | return; |
200 | 0 | } |
201 | 2 | if (incl->isBogus()) { |
202 | 0 | errorCode = U_MEMORY_ALLOCATION_ERROR; |
203 | 0 | return; |
204 | 0 | } |
205 | | // Compact for caching. |
206 | 2 | incl->compact(); |
207 | 2 | gInclusions[src].fSet = incl.orphan(); |
208 | 2 | ucln_common_registerCleanup(UCLN_COMMON_CHARACTERPROPERTIES, characterproperties_cleanup); |
209 | 2 | } |
210 | | |
211 | 15 | const UnicodeSet *getInclusionsForSource(UPropertySource src, UErrorCode &errorCode) { |
212 | 15 | if (U_FAILURE(errorCode)) { return nullptr; } |
213 | 15 | if (src < 0 || UPROPS_SRC_COUNT <= src) { |
214 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
215 | 0 | return nullptr; |
216 | 0 | } |
217 | 15 | Inclusion &i = gInclusions[src]; |
218 | 15 | umtx_initOnce(i.fInitOnce, &initInclusion, src, errorCode); |
219 | 15 | return i.fSet; |
220 | 15 | } |
221 | | |
222 | 2 | void U_CALLCONV initIntPropInclusion(UProperty prop, UErrorCode &errorCode) { |
223 | | // This function is invoked only via umtx_initOnce(). |
224 | 2 | U_ASSERT(UCHAR_INT_START <= prop && prop < UCHAR_INT_LIMIT); |
225 | 2 | int32_t inclIndex = UPROPS_SRC_COUNT + (prop - UCHAR_INT_START); |
226 | 2 | U_ASSERT(gInclusions[inclIndex].fSet == nullptr); |
227 | 2 | UPropertySource src = uprops_getSource(prop); |
228 | 2 | const UnicodeSet *incl = getInclusionsForSource(src, errorCode); |
229 | 2 | if (U_FAILURE(errorCode)) { |
230 | 0 | return; |
231 | 0 | } |
232 | | |
233 | 2 | LocalPointer<UnicodeSet> intPropIncl(new UnicodeSet(0, 0)); |
234 | 2 | if (intPropIncl.isNull()) { |
235 | 0 | errorCode = U_MEMORY_ALLOCATION_ERROR; |
236 | 0 | return; |
237 | 0 | } |
238 | 2 | int32_t numRanges = incl->getRangeCount(); |
239 | 2 | int32_t prevValue = 0; |
240 | 6.11k | for (int32_t i = 0; i < numRanges; ++i) { |
241 | 6.11k | UChar32 rangeEnd = incl->getRangeEnd(i); |
242 | 20.3k | for (UChar32 c = incl->getRangeStart(i); c <= rangeEnd; ++c) { |
243 | | // TODO: Get a UCharacterProperty.IntProperty to avoid the property dispatch. |
244 | 14.2k | int32_t value = u_getIntPropertyValue(c, prop); |
245 | 14.2k | if (value != prevValue) { |
246 | 5.29k | intPropIncl->add(c); |
247 | 5.29k | prevValue = value; |
248 | 5.29k | } |
249 | 14.2k | } |
250 | 6.11k | } |
251 | | |
252 | 2 | if (intPropIncl->isBogus()) { |
253 | 0 | errorCode = U_MEMORY_ALLOCATION_ERROR; |
254 | 0 | return; |
255 | 0 | } |
256 | | // Compact for caching. |
257 | 2 | intPropIncl->compact(); |
258 | 2 | gInclusions[inclIndex].fSet = intPropIncl.orphan(); |
259 | 2 | ucln_common_registerCleanup(UCLN_COMMON_CHARACTERPROPERTIES, characterproperties_cleanup); |
260 | 2 | } |
261 | | |
262 | | } // namespace |
263 | | |
264 | | U_NAMESPACE_BEGIN |
265 | | |
266 | | const UnicodeSet *CharacterProperties::getInclusionsForProperty( |
267 | 66.0k | UProperty prop, UErrorCode &errorCode) { |
268 | 66.0k | if (U_FAILURE(errorCode)) { return nullptr; } |
269 | 66.0k | if (UCHAR_INT_START <= prop && prop < UCHAR_INT_LIMIT) { |
270 | 66.0k | int32_t inclIndex = UPROPS_SRC_COUNT + (prop - UCHAR_INT_START); |
271 | 66.0k | Inclusion &i = gInclusions[inclIndex]; |
272 | 66.0k | umtx_initOnce(i.fInitOnce, &initIntPropInclusion, prop, errorCode); |
273 | 66.0k | return i.fSet; |
274 | 66.0k | } else { |
275 | 13 | UPropertySource src = uprops_getSource(prop); |
276 | 13 | return getInclusionsForSource(src, errorCode); |
277 | 13 | } |
278 | 66.0k | } |
279 | | |
280 | | U_NAMESPACE_END |
281 | | |
282 | | namespace { |
283 | | |
284 | 1 | UnicodeSet *makeSet(UProperty property, UErrorCode &errorCode) { |
285 | 1 | if (U_FAILURE(errorCode)) { return nullptr; } |
286 | 1 | LocalPointer<UnicodeSet> set(new UnicodeSet()); |
287 | 1 | if (set.isNull()) { |
288 | 0 | errorCode = U_MEMORY_ALLOCATION_ERROR; |
289 | 0 | return nullptr; |
290 | 0 | } |
291 | 1 | if (UCHAR_BASIC_EMOJI <= property && property <= UCHAR_RGI_EMOJI) { |
292 | | // property of strings |
293 | 0 | const icu::EmojiProps *ep = icu::EmojiProps::getSingleton(errorCode); |
294 | 0 | if (U_FAILURE(errorCode)) { return nullptr; } |
295 | 0 | USetAdder sa = { |
296 | 0 | reinterpret_cast<USet*>(set.getAlias()), |
297 | 0 | _set_add, |
298 | 0 | _set_addRange, |
299 | 0 | _set_addString, |
300 | 0 | nullptr, // don't need remove() |
301 | 0 | nullptr // don't need removeRange() |
302 | 0 | }; |
303 | 0 | ep->addStrings(&sa, property, errorCode); |
304 | 0 | if (property != UCHAR_BASIC_EMOJI && property != UCHAR_RGI_EMOJI) { |
305 | | // property of _only_ strings |
306 | 0 | set->freeze(); |
307 | 0 | return set.orphan(); |
308 | 0 | } |
309 | 0 | } |
310 | | |
311 | 1 | const UnicodeSet *inclusions = |
312 | 1 | icu::CharacterProperties::getInclusionsForProperty(property, errorCode); |
313 | 1 | if (U_FAILURE(errorCode)) { return nullptr; } |
314 | 1 | int32_t numRanges = inclusions->getRangeCount(); |
315 | 1 | UChar32 startHasProperty = -1; |
316 | | |
317 | 3.05k | for (int32_t i = 0; i < numRanges; ++i) { |
318 | 3.05k | UChar32 rangeEnd = inclusions->getRangeEnd(i); |
319 | 10.1k | for (UChar32 c = inclusions->getRangeStart(i); c <= rangeEnd; ++c) { |
320 | | // TODO: Get a UCharacterProperty.BinaryProperty to avoid the property dispatch. |
321 | 7.12k | if (u_hasBinaryProperty(c, property)) { |
322 | 4.24k | if (startHasProperty < 0) { |
323 | | // Transition from false to true. |
324 | 757 | startHasProperty = c; |
325 | 757 | } |
326 | 4.24k | } else if (startHasProperty >= 0) { |
327 | | // Transition from true to false. |
328 | 757 | set->add(startHasProperty, c - 1); |
329 | 757 | startHasProperty = -1; |
330 | 757 | } |
331 | 7.12k | } |
332 | 3.05k | } |
333 | 1 | if (startHasProperty >= 0) { |
334 | 0 | set->add(startHasProperty, 0x10FFFF); |
335 | 0 | } |
336 | 1 | set->freeze(); |
337 | 1 | return set.orphan(); |
338 | 1 | } |
339 | | |
340 | 0 | UCPMap *makeMap(UProperty property, UErrorCode &errorCode) { |
341 | 0 | if (U_FAILURE(errorCode)) { return nullptr; } |
342 | 0 | uint32_t nullValue = property == UCHAR_SCRIPT ? USCRIPT_UNKNOWN : 0; |
343 | 0 | icu::LocalUMutableCPTriePointer mutableTrie( |
344 | 0 | umutablecptrie_open(nullValue, nullValue, &errorCode)); |
345 | 0 | const UnicodeSet *inclusions = |
346 | 0 | icu::CharacterProperties::getInclusionsForProperty(property, errorCode); |
347 | 0 | if (U_FAILURE(errorCode)) { return nullptr; } |
348 | 0 | int32_t numRanges = inclusions->getRangeCount(); |
349 | 0 | UChar32 start = 0; |
350 | 0 | uint32_t value = nullValue; |
351 | |
|
352 | 0 | for (int32_t i = 0; i < numRanges; ++i) { |
353 | 0 | UChar32 rangeEnd = inclusions->getRangeEnd(i); |
354 | 0 | for (UChar32 c = inclusions->getRangeStart(i); c <= rangeEnd; ++c) { |
355 | | // TODO: Get a UCharacterProperty.IntProperty to avoid the property dispatch. |
356 | 0 | uint32_t nextValue = u_getIntPropertyValue(c, property); |
357 | 0 | if (value != nextValue) { |
358 | 0 | if (value != nullValue) { |
359 | 0 | umutablecptrie_setRange(mutableTrie.getAlias(), start, c - 1, value, &errorCode); |
360 | 0 | } |
361 | 0 | start = c; |
362 | 0 | value = nextValue; |
363 | 0 | } |
364 | 0 | } |
365 | 0 | } |
366 | 0 | if (value != 0) { |
367 | 0 | umutablecptrie_setRange(mutableTrie.getAlias(), start, 0x10FFFF, value, &errorCode); |
368 | 0 | } |
369 | |
|
370 | 0 | UCPTrieType type; |
371 | 0 | if (property == UCHAR_BIDI_CLASS || property == UCHAR_GENERAL_CATEGORY) { |
372 | 0 | type = UCPTRIE_TYPE_FAST; |
373 | 0 | } else { |
374 | 0 | type = UCPTRIE_TYPE_SMALL; |
375 | 0 | } |
376 | 0 | UCPTrieValueWidth valueWidth; |
377 | | // TODO: UCharacterProperty.IntProperty |
378 | 0 | int32_t max = u_getIntPropertyMaxValue(property); |
379 | 0 | if (max <= 0xff) { |
380 | 0 | valueWidth = UCPTRIE_VALUE_BITS_8; |
381 | 0 | } else if (max <= 0xffff) { |
382 | 0 | valueWidth = UCPTRIE_VALUE_BITS_16; |
383 | 0 | } else { |
384 | 0 | valueWidth = UCPTRIE_VALUE_BITS_32; |
385 | 0 | } |
386 | 0 | return reinterpret_cast<UCPMap *>( |
387 | 0 | umutablecptrie_buildImmutable(mutableTrie.getAlias(), type, valueWidth, &errorCode)); |
388 | 0 | } |
389 | | |
390 | | } // namespace |
391 | | |
392 | | U_NAMESPACE_BEGIN |
393 | | |
394 | 1 | const UnicodeSet *CharacterProperties::getBinaryPropertySet(UProperty property, UErrorCode &errorCode) { |
395 | 1 | if (U_FAILURE(errorCode)) { return nullptr; } |
396 | 1 | if (property < 0 || UCHAR_BINARY_LIMIT <= property) { |
397 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
398 | 0 | return nullptr; |
399 | 0 | } |
400 | 1 | Mutex m(&cpMutex); |
401 | 1 | UnicodeSet *set = sets[property]; |
402 | 1 | if (set == nullptr) { |
403 | 1 | sets[property] = set = makeSet(property, errorCode); |
404 | 1 | } |
405 | 1 | return set; |
406 | 1 | } |
407 | | |
408 | | U_NAMESPACE_END |
409 | | |
410 | | U_NAMESPACE_USE |
411 | | |
412 | | U_CAPI const USet * U_EXPORT2 |
413 | 1 | u_getBinaryPropertySet(UProperty property, UErrorCode *pErrorCode) { |
414 | 1 | const UnicodeSet *set = CharacterProperties::getBinaryPropertySet(property, *pErrorCode); |
415 | 1 | return U_SUCCESS(*pErrorCode) ? set->toUSet() : nullptr; |
416 | 1 | } |
417 | | |
418 | | U_CAPI const UCPMap * U_EXPORT2 |
419 | 0 | u_getIntPropertyMap(UProperty property, UErrorCode *pErrorCode) { |
420 | 0 | if (U_FAILURE(*pErrorCode)) { return nullptr; } |
421 | 0 | if (property < UCHAR_INT_START || UCHAR_INT_LIMIT <= property) { |
422 | 0 | *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; |
423 | 0 | return nullptr; |
424 | 0 | } |
425 | 0 | Mutex m(&cpMutex); |
426 | 0 | UCPMap *map = maps[property - UCHAR_INT_START]; |
427 | 0 | if (map == nullptr) { |
428 | 0 | maps[property - UCHAR_INT_START] = map = makeMap(property, *pErrorCode); |
429 | 0 | } |
430 | 0 | return map; |
431 | 0 | } |