Coverage Report

Created: 2026-10-31 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/icu/icu4c/source/common/ucptrie.cpp
Line
Count
Source
1
// © 2017 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
4
// ucptrie.cpp (modified from utrie2.cpp)
5
// created: 2017dec29 Markus W. Scherer
6
7
// #define UCPTRIE_DEBUG
8
#ifdef UCPTRIE_DEBUG
9
#   include <stdio.h>
10
#endif
11
12
#include "unicode/utypes.h"
13
#include "unicode/ucptrie.h"
14
#include "unicode/utf.h"
15
#include "unicode/utf8.h"
16
#include "unicode/utf16.h"
17
#include "cmemory.h"
18
#include "uassert.h"
19
#include "ucptrie_impl.h"
20
21
U_CAPI UCPTrie* U_EXPORT2
22
ucptrie_openFromBinary(UCPTrieType type,
23
                       UCPTrieValueWidth valueWidth,
24
                       const void* data U_LIFETIME_BOUND,
25
                       int32_t length,
26
                       int32_t* pActualLength,
27
24.0k
                       UErrorCode* pErrorCode) {
28
24.0k
    if (U_FAILURE(*pErrorCode)) {
29
0
        return nullptr;
30
0
    }
31
32
24.0k
    if (length <= 0 || (U_POINTER_MASK_LSB(data, 3) != 0) ||
33
24.0k
            type < UCPTRIE_TYPE_ANY || UCPTRIE_TYPE_SMALL < type ||
34
24.0k
            valueWidth < UCPTRIE_VALUE_BITS_ANY || UCPTRIE_VALUE_BITS_8 < valueWidth) {
35
0
        *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
36
0
        return nullptr;
37
0
    }
38
39
    // Enough data for a trie header?
40
24.0k
    if (length < (int32_t)sizeof(UCPTrieHeader)) {
41
0
        *pErrorCode = U_INVALID_FORMAT_ERROR;
42
0
        return nullptr;
43
0
    }
44
45
    // Check the signature.
46
24.0k
    const UCPTrieHeader *header = (const UCPTrieHeader *)data;
47
24.0k
    if (header->signature != UCPTRIE_SIG) {
48
0
        *pErrorCode = U_INVALID_FORMAT_ERROR;
49
0
        return nullptr;
50
0
    }
51
52
24.0k
    int32_t options = header->options;
53
24.0k
    int32_t typeInt = (options >> 6) & 3;
54
24.0k
    int32_t valueWidthInt = options & UCPTRIE_OPTIONS_VALUE_BITS_MASK;
55
24.0k
    if (typeInt > UCPTRIE_TYPE_SMALL || valueWidthInt > UCPTRIE_VALUE_BITS_8 ||
56
24.0k
            (options & UCPTRIE_OPTIONS_RESERVED_MASK) != 0) {
57
0
        *pErrorCode = U_INVALID_FORMAT_ERROR;
58
0
        return nullptr;
59
0
    }
60
24.0k
    UCPTrieType actualType = (UCPTrieType)typeInt;
61
24.0k
    UCPTrieValueWidth actualValueWidth = (UCPTrieValueWidth)valueWidthInt;
62
24.0k
    if (type < 0) {
63
12
        type = actualType;
64
12
    }
65
24.0k
    if (valueWidth < 0) {
66
24.0k
        valueWidth = actualValueWidth;
67
24.0k
    }
68
24.0k
    if (type != actualType || valueWidth != actualValueWidth) {
69
0
        *pErrorCode = U_INVALID_FORMAT_ERROR;
70
0
        return nullptr;
71
0
    }
72
73
    // Get the length values and offsets.
74
24.0k
    UCPTrie tempTrie;
75
24.0k
    uprv_memset(&tempTrie, 0, sizeof(tempTrie));
76
24.0k
    tempTrie.indexLength = header->indexLength;
77
24.0k
    tempTrie.dataLength =
78
24.0k
        ((options & UCPTRIE_OPTIONS_DATA_LENGTH_MASK) << 4) | header->dataLength;
79
24.0k
    tempTrie.index3NullOffset = header->index3NullOffset;
80
24.0k
    tempTrie.dataNullOffset =
81
24.0k
        ((options & UCPTRIE_OPTIONS_DATA_NULL_OFFSET_MASK) << 8) | header->dataNullOffset;
82
83
24.0k
    tempTrie.highStart = header->shiftedHighStart << UCPTRIE_SHIFT_2;
84
24.0k
    tempTrie.shifted12HighStart = (tempTrie.highStart + 0xfff) >> 12;
85
24.0k
    tempTrie.type = type;
86
24.0k
    tempTrie.valueWidth = valueWidth;
87
88
    // Calculate the actual length.
89
24.0k
    int32_t actualLength = (int32_t)sizeof(UCPTrieHeader) + tempTrie.indexLength * 2;
90
24.0k
    if (valueWidth == UCPTRIE_VALUE_BITS_16) {
91
16
        actualLength += tempTrie.dataLength * 2;
92
24.0k
    } else if (valueWidth == UCPTRIE_VALUE_BITS_32) {
93
0
        actualLength += tempTrie.dataLength * 4;
94
24.0k
    } else {
95
24.0k
        actualLength += tempTrie.dataLength;
96
24.0k
    }
97
24.0k
    if (length < actualLength) {
98
0
        *pErrorCode = U_INVALID_FORMAT_ERROR;  // Not enough bytes.
99
0
        return nullptr;
100
0
    }
101
102
    // Allocate the trie.
103
24.0k
    UCPTrie *trie = (UCPTrie *)uprv_malloc(sizeof(UCPTrie));
104
24.0k
    if (trie == nullptr) {
105
0
        *pErrorCode = U_MEMORY_ALLOCATION_ERROR;
106
0
        return nullptr;
107
0
    }
108
24.0k
    uprv_memcpy(trie, &tempTrie, sizeof(tempTrie));
109
#ifdef UCPTRIE_DEBUG
110
    trie->name = "fromSerialized";
111
#endif
112
113
    // Set the pointers to its index and data arrays.
114
24.0k
    const uint16_t *p16 = (const uint16_t *)(header + 1);
115
24.0k
    trie->index = p16;
116
24.0k
    p16 += trie->indexLength;
117
118
    // Get the data.
119
24.0k
    int32_t nullValueOffset = trie->dataNullOffset;
120
24.0k
    if (nullValueOffset >= trie->dataLength) {
121
0
        nullValueOffset = trie->dataLength - UCPTRIE_HIGH_VALUE_NEG_DATA_OFFSET;
122
0
    }
123
24.0k
    switch (valueWidth) {
124
16
    case UCPTRIE_VALUE_BITS_16:
125
16
        trie->data.ptr16 = p16;
126
16
        trie->nullValue = trie->data.ptr16[nullValueOffset];
127
16
        break;
128
0
    case UCPTRIE_VALUE_BITS_32:
129
0
        trie->data.ptr32 = (const uint32_t *)p16;
130
0
        trie->nullValue = trie->data.ptr32[nullValueOffset];
131
0
        break;
132
24.0k
    case UCPTRIE_VALUE_BITS_8:
133
24.0k
        trie->data.ptr8 = (const uint8_t *)p16;
134
24.0k
        trie->nullValue = trie->data.ptr8[nullValueOffset];
135
24.0k
        break;
136
0
    default:
137
        // Unreachable because valueWidth was checked above.
138
0
        *pErrorCode = U_INVALID_FORMAT_ERROR;
139
0
        return nullptr;
140
24.0k
    }
141
142
24.0k
    if (pActualLength != nullptr) {
143
0
        *pActualLength = actualLength;
144
0
    }
145
24.0k
    return trie;
146
24.0k
}
147
148
U_CAPI void U_EXPORT2
149
31.4k
ucptrie_close(UCPTrie *trie) {
150
31.4k
    uprv_free(trie);
151
31.4k
}
152
153
U_CAPI UCPTrieType U_EXPORT2
154
0
ucptrie_getType(const UCPTrie *trie) {
155
0
    return (UCPTrieType)trie->type;
156
0
}
157
158
U_CAPI UCPTrieValueWidth U_EXPORT2
159
324M
ucptrie_getValueWidth(const UCPTrie *trie) {
160
324M
    return (UCPTrieValueWidth)trie->valueWidth;
161
324M
}
162
163
U_CAPI int32_t U_EXPORT2
164
61.7M
ucptrie_internalSmallIndex(const UCPTrie *trie, UChar32 c) {
165
61.7M
    int32_t i1 = c >> UCPTRIE_SHIFT_1;
166
61.7M
    if (trie->type == UCPTRIE_TYPE_FAST) {
167
50.5M
        U_ASSERT(0xffff < c && c < trie->highStart);
168
50.5M
        i1 += UCPTRIE_BMP_INDEX_LENGTH - UCPTRIE_OMITTED_BMP_INDEX_1_LENGTH;
169
50.5M
    } else {
170
11.2M
        U_ASSERT((uint32_t)c < (uint32_t)trie->highStart && trie->highStart > UCPTRIE_SMALL_LIMIT);
171
11.2M
        i1 += UCPTRIE_SMALL_INDEX_LENGTH;
172
11.2M
    }
173
61.7M
    int32_t i3Block = trie->index[
174
61.7M
        (int32_t)trie->index[i1] + ((c >> UCPTRIE_SHIFT_2) & UCPTRIE_INDEX_2_MASK)];
175
61.7M
    int32_t i3 = (c >> UCPTRIE_SHIFT_3) & UCPTRIE_INDEX_3_MASK;
176
61.7M
    int32_t dataBlock;
177
61.7M
    if ((i3Block & 0x8000) == 0) {
178
        // 16-bit indexes
179
61.7M
        dataBlock = trie->index[i3Block + i3];
180
61.7M
    } else {
181
        // 18-bit indexes stored in groups of 9 entries per 8 indexes.
182
0
        i3Block = (i3Block & 0x7fff) + (i3 & ~7) + (i3 >> 3);
183
0
        i3 &= 7;
184
0
        dataBlock = ((int32_t)trie->index[i3Block++] << (2 + (2 * i3))) & 0x30000;
185
0
        dataBlock |= trie->index[i3Block + i3];
186
0
    }
187
61.7M
    return dataBlock + (c & UCPTRIE_SMALL_DATA_MASK);
188
61.7M
}
189
190
U_CAPI int32_t U_EXPORT2
191
0
ucptrie_internalSmallU8Index(const UCPTrie *trie, int32_t lt1, uint8_t t2, uint8_t t3) {
192
0
    UChar32 c = (lt1 << 12) | (t2 << 6) | t3;
193
0
    if (c >= trie->highStart) {
194
        // Possible because the UTF-8 macro compares with shifted12HighStart which may be higher.
195
0
        return trie->dataLength - UCPTRIE_HIGH_VALUE_NEG_DATA_OFFSET;
196
0
    }
197
0
    return ucptrie_internalSmallIndex(trie, c);
198
0
}
199
200
U_CAPI int32_t U_EXPORT2
201
ucptrie_internalU8PrevIndex(const UCPTrie *trie, UChar32 c,
202
0
                            const uint8_t *start, const uint8_t *src) {
203
0
    int32_t i, length;
204
    // Support 64-bit pointers by avoiding cast of arbitrary difference.
205
0
    if ((src - start) <= 7) {
206
0
        i = length = (int32_t)(src - start);
207
0
    } else {
208
0
        i = length = 7;
209
0
        start = src - 7;
210
0
    }
211
0
    c = utf8_prevCharSafeBody(start, 0, &i, c, -1);
212
0
    i = length - i;  // Number of bytes read backward from src.
213
0
    int32_t idx = _UCPTRIE_CP_INDEX(trie, 0xffff, c);
214
0
    return (idx << 3) | i;
215
0
}
216
217
namespace {
218
219
64.7M
inline uint32_t getValue(UCPTrieData data, UCPTrieValueWidth valueWidth, int32_t dataIndex) {
220
64.7M
    switch (valueWidth) {
221
4.16M
    case UCPTRIE_VALUE_BITS_16:
222
4.16M
        return data.ptr16[dataIndex];
223
18.4M
    case UCPTRIE_VALUE_BITS_32:
224
18.4M
        return data.ptr32[dataIndex];
225
42.2M
    case UCPTRIE_VALUE_BITS_8:
226
42.2M
        return data.ptr8[dataIndex];
227
0
    default:
228
        // Unreachable if the trie is properly initialized.
229
0
        return 0xffffffff;
230
64.7M
    }
231
64.7M
}
232
233
}  // namespace
234
235
U_CAPI uint32_t U_EXPORT2
236
64.3M
ucptrie_get(const UCPTrie *trie, UChar32 c) {
237
64.3M
    int32_t dataIndex;
238
64.3M
    if ((uint32_t)c <= 0x7f) {
239
        // linear ASCII
240
3.42M
        dataIndex = c;
241
60.8M
    } else {
242
60.8M
        UChar32 fastMax = trie->type == UCPTRIE_TYPE_FAST ? 0xffff : UCPTRIE_SMALL_MAX;
243
60.8M
        dataIndex = _UCPTRIE_CP_INDEX(trie, fastMax, c);
244
60.8M
    }
245
64.3M
    return getValue(trie->data, (UCPTrieValueWidth)trie->valueWidth, dataIndex);
246
64.3M
}
247
248
namespace {
249
250
constexpr int32_t MAX_UNICODE = 0x10ffff;
251
252
inline uint32_t maybeFilterValue(uint32_t value, uint32_t trieNullValue, uint32_t nullValue,
253
84.2k
                                 UCPMapValueFilter *filter, const void *context) {
254
84.2k
    if (value == trieNullValue) {
255
11.2k
        value = nullValue;
256
72.9k
    } else if (filter != nullptr) {
257
1.63k
        value = filter(context, value);
258
1.63k
    }
259
84.2k
    return value;
260
84.2k
}
261
262
UChar32 getRange(const void *t, UChar32 start,
263
81.9k
                 UCPMapValueFilter *filter, const void *context, uint32_t *pValue) {
264
81.9k
    if (static_cast<uint32_t>(start) > MAX_UNICODE) {
265
27
        return U_SENTINEL;
266
27
    }
267
81.9k
    const UCPTrie *trie = reinterpret_cast<const UCPTrie *>(t);
268
81.9k
    UCPTrieValueWidth valueWidth = static_cast<UCPTrieValueWidth>(trie->valueWidth);
269
81.9k
    if (start >= trie->highStart) {
270
2
        if (pValue != nullptr) {
271
2
            int32_t di = trie->dataLength - UCPTRIE_HIGH_VALUE_NEG_DATA_OFFSET;
272
2
            uint32_t value = getValue(trie->data, valueWidth, di);
273
2
            if (filter != nullptr) { value = filter(context, value); }
274
2
            *pValue = value;
275
2
        }
276
2
        return MAX_UNICODE;
277
2
    }
278
279
81.9k
    uint32_t nullValue = trie->nullValue;
280
81.9k
    if (filter != nullptr) { nullValue = filter(context, nullValue); }
281
81.9k
    const uint16_t *index = trie->index;
282
283
81.9k
    int32_t prevI3Block = -1;
284
81.9k
    int32_t prevBlock = -1;
285
81.9k
    UChar32 c = start;
286
81.9k
    uint32_t trieValue, value = nullValue;
287
81.9k
    bool haveValue = false;
288
103k
    do {
289
103k
        int32_t i3Block;
290
103k
        int32_t i3;
291
103k
        int32_t i3BlockLength;
292
103k
        int32_t dataBlockLength;
293
103k
        if (c <= 0xffff && (trie->type == UCPTRIE_TYPE_FAST || c <= UCPTRIE_SMALL_MAX)) {
294
58.9k
            i3Block = 0;
295
58.9k
            i3 = c >> UCPTRIE_FAST_SHIFT;
296
58.9k
            i3BlockLength = trie->type == UCPTRIE_TYPE_FAST ?
297
57.0k
                UCPTRIE_BMP_INDEX_LENGTH : UCPTRIE_SMALL_INDEX_LENGTH;
298
58.9k
            dataBlockLength = UCPTRIE_FAST_DATA_BLOCK_LENGTH;
299
58.9k
        } else {
300
            // Use the multi-stage index.
301
44.7k
            int32_t i1 = c >> UCPTRIE_SHIFT_1;
302
44.7k
            if (trie->type == UCPTRIE_TYPE_FAST) {
303
29.9k
                U_ASSERT(0xffff < c && c < trie->highStart);
304
29.9k
                i1 += UCPTRIE_BMP_INDEX_LENGTH - UCPTRIE_OMITTED_BMP_INDEX_1_LENGTH;
305
29.9k
            } else {
306
14.8k
                U_ASSERT(c < trie->highStart && trie->highStart > UCPTRIE_SMALL_LIMIT);
307
14.8k
                i1 += UCPTRIE_SMALL_INDEX_LENGTH;
308
14.8k
            }
309
44.7k
            i3Block = trie->index[
310
44.7k
                static_cast<int32_t>(trie->index[i1]) + ((c >> UCPTRIE_SHIFT_2) & UCPTRIE_INDEX_2_MASK)];
311
44.7k
            if (i3Block == prevI3Block && (c - start) >= UCPTRIE_CP_PER_INDEX_2_ENTRY) {
312
                // The index-3 block is the same as the previous one, and filled with value.
313
20.4k
                U_ASSERT((c & (UCPTRIE_CP_PER_INDEX_2_ENTRY - 1)) == 0);
314
20.4k
                c += UCPTRIE_CP_PER_INDEX_2_ENTRY;
315
20.4k
                continue;
316
20.4k
            }
317
24.2k
            prevI3Block = i3Block;
318
24.2k
            if (i3Block == trie->index3NullOffset) {
319
                // This is the index-3 null block.
320
290
                if (haveValue) {
321
270
                    if (nullValue != value) {
322
20
                        return c - 1;
323
20
                    }
324
270
                } else {
325
20
                    trieValue = trie->nullValue;
326
20
                    value = nullValue;
327
20
                    if (pValue != nullptr) { *pValue = nullValue; }
328
20
                    haveValue = true;
329
20
                }
330
270
                prevBlock = trie->dataNullOffset;
331
270
                c = (c + UCPTRIE_CP_PER_INDEX_2_ENTRY) & ~(UCPTRIE_CP_PER_INDEX_2_ENTRY - 1);
332
270
                continue;
333
290
            }
334
23.9k
            i3 = (c >> UCPTRIE_SHIFT_3) & UCPTRIE_INDEX_3_MASK;
335
23.9k
            i3BlockLength = UCPTRIE_INDEX_3_BLOCK_LENGTH;
336
23.9k
            dataBlockLength = UCPTRIE_SMALL_DATA_BLOCK_LENGTH;
337
23.9k
        }
338
        // Enumerate data blocks for one index-3 block.
339
131k
        do {
340
131k
            int32_t block;
341
131k
            if ((i3Block & 0x8000) == 0) {
342
131k
                block = index[i3Block + i3];
343
131k
            } else {
344
                // 18-bit indexes stored in groups of 9 entries per 8 indexes.
345
0
                int32_t group = (i3Block & 0x7fff) + (i3 & ~7) + (i3 >> 3);
346
0
                int32_t gi = i3 & 7;
347
0
                block = (static_cast<int32_t>(index[group++]) << (2 + (2 * gi))) & 0x30000;
348
0
                block |= index[group + gi];
349
0
            }
350
131k
            if (block == prevBlock && (c - start) >= dataBlockLength) {
351
                // The block is the same as the previous one, and filled with value.
352
37.1k
                U_ASSERT((c & (dataBlockLength - 1)) == 0);
353
37.1k
                c += dataBlockLength;
354
94.5k
            } else {
355
94.5k
                int32_t dataMask = dataBlockLength - 1;
356
94.5k
                prevBlock = block;
357
94.5k
                if (block == trie->dataNullOffset) {
358
                    // This is the data null block.
359
2.93k
                    if (haveValue) {
360
2.50k
                        if (nullValue != value) {
361
409
                            return c - 1;
362
409
                        }
363
2.50k
                    } else {
364
431
                        trieValue = trie->nullValue;
365
431
                        value = nullValue;
366
431
                        if (pValue != nullptr) { *pValue = nullValue; }
367
431
                        haveValue = true;
368
431
                    }
369
2.53k
                    c = (c + dataBlockLength) & ~dataMask;
370
91.5k
                } else {
371
91.5k
                    int32_t di = block + (c & dataMask);
372
91.5k
                    uint32_t trieValue2 = getValue(trie->data, valueWidth, di);
373
91.5k
                    if (haveValue) {
374
10.0k
                        if (trieValue2 != trieValue) {
375
3.44k
                            if (filter == nullptr ||
376
144
                                    maybeFilterValue(trieValue2, trie->nullValue, nullValue,
377
3.33k
                                                     filter, context) != value) {
378
3.33k
                                return c - 1;
379
3.33k
                            }
380
108
                            trieValue = trieValue2;  // may or may not help
381
108
                        }
382
81.5k
                    } else {
383
81.5k
                        trieValue = trieValue2;
384
81.5k
                        value = maybeFilterValue(trieValue2, trie->nullValue, nullValue,
385
81.5k
                                                 filter, context);
386
81.5k
                        if (pValue != nullptr) { *pValue = value; }
387
81.5k
                        haveValue = true;
388
81.5k
                    }
389
404k
                    while ((++c & dataMask) != 0) {
390
394k
                        trieValue2 = getValue(trie->data, valueWidth, ++di);
391
394k
                        if (trieValue2 != trieValue) {
392
80.2k
                            if (filter == nullptr ||
393
2.53k
                                    maybeFilterValue(trieValue2, trie->nullValue, nullValue,
394
78.1k
                                                     filter, context) != value) {
395
78.1k
                                return c - 1;
396
78.1k
                            }
397
2.11k
                            trieValue = trieValue2;  // may or may not help
398
2.11k
                        }
399
394k
                    }
400
88.2k
                }
401
94.5k
            }
402
131k
        } while (++i3 < i3BlockLength);
403
82.8k
    } while (c < trie->highStart);
404
30
    U_ASSERT(haveValue);
405
30
    int32_t di = trie->dataLength - UCPTRIE_HIGH_VALUE_NEG_DATA_OFFSET;
406
30
    uint32_t highValue = getValue(trie->data, valueWidth, di);
407
30
    if (maybeFilterValue(highValue, trie->nullValue, nullValue,
408
30
                         filter, context) != value) {
409
5
        return c - 1;
410
25
    } else {
411
25
        return MAX_UNICODE;
412
25
    }
413
30
}
414
415
}  // namespace
416
417
U_CFUNC UChar32
418
ucptrie_internalGetRange(UCPTrieGetRange *getRange,
419
                         const void *trie, UChar32 start,
420
                         UCPMapRangeOption option, uint32_t surrogateValue,
421
81.9k
                         UCPMapValueFilter *filter, const void *context, uint32_t *pValue) {
422
81.9k
    if (option == UCPMAP_RANGE_NORMAL) {
423
8.06k
        return getRange(trie, start, filter, context, pValue);
424
8.06k
    }
425
73.8k
    uint32_t value;
426
73.8k
    if (pValue == nullptr) {
427
        // We need to examine the range value even if the caller does not want it.
428
0
        pValue = &value;
429
0
    }
430
73.8k
    UChar32 surrEnd = option == UCPMAP_RANGE_FIXED_ALL_SURROGATES ? 0xdfff : 0xdbff;
431
73.8k
    UChar32 end = getRange(trie, start, filter, context, pValue);
432
73.8k
    if (end < 0xd7ff || start > surrEnd) {
433
73.8k
        return end;
434
73.8k
    }
435
    // The range overlaps with surrogates, or ends just before the first one.
436
15
    if (*pValue == surrogateValue) {
437
15
        if (end >= surrEnd) {
438
            // Surrogates followed by a non-surrogateValue range,
439
            // or surrogates are part of a larger surrogateValue range.
440
0
            return end;
441
0
        }
442
15
    } else {
443
0
        if (start <= 0xd7ff) {
444
0
            return 0xd7ff;  // Non-surrogateValue range ends before surrogateValue surrogates.
445
0
        }
446
        // Start is a surrogate with a non-surrogateValue code *unit* value.
447
        // Return a surrogateValue code *point* range.
448
0
        *pValue = surrogateValue;
449
0
        if (end > surrEnd) {
450
0
            return surrEnd;  // Surrogate range ends before non-surrogateValue rest of range.
451
0
        }
452
0
    }
453
    // See if the surrogateValue surrogate range can be merged with
454
    // an immediately following range.
455
15
    uint32_t value2;
456
15
    UChar32 end2 = getRange(trie, surrEnd + 1, filter, context, &value2);
457
15
    if (value2 == surrogateValue) {
458
15
        return end2;
459
15
    }
460
0
    return surrEnd;
461
15
}
462
463
U_CAPI UChar32 U_EXPORT2
464
ucptrie_getRange(const UCPTrie *trie, UChar32 start,
465
                 UCPMapRangeOption option, uint32_t surrogateValue,
466
81.9k
                 UCPMapValueFilter *filter, const void *context, uint32_t *pValue) {
467
81.9k
    return ucptrie_internalGetRange(getRange, trie, start,
468
81.9k
                                    option, surrogateValue,
469
81.9k
                                    filter, context, pValue);
470
81.9k
}
471
472
U_CAPI int32_t U_EXPORT2
473
ucptrie_toBinary(const UCPTrie *trie,
474
                 void *data, int32_t capacity,
475
5.03k
                 UErrorCode *pErrorCode) {
476
5.03k
    if (U_FAILURE(*pErrorCode)) {
477
0
        return 0;
478
0
    }
479
480
5.03k
    UCPTrieType type = (UCPTrieType)trie->type;
481
5.03k
    UCPTrieValueWidth valueWidth = (UCPTrieValueWidth)trie->valueWidth;
482
5.03k
    if (type < UCPTRIE_TYPE_FAST || UCPTRIE_TYPE_SMALL < type ||
483
5.03k
            valueWidth < UCPTRIE_VALUE_BITS_16 || UCPTRIE_VALUE_BITS_8 < valueWidth ||
484
5.03k
            capacity < 0 ||
485
5.03k
            (capacity > 0 && (data == nullptr || (U_POINTER_MASK_LSB(data, 3) != 0)))) {
486
0
        *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
487
0
        return 0;
488
0
    }
489
490
5.03k
    int32_t length = (int32_t)sizeof(UCPTrieHeader) + trie->indexLength * 2;
491
5.03k
    switch (valueWidth) {
492
14
    case UCPTRIE_VALUE_BITS_16:
493
14
        length += trie->dataLength * 2;
494
14
        break;
495
0
    case UCPTRIE_VALUE_BITS_32:
496
0
        length += trie->dataLength * 4;
497
0
        break;
498
5.01k
    case UCPTRIE_VALUE_BITS_8:
499
5.01k
        length += trie->dataLength;
500
5.01k
        break;
501
0
    default:
502
        // unreachable
503
0
        break;
504
5.03k
    }
505
5.03k
    if (capacity < length) {
506
2.51k
        *pErrorCode = U_BUFFER_OVERFLOW_ERROR;
507
2.51k
        return length;
508
2.51k
    }
509
510
2.51k
    char *bytes = (char *)data;
511
2.51k
    UCPTrieHeader *header = (UCPTrieHeader *)bytes;
512
2.51k
    header->signature = UCPTRIE_SIG;  // "Tri3"
513
2.51k
    header->options = (uint16_t)(
514
2.51k
        ((trie->dataLength & 0xf0000) >> 4) |
515
2.51k
        ((trie->dataNullOffset & 0xf0000) >> 8) |
516
2.51k
        (trie->type << 6) |
517
2.51k
        valueWidth);
518
2.51k
    header->indexLength = (uint16_t)trie->indexLength;
519
2.51k
    header->dataLength = (uint16_t)trie->dataLength;
520
2.51k
    header->index3NullOffset = trie->index3NullOffset;
521
2.51k
    header->dataNullOffset = (uint16_t)trie->dataNullOffset;
522
2.51k
    header->shiftedHighStart = trie->highStart >> UCPTRIE_SHIFT_2;
523
2.51k
    bytes += sizeof(UCPTrieHeader);
524
525
2.51k
    uprv_memcpy(bytes, trie->index, trie->indexLength * 2);
526
2.51k
    bytes += trie->indexLength * 2;
527
528
2.51k
    switch (valueWidth) {
529
7
    case UCPTRIE_VALUE_BITS_16:
530
7
        uprv_memcpy(bytes, trie->data.ptr16, trie->dataLength * 2);
531
7
        break;
532
0
    case UCPTRIE_VALUE_BITS_32:
533
0
        uprv_memcpy(bytes, trie->data.ptr32, trie->dataLength * 4);
534
0
        break;
535
2.50k
    case UCPTRIE_VALUE_BITS_8:
536
2.50k
        uprv_memcpy(bytes, trie->data.ptr8, trie->dataLength);
537
2.50k
        break;
538
0
    default:
539
        // unreachable
540
0
        break;
541
2.51k
    }
542
2.51k
    return length;
543
2.51k
}
544
545
namespace {
546
547
#ifdef UCPTRIE_DEBUG
548
long countNull(const UCPTrie *trie) {
549
    uint32_t nullValue=trie->nullValue;
550
    int32_t length=trie->dataLength;
551
    long count=0;
552
    switch (trie->valueWidth) {
553
    case UCPTRIE_VALUE_BITS_16:
554
        for(int32_t i=0; i<length; ++i) {
555
            if(trie->data.ptr16[i]==nullValue) { ++count; }
556
        }
557
        break;
558
    case UCPTRIE_VALUE_BITS_32:
559
        for(int32_t i=0; i<length; ++i) {
560
            if(trie->data.ptr32[i]==nullValue) { ++count; }
561
        }
562
        break;
563
    case UCPTRIE_VALUE_BITS_8:
564
        for(int32_t i=0; i<length; ++i) {
565
            if(trie->data.ptr8[i]==nullValue) { ++count; }
566
        }
567
        break;
568
    default:
569
        // unreachable
570
        break;
571
    }
572
    return count;
573
}
574
575
U_CFUNC void
576
ucptrie_printLengths(const UCPTrie *trie, const char *which) {
577
    long indexLength=trie->indexLength;
578
    long dataLength=(long)trie->dataLength;
579
    long totalLength=(long)sizeof(UCPTrieHeader)+indexLength*2+
580
            dataLength*(trie->valueWidth==UCPTRIE_VALUE_BITS_16 ? 2 :
581
                        trie->valueWidth==UCPTRIE_VALUE_BITS_32 ? 4 : 1);
582
    printf("**UCPTrieLengths(%s %s)** index:%6ld  data:%6ld  countNull:%6ld  serialized:%6ld\n",
583
           which, trie->name, indexLength, dataLength, countNull(trie), totalLength);
584
}
585
#endif
586
587
}  // namespace
588
589
// UCPMap ----
590
// Initially, this is the same as UCPTrie. This may well change.
591
592
U_CAPI uint32_t U_EXPORT2
593
0
ucpmap_get(const UCPMap *map, UChar32 c) {
594
0
    return ucptrie_get(reinterpret_cast<const UCPTrie *>(map), c);
595
0
}
596
597
U_CAPI UChar32 U_EXPORT2
598
ucpmap_getRange(const UCPMap *map, UChar32 start,
599
                UCPMapRangeOption option, uint32_t surrogateValue,
600
0
                UCPMapValueFilter *filter, const void *context, uint32_t *pValue) {
601
0
    return ucptrie_getRange(reinterpret_cast<const UCPTrie *>(map), start,
602
0
                            option, surrogateValue,
603
0
                            filter, context, pValue);
604
0
}