Coverage Report

Created: 2022-11-20 06:20

/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
        : SharedObject(other),
32
          options(other.options), variableTop(other.variableTop),
33
          reorderTable(NULL),
34
          minHighNoReorder(other.minHighNoReorder),
35
          reorderRanges(NULL), reorderRangesLength(0),
36
          reorderCodes(NULL), reorderCodesLength(0), reorderCodesCapacity(0),
37
1.98k
          fastLatinOptions(other.fastLatinOptions) {
38
1.98k
    UErrorCode errorCode = U_ZERO_ERROR;
39
1.98k
    copyReorderingFrom(other, errorCode);
40
1.98k
    if(fastLatinOptions >= 0) {
41
1.98k
        uprv_memcpy(fastLatinPrimaries, other.fastLatinPrimaries, sizeof(fastLatinPrimaries));
42
1.98k
    }
43
1.98k
}
44
45
1.98k
CollationSettings::~CollationSettings() {
46
1.98k
    if(reorderCodesCapacity != 0) {
47
215
        uprv_free(const_cast<int32_t *>(reorderCodes));
48
215
    }
49
1.98k
}
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
1.98k
CollationSettings::resetReordering() {
75
    // When we turn off reordering, we want to set a NULL permutation
76
    // rather than a no-op permutation.
77
    // Keep the memory via reorderCodes and its capacity.
78
1.98k
    reorderTable = NULL;
79
1.98k
    minHighNoReorder = 0;
80
1.98k
    reorderRangesLength = 0;
81
1.98k
    reorderCodesLength = 0;
82
1.98k
}
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
0
                                   const uint8_t *table, UErrorCode &errorCode) {
88
0
    if(U_FAILURE(errorCode)) { return; }
89
0
    if(table != NULL &&
90
0
            (rangesLength == 0 ?
91
0
                    !reorderTableHasSplitBytes(table) :
92
0
                    rangesLength >= 2 &&
93
                    // The first offset must be 0. The last offset must not be 0.
94
0
                    (ranges[0] & 0xffff) == 0 && (ranges[rangesLength - 1] & 0xffff) != 0)) {
95
        // We need to release the memory before setting the alias pointer.
96
0
        if(reorderCodesCapacity != 0) {
97
0
            uprv_free(const_cast<int32_t *>(reorderCodes));
98
0
            reorderCodesCapacity = 0;
99
0
        }
100
0
        reorderTable = table;
101
0
        reorderCodes = codes;
102
0
        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
0
        int32_t firstSplitByteRangeIndex = 0;
106
0
        while(firstSplitByteRangeIndex < rangesLength &&
107
0
                (ranges[firstSplitByteRangeIndex] & 0xff0000) == 0) {
108
            // The second byte of the primary limit is 0.
109
0
            ++firstSplitByteRangeIndex;
110
0
        }
111
0
        if(firstSplitByteRangeIndex == rangesLength) {
112
0
            U_ASSERT(!reorderTableHasSplitBytes(table));
113
0
            minHighNoReorder = 0;
114
0
            reorderRanges = NULL;
115
0
            reorderRangesLength = 0;
116
0
        } else {
117
0
            U_ASSERT(table[ranges[firstSplitByteRangeIndex] >> 24] == 0);
118
0
            minHighNoReorder = ranges[rangesLength - 1] & 0xffff0000;
119
0
            reorderRanges = ranges + firstSplitByteRangeIndex;
120
0
            reorderRangesLength = rangesLength - firstSplitByteRangeIndex;
121
0
        }
122
0
        return;
123
0
    }
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
2.22k
                                 UErrorCode &errorCode) {
132
2.22k
    if(U_FAILURE(errorCode)) { return; }
133
2.22k
    if(codesLength == 0 || (codesLength == 1 && codes[0] == UCOL_REORDER_CODE_NONE)) {
134
0
        resetReordering();
135
0
        return;
136
0
    }
137
2.22k
    UVector32 rangesList(errorCode);
138
2.22k
    data.makeReorderRanges(codes, codesLength, rangesList, errorCode);
139
2.22k
    if(U_FAILURE(errorCode)) { return; }
140
2.22k
    int32_t rangesLength = rangesList.size();
141
2.22k
    if(rangesLength == 0) {
142
0
        resetReordering();
143
0
        return;
144
0
    }
145
2.22k
    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
2.22k
    U_ASSERT(rangesLength >= 2);
151
2.22k
    U_ASSERT((ranges[0] & 0xffff) == 0 && (ranges[rangesLength - 1] & 0xffff) != 0);
152
2.22k
    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
2.22k
    uint8_t table[256];
157
2.22k
    int32_t b = 0;
158
2.22k
    int32_t firstSplitByteRangeIndex = -1;
159
8.48k
    for(int32_t i = 0; i < rangesLength; ++i) {
160
6.26k
        uint32_t pair = ranges[i];
161
6.26k
        int32_t limit1 = (int32_t)(pair >> 24);
162
241k
        while(b < limit1) {
163
235k
            table[b] = (uint8_t)(b + pair);
164
235k
            ++b;
165
235k
        }
166
        // Check the second byte of the limit.
167
6.26k
        if((pair & 0xff0000) != 0) {
168
587
            table[limit1] = 0;
169
587
            b = limit1 + 1;
170
587
            if(firstSplitByteRangeIndex < 0) {
171
587
                firstSplitByteRangeIndex = i;
172
587
            }
173
587
        }
174
6.26k
    }
175
335k
    while(b <= 0xff) {
176
333k
        table[b] = (uint8_t)b;
177
333k
        ++b;
178
333k
    }
179
2.22k
    if(firstSplitByteRangeIndex < 0) {
180
        // The lead byte permutation table alone suffices for reordering.
181
1.63k
        rangesLength = 0;
182
1.63k
    } else {
183
        // Remove the ranges below the first split byte.
184
587
        ranges += firstSplitByteRangeIndex;
185
587
        rangesLength -= firstSplitByteRangeIndex;
186
587
    }
187
2.22k
    setReorderArrays(codes, codesLength, ranges, rangesLength, table, errorCode);
188
2.22k
}
189
190
void
191
CollationSettings::setReorderArrays(const int32_t *codes, int32_t codesLength,
192
                                    const uint32_t *ranges, int32_t rangesLength,
193
2.22k
                                    const uint8_t *table, UErrorCode &errorCode) {
194
2.22k
    if(U_FAILURE(errorCode)) { return; }
195
2.22k
    int32_t *ownedCodes;
196
2.22k
    int32_t totalLength = codesLength + rangesLength;
197
2.22k
    U_ASSERT(totalLength > 0);
198
2.22k
    if(totalLength <= reorderCodesCapacity) {
199
1.94k
        ownedCodes = const_cast<int32_t *>(reorderCodes);
200
1.94k
    } else {
201
        // Allocate one memory block for the codes, the ranges, and the 16-aligned table.
202
277
        int32_t capacity = (totalLength + 3) & ~3;  // round up to a multiple of 4 ints
203
277
        ownedCodes = (int32_t *)uprv_malloc(capacity * 4 + 256);
204
277
        if(ownedCodes == NULL) {
205
0
            resetReordering();
206
0
            errorCode = U_MEMORY_ALLOCATION_ERROR;
207
0
            return;
208
0
        }
209
277
        if(reorderCodesCapacity != 0) {
210
62
            uprv_free(const_cast<int32_t *>(reorderCodes));
211
62
        }
212
277
        reorderCodes = ownedCodes;
213
277
        reorderCodesCapacity = capacity;
214
277
    }
215
2.22k
    uprv_memcpy(ownedCodes + reorderCodesCapacity, table, 256);
216
2.22k
    uprv_memcpy(ownedCodes, codes, codesLength * 4);
217
2.22k
    uprv_memcpy(ownedCodes + codesLength, ranges, rangesLength * 4);
218
2.22k
    reorderTable = reinterpret_cast<const uint8_t *>(reorderCodes + reorderCodesCapacity);
219
2.22k
    reorderCodesLength = codesLength;
220
2.22k
    reorderRanges = reinterpret_cast<uint32_t *>(ownedCodes) + codesLength;
221
2.22k
    reorderRangesLength = rangesLength;
222
2.22k
}
223
224
void
225
1.98k
CollationSettings::copyReorderingFrom(const CollationSettings &other, UErrorCode &errorCode) {
226
1.98k
    if(U_FAILURE(errorCode)) { return; }
227
1.98k
    if(!other.hasReordering()) {
228
1.98k
        resetReordering();
229
1.98k
        return;
230
1.98k
    }
231
0
    minHighNoReorder = other.minHighNoReorder;
232
0
    if(other.reorderCodesCapacity == 0) {
233
        // The reorder arrays are aliased to memory-mapped data.
234
0
        reorderTable = other.reorderTable;
235
0
        reorderRanges = other.reorderRanges;
236
0
        reorderRangesLength = other.reorderRangesLength;
237
0
        reorderCodes = other.reorderCodes;
238
0
        reorderCodesLength = other.reorderCodesLength;
239
0
    } else {
240
0
        setReorderArrays(other.reorderCodes, other.reorderCodesLength,
241
0
                         other.reorderRanges, other.reorderRangesLength,
242
0
                         other.reorderTable, errorCode);
243
0
    }
244
0
}
245
246
UBool
247
0
CollationSettings::reorderTableHasSplitBytes(const uint8_t table[256]) {
248
0
    U_ASSERT(table[0] == 0);
249
0
    for(int32_t i = 1; i < 256; ++i) {
250
0
        if(table[i] == 0) {
251
0
            return true;
252
0
        }
253
0
    }
254
0
    return false;
255
0
}
256
257
uint32_t
258
0
CollationSettings::reorderEx(uint32_t p) const {
259
0
    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
0
    uint32_t q = p | 0xffff;
263
0
    uint32_t r;
264
0
    const uint32_t *ranges = reorderRanges;
265
0
    while(q >= (r = *ranges)) { ++ranges; }
266
0
    return p + (r << 24);
267
0
}
268
269
void
270
0
CollationSettings::setStrength(int32_t value, int32_t defaultOptions, UErrorCode &errorCode) {
271
0
    if(U_FAILURE(errorCode)) { return; }
272
0
    int32_t noStrength = options & ~STRENGTH_MASK;
273
0
    switch(value) {
274
0
    case UCOL_PRIMARY:
275
0
    case UCOL_SECONDARY:
276
0
    case UCOL_TERTIARY:
277
0
    case UCOL_QUATERNARY:
278
0
    case UCOL_IDENTICAL:
279
0
        options = noStrength | (value << STRENGTH_SHIFT);
280
0
        break;
281
0
    case UCOL_DEFAULT:
282
0
        options = noStrength | (defaultOptions & STRENGTH_MASK);
283
0
        break;
284
0
    default:
285
0
        errorCode = U_ILLEGAL_ARGUMENT_ERROR;
286
0
        break;
287
0
    }
288
0
}
289
290
void
291
CollationSettings::setFlag(int32_t bit, UColAttributeValue value,
292
596
                           int32_t defaultOptions, UErrorCode &errorCode) {
293
596
    if(U_FAILURE(errorCode)) { return; }
294
596
    switch(value) {
295
596
    case UCOL_ON:
296
596
        options |= bit;
297
596
        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
0
    default:
305
0
        errorCode = U_ILLEGAL_ARGUMENT_ERROR;
306
0
        break;
307
596
    }
308
596
}
309
310
void
311
CollationSettings::setCaseFirst(UColAttributeValue value,
312
0
                                int32_t defaultOptions, UErrorCode &errorCode) {
313
0
    if(U_FAILURE(errorCode)) { return; }
314
0
    int32_t noCaseFirst = options & ~CASE_FIRST_AND_UPPER_MASK;
315
0
    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
0
    case UCOL_UPPER_FIRST:
323
0
        options = noCaseFirst | CASE_FIRST_AND_UPPER_MASK;
324
0
        break;
325
0
    case UCOL_DEFAULT:
326
0
        options = noCaseFirst | (defaultOptions & CASE_FIRST_AND_UPPER_MASK);
327
0
        break;
328
0
    default:
329
0
        errorCode = U_ILLEGAL_ARGUMENT_ERROR;
330
0
        break;
331
0
    }
332
0
}
333
334
void
335
CollationSettings::setAlternateHandling(UColAttributeValue value,
336
124
                                        int32_t defaultOptions, UErrorCode &errorCode) {
337
124
    if(U_FAILURE(errorCode)) { return; }
338
124
    int32_t noAlternate = options & ~ALTERNATE_MASK;
339
124
    switch(value) {
340
0
    case UCOL_NON_IGNORABLE:
341
0
        options = noAlternate;
342
0
        break;
343
124
    case UCOL_SHIFTED:
344
124
        options = noAlternate | SHIFTED;
345
124
        break;
346
0
    case UCOL_DEFAULT:
347
0
        options = noAlternate | (defaultOptions & ALTERNATE_MASK);
348
0
        break;
349
0
    default:
350
0
        errorCode = U_ILLEGAL_ARGUMENT_ERROR;
351
0
        break;
352
124
    }
353
124
}
354
355
void
356
0
CollationSettings::setMaxVariable(int32_t value, int32_t defaultOptions, UErrorCode &errorCode) {
357
0
    if(U_FAILURE(errorCode)) { return; }
358
0
    int32_t noMax = options & ~MAX_VARIABLE_MASK;
359
0
    switch(value) {
360
0
    case MAX_VAR_SPACE:
361
0
    case MAX_VAR_PUNCT:
362
0
    case MAX_VAR_SYMBOL:
363
0
    case MAX_VAR_CURRENCY:
364
0
        options = noMax | (value << MAX_VARIABLE_SHIFT);
365
0
        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
0
    }
373
0
}
374
375
U_NAMESPACE_END
376
377
#endif  // !UCONFIG_NO_COLLATION