/src/icu/icu4c/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 | 3.97k | 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 | 3.99k | : context(), |
56 | 3.99k | ce32(0), defaultCE32(Collation::NO_CE32), builtCE32(Collation::NO_CE32), |
57 | 3.99k | next(-1) {} |
58 | | ConditionalCE32(const UnicodeString &ct, uint32_t ce) |
59 | 734k | : context(ct), |
60 | 734k | ce32(ce), defaultCE32(Collation::NO_CE32), builtCE32(Collation::NO_CE32), |
61 | 734k | next(-1) {} |
62 | | |
63 | 0 | inline UBool hasContext() const { return context.length() > 1; } |
64 | 88.6M | 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 | | * All of these builtCE32 are invalidated by clearContexts(), |
90 | | * via incrementing the contextsEra. |
91 | | */ |
92 | | uint32_t builtCE32; |
93 | | /** |
94 | | * The "era" of building intermediate contexts when the above builtCE32 was set. |
95 | | * When the array of cached, temporary contexts overflows, then clearContexts() |
96 | | * removes them all and invalidates the builtCE32 that used to point to built tries. |
97 | | */ |
98 | | int32_t era = -1; |
99 | | /** |
100 | | * Index of the next ConditionalCE32. |
101 | | * Negative for the end of the list. |
102 | | */ |
103 | | int32_t next; |
104 | | // Note: We could create a separate class for all of the contextual mappings for |
105 | | // a code point, with the builtCE32, the era, and a list of the actual mappings. |
106 | | // The class that represents one mapping would then not need to |
107 | | // store those fields in each element. |
108 | | }; |
109 | | |
110 | | U_CDECL_BEGIN |
111 | | |
112 | | void U_CALLCONV |
113 | 734k | uprv_deleteConditionalCE32(void *obj) { |
114 | 734k | delete static_cast<ConditionalCE32 *>(obj); |
115 | 734k | } |
116 | | |
117 | | U_CDECL_END |
118 | | |
119 | | /** |
120 | | * Build-time collation element and character iterator. |
121 | | * Uses the runtime CollationIterator for fetching CEs for a string |
122 | | * but reads from the builder's unfinished data structures. |
123 | | * In particular, this class reads from the unfinished trie |
124 | | * and has to avoid CollationIterator::nextCE() and redirect other |
125 | | * calls to data->getCE32() and data->getCE32FromSupplementary(). |
126 | | * |
127 | | * We do this so that we need not implement the collation algorithm |
128 | | * again for the builder and make it behave exactly like the runtime code. |
129 | | * That would be more difficult to test and maintain than this indirection. |
130 | | * |
131 | | * Some CE32 tags (for example, the DIGIT_TAG) do not occur in the builder data, |
132 | | * so the data accesses from those code paths need not be modified. |
133 | | * |
134 | | * This class iterates directly over whole code points |
135 | | * so that the CollationIterator does not need the finished trie |
136 | | * for handling the LEAD_SURROGATE_TAG. |
137 | | */ |
138 | | class DataBuilderCollationIterator : public CollationIterator { |
139 | | public: |
140 | | DataBuilderCollationIterator(CollationDataBuilder &b); |
141 | | |
142 | | virtual ~DataBuilderCollationIterator(); |
143 | | |
144 | | int32_t fetchCEs(const UnicodeString &str, int32_t start, int64_t ces[], int32_t cesLength); |
145 | | |
146 | | virtual void resetToOffset(int32_t newOffset) override; |
147 | | virtual int32_t getOffset() const override; |
148 | | |
149 | | virtual UChar32 nextCodePoint(UErrorCode &errorCode) override; |
150 | | virtual UChar32 previousCodePoint(UErrorCode &errorCode) override; |
151 | | |
152 | | protected: |
153 | | virtual void forwardNumCodePoints(int32_t num, UErrorCode &errorCode) override; |
154 | | virtual void backwardNumCodePoints(int32_t num, UErrorCode &errorCode) override; |
155 | | |
156 | | virtual uint32_t getDataCE32(UChar32 c) const override; |
157 | | virtual uint32_t getCE32FromBuilderData(uint32_t ce32, UErrorCode &errorCode) override; |
158 | | |
159 | | CollationDataBuilder &builder; |
160 | | CollationData builderData; |
161 | | uint32_t jamoCE32s[CollationData::JAMO_CE32S_LENGTH]; |
162 | | const UnicodeString *s; |
163 | | int32_t pos; |
164 | | }; |
165 | | |
166 | | DataBuilderCollationIterator::DataBuilderCollationIterator(CollationDataBuilder &b) |
167 | 5.70k | : CollationIterator(&builderData, /*numeric=*/ false), |
168 | 5.70k | builder(b), builderData(b.nfcImpl), |
169 | 5.70k | s(nullptr), pos(0) { |
170 | 5.70k | builderData.base = builder.base; |
171 | | // Set all of the jamoCE32s[] to indirection CE32s. |
172 | 388k | for(int32_t j = 0; j < CollationData::JAMO_CE32S_LENGTH; ++j) { // Count across Jamo types. |
173 | 382k | UChar32 jamo = CollationDataBuilder::jamoCpFromIndex(j); |
174 | 382k | jamoCE32s[j] = Collation::makeCE32FromTagAndIndex(Collation::BUILDER_DATA_TAG, jamo) | |
175 | 382k | CollationDataBuilder::IS_BUILDER_JAMO_CE32; |
176 | 382k | } |
177 | 5.70k | builderData.jamoCE32s = jamoCE32s; |
178 | 5.70k | } |
179 | | |
180 | 5.70k | DataBuilderCollationIterator::~DataBuilderCollationIterator() {} |
181 | | |
182 | | int32_t |
183 | | DataBuilderCollationIterator::fetchCEs(const UnicodeString &str, int32_t start, |
184 | 26.7M | int64_t ces[], int32_t cesLength) { |
185 | | // Set the pointers each time, in case they changed due to reallocation. |
186 | 26.7M | builderData.ce32s = reinterpret_cast<const uint32_t *>(builder.ce32s.getBuffer()); |
187 | 26.7M | builderData.ces = builder.ce64s.getBuffer(); |
188 | 26.7M | builderData.contexts = builder.contexts.getBuffer(); |
189 | | // Modified copy of CollationIterator::nextCE() and CollationIterator::nextCEFromCE32(). |
190 | 26.7M | reset(); |
191 | 26.7M | s = &str; |
192 | 26.7M | pos = start; |
193 | 26.7M | UErrorCode errorCode = U_ZERO_ERROR; |
194 | 71.8M | while(U_SUCCESS(errorCode) && pos < s->length()) { |
195 | | // No need to keep all CEs in the iterator buffer. |
196 | 45.1M | clearCEs(); |
197 | 45.1M | UChar32 c = s->char32At(pos); |
198 | 45.1M | pos += U16_LENGTH(c); |
199 | 45.1M | uint32_t ce32 = utrie2_get32(builder.trie, c); |
200 | 45.1M | const CollationData *d; |
201 | 45.1M | if(ce32 == Collation::FALLBACK_CE32) { |
202 | 37.0M | d = builder.base; |
203 | 37.0M | ce32 = builder.base->getCE32(c); |
204 | 37.0M | } else { |
205 | 8.05M | d = &builderData; |
206 | 8.05M | } |
207 | 45.1M | appendCEsFromCE32(d, c, ce32, /*forward=*/ true, errorCode); |
208 | 45.1M | U_ASSERT(U_SUCCESS(errorCode)); |
209 | 109M | for(int32_t i = 0; i < getCEsLength(); ++i) { |
210 | 64.0M | int64_t ce = getCE(i); |
211 | 64.0M | if(ce != 0) { |
212 | 62.9M | if(cesLength < Collation::MAX_EXPANSION_LENGTH) { |
213 | 50.7M | ces[cesLength] = ce; |
214 | 50.7M | } |
215 | 62.9M | ++cesLength; |
216 | 62.9M | } |
217 | 64.0M | } |
218 | 45.1M | } |
219 | 26.7M | return cesLength; |
220 | 26.7M | } |
221 | | |
222 | | void |
223 | 0 | DataBuilderCollationIterator::resetToOffset(int32_t newOffset) { |
224 | 0 | reset(); |
225 | 0 | pos = newOffset; |
226 | 0 | } |
227 | | |
228 | | int32_t |
229 | 0 | DataBuilderCollationIterator::getOffset() const { |
230 | 0 | return pos; |
231 | 0 | } |
232 | | |
233 | | UChar32 |
234 | 16.0M | DataBuilderCollationIterator::nextCodePoint(UErrorCode & /*errorCode*/) { |
235 | 16.0M | if(pos == s->length()) { |
236 | 352k | return U_SENTINEL; |
237 | 352k | } |
238 | 15.6M | UChar32 c = s->char32At(pos); |
239 | 15.6M | pos += U16_LENGTH(c); |
240 | 15.6M | return c; |
241 | 16.0M | } |
242 | | |
243 | | UChar32 |
244 | 3.47M | DataBuilderCollationIterator::previousCodePoint(UErrorCode & /*errorCode*/) { |
245 | 3.47M | if(pos == 0) { |
246 | 41.7k | return U_SENTINEL; |
247 | 41.7k | } |
248 | 3.42M | UChar32 c = s->char32At(pos - 1); |
249 | 3.42M | pos -= U16_LENGTH(c); |
250 | 3.42M | return c; |
251 | 3.47M | } |
252 | | |
253 | | void |
254 | 1.14M | DataBuilderCollationIterator::forwardNumCodePoints(int32_t num, UErrorCode & /*errorCode*/) { |
255 | 1.14M | pos = s->moveIndex32(pos, num); |
256 | 1.14M | } |
257 | | |
258 | | void |
259 | 2.06M | DataBuilderCollationIterator::backwardNumCodePoints(int32_t num, UErrorCode & /*errorCode*/) { |
260 | 2.06M | pos = s->moveIndex32(pos, -num); |
261 | 2.06M | } |
262 | | |
263 | | uint32_t |
264 | 95.6k | DataBuilderCollationIterator::getDataCE32(UChar32 c) const { |
265 | 95.6k | return utrie2_get32(builder.trie, c); |
266 | 95.6k | } |
267 | | |
268 | | uint32_t |
269 | 2.61M | DataBuilderCollationIterator::getCE32FromBuilderData(uint32_t ce32, UErrorCode &errorCode) { |
270 | 2.61M | if (U_FAILURE(errorCode)) { return 0; } |
271 | 2.61M | U_ASSERT(Collation::hasCE32Tag(ce32, Collation::BUILDER_DATA_TAG)); |
272 | 2.61M | if((ce32 & CollationDataBuilder::IS_BUILDER_JAMO_CE32) != 0) { |
273 | 733k | UChar32 jamo = Collation::indexFromCE32(ce32); |
274 | 733k | return utrie2_get32(builder.trie, jamo); |
275 | 1.88M | } else { |
276 | 1.88M | ConditionalCE32 *cond = builder.getConditionalCE32ForCE32(ce32); |
277 | 1.88M | if (cond == nullptr) { |
278 | 0 | errorCode = U_INTERNAL_PROGRAM_ERROR; |
279 | | // TODO: ICU-21531 figure out why this happens. |
280 | 0 | return 0; |
281 | 0 | } |
282 | 1.88M | if(cond->builtCE32 == Collation::NO_CE32 || cond->era != builder.contextsEra) { |
283 | | // Build the context-sensitive mappings into their runtime form and cache the result. |
284 | 416k | cond->builtCE32 = builder.buildContext(cond, errorCode); |
285 | 416k | if(errorCode == U_BUFFER_OVERFLOW_ERROR) { |
286 | 101 | errorCode = U_ZERO_ERROR; |
287 | 101 | builder.clearContexts(); |
288 | 101 | cond->builtCE32 = builder.buildContext(cond, errorCode); |
289 | 101 | } |
290 | 416k | cond->era = builder.contextsEra; |
291 | 416k | builderData.contexts = builder.contexts.getBuffer(); |
292 | 416k | } |
293 | 1.88M | return cond->builtCE32; |
294 | 1.88M | } |
295 | 2.61M | } |
296 | | |
297 | | // ------------------------------------------------------------------------- *** |
298 | | |
299 | | CollationDataBuilder::CollationDataBuilder(UBool icu4xMode, UErrorCode &errorCode) |
300 | 11.7k | : nfcImpl(*Normalizer2Factory::getNFCImpl(errorCode)), |
301 | 11.7k | base(nullptr), baseSettings(nullptr), |
302 | 11.7k | trie(nullptr), |
303 | 11.7k | ce32s(errorCode), ce64s(errorCode), conditionalCE32s(errorCode), |
304 | 11.7k | modified(false), |
305 | 11.7k | icu4xMode(icu4xMode), |
306 | 11.7k | fastLatinEnabled(false), fastLatinBuilder(nullptr), |
307 | 11.7k | collIter(nullptr) { |
308 | | // Reserve the first CE32 for U+0000. |
309 | 11.7k | if (!icu4xMode) { |
310 | 11.7k | ce32s.addElement(0, errorCode); |
311 | 11.7k | } |
312 | 11.7k | conditionalCE32s.setDeleter(uprv_deleteConditionalCE32); |
313 | 11.7k | } |
314 | | |
315 | 11.7k | CollationDataBuilder::~CollationDataBuilder() { |
316 | 11.7k | utrie2_close(trie); |
317 | 11.7k | delete fastLatinBuilder; |
318 | 11.7k | delete collIter; |
319 | 11.7k | } |
320 | | |
321 | | void |
322 | 11.7k | CollationDataBuilder::initForTailoring(const CollationData *b, UErrorCode &errorCode) { |
323 | 11.7k | if(U_FAILURE(errorCode)) { return; } |
324 | 11.7k | if(trie != nullptr) { |
325 | 0 | errorCode = U_INVALID_STATE_ERROR; |
326 | 0 | return; |
327 | 0 | } |
328 | 11.7k | if(b == nullptr) { |
329 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
330 | 0 | return; |
331 | 0 | } |
332 | 11.7k | base = b; |
333 | | |
334 | | // For a tailoring, the default is to fall back to the base. |
335 | | // For ICU4X, use the same value for fallback as for the default |
336 | | // to avoid having to have different blocks for the two. |
337 | 11.7k | trie = utrie2_open(Collation::FALLBACK_CE32, icu4xMode ? Collation::FALLBACK_CE32 : Collation::FFFD_CE32, &errorCode); |
338 | | |
339 | 11.7k | if (!icu4xMode) { |
340 | | // Set the Latin-1 letters block so that it is allocated first in the data array, |
341 | | // to try to improve locality of reference when sorting Latin-1 text. |
342 | | // Do not use utrie2_setRange32() since that will not actually allocate blocks |
343 | | // that are filled with the default value. |
344 | | // ASCII (0..7F) is already preallocated anyway. |
345 | 766k | for(UChar32 c = 0xc0; c <= 0xff; ++c) { |
346 | 754k | utrie2_set32(trie, c, Collation::FALLBACK_CE32, &errorCode); |
347 | 754k | } |
348 | | |
349 | | // Hangul syllables are not tailorable (except via tailoring Jamos). |
350 | | // Always set the Hangul tag to help performance. |
351 | | // Do this here, rather than in buildMappings(), |
352 | | // so that we see the HANGUL_TAG in various assertions. |
353 | 11.7k | uint32_t hangulCE32 = Collation::makeCE32FromTagAndIndex(Collation::HANGUL_TAG, 0); |
354 | 11.7k | utrie2_setRange32(trie, Hangul::HANGUL_BASE, Hangul::HANGUL_END, hangulCE32, true, &errorCode); |
355 | | |
356 | | // Copy the set contents but don't copy/clone the set as a whole because |
357 | | // that would copy the isFrozen state too. |
358 | 11.7k | unsafeBackwardSet.addAll(*b->unsafeBackwardSet); |
359 | 11.7k | } |
360 | | |
361 | 11.7k | if(U_FAILURE(errorCode)) { return; } |
362 | 11.7k | } |
363 | | |
364 | | UBool |
365 | | CollationDataBuilder::maybeSetPrimaryRange(UChar32 start, UChar32 end, |
366 | | uint32_t primary, int32_t step, |
367 | 0 | UErrorCode &errorCode) { |
368 | 0 | if(U_FAILURE(errorCode)) { return false; } |
369 | 0 | U_ASSERT(start <= end); |
370 | | // TODO: Do we need to check what values are currently set for start..end? |
371 | | // An offset range is worth it only if we can achieve an overlap between |
372 | | // adjacent UTrie2 blocks of 32 code points each. |
373 | | // An offset CE is also a little more expensive to look up and compute |
374 | | // than a simple CE. |
375 | | // If the range spans at least three UTrie2 block boundaries (> 64 code points), |
376 | | // then we take it. |
377 | | // If the range spans one or two block boundaries and there are |
378 | | // at least 4 code points on either side, then we take it. |
379 | | // (We could additionally require a minimum range length of, say, 16.) |
380 | 0 | int32_t blockDelta = (end >> 5) - (start >> 5); |
381 | 0 | if(2 <= step && step <= 0x7f && |
382 | 0 | (blockDelta >= 3 || |
383 | 0 | (blockDelta > 0 && (start & 0x1f) <= 0x1c && (end & 0x1f) >= 3))) { |
384 | 0 | int64_t dataCE = (static_cast<int64_t>(primary) << 32) | (start << 8) | step; |
385 | 0 | if(isCompressiblePrimary(primary)) { dataCE |= 0x80; } |
386 | 0 | int32_t index = addCE(dataCE, errorCode); |
387 | 0 | if(U_FAILURE(errorCode)) { return 0; } |
388 | 0 | if(index > Collation::MAX_INDEX) { |
389 | 0 | errorCode = U_BUFFER_OVERFLOW_ERROR; |
390 | 0 | return 0; |
391 | 0 | } |
392 | 0 | uint32_t offsetCE32 = Collation::makeCE32FromTagAndIndex(Collation::OFFSET_TAG, index); |
393 | 0 | utrie2_setRange32(trie, start, end, offsetCE32, true, &errorCode); |
394 | 0 | modified = true; |
395 | 0 | return true; |
396 | 0 | } else { |
397 | 0 | return false; |
398 | 0 | } |
399 | 0 | } |
400 | | |
401 | | uint32_t |
402 | | CollationDataBuilder::setPrimaryRangeAndReturnNext(UChar32 start, UChar32 end, |
403 | | uint32_t primary, int32_t step, |
404 | 0 | UErrorCode &errorCode) { |
405 | 0 | if(U_FAILURE(errorCode)) { return 0; } |
406 | 0 | UBool isCompressible = isCompressiblePrimary(primary); |
407 | 0 | if(maybeSetPrimaryRange(start, end, primary, step, errorCode)) { |
408 | 0 | return Collation::incThreeBytePrimaryByOffset(primary, isCompressible, |
409 | 0 | (end - start + 1) * step); |
410 | 0 | } else { |
411 | | // Short range: Set individual CE32s. |
412 | 0 | for(;;) { |
413 | 0 | utrie2_set32(trie, start, Collation::makeLongPrimaryCE32(primary), &errorCode); |
414 | 0 | ++start; |
415 | 0 | primary = Collation::incThreeBytePrimaryByOffset(primary, isCompressible, step); |
416 | 0 | if(start > end) { return primary; } |
417 | 0 | } |
418 | 0 | modified = true; |
419 | 0 | } |
420 | 0 | } |
421 | | |
422 | | uint32_t |
423 | 573k | CollationDataBuilder::getCE32FromOffsetCE32(UBool fromBase, UChar32 c, uint32_t ce32) const { |
424 | 573k | int32_t i = Collation::indexFromCE32(ce32); |
425 | 573k | int64_t dataCE = fromBase ? base->ces[i] : ce64s.elementAti(i); |
426 | 573k | uint32_t p = Collation::getThreeBytePrimaryForOffsetData(c, dataCE); |
427 | 573k | return Collation::makeLongPrimaryCE32(p); |
428 | 573k | } |
429 | | |
430 | | UBool |
431 | 0 | CollationDataBuilder::isCompressibleLeadByte(uint32_t b) const { |
432 | 0 | return base->isCompressibleLeadByte(b); |
433 | 0 | } |
434 | | |
435 | | UBool |
436 | 0 | CollationDataBuilder::isAssigned(UChar32 c) const { |
437 | 0 | return Collation::isAssignedCE32(utrie2_get32(trie, c)); |
438 | 0 | } |
439 | | |
440 | | uint32_t |
441 | 0 | CollationDataBuilder::getLongPrimaryIfSingleCE(UChar32 c) const { |
442 | 0 | uint32_t ce32 = utrie2_get32(trie, c); |
443 | 0 | if(Collation::isLongPrimaryCE32(ce32)) { |
444 | 0 | return Collation::primaryFromLongPrimaryCE32(ce32); |
445 | 0 | } else { |
446 | 0 | return 0; |
447 | 0 | } |
448 | 0 | } |
449 | | |
450 | | int64_t |
451 | 0 | CollationDataBuilder::getSingleCE(UChar32 c, UErrorCode &errorCode) const { |
452 | 0 | if(U_FAILURE(errorCode)) { return 0; } |
453 | | // Keep parallel with CollationData::getSingleCE(). |
454 | 0 | UBool fromBase = false; |
455 | 0 | uint32_t ce32 = utrie2_get32(trie, c); |
456 | 0 | if(ce32 == Collation::FALLBACK_CE32) { |
457 | 0 | fromBase = true; |
458 | 0 | ce32 = base->getCE32(c); |
459 | 0 | } |
460 | 0 | while(Collation::isSpecialCE32(ce32)) { |
461 | 0 | switch(Collation::tagFromCE32(ce32)) { |
462 | 0 | case Collation::LATIN_EXPANSION_TAG: |
463 | 0 | case Collation::BUILDER_DATA_TAG: |
464 | 0 | case Collation::PREFIX_TAG: |
465 | 0 | case Collation::CONTRACTION_TAG: |
466 | 0 | case Collation::HANGUL_TAG: |
467 | 0 | case Collation::LEAD_SURROGATE_TAG: |
468 | 0 | errorCode = U_UNSUPPORTED_ERROR; |
469 | 0 | return 0; |
470 | 0 | case Collation::FALLBACK_TAG: |
471 | 0 | case Collation::RESERVED_TAG_3: |
472 | 0 | errorCode = U_INTERNAL_PROGRAM_ERROR; |
473 | 0 | return 0; |
474 | 0 | case Collation::LONG_PRIMARY_TAG: |
475 | 0 | return Collation::ceFromLongPrimaryCE32(ce32); |
476 | 0 | case Collation::LONG_SECONDARY_TAG: |
477 | 0 | return Collation::ceFromLongSecondaryCE32(ce32); |
478 | 0 | case Collation::EXPANSION32_TAG: |
479 | 0 | if(Collation::lengthFromCE32(ce32) == 1) { |
480 | 0 | int32_t i = Collation::indexFromCE32(ce32); |
481 | 0 | ce32 = fromBase ? base->ce32s[i] : ce32s.elementAti(i); |
482 | 0 | break; |
483 | 0 | } else { |
484 | 0 | errorCode = U_UNSUPPORTED_ERROR; |
485 | 0 | return 0; |
486 | 0 | } |
487 | 0 | case Collation::EXPANSION_TAG: { |
488 | 0 | if(Collation::lengthFromCE32(ce32) == 1) { |
489 | 0 | int32_t i = Collation::indexFromCE32(ce32); |
490 | 0 | return fromBase ? base->ces[i] : ce64s.elementAti(i); |
491 | 0 | } else { |
492 | 0 | errorCode = U_UNSUPPORTED_ERROR; |
493 | 0 | return 0; |
494 | 0 | } |
495 | 0 | } |
496 | 0 | case Collation::DIGIT_TAG: |
497 | | // Fetch the non-numeric-collation CE32 and continue. |
498 | 0 | ce32 = ce32s.elementAti(Collation::indexFromCE32(ce32)); |
499 | 0 | break; |
500 | 0 | case Collation::U0000_TAG: |
501 | 0 | U_ASSERT(c == 0); |
502 | | // Fetch the normal ce32 for U+0000 and continue. |
503 | 0 | ce32 = fromBase ? base->ce32s[0] : ce32s.elementAti(0); |
504 | 0 | break; |
505 | 0 | case Collation::OFFSET_TAG: |
506 | 0 | ce32 = getCE32FromOffsetCE32(fromBase, c, ce32); |
507 | 0 | break; |
508 | 0 | case Collation::IMPLICIT_TAG: |
509 | 0 | return Collation::unassignedCEFromCodePoint(c); |
510 | 0 | } |
511 | 0 | } |
512 | 0 | return Collation::ceFromSimpleCE32(ce32); |
513 | 0 | } |
514 | | |
515 | | int32_t |
516 | 819k | CollationDataBuilder::addCE(int64_t ce, UErrorCode &errorCode) { |
517 | 819k | int32_t length = ce64s.size(); |
518 | 3.98G | for(int32_t i = 0; i < length; ++i) { |
519 | 3.98G | if(ce == ce64s.elementAti(i)) { return i; } |
520 | 3.98G | } |
521 | 743k | ce64s.addElement(ce, errorCode); |
522 | 743k | return length; |
523 | 819k | } |
524 | | |
525 | | int32_t |
526 | 51.4k | CollationDataBuilder::addCE32(uint32_t ce32, UErrorCode &errorCode) { |
527 | 51.4k | int32_t length = ce32s.size(); |
528 | 62.1M | for(int32_t i = 0; i < length; ++i) { |
529 | 62.1M | if (ce32 == static_cast<uint32_t>(ce32s.elementAti(i))) { return i; } |
530 | 62.1M | } |
531 | 38.8k | ce32s.addElement(static_cast<int32_t>(ce32), errorCode); |
532 | 38.8k | return length; |
533 | 51.4k | } |
534 | | |
535 | | int32_t |
536 | | CollationDataBuilder::addConditionalCE32(const UnicodeString &context, uint32_t ce32, |
537 | 734k | UErrorCode &errorCode) { |
538 | 734k | if(U_FAILURE(errorCode)) { return -1; } |
539 | 734k | U_ASSERT(!context.isEmpty()); |
540 | 734k | int32_t index = conditionalCE32s.size(); |
541 | 734k | if(index > Collation::MAX_INDEX) { |
542 | 0 | errorCode = U_BUFFER_OVERFLOW_ERROR; |
543 | 0 | return -1; |
544 | 0 | } |
545 | 734k | LocalPointer<ConditionalCE32> cond(new ConditionalCE32(context, ce32), errorCode); |
546 | 734k | conditionalCE32s.adoptElement(cond.orphan(), errorCode); |
547 | 734k | if(U_FAILURE(errorCode)) { |
548 | 0 | return -1; |
549 | 0 | } |
550 | 734k | return index; |
551 | 734k | } |
552 | | |
553 | | void |
554 | | CollationDataBuilder::add(const UnicodeString &prefix, const UnicodeString &s, |
555 | | const int64_t ces[], int32_t cesLength, |
556 | 0 | UErrorCode &errorCode) { |
557 | 0 | uint32_t ce32 = encodeCEs(ces, cesLength, errorCode); |
558 | 0 | addCE32(prefix, s, ce32, errorCode); |
559 | 0 | } |
560 | | |
561 | | void |
562 | | CollationDataBuilder::addCE32(const UnicodeString &prefix, const UnicodeString &s, |
563 | 6.83M | uint32_t ce32, UErrorCode &errorCode) { |
564 | 6.83M | if(U_FAILURE(errorCode)) { return; } |
565 | 6.83M | if(s.isEmpty()) { |
566 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
567 | 0 | return; |
568 | 0 | } |
569 | 6.83M | if(trie == nullptr || utrie2_isFrozen(trie)) { |
570 | 0 | errorCode = U_INVALID_STATE_ERROR; |
571 | 0 | return; |
572 | 0 | } |
573 | 6.83M | UChar32 c = s.char32At(0); |
574 | 6.83M | int32_t cLength = U16_LENGTH(c); |
575 | 6.83M | uint32_t oldCE32 = utrie2_get32(trie, c); |
576 | 6.83M | UBool hasContext = !prefix.isEmpty() || s.length() > cLength; |
577 | | |
578 | 6.83M | if (icu4xMode) { |
579 | 0 | if (base && c >= 0x1100 && c < 0x1200) { |
580 | | // Omit jamo tailorings. |
581 | | // TODO(https://github.com/unicode-org/icu4x/issues/1941). |
582 | 0 | } |
583 | 0 | const Normalizer2* nfdNormalizer = Normalizer2::getNFDInstance(errorCode); |
584 | 0 | UnicodeString sInNfd; |
585 | 0 | nfdNormalizer->normalize(s, sInNfd, errorCode); |
586 | 0 | if (s != sInNfd) { |
587 | | // s is not in NFD, so it cannot match in ICU4X, since ICU4X only |
588 | | // does NFD lookups. |
589 | | |
590 | | // As of Unicode 16 alpha, the cases that come here are: |
591 | | // |
592 | | // 1. The second character is a special decomposing Tibetan vowel |
593 | | // sign. These are OK to ignore in the precomposed form, since |
594 | | // the decomposed form is added also. |
595 | | // 2. Likewise for KIRAT RAI VOWEL SIGN AA followed by KIRAT RAI VOWEL SIGN AI |
596 | | // and other such cases. |
597 | | // For details see the normalization section of |
598 | | // https://www.unicode.org/review/pri497/pri497-background.html |
599 | | // 3. U+FDD1 followed by U+AC00 is a marker for the alphabetical |
600 | | // index feature of ICU4C, which at this time does not have |
601 | | // a counterpart in ICU4X. |
602 | 0 | return; |
603 | 0 | } |
604 | | |
605 | 0 | if (!prefix.isEmpty()) { |
606 | 0 | UnicodeString prefixInNfd; |
607 | 0 | nfdNormalizer->normalize(prefix, prefixInNfd, errorCode); |
608 | 0 | if (prefix != prefixInNfd) { |
609 | 0 | errorCode = U_UNSUPPORTED_ERROR; |
610 | 0 | return; |
611 | 0 | } |
612 | | |
613 | 0 | int32_t count = prefix.countChar32(); |
614 | 0 | if (count > 2) { |
615 | | // Prefix too long for ICU4X. |
616 | 0 | errorCode = U_UNSUPPORTED_ERROR; |
617 | 0 | return; |
618 | 0 | } |
619 | 0 | UChar32 utf32[4]; |
620 | 0 | int32_t len = prefix.toUTF32(utf32, 4, errorCode); |
621 | 0 | if (len != count) { |
622 | 0 | errorCode = U_INVALID_STATE_ERROR; |
623 | 0 | return; |
624 | 0 | } |
625 | 0 | UChar32 c = utf32[0]; |
626 | 0 | if (u_getCombiningClass(c)) { |
627 | | // Prefix must start with as starter for ICU4X. |
628 | 0 | errorCode = U_UNSUPPORTED_ERROR; |
629 | 0 | return; |
630 | 0 | } |
631 | | // XXX: Korean searchjl has jamo in prefix, so commenting out this |
632 | | // check for now. ICU4X currently ignores non-root jamo tables anyway. |
633 | | // searchjl was added in |
634 | | // https://unicode-org.atlassian.net/browse/CLDR-3560 |
635 | | // Contractions were changed to prefixes in |
636 | | // https://unicode-org.atlassian.net/browse/CLDR-6546 |
637 | | // |
638 | | // if ((c >= 0x1100 && c < 0x1200) || (c >= 0xAC00 && c < 0xD7A4)) { |
639 | | // errorCode = U_UNSUPPORTED_ERROR; |
640 | | // return; |
641 | | // } |
642 | 0 | if ((len > 1) && !(utf32[1] == 0x3099 || utf32[1] == 0x309A)) { |
643 | | // Second character in prefix, if present, must be a kana voicing mark for ICU4X. |
644 | 0 | errorCode = U_UNSUPPORTED_ERROR; |
645 | 0 | return; |
646 | 0 | } |
647 | 0 | } |
648 | | |
649 | 0 | if (s.length() > cLength) { |
650 | | // Check that there's no modern Hangul in contractions. |
651 | 0 | for (int32_t i = 0; i < s.length(); ++i) { |
652 | 0 | char16_t c = s.charAt(i); |
653 | 0 | if ((c >= 0x1100 && c < 0x1100 + 19) || (c >= 0x1161 && c < 0x1161 + 21) || (c >= 0x11A7 && c < 0x11A7 + 28) || (c >= 0xAC00 && c < 0xD7A4)) { |
654 | 0 | errorCode = U_UNSUPPORTED_ERROR; |
655 | 0 | return; |
656 | 0 | } |
657 | 0 | } |
658 | 0 | int32_t sCount = s.countChar32(); |
659 | 0 | UChar32 sUtf32[32]; |
660 | 0 | int32_t sLen = s.toUTF32(sUtf32, 32, errorCode); |
661 | 0 | if (sLen != sCount) { |
662 | | // If this error is ever reached, just increase the buffer |
663 | | // size above. |
664 | 0 | errorCode = U_UNSUPPORTED_ERROR; |
665 | 0 | return; |
666 | 0 | } |
667 | 0 | for (int32_t i = 1; i < sLen - 1; ++i) { |
668 | 0 | if (u_getCombiningClass(sUtf32[i]) == 0) { |
669 | 0 | contractionMiddleStarter.add(sUtf32[i]); |
670 | 0 | } |
671 | 0 | } |
672 | 0 | } |
673 | 0 | } |
674 | | |
675 | 6.83M | if(oldCE32 == Collation::FALLBACK_CE32) { |
676 | | // First tailoring for c. |
677 | | // If c has contextual base mappings or if we add a contextual mapping, |
678 | | // then copy the base mappings. |
679 | | // Otherwise we just override the base mapping. |
680 | 3.90M | uint32_t baseCE32 = base->getFinalCE32(base->getCE32(c)); |
681 | 3.90M | if(hasContext || Collation::ce32HasContext(baseCE32)) { |
682 | 31.2k | oldCE32 = copyFromBaseCE32(c, baseCE32, true, errorCode); |
683 | 31.2k | utrie2_set32(trie, c, oldCE32, &errorCode); |
684 | 31.2k | if(U_FAILURE(errorCode)) { return; } |
685 | 31.2k | } |
686 | 3.90M | } |
687 | 6.83M | if(!hasContext) { |
688 | | // No prefix, no contraction. |
689 | 6.42M | if(!isBuilderContextCE32(oldCE32)) { |
690 | 6.41M | utrie2_set32(trie, c, ce32, &errorCode); |
691 | 6.41M | } else { |
692 | 14.8k | ConditionalCE32 *cond = getConditionalCE32ForCE32(oldCE32); |
693 | 14.8k | cond->builtCE32 = Collation::NO_CE32; |
694 | 14.8k | cond->ce32 = ce32; |
695 | 14.8k | } |
696 | 6.42M | } else { |
697 | 411k | ConditionalCE32 *cond; |
698 | 411k | if(!isBuilderContextCE32(oldCE32)) { |
699 | | // Replace the simple oldCE32 with a builder context CE32 |
700 | | // pointing to a new ConditionalCE32 list head. |
701 | 33.1k | int32_t index = addConditionalCE32(UnicodeString(static_cast<char16_t>(0)), oldCE32, errorCode); |
702 | 33.1k | if(U_FAILURE(errorCode)) { return; } |
703 | 33.1k | uint32_t contextCE32 = makeBuilderContextCE32(index); |
704 | 33.1k | utrie2_set32(trie, c, contextCE32, &errorCode); |
705 | 33.1k | contextChars.add(c); |
706 | 33.1k | cond = getConditionalCE32(index); |
707 | 378k | } else { |
708 | 378k | cond = getConditionalCE32ForCE32(oldCE32); |
709 | 378k | cond->builtCE32 = Collation::NO_CE32; |
710 | 378k | } |
711 | 411k | UnicodeString suffix(s, cLength); |
712 | 411k | UnicodeString context(static_cast<char16_t>(prefix.length())); |
713 | 411k | context.append(prefix).append(suffix); |
714 | 411k | if (icu4xMode && !suffix.isEmpty() && !prefix.isEmpty()) { |
715 | | // ICU4X does not support the combination of prefix and contraction. |
716 | | // This combination is supported by LDML but does not occur in the |
717 | | // root or any tailorings in CLDR as of February 2025. |
718 | | // If support for this case becomes necessary, a practical change |
719 | | // would be allocating a flag on prefix ce32 and setting the |
720 | | // flag on a prefix ce32 if any ce32 that can be found under |
721 | | // the prefix ce32 (either the default or any UCharsTrie value) is |
722 | | // a contraction ce32 or if the prefix ce32 is the utrie2 value |
723 | | // for a character that is a starter that occurs in a middle |
724 | | // (neither first nor last) position in a contraction. |
725 | 0 | errorCode = U_UNSUPPORTED_ERROR; |
726 | 0 | return; |
727 | 0 | } |
728 | 411k | unsafeBackwardSet.addAll(suffix); |
729 | 20.4M | for(;;) { |
730 | | // invariant: context > cond->context |
731 | 20.4M | int32_t next = cond->next; |
732 | 20.4M | if(next < 0) { |
733 | | // Append a new ConditionalCE32 after cond. |
734 | 82.0k | int32_t index = addConditionalCE32(context, ce32, errorCode); |
735 | 82.0k | if(U_FAILURE(errorCode)) { return; } |
736 | 82.0k | cond->next = index; |
737 | 82.0k | break; |
738 | 82.0k | } |
739 | 20.4M | ConditionalCE32 *nextCond = getConditionalCE32(next); |
740 | 20.4M | int8_t cmp = context.compare(nextCond->context); |
741 | 20.4M | if(cmp < 0) { |
742 | | // Insert a new ConditionalCE32 between cond and nextCond. |
743 | 271k | int32_t index = addConditionalCE32(context, ce32, errorCode); |
744 | 271k | if(U_FAILURE(errorCode)) { return; } |
745 | 271k | cond->next = index; |
746 | 271k | getConditionalCE32(index)->next = next; |
747 | 271k | break; |
748 | 20.1M | } else if(cmp == 0) { |
749 | | // Same context as before, overwrite its ce32. |
750 | 57.2k | nextCond->ce32 = ce32; |
751 | 57.2k | break; |
752 | 57.2k | } |
753 | 20.0M | cond = nextCond; |
754 | 20.0M | } |
755 | 411k | } |
756 | 6.83M | modified = true; |
757 | 6.83M | } |
758 | | |
759 | | uint32_t |
760 | 14.7M | CollationDataBuilder::encodeOneCEAsCE32(int64_t ce) { |
761 | 14.7M | uint32_t p = static_cast<uint32_t>(ce >> 32); |
762 | 14.7M | uint32_t lower32 = static_cast<uint32_t>(ce); |
763 | 14.7M | uint32_t t = static_cast<uint32_t>(ce & 0xffff); |
764 | 14.7M | U_ASSERT((t & 0xc000) != 0xc000); // Impossible case bits 11 mark special CE32s. |
765 | 14.7M | if((ce & INT64_C(0xffff00ff00ff)) == 0) { |
766 | | // normal form ppppsstt |
767 | 11.3M | return p | (lower32 >> 16) | (t >> 8); |
768 | 11.3M | } else if((ce & INT64_C(0xffffffffff)) == Collation::COMMON_SEC_AND_TER_CE) { |
769 | | // long-primary form ppppppC1 |
770 | 2.02M | return Collation::makeLongPrimaryCE32(p); |
771 | 2.02M | } else if(p == 0 && (t & 0xff) == 0) { |
772 | | // long-secondary form ssssttC2 |
773 | 74.7k | return Collation::makeLongSecondaryCE32(lower32); |
774 | 74.7k | } |
775 | 1.29M | return Collation::NO_CE32; |
776 | 14.7M | } |
777 | | |
778 | | uint32_t |
779 | 5.95M | CollationDataBuilder::encodeOneCE(int64_t ce, UErrorCode &errorCode) { |
780 | | // Try to encode one CE as one CE32. |
781 | 5.95M | uint32_t ce32 = encodeOneCEAsCE32(ce); |
782 | 5.95M | if(ce32 != Collation::NO_CE32) { return ce32; } |
783 | 819k | int32_t index = addCE(ce, errorCode); |
784 | 819k | if(U_FAILURE(errorCode)) { return 0; } |
785 | 819k | if(index > Collation::MAX_INDEX) { |
786 | 0 | errorCode = U_BUFFER_OVERFLOW_ERROR; |
787 | 0 | return 0; |
788 | 0 | } |
789 | 819k | return Collation::makeCE32FromTagIndexAndLength(Collation::EXPANSION_TAG, index, 1); |
790 | 819k | } |
791 | | |
792 | | uint32_t |
793 | | CollationDataBuilder::encodeCEs(const int64_t ces[], int32_t cesLength, |
794 | 6.85M | UErrorCode &errorCode) { |
795 | 6.85M | if(U_FAILURE(errorCode)) { return 0; } |
796 | 6.85M | if(cesLength < 0 || cesLength > Collation::MAX_EXPANSION_LENGTH) { |
797 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
798 | 0 | return 0; |
799 | 0 | } |
800 | 6.85M | if(trie == nullptr || utrie2_isFrozen(trie)) { |
801 | 0 | errorCode = U_INVALID_STATE_ERROR; |
802 | 0 | return 0; |
803 | 0 | } |
804 | 6.85M | if(cesLength == 0) { |
805 | | // Convenience: We cannot map to nothing, but we can map to a completely ignorable CE. |
806 | | // Do this here so that callers need not do it. |
807 | 88.2k | return encodeOneCEAsCE32(0); |
808 | 6.76M | } else if(cesLength == 1) { |
809 | 4.55M | return encodeOneCE(ces[0], errorCode); |
810 | 4.55M | } else if(cesLength == 2 && !icu4xMode) { |
811 | | // Try to encode two CEs as one CE32. |
812 | | // Turn this off for ICU4X, because without the canonical closure |
813 | | // these are so rare that it doesn't make sense to spend a branch |
814 | | // on checking this tag when using the data. |
815 | 1.15M | int64_t ce0 = ces[0]; |
816 | 1.15M | int64_t ce1 = ces[1]; |
817 | 1.15M | uint32_t p0 = static_cast<uint32_t>(ce0 >> 32); |
818 | 1.15M | if((ce0 & INT64_C(0xffffffffff00ff)) == Collation::COMMON_SECONDARY_CE && |
819 | 1.15M | (ce1 & INT64_C(0xffffffff00ffffff)) == Collation::COMMON_TERTIARY_CE && |
820 | 1.15M | p0 != 0) { |
821 | | // Latin mini expansion |
822 | 10.0k | return |
823 | 10.0k | p0 | |
824 | 10.0k | ((static_cast<uint32_t>(ce0) & 0xff00u) << 8) | |
825 | 10.0k | static_cast<uint32_t>(ce1 >> 16) | |
826 | 10.0k | Collation::SPECIAL_CE32_LOW_BYTE | |
827 | 10.0k | Collation::LATIN_EXPANSION_TAG; |
828 | 10.0k | } |
829 | 1.15M | } |
830 | | // Try to encode two or more CEs as CE32s. |
831 | 2.20M | int32_t newCE32s[Collation::MAX_EXPANSION_LENGTH]; |
832 | 10.4M | for(int32_t i = 0;; ++i) { |
833 | 10.4M | if(i == cesLength) { |
834 | 1.73M | return encodeExpansion32(newCE32s, cesLength, errorCode); |
835 | 1.73M | } |
836 | 8.73M | uint32_t ce32 = encodeOneCEAsCE32(ces[i]); |
837 | 8.73M | if(ce32 == Collation::NO_CE32) { break; } |
838 | 8.26M | newCE32s[i] = static_cast<int32_t>(ce32); |
839 | 8.26M | } |
840 | 471k | return encodeExpansion(ces, cesLength, errorCode); |
841 | 2.20M | } |
842 | | |
843 | | uint32_t |
844 | 514k | CollationDataBuilder::encodeExpansion(const int64_t ces[], int32_t length, UErrorCode &errorCode) { |
845 | 514k | if(U_FAILURE(errorCode)) { return 0; } |
846 | | // See if this sequence of CEs has already been stored. |
847 | 514k | int64_t first = ces[0]; |
848 | 514k | int32_t ce64sMax = ce64s.size() - length; |
849 | 791M | for(int32_t i = 0; i <= ce64sMax; ++i) { |
850 | 791M | if(first == ce64s.elementAti(i)) { |
851 | 88.5M | if(i > Collation::MAX_INDEX) { |
852 | 0 | errorCode = U_BUFFER_OVERFLOW_ERROR; |
853 | 0 | return 0; |
854 | 0 | } |
855 | 472M | for(int32_t j = 1;; ++j) { |
856 | 472M | if(j == length) { |
857 | 252k | return Collation::makeCE32FromTagIndexAndLength( |
858 | 252k | Collation::EXPANSION_TAG, i, length); |
859 | 252k | } |
860 | 472M | if(ce64s.elementAti(i + j) != ces[j]) { break; } |
861 | 472M | } |
862 | 88.5M | } |
863 | 791M | } |
864 | | // Store the new sequence. |
865 | 262k | int32_t i = ce64s.size(); |
866 | 262k | if(i > Collation::MAX_INDEX) { |
867 | 0 | errorCode = U_BUFFER_OVERFLOW_ERROR; |
868 | 0 | return 0; |
869 | 0 | } |
870 | 1.94M | for(int32_t j = 0; j < length; ++j) { |
871 | 1.68M | ce64s.addElement(ces[j], errorCode); |
872 | 1.68M | } |
873 | 262k | return Collation::makeCE32FromTagIndexAndLength(Collation::EXPANSION_TAG, i, length); |
874 | 262k | } |
875 | | |
876 | | uint32_t |
877 | | CollationDataBuilder::encodeExpansion32(const int32_t newCE32s[], int32_t length, |
878 | 1.85M | UErrorCode &errorCode) { |
879 | 1.85M | if(U_FAILURE(errorCode)) { return 0; } |
880 | | // See if this sequence of CE32s has already been stored. |
881 | 1.85M | int32_t first = newCE32s[0]; |
882 | 1.85M | int32_t ce32sMax = ce32s.size() - length; |
883 | 4.14G | for(int32_t i = 0; i <= ce32sMax; ++i) { |
884 | 4.14G | if(first == ce32s.elementAti(i)) { |
885 | 328M | if(i > Collation::MAX_INDEX) { |
886 | 0 | errorCode = U_BUFFER_OVERFLOW_ERROR; |
887 | 0 | return 0; |
888 | 0 | } |
889 | 599M | for(int32_t j = 1;; ++j) { |
890 | 599M | if(j == length) { |
891 | 519k | return Collation::makeCE32FromTagIndexAndLength( |
892 | 519k | Collation::EXPANSION32_TAG, i, length); |
893 | 519k | } |
894 | 599M | if(ce32s.elementAti(i + j) != newCE32s[j]) { break; } |
895 | 599M | } |
896 | 328M | } |
897 | 4.14G | } |
898 | | // Store the new sequence. |
899 | 1.33M | int32_t i = ce32s.size(); |
900 | 1.33M | if(i > Collation::MAX_INDEX) { |
901 | 0 | errorCode = U_BUFFER_OVERFLOW_ERROR; |
902 | 0 | return 0; |
903 | 0 | } |
904 | 5.07M | for(int32_t j = 0; j < length; ++j) { |
905 | 3.73M | ce32s.addElement(newCE32s[j], errorCode); |
906 | 3.73M | } |
907 | 1.33M | return Collation::makeCE32FromTagIndexAndLength(Collation::EXPANSION32_TAG, i, length); |
908 | 1.33M | } |
909 | | |
910 | | uint32_t |
911 | | CollationDataBuilder::copyFromBaseCE32(UChar32 c, uint32_t ce32, UBool withContext, |
912 | 2.43M | UErrorCode &errorCode) { |
913 | 2.43M | if(U_FAILURE(errorCode)) { return 0; } |
914 | 2.43M | if(!Collation::isSpecialCE32(ce32)) { return ce32; } |
915 | 1.76M | switch(Collation::tagFromCE32(ce32)) { |
916 | 783k | case Collation::LONG_PRIMARY_TAG: |
917 | 792k | case Collation::LONG_SECONDARY_TAG: |
918 | 999k | case Collation::LATIN_EXPANSION_TAG: |
919 | | // copy as is |
920 | 999k | break; |
921 | 74.8k | case Collation::EXPANSION32_TAG: { |
922 | 74.8k | const uint32_t *baseCE32s = base->ce32s + Collation::indexFromCE32(ce32); |
923 | 74.8k | int32_t length = Collation::lengthFromCE32(ce32); |
924 | 74.8k | ce32 = encodeExpansion32( |
925 | 74.8k | reinterpret_cast<const int32_t *>(baseCE32s), length, errorCode); |
926 | 74.8k | break; |
927 | 792k | } |
928 | 18.7k | case Collation::EXPANSION_TAG: { |
929 | 18.7k | const int64_t *baseCEs = base->ces + Collation::indexFromCE32(ce32); |
930 | 18.7k | int32_t length = Collation::lengthFromCE32(ce32); |
931 | 18.7k | ce32 = encodeExpansion(baseCEs, length, errorCode); |
932 | 18.7k | break; |
933 | 792k | } |
934 | 459 | case Collation::PREFIX_TAG: { |
935 | | // Flatten prefixes and nested suffixes (contractions) |
936 | | // into a linear list of ConditionalCE32. |
937 | 459 | const char16_t *p = base->contexts + Collation::indexFromCE32(ce32); |
938 | 459 | ce32 = CollationData::readCE32(p); // Default if no prefix match. |
939 | 459 | if(!withContext) { |
940 | 0 | return copyFromBaseCE32(c, ce32, false, errorCode); |
941 | 0 | } |
942 | 459 | ConditionalCE32 head; |
943 | 459 | UnicodeString context(static_cast<char16_t>(0)); |
944 | 459 | int32_t index; |
945 | 459 | if(Collation::isContractionCE32(ce32)) { |
946 | 0 | index = copyContractionsFromBaseCE32(context, c, ce32, &head, errorCode); |
947 | 459 | } else { |
948 | 459 | ce32 = copyFromBaseCE32(c, ce32, true, errorCode); |
949 | 459 | head.next = index = addConditionalCE32(context, ce32, errorCode); |
950 | 459 | } |
951 | 459 | if(U_FAILURE(errorCode)) { return 0; } |
952 | 459 | ConditionalCE32 *cond = getConditionalCE32(index); // the last ConditionalCE32 so far |
953 | 459 | UCharsTrie::Iterator prefixes(p + 2, 0, errorCode); |
954 | 1.37k | while(prefixes.next(errorCode)) { |
955 | 918 | context = prefixes.getString(); |
956 | 918 | context.reverse(); |
957 | 918 | context.insert(0, static_cast<char16_t>(context.length())); |
958 | 918 | ce32 = static_cast<uint32_t>(prefixes.getValue()); |
959 | 918 | if(Collation::isContractionCE32(ce32)) { |
960 | 0 | index = copyContractionsFromBaseCE32(context, c, ce32, cond, errorCode); |
961 | 918 | } else { |
962 | 918 | ce32 = copyFromBaseCE32(c, ce32, true, errorCode); |
963 | 918 | cond->next = index = addConditionalCE32(context, ce32, errorCode); |
964 | 918 | } |
965 | 918 | if(U_FAILURE(errorCode)) { return 0; } |
966 | 918 | cond = getConditionalCE32(index); |
967 | 918 | } |
968 | 459 | ce32 = makeBuilderContextCE32(head.next); |
969 | 459 | contextChars.add(c); |
970 | 459 | break; |
971 | 459 | } |
972 | 3.67k | case Collation::CONTRACTION_TAG: { |
973 | 3.67k | if(!withContext) { |
974 | 143 | const char16_t *p = base->contexts + Collation::indexFromCE32(ce32); |
975 | 143 | ce32 = CollationData::readCE32(p); // Default if no suffix match. |
976 | 143 | return copyFromBaseCE32(c, ce32, false, errorCode); |
977 | 143 | } |
978 | 3.53k | ConditionalCE32 head; |
979 | 3.53k | UnicodeString context(static_cast<char16_t>(0)); |
980 | 3.53k | copyContractionsFromBaseCE32(context, c, ce32, &head, errorCode); |
981 | 3.53k | ce32 = makeBuilderContextCE32(head.next); |
982 | 3.53k | contextChars.add(c); |
983 | 3.53k | break; |
984 | 3.67k | } |
985 | 0 | case Collation::HANGUL_TAG: |
986 | 0 | errorCode = U_UNSUPPORTED_ERROR; // We forbid tailoring of Hangul syllables. |
987 | 0 | break; |
988 | 573k | case Collation::OFFSET_TAG: |
989 | 573k | ce32 = getCE32FromOffsetCE32(true, c, ce32); |
990 | 573k | break; |
991 | 99.5k | case Collation::IMPLICIT_TAG: |
992 | 99.5k | ce32 = encodeOneCE(Collation::unassignedCEFromCodePoint(c), errorCode); |
993 | 99.5k | break; |
994 | 0 | default: |
995 | 0 | UPRV_UNREACHABLE_EXIT; // require ce32 == base->getFinalCE32(ce32) |
996 | 1.76M | } |
997 | 1.76M | return ce32; |
998 | 1.76M | } |
999 | | |
1000 | | int32_t |
1001 | | CollationDataBuilder::copyContractionsFromBaseCE32(UnicodeString &context, UChar32 c, uint32_t ce32, |
1002 | 3.53k | ConditionalCE32 *cond, UErrorCode &errorCode) { |
1003 | 3.53k | if(U_FAILURE(errorCode)) { return 0; } |
1004 | 3.53k | const char16_t *p = base->contexts + Collation::indexFromCE32(ce32); |
1005 | 3.53k | int32_t index; |
1006 | 3.53k | if((ce32 & Collation::CONTRACT_SINGLE_CP_NO_MATCH) != 0) { |
1007 | | // No match on the single code point. |
1008 | | // We are underneath a prefix, and the default mapping is just |
1009 | | // a fallback to the mappings for a shorter prefix. |
1010 | 0 | U_ASSERT(context.length() > 1); |
1011 | 0 | index = -1; |
1012 | 3.53k | } else { |
1013 | 3.53k | ce32 = CollationData::readCE32(p); // Default if no suffix match. |
1014 | 3.53k | U_ASSERT(!Collation::isContractionCE32(ce32)); |
1015 | 3.53k | ce32 = copyFromBaseCE32(c, ce32, true, errorCode); |
1016 | 3.53k | cond->next = index = addConditionalCE32(context, ce32, errorCode); |
1017 | 3.53k | if(U_FAILURE(errorCode)) { return 0; } |
1018 | 3.53k | cond = getConditionalCE32(index); |
1019 | 3.53k | } |
1020 | | |
1021 | 3.53k | int32_t suffixStart = context.length(); |
1022 | 3.53k | UCharsTrie::Iterator suffixes(p + 2, 0, errorCode); |
1023 | 46.4k | while(suffixes.next(errorCode)) { |
1024 | 42.9k | context.append(suffixes.getString()); |
1025 | 42.9k | ce32 = copyFromBaseCE32(c, static_cast<uint32_t>(suffixes.getValue()), true, errorCode); |
1026 | 42.9k | cond->next = index = addConditionalCE32(context, ce32, errorCode); |
1027 | 42.9k | if(U_FAILURE(errorCode)) { return 0; } |
1028 | | // No need to update the unsafeBackwardSet because the tailoring set |
1029 | | // is already a copy of the base set. |
1030 | 42.9k | cond = getConditionalCE32(index); |
1031 | 42.9k | context.truncate(suffixStart); |
1032 | 42.9k | } |
1033 | 3.53k | U_ASSERT(index >= 0); |
1034 | 3.53k | return index; |
1035 | 3.53k | } |
1036 | | |
1037 | | class CopyHelper { |
1038 | | public: |
1039 | | CopyHelper(const CollationDataBuilder &s, CollationDataBuilder &d, |
1040 | | const CollationDataBuilder::CEModifier &m, UErrorCode &initialErrorCode) |
1041 | 3.97k | : src(s), dest(d), modifier(m), |
1042 | 3.97k | errorCode(initialErrorCode) {} |
1043 | | |
1044 | 1.63M | UBool copyRangeCE32(UChar32 start, UChar32 end, uint32_t ce32) { |
1045 | 1.63M | ce32 = copyCE32(ce32); |
1046 | 1.63M | utrie2_setRange32(dest.trie, start, end, ce32, true, &errorCode); |
1047 | 1.63M | if(CollationDataBuilder::isBuilderContextCE32(ce32)) { |
1048 | 23.7k | dest.contextChars.add(start, end); |
1049 | 23.7k | } |
1050 | 1.63M | return U_SUCCESS(errorCode); |
1051 | 1.63M | } |
1052 | | |
1053 | 1.93M | uint32_t copyCE32(uint32_t ce32) { |
1054 | 1.93M | if(!Collation::isSpecialCE32(ce32)) { |
1055 | 1.33M | int64_t ce = modifier.modifyCE32(ce32); |
1056 | 1.33M | if(ce != Collation::NO_CE) { |
1057 | 1.30M | ce32 = dest.encodeOneCE(ce, errorCode); |
1058 | 1.30M | } |
1059 | 1.33M | } else { |
1060 | 600k | int32_t tag = Collation::tagFromCE32(ce32); |
1061 | 600k | if(tag == Collation::EXPANSION32_TAG) { |
1062 | 471k | const uint32_t *srcCE32s = reinterpret_cast<uint32_t *>(src.ce32s.getBuffer()); |
1063 | 471k | srcCE32s += Collation::indexFromCE32(ce32); |
1064 | 471k | int32_t length = Collation::lengthFromCE32(ce32); |
1065 | | // Inspect the source CE32s. Just copy them if none are modified. |
1066 | | // Otherwise copy to modifiedCEs, with modifications. |
1067 | 471k | UBool isModified = false; |
1068 | 2.02M | for(int32_t i = 0; i < length; ++i) { |
1069 | 1.55M | ce32 = srcCE32s[i]; |
1070 | 1.55M | int64_t ce; |
1071 | 1.55M | if(Collation::isSpecialCE32(ce32) || |
1072 | 1.55M | (ce = modifier.modifyCE32(ce32)) == Collation::NO_CE) { |
1073 | 1.10M | if(isModified) { |
1074 | 404k | modifiedCEs[i] = Collation::ceFromCE32(ce32); |
1075 | 404k | } |
1076 | 1.10M | } else { |
1077 | 449k | if(!isModified) { |
1078 | 954k | for(int32_t j = 0; j < i; ++j) { |
1079 | 531k | modifiedCEs[j] = Collation::ceFromCE32(srcCE32s[j]); |
1080 | 531k | } |
1081 | 423k | isModified = true; |
1082 | 423k | } |
1083 | 449k | modifiedCEs[i] = ce; |
1084 | 449k | } |
1085 | 1.55M | } |
1086 | 471k | if(isModified) { |
1087 | 423k | ce32 = dest.encodeCEs(modifiedCEs, length, errorCode); |
1088 | 423k | } else { |
1089 | 48.9k | ce32 = dest.encodeExpansion32( |
1090 | 48.9k | reinterpret_cast<const int32_t *>(srcCE32s), length, errorCode); |
1091 | 48.9k | } |
1092 | 471k | } else if(tag == Collation::EXPANSION_TAG) { |
1093 | 65.8k | const int64_t *srcCEs = src.ce64s.getBuffer(); |
1094 | 65.8k | srcCEs += Collation::indexFromCE32(ce32); |
1095 | 65.8k | int32_t length = Collation::lengthFromCE32(ce32); |
1096 | | // Inspect the source CEs. Just copy them if none are modified. |
1097 | | // Otherwise copy to modifiedCEs, with modifications. |
1098 | 65.8k | UBool isModified = false; |
1099 | 773k | for(int32_t i = 0; i < length; ++i) { |
1100 | 708k | int64_t srcCE = srcCEs[i]; |
1101 | 708k | int64_t ce = modifier.modifyCE(srcCE); |
1102 | 708k | if(ce == Collation::NO_CE) { |
1103 | 648k | if(isModified) { |
1104 | 128k | modifiedCEs[i] = srcCE; |
1105 | 128k | } |
1106 | 648k | } else { |
1107 | 59.6k | if(!isModified) { |
1108 | 382k | for(int32_t j = 0; j < i; ++j) { |
1109 | 341k | modifiedCEs[j] = srcCEs[j]; |
1110 | 341k | } |
1111 | 41.2k | isModified = true; |
1112 | 41.2k | } |
1113 | 59.6k | modifiedCEs[i] = ce; |
1114 | 59.6k | } |
1115 | 708k | } |
1116 | 65.8k | if(isModified) { |
1117 | 41.2k | ce32 = dest.encodeCEs(modifiedCEs, length, errorCode); |
1118 | 41.2k | } else { |
1119 | 24.6k | ce32 = dest.encodeExpansion(srcCEs, length, errorCode); |
1120 | 24.6k | } |
1121 | 65.8k | } else if(tag == Collation::BUILDER_DATA_TAG) { |
1122 | | // Copy the list of ConditionalCE32. |
1123 | 23.7k | ConditionalCE32 *cond = src.getConditionalCE32ForCE32(ce32); |
1124 | 23.7k | U_ASSERT(!cond->hasContext()); |
1125 | 23.7k | int32_t destIndex = dest.addConditionalCE32( |
1126 | 23.7k | cond->context, copyCE32(cond->ce32), errorCode); |
1127 | 23.7k | ce32 = CollationDataBuilder::makeBuilderContextCE32(destIndex); |
1128 | 299k | while(cond->next >= 0) { |
1129 | 275k | cond = src.getConditionalCE32(cond->next); |
1130 | 275k | ConditionalCE32 *prevDestCond = dest.getConditionalCE32(destIndex); |
1131 | 275k | destIndex = dest.addConditionalCE32( |
1132 | 275k | cond->context, copyCE32(cond->ce32), errorCode); |
1133 | 275k | int32_t suffixStart = cond->prefixLength() + 1; |
1134 | 275k | dest.unsafeBackwardSet.addAll(cond->context.tempSubString(suffixStart)); |
1135 | 275k | prevDestCond->next = destIndex; |
1136 | 275k | } |
1137 | 38.7k | } else { |
1138 | | // Just copy long CEs and Latin mini expansions (and other expected values) as is, |
1139 | | // assuming that the modifier would not modify them. |
1140 | 38.7k | U_ASSERT(tag == Collation::LONG_PRIMARY_TAG || |
1141 | 38.7k | tag == Collation::LONG_SECONDARY_TAG || |
1142 | 38.7k | tag == Collation::LATIN_EXPANSION_TAG || |
1143 | 38.7k | tag == Collation::HANGUL_TAG); |
1144 | 38.7k | } |
1145 | 600k | } |
1146 | 1.93M | return ce32; |
1147 | 1.93M | } |
1148 | | |
1149 | | const CollationDataBuilder &src; |
1150 | | CollationDataBuilder &dest; |
1151 | | const CollationDataBuilder::CEModifier &modifier; |
1152 | | int64_t modifiedCEs[Collation::MAX_EXPANSION_LENGTH]; |
1153 | | UErrorCode errorCode; |
1154 | | }; |
1155 | | |
1156 | | U_CDECL_BEGIN |
1157 | | |
1158 | | static UBool U_CALLCONV |
1159 | 1.76M | enumRangeForCopy(const void *context, UChar32 start, UChar32 end, uint32_t value) { |
1160 | 1.76M | return |
1161 | 1.76M | value == Collation::UNASSIGNED_CE32 || value == Collation::FALLBACK_CE32 || |
1162 | 1.76M | ((CopyHelper *)context)->copyRangeCE32(start, end, value); |
1163 | 1.76M | } |
1164 | | |
1165 | | U_CDECL_END |
1166 | | |
1167 | | void |
1168 | | CollationDataBuilder::copyFrom(const CollationDataBuilder &src, const CEModifier &modifier, |
1169 | 3.97k | UErrorCode &errorCode) { |
1170 | 3.97k | if(U_FAILURE(errorCode)) { return; } |
1171 | 3.97k | if(trie == nullptr || utrie2_isFrozen(trie)) { |
1172 | 0 | errorCode = U_INVALID_STATE_ERROR; |
1173 | 0 | return; |
1174 | 0 | } |
1175 | 3.97k | CopyHelper helper(src, *this, modifier, errorCode); |
1176 | 3.97k | utrie2_enum(src.trie, nullptr, enumRangeForCopy, &helper); |
1177 | 3.97k | errorCode = helper.errorCode; |
1178 | | // Update the contextChars and the unsafeBackwardSet while copying, |
1179 | | // in case a character had conditional mappings in the source builder |
1180 | | // and they were removed later. |
1181 | 3.97k | modified |= src.modified; |
1182 | 3.97k | } |
1183 | | |
1184 | | void |
1185 | 4.01k | CollationDataBuilder::optimize(const UnicodeSet &set, UErrorCode &errorCode) { |
1186 | 4.01k | if(U_FAILURE(errorCode) || set.isEmpty()) { return; } |
1187 | 3.97k | UnicodeSetIterator iter(set); |
1188 | 2.44M | while(iter.next() && !iter.isString()) { |
1189 | 2.43M | UChar32 c = iter.getCodepoint(); |
1190 | 2.43M | uint32_t ce32 = utrie2_get32(trie, c); |
1191 | 2.43M | if(ce32 == Collation::FALLBACK_CE32) { |
1192 | 2.35M | ce32 = base->getFinalCE32(base->getCE32(c)); |
1193 | 2.35M | ce32 = copyFromBaseCE32(c, ce32, true, errorCode); |
1194 | 2.35M | utrie2_set32(trie, c, ce32, &errorCode); |
1195 | 2.35M | } |
1196 | 2.43M | } |
1197 | 3.97k | modified = true; |
1198 | 3.97k | } |
1199 | | |
1200 | | void |
1201 | 327 | CollationDataBuilder::suppressContractions(const UnicodeSet &set, UErrorCode &errorCode) { |
1202 | 327 | if(U_FAILURE(errorCode) || set.isEmpty()) { return; } |
1203 | 327 | UnicodeSetIterator iter(set); |
1204 | 981 | while(iter.next() && !iter.isString()) { |
1205 | 654 | UChar32 c = iter.getCodepoint(); |
1206 | 654 | uint32_t ce32 = utrie2_get32(trie, c); |
1207 | 654 | if(ce32 == Collation::FALLBACK_CE32) { |
1208 | 143 | ce32 = base->getFinalCE32(base->getCE32(c)); |
1209 | 143 | if(Collation::ce32HasContext(ce32)) { |
1210 | 143 | ce32 = copyFromBaseCE32(c, ce32, false /* without context */, errorCode); |
1211 | 143 | utrie2_set32(trie, c, ce32, &errorCode); |
1212 | 143 | } |
1213 | 511 | } else if(isBuilderContextCE32(ce32)) { |
1214 | 13 | ce32 = getConditionalCE32ForCE32(ce32)->ce32; |
1215 | | // Simply abandon the list of ConditionalCE32. |
1216 | | // The caller will copy this builder in the end, |
1217 | | // eliminating unreachable data. |
1218 | 13 | utrie2_set32(trie, c, ce32, &errorCode); |
1219 | 13 | contextChars.remove(c); |
1220 | 13 | } |
1221 | 654 | } |
1222 | 327 | modified = true; |
1223 | 327 | } |
1224 | | |
1225 | | UBool |
1226 | 3.97k | CollationDataBuilder::getJamoCE32s(uint32_t jamoCE32s[], UErrorCode &errorCode) { |
1227 | 3.97k | if(U_FAILURE(errorCode)) { return false; } |
1228 | 3.97k | UBool anyJamoAssigned = base == nullptr; // always set jamoCE32s in the base data |
1229 | 3.97k | UBool needToCopyFromBase = false; |
1230 | 270k | for(int32_t j = 0; j < CollationData::JAMO_CE32S_LENGTH; ++j) { // Count across Jamo types. |
1231 | 266k | UChar32 jamo = jamoCpFromIndex(j); |
1232 | 266k | UBool fromBase = false; |
1233 | 266k | uint32_t ce32 = utrie2_get32(trie, jamo); |
1234 | 266k | anyJamoAssigned |= Collation::isAssignedCE32(ce32); |
1235 | | // TODO: Try to prevent [optimize [Jamo]] from counting as anyJamoAssigned. |
1236 | | // (As of CLDR 24 [2013] the Korean tailoring does not optimize conjoining Jamo.) |
1237 | 266k | if(ce32 == Collation::FALLBACK_CE32) { |
1238 | 262k | fromBase = true; |
1239 | 262k | ce32 = base->getCE32(jamo); |
1240 | 262k | } |
1241 | 266k | if(Collation::isSpecialCE32(ce32)) { |
1242 | 1.81k | switch(Collation::tagFromCE32(ce32)) { |
1243 | 296 | case Collation::LONG_PRIMARY_TAG: |
1244 | 313 | case Collation::LONG_SECONDARY_TAG: |
1245 | 313 | case Collation::LATIN_EXPANSION_TAG: |
1246 | | // Copy the ce32 as-is. |
1247 | 313 | break; |
1248 | 954 | case Collation::EXPANSION32_TAG: |
1249 | 1.47k | case Collation::EXPANSION_TAG: |
1250 | 1.48k | case Collation::PREFIX_TAG: |
1251 | 1.49k | case Collation::CONTRACTION_TAG: |
1252 | 1.49k | if(fromBase) { |
1253 | | // Defer copying until we know if anyJamoAssigned. |
1254 | 0 | ce32 = Collation::FALLBACK_CE32; |
1255 | 0 | needToCopyFromBase = true; |
1256 | 0 | } |
1257 | 1.49k | break; |
1258 | 0 | case Collation::IMPLICIT_TAG: |
1259 | | // An unassigned Jamo should only occur in tests with incomplete bases. |
1260 | 0 | U_ASSERT(fromBase); |
1261 | 0 | ce32 = Collation::FALLBACK_CE32; |
1262 | 0 | needToCopyFromBase = true; |
1263 | 0 | break; |
1264 | 0 | case Collation::OFFSET_TAG: |
1265 | 0 | ce32 = getCE32FromOffsetCE32(fromBase, jamo, ce32); |
1266 | 0 | break; |
1267 | 0 | case Collation::FALLBACK_TAG: |
1268 | 0 | case Collation::RESERVED_TAG_3: |
1269 | 0 | case Collation::BUILDER_DATA_TAG: |
1270 | 0 | case Collation::DIGIT_TAG: |
1271 | 0 | case Collation::U0000_TAG: |
1272 | 0 | case Collation::HANGUL_TAG: |
1273 | 0 | case Collation::LEAD_SURROGATE_TAG: |
1274 | 0 | errorCode = U_INTERNAL_PROGRAM_ERROR; |
1275 | 0 | return false; |
1276 | 1.81k | } |
1277 | 1.81k | } |
1278 | 266k | jamoCE32s[j] = ce32; |
1279 | 266k | } |
1280 | 3.97k | if(anyJamoAssigned && needToCopyFromBase) { |
1281 | 0 | for(int32_t j = 0; j < CollationData::JAMO_CE32S_LENGTH; ++j) { |
1282 | 0 | if(jamoCE32s[j] == Collation::FALLBACK_CE32) { |
1283 | 0 | UChar32 jamo = jamoCpFromIndex(j); |
1284 | 0 | jamoCE32s[j] = copyFromBaseCE32(jamo, base->getCE32(jamo), |
1285 | 0 | /*withContext=*/ true, errorCode); |
1286 | 0 | } |
1287 | 0 | } |
1288 | 0 | } |
1289 | 3.97k | return anyJamoAssigned && U_SUCCESS(errorCode); |
1290 | 3.97k | } |
1291 | | |
1292 | | void |
1293 | 3.97k | CollationDataBuilder::setDigitTags(UErrorCode &errorCode) { |
1294 | 3.97k | UnicodeSet digits(UNICODE_STRING_SIMPLE("[:Nd:]"), errorCode); |
1295 | 3.97k | if(U_FAILURE(errorCode)) { return; } |
1296 | 3.97k | UnicodeSetIterator iter(digits); |
1297 | 3.02M | while(iter.next()) { |
1298 | 3.02M | U_ASSERT(!iter.isString()); |
1299 | 3.02M | UChar32 c = iter.getCodepoint(); |
1300 | 3.02M | uint32_t ce32 = utrie2_get32(trie, c); |
1301 | 3.02M | if(ce32 != Collation::FALLBACK_CE32 && ce32 != Collation::UNASSIGNED_CE32) { |
1302 | 51.4k | int32_t index = addCE32(ce32, errorCode); |
1303 | 51.4k | if(U_FAILURE(errorCode)) { return; } |
1304 | 51.4k | if(index > Collation::MAX_INDEX) { |
1305 | 0 | errorCode = U_BUFFER_OVERFLOW_ERROR; |
1306 | 0 | return; |
1307 | 0 | } |
1308 | 51.4k | ce32 = Collation::makeCE32FromTagIndexAndLength( |
1309 | 51.4k | Collation::DIGIT_TAG, index, u_charDigitValue(c)); |
1310 | 51.4k | utrie2_set32(trie, c, ce32, &errorCode); |
1311 | 51.4k | } |
1312 | 3.02M | } |
1313 | 3.97k | } |
1314 | | |
1315 | | U_CDECL_BEGIN |
1316 | | |
1317 | | static UBool U_CALLCONV |
1318 | 4.07M | enumRangeLeadValue(const void *context, UChar32 /*start*/, UChar32 /*end*/, uint32_t value) { |
1319 | 4.07M | int32_t *pValue = (int32_t *)context; |
1320 | 4.07M | if(value == Collation::UNASSIGNED_CE32) { |
1321 | 0 | value = Collation::LEAD_ALL_UNASSIGNED; |
1322 | 4.07M | } else if(value == Collation::FALLBACK_CE32) { |
1323 | 4.07M | value = Collation::LEAD_ALL_FALLBACK; |
1324 | 4.07M | } else { |
1325 | 2.05k | *pValue = Collation::LEAD_MIXED; |
1326 | 2.05k | return false; |
1327 | 2.05k | } |
1328 | 4.07M | if(*pValue < 0) { |
1329 | 4.07M | *pValue = (int32_t)value; |
1330 | 4.07M | } else if(*pValue != (int32_t)value) { |
1331 | 0 | *pValue = Collation::LEAD_MIXED; |
1332 | 0 | return false; |
1333 | 0 | } |
1334 | 4.07M | return true; |
1335 | 4.07M | } |
1336 | | |
1337 | | U_CDECL_END |
1338 | | |
1339 | | void |
1340 | 3.97k | CollationDataBuilder::setLeadSurrogates(UErrorCode &errorCode) { |
1341 | 4.07M | for(char16_t lead = 0xd800; lead < 0xdc00; ++lead) { |
1342 | 4.07M | int32_t value = -1; |
1343 | 4.07M | utrie2_enumForLeadSurrogate(trie, lead, nullptr, enumRangeLeadValue, &value); |
1344 | 4.07M | utrie2_set32ForLeadSurrogateCodeUnit( |
1345 | 4.07M | trie, lead, |
1346 | 4.07M | Collation::makeCE32FromTagAndIndex(Collation::LEAD_SURROGATE_TAG, 0) | static_cast<uint32_t>(value), |
1347 | 4.07M | &errorCode); |
1348 | 4.07M | } |
1349 | 3.97k | } |
1350 | | |
1351 | | void |
1352 | 3.97k | CollationDataBuilder::build(CollationData &data, UErrorCode &errorCode) { |
1353 | 3.97k | buildMappings(data, errorCode); |
1354 | 3.97k | if(base != nullptr) { |
1355 | 3.97k | data.numericPrimary = base->numericPrimary; |
1356 | 3.97k | data.compressibleBytes = base->compressibleBytes; |
1357 | 3.97k | data.numScripts = base->numScripts; |
1358 | 3.97k | data.scriptsIndex = base->scriptsIndex; |
1359 | 3.97k | data.scriptStarts = base->scriptStarts; |
1360 | 3.97k | data.scriptStartsLength = base->scriptStartsLength; |
1361 | 3.97k | } |
1362 | 3.97k | buildFastLatinTable(data, errorCode); |
1363 | 3.97k | } |
1364 | | |
1365 | | void |
1366 | 3.97k | CollationDataBuilder::buildMappings(CollationData &data, UErrorCode &errorCode) { |
1367 | 3.97k | if(U_FAILURE(errorCode)) { return; } |
1368 | 3.97k | if(trie == nullptr || utrie2_isFrozen(trie)) { |
1369 | 0 | errorCode = U_INVALID_STATE_ERROR; |
1370 | 0 | return; |
1371 | 0 | } |
1372 | | |
1373 | 3.97k | buildContexts(errorCode); |
1374 | | |
1375 | 3.97k | uint32_t jamoCE32s[CollationData::JAMO_CE32S_LENGTH]; |
1376 | 3.97k | int32_t jamoIndex = -1; |
1377 | 3.97k | if(getJamoCE32s(jamoCE32s, errorCode)) { |
1378 | 266 | jamoIndex = ce32s.size(); |
1379 | 18.0k | for(int32_t i = 0; i < CollationData::JAMO_CE32S_LENGTH; ++i) { |
1380 | 17.8k | ce32s.addElement(static_cast<int32_t>(jamoCE32s[i]), errorCode); |
1381 | 17.8k | } |
1382 | | // Small optimization: Use a bit in the Hangul ce32 |
1383 | | // to indicate that none of the Jamo CE32s are isSpecialCE32() |
1384 | | // (as it should be in the root collator). |
1385 | | // It allows CollationIterator to avoid recursive function calls and per-Jamo tests. |
1386 | | // In order to still have good trie compression and keep this code simple, |
1387 | | // we only set this flag if a whole block of 588 Hangul syllables starting with |
1388 | | // a common leading consonant (Jamo L) has this property. |
1389 | 266 | UBool isAnyJamoVTSpecial = false; |
1390 | 9.86k | for(int32_t i = Hangul::JAMO_L_COUNT; i < CollationData::JAMO_CE32S_LENGTH; ++i) { |
1391 | 9.68k | if(Collation::isSpecialCE32(jamoCE32s[i])) { |
1392 | 80 | isAnyJamoVTSpecial = true; |
1393 | 80 | break; |
1394 | 80 | } |
1395 | 9.68k | } |
1396 | 266 | uint32_t hangulCE32 = Collation::makeCE32FromTagAndIndex(Collation::HANGUL_TAG, 0); |
1397 | 266 | UChar32 c = Hangul::HANGUL_BASE; |
1398 | 5.32k | for(int32_t i = 0; i < Hangul::JAMO_L_COUNT; ++i) { // iterate over the Jamo L |
1399 | 5.05k | uint32_t ce32 = hangulCE32; |
1400 | 5.05k | if(!isAnyJamoVTSpecial && !Collation::isSpecialCE32(jamoCE32s[i])) { |
1401 | 3.23k | ce32 |= Collation::HANGUL_NO_SPECIAL_JAMO; |
1402 | 3.23k | } |
1403 | 5.05k | UChar32 limit = c + Hangul::JAMO_VT_COUNT; |
1404 | 5.05k | utrie2_setRange32(trie, c, limit - 1, ce32, true, &errorCode); |
1405 | 5.05k | c = limit; |
1406 | 5.05k | } |
1407 | 3.71k | } else { |
1408 | | // Copy the Hangul CE32s from the base in blocks per Jamo L, |
1409 | | // assuming that HANGUL_NO_SPECIAL_JAMO is set or not set for whole blocks. |
1410 | 74.2k | for(UChar32 c = Hangul::HANGUL_BASE; c < Hangul::HANGUL_LIMIT;) { |
1411 | 70.5k | uint32_t ce32 = base->getCE32(c); |
1412 | 70.5k | U_ASSERT(Collation::hasCE32Tag(ce32, Collation::HANGUL_TAG)); |
1413 | 70.5k | UChar32 limit = c + Hangul::JAMO_VT_COUNT; |
1414 | 70.5k | utrie2_setRange32(trie, c, limit - 1, ce32, true, &errorCode); |
1415 | 70.5k | c = limit; |
1416 | 70.5k | } |
1417 | 3.71k | } |
1418 | | |
1419 | 3.97k | setDigitTags(errorCode); |
1420 | 3.97k | setLeadSurrogates(errorCode); |
1421 | | |
1422 | 3.97k | if (icu4xMode) { |
1423 | | // Make sure that starters that occur is the middle of a |
1424 | | // contraction have contraction ce32 with the |
1425 | | // `CONTRACT_HAS_STARTER` flag set so that starters that |
1426 | | // can occur in a non-final position in a contraction can |
1427 | | // be easily recognized from having a contraction ce32 |
1428 | | // that has the `CONTRACT_HAS_STARTER` flag set. |
1429 | |
|
1430 | 0 | UCharsTrieBuilder contractionBuilder(errorCode); |
1431 | | // Intentionally unpaired low surrogate to make it never |
1432 | | // match well-formed UTF-16 which ICU4X feeds to the |
1433 | | // matcher. |
1434 | 0 | UnicodeString placeholder(0xDC00); |
1435 | |
|
1436 | 0 | for (UChar32 c : contractionMiddleStarter.codePoints()) { |
1437 | 0 | uint32_t ce32 = utrie2_get32(trie, c); |
1438 | 0 | UBool fromBase = false; |
1439 | 0 | if(ce32 == Collation::FALLBACK_CE32) { |
1440 | 0 | fromBase = true; |
1441 | 0 | ce32 = base->getCE32(c); |
1442 | 0 | } |
1443 | 0 | if (!(Collation::hasCE32Tag(ce32, Collation::CONTRACTION_TAG) && (ce32 & Collation::CONTRACT_HAS_STARTER))) { |
1444 | 0 | if (fromBase) { |
1445 | | // This case does not actually happen as of February 2025. |
1446 | 0 | ce32 = copyFromBaseCE32(c, ce32, true, errorCode); |
1447 | 0 | } |
1448 | 0 | if (Collation::hasCE32Tag(ce32, Collation::CONTRACTION_TAG)) { |
1449 | | // This middle starter is also the first character of another |
1450 | | // contraction, but that contraction does not have the |
1451 | | // CONTRACT_HAS_STARTER flag. Let's add the flag to |
1452 | | // mark this at the expense of pessimizing the matching |
1453 | | // of this contraction. |
1454 | | // As of February 2025, this case does not actually occur |
1455 | | // in CLDR. |
1456 | 0 | ce32 |= Collation::CONTRACT_HAS_STARTER; |
1457 | 0 | } else { |
1458 | | // This middle starter is not also the first character |
1459 | | // in another contraction. |
1460 | | |
1461 | | // The UCharsTrie needs to contain some placeholder |
1462 | | // because it cannot be empty. We build a trie |
1463 | | // that never actually matches anything that ICU4X can try to |
1464 | | // match, since ICU4X always passes well-formed UTF-16 to the |
1465 | | // matcher and we put an unpaired low surrogate into the trie. |
1466 | | // This pessimizes the character to CE mapping of the `c`, |
1467 | | // since useless trie matching will be attempted but as of |
1468 | | // February 2025, only two relatively rare characters are affected. |
1469 | 0 | contractionBuilder.clear(); |
1470 | 0 | contractionBuilder.add(placeholder, static_cast<int32_t>(ce32), errorCode); |
1471 | |
|
1472 | 0 | int32_t index = addContextTrie(ce32, contractionBuilder, errorCode); |
1473 | 0 | if(U_FAILURE(errorCode)) { return; } |
1474 | 0 | if(index > Collation::MAX_INDEX) { |
1475 | 0 | errorCode = U_BUFFER_OVERFLOW_ERROR; |
1476 | 0 | return; |
1477 | 0 | } |
1478 | | // Set CONTRACT_HAS_STARTER to make identical prefix matching able to catch this. |
1479 | 0 | ce32 = Collation::makeCE32FromTagAndIndex(Collation::CONTRACTION_TAG, index) | Collation::CONTRACT_HAS_STARTER; |
1480 | 0 | } |
1481 | 0 | utrie2_set32(trie, c, ce32, &errorCode); |
1482 | 0 | } |
1483 | 0 | } |
1484 | 3.97k | } else { |
1485 | | // For U+0000, move its normal ce32 into CE32s[0] and set U0000_TAG. |
1486 | 3.97k | ce32s.setElementAt(static_cast<int32_t>(utrie2_get32(trie, 0)), 0); |
1487 | 3.97k | utrie2_set32(trie, 0, Collation::makeCE32FromTagAndIndex(Collation::U0000_TAG, 0), &errorCode); |
1488 | 3.97k | } |
1489 | | |
1490 | 3.97k | utrie2_freeze(trie, UTRIE2_32_VALUE_BITS, &errorCode); |
1491 | 3.97k | if(U_FAILURE(errorCode)) { return; } |
1492 | | |
1493 | | // Mark each lead surrogate as "unsafe" |
1494 | | // if any of its 1024 associated supplementary code points is "unsafe". |
1495 | 3.97k | UChar32 c = 0x10000; |
1496 | 4.07M | for(char16_t lead = 0xd800; lead < 0xdc00; ++lead, c += 0x400) { |
1497 | 4.07M | if(unsafeBackwardSet.containsSome(c, c + 0x3ff)) { |
1498 | 91.8k | unsafeBackwardSet.add(lead); |
1499 | 91.8k | } |
1500 | 4.07M | } |
1501 | 3.97k | unsafeBackwardSet.freeze(); |
1502 | | |
1503 | 3.97k | data.trie = trie; |
1504 | 3.97k | data.ce32s = reinterpret_cast<const uint32_t *>(ce32s.getBuffer()); |
1505 | 3.97k | data.ces = ce64s.getBuffer(); |
1506 | 3.97k | data.contexts = contexts.getBuffer(); |
1507 | | |
1508 | 3.97k | data.ce32sLength = ce32s.size(); |
1509 | 3.97k | data.cesLength = ce64s.size(); |
1510 | 3.97k | data.contextsLength = contexts.length(); |
1511 | | |
1512 | 3.97k | data.base = base; |
1513 | 3.97k | if(jamoIndex >= 0) { |
1514 | 266 | data.jamoCE32s = data.ce32s + jamoIndex; |
1515 | 3.71k | } else { |
1516 | 3.71k | data.jamoCE32s = base->jamoCE32s; |
1517 | 3.71k | } |
1518 | 3.97k | data.unsafeBackwardSet = &unsafeBackwardSet; |
1519 | 3.97k | } |
1520 | | |
1521 | | void |
1522 | 4.07k | CollationDataBuilder::clearContexts() { |
1523 | 4.07k | contexts.remove(); |
1524 | | // Incrementing the contexts build "era" invalidates all of the builtCE32 |
1525 | | // from before this clearContexts() call. |
1526 | | // Simpler than finding and resetting all of those fields. |
1527 | 4.07k | ++contextsEra; |
1528 | 4.07k | } |
1529 | | |
1530 | | void |
1531 | 3.97k | CollationDataBuilder::buildContexts(UErrorCode &errorCode) { |
1532 | 3.97k | if(U_FAILURE(errorCode)) { return; } |
1533 | | // Ignore abandoned lists and the cached builtCE32, |
1534 | | // and build all contexts from scratch. |
1535 | 3.97k | clearContexts(); |
1536 | 3.97k | UnicodeSetIterator iter(contextChars); |
1537 | 29.4k | while(U_SUCCESS(errorCode) && iter.next()) { |
1538 | 25.4k | U_ASSERT(!iter.isString()); |
1539 | 25.4k | UChar32 c = iter.getCodepoint(); |
1540 | 25.4k | uint32_t ce32 = utrie2_get32(trie, c); |
1541 | 25.4k | if(!isBuilderContextCE32(ce32)) { |
1542 | | // Impossible: No context data for c in contextChars. |
1543 | 0 | errorCode = U_INTERNAL_PROGRAM_ERROR; |
1544 | 0 | return; |
1545 | 0 | } |
1546 | 25.4k | ConditionalCE32 *cond = getConditionalCE32ForCE32(ce32); |
1547 | 25.4k | ce32 = buildContext(cond, errorCode); |
1548 | 25.4k | utrie2_set32(trie, c, ce32, &errorCode); |
1549 | 25.4k | } |
1550 | 3.97k | } |
1551 | | |
1552 | | uint32_t |
1553 | 441k | CollationDataBuilder::buildContext(ConditionalCE32 *head, UErrorCode &errorCode) { |
1554 | 441k | if(U_FAILURE(errorCode)) { return 0; } |
1555 | | // The list head must have no context. |
1556 | 441k | U_ASSERT(!head->hasContext()); |
1557 | | // The list head must be followed by one or more nodes that all do have context. |
1558 | 441k | U_ASSERT(head->next >= 0); |
1559 | 441k | UCharsTrieBuilder prefixBuilder(errorCode); |
1560 | 441k | UCharsTrieBuilder contractionBuilder(errorCode); |
1561 | | // This outer loop goes from each prefix to the next. |
1562 | | // For each prefix it finds the one or more same-prefix entries (firstCond..lastCond). |
1563 | | // If there are multiple suffixes for the same prefix, |
1564 | | // then an inner loop builds a contraction trie for them. |
1565 | 3.22M | for(ConditionalCE32 *cond = head;; cond = getConditionalCE32(cond->next)) { |
1566 | 3.22M | if(U_FAILURE(errorCode)) { return 0; } // early out for memory allocation errors |
1567 | | // After the list head, the prefix or suffix can be empty, but not both. |
1568 | 3.22M | U_ASSERT(cond == head || cond->hasContext()); |
1569 | 3.22M | int32_t prefixLength = cond->prefixLength(); |
1570 | 3.22M | UnicodeString prefix(cond->context, 0, prefixLength + 1); |
1571 | | // Collect all contraction suffixes for one prefix. |
1572 | 3.22M | ConditionalCE32 *firstCond = cond; |
1573 | 3.22M | ConditionalCE32 *lastCond; |
1574 | 46.5M | do { |
1575 | 46.5M | lastCond = cond; |
1576 | | // Clear the defaultCE32 fields as we go. |
1577 | | // They are left over from building a previous version of this list of contexts. |
1578 | | // |
1579 | | // One of the code paths below may copy a preceding defaultCE32 |
1580 | | // into its emptySuffixCE32. |
1581 | | // If a new suffix has been inserted before what used to be |
1582 | | // the firstCond for its prefix, then that previous firstCond could still |
1583 | | // contain an outdated defaultCE32 from an earlier buildContext() and |
1584 | | // result in an incorrect emptySuffixCE32. |
1585 | | // So we reset all defaultCE32 before reading and setting new values. |
1586 | 46.5M | cond->defaultCE32 = Collation::NO_CE32; |
1587 | 46.5M | } while(cond->next >= 0 && |
1588 | 46.5M | (cond = getConditionalCE32(cond->next))->context.startsWith(prefix)); |
1589 | 3.22M | uint32_t ce32; |
1590 | 3.22M | int32_t suffixStart = prefixLength + 1; // == prefix.length() |
1591 | 3.22M | if(lastCond->context.length() == suffixStart) { |
1592 | | // One prefix without contraction suffix. |
1593 | 2.00M | U_ASSERT(firstCond == lastCond); |
1594 | 2.00M | ce32 = lastCond->ce32; |
1595 | 2.00M | cond = lastCond; |
1596 | 2.00M | } else { |
1597 | | // Build the contractions trie. |
1598 | 1.21M | contractionBuilder.clear(); |
1599 | | // Entry for an empty suffix, to be stored before the trie. |
1600 | 1.21M | uint32_t emptySuffixCE32 = 0; |
1601 | 1.21M | uint32_t flags = 0; |
1602 | 1.21M | if(firstCond->context.length() == suffixStart) { |
1603 | | // There is a mapping for the prefix and the single character c. (p|c) |
1604 | | // If no other suffix matches, then we return this value. |
1605 | 373k | emptySuffixCE32 = firstCond->ce32; |
1606 | 373k | cond = getConditionalCE32(firstCond->next); |
1607 | 840k | } else { |
1608 | | // There is no mapping for the prefix and just the single character. |
1609 | | // (There is no p|c, only p|cd, p|ce etc.) |
1610 | 840k | flags |= Collation::CONTRACT_SINGLE_CP_NO_MATCH; |
1611 | | // When the prefix matches but none of the prefix-specific suffixes, |
1612 | | // then we fall back to the mappings with the next-longest prefix, |
1613 | | // and ultimately to mappings with no prefix. |
1614 | | // Each fallback might be another set of contractions. |
1615 | | // For example, if there are mappings for ch, p|cd, p|ce, but not for p|c, |
1616 | | // then in text "pch" we find the ch contraction. |
1617 | 85.1M | for(cond = head;; cond = getConditionalCE32(cond->next)) { |
1618 | 85.1M | int32_t length = cond->prefixLength(); |
1619 | 85.1M | if(length == prefixLength) { break; } |
1620 | 84.3M | if(cond->defaultCE32 != Collation::NO_CE32 && |
1621 | 84.3M | (length==0 || prefix.endsWith(cond->context, 1, length))) { |
1622 | 929k | emptySuffixCE32 = cond->defaultCE32; |
1623 | 929k | } |
1624 | 84.3M | } |
1625 | 840k | cond = firstCond; |
1626 | 840k | } |
1627 | | // Optimization: Set a flag when |
1628 | | // the first character of every contraction suffix has lccc!=0. |
1629 | | // Short-circuits contraction matching when a normal letter follows. |
1630 | 1.21M | flags |= Collation::CONTRACT_NEXT_CCC; |
1631 | | // Add all of the non-empty suffixes into the contraction trie. |
1632 | 44.1M | for(;;) { |
1633 | 44.1M | UnicodeString suffix(cond->context, suffixStart); |
1634 | 44.1M | uint16_t fcd16 = nfcImpl.getFCD16(suffix.char32At(0)); |
1635 | 44.1M | if(fcd16 <= 0xff) { |
1636 | 34.2M | flags &= ~Collation::CONTRACT_NEXT_CCC; |
1637 | 34.2M | } |
1638 | 44.1M | fcd16 = nfcImpl.getFCD16(suffix.char32At(suffix.length() - 1)); |
1639 | 44.1M | if(fcd16 > 0xff) { |
1640 | | // The last suffix character has lccc!=0, allowing for discontiguous contractions. |
1641 | 5.88M | flags |= Collation::CONTRACT_TRAILING_CCC; |
1642 | 5.88M | } |
1643 | 44.1M | if (icu4xMode && (flags & Collation::CONTRACT_HAS_STARTER) == 0) { |
1644 | 0 | for (int32_t i = 0; i < suffix.length();) { |
1645 | 0 | UChar32 c = suffix.char32At(i); |
1646 | 0 | if (!u_getCombiningClass(c)) { |
1647 | 0 | flags |= Collation::CONTRACT_HAS_STARTER; |
1648 | 0 | break; |
1649 | 0 | } |
1650 | 0 | if (c > 0xFFFF) { |
1651 | 0 | i += 2; |
1652 | 0 | } else { |
1653 | 0 | ++i; |
1654 | 0 | } |
1655 | 0 | } |
1656 | 0 | } |
1657 | 44.1M | contractionBuilder.add(suffix, static_cast<int32_t>(cond->ce32), errorCode); |
1658 | 44.1M | if(cond == lastCond) { break; } |
1659 | 42.9M | cond = getConditionalCE32(cond->next); |
1660 | 42.9M | } |
1661 | 1.21M | int32_t index = addContextTrie(emptySuffixCE32, contractionBuilder, errorCode); |
1662 | 1.21M | if(U_FAILURE(errorCode)) { return 0; } |
1663 | 1.21M | if(index > Collation::MAX_INDEX) { |
1664 | 52 | errorCode = U_BUFFER_OVERFLOW_ERROR; |
1665 | 52 | return 0; |
1666 | 52 | } |
1667 | 1.21M | ce32 = Collation::makeCE32FromTagAndIndex(Collation::CONTRACTION_TAG, index) | flags; |
1668 | 1.21M | } |
1669 | 3.22M | U_ASSERT(cond == lastCond); |
1670 | 3.22M | firstCond->defaultCE32 = ce32; |
1671 | 3.22M | if(prefixLength == 0) { |
1672 | 441k | if(cond->next < 0) { |
1673 | | // No non-empty prefixes, only contractions. |
1674 | 362k | return ce32; |
1675 | 362k | } |
1676 | 2.77M | } else { |
1677 | 2.77M | prefix.remove(0, 1); // Remove the length unit. |
1678 | 2.77M | prefix.reverse(); |
1679 | 2.77M | prefixBuilder.add(prefix, static_cast<int32_t>(ce32), errorCode); |
1680 | 2.77M | if(cond->next < 0) { break; } |
1681 | 2.77M | } |
1682 | 3.22M | } |
1683 | 78.7k | U_ASSERT(head->defaultCE32 != Collation::NO_CE32); |
1684 | 78.7k | int32_t index = addContextTrie(head->defaultCE32, prefixBuilder, errorCode); |
1685 | 78.7k | if(U_FAILURE(errorCode)) { return 0; } |
1686 | 78.7k | if(index > Collation::MAX_INDEX) { |
1687 | 49 | errorCode = U_BUFFER_OVERFLOW_ERROR; |
1688 | 49 | return 0; |
1689 | 49 | } |
1690 | 78.6k | return Collation::makeCE32FromTagAndIndex(Collation::PREFIX_TAG, index); |
1691 | 78.7k | } |
1692 | | |
1693 | | int32_t |
1694 | | CollationDataBuilder::addContextTrie(uint32_t defaultCE32, UCharsTrieBuilder &trieBuilder, |
1695 | 1.29M | UErrorCode &errorCode) { |
1696 | 1.29M | UnicodeString context; |
1697 | 1.29M | context.append(static_cast<char16_t>(defaultCE32 >> 16)).append(static_cast<char16_t>(defaultCE32)); |
1698 | 1.29M | UnicodeString trieString; |
1699 | 1.29M | context.append(trieBuilder.buildUnicodeString(USTRINGTRIE_BUILD_SMALL, trieString, errorCode)); |
1700 | 1.29M | if(U_FAILURE(errorCode)) { return -1; } |
1701 | 1.29M | int32_t index = contexts.indexOf(context); |
1702 | 1.29M | if(index < 0) { |
1703 | 469k | index = contexts.length(); |
1704 | 469k | contexts.append(context); |
1705 | 469k | } |
1706 | 1.29M | return index; |
1707 | 1.29M | } |
1708 | | |
1709 | | void |
1710 | 3.97k | CollationDataBuilder::buildFastLatinTable(CollationData &data, UErrorCode &errorCode) { |
1711 | 3.97k | if(U_FAILURE(errorCode) || !fastLatinEnabled) { return; } |
1712 | | |
1713 | 3.97k | delete fastLatinBuilder; |
1714 | 3.97k | fastLatinBuilder = new CollationFastLatinBuilder(errorCode); |
1715 | 3.97k | if(fastLatinBuilder == nullptr) { |
1716 | 0 | errorCode = U_MEMORY_ALLOCATION_ERROR; |
1717 | 0 | return; |
1718 | 0 | } |
1719 | 3.97k | if(fastLatinBuilder->forData(data, errorCode)) { |
1720 | 3.81k | const uint16_t *table = fastLatinBuilder->getTable(); |
1721 | 3.81k | int32_t length = fastLatinBuilder->lengthOfTable(); |
1722 | 3.81k | if(base != nullptr && length == base->fastLatinTableLength && |
1723 | 3.81k | uprv_memcmp(table, base->fastLatinTable, length * 2) == 0) { |
1724 | | // Same fast Latin table as in the base, use that one instead. |
1725 | 1.60k | delete fastLatinBuilder; |
1726 | 1.60k | fastLatinBuilder = nullptr; |
1727 | 1.60k | table = base->fastLatinTable; |
1728 | 1.60k | } |
1729 | 3.81k | data.fastLatinTable = table; |
1730 | 3.81k | data.fastLatinTableLength = length; |
1731 | 3.81k | } else { |
1732 | 165 | delete fastLatinBuilder; |
1733 | 165 | fastLatinBuilder = nullptr; |
1734 | 165 | } |
1735 | 3.97k | } |
1736 | | |
1737 | | int32_t |
1738 | 8.60M | CollationDataBuilder::getCEs(const UnicodeString &s, int64_t ces[], int32_t cesLength) { |
1739 | 8.60M | return getCEs(s, 0, ces, cesLength); |
1740 | 8.60M | } |
1741 | | |
1742 | | int32_t |
1743 | | CollationDataBuilder::getCEs(const UnicodeString &prefix, const UnicodeString &s, |
1744 | 18.1M | int64_t ces[], int32_t cesLength) { |
1745 | 18.1M | int32_t prefixLength = prefix.length(); |
1746 | 18.1M | if(prefixLength == 0) { |
1747 | 18.0M | return getCEs(s, 0, ces, cesLength); |
1748 | 18.0M | } else { |
1749 | 157k | return getCEs(prefix + s, prefixLength, ces, cesLength); |
1750 | 157k | } |
1751 | 18.1M | } |
1752 | | |
1753 | | int32_t |
1754 | | CollationDataBuilder::getCEs(const UnicodeString &s, int32_t start, |
1755 | 26.7M | int64_t ces[], int32_t cesLength) { |
1756 | 26.7M | if(collIter == nullptr) { |
1757 | 5.70k | collIter = new DataBuilderCollationIterator(*this); |
1758 | 5.70k | if(collIter == nullptr) { return 0; } |
1759 | 5.70k | } |
1760 | 26.7M | return collIter->fetchCEs(s, start, ces, cesLength); |
1761 | 26.7M | } |
1762 | | |
1763 | | U_NAMESPACE_END |
1764 | | |
1765 | | #endif // !UCONFIG_NO_COLLATION |