Coverage Report

Created: 2026-10-31 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/icu/icu4c/source/i18n/collationsets.cpp
Line
Count
Source
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
* collationsets.cpp
9
*
10
* created on: 2013feb09
11
* created by: Markus W. Scherer
12
*/
13
14
#include "unicode/utypes.h"
15
16
#if !UCONFIG_NO_COLLATION
17
18
#include "unicode/ucharstrie.h"
19
#include "unicode/uniset.h"
20
#include "unicode/unistr.h"
21
#include "unicode/ustringtrie.h"
22
#include "collation.h"
23
#include "collationdata.h"
24
#include "collationsets.h"
25
#include "normalizer2impl.h"
26
#include "uassert.h"
27
#include "utf16collationiterator.h"
28
#include "utrie2.h"
29
30
U_NAMESPACE_BEGIN
31
32
U_CDECL_BEGIN
33
34
static UBool U_CALLCONV
35
3.86M
enumTailoredRange(const void *context, UChar32 start, UChar32 end, uint32_t ce32) {
36
3.86M
    if(ce32 == Collation::FALLBACK_CE32) {
37
122k
        return true;  // fallback to base, not tailored
38
122k
    }
39
3.73M
    TailoredSet *ts = (TailoredSet *)context;
40
3.73M
    return ts->handleCE32(start, end, ce32);
41
3.86M
}
42
43
U_CDECL_END
44
45
void
46
3.76k
TailoredSet::forData(const CollationData *d, UErrorCode &ec) {
47
3.76k
    if(U_FAILURE(ec)) { return; }
48
3.76k
    errorCode = ec;  // Preserve info & warning codes.
49
3.76k
    data = d;
50
3.76k
    baseData = d->base;
51
3.76k
    U_ASSERT(baseData != nullptr);
52
3.76k
    utrie2_enum(data->trie, nullptr, enumTailoredRange, this);
53
3.76k
    ec = errorCode;
54
3.76k
}
55
56
UBool
57
3.73M
TailoredSet::handleCE32(UChar32 start, UChar32 end, uint32_t ce32) {
58
3.73M
    U_ASSERT(ce32 != Collation::FALLBACK_CE32);
59
3.73M
    if(Collation::isSpecialCE32(ce32)) {
60
3.21M
        ce32 = data->getIndirectCE32(ce32);
61
3.21M
        if(ce32 == Collation::FALLBACK_CE32) {
62
0
            return U_SUCCESS(errorCode);
63
0
        }
64
3.21M
    }
65
46.6M
    do {
66
46.6M
        uint32_t baseCE32 = baseData->getFinalCE32(baseData->getCE32(start));
67
        // Do not just continue if ce32 == baseCE32 because
68
        // contractions and expansions in different data objects
69
        // normally differ even if they have the same data offsets.
70
46.6M
        if(Collation::isSelfContainedCE32(ce32) && Collation::isSelfContainedCE32(baseCE32)) {
71
            // fastpath
72
2.15M
            if(ce32 != baseCE32) {
73
529k
                tailored->add(start);
74
529k
            }
75
44.4M
        } else {
76
44.4M
            compare(start, ce32, baseCE32);
77
44.4M
        }
78
46.6M
    } while(++start <= end);
79
3.73M
    return U_SUCCESS(errorCode);
80
3.73M
}
81
82
void
83
44.5M
TailoredSet::compare(UChar32 c, uint32_t ce32, uint32_t baseCE32) {
84
44.5M
    if(Collation::isPrefixCE32(ce32)) {
85
2.01k
        const char16_t *p = data->contexts + Collation::indexFromCE32(ce32);
86
2.01k
        ce32 = data->getFinalCE32(CollationData::readCE32(p));
87
2.01k
        if(Collation::isPrefixCE32(baseCE32)) {
88
379
            const char16_t *q = baseData->contexts + Collation::indexFromCE32(baseCE32);
89
379
            baseCE32 = baseData->getFinalCE32(CollationData::readCE32(q));
90
379
            comparePrefixes(c, p + 2, q + 2);
91
1.63k
        } else {
92
1.63k
            addPrefixes(data, c, p + 2);
93
1.63k
        }
94
44.5M
    } else if(Collation::isPrefixCE32(baseCE32)) {
95
0
        const char16_t *q = baseData->contexts + Collation::indexFromCE32(baseCE32);
96
0
        baseCE32 = baseData->getFinalCE32(CollationData::readCE32(q));
97
0
        addPrefixes(baseData, c, q + 2);
98
0
    }
99
100
44.5M
    if(Collation::isContractionCE32(ce32)) {
101
23.1k
        const char16_t *p = data->contexts + Collation::indexFromCE32(ce32);
102
23.1k
        if((ce32 & Collation::CONTRACT_SINGLE_CP_NO_MATCH) != 0) {
103
0
            ce32 = Collation::NO_CE32;
104
23.1k
        } else {
105
23.1k
            ce32 = data->getFinalCE32(CollationData::readCE32(p));
106
23.1k
        }
107
23.1k
        if(Collation::isContractionCE32(baseCE32)) {
108
3.06k
            const char16_t *q = baseData->contexts + Collation::indexFromCE32(baseCE32);
109
3.06k
            if((baseCE32 & Collation::CONTRACT_SINGLE_CP_NO_MATCH) != 0) {
110
0
                baseCE32 = Collation::NO_CE32;
111
3.06k
            } else {
112
3.06k
                baseCE32 = baseData->getFinalCE32(CollationData::readCE32(q));
113
3.06k
            }
114
3.06k
            compareContractions(c, p + 2, q + 2);
115
20.0k
        } else {
116
20.0k
            addContractions(c, p + 2);
117
20.0k
        }
118
44.5M
    } else if(Collation::isContractionCE32(baseCE32)) {
119
41
        const char16_t *q = baseData->contexts + Collation::indexFromCE32(baseCE32);
120
41
        baseCE32 = baseData->getFinalCE32(CollationData::readCE32(q));
121
41
        addContractions(c, q + 2);
122
41
    }
123
124
44.5M
    int32_t tag;
125
44.5M
    if(Collation::isSpecialCE32(ce32)) {
126
44.4M
        tag = Collation::tagFromCE32(ce32);
127
44.4M
        U_ASSERT(tag != Collation::PREFIX_TAG);
128
44.4M
        U_ASSERT(tag != Collation::CONTRACTION_TAG);
129
        // Currently, the tailoring data builder does not write offset tags.
130
        // They might be useful for saving space,
131
        // but they would complicate the builder,
132
        // and in tailorings we assume that performance of tailored characters is more important.
133
44.4M
        U_ASSERT(tag != Collation::OFFSET_TAG);
134
44.4M
    } else {
135
76.1k
        tag = -1;
136
76.1k
    }
137
44.5M
    int32_t baseTag;
138
44.5M
    if(Collation::isSpecialCE32(baseCE32)) {
139
44.4M
        baseTag = Collation::tagFromCE32(baseCE32);
140
44.4M
        U_ASSERT(baseTag != Collation::PREFIX_TAG);
141
44.4M
        U_ASSERT(baseTag != Collation::CONTRACTION_TAG);
142
44.4M
    } else {
143
64.5k
        baseTag = -1;
144
64.5k
    }
145
146
    // Non-contextual mappings, expansions, etc.
147
44.5M
    if(baseTag == Collation::OFFSET_TAG) {
148
        // We might be comparing a tailoring CE which is a copy of
149
        // a base offset-tag CE, via the [optimize [set]] syntax
150
        // or when a single-character mapping was copied for tailored contractions.
151
        // Offset tags always result in long-primary CEs,
152
        // with common secondary/tertiary weights.
153
1.22M
        if(!Collation::isLongPrimaryCE32(ce32)) {
154
367k
            add(c);
155
367k
            return;
156
367k
        }
157
853k
        int64_t dataCE = baseData->ces[Collation::indexFromCE32(baseCE32)];
158
853k
        uint32_t p = Collation::getThreeBytePrimaryForOffsetData(c, dataCE);
159
853k
        if(Collation::primaryFromLongPrimaryCE32(ce32) != p) {
160
228k
            add(c);
161
228k
            return;
162
228k
        }
163
853k
    }
164
165
43.9M
    if(tag != baseTag) {
166
1.73M
        add(c);
167
1.73M
        return;
168
1.73M
    }
169
170
42.2M
    if(tag == Collation::EXPANSION32_TAG) {
171
148k
        const uint32_t *ce32s = data->ce32s + Collation::indexFromCE32(ce32);
172
148k
        int32_t length = Collation::lengthFromCE32(ce32);
173
174
148k
        const uint32_t *baseCE32s = baseData->ce32s + Collation::indexFromCE32(baseCE32);
175
148k
        int32_t baseLength = Collation::lengthFromCE32(baseCE32);
176
177
148k
        if(length != baseLength) {
178
17.2k
            add(c);
179
17.2k
            return;
180
17.2k
        }
181
342k
        for(int32_t i = 0; i < length; ++i) {
182
266k
            if(ce32s[i] != baseCE32s[i]) {
183
54.9k
                add(c);
184
54.9k
                break;
185
54.9k
            }
186
266k
        }
187
42.0M
    } else if(tag == Collation::EXPANSION_TAG) {
188
23.1k
        const int64_t *ces = data->ces + Collation::indexFromCE32(ce32);
189
23.1k
        int32_t length = Collation::lengthFromCE32(ce32);
190
191
23.1k
        const int64_t *baseCEs = baseData->ces + Collation::indexFromCE32(baseCE32);
192
23.1k
        int32_t baseLength = Collation::lengthFromCE32(baseCE32);
193
194
23.1k
        if(length != baseLength) {
195
1.47k
            add(c);
196
1.47k
            return;
197
1.47k
        }
198
48.5k
        for(int32_t i = 0; i < length; ++i) {
199
28.1k
            if(ces[i] != baseCEs[i]) {
200
1.29k
                add(c);
201
1.29k
                break;
202
1.29k
            }
203
28.1k
        }
204
42.0M
    } else if(tag == Collation::HANGUL_TAG) {
205
42.0M
        char16_t jamos[3];
206
42.0M
        int32_t length = Hangul::decompose(c, jamos);
207
42.0M
        if(tailored->contains(jamos[0]) || tailored->contains(jamos[1]) ||
208
41.4M
                (length == 3 && tailored->contains(jamos[2]))) {
209
608k
            add(c);
210
608k
        }
211
42.0M
    } else if(ce32 != baseCE32) {
212
918
        add(c);
213
918
    }
214
42.2M
}
215
216
void
217
379
TailoredSet::comparePrefixes(UChar32 c, const char16_t *p, const char16_t *q) {
218
    // Parallel iteration over prefixes of both tables.
219
379
    UCharsTrie::Iterator prefixes(p, 0, errorCode);
220
379
    UCharsTrie::Iterator basePrefixes(q, 0, errorCode);
221
379
    const UnicodeString *tp = nullptr;  // Tailoring prefix.
222
379
    const UnicodeString *bp = nullptr;  // Base prefix.
223
    // Use a string with a U+FFFF as the limit sentinel.
224
    // U+FFFF is untailorable and will not occur in prefixes.
225
379
    UnicodeString none(static_cast<char16_t>(0xffff));
226
3.08k
    for(;;) {
227
3.08k
        if(tp == nullptr) {
228
3.08k
            if(prefixes.next(errorCode)) {
229
2.70k
                tp = &prefixes.getString();
230
2.70k
            } else {
231
379
                tp = &none;
232
379
            }
233
3.08k
        }
234
3.08k
        if(bp == nullptr) {
235
1.13k
            if(basePrefixes.next(errorCode)) {
236
758
                bp = &basePrefixes.getString();
237
758
            } else {
238
379
                bp = &none;
239
379
            }
240
1.13k
        }
241
3.08k
        if(tp == &none && bp == &none) { break; }
242
2.70k
        int32_t cmp = tp->compare(*bp);
243
2.70k
        if(cmp < 0) {
244
            // tp occurs in the tailoring but not in the base.
245
1.94k
            addPrefix(data, *tp, c, static_cast<uint32_t>(prefixes.getValue()));
246
1.94k
            tp = nullptr;
247
1.94k
        } else if(cmp > 0) {
248
            // bp occurs in the base but not in the tailoring.
249
0
            addPrefix(baseData, *bp, c, static_cast<uint32_t>(basePrefixes.getValue()));
250
0
            bp = nullptr;
251
758
        } else {
252
758
            setPrefix(*tp);
253
758
            compare(c, static_cast<uint32_t>(prefixes.getValue()), static_cast<uint32_t>(basePrefixes.getValue()));
254
758
            resetPrefix();
255
758
            tp = nullptr;
256
758
            bp = nullptr;
257
758
        }
258
2.70k
    }
259
379
}
260
261
void
262
3.06k
TailoredSet::compareContractions(UChar32 c, const char16_t *p, const char16_t *q) {
263
    // Parallel iteration over suffixes of both tables.
264
3.06k
    UCharsTrie::Iterator suffixes(p, 0, errorCode);
265
3.06k
    UCharsTrie::Iterator baseSuffixes(q, 0, errorCode);
266
3.06k
    const UnicodeString *ts = nullptr;  // Tailoring suffix.
267
3.06k
    const UnicodeString *bs = nullptr;  // Base suffix.
268
    // Use a string with two U+FFFF as the limit sentinel.
269
    // U+FFFF is untailorable and will not occur in contractions except maybe
270
    // as a single suffix character for a root-collator boundary contraction.
271
3.06k
    UnicodeString none(static_cast<char16_t>(0xffff));
272
3.06k
    none.append(static_cast<char16_t>(0xffff));
273
56.5k
    for(;;) {
274
56.5k
        if(ts == nullptr) {
275
56.5k
            if(suffixes.next(errorCode)) {
276
53.5k
                ts = &suffixes.getString();
277
53.5k
            } else {
278
3.06k
                ts = &none;
279
3.06k
            }
280
56.5k
        }
281
56.5k
        if(bs == nullptr) {
282
41.4k
            if(baseSuffixes.next(errorCode)) {
283
38.3k
                bs = &baseSuffixes.getString();
284
38.3k
            } else {
285
3.06k
                bs = &none;
286
3.06k
            }
287
41.4k
        }
288
56.5k
        if(ts == &none && bs == &none) { break; }
289
53.5k
        int32_t cmp = ts->compare(*bs);
290
53.5k
        if(cmp < 0) {
291
            // ts occurs in the tailoring but not in the base.
292
15.1k
            addSuffix(c, *ts);
293
15.1k
            ts = nullptr;
294
38.3k
        } else if(cmp > 0) {
295
            // bs occurs in the base but not in the tailoring.
296
3
            addSuffix(c, *bs);
297
3
            bs = nullptr;
298
38.3k
        } else {
299
38.3k
            suffix = ts;
300
38.3k
            compare(c, static_cast<uint32_t>(suffixes.getValue()), static_cast<uint32_t>(baseSuffixes.getValue()));
301
38.3k
            suffix = nullptr;
302
38.3k
            ts = nullptr;
303
38.3k
            bs = nullptr;
304
38.3k
        }
305
53.5k
    }
306
3.06k
}
307
308
void
309
1.63k
TailoredSet::addPrefixes(const CollationData *d, UChar32 c, const char16_t *p) {
310
1.63k
    UCharsTrie::Iterator prefixes(p, 0, errorCode);
311
29.3k
    while(prefixes.next(errorCode)) {
312
27.7k
        addPrefix(d, prefixes.getString(), c, static_cast<uint32_t>(prefixes.getValue()));
313
27.7k
    }
314
1.63k
}
315
316
void
317
29.6k
TailoredSet::addPrefix(const CollationData *d, const UnicodeString &pfx, UChar32 c, uint32_t ce32) {
318
29.6k
    setPrefix(pfx);
319
29.6k
    ce32 = d->getFinalCE32(ce32);
320
29.6k
    if(Collation::isContractionCE32(ce32)) {
321
5.61k
        const char16_t *p = d->contexts + Collation::indexFromCE32(ce32);
322
5.61k
        addContractions(c, p + 2);
323
5.61k
    }
324
29.6k
    tailored->add(UnicodeString(unreversedPrefix).append(c));
325
29.6k
    resetPrefix();
326
29.6k
}
327
328
void
329
25.7k
TailoredSet::addContractions(UChar32 c, const char16_t *p) {
330
25.7k
    UCharsTrie::Iterator suffixes(p, 0, errorCode);
331
227k
    while(suffixes.next(errorCode)) {
332
201k
        addSuffix(c, suffixes.getString());
333
201k
    }
334
25.7k
}
335
336
void
337
216k
TailoredSet::addSuffix(UChar32 c, const UnicodeString &sfx) {
338
216k
    tailored->add(UnicodeString(unreversedPrefix).append(c).append(sfx));
339
216k
}
340
341
void
342
3.01M
TailoredSet::add(UChar32 c) {
343
3.01M
    if(unreversedPrefix.isEmpty() && suffix == nullptr) {
344
3.01M
        tailored->add(c);
345
3.01M
    } else {
346
837
        UnicodeString s(unreversedPrefix);
347
837
        s.append(c);
348
837
        if(suffix != nullptr) {
349
837
            s.append(*suffix);
350
837
        }
351
837
        tailored->add(s);
352
837
    }
353
3.01M
}
354
355
0
ContractionsAndExpansions::CESink::~CESink() {}
356
357
U_CDECL_BEGIN
358
359
static UBool U_CALLCONV
360
0
enumCnERange(const void *context, UChar32 start, UChar32 end, uint32_t ce32) {
361
0
    ContractionsAndExpansions *cne = (ContractionsAndExpansions *)context;
362
0
    if(cne->checkTailored == 0) {
363
        // There is no tailoring.
364
        // No need to collect nor check the tailored set.
365
0
    } else if(cne->checkTailored < 0) {
366
        // Collect the set of code points with mappings in the tailoring data.
367
0
        if(ce32 == Collation::FALLBACK_CE32) {
368
0
            return true;  // fallback to base, not tailored
369
0
        } else {
370
0
            cne->tailored.add(start, end);
371
0
        }
372
        // checkTailored > 0: Exclude tailored ranges from the base data enumeration.
373
0
    } else if(start == end) {
374
0
        if(cne->tailored.contains(start)) {
375
0
            return true;
376
0
        }
377
0
    } else if(cne->tailored.containsSome(start, end)) {
378
0
        cne->ranges.set(start, end).removeAll(cne->tailored);
379
0
        int32_t count = cne->ranges.getRangeCount();
380
0
        for(int32_t i = 0; i < count; ++i) {
381
0
            cne->handleCE32(cne->ranges.getRangeStart(i), cne->ranges.getRangeEnd(i), ce32);
382
0
        }
383
0
        return U_SUCCESS(cne->errorCode);
384
0
    }
385
0
    cne->handleCE32(start, end, ce32);
386
0
    return U_SUCCESS(cne->errorCode);
387
0
}
388
389
U_CDECL_END
390
391
void
392
0
ContractionsAndExpansions::forData(const CollationData *d, UErrorCode &ec) {
393
0
    if(U_FAILURE(ec)) { return; }
394
0
    errorCode = ec;  // Preserve info & warning codes.
395
    // Add all from the data, can be tailoring or base.
396
0
    if(d->base != nullptr) {
397
0
        checkTailored = -1;
398
0
    }
399
0
    data = d;
400
0
    utrie2_enum(data->trie, nullptr, enumCnERange, this);
401
0
    if(d->base == nullptr || U_FAILURE(errorCode)) {
402
0
        ec = errorCode;
403
0
        return;
404
0
    }
405
    // Add all from the base data but only for un-tailored code points.
406
0
    tailored.freeze();
407
0
    checkTailored = 1;
408
0
    data = d->base;
409
0
    utrie2_enum(data->trie, nullptr, enumCnERange, this);
410
0
    ec = errorCode;
411
0
}
412
413
void
414
0
ContractionsAndExpansions::forCodePoint(const CollationData *d, UChar32 c, UErrorCode &ec) {
415
0
    if(U_FAILURE(ec)) { return; }
416
0
    errorCode = ec;  // Preserve info & warning codes.
417
0
    uint32_t ce32 = d->getCE32(c);
418
0
    if(ce32 == Collation::FALLBACK_CE32) {
419
0
        d = d->base;
420
0
        ce32 = d->getCE32(c);
421
0
    }
422
0
    data = d;
423
0
    handleCE32(c, c, ce32);
424
0
    ec = errorCode;
425
0
}
426
427
void
428
0
ContractionsAndExpansions::handleCE32(UChar32 start, UChar32 end, uint32_t ce32) {
429
0
    for(;;) {
430
0
        if((ce32 & 0xff) < Collation::SPECIAL_CE32_LOW_BYTE) {
431
            // !isSpecialCE32()
432
0
            if(sink != nullptr) {
433
0
                sink->handleCE(Collation::ceFromSimpleCE32(ce32));
434
0
            }
435
0
            return;
436
0
        }
437
0
        switch(Collation::tagFromCE32(ce32)) {
438
0
        case Collation::FALLBACK_TAG:
439
0
            return;
440
0
        case Collation::RESERVED_TAG_3:
441
0
        case Collation::BUILDER_DATA_TAG:
442
0
        case Collation::LEAD_SURROGATE_TAG:
443
0
            if(U_SUCCESS(errorCode)) { errorCode = U_INTERNAL_PROGRAM_ERROR; }
444
0
            return;
445
0
        case Collation::LONG_PRIMARY_TAG:
446
0
            if(sink != nullptr) {
447
0
                sink->handleCE(Collation::ceFromLongPrimaryCE32(ce32));
448
0
            }
449
0
            return;
450
0
        case Collation::LONG_SECONDARY_TAG:
451
0
            if(sink != nullptr) {
452
0
                sink->handleCE(Collation::ceFromLongSecondaryCE32(ce32));
453
0
            }
454
0
            return;
455
0
        case Collation::LATIN_EXPANSION_TAG:
456
0
            if(sink != nullptr) {
457
0
                ces[0] = Collation::latinCE0FromCE32(ce32);
458
0
                ces[1] = Collation::latinCE1FromCE32(ce32);
459
0
                sink->handleExpansion(ces, 2);
460
0
            }
461
            // Optimization: If we have a prefix,
462
            // then the relevant strings have been added already.
463
0
            if(unreversedPrefix.isEmpty()) {
464
0
                addExpansions(start, end);
465
0
            }
466
0
            return;
467
0
        case Collation::EXPANSION32_TAG:
468
0
            if(sink != nullptr) {
469
0
                const uint32_t *ce32s = data->ce32s + Collation::indexFromCE32(ce32);
470
0
                int32_t length = Collation::lengthFromCE32(ce32);
471
0
                for(int32_t i = 0; i < length; ++i) {
472
0
                    ces[i] = Collation::ceFromCE32(*ce32s++);
473
0
                }
474
0
                sink->handleExpansion(ces, length);
475
0
            }
476
            // Optimization: If we have a prefix,
477
            // then the relevant strings have been added already.
478
0
            if(unreversedPrefix.isEmpty()) {
479
0
                addExpansions(start, end);
480
0
            }
481
0
            return;
482
0
        case Collation::EXPANSION_TAG:
483
0
            if(sink != nullptr) {
484
0
                int32_t length = Collation::lengthFromCE32(ce32);
485
0
                sink->handleExpansion(data->ces + Collation::indexFromCE32(ce32), length);
486
0
            }
487
            // Optimization: If we have a prefix,
488
            // then the relevant strings have been added already.
489
0
            if(unreversedPrefix.isEmpty()) {
490
0
                addExpansions(start, end);
491
0
            }
492
0
            return;
493
0
        case Collation::PREFIX_TAG:
494
0
            handlePrefixes(start, end, ce32);
495
0
            return;
496
0
        case Collation::CONTRACTION_TAG:
497
0
            handleContractions(start, end, ce32);
498
0
            return;
499
0
        case Collation::DIGIT_TAG:
500
            // Fetch the non-numeric-collation CE32 and continue.
501
0
            ce32 = data->ce32s[Collation::indexFromCE32(ce32)];
502
0
            break;
503
0
        case Collation::U0000_TAG:
504
0
            U_ASSERT(start == 0 && end == 0);
505
            // Fetch the normal ce32 for U+0000 and continue.
506
0
            ce32 = data->ce32s[0];
507
0
            break;
508
0
        case Collation::HANGUL_TAG:
509
0
            if(sink != nullptr) {
510
                // TODO: This should be optimized,
511
                // especially if [start..end] is the complete Hangul range. (assert that)
512
0
                UTF16CollationIterator iter(data, false, nullptr, nullptr, nullptr);
513
0
                char16_t hangul[1] = { 0 };
514
0
                for(UChar32 c = start; c <= end; ++c) {
515
0
                    hangul[0] = static_cast<char16_t>(c);
516
0
                    iter.setText(hangul, hangul + 1);
517
0
                    int32_t length = iter.fetchCEs(errorCode);
518
0
                    if(U_FAILURE(errorCode)) { return; }
519
                    // Ignore the terminating non-CE.
520
0
                    U_ASSERT(length >= 2 && iter.getCE(length - 1) == Collation::NO_CE);
521
0
                    sink->handleExpansion(iter.getCEs(), length - 1);
522
0
                }
523
0
            }
524
            // Optimization: If we have a prefix,
525
            // then the relevant strings have been added already.
526
0
            if(unreversedPrefix.isEmpty()) {
527
0
                addExpansions(start, end);
528
0
            }
529
0
            return;
530
0
        case Collation::OFFSET_TAG:
531
            // Currently no need to send offset CEs to the sink.
532
0
            return;
533
0
        case Collation::IMPLICIT_TAG:
534
            // Currently no need to send implicit CEs to the sink.
535
0
            return;
536
0
        }
537
0
    }
538
0
}
539
540
void
541
ContractionsAndExpansions::handlePrefixes(
542
0
        UChar32 start, UChar32 end, uint32_t ce32) {
543
0
    const char16_t *p = data->contexts + Collation::indexFromCE32(ce32);
544
0
    ce32 = CollationData::readCE32(p);  // Default if no prefix match.
545
0
    handleCE32(start, end, ce32);
546
0
    if(!addPrefixes) { return; }
547
0
    UCharsTrie::Iterator prefixes(p + 2, 0, errorCode);
548
0
    while(prefixes.next(errorCode)) {
549
0
        setPrefix(prefixes.getString());
550
        // Prefix/pre-context mappings are special kinds of contractions
551
        // that always yield expansions.
552
0
        addStrings(start, end, contractions);
553
0
        addStrings(start, end, expansions);
554
0
        handleCE32(start, end, static_cast<uint32_t>(prefixes.getValue()));
555
0
    }
556
0
    resetPrefix();
557
0
}
558
559
void
560
ContractionsAndExpansions::handleContractions(
561
0
        UChar32 start, UChar32 end, uint32_t ce32) {
562
0
    const char16_t *p = data->contexts + Collation::indexFromCE32(ce32);
563
0
    if((ce32 & Collation::CONTRACT_SINGLE_CP_NO_MATCH) != 0) {
564
        // No match on the single code point.
565
        // We are underneath a prefix, and the default mapping is just
566
        // a fallback to the mappings for a shorter prefix.
567
0
        U_ASSERT(!unreversedPrefix.isEmpty());
568
0
    } else {
569
0
        ce32 = CollationData::readCE32(p);  // Default if no suffix match.
570
0
        U_ASSERT(!Collation::isContractionCE32(ce32));
571
0
        handleCE32(start, end, ce32);
572
0
    }
573
0
    UCharsTrie::Iterator suffixes(p + 2, 0, errorCode);
574
0
    while(suffixes.next(errorCode)) {
575
0
        suffix = &suffixes.getString();
576
0
        addStrings(start, end, contractions);
577
0
        if(!unreversedPrefix.isEmpty()) {
578
0
            addStrings(start, end, expansions);
579
0
        }
580
0
        handleCE32(start, end, static_cast<uint32_t>(suffixes.getValue()));
581
0
    }
582
0
    suffix = nullptr;
583
0
}
584
585
void
586
0
ContractionsAndExpansions::addExpansions(UChar32 start, UChar32 end) {
587
0
    if(unreversedPrefix.isEmpty() && suffix == nullptr) {
588
0
        if(expansions != nullptr) {
589
0
            expansions->add(start, end);
590
0
        }
591
0
    } else {
592
0
        addStrings(start, end, expansions);
593
0
    }
594
0
}
595
596
void
597
0
ContractionsAndExpansions::addStrings(UChar32 start, UChar32 end, UnicodeSet *set) {
598
0
    if(set == nullptr) { return; }
599
0
    UnicodeString s(unreversedPrefix);
600
0
    do {
601
0
        s.append(start);
602
0
        if(suffix != nullptr) {
603
0
            s.append(*suffix);
604
0
        }
605
0
        set->add(s);
606
0
        s.truncate(unreversedPrefix.length());
607
0
    } while(++start <= end);
608
0
}
609
610
U_NAMESPACE_END
611
612
#endif  // !UCONFIG_NO_COLLATION