Coverage Report

Created: 2025-06-24 06:54

/src/icu/icu4c/source/i18n/collationsettings.cpp
Line
Count
Source (jump to first uncovered line)
1
// © 2016 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
/*
4
*******************************************************************************
5
* Copyright (C) 2013-2015, International Business Machines
6
* Corporation and others.  All Rights Reserved.
7
*******************************************************************************
8
* collationsettings.cpp
9
*
10
* created on: 2013feb07
11
* created by: Markus W. Scherer
12
*/
13
14
#include "unicode/utypes.h"
15
16
#if !UCONFIG_NO_COLLATION
17
18
#include "unicode/ucol.h"
19
#include "cmemory.h"
20
#include "collation.h"
21
#include "collationdata.h"
22
#include "collationsettings.h"
23
#include "sharedobject.h"
24
#include "uassert.h"
25
#include "umutex.h"
26
#include "uvectr32.h"
27
28
U_NAMESPACE_BEGIN
29
30
CollationSettings::CollationSettings(const CollationSettings &other)
31
14.8k
        : SharedObject(other),
32
14.8k
          options(other.options), variableTop(other.variableTop),
33
14.8k
          reorderTable(nullptr),
34
14.8k
          minHighNoReorder(other.minHighNoReorder),
35
14.8k
          reorderRanges(nullptr), reorderRangesLength(0),
36
14.8k
          reorderCodes(nullptr), reorderCodesLength(0), reorderCodesCapacity(0),
37
14.8k
          fastLatinOptions(other.fastLatinOptions) {
38
14.8k
    UErrorCode errorCode = U_ZERO_ERROR;
39
14.8k
    copyReorderingFrom(other, errorCode);
40
14.8k
    if(fastLatinOptions >= 0) {
41
14.7k
        uprv_memcpy(fastLatinPrimaries, other.fastLatinPrimaries, sizeof(fastLatinPrimaries));
42
14.7k
    }
43
14.8k
}
44
45
14.7k
CollationSettings::~CollationSettings() {
46
14.7k
    if(reorderCodesCapacity != 0) {
47
897
        uprv_free(const_cast<int32_t *>(reorderCodes));
48
897
    }
49
14.7k
}
50
51
bool
52
0
CollationSettings::operator==(const CollationSettings &other) const {
53
0
    if(options != other.options) { return false; }
54
0
    if((options & ALTERNATE_MASK) != 0 && variableTop != other.variableTop) { return false; }
55
0
    if(reorderCodesLength != other.reorderCodesLength) { return false; }
56
0
    for(int32_t i = 0; i < reorderCodesLength; ++i) {
57
0
        if(reorderCodes[i] != other.reorderCodes[i]) { return false; }
58
0
    }
59
0
    return true;
60
0
}
61
62
int32_t
63
0
CollationSettings::hashCode() const {
64
0
    int32_t h = options << 8;
65
0
    if((options & ALTERNATE_MASK) != 0) { h ^= variableTop; }
66
0
    h ^= reorderCodesLength;
67
0
    for(int32_t i = 0; i < reorderCodesLength; ++i) {
68
0
        h ^= (reorderCodes[i] << i);
69
0
    }
70
0
    return h;
71
0
}
72
73
void
74
11.7k
CollationSettings::resetReordering() {
75
    // When we turn off reordering, we want to set a nullptr permutation
76
    // rather than a no-op permutation.
77
    // Keep the memory via reorderCodes and its capacity.
78
11.7k
    reorderTable = nullptr;
79
11.7k
    minHighNoReorder = 0;
80
11.7k
    reorderRangesLength = 0;
81
11.7k
    reorderCodesLength = 0;
82
11.7k
}
83
84
void
85
CollationSettings::aliasReordering(const CollationData &data, const int32_t *codes, int32_t length,
86
                                   const uint32_t *ranges, int32_t rangesLength,
87
84
                                   const uint8_t *table, UErrorCode &errorCode) {
88
84
    if(U_FAILURE(errorCode)) { return; }
89
84
    if(table != nullptr &&
90
84
            (rangesLength == 0 ?
91
45
                    !reorderTableHasSplitBytes(table) :
92
84
                    rangesLength >= 2 &&
93
                    // The first offset must be 0. The last offset must not be 0.
94
84
                    (ranges[0] & 0xffff) == 0 && (ranges[rangesLength - 1] & 0xffff) != 0)) {
95
        // We need to release the memory before setting the alias pointer.
96
84
        if(reorderCodesCapacity != 0) {
97
0
            uprv_free(const_cast<int32_t *>(reorderCodes));
98
0
            reorderCodesCapacity = 0;
99
0
        }
100
84
        reorderTable = table;
101
84
        reorderCodes = codes;
102
84
        reorderCodesLength = length;
103
        // Drop ranges before the first split byte. They are reordered by the table.
104
        // This then speeds up reordering of the remaining ranges.
105
84
        int32_t firstSplitByteRangeIndex = 0;
106
219
        while(firstSplitByteRangeIndex < rangesLength &&
107
219
                (ranges[firstSplitByteRangeIndex] & 0xff0000) == 0) {
108
            // The second byte of the primary limit is 0.
109
135
            ++firstSplitByteRangeIndex;
110
135
        }
111
84
        if(firstSplitByteRangeIndex == rangesLength) {
112
45
            U_ASSERT(!reorderTableHasSplitBytes(table));
113
45
            minHighNoReorder = 0;
114
45
            reorderRanges = nullptr;
115
45
            reorderRangesLength = 0;
116
45
        } else {
117
39
            U_ASSERT(table[ranges[firstSplitByteRangeIndex] >> 24] == 0);
118
39
            minHighNoReorder = ranges[rangesLength - 1] & 0xffff0000;
119
39
            reorderRanges = ranges + firstSplitByteRangeIndex;
120
39
            reorderRangesLength = rangesLength - firstSplitByteRangeIndex;
121
39
        }
122
84
        return;
123
84
    }
124
    // Regenerate missing data.
125
0
    setReordering(data, codes, length, errorCode);
126
0
}
127
128
void
129
CollationSettings::setReordering(const CollationData &data,
130
                                 const int32_t *codes, int32_t codesLength,
131
3.53k
                                 UErrorCode &errorCode) {
132
3.53k
    if(U_FAILURE(errorCode)) { return; }
133
3.53k
    if(codesLength == 0 || (codesLength == 1 && codes[0] == UCOL_REORDER_CODE_NONE)) {
134
1
        resetReordering();
135
1
        return;
136
1
    }
137
3.53k
    UVector32 rangesList(errorCode);
138
3.53k
    data.makeReorderRanges(codes, codesLength, rangesList, errorCode);
139
3.53k
    if(U_FAILURE(errorCode)) { return; }
140
3.46k
    int32_t rangesLength = rangesList.size();
141
3.46k
    if(rangesLength == 0) {
142
22
        resetReordering();
143
22
        return;
144
22
    }
145
3.44k
    const uint32_t *ranges = reinterpret_cast<uint32_t *>(rangesList.getBuffer());
146
    // ranges[] contains at least two (limit, offset) pairs.
147
    // The first offset must be 0. The last offset must not be 0.
148
    // Separators (at the low end) and trailing weights (at the high end)
149
    // are never reordered.
150
3.44k
    U_ASSERT(rangesLength >= 2);
151
3.44k
    U_ASSERT((ranges[0] & 0xffff) == 0 && (ranges[rangesLength - 1] & 0xffff) != 0);
152
3.44k
    minHighNoReorder = ranges[rangesLength - 1] & 0xffff0000;
153
154
    // Write the lead byte permutation table.
155
    // Set a 0 for each lead byte that has a range boundary in the middle.
156
3.44k
    uint8_t table[256];
157
3.44k
    int32_t b = 0;
158
3.44k
    int32_t firstSplitByteRangeIndex = -1;
159
13.4k
    for(int32_t i = 0; i < rangesLength; ++i) {
160
10.0k
        uint32_t pair = ranges[i];
161
10.0k
        int32_t limit1 = static_cast<int32_t>(pair >> 24);
162
393k
        while(b < limit1) {
163
383k
            table[b] = static_cast<uint8_t>(b + pair);
164
383k
            ++b;
165
383k
        }
166
        // Check the second byte of the limit.
167
10.0k
        if((pair & 0xff0000) != 0) {
168
1.48k
            table[limit1] = 0;
169
1.48k
            b = limit1 + 1;
170
1.48k
            if(firstSplitByteRangeIndex < 0) {
171
1.30k
                firstSplitByteRangeIndex = i;
172
1.30k
            }
173
1.48k
        }
174
10.0k
    }
175
500k
    while(b <= 0xff) {
176
496k
        table[b] = static_cast<uint8_t>(b);
177
496k
        ++b;
178
496k
    }
179
3.44k
    if(firstSplitByteRangeIndex < 0) {
180
        // The lead byte permutation table alone suffices for reordering.
181
2.14k
        rangesLength = 0;
182
2.14k
    } else {
183
        // Remove the ranges below the first split byte.
184
1.30k
        ranges += firstSplitByteRangeIndex;
185
1.30k
        rangesLength -= firstSplitByteRangeIndex;
186
1.30k
    }
187
3.44k
    setReorderArrays(codes, codesLength, ranges, rangesLength, table, errorCode);
188
3.44k
}
189
190
void
191
CollationSettings::setReorderArrays(const int32_t *codes, int32_t codesLength,
192
                                    const uint32_t *ranges, int32_t rangesLength,
193
3.44k
                                    const uint8_t *table, UErrorCode &errorCode) {
194
3.44k
    if(U_FAILURE(errorCode)) { return; }
195
3.44k
    int32_t *ownedCodes;
196
3.44k
    int32_t totalLength = codesLength + rangesLength;
197
3.44k
    U_ASSERT(totalLength > 0);
198
3.44k
    if(totalLength <= reorderCodesCapacity) {
199
2.46k
        ownedCodes = const_cast<int32_t *>(reorderCodes);
200
2.46k
    } else {
201
        // Allocate one memory block for the codes, the ranges, and the 16-aligned table.
202
979
        int32_t capacity = (totalLength + 3) & ~3;  // round up to a multiple of 4 ints
203
979
        ownedCodes = static_cast<int32_t*>(uprv_malloc(capacity * 4 + 256));
204
979
        if(ownedCodes == nullptr) {
205
0
            resetReordering();
206
0
            errorCode = U_MEMORY_ALLOCATION_ERROR;
207
0
            return;
208
0
        }
209
979
        if(reorderCodesCapacity != 0) {
210
82
            uprv_free(const_cast<int32_t *>(reorderCodes));
211
82
        }
212
979
        reorderCodes = ownedCodes;
213
979
        reorderCodesCapacity = capacity;
214
979
    }
215
3.44k
    uprv_memcpy(ownedCodes + reorderCodesCapacity, table, 256);
216
3.44k
    uprv_memcpy(ownedCodes, codes, codesLength * 4);
217
3.44k
    uprv_memcpy(ownedCodes + codesLength, ranges, rangesLength * 4);
218
3.44k
    reorderTable = reinterpret_cast<const uint8_t *>(reorderCodes + reorderCodesCapacity);
219
3.44k
    reorderCodesLength = codesLength;
220
3.44k
    reorderRanges = reinterpret_cast<uint32_t *>(ownedCodes) + codesLength;
221
3.44k
    reorderRangesLength = rangesLength;
222
3.44k
}
223
224
void
225
14.8k
CollationSettings::copyReorderingFrom(const CollationSettings &other, UErrorCode &errorCode) {
226
14.8k
    if(U_FAILURE(errorCode)) { return; }
227
14.8k
    if(!other.hasReordering()) {
228
11.7k
        resetReordering();
229
11.7k
        return;
230
11.7k
    }
231
3.10k
    minHighNoReorder = other.minHighNoReorder;
232
3.10k
    if(other.reorderCodesCapacity == 0) {
233
        // The reorder arrays are aliased to memory-mapped data.
234
3.10k
        reorderTable = other.reorderTable;
235
3.10k
        reorderRanges = other.reorderRanges;
236
3.10k
        reorderRangesLength = other.reorderRangesLength;
237
3.10k
        reorderCodes = other.reorderCodes;
238
3.10k
        reorderCodesLength = other.reorderCodesLength;
239
3.10k
    } else {
240
0
        setReorderArrays(other.reorderCodes, other.reorderCodesLength,
241
0
                         other.reorderRanges, other.reorderRangesLength,
242
0
                         other.reorderTable, errorCode);
243
0
    }
244
3.10k
}
245
246
UBool
247
45
CollationSettings::reorderTableHasSplitBytes(const uint8_t table[256]) {
248
45
    U_ASSERT(table[0] == 0);
249
11.5k
    for(int32_t i = 1; i < 256; ++i) {
250
11.4k
        if(table[i] == 0) {
251
0
            return true;
252
0
        }
253
11.4k
    }
254
45
    return false;
255
45
}
256
257
uint32_t
258
2.34k
CollationSettings::reorderEx(uint32_t p) const {
259
2.34k
    if(p >= minHighNoReorder) { return p; }
260
    // Round up p so that its lower 16 bits are >= any offset bits.
261
    // Then compare q directly with (limit, offset) pairs.
262
1.71k
    uint32_t q = p | 0xffff;
263
1.71k
    uint32_t r;
264
1.71k
    const uint32_t *ranges = reorderRanges;
265
2.43k
    while(q >= (r = *ranges)) { ++ranges; }
266
1.71k
    return p + (r << 24);
267
2.34k
}
268
269
void
270
6.78k
CollationSettings::setStrength(int32_t value, int32_t defaultOptions, UErrorCode &errorCode) {
271
6.78k
    if(U_FAILURE(errorCode)) { return; }
272
6.78k
    int32_t noStrength = options & ~STRENGTH_MASK;
273
6.78k
    switch(value) {
274
1.38k
    case UCOL_PRIMARY:
275
2.72k
    case UCOL_SECONDARY:
276
2.73k
    case UCOL_TERTIARY:
277
4.26k
    case UCOL_QUATERNARY:
278
6.77k
    case UCOL_IDENTICAL:
279
6.77k
        options = noStrength | (value << STRENGTH_SHIFT);
280
6.77k
        break;
281
0
    case UCOL_DEFAULT:
282
0
        options = noStrength | (defaultOptions & STRENGTH_MASK);
283
0
        break;
284
16
    default:
285
16
        errorCode = U_ILLEGAL_ARGUMENT_ERROR;
286
16
        break;
287
6.78k
    }
288
6.78k
}
289
290
void
291
CollationSettings::setFlag(int32_t bit, UColAttributeValue value,
292
2.06k
                           int32_t defaultOptions, UErrorCode &errorCode) {
293
2.06k
    if(U_FAILURE(errorCode)) { return; }
294
2.06k
    switch(value) {
295
2.06k
    case UCOL_ON:
296
2.06k
        options |= bit;
297
2.06k
        break;
298
0
    case UCOL_OFF:
299
0
        options &= ~bit;
300
0
        break;
301
0
    case UCOL_DEFAULT:
302
0
        options = (options & ~bit) | (defaultOptions & bit);
303
0
        break;
304
1
    default:
305
1
        errorCode = U_ILLEGAL_ARGUMENT_ERROR;
306
1
        break;
307
2.06k
    }
308
2.06k
}
309
310
void
311
CollationSettings::setCaseFirst(UColAttributeValue value,
312
144
                                int32_t defaultOptions, UErrorCode &errorCode) {
313
144
    if(U_FAILURE(errorCode)) { return; }
314
144
    int32_t noCaseFirst = options & ~CASE_FIRST_AND_UPPER_MASK;
315
144
    switch(value) {
316
0
    case UCOL_OFF:
317
0
        options = noCaseFirst;
318
0
        break;
319
0
    case UCOL_LOWER_FIRST:
320
0
        options = noCaseFirst | CASE_FIRST;
321
0
        break;
322
123
    case UCOL_UPPER_FIRST:
323
123
        options = noCaseFirst | CASE_FIRST_AND_UPPER_MASK;
324
123
        break;
325
0
    case UCOL_DEFAULT:
326
0
        options = noCaseFirst | (defaultOptions & CASE_FIRST_AND_UPPER_MASK);
327
0
        break;
328
21
    default:
329
21
        errorCode = U_ILLEGAL_ARGUMENT_ERROR;
330
21
        break;
331
144
    }
332
144
}
333
334
void
335
CollationSettings::setAlternateHandling(UColAttributeValue value,
336
472
                                        int32_t defaultOptions, UErrorCode &errorCode) {
337
472
    if(U_FAILURE(errorCode)) { return; }
338
472
    int32_t noAlternate = options & ~ALTERNATE_MASK;
339
472
    switch(value) {
340
0
    case UCOL_NON_IGNORABLE:
341
0
        options = noAlternate;
342
0
        break;
343
465
    case UCOL_SHIFTED:
344
465
        options = noAlternate | SHIFTED;
345
465
        break;
346
0
    case UCOL_DEFAULT:
347
0
        options = noAlternate | (defaultOptions & ALTERNATE_MASK);
348
0
        break;
349
7
    default:
350
7
        errorCode = U_ILLEGAL_ARGUMENT_ERROR;
351
7
        break;
352
472
    }
353
472
}
354
355
void
356
12
CollationSettings::setMaxVariable(int32_t value, int32_t defaultOptions, UErrorCode &errorCode) {
357
12
    if(U_FAILURE(errorCode)) { return; }
358
12
    int32_t noMax = options & ~MAX_VARIABLE_MASK;
359
12
    switch(value) {
360
9
    case MAX_VAR_SPACE:
361
9
    case MAX_VAR_PUNCT:
362
9
    case MAX_VAR_SYMBOL:
363
12
    case MAX_VAR_CURRENCY:
364
12
        options = noMax | (value << MAX_VARIABLE_SHIFT);
365
12
        break;
366
0
    case UCOL_DEFAULT:
367
0
        options = noMax | (defaultOptions & MAX_VARIABLE_MASK);
368
0
        break;
369
0
    default:
370
0
        errorCode = U_ILLEGAL_ARGUMENT_ERROR;
371
0
        break;
372
12
    }
373
12
}
374
375
U_NAMESPACE_END
376
377
#endif  // !UCONFIG_NO_COLLATION