Coverage Report

Created: 2025-06-13 06:34

/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
0
_set_add(USet *set, UChar32 c) {
61
0
    reinterpret_cast<UnicodeSet*>(set)->add(c);
62
0
}
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
0
void U_CALLCONV initInclusion(UPropertySource src, UErrorCode &errorCode) {
92
    // This function is invoked only via umtx_initOnce().
93
0
    U_ASSERT(0 <= src && src < UPROPS_SRC_COUNT);
94
0
    if (src == UPROPS_SRC_NONE) {
95
0
        errorCode = U_INTERNAL_PROGRAM_ERROR;
96
0
        return;
97
0
    }
98
0
    U_ASSERT(gInclusions[src].fSet == nullptr);
99
100
0
    LocalPointer<UnicodeSet> incl(new UnicodeSet());
101
0
    if (incl.isNull()) {
102
0
        errorCode = U_MEMORY_ALLOCATION_ERROR;
103
0
        return;
104
0
    }
105
0
    USetAdder sa = {
106
0
        reinterpret_cast<USet*>(incl.getAlias()),
107
0
        _set_add,
108
0
        _set_addRange,
109
0
        _set_addString,
110
0
        nullptr, // don't need remove()
111
0
        nullptr // don't need removeRange()
112
0
    };
113
114
0
    switch(src) {
115
0
    case UPROPS_SRC_CHAR:
116
0
        uchar_addPropertyStarts(&sa, &errorCode);
117
0
        break;
118
0
    case UPROPS_SRC_PROPSVEC:
119
0
        upropsvec_addPropertyStarts(&sa, &errorCode);
120
0
        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
0
    }
197
198
0
    if (U_FAILURE(errorCode)) {
199
0
        return;
200
0
    }
201
0
    if (incl->isBogus()) {
202
0
        errorCode = U_MEMORY_ALLOCATION_ERROR;
203
0
        return;
204
0
    }
205
    // Compact for caching.
206
0
    incl->compact();
207
0
    gInclusions[src].fSet = incl.orphan();
208
0
    ucln_common_registerCleanup(UCLN_COMMON_CHARACTERPROPERTIES, characterproperties_cleanup);
209
0
}
210
211
0
const UnicodeSet *getInclusionsForSource(UPropertySource src, UErrorCode &errorCode) {
212
0
    if (U_FAILURE(errorCode)) { return nullptr; }
213
0
    if (src < 0 || UPROPS_SRC_COUNT <= src) {
214
0
        errorCode = U_ILLEGAL_ARGUMENT_ERROR;
215
0
        return nullptr;
216
0
    }
217
0
    Inclusion &i = gInclusions[src];
218
0
    umtx_initOnce(i.fInitOnce, &initInclusion, src, errorCode);
219
0
    return i.fSet;
220
0
}
221
222
0
void U_CALLCONV initIntPropInclusion(UProperty prop, UErrorCode &errorCode) {
223
    // This function is invoked only via umtx_initOnce().
224
0
    U_ASSERT(UCHAR_INT_START <= prop && prop < UCHAR_INT_LIMIT);
225
0
    int32_t inclIndex = UPROPS_SRC_COUNT + (prop - UCHAR_INT_START);
226
0
    U_ASSERT(gInclusions[inclIndex].fSet == nullptr);
227
0
    UPropertySource src = uprops_getSource(prop);
228
0
    const UnicodeSet *incl = getInclusionsForSource(src, errorCode);
229
0
    if (U_FAILURE(errorCode)) {
230
0
        return;
231
0
    }
232
233
0
    LocalPointer<UnicodeSet> intPropIncl(new UnicodeSet(0, 0));
234
0
    if (intPropIncl.isNull()) {
235
0
        errorCode = U_MEMORY_ALLOCATION_ERROR;
236
0
        return;
237
0
    }
238
0
    int32_t numRanges = incl->getRangeCount();
239
0
    int32_t prevValue = 0;
240
0
    for (int32_t i = 0; i < numRanges; ++i) {
241
0
        UChar32 rangeEnd = incl->getRangeEnd(i);
242
0
        for (UChar32 c = incl->getRangeStart(i); c <= rangeEnd; ++c) {
243
            // TODO: Get a UCharacterProperty.IntProperty to avoid the property dispatch.
244
0
            int32_t value = u_getIntPropertyValue(c, prop);
245
0
            if (value != prevValue) {
246
0
                intPropIncl->add(c);
247
0
                prevValue = value;
248
0
            }
249
0
        }
250
0
    }
251
252
0
    if (intPropIncl->isBogus()) {
253
0
        errorCode = U_MEMORY_ALLOCATION_ERROR;
254
0
        return;
255
0
    }
256
    // Compact for caching.
257
0
    intPropIncl->compact();
258
0
    gInclusions[inclIndex].fSet = intPropIncl.orphan();
259
0
    ucln_common_registerCleanup(UCLN_COMMON_CHARACTERPROPERTIES, characterproperties_cleanup);
260
0
}
261
262
}  // namespace
263
264
U_NAMESPACE_BEGIN
265
266
const UnicodeSet *CharacterProperties::getInclusionsForProperty(
267
0
        UProperty prop, UErrorCode &errorCode) {
268
0
    if (U_FAILURE(errorCode)) { return nullptr; }
269
0
    if (UCHAR_INT_START <= prop && prop < UCHAR_INT_LIMIT) {
270
0
        int32_t inclIndex = UPROPS_SRC_COUNT + (prop - UCHAR_INT_START);
271
0
        Inclusion &i = gInclusions[inclIndex];
272
0
        umtx_initOnce(i.fInitOnce, &initIntPropInclusion, prop, errorCode);
273
0
        return i.fSet;
274
0
    } else {
275
0
        UPropertySource src = uprops_getSource(prop);
276
0
        return getInclusionsForSource(src, errorCode);
277
0
    }
278
0
}
279
280
U_NAMESPACE_END
281
282
namespace {
283
284
0
UnicodeSet *makeSet(UProperty property, UErrorCode &errorCode) {
285
0
    if (U_FAILURE(errorCode)) { return nullptr; }
286
0
    LocalPointer<UnicodeSet> set(new UnicodeSet());
287
0
    if (set.isNull()) {
288
0
        errorCode = U_MEMORY_ALLOCATION_ERROR;
289
0
        return nullptr;
290
0
    }
291
0
    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
0
    const UnicodeSet *inclusions =
312
0
        icu::CharacterProperties::getInclusionsForProperty(property, errorCode);
313
0
    if (U_FAILURE(errorCode)) { return nullptr; }
314
0
    int32_t numRanges = inclusions->getRangeCount();
315
0
    UChar32 startHasProperty = -1;
316
317
0
    for (int32_t i = 0; i < numRanges; ++i) {
318
0
        UChar32 rangeEnd = inclusions->getRangeEnd(i);
319
0
        for (UChar32 c = inclusions->getRangeStart(i); c <= rangeEnd; ++c) {
320
            // TODO: Get a UCharacterProperty.BinaryProperty to avoid the property dispatch.
321
0
            if (u_hasBinaryProperty(c, property)) {
322
0
                if (startHasProperty < 0) {
323
                    // Transition from false to true.
324
0
                    startHasProperty = c;
325
0
                }
326
0
            } else if (startHasProperty >= 0) {
327
                // Transition from true to false.
328
0
                set->add(startHasProperty, c - 1);
329
0
                startHasProperty = -1;
330
0
            }
331
0
        }
332
0
    }
333
0
    if (startHasProperty >= 0) {
334
0
        set->add(startHasProperty, 0x10FFFF);
335
0
    }
336
0
    set->freeze();
337
0
    return set.orphan();
338
0
}
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
0
const UnicodeSet *CharacterProperties::getBinaryPropertySet(UProperty property, UErrorCode &errorCode) {
395
0
    if (U_FAILURE(errorCode)) { return nullptr; }
396
0
    if (property < 0 || UCHAR_BINARY_LIMIT <= property) {
397
0
        errorCode = U_ILLEGAL_ARGUMENT_ERROR;
398
0
        return nullptr;
399
0
    }
400
0
    Mutex m(&cpMutex);
401
0
    UnicodeSet *set = sets[property];
402
0
    if (set == nullptr) {
403
0
        sets[property] = set = makeSet(property, errorCode);
404
0
    }
405
0
    return set;
406
0
}
407
408
U_NAMESPACE_END
409
410
U_NAMESPACE_USE
411
412
U_CAPI const USet * U_EXPORT2
413
0
u_getBinaryPropertySet(UProperty property, UErrorCode *pErrorCode) {
414
0
    const UnicodeSet *set = CharacterProperties::getBinaryPropertySet(property, *pErrorCode);
415
0
    return U_SUCCESS(*pErrorCode) ? set->toUSet() : nullptr;
416
0
}
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
}