Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/intl/icu/source/i18n/collationdatabuilder.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) 2012-2015, International Business Machines
6
* Corporation and others.  All Rights Reserved.
7
*******************************************************************************
8
* collationdatabuilder.cpp
9
*
10
* (replaced the former ucol_elm.cpp)
11
*
12
* created on: 2012apr01
13
* created by: Markus W. Scherer
14
*/
15
16
#include "unicode/utypes.h"
17
18
#if !UCONFIG_NO_COLLATION
19
20
#include "unicode/localpointer.h"
21
#include "unicode/uchar.h"
22
#include "unicode/ucharstrie.h"
23
#include "unicode/ucharstriebuilder.h"
24
#include "unicode/uniset.h"
25
#include "unicode/unistr.h"
26
#include "unicode/usetiter.h"
27
#include "unicode/utf16.h"
28
#include "cmemory.h"
29
#include "collation.h"
30
#include "collationdata.h"
31
#include "collationdatabuilder.h"
32
#include "collationfastlatinbuilder.h"
33
#include "collationiterator.h"
34
#include "normalizer2impl.h"
35
#include "utrie2.h"
36
#include "uvectr32.h"
37
#include "uvectr64.h"
38
#include "uvector.h"
39
40
U_NAMESPACE_BEGIN
41
42
0
CollationDataBuilder::CEModifier::~CEModifier() {}
43
44
/**
45
 * Build-time context and CE32 for a code point.
46
 * If a code point has contextual mappings, then the default (no-context) mapping
47
 * and all conditional mappings are stored in a singly-linked list
48
 * of ConditionalCE32, sorted by context strings.
49
 *
50
 * Context strings sort by prefix length, then by prefix, then by contraction suffix.
51
 * Context strings must be unique and in ascending order.
52
 */
53
struct ConditionalCE32 : public UMemory {
54
    ConditionalCE32()
55
            : context(),
56
              ce32(0), defaultCE32(Collation::NO_CE32), builtCE32(Collation::NO_CE32),
57
0
              next(-1) {}
58
    ConditionalCE32(const UnicodeString &ct, uint32_t ce)
59
            : context(ct),
60
              ce32(ce), defaultCE32(Collation::NO_CE32), builtCE32(Collation::NO_CE32),
61
0
              next(-1) {}
62
63
0
    inline UBool hasContext() const { return context.length() > 1; }
64
0
    inline int32_t prefixLength() const { return context.charAt(0); }
65
66
    /**
67
     * "\0" for the first entry for any code point, with its default CE32.
68
     *
69
     * Otherwise one unit with the length of the prefix string,
70
     * then the prefix string, then the contraction suffix.
71
     */
72
    UnicodeString context;
73
    /**
74
     * CE32 for the code point and its context.
75
     * Can be special (e.g., for an expansion) but not contextual (prefix or contraction tag).
76
     */
77
    uint32_t ce32;
78
    /**
79
     * Default CE32 for all contexts with this same prefix.
80
     * Initially NO_CE32. Set only while building runtime data structures,
81
     * and only on one of the nodes of a sub-list with the same prefix.
82
     */
83
    uint32_t defaultCE32;
84
    /**
85
     * CE32 for the built contexts.
86
     * When fetching CEs from the builder, the contexts are built into their runtime form
87
     * so that the normal collation implementation can process them.
88
     * The result is cached in the list head. It is reset when the contexts are modified.
89
     */
90
    uint32_t builtCE32;
91
    /**
92
     * Index of the next ConditionalCE32.
93
     * Negative for the end of the list.
94
     */
95
    int32_t next;
96
};
97
98
U_CDECL_BEGIN
99
100
U_CAPI void U_CALLCONV
101
0
uprv_deleteConditionalCE32(void *obj) {
102
0
    delete static_cast<ConditionalCE32 *>(obj);
103
0
}
104
105
U_CDECL_END
106
107
/**
108
 * Build-time collation element and character iterator.
109
 * Uses the runtime CollationIterator for fetching CEs for a string
110
 * but reads from the builder's unfinished data structures.
111
 * In particular, this class reads from the unfinished trie
112
 * and has to avoid CollationIterator::nextCE() and redirect other
113
 * calls to data->getCE32() and data->getCE32FromSupplementary().
114
 *
115
 * We do this so that we need not implement the collation algorithm
116
 * again for the builder and make it behave exactly like the runtime code.
117
 * That would be more difficult to test and maintain than this indirection.
118
 *
119
 * Some CE32 tags (for example, the DIGIT_TAG) do not occur in the builder data,
120
 * so the data accesses from those code paths need not be modified.
121
 *
122
 * This class iterates directly over whole code points
123
 * so that the CollationIterator does not need the finished trie
124
 * for handling the LEAD_SURROGATE_TAG.
125
 */
126
class DataBuilderCollationIterator : public CollationIterator {
127
public:
128
    DataBuilderCollationIterator(CollationDataBuilder &b);
129
130
    virtual ~DataBuilderCollationIterator();
131
132
    int32_t fetchCEs(const UnicodeString &str, int32_t start, int64_t ces[], int32_t cesLength);
133
134
    virtual void resetToOffset(int32_t newOffset);
135
    virtual int32_t getOffset() const;
136
137
    virtual UChar32 nextCodePoint(UErrorCode &errorCode);
138
    virtual UChar32 previousCodePoint(UErrorCode &errorCode);
139
140
protected:
141
    virtual void forwardNumCodePoints(int32_t num, UErrorCode &errorCode);
142
    virtual void backwardNumCodePoints(int32_t num, UErrorCode &errorCode);
143
144
    virtual uint32_t getDataCE32(UChar32 c) const;
145
    virtual uint32_t getCE32FromBuilderData(uint32_t ce32, UErrorCode &errorCode);
146
147
    CollationDataBuilder &builder;
148
    CollationData builderData;
149
    uint32_t jamoCE32s[CollationData::JAMO_CE32S_LENGTH];
150
    const UnicodeString *s;
151
    int32_t pos;
152
};
153
154
DataBuilderCollationIterator::DataBuilderCollationIterator(CollationDataBuilder &b)
155
        : CollationIterator(&builderData, /*numeric=*/ FALSE),
156
          builder(b), builderData(b.nfcImpl),
157
0
          s(NULL), pos(0) {
158
0
    builderData.base = builder.base;
159
0
    // Set all of the jamoCE32s[] to indirection CE32s.
160
0
    for(int32_t j = 0; j < CollationData::JAMO_CE32S_LENGTH; ++j) {  // Count across Jamo types.
161
0
        UChar32 jamo = CollationDataBuilder::jamoCpFromIndex(j);
162
0
        jamoCE32s[j] = Collation::makeCE32FromTagAndIndex(Collation::BUILDER_DATA_TAG, jamo) |
163
0
                CollationDataBuilder::IS_BUILDER_JAMO_CE32;
164
0
    }
165
0
    builderData.jamoCE32s = jamoCE32s;
166
0
}
167
168
0
DataBuilderCollationIterator::~DataBuilderCollationIterator() {}
169
170
int32_t
171
DataBuilderCollationIterator::fetchCEs(const UnicodeString &str, int32_t start,
172
0
                                       int64_t ces[], int32_t cesLength) {
173
0
    // Set the pointers each time, in case they changed due to reallocation.
174
0
    builderData.ce32s = reinterpret_cast<const uint32_t *>(builder.ce32s.getBuffer());
175
0
    builderData.ces = builder.ce64s.getBuffer();
176
0
    builderData.contexts = builder.contexts.getBuffer();
177
0
    // Modified copy of CollationIterator::nextCE() and CollationIterator::nextCEFromCE32().
178
0
    reset();
179
0
    s = &str;
180
0
    pos = start;
181
0
    UErrorCode errorCode = U_ZERO_ERROR;
182
0
    while(U_SUCCESS(errorCode) && pos < s->length()) {
183
0
        // No need to keep all CEs in the iterator buffer.
184
0
        clearCEs();
185
0
        UChar32 c = s->char32At(pos);
186
0
        pos += U16_LENGTH(c);
187
0
        uint32_t ce32 = utrie2_get32(builder.trie, c);
188
0
        const CollationData *d;
189
0
        if(ce32 == Collation::FALLBACK_CE32) {
190
0
            d = builder.base;
191
0
            ce32 = builder.base->getCE32(c);
192
0
        } else {
193
0
            d = &builderData;
194
0
        }
195
0
        appendCEsFromCE32(d, c, ce32, /*forward=*/ TRUE, errorCode);
196
0
        U_ASSERT(U_SUCCESS(errorCode));
197
0
        for(int32_t i = 0; i < getCEsLength(); ++i) {
198
0
            int64_t ce = getCE(i);
199
0
            if(ce != 0) {
200
0
                if(cesLength < Collation::MAX_EXPANSION_LENGTH) {
201
0
                    ces[cesLength] = ce;
202
0
                }
203
0
                ++cesLength;
204
0
            }
205
0
        }
206
0
    }
207
0
    return cesLength;
208
0
}
209
210
void
211
0
DataBuilderCollationIterator::resetToOffset(int32_t newOffset) {
212
0
    reset();
213
0
    pos = newOffset;
214
0
}
215
216
int32_t
217
0
DataBuilderCollationIterator::getOffset() const {
218
0
    return pos;
219
0
}
220
221
UChar32
222
0
DataBuilderCollationIterator::nextCodePoint(UErrorCode & /*errorCode*/) {
223
0
    if(pos == s->length()) {
224
0
        return U_SENTINEL;
225
0
    }
226
0
    UChar32 c = s->char32At(pos);
227
0
    pos += U16_LENGTH(c);
228
0
    return c;
229
0
}
230
231
UChar32
232
0
DataBuilderCollationIterator::previousCodePoint(UErrorCode & /*errorCode*/) {
233
0
    if(pos == 0) {
234
0
        return U_SENTINEL;
235
0
    }
236
0
    UChar32 c = s->char32At(pos - 1);
237
0
    pos -= U16_LENGTH(c);
238
0
    return c;
239
0
}
240
241
void
242
0
DataBuilderCollationIterator::forwardNumCodePoints(int32_t num, UErrorCode & /*errorCode*/) {
243
0
    pos = s->moveIndex32(pos, num);
244
0
}
245
246
void
247
0
DataBuilderCollationIterator::backwardNumCodePoints(int32_t num, UErrorCode & /*errorCode*/) {
248
0
    pos = s->moveIndex32(pos, -num);
249
0
}
250
251
uint32_t
252
0
DataBuilderCollationIterator::getDataCE32(UChar32 c) const {
253
0
    return utrie2_get32(builder.trie, c);
254
0
}
255
256
uint32_t
257
0
DataBuilderCollationIterator::getCE32FromBuilderData(uint32_t ce32, UErrorCode &errorCode) {
258
0
    U_ASSERT(Collation::hasCE32Tag(ce32, Collation::BUILDER_DATA_TAG));
259
0
    if((ce32 & CollationDataBuilder::IS_BUILDER_JAMO_CE32) != 0) {
260
0
        UChar32 jamo = Collation::indexFromCE32(ce32);
261
0
        return utrie2_get32(builder.trie, jamo);
262
0
    } else {
263
0
        ConditionalCE32 *cond = builder.getConditionalCE32ForCE32(ce32);
264
0
        if(cond->builtCE32 == Collation::NO_CE32) {
265
0
            // Build the context-sensitive mappings into their runtime form and cache the result.
266
0
            cond->builtCE32 = builder.buildContext(cond, errorCode);
267
0
            if(errorCode == U_BUFFER_OVERFLOW_ERROR) {
268
0
                errorCode = U_ZERO_ERROR;
269
0
                builder.clearContexts();
270
0
                cond->builtCE32 = builder.buildContext(cond, errorCode);
271
0
            }
272
0
            builderData.contexts = builder.contexts.getBuffer();
273
0
        }
274
0
        return cond->builtCE32;
275
0
    }
276
0
}
277
278
// ------------------------------------------------------------------------- ***
279
280
CollationDataBuilder::CollationDataBuilder(UErrorCode &errorCode)
281
        : nfcImpl(*Normalizer2Factory::getNFCImpl(errorCode)),
282
          base(NULL), baseSettings(NULL),
283
          trie(NULL),
284
          ce32s(errorCode), ce64s(errorCode), conditionalCE32s(errorCode),
285
          modified(FALSE),
286
          fastLatinEnabled(FALSE), fastLatinBuilder(NULL),
287
0
          collIter(NULL) {
288
0
    // Reserve the first CE32 for U+0000.
289
0
    ce32s.addElement(0, errorCode);
290
0
    conditionalCE32s.setDeleter(uprv_deleteConditionalCE32);
291
0
}
292
293
0
CollationDataBuilder::~CollationDataBuilder() {
294
0
    utrie2_close(trie);
295
0
    delete fastLatinBuilder;
296
0
    delete collIter;
297
0
}
298
299
void
300
0
CollationDataBuilder::initForTailoring(const CollationData *b, UErrorCode &errorCode) {
301
0
    if(U_FAILURE(errorCode)) { return; }
302
0
    if(trie != NULL) {
303
0
        errorCode = U_INVALID_STATE_ERROR;
304
0
        return;
305
0
    }
306
0
    if(b == NULL) {
307
0
        errorCode = U_ILLEGAL_ARGUMENT_ERROR;
308
0
        return;
309
0
    }
310
0
    base = b;
311
0
312
0
    // For a tailoring, the default is to fall back to the base.
313
0
    trie = utrie2_open(Collation::FALLBACK_CE32, Collation::FFFD_CE32, &errorCode);
314
0
315
0
    // Set the Latin-1 letters block so that it is allocated first in the data array,
316
0
    // to try to improve locality of reference when sorting Latin-1 text.
317
0
    // Do not use utrie2_setRange32() since that will not actually allocate blocks
318
0
    // that are filled with the default value.
319
0
    // ASCII (0..7F) is already preallocated anyway.
320
0
    for(UChar32 c = 0xc0; c <= 0xff; ++c) {
321
0
        utrie2_set32(trie, c, Collation::FALLBACK_CE32, &errorCode);
322
0
    }
323
0
324
0
    // Hangul syllables are not tailorable (except via tailoring Jamos).
325
0
    // Always set the Hangul tag to help performance.
326
0
    // Do this here, rather than in buildMappings(),
327
0
    // so that we see the HANGUL_TAG in various assertions.
328
0
    uint32_t hangulCE32 = Collation::makeCE32FromTagAndIndex(Collation::HANGUL_TAG, 0);
329
0
    utrie2_setRange32(trie, Hangul::HANGUL_BASE, Hangul::HANGUL_END, hangulCE32, TRUE, &errorCode);
330
0
331
0
    // Copy the set contents but don't copy/clone the set as a whole because
332
0
    // that would copy the isFrozen state too.
333
0
    unsafeBackwardSet.addAll(*b->unsafeBackwardSet);
334
0
335
0
    if(U_FAILURE(errorCode)) { return; }
336
0
}
337
338
UBool
339
CollationDataBuilder::maybeSetPrimaryRange(UChar32 start, UChar32 end,
340
                                           uint32_t primary, int32_t step,
341
0
                                           UErrorCode &errorCode) {
342
0
    if(U_FAILURE(errorCode)) { return FALSE; }
343
0
    U_ASSERT(start <= end);
344
0
    // TODO: Do we need to check what values are currently set for start..end?
345
0
    // An offset range is worth it only if we can achieve an overlap between
346
0
    // adjacent UTrie2 blocks of 32 code points each.
347
0
    // An offset CE is also a little more expensive to look up and compute
348
0
    // than a simple CE.
349
0
    // If the range spans at least three UTrie2 block boundaries (> 64 code points),
350
0
    // then we take it.
351
0
    // If the range spans one or two block boundaries and there are
352
0
    // at least 4 code points on either side, then we take it.
353
0
    // (We could additionally require a minimum range length of, say, 16.)
354
0
    int32_t blockDelta = (end >> 5) - (start >> 5);
355
0
    if(2 <= step && step <= 0x7f &&
356
0
            (blockDelta >= 3 ||
357
0
            (blockDelta > 0 && (start & 0x1f) <= 0x1c && (end & 0x1f) >= 3))) {
358
0
        int64_t dataCE = ((int64_t)primary << 32) | (start << 8) | step;
359
0
        if(isCompressiblePrimary(primary)) { dataCE |= 0x80; }
360
0
        int32_t index = addCE(dataCE, errorCode);
361
0
        if(U_FAILURE(errorCode)) { return 0; }
362
0
        if(index > Collation::MAX_INDEX) {
363
0
            errorCode = U_BUFFER_OVERFLOW_ERROR;
364
0
            return 0;
365
0
        }
366
0
        uint32_t offsetCE32 = Collation::makeCE32FromTagAndIndex(Collation::OFFSET_TAG, index);
367
0
        utrie2_setRange32(trie, start, end, offsetCE32, TRUE, &errorCode);
368
0
        modified = TRUE;
369
0
        return TRUE;
370
0
    } else {
371
0
        return FALSE;
372
0
    }
373
0
}
374
375
uint32_t
376
CollationDataBuilder::setPrimaryRangeAndReturnNext(UChar32 start, UChar32 end,
377
                                                   uint32_t primary, int32_t step,
378
0
                                                   UErrorCode &errorCode) {
379
0
    if(U_FAILURE(errorCode)) { return 0; }
380
0
    UBool isCompressible = isCompressiblePrimary(primary);
381
0
    if(maybeSetPrimaryRange(start, end, primary, step, errorCode)) {
382
0
        return Collation::incThreeBytePrimaryByOffset(primary, isCompressible,
383
0
                                                      (end - start + 1) * step);
384
0
    } else {
385
0
        // Short range: Set individual CE32s.
386
0
        for(;;) {
387
0
            utrie2_set32(trie, start, Collation::makeLongPrimaryCE32(primary), &errorCode);
388
0
            ++start;
389
0
            primary = Collation::incThreeBytePrimaryByOffset(primary, isCompressible, step);
390
0
            if(start > end) { return primary; }
391
0
        }
392
0
        modified = TRUE;
393
0
    }
394
0
}
395
396
uint32_t
397
0
CollationDataBuilder::getCE32FromOffsetCE32(UBool fromBase, UChar32 c, uint32_t ce32) const {
398
0
    int32_t i = Collation::indexFromCE32(ce32);
399
0
    int64_t dataCE = fromBase ? base->ces[i] : ce64s.elementAti(i);
400
0
    uint32_t p = Collation::getThreeBytePrimaryForOffsetData(c, dataCE);
401
0
    return Collation::makeLongPrimaryCE32(p);
402
0
}
403
404
UBool
405
0
CollationDataBuilder::isCompressibleLeadByte(uint32_t b) const {
406
0
    return base->isCompressibleLeadByte(b);
407
0
}
408
409
UBool
410
0
CollationDataBuilder::isAssigned(UChar32 c) const {
411
0
    return Collation::isAssignedCE32(utrie2_get32(trie, c));
412
0
}
413
414
uint32_t
415
0
CollationDataBuilder::getLongPrimaryIfSingleCE(UChar32 c) const {
416
0
    uint32_t ce32 = utrie2_get32(trie, c);
417
0
    if(Collation::isLongPrimaryCE32(ce32)) {
418
0
        return Collation::primaryFromLongPrimaryCE32(ce32);
419
0
    } else {
420
0
        return 0;
421
0
    }
422
0
}
423
424
int64_t
425
0
CollationDataBuilder::getSingleCE(UChar32 c, UErrorCode &errorCode) const {
426
0
    if(U_FAILURE(errorCode)) { return 0; }
427
0
    // Keep parallel with CollationData::getSingleCE().
428
0
    UBool fromBase = FALSE;
429
0
    uint32_t ce32 = utrie2_get32(trie, c);
430
0
    if(ce32 == Collation::FALLBACK_CE32) {
431
0
        fromBase = TRUE;
432
0
        ce32 = base->getCE32(c);
433
0
    }
434
0
    while(Collation::isSpecialCE32(ce32)) {
435
0
        switch(Collation::tagFromCE32(ce32)) {
436
0
        case Collation::LATIN_EXPANSION_TAG:
437
0
        case Collation::BUILDER_DATA_TAG:
438
0
        case Collation::PREFIX_TAG:
439
0
        case Collation::CONTRACTION_TAG:
440
0
        case Collation::HANGUL_TAG:
441
0
        case Collation::LEAD_SURROGATE_TAG:
442
0
            errorCode = U_UNSUPPORTED_ERROR;
443
0
            return 0;
444
0
        case Collation::FALLBACK_TAG:
445
0
        case Collation::RESERVED_TAG_3:
446
0
            errorCode = U_INTERNAL_PROGRAM_ERROR;
447
0
            return 0;
448
0
        case Collation::LONG_PRIMARY_TAG:
449
0
            return Collation::ceFromLongPrimaryCE32(ce32);
450
0
        case Collation::LONG_SECONDARY_TAG:
451
0
            return Collation::ceFromLongSecondaryCE32(ce32);
452
0
        case Collation::EXPANSION32_TAG:
453
0
            if(Collation::lengthFromCE32(ce32) == 1) {
454
0
                int32_t i = Collation::indexFromCE32(ce32);
455
0
                ce32 = fromBase ? base->ce32s[i] : ce32s.elementAti(i);
456
0
                break;
457
0
            } else {
458
0
                errorCode = U_UNSUPPORTED_ERROR;
459
0
                return 0;
460
0
            }
461
0
        case Collation::EXPANSION_TAG: {
462
0
            if(Collation::lengthFromCE32(ce32) == 1) {
463
0
                int32_t i = Collation::indexFromCE32(ce32);
464
0
                return fromBase ? base->ces[i] : ce64s.elementAti(i);
465
0
            } else {
466
0
                errorCode = U_UNSUPPORTED_ERROR;
467
0
                return 0;
468
0
            }
469
0
        }
470
0
        case Collation::DIGIT_TAG:
471
0
            // Fetch the non-numeric-collation CE32 and continue.
472
0
            ce32 = ce32s.elementAti(Collation::indexFromCE32(ce32));
473
0
            break;
474
0
        case Collation::U0000_TAG:
475
0
            U_ASSERT(c == 0);
476
0
            // Fetch the normal ce32 for U+0000 and continue.
477
0
            ce32 = fromBase ? base->ce32s[0] : ce32s.elementAti(0);
478
0
            break;
479
0
        case Collation::OFFSET_TAG:
480
0
            ce32 = getCE32FromOffsetCE32(fromBase, c, ce32);
481
0
            break;
482
0
        case Collation::IMPLICIT_TAG:
483
0
            return Collation::unassignedCEFromCodePoint(c);
484
0
        }
485
0
    }
486
0
    return Collation::ceFromSimpleCE32(ce32);
487
0
}
488
489
int32_t
490
0
CollationDataBuilder::addCE(int64_t ce, UErrorCode &errorCode) {
491
0
    int32_t length = ce64s.size();
492
0
    for(int32_t i = 0; i < length; ++i) {
493
0
        if(ce == ce64s.elementAti(i)) { return i; }
494
0
    }
495
0
    ce64s.addElement(ce, errorCode);
496
0
    return length;
497
0
}
498
499
int32_t
500
0
CollationDataBuilder::addCE32(uint32_t ce32, UErrorCode &errorCode) {
501
0
    int32_t length = ce32s.size();
502
0
    for(int32_t i = 0; i < length; ++i) {
503
0
        if(ce32 == (uint32_t)ce32s.elementAti(i)) { return i; }
504
0
    }
505
0
    ce32s.addElement((int32_t)ce32, errorCode);  
506
0
    return length;
507
0
}
508
509
int32_t
510
CollationDataBuilder::addConditionalCE32(const UnicodeString &context, uint32_t ce32,
511
0
                                         UErrorCode &errorCode) {
512
0
    if(U_FAILURE(errorCode)) { return -1; }
513
0
    U_ASSERT(!context.isEmpty());
514
0
    int32_t index = conditionalCE32s.size();
515
0
    if(index > Collation::MAX_INDEX) {
516
0
        errorCode = U_BUFFER_OVERFLOW_ERROR;
517
0
        return -1;
518
0
    }
519
0
    ConditionalCE32 *cond = new ConditionalCE32(context, ce32);
520
0
    if(cond == NULL) {
521
0
        errorCode = U_MEMORY_ALLOCATION_ERROR;
522
0
        return -1;
523
0
    }
524
0
    conditionalCE32s.addElement(cond, errorCode);
525
0
    return index;
526
0
}
527
528
void
529
CollationDataBuilder::add(const UnicodeString &prefix, const UnicodeString &s,
530
                          const int64_t ces[], int32_t cesLength,
531
0
                          UErrorCode &errorCode) {
532
0
    uint32_t ce32 = encodeCEs(ces, cesLength, errorCode);
533
0
    addCE32(prefix, s, ce32, errorCode);
534
0
}
535
536
void
537
CollationDataBuilder::addCE32(const UnicodeString &prefix, const UnicodeString &s,
538
0
                              uint32_t ce32, UErrorCode &errorCode) {
539
0
    if(U_FAILURE(errorCode)) { return; }
540
0
    if(s.isEmpty()) {
541
0
        errorCode = U_ILLEGAL_ARGUMENT_ERROR;
542
0
        return;
543
0
    }
544
0
    if(trie == NULL || utrie2_isFrozen(trie)) {
545
0
        errorCode = U_INVALID_STATE_ERROR;
546
0
        return;
547
0
    }
548
0
    UChar32 c = s.char32At(0);
549
0
    int32_t cLength = U16_LENGTH(c);
550
0
    uint32_t oldCE32 = utrie2_get32(trie, c);
551
0
    UBool hasContext = !prefix.isEmpty() || s.length() > cLength;
552
0
    if(oldCE32 == Collation::FALLBACK_CE32) {
553
0
        // First tailoring for c.
554
0
        // If c has contextual base mappings or if we add a contextual mapping,
555
0
        // then copy the base mappings.
556
0
        // Otherwise we just override the base mapping.
557
0
        uint32_t baseCE32 = base->getFinalCE32(base->getCE32(c));
558
0
        if(hasContext || Collation::ce32HasContext(baseCE32)) {
559
0
            oldCE32 = copyFromBaseCE32(c, baseCE32, TRUE, errorCode);
560
0
            utrie2_set32(trie, c, oldCE32, &errorCode);
561
0
            if(U_FAILURE(errorCode)) { return; }
562
0
        }
563
0
    }
564
0
    if(!hasContext) {
565
0
        // No prefix, no contraction.
566
0
        if(!isBuilderContextCE32(oldCE32)) {
567
0
            utrie2_set32(trie, c, ce32, &errorCode);
568
0
        } else {
569
0
            ConditionalCE32 *cond = getConditionalCE32ForCE32(oldCE32);
570
0
            cond->builtCE32 = Collation::NO_CE32;
571
0
            cond->ce32 = ce32;
572
0
        }
573
0
    } else {
574
0
        ConditionalCE32 *cond;
575
0
        if(!isBuilderContextCE32(oldCE32)) {
576
0
            // Replace the simple oldCE32 with a builder context CE32
577
0
            // pointing to a new ConditionalCE32 list head.
578
0
            int32_t index = addConditionalCE32(UnicodeString((UChar)0), oldCE32, errorCode);
579
0
            if(U_FAILURE(errorCode)) { return; }
580
0
            uint32_t contextCE32 = makeBuilderContextCE32(index);
581
0
            utrie2_set32(trie, c, contextCE32, &errorCode);
582
0
            contextChars.add(c);
583
0
            cond = getConditionalCE32(index);
584
0
        } else {
585
0
            cond = getConditionalCE32ForCE32(oldCE32);
586
0
            cond->builtCE32 = Collation::NO_CE32;
587
0
        }
588
0
        UnicodeString suffix(s, cLength);
589
0
        UnicodeString context((UChar)prefix.length());
590
0
        context.append(prefix).append(suffix);
591
0
        unsafeBackwardSet.addAll(suffix);
592
0
        for(;;) {
593
0
            // invariant: context > cond->context
594
0
            int32_t next = cond->next;
595
0
            if(next < 0) {
596
0
                // Append a new ConditionalCE32 after cond.
597
0
                int32_t index = addConditionalCE32(context, ce32, errorCode);
598
0
                if(U_FAILURE(errorCode)) { return; }
599
0
                cond->next = index;
600
0
                break;
601
0
            }
602
0
            ConditionalCE32 *nextCond = getConditionalCE32(next);
603
0
            int8_t cmp = context.compare(nextCond->context);
604
0
            if(cmp < 0) {
605
0
                // Insert a new ConditionalCE32 between cond and nextCond.
606
0
                int32_t index = addConditionalCE32(context, ce32, errorCode);
607
0
                if(U_FAILURE(errorCode)) { return; }
608
0
                cond->next = index;
609
0
                getConditionalCE32(index)->next = next;
610
0
                break;
611
0
            } else if(cmp == 0) {
612
0
                // Same context as before, overwrite its ce32.
613
0
                nextCond->ce32 = ce32;
614
0
                break;
615
0
            }
616
0
            cond = nextCond;
617
0
        }
618
0
    }
619
0
    modified = TRUE;
620
0
}
621
622
uint32_t
623
0
CollationDataBuilder::encodeOneCEAsCE32(int64_t ce) {
624
0
    uint32_t p = (uint32_t)(ce >> 32);
625
0
    uint32_t lower32 = (uint32_t)ce;
626
0
    uint32_t t = (uint32_t)(ce & 0xffff);
627
0
    U_ASSERT((t & 0xc000) != 0xc000);  // Impossible case bits 11 mark special CE32s.
628
0
    if((ce & INT64_C(0xffff00ff00ff)) == 0) {
629
0
        // normal form ppppsstt
630
0
        return p | (lower32 >> 16) | (t >> 8);
631
0
    } else if((ce & INT64_C(0xffffffffff)) == Collation::COMMON_SEC_AND_TER_CE) {
632
0
        // long-primary form ppppppC1
633
0
        return Collation::makeLongPrimaryCE32(p);
634
0
    } else if(p == 0 && (t & 0xff) == 0) {
635
0
        // long-secondary form ssssttC2
636
0
        return Collation::makeLongSecondaryCE32(lower32);
637
0
    }
638
0
    return Collation::NO_CE32;
639
0
}
640
641
uint32_t
642
0
CollationDataBuilder::encodeOneCE(int64_t ce, UErrorCode &errorCode) {
643
0
    // Try to encode one CE as one CE32.
644
0
    uint32_t ce32 = encodeOneCEAsCE32(ce);
645
0
    if(ce32 != Collation::NO_CE32) { return ce32; }
646
0
    int32_t index = addCE(ce, errorCode);
647
0
    if(U_FAILURE(errorCode)) { return 0; }
648
0
    if(index > Collation::MAX_INDEX) {
649
0
        errorCode = U_BUFFER_OVERFLOW_ERROR;
650
0
        return 0;
651
0
    }
652
0
    return Collation::makeCE32FromTagIndexAndLength(Collation::EXPANSION_TAG, index, 1);
653
0
}
654
655
uint32_t
656
CollationDataBuilder::encodeCEs(const int64_t ces[], int32_t cesLength,
657
0
                                UErrorCode &errorCode) {
658
0
    if(U_FAILURE(errorCode)) { return 0; }
659
0
    if(cesLength < 0 || cesLength > Collation::MAX_EXPANSION_LENGTH) {
660
0
        errorCode = U_ILLEGAL_ARGUMENT_ERROR;
661
0
        return 0;
662
0
    }
663
0
    if(trie == NULL || utrie2_isFrozen(trie)) {
664
0
        errorCode = U_INVALID_STATE_ERROR;
665
0
        return 0;
666
0
    }
667
0
    if(cesLength == 0) {
668
0
        // Convenience: We cannot map to nothing, but we can map to a completely ignorable CE.
669
0
        // Do this here so that callers need not do it.
670
0
        return encodeOneCEAsCE32(0);
671
0
    } else if(cesLength == 1) {
672
0
        return encodeOneCE(ces[0], errorCode);
673
0
    } else if(cesLength == 2) {
674
0
        // Try to encode two CEs as one CE32.
675
0
        int64_t ce0 = ces[0];
676
0
        int64_t ce1 = ces[1];
677
0
        uint32_t p0 = (uint32_t)(ce0 >> 32);
678
0
        if((ce0 & INT64_C(0xffffffffff00ff)) == Collation::COMMON_SECONDARY_CE &&
679
0
                (ce1 & INT64_C(0xffffffff00ffffff)) == Collation::COMMON_TERTIARY_CE &&
680
0
                p0 != 0) {
681
0
            // Latin mini expansion
682
0
            return
683
0
                p0 |
684
0
                (((uint32_t)ce0 & 0xff00u) << 8) |
685
0
                (uint32_t)(ce1 >> 16) |
686
0
                Collation::SPECIAL_CE32_LOW_BYTE |
687
0
                Collation::LATIN_EXPANSION_TAG;
688
0
        }
689
0
    }
690
0
    // Try to encode two or more CEs as CE32s.
691
0
    int32_t newCE32s[Collation::MAX_EXPANSION_LENGTH];
692
0
    for(int32_t i = 0;; ++i) {
693
0
        if(i == cesLength) {
694
0
            return encodeExpansion32(newCE32s, cesLength, errorCode);
695
0
        }
696
0
        uint32_t ce32 = encodeOneCEAsCE32(ces[i]);
697
0
        if(ce32 == Collation::NO_CE32) { break; }
698
0
        newCE32s[i] = (int32_t)ce32;
699
0
    }
700
0
    return encodeExpansion(ces, cesLength, errorCode);
701
0
}
702
703
uint32_t
704
0
CollationDataBuilder::encodeExpansion(const int64_t ces[], int32_t length, UErrorCode &errorCode) {
705
0
    if(U_FAILURE(errorCode)) { return 0; }
706
0
    // See if this sequence of CEs has already been stored.
707
0
    int64_t first = ces[0];
708
0
    int32_t ce64sMax = ce64s.size() - length;
709
0
    for(int32_t i = 0; i <= ce64sMax; ++i) {
710
0
        if(first == ce64s.elementAti(i)) {
711
0
            if(i > Collation::MAX_INDEX) {
712
0
                errorCode = U_BUFFER_OVERFLOW_ERROR;
713
0
                return 0;
714
0
            }
715
0
            for(int32_t j = 1;; ++j) {
716
0
                if(j == length) {
717
0
                    return Collation::makeCE32FromTagIndexAndLength(
718
0
                            Collation::EXPANSION_TAG, i, length);
719
0
                }
720
0
                if(ce64s.elementAti(i + j) != ces[j]) { break; }
721
0
            }
722
0
        }
723
0
    }
724
0
    // Store the new sequence.
725
0
    int32_t i = ce64s.size();
726
0
    if(i > Collation::MAX_INDEX) {
727
0
        errorCode = U_BUFFER_OVERFLOW_ERROR;
728
0
        return 0;
729
0
    }
730
0
    for(int32_t j = 0; j < length; ++j) {
731
0
        ce64s.addElement(ces[j], errorCode);
732
0
    }
733
0
    return Collation::makeCE32FromTagIndexAndLength(Collation::EXPANSION_TAG, i, length);
734
0
}
735
736
uint32_t
737
CollationDataBuilder::encodeExpansion32(const int32_t newCE32s[], int32_t length,
738
0
                                        UErrorCode &errorCode) {
739
0
    if(U_FAILURE(errorCode)) { return 0; }
740
0
    // See if this sequence of CE32s has already been stored.
741
0
    int32_t first = newCE32s[0];
742
0
    int32_t ce32sMax = ce32s.size() - length;
743
0
    for(int32_t i = 0; i <= ce32sMax; ++i) {
744
0
        if(first == ce32s.elementAti(i)) {
745
0
            if(i > Collation::MAX_INDEX) {
746
0
                errorCode = U_BUFFER_OVERFLOW_ERROR;
747
0
                return 0;
748
0
            }
749
0
            for(int32_t j = 1;; ++j) {
750
0
                if(j == length) {
751
0
                    return Collation::makeCE32FromTagIndexAndLength(
752
0
                            Collation::EXPANSION32_TAG, i, length);
753
0
                }
754
0
                if(ce32s.elementAti(i + j) != newCE32s[j]) { break; }
755
0
            }
756
0
        }
757
0
    }
758
0
    // Store the new sequence.
759
0
    int32_t i = ce32s.size();
760
0
    if(i > Collation::MAX_INDEX) {
761
0
        errorCode = U_BUFFER_OVERFLOW_ERROR;
762
0
        return 0;
763
0
    }
764
0
    for(int32_t j = 0; j < length; ++j) {
765
0
        ce32s.addElement(newCE32s[j], errorCode);
766
0
    }
767
0
    return Collation::makeCE32FromTagIndexAndLength(Collation::EXPANSION32_TAG, i, length);
768
0
}
769
770
uint32_t
771
CollationDataBuilder::copyFromBaseCE32(UChar32 c, uint32_t ce32, UBool withContext,
772
0
                                       UErrorCode &errorCode) {
773
0
    if(U_FAILURE(errorCode)) { return 0; }
774
0
    if(!Collation::isSpecialCE32(ce32)) { return ce32; }
775
0
    switch(Collation::tagFromCE32(ce32)) {
776
0
    case Collation::LONG_PRIMARY_TAG:
777
0
    case Collation::LONG_SECONDARY_TAG:
778
0
    case Collation::LATIN_EXPANSION_TAG:
779
0
        // copy as is
780
0
        break;
781
0
    case Collation::EXPANSION32_TAG: {
782
0
        const uint32_t *baseCE32s = base->ce32s + Collation::indexFromCE32(ce32);
783
0
        int32_t length = Collation::lengthFromCE32(ce32);
784
0
        ce32 = encodeExpansion32(
785
0
            reinterpret_cast<const int32_t *>(baseCE32s), length, errorCode);
786
0
        break;
787
0
    }
788
0
    case Collation::EXPANSION_TAG: {
789
0
        const int64_t *baseCEs = base->ces + Collation::indexFromCE32(ce32);
790
0
        int32_t length = Collation::lengthFromCE32(ce32);
791
0
        ce32 = encodeExpansion(baseCEs, length, errorCode);
792
0
        break;
793
0
    }
794
0
    case Collation::PREFIX_TAG: {
795
0
        // Flatten prefixes and nested suffixes (contractions)
796
0
        // into a linear list of ConditionalCE32.
797
0
        const UChar *p = base->contexts + Collation::indexFromCE32(ce32);
798
0
        ce32 = CollationData::readCE32(p);  // Default if no prefix match.
799
0
        if(!withContext) {
800
0
            return copyFromBaseCE32(c, ce32, FALSE, errorCode);
801
0
        }
802
0
        ConditionalCE32 head;
803
0
        UnicodeString context((UChar)0);
804
0
        int32_t index;
805
0
        if(Collation::isContractionCE32(ce32)) {
806
0
            index = copyContractionsFromBaseCE32(context, c, ce32, &head, errorCode);
807
0
        } else {
808
0
            ce32 = copyFromBaseCE32(c, ce32, TRUE, errorCode);
809
0
            head.next = index = addConditionalCE32(context, ce32, errorCode);
810
0
        }
811
0
        if(U_FAILURE(errorCode)) { return 0; }
812
0
        ConditionalCE32 *cond = getConditionalCE32(index);  // the last ConditionalCE32 so far
813
0
        UCharsTrie::Iterator prefixes(p + 2, 0, errorCode);
814
0
        while(prefixes.next(errorCode)) {
815
0
            context = prefixes.getString();
816
0
            context.reverse();
817
0
            context.insert(0, (UChar)context.length());
818
0
            ce32 = (uint32_t)prefixes.getValue();
819
0
            if(Collation::isContractionCE32(ce32)) {
820
0
                index = copyContractionsFromBaseCE32(context, c, ce32, cond, errorCode);
821
0
            } else {
822
0
                ce32 = copyFromBaseCE32(c, ce32, TRUE, errorCode);
823
0
                cond->next = index = addConditionalCE32(context, ce32, errorCode);
824
0
            }
825
0
            if(U_FAILURE(errorCode)) { return 0; }
826
0
            cond = getConditionalCE32(index);
827
0
        }
828
0
        ce32 = makeBuilderContextCE32(head.next);
829
0
        contextChars.add(c);
830
0
        break;
831
0
    }
832
0
    case Collation::CONTRACTION_TAG: {
833
0
        if(!withContext) {
834
0
            const UChar *p = base->contexts + Collation::indexFromCE32(ce32);
835
0
            ce32 = CollationData::readCE32(p);  // Default if no suffix match.
836
0
            return copyFromBaseCE32(c, ce32, FALSE, errorCode);
837
0
        }
838
0
        ConditionalCE32 head;
839
0
        UnicodeString context((UChar)0);
840
0
        copyContractionsFromBaseCE32(context, c, ce32, &head, errorCode);
841
0
        ce32 = makeBuilderContextCE32(head.next);
842
0
        contextChars.add(c);
843
0
        break;
844
0
    }
845
0
    case Collation::HANGUL_TAG:
846
0
        errorCode = U_UNSUPPORTED_ERROR;  // We forbid tailoring of Hangul syllables.
847
0
        break;
848
0
    case Collation::OFFSET_TAG:
849
0
        ce32 = getCE32FromOffsetCE32(TRUE, c, ce32);
850
0
        break;
851
0
    case Collation::IMPLICIT_TAG:
852
0
        ce32 = encodeOneCE(Collation::unassignedCEFromCodePoint(c), errorCode);
853
0
        break;
854
0
    default:
855
0
        U_ASSERT(FALSE);  // require ce32 == base->getFinalCE32(ce32)
856
0
        break;
857
0
    }
858
0
    return ce32;
859
0
}
860
861
int32_t
862
CollationDataBuilder::copyContractionsFromBaseCE32(UnicodeString &context, UChar32 c, uint32_t ce32,
863
0
                                                   ConditionalCE32 *cond, UErrorCode &errorCode) {
864
0
    if(U_FAILURE(errorCode)) { return 0; }
865
0
    const UChar *p = base->contexts + Collation::indexFromCE32(ce32);
866
0
    int32_t index;
867
0
    if((ce32 & Collation::CONTRACT_SINGLE_CP_NO_MATCH) != 0) {
868
0
        // No match on the single code point.
869
0
        // We are underneath a prefix, and the default mapping is just
870
0
        // a fallback to the mappings for a shorter prefix.
871
0
        U_ASSERT(context.length() > 1);
872
0
        index = -1;
873
0
    } else {
874
0
        ce32 = CollationData::readCE32(p);  // Default if no suffix match.
875
0
        U_ASSERT(!Collation::isContractionCE32(ce32));
876
0
        ce32 = copyFromBaseCE32(c, ce32, TRUE, errorCode);
877
0
        cond->next = index = addConditionalCE32(context, ce32, errorCode);
878
0
        if(U_FAILURE(errorCode)) { return 0; }
879
0
        cond = getConditionalCE32(index);
880
0
    }
881
0
882
0
    int32_t suffixStart = context.length();
883
0
    UCharsTrie::Iterator suffixes(p + 2, 0, errorCode);
884
0
    while(suffixes.next(errorCode)) {
885
0
        context.append(suffixes.getString());
886
0
        ce32 = copyFromBaseCE32(c, (uint32_t)suffixes.getValue(), TRUE, errorCode);
887
0
        cond->next = index = addConditionalCE32(context, ce32, errorCode);
888
0
        if(U_FAILURE(errorCode)) { return 0; }
889
0
        // No need to update the unsafeBackwardSet because the tailoring set
890
0
        // is already a copy of the base set.
891
0
        cond = getConditionalCE32(index);
892
0
        context.truncate(suffixStart);
893
0
    }
894
0
    U_ASSERT(index >= 0);
895
0
    return index;
896
0
}
897
898
class CopyHelper {
899
public:
900
    CopyHelper(const CollationDataBuilder &s, CollationDataBuilder &d,
901
               const CollationDataBuilder::CEModifier &m, UErrorCode &initialErrorCode)
902
            : src(s), dest(d), modifier(m),
903
0
              errorCode(initialErrorCode) {}
904
905
0
    UBool copyRangeCE32(UChar32 start, UChar32 end, uint32_t ce32) {
906
0
        ce32 = copyCE32(ce32);
907
0
        utrie2_setRange32(dest.trie, start, end, ce32, TRUE, &errorCode);
908
0
        if(CollationDataBuilder::isBuilderContextCE32(ce32)) {
909
0
            dest.contextChars.add(start, end);
910
0
        }
911
0
        return U_SUCCESS(errorCode);
912
0
    }
913
914
0
    uint32_t copyCE32(uint32_t ce32) {
915
0
        if(!Collation::isSpecialCE32(ce32)) {
916
0
            int64_t ce = modifier.modifyCE32(ce32);
917
0
            if(ce != Collation::NO_CE) {
918
0
                ce32 = dest.encodeOneCE(ce, errorCode);
919
0
            }
920
0
        } else {
921
0
            int32_t tag = Collation::tagFromCE32(ce32);
922
0
            if(tag == Collation::EXPANSION32_TAG) {
923
0
                const uint32_t *srcCE32s = reinterpret_cast<uint32_t *>(src.ce32s.getBuffer());
924
0
                srcCE32s += Collation::indexFromCE32(ce32);
925
0
                int32_t length = Collation::lengthFromCE32(ce32);
926
0
                // Inspect the source CE32s. Just copy them if none are modified.
927
0
                // Otherwise copy to modifiedCEs, with modifications.
928
0
                UBool isModified = FALSE;
929
0
                for(int32_t i = 0; i < length; ++i) {
930
0
                    ce32 = srcCE32s[i];
931
0
                    int64_t ce;
932
0
                    if(Collation::isSpecialCE32(ce32) ||
933
0
                            (ce = modifier.modifyCE32(ce32)) == Collation::NO_CE) {
934
0
                        if(isModified) {
935
0
                            modifiedCEs[i] = Collation::ceFromCE32(ce32);
936
0
                        }
937
0
                    } else {
938
0
                        if(!isModified) {
939
0
                            for(int32_t j = 0; j < i; ++j) {
940
0
                                modifiedCEs[j] = Collation::ceFromCE32(srcCE32s[j]);
941
0
                            }
942
0
                            isModified = TRUE;
943
0
                        }
944
0
                        modifiedCEs[i] = ce;
945
0
                    }
946
0
                }
947
0
                if(isModified) {
948
0
                    ce32 = dest.encodeCEs(modifiedCEs, length, errorCode);
949
0
                } else {
950
0
                    ce32 = dest.encodeExpansion32(
951
0
                        reinterpret_cast<const int32_t *>(srcCE32s), length, errorCode);
952
0
                }
953
0
            } else if(tag == Collation::EXPANSION_TAG) {
954
0
                const int64_t *srcCEs = src.ce64s.getBuffer();
955
0
                srcCEs += Collation::indexFromCE32(ce32);
956
0
                int32_t length = Collation::lengthFromCE32(ce32);
957
0
                // Inspect the source CEs. Just copy them if none are modified.
958
0
                // Otherwise copy to modifiedCEs, with modifications.
959
0
                UBool isModified = FALSE;
960
0
                for(int32_t i = 0; i < length; ++i) {
961
0
                    int64_t srcCE = srcCEs[i];
962
0
                    int64_t ce = modifier.modifyCE(srcCE);
963
0
                    if(ce == Collation::NO_CE) {
964
0
                        if(isModified) {
965
0
                            modifiedCEs[i] = srcCE;
966
0
                        }
967
0
                    } else {
968
0
                        if(!isModified) {
969
0
                            for(int32_t j = 0; j < i; ++j) {
970
0
                                modifiedCEs[j] = srcCEs[j];
971
0
                            }
972
0
                            isModified = TRUE;
973
0
                        }
974
0
                        modifiedCEs[i] = ce;
975
0
                    }
976
0
                }
977
0
                if(isModified) {
978
0
                    ce32 = dest.encodeCEs(modifiedCEs, length, errorCode);
979
0
                } else {
980
0
                    ce32 = dest.encodeExpansion(srcCEs, length, errorCode);
981
0
                }
982
0
            } else if(tag == Collation::BUILDER_DATA_TAG) {
983
0
                // Copy the list of ConditionalCE32.
984
0
                ConditionalCE32 *cond = src.getConditionalCE32ForCE32(ce32);
985
0
                U_ASSERT(!cond->hasContext());
986
0
                int32_t destIndex = dest.addConditionalCE32(
987
0
                        cond->context, copyCE32(cond->ce32), errorCode);
988
0
                ce32 = CollationDataBuilder::makeBuilderContextCE32(destIndex);
989
0
                while(cond->next >= 0) {
990
0
                    cond = src.getConditionalCE32(cond->next);
991
0
                    ConditionalCE32 *prevDestCond = dest.getConditionalCE32(destIndex);
992
0
                    destIndex = dest.addConditionalCE32(
993
0
                            cond->context, copyCE32(cond->ce32), errorCode);
994
0
                    int32_t suffixStart = cond->prefixLength() + 1;
995
0
                    dest.unsafeBackwardSet.addAll(cond->context.tempSubString(suffixStart));
996
0
                    prevDestCond->next = destIndex;
997
0
                }
998
0
            } else {
999
0
                // Just copy long CEs and Latin mini expansions (and other expected values) as is,
1000
0
                // assuming that the modifier would not modify them.
1001
0
                U_ASSERT(tag == Collation::LONG_PRIMARY_TAG ||
1002
0
                        tag == Collation::LONG_SECONDARY_TAG ||
1003
0
                        tag == Collation::LATIN_EXPANSION_TAG ||
1004
0
                        tag == Collation::HANGUL_TAG);
1005
0
            }
1006
0
        }
1007
0
        return ce32;
1008
0
    }
1009
1010
    const CollationDataBuilder &src;
1011
    CollationDataBuilder &dest;
1012
    const CollationDataBuilder::CEModifier &modifier;
1013
    int64_t modifiedCEs[Collation::MAX_EXPANSION_LENGTH];
1014
    UErrorCode errorCode;
1015
};
1016
1017
U_CDECL_BEGIN
1018
1019
static UBool U_CALLCONV
1020
0
enumRangeForCopy(const void *context, UChar32 start, UChar32 end, uint32_t value) {
1021
0
    return
1022
0
        value == Collation::UNASSIGNED_CE32 || value == Collation::FALLBACK_CE32 ||
1023
0
        ((CopyHelper *)context)->copyRangeCE32(start, end, value);
1024
0
}
1025
1026
U_CDECL_END
1027
1028
void
1029
CollationDataBuilder::copyFrom(const CollationDataBuilder &src, const CEModifier &modifier,
1030
0
                               UErrorCode &errorCode) {
1031
0
    if(U_FAILURE(errorCode)) { return; }
1032
0
    if(trie == NULL || utrie2_isFrozen(trie)) {
1033
0
        errorCode = U_INVALID_STATE_ERROR;
1034
0
        return;
1035
0
    }
1036
0
    CopyHelper helper(src, *this, modifier, errorCode);
1037
0
    utrie2_enum(src.trie, NULL, enumRangeForCopy, &helper);
1038
0
    errorCode = helper.errorCode;
1039
0
    // Update the contextChars and the unsafeBackwardSet while copying,
1040
0
    // in case a character had conditional mappings in the source builder
1041
0
    // and they were removed later.
1042
0
    modified |= src.modified;
1043
0
}
1044
1045
void
1046
0
CollationDataBuilder::optimize(const UnicodeSet &set, UErrorCode &errorCode) {
1047
0
    if(U_FAILURE(errorCode) || set.isEmpty()) { return; }
1048
0
    UnicodeSetIterator iter(set);
1049
0
    while(iter.next() && !iter.isString()) {
1050
0
        UChar32 c = iter.getCodepoint();
1051
0
        uint32_t ce32 = utrie2_get32(trie, c);
1052
0
        if(ce32 == Collation::FALLBACK_CE32) {
1053
0
            ce32 = base->getFinalCE32(base->getCE32(c));
1054
0
            ce32 = copyFromBaseCE32(c, ce32, TRUE, errorCode);
1055
0
            utrie2_set32(trie, c, ce32, &errorCode);
1056
0
        }
1057
0
    }
1058
0
    modified = TRUE;
1059
0
}
1060
1061
void
1062
0
CollationDataBuilder::suppressContractions(const UnicodeSet &set, UErrorCode &errorCode) {
1063
0
    if(U_FAILURE(errorCode) || set.isEmpty()) { return; }
1064
0
    UnicodeSetIterator iter(set);
1065
0
    while(iter.next() && !iter.isString()) {
1066
0
        UChar32 c = iter.getCodepoint();
1067
0
        uint32_t ce32 = utrie2_get32(trie, c);
1068
0
        if(ce32 == Collation::FALLBACK_CE32) {
1069
0
            ce32 = base->getFinalCE32(base->getCE32(c));
1070
0
            if(Collation::ce32HasContext(ce32)) {
1071
0
                ce32 = copyFromBaseCE32(c, ce32, FALSE /* without context */, errorCode);
1072
0
                utrie2_set32(trie, c, ce32, &errorCode);
1073
0
            }
1074
0
        } else if(isBuilderContextCE32(ce32)) {
1075
0
            ce32 = getConditionalCE32ForCE32(ce32)->ce32;
1076
0
            // Simply abandon the list of ConditionalCE32.
1077
0
            // The caller will copy this builder in the end,
1078
0
            // eliminating unreachable data.
1079
0
            utrie2_set32(trie, c, ce32, &errorCode);
1080
0
            contextChars.remove(c);
1081
0
        }
1082
0
    }
1083
0
    modified = TRUE;
1084
0
}
1085
1086
UBool
1087
0
CollationDataBuilder::getJamoCE32s(uint32_t jamoCE32s[], UErrorCode &errorCode) {
1088
0
    if(U_FAILURE(errorCode)) { return FALSE; }
1089
0
    UBool anyJamoAssigned = base == NULL;  // always set jamoCE32s in the base data
1090
0
    UBool needToCopyFromBase = FALSE;
1091
0
    for(int32_t j = 0; j < CollationData::JAMO_CE32S_LENGTH; ++j) {  // Count across Jamo types.
1092
0
        UChar32 jamo = jamoCpFromIndex(j);
1093
0
        UBool fromBase = FALSE;
1094
0
        uint32_t ce32 = utrie2_get32(trie, jamo);
1095
0
        anyJamoAssigned |= Collation::isAssignedCE32(ce32);
1096
0
        // TODO: Try to prevent [optimize [Jamo]] from counting as anyJamoAssigned.
1097
0
        // (As of CLDR 24 [2013] the Korean tailoring does not optimize conjoining Jamo.)
1098
0
        if(ce32 == Collation::FALLBACK_CE32) {
1099
0
            fromBase = TRUE;
1100
0
            ce32 = base->getCE32(jamo);
1101
0
        }
1102
0
        if(Collation::isSpecialCE32(ce32)) {
1103
0
            switch(Collation::tagFromCE32(ce32)) {
1104
0
            case Collation::LONG_PRIMARY_TAG:
1105
0
            case Collation::LONG_SECONDARY_TAG:
1106
0
            case Collation::LATIN_EXPANSION_TAG:
1107
0
                // Copy the ce32 as-is.
1108
0
                break;
1109
0
            case Collation::EXPANSION32_TAG:
1110
0
            case Collation::EXPANSION_TAG:
1111
0
            case Collation::PREFIX_TAG:
1112
0
            case Collation::CONTRACTION_TAG:
1113
0
                if(fromBase) {
1114
0
                    // Defer copying until we know if anyJamoAssigned.
1115
0
                    ce32 = Collation::FALLBACK_CE32;
1116
0
                    needToCopyFromBase = TRUE;
1117
0
                }
1118
0
                break;
1119
0
            case Collation::IMPLICIT_TAG:
1120
0
                // An unassigned Jamo should only occur in tests with incomplete bases.
1121
0
                U_ASSERT(fromBase);
1122
0
                ce32 = Collation::FALLBACK_CE32;
1123
0
                needToCopyFromBase = TRUE;
1124
0
                break;
1125
0
            case Collation::OFFSET_TAG:
1126
0
                ce32 = getCE32FromOffsetCE32(fromBase, jamo, ce32);
1127
0
                break;
1128
0
            case Collation::FALLBACK_TAG:
1129
0
            case Collation::RESERVED_TAG_3:
1130
0
            case Collation::BUILDER_DATA_TAG:
1131
0
            case Collation::DIGIT_TAG:
1132
0
            case Collation::U0000_TAG:
1133
0
            case Collation::HANGUL_TAG:
1134
0
            case Collation::LEAD_SURROGATE_TAG:
1135
0
                errorCode = U_INTERNAL_PROGRAM_ERROR;
1136
0
                return FALSE;
1137
0
            }
1138
0
        }
1139
0
        jamoCE32s[j] = ce32;
1140
0
    }
1141
0
    if(anyJamoAssigned && needToCopyFromBase) {
1142
0
        for(int32_t j = 0; j < CollationData::JAMO_CE32S_LENGTH; ++j) {
1143
0
            if(jamoCE32s[j] == Collation::FALLBACK_CE32) {
1144
0
                UChar32 jamo = jamoCpFromIndex(j);
1145
0
                jamoCE32s[j] = copyFromBaseCE32(jamo, base->getCE32(jamo),
1146
0
                                                /*withContext=*/ TRUE, errorCode);
1147
0
            }
1148
0
        }
1149
0
    }
1150
0
    return anyJamoAssigned && U_SUCCESS(errorCode);
1151
0
}
1152
1153
void
1154
0
CollationDataBuilder::setDigitTags(UErrorCode &errorCode) {
1155
0
    UnicodeSet digits(UNICODE_STRING_SIMPLE("[:Nd:]"), errorCode);
1156
0
    if(U_FAILURE(errorCode)) { return; }
1157
0
    UnicodeSetIterator iter(digits);
1158
0
    while(iter.next()) {
1159
0
        U_ASSERT(!iter.isString());
1160
0
        UChar32 c = iter.getCodepoint();
1161
0
        uint32_t ce32 = utrie2_get32(trie, c);
1162
0
        if(ce32 != Collation::FALLBACK_CE32 && ce32 != Collation::UNASSIGNED_CE32) {
1163
0
            int32_t index = addCE32(ce32, errorCode);
1164
0
            if(U_FAILURE(errorCode)) { return; }
1165
0
            if(index > Collation::MAX_INDEX) {
1166
0
                errorCode = U_BUFFER_OVERFLOW_ERROR;
1167
0
                return;
1168
0
            }
1169
0
            ce32 = Collation::makeCE32FromTagIndexAndLength(
1170
0
                    Collation::DIGIT_TAG, index, u_charDigitValue(c));
1171
0
            utrie2_set32(trie, c, ce32, &errorCode);
1172
0
        }
1173
0
    }
1174
0
}
1175
1176
U_CDECL_BEGIN
1177
1178
static UBool U_CALLCONV
1179
0
enumRangeLeadValue(const void *context, UChar32 /*start*/, UChar32 /*end*/, uint32_t value) {
1180
0
    int32_t *pValue = (int32_t *)context;
1181
0
    if(value == Collation::UNASSIGNED_CE32) {
1182
0
        value = Collation::LEAD_ALL_UNASSIGNED;
1183
0
    } else if(value == Collation::FALLBACK_CE32) {
1184
0
        value = Collation::LEAD_ALL_FALLBACK;
1185
0
    } else {
1186
0
        *pValue = Collation::LEAD_MIXED;
1187
0
        return FALSE;
1188
0
    }
1189
0
    if(*pValue < 0) {
1190
0
        *pValue = (int32_t)value;
1191
0
    } else if(*pValue != (int32_t)value) {
1192
0
        *pValue = Collation::LEAD_MIXED;
1193
0
        return FALSE;
1194
0
    }
1195
0
    return TRUE;
1196
0
}
1197
1198
U_CDECL_END
1199
1200
void
1201
0
CollationDataBuilder::setLeadSurrogates(UErrorCode &errorCode) {
1202
0
    for(UChar lead = 0xd800; lead < 0xdc00; ++lead) {
1203
0
        int32_t value = -1;
1204
0
        utrie2_enumForLeadSurrogate(trie, lead, NULL, enumRangeLeadValue, &value);
1205
0
        utrie2_set32ForLeadSurrogateCodeUnit(
1206
0
            trie, lead,
1207
0
            Collation::makeCE32FromTagAndIndex(Collation::LEAD_SURROGATE_TAG, 0) | (uint32_t)value,
1208
0
            &errorCode);
1209
0
    }
1210
0
}
1211
1212
void
1213
0
CollationDataBuilder::build(CollationData &data, UErrorCode &errorCode) {
1214
0
    buildMappings(data, errorCode);
1215
0
    if(base != NULL) {
1216
0
        data.numericPrimary = base->numericPrimary;
1217
0
        data.compressibleBytes = base->compressibleBytes;
1218
0
        data.numScripts = base->numScripts;
1219
0
        data.scriptsIndex = base->scriptsIndex;
1220
0
        data.scriptStarts = base->scriptStarts;
1221
0
        data.scriptStartsLength = base->scriptStartsLength;
1222
0
    }
1223
0
    buildFastLatinTable(data, errorCode);
1224
0
}
1225
1226
void
1227
0
CollationDataBuilder::buildMappings(CollationData &data, UErrorCode &errorCode) {
1228
0
    if(U_FAILURE(errorCode)) { return; }
1229
0
    if(trie == NULL || utrie2_isFrozen(trie)) {
1230
0
        errorCode = U_INVALID_STATE_ERROR;
1231
0
        return;
1232
0
    }
1233
0
1234
0
    buildContexts(errorCode);
1235
0
1236
0
    uint32_t jamoCE32s[CollationData::JAMO_CE32S_LENGTH];
1237
0
    int32_t jamoIndex = -1;
1238
0
    if(getJamoCE32s(jamoCE32s, errorCode)) {
1239
0
        jamoIndex = ce32s.size();
1240
0
        for(int32_t i = 0; i < CollationData::JAMO_CE32S_LENGTH; ++i) {
1241
0
            ce32s.addElement((int32_t)jamoCE32s[i], errorCode);
1242
0
        }
1243
0
        // Small optimization: Use a bit in the Hangul ce32
1244
0
        // to indicate that none of the Jamo CE32s are isSpecialCE32()
1245
0
        // (as it should be in the root collator).
1246
0
        // It allows CollationIterator to avoid recursive function calls and per-Jamo tests.
1247
0
        // In order to still have good trie compression and keep this code simple,
1248
0
        // we only set this flag if a whole block of 588 Hangul syllables starting with
1249
0
        // a common leading consonant (Jamo L) has this property.
1250
0
        UBool isAnyJamoVTSpecial = FALSE;
1251
0
        for(int32_t i = Hangul::JAMO_L_COUNT; i < CollationData::JAMO_CE32S_LENGTH; ++i) {
1252
0
            if(Collation::isSpecialCE32(jamoCE32s[i])) {
1253
0
                isAnyJamoVTSpecial = TRUE;
1254
0
                break;
1255
0
            }
1256
0
        }
1257
0
        uint32_t hangulCE32 = Collation::makeCE32FromTagAndIndex(Collation::HANGUL_TAG, 0);
1258
0
        UChar32 c = Hangul::HANGUL_BASE;
1259
0
        for(int32_t i = 0; i < Hangul::JAMO_L_COUNT; ++i) {  // iterate over the Jamo L
1260
0
            uint32_t ce32 = hangulCE32;
1261
0
            if(!isAnyJamoVTSpecial && !Collation::isSpecialCE32(jamoCE32s[i])) {
1262
0
                ce32 |= Collation::HANGUL_NO_SPECIAL_JAMO;
1263
0
            }
1264
0
            UChar32 limit = c + Hangul::JAMO_VT_COUNT;
1265
0
            utrie2_setRange32(trie, c, limit - 1, ce32, TRUE, &errorCode);
1266
0
            c = limit;
1267
0
        }
1268
0
    } else {
1269
0
        // Copy the Hangul CE32s from the base in blocks per Jamo L,
1270
0
        // assuming that HANGUL_NO_SPECIAL_JAMO is set or not set for whole blocks.
1271
0
        for(UChar32 c = Hangul::HANGUL_BASE; c < Hangul::HANGUL_LIMIT;) {
1272
0
            uint32_t ce32 = base->getCE32(c);
1273
0
            U_ASSERT(Collation::hasCE32Tag(ce32, Collation::HANGUL_TAG));
1274
0
            UChar32 limit = c + Hangul::JAMO_VT_COUNT;
1275
0
            utrie2_setRange32(trie, c, limit - 1, ce32, TRUE, &errorCode);
1276
0
            c = limit;
1277
0
        }
1278
0
    }
1279
0
1280
0
    setDigitTags(errorCode);
1281
0
    setLeadSurrogates(errorCode);
1282
0
1283
0
    // For U+0000, move its normal ce32 into CE32s[0] and set U0000_TAG.
1284
0
    ce32s.setElementAt((int32_t)utrie2_get32(trie, 0), 0);
1285
0
    utrie2_set32(trie, 0, Collation::makeCE32FromTagAndIndex(Collation::U0000_TAG, 0), &errorCode);
1286
0
1287
0
    utrie2_freeze(trie, UTRIE2_32_VALUE_BITS, &errorCode);
1288
0
    if(U_FAILURE(errorCode)) { return; }
1289
0
1290
0
    // Mark each lead surrogate as "unsafe"
1291
0
    // if any of its 1024 associated supplementary code points is "unsafe".
1292
0
    UChar32 c = 0x10000;
1293
0
    for(UChar lead = 0xd800; lead < 0xdc00; ++lead, c += 0x400) {
1294
0
        if(unsafeBackwardSet.containsSome(c, c + 0x3ff)) {
1295
0
            unsafeBackwardSet.add(lead);
1296
0
        }
1297
0
    }
1298
0
    unsafeBackwardSet.freeze();
1299
0
1300
0
    data.trie = trie;
1301
0
    data.ce32s = reinterpret_cast<const uint32_t *>(ce32s.getBuffer());
1302
0
    data.ces = ce64s.getBuffer();
1303
0
    data.contexts = contexts.getBuffer();
1304
0
1305
0
    data.ce32sLength = ce32s.size();
1306
0
    data.cesLength = ce64s.size();
1307
0
    data.contextsLength = contexts.length();
1308
0
1309
0
    data.base = base;
1310
0
    if(jamoIndex >= 0) {
1311
0
        data.jamoCE32s = data.ce32s + jamoIndex;
1312
0
    } else {
1313
0
        data.jamoCE32s = base->jamoCE32s;
1314
0
    }
1315
0
    data.unsafeBackwardSet = &unsafeBackwardSet;
1316
0
}
1317
1318
void
1319
0
CollationDataBuilder::clearContexts() {
1320
0
    contexts.remove();
1321
0
    UnicodeSetIterator iter(contextChars);
1322
0
    while(iter.next()) {
1323
0
        U_ASSERT(!iter.isString());
1324
0
        uint32_t ce32 = utrie2_get32(trie, iter.getCodepoint());
1325
0
        U_ASSERT(isBuilderContextCE32(ce32));
1326
0
        getConditionalCE32ForCE32(ce32)->builtCE32 = Collation::NO_CE32;
1327
0
    }
1328
0
}
1329
1330
void
1331
0
CollationDataBuilder::buildContexts(UErrorCode &errorCode) {
1332
0
    if(U_FAILURE(errorCode)) { return; }
1333
0
    // Ignore abandoned lists and the cached builtCE32,
1334
0
    // and build all contexts from scratch.
1335
0
    contexts.remove();
1336
0
    UnicodeSetIterator iter(contextChars);
1337
0
    while(U_SUCCESS(errorCode) && iter.next()) {
1338
0
        U_ASSERT(!iter.isString());
1339
0
        UChar32 c = iter.getCodepoint();
1340
0
        uint32_t ce32 = utrie2_get32(trie, c);
1341
0
        if(!isBuilderContextCE32(ce32)) {
1342
0
            // Impossible: No context data for c in contextChars.
1343
0
            errorCode = U_INTERNAL_PROGRAM_ERROR;
1344
0
            return;
1345
0
        }
1346
0
        ConditionalCE32 *cond = getConditionalCE32ForCE32(ce32);
1347
0
        ce32 = buildContext(cond, errorCode);
1348
0
        utrie2_set32(trie, c, ce32, &errorCode);
1349
0
    }
1350
0
}
1351
1352
uint32_t
1353
0
CollationDataBuilder::buildContext(ConditionalCE32 *head, UErrorCode &errorCode) {
1354
0
    if(U_FAILURE(errorCode)) { return 0; }
1355
0
    // The list head must have no context.
1356
0
    U_ASSERT(!head->hasContext());
1357
0
    // The list head must be followed by one or more nodes that all do have context.
1358
0
    U_ASSERT(head->next >= 0);
1359
0
    UCharsTrieBuilder prefixBuilder(errorCode);
1360
0
    UCharsTrieBuilder contractionBuilder(errorCode);
1361
0
    for(ConditionalCE32 *cond = head;; cond = getConditionalCE32(cond->next)) {
1362
0
        // After the list head, the prefix or suffix can be empty, but not both.
1363
0
        U_ASSERT(cond == head || cond->hasContext());
1364
0
        int32_t prefixLength = cond->prefixLength();
1365
0
        UnicodeString prefix(cond->context, 0, prefixLength + 1);
1366
0
        // Collect all contraction suffixes for one prefix.
1367
0
        ConditionalCE32 *firstCond = cond;
1368
0
        ConditionalCE32 *lastCond = cond;
1369
0
        while(cond->next >= 0 &&
1370
0
                (cond = getConditionalCE32(cond->next))->context.startsWith(prefix)) {
1371
0
            lastCond = cond;
1372
0
        }
1373
0
        uint32_t ce32;
1374
0
        int32_t suffixStart = prefixLength + 1;  // == prefix.length()
1375
0
        if(lastCond->context.length() == suffixStart) {
1376
0
            // One prefix without contraction suffix.
1377
0
            U_ASSERT(firstCond == lastCond);
1378
0
            ce32 = lastCond->ce32;
1379
0
            cond = lastCond;
1380
0
        } else {
1381
0
            // Build the contractions trie.
1382
0
            contractionBuilder.clear();
1383
0
            // Entry for an empty suffix, to be stored before the trie.
1384
0
            uint32_t emptySuffixCE32 = 0;
1385
0
            uint32_t flags = 0;
1386
0
            if(firstCond->context.length() == suffixStart) {
1387
0
                // There is a mapping for the prefix and the single character c. (p|c)
1388
0
                // If no other suffix matches, then we return this value.
1389
0
                emptySuffixCE32 = firstCond->ce32;
1390
0
                cond = getConditionalCE32(firstCond->next);
1391
0
            } else {
1392
0
                // There is no mapping for the prefix and just the single character.
1393
0
                // (There is no p|c, only p|cd, p|ce etc.)
1394
0
                flags |= Collation::CONTRACT_SINGLE_CP_NO_MATCH;
1395
0
                // When the prefix matches but none of the prefix-specific suffixes,
1396
0
                // then we fall back to the mappings with the next-longest prefix,
1397
0
                // and ultimately to mappings with no prefix.
1398
0
                // Each fallback might be another set of contractions.
1399
0
                // For example, if there are mappings for ch, p|cd, p|ce, but not for p|c,
1400
0
                // then in text "pch" we find the ch contraction.
1401
0
                for(cond = head;; cond = getConditionalCE32(cond->next)) {
1402
0
                    int32_t length = cond->prefixLength();
1403
0
                    if(length == prefixLength) { break; }
1404
0
                    if(cond->defaultCE32 != Collation::NO_CE32 &&
1405
0
                            (length==0 || prefix.endsWith(cond->context, 1, length))) {
1406
0
                        emptySuffixCE32 = cond->defaultCE32;
1407
0
                    }
1408
0
                }
1409
0
                cond = firstCond;
1410
0
            }
1411
0
            // Optimization: Set a flag when
1412
0
            // the first character of every contraction suffix has lccc!=0.
1413
0
            // Short-circuits contraction matching when a normal letter follows.
1414
0
            flags |= Collation::CONTRACT_NEXT_CCC;
1415
0
            // Add all of the non-empty suffixes into the contraction trie.
1416
0
            for(;;) {
1417
0
                UnicodeString suffix(cond->context, suffixStart);
1418
0
                uint16_t fcd16 = nfcImpl.getFCD16(suffix.char32At(0));
1419
0
                if(fcd16 <= 0xff) {
1420
0
                    flags &= ~Collation::CONTRACT_NEXT_CCC;
1421
0
                }
1422
0
                fcd16 = nfcImpl.getFCD16(suffix.char32At(suffix.length() - 1));
1423
0
                if(fcd16 > 0xff) {
1424
0
                    // The last suffix character has lccc!=0, allowing for discontiguous contractions.
1425
0
                    flags |= Collation::CONTRACT_TRAILING_CCC;
1426
0
                }
1427
0
                contractionBuilder.add(suffix, (int32_t)cond->ce32, errorCode);
1428
0
                if(cond == lastCond) { break; }
1429
0
                cond = getConditionalCE32(cond->next);
1430
0
            }
1431
0
            int32_t index = addContextTrie(emptySuffixCE32, contractionBuilder, errorCode);
1432
0
            if(U_FAILURE(errorCode)) { return 0; }
1433
0
            if(index > Collation::MAX_INDEX) {
1434
0
                errorCode = U_BUFFER_OVERFLOW_ERROR;
1435
0
                return 0;
1436
0
            }
1437
0
            ce32 = Collation::makeCE32FromTagAndIndex(Collation::CONTRACTION_TAG, index) | flags;
1438
0
        }
1439
0
        U_ASSERT(cond == lastCond);
1440
0
        firstCond->defaultCE32 = ce32;
1441
0
        if(prefixLength == 0) {
1442
0
            if(cond->next < 0) {
1443
0
                // No non-empty prefixes, only contractions.
1444
0
                return ce32;
1445
0
            }
1446
0
        } else {
1447
0
            prefix.remove(0, 1);  // Remove the length unit.
1448
0
            prefix.reverse();
1449
0
            prefixBuilder.add(prefix, (int32_t)ce32, errorCode);
1450
0
            if(cond->next < 0) { break; }
1451
0
        }
1452
0
    }
1453
0
    U_ASSERT(head->defaultCE32 != Collation::NO_CE32);
1454
0
    int32_t index = addContextTrie(head->defaultCE32, prefixBuilder, errorCode);
1455
0
    if(U_FAILURE(errorCode)) { return 0; }
1456
0
    if(index > Collation::MAX_INDEX) {
1457
0
        errorCode = U_BUFFER_OVERFLOW_ERROR;
1458
0
        return 0;
1459
0
    }
1460
0
    return Collation::makeCE32FromTagAndIndex(Collation::PREFIX_TAG, index);
1461
0
}
1462
1463
int32_t
1464
CollationDataBuilder::addContextTrie(uint32_t defaultCE32, UCharsTrieBuilder &trieBuilder,
1465
0
                                     UErrorCode &errorCode) {
1466
0
    UnicodeString context;
1467
0
    context.append((UChar)(defaultCE32 >> 16)).append((UChar)defaultCE32);
1468
0
    UnicodeString trieString;
1469
0
    context.append(trieBuilder.buildUnicodeString(USTRINGTRIE_BUILD_SMALL, trieString, errorCode));
1470
0
    if(U_FAILURE(errorCode)) { return -1; }
1471
0
    int32_t index = contexts.indexOf(context);
1472
0
    if(index < 0) {
1473
0
        index = contexts.length();
1474
0
        contexts.append(context);
1475
0
    }
1476
0
    return index;
1477
0
}
1478
1479
void
1480
0
CollationDataBuilder::buildFastLatinTable(CollationData &data, UErrorCode &errorCode) {
1481
0
    if(U_FAILURE(errorCode) || !fastLatinEnabled) { return; }
1482
0
1483
0
    delete fastLatinBuilder;
1484
0
    fastLatinBuilder = new CollationFastLatinBuilder(errorCode);
1485
0
    if(fastLatinBuilder == NULL) {
1486
0
        errorCode = U_MEMORY_ALLOCATION_ERROR;
1487
0
        return;
1488
0
    }
1489
0
    if(fastLatinBuilder->forData(data, errorCode)) {
1490
0
        const uint16_t *table = fastLatinBuilder->getTable();
1491
0
        int32_t length = fastLatinBuilder->lengthOfTable();
1492
0
        if(base != NULL && length == base->fastLatinTableLength &&
1493
0
                uprv_memcmp(table, base->fastLatinTable, length * 2) == 0) {
1494
0
            // Same fast Latin table as in the base, use that one instead.
1495
0
            delete fastLatinBuilder;
1496
0
            fastLatinBuilder = NULL;
1497
0
            table = base->fastLatinTable;
1498
0
        }
1499
0
        data.fastLatinTable = table;
1500
0
        data.fastLatinTableLength = length;
1501
0
    } else {
1502
0
        delete fastLatinBuilder;
1503
0
        fastLatinBuilder = NULL;
1504
0
    }
1505
0
}
1506
1507
int32_t
1508
0
CollationDataBuilder::getCEs(const UnicodeString &s, int64_t ces[], int32_t cesLength) {
1509
0
    return getCEs(s, 0, ces, cesLength);
1510
0
}
1511
1512
int32_t
1513
CollationDataBuilder::getCEs(const UnicodeString &prefix, const UnicodeString &s,
1514
0
                             int64_t ces[], int32_t cesLength) {
1515
0
    int32_t prefixLength = prefix.length();
1516
0
    if(prefixLength == 0) {
1517
0
        return getCEs(s, 0, ces, cesLength);
1518
0
    } else {
1519
0
        return getCEs(prefix + s, prefixLength, ces, cesLength);
1520
0
    }
1521
0
}
1522
1523
int32_t
1524
CollationDataBuilder::getCEs(const UnicodeString &s, int32_t start,
1525
0
                             int64_t ces[], int32_t cesLength) {
1526
0
    if(collIter == NULL) {
1527
0
        collIter = new DataBuilderCollationIterator(*this);
1528
0
        if(collIter == NULL) { return 0; }
1529
0
    }
1530
0
    return collIter->fetchCEs(s, start, ces, cesLength);
1531
0
}
1532
1533
U_NAMESPACE_END
1534
1535
#endif  // !UCONFIG_NO_COLLATION