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