Coverage Report

Created: 2025-06-24 06:54

/src/icu/icu4c/source/i18n/collationrootelements.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-2014, International Business Machines
6
* Corporation and others.  All Rights Reserved.
7
*******************************************************************************
8
* collationrootelements.cpp
9
*
10
* created on: 2013mar05
11
* created by: Markus W. Scherer
12
*/
13
14
#include "unicode/utypes.h"
15
16
#if !UCONFIG_NO_COLLATION
17
18
#include "collation.h"
19
#include "collationrootelements.h"
20
#include "uassert.h"
21
22
U_NAMESPACE_BEGIN
23
24
int64_t
25
0
CollationRootElements::lastCEWithPrimaryBefore(uint32_t p) const {
26
0
    if(p == 0) { return 0; }
27
0
    U_ASSERT(p > elements[elements[IX_FIRST_PRIMARY_INDEX]]);
28
0
    int32_t index = findP(p);
29
0
    uint32_t q = elements[index];
30
0
    uint32_t secTer;
31
0
    if(p == (q & 0xffffff00)) {
32
        // p == elements[index] is a root primary. Find the CE before it.
33
        // We must not be in a primary range.
34
0
        U_ASSERT((q & PRIMARY_STEP_MASK) == 0);
35
0
        secTer = elements[index - 1];
36
0
        if((secTer & SEC_TER_DELTA_FLAG) == 0) {
37
            // Primary CE just before p.
38
0
            p = secTer & 0xffffff00;
39
0
            secTer = Collation::COMMON_SEC_AND_TER_CE;
40
0
        } else {
41
            // secTer = last secondary & tertiary for the previous primary
42
0
            index -= 2;
43
0
            for(;;) {
44
0
                p = elements[index];
45
0
                if((p & SEC_TER_DELTA_FLAG) == 0) {
46
0
                    p &= 0xffffff00;
47
0
                    break;
48
0
                }
49
0
                --index;
50
0
            }
51
0
        }
52
0
    } else {
53
        // p > elements[index] which is the previous primary.
54
        // Find the last secondary & tertiary weights for it.
55
0
        p = q & 0xffffff00;
56
0
        secTer = Collation::COMMON_SEC_AND_TER_CE;
57
0
        for(;;) {
58
0
            q = elements[++index];
59
0
            if((q & SEC_TER_DELTA_FLAG) == 0) {
60
                // We must not be in a primary range.
61
0
                U_ASSERT((q & PRIMARY_STEP_MASK) == 0);
62
0
                break;
63
0
            }
64
0
            secTer = q;
65
0
        }
66
0
    }
67
0
    return (static_cast<int64_t>(p) << 32) | (secTer & ~SEC_TER_DELTA_FLAG);
68
0
}
69
70
int64_t
71
4
CollationRootElements::firstCEWithPrimaryAtLeast(uint32_t p) const {
72
4
    if(p == 0) { return 0; }
73
4
    int32_t index = findP(p);
74
4
    if(p != (elements[index] & 0xffffff00)) {
75
4
        for(;;) {
76
4
            p = elements[++index];
77
4
            if((p & SEC_TER_DELTA_FLAG) == 0) {
78
                // First primary after p. We must not be in a primary range.
79
4
                U_ASSERT((p & PRIMARY_STEP_MASK) == 0);
80
4
                break;
81
4
            }
82
4
        }
83
4
    }
84
    // The code above guarantees that p has at most 3 bytes: (p & 0xff) == 0.
85
4
    return (static_cast<int64_t>(p) << 32) | Collation::COMMON_SEC_AND_TER_CE;
86
4
}
87
88
uint32_t
89
4.79k
CollationRootElements::getPrimaryBefore(uint32_t p, UBool isCompressible) const {
90
4.79k
    int32_t index = findPrimary(p);
91
4.79k
    int32_t step;
92
4.79k
    uint32_t q = elements[index];
93
4.79k
    if(p == (q & 0xffffff00)) {
94
        // Found p itself. Return the previous primary.
95
        // See if p is at the end of a previous range.
96
2.51k
        step = static_cast<int32_t>(q) & PRIMARY_STEP_MASK;
97
2.51k
        if(step == 0) {
98
            // p is not at the end of a range. Look for the previous primary.
99
4.02k
            do {
100
4.02k
                p = elements[--index];
101
4.02k
            } while((p & SEC_TER_DELTA_FLAG) != 0);
102
2.04k
            return p & 0xffffff00;
103
2.04k
        }
104
2.51k
    } else {
105
        // p is in a range, and not at the start.
106
2.28k
        uint32_t nextElement = elements[index + 1];
107
2.28k
        U_ASSERT(isEndOfPrimaryRange(nextElement));
108
2.28k
        step = static_cast<int32_t>(nextElement) & PRIMARY_STEP_MASK;
109
2.28k
    }
110
    // Return the previous range primary.
111
2.75k
    if((p & 0xffff) == 0) {
112
2.39k
        return Collation::decTwoBytePrimaryByOneStep(p, isCompressible, step);
113
2.39k
    } else {
114
360
        return Collation::decThreeBytePrimaryByOneStep(p, isCompressible, step);
115
360
    }
116
2.75k
}
117
118
uint32_t
119
769
CollationRootElements::getSecondaryBefore(uint32_t p, uint32_t s) const {
120
769
    int32_t index;
121
769
    uint32_t previousSec, sec;
122
769
    if(p == 0) {
123
0
        index = static_cast<int32_t>(elements[IX_FIRST_SECONDARY_INDEX]);
124
        // Gap at the beginning of the secondary CE range.
125
0
        previousSec = 0;
126
0
        sec = elements[index] >> 16;
127
769
    } else {
128
769
        index = findPrimary(p) + 1;
129
769
        previousSec = Collation::BEFORE_WEIGHT16;
130
769
        sec = getFirstSecTerForPrimary(index) >> 16;
131
769
    }
132
769
    U_ASSERT(s >= sec);
133
769
    while(s > sec) {
134
0
        previousSec = sec;
135
0
        U_ASSERT((elements[index] & SEC_TER_DELTA_FLAG) != 0);
136
0
        sec = elements[index++] >> 16;
137
0
    }
138
769
    U_ASSERT(sec == s);
139
769
    return previousSec;
140
769
}
141
142
uint32_t
143
220
CollationRootElements::getTertiaryBefore(uint32_t p, uint32_t s, uint32_t t) const {
144
220
    U_ASSERT((t & ~Collation::ONLY_TERTIARY_MASK) == 0);
145
220
    int32_t index;
146
220
    uint32_t previousTer, secTer;
147
220
    if(p == 0) {
148
8
        if(s == 0) {
149
0
            index = static_cast<int32_t>(elements[IX_FIRST_TERTIARY_INDEX]);
150
            // Gap at the beginning of the tertiary CE range.
151
0
            previousTer = 0;
152
8
        } else {
153
8
            index = static_cast<int32_t>(elements[IX_FIRST_SECONDARY_INDEX]);
154
8
            previousTer = Collation::BEFORE_WEIGHT16;
155
8
        }
156
8
        secTer = elements[index] & ~SEC_TER_DELTA_FLAG;
157
212
    } else {
158
212
        index = findPrimary(p) + 1;
159
212
        previousTer = Collation::BEFORE_WEIGHT16;
160
212
        secTer = getFirstSecTerForPrimary(index);
161
212
    }
162
220
    uint32_t st = (s << 16) | t;
163
620
    while(st > secTer) {
164
400
        if((secTer >> 16) == s) { previousTer = secTer; }
165
400
        U_ASSERT((elements[index] & SEC_TER_DELTA_FLAG) != 0);
166
400
        secTer = elements[index++] & ~SEC_TER_DELTA_FLAG;
167
400
    }
168
220
    U_ASSERT(secTer == st);
169
220
    return previousTer & 0xffff;
170
220
}
171
172
uint32_t
173
7.81k
CollationRootElements::getPrimaryAfter(uint32_t p, int32_t index, UBool isCompressible) const {
174
7.81k
    U_ASSERT(p == (elements[index] & 0xffffff00) || isEndOfPrimaryRange(elements[index + 1]));
175
7.81k
    uint32_t q = elements[++index];
176
7.81k
    int32_t step;
177
7.81k
    if ((q & SEC_TER_DELTA_FLAG) == 0 && (step = static_cast<int32_t>(q) & PRIMARY_STEP_MASK) != 0) {
178
        // Return the next primary in this range.
179
4.12k
        if((p & 0xffff) == 0) {
180
2.39k
            return Collation::incTwoBytePrimaryByOffset(p, isCompressible, step);
181
2.39k
        } else {
182
1.73k
            return Collation::incThreeBytePrimaryByOffset(p, isCompressible, step);
183
1.73k
        }
184
4.12k
    } else {
185
        // Return the next primary in the list.
186
20.4k
        while((q & SEC_TER_DELTA_FLAG) != 0) {
187
16.7k
            q = elements[++index];
188
16.7k
        }
189
3.69k
        U_ASSERT((q & PRIMARY_STEP_MASK) == 0);
190
3.69k
        return q;
191
3.69k
    }
192
7.81k
}
193
194
uint32_t
195
3.98k
CollationRootElements::getSecondaryAfter(int32_t index, uint32_t s) const {
196
3.98k
    uint32_t secTer;
197
3.98k
    uint32_t secLimit;
198
3.98k
    if(index == 0) {
199
        // primary = 0
200
969
        U_ASSERT(s != 0);
201
969
        index = static_cast<int32_t>(elements[IX_FIRST_SECONDARY_INDEX]);
202
969
        secTer = elements[index];
203
        // Gap at the end of the secondary CE range.
204
969
        secLimit = 0x10000;
205
3.01k
    } else {
206
3.01k
        U_ASSERT(index >= (int32_t)elements[IX_FIRST_PRIMARY_INDEX]);
207
3.01k
        secTer = getFirstSecTerForPrimary(index + 1);
208
        // If this is an explicit sec/ter unit, then it will be read once more.
209
        // Gap for secondaries of primary CEs.
210
3.01k
        secLimit = getSecondaryBoundary();
211
3.01k
    }
212
131k
    for(;;) {
213
131k
        uint32_t sec = secTer >> 16;
214
131k
        if(sec > s) { return sec; }
215
130k
        secTer = elements[++index];
216
130k
        if((secTer & SEC_TER_DELTA_FLAG) == 0) { return secLimit; }
217
130k
    }
218
3.98k
}
219
220
uint32_t
221
2.08k
CollationRootElements::getTertiaryAfter(int32_t index, uint32_t s, uint32_t t) const {
222
2.08k
    uint32_t secTer;
223
2.08k
    uint32_t terLimit;
224
2.08k
    if(index == 0) {
225
        // primary = 0
226
353
        if(s == 0) {
227
72
            U_ASSERT(t != 0);
228
72
            index = static_cast<int32_t>(elements[IX_FIRST_TERTIARY_INDEX]);
229
            // Gap at the end of the tertiary CE range.
230
72
            terLimit = 0x4000;
231
281
        } else {
232
281
            index = static_cast<int32_t>(elements[IX_FIRST_SECONDARY_INDEX]);
233
            // Gap for tertiaries of primary/secondary CEs.
234
281
            terLimit = getTertiaryBoundary();
235
281
        }
236
353
        secTer = elements[index] & ~SEC_TER_DELTA_FLAG;
237
1.73k
    } else {
238
1.73k
        U_ASSERT(index >= (int32_t)elements[IX_FIRST_PRIMARY_INDEX]);
239
1.73k
        secTer = getFirstSecTerForPrimary(index + 1);
240
        // If this is an explicit sec/ter unit, then it will be read once more.
241
1.73k
        terLimit = getTertiaryBoundary();
242
1.73k
    }
243
2.08k
    uint32_t st = (s << 16) | t;
244
19.7k
    for(;;) {
245
19.7k
        if(secTer > st) {
246
994
            U_ASSERT((secTer >> 16) == s);
247
994
            return secTer & 0xffff;
248
994
        }
249
18.7k
        secTer = elements[++index];
250
        // No tertiary greater than t for this primary+secondary.
251
18.7k
        if((secTer & SEC_TER_DELTA_FLAG) == 0 || (secTer >> 16) > s) { return terLimit; }
252
17.6k
        secTer &= ~SEC_TER_DELTA_FLAG;
253
17.6k
    }
254
2.08k
}
255
256
uint32_t
257
5.73k
CollationRootElements::getFirstSecTerForPrimary(int32_t index) const {
258
5.73k
    uint32_t secTer = elements[index];
259
5.73k
    if((secTer & SEC_TER_DELTA_FLAG) == 0) {
260
        // No sec/ter delta.
261
2.71k
        return Collation::COMMON_SEC_AND_TER_CE;
262
2.71k
    }
263
3.01k
    secTer &= ~SEC_TER_DELTA_FLAG;
264
3.01k
    if(secTer > Collation::COMMON_SEC_AND_TER_CE) {
265
        // Implied sec/ter.
266
2.75k
        return Collation::COMMON_SEC_AND_TER_CE;
267
2.75k
    }
268
    // Explicit sec/ter below common/common.
269
260
    return secTer;
270
3.01k
}
271
272
int32_t
273
19.5k
CollationRootElements::findPrimary(uint32_t p) const {
274
    // Requirement: p must occur as a root primary.
275
19.5k
    U_ASSERT((p & 0xff) == 0);  // at most a 3-byte primary
276
19.5k
    int32_t index = findP(p);
277
    // If p is in a range, then we just assume that p is an actual primary in this range.
278
    // (Too cumbersome/expensive to check.)
279
    // Otherwise, it must be an exact match.
280
19.5k
    U_ASSERT(isEndOfPrimaryRange(elements[index + 1]) || p == (elements[index] & 0xffffff00));
281
19.5k
    return index;
282
19.5k
}
283
284
int32_t
285
19.5k
CollationRootElements::findP(uint32_t p) const {
286
    // p need not occur as a root primary.
287
    // For example, it might be a reordering group boundary.
288
19.5k
    U_ASSERT((p >> 24) != Collation::UNASSIGNED_IMPLICIT_BYTE);
289
    // modified binary search
290
19.5k
    int32_t start = static_cast<int32_t>(elements[IX_FIRST_PRIMARY_INDEX]);
291
19.5k
    U_ASSERT(p >= elements[start]);
292
19.5k
    int32_t limit = length - 1;
293
19.5k
    U_ASSERT(elements[limit] >= PRIMARY_SENTINEL);
294
19.5k
    U_ASSERT(p < elements[limit]);
295
246k
    while((start + 1) < limit) {
296
        // Invariant: elements[start] and elements[limit] are primaries,
297
        // and elements[start]<=p<=elements[limit].
298
235k
        int32_t i = (start + limit) / 2;
299
235k
        uint32_t q = elements[i];
300
235k
        if((q & SEC_TER_DELTA_FLAG) != 0) {
301
            // Find the next primary.
302
93.6k
            int32_t j = i + 1;
303
303k
            for(;;) {
304
303k
                if(j == limit) { break; }
305
289k
                q = elements[j];
306
289k
                if((q & SEC_TER_DELTA_FLAG) == 0) {
307
80.0k
                    i = j;
308
80.0k
                    break;
309
80.0k
                }
310
209k
                ++j;
311
209k
            }
312
93.6k
            if((q & SEC_TER_DELTA_FLAG) != 0) {
313
                // Find the preceding primary.
314
13.6k
                j = i - 1;
315
51.2k
                for(;;) {
316
51.2k
                    if(j == start) { break; }
317
42.2k
                    q = elements[j];
318
42.2k
                    if((q & SEC_TER_DELTA_FLAG) == 0) {
319
4.61k
                        i = j;
320
4.61k
                        break;
321
4.61k
                    }
322
37.5k
                    --j;
323
37.5k
                }
324
13.6k
                if((q & SEC_TER_DELTA_FLAG) != 0) {
325
                    // No primary between start and limit.
326
9.00k
                    break;
327
9.00k
                }
328
13.6k
            }
329
93.6k
        }
330
226k
        if(p < (q & 0xffffff00)) {  // Reset the "step" bits of a range end primary.
331
120k
            limit = i;
332
120k
        } else {
333
106k
            start = i;
334
106k
        }
335
226k
    }
336
19.5k
    return start;
337
19.5k
}
338
339
U_NAMESPACE_END
340
341
#endif  // !UCONFIG_NO_COLLATION