clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name collationdatabuilder.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/home/maurizio/node-v18.6.0/out -resource-dir /usr/local/lib/clang/16.0.0 -D V8_DEPRECATION_WARNINGS -D V8_IMMINENT_DEPRECATION_WARNINGS -D _GLIBCXX_USE_CXX11_ABI=1 -D NODE_OPENSSL_CONF_NAME=nodejs_conf -D NODE_OPENSSL_HAS_QUIC -D __STDC_FORMAT_MACROS -D OPENSSL_NO_PINSHARED -D OPENSSL_THREADS -D U_COMMON_IMPLEMENTATION=1 -D U_I18N_IMPLEMENTATION=1 -D U_IO_IMPLEMENTATION=1 -D U_TOOLUTIL_IMPLEMENTATION=1 -D U_ATTRIBUTE_DEPRECATED= -D _CRT_SECURE_NO_DEPRECATE= -D U_STATIC_IMPLEMENTATION=1 -D UCONFIG_NO_SERVICE=1 -D U_ENABLE_DYLOAD=0 -D U_HAVE_STD_STRING=1 -D UCONFIG_NO_BREAK_ITERATION=0 -I ../deps/icu-small/source/common -I ../deps/icu-small/source/i18n -I ../deps/icu-small/source/tools/toolutil -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8 -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/x86_64-redhat-linux -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/backward -internal-isystem /usr/local/lib/clang/16.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/8/../../../../x86_64-redhat-linux/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Wno-unused-parameter -Wno-deprecated-declarations -Wno-strict-aliasing -std=gnu++17 -fdeprecated-macro -fdebug-compilation-dir=/home/maurizio/node-v18.6.0/out -ferror-limit 19 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-08-22-142216-507842-1 -x c++ ../deps/icu-small/source/i18n/collationdatabuilder.cpp
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 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 | CollationDataBuilder::CEModifier::~CEModifier() {} |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | struct ConditionalCE32 : public UMemory { |
| 54 | ConditionalCE32() |
| 55 | : context(), |
| 56 | ce32(0), defaultCE32(Collation::NO_CE32), builtCE32(Collation::NO_CE32), |
| 57 | 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 | next(-1) {} |
| 62 | |
| 63 | inline UBool hasContext() const { return context.length() > 1; } |
| 64 | inline int32_t prefixLength() const { return context.charAt(0); } |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | UnicodeString context; |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | uint32_t ce32; |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | uint32_t defaultCE32; |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | uint32_t builtCE32; |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | int32_t next; |
| 96 | }; |
| 97 | |
| 98 | U_CDECL_BEGIN |
| 99 | |
| 100 | U_CAPI void U_CALLCONV |
| 101 | uprv_deleteConditionalCE32(void *obj) { |
| 102 | delete static_cast<ConditionalCE32 *>(obj); |
| 103 | } |
| 104 | |
| 105 | U_CDECL_END |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 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) override; |
| 135 | virtual int32_t getOffset() const override; |
| 136 | |
| 137 | virtual UChar32 nextCodePoint(UErrorCode &errorCode) override; |
| 138 | virtual UChar32 previousCodePoint(UErrorCode &errorCode) override; |
| 139 | |
| 140 | protected: |
| 141 | virtual void forwardNumCodePoints(int32_t num, UErrorCode &errorCode) override; |
| 142 | virtual void backwardNumCodePoints(int32_t num, UErrorCode &errorCode) override; |
| 143 | |
| 144 | virtual uint32_t getDataCE32(UChar32 c) const override; |
| 145 | virtual uint32_t getCE32FromBuilderData(uint32_t ce32, UErrorCode &errorCode) override; |
| 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, FALSE), |
| 156 | builder(b), builderData(b.nfcImpl), |
| 157 | s(NULL), pos(0) { |
| 158 | builderData.base = builder.base; |
| 159 | |
| 160 | for(int32_t j = 0; j < CollationData::JAMO_CE32S_LENGTH; ++j) { |
| 161 | UChar32 jamo = CollationDataBuilder::jamoCpFromIndex(j); |
| 162 | jamoCE32s[j] = Collation::makeCE32FromTagAndIndex(Collation::BUILDER_DATA_TAG, jamo) | |
| 163 | CollationDataBuilder::IS_BUILDER_JAMO_CE32; |
| 164 | } |
| 165 | builderData.jamoCE32s = jamoCE32s; |
| 166 | } |
| 167 | |
| 168 | DataBuilderCollationIterator::~DataBuilderCollationIterator() {} |
| 169 | |
| 170 | int32_t |
| 171 | DataBuilderCollationIterator::fetchCEs(const UnicodeString &str, int32_t start, |
| 172 | int64_t ces[], int32_t cesLength) { |
| 173 | |
| 174 | builderData.ce32s = reinterpret_cast<const uint32_t *>(builder.ce32s.getBuffer()); |
| 175 | builderData.ces = builder.ce64s.getBuffer(); |
| 176 | builderData.contexts = builder.contexts.getBuffer(); |
| 177 | |
| 178 | reset(); |
| 179 | s = &str; |
| 180 | pos = start; |
| 181 | UErrorCode errorCode = U_ZERO_ERROR; |
| 182 | while(U_SUCCESS(errorCode) && pos < s->length()) { |
| 183 | |
| 184 | clearCEs(); |
| 185 | UChar32 c = s->char32At(pos); |
| 186 | pos += U16_LENGTH(c); |
| 187 | uint32_t ce32 = utrie2_get32(builder.trie, c); |
| 188 | const CollationData *d; |
| 189 | if(ce32 == Collation::FALLBACK_CE32) { |
| 190 | d = builder.base; |
| 191 | ce32 = builder.base->getCE32(c); |
| 192 | } else { |
| 193 | d = &builderData; |
| 194 | } |
| 195 | appendCEsFromCE32(d, c, ce32, TRUE, errorCode); |
| 196 | U_ASSERT(U_SUCCESS(errorCode)); |
| 197 | for(int32_t i = 0; i < getCEsLength(); ++i) { |
| 198 | int64_t ce = getCE(i); |
| 199 | if(ce != 0) { |
| 200 | if(cesLength < Collation::MAX_EXPANSION_LENGTH) { |
| 201 | ces[cesLength] = ce; |
| 202 | } |
| 203 | ++cesLength; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | return cesLength; |
| 208 | } |
| 209 | |
| 210 | void |
| 211 | DataBuilderCollationIterator::resetToOffset(int32_t newOffset) { |
| 212 | reset(); |
| 213 | pos = newOffset; |
| 214 | } |
| 215 | |
| 216 | int32_t |
| 217 | DataBuilderCollationIterator::getOffset() const { |
| 218 | return pos; |
| 219 | } |
| 220 | |
| 221 | UChar32 |
| 222 | DataBuilderCollationIterator::nextCodePoint(UErrorCode & ) { |
| 223 | if(pos == s->length()) { |
| 224 | return U_SENTINEL; |
| 225 | } |
| 226 | UChar32 c = s->char32At(pos); |
| 227 | pos += U16_LENGTH(c); |
| 228 | return c; |
| 229 | } |
| 230 | |
| 231 | UChar32 |
| 232 | DataBuilderCollationIterator::previousCodePoint(UErrorCode & ) { |
| 233 | if(pos == 0) { |
| 234 | return U_SENTINEL; |
| 235 | } |
| 236 | UChar32 c = s->char32At(pos - 1); |
| 237 | pos -= U16_LENGTH(c); |
| 238 | return c; |
| 239 | } |
| 240 | |
| 241 | void |
| 242 | DataBuilderCollationIterator::forwardNumCodePoints(int32_t num, UErrorCode & ) { |
| 243 | pos = s->moveIndex32(pos, num); |
| 244 | } |
| 245 | |
| 246 | void |
| 247 | DataBuilderCollationIterator::backwardNumCodePoints(int32_t num, UErrorCode & ) { |
| 248 | pos = s->moveIndex32(pos, -num); |
| 249 | } |
| 250 | |
| 251 | uint32_t |
| 252 | DataBuilderCollationIterator::getDataCE32(UChar32 c) const { |
| 253 | return utrie2_get32(builder.trie, c); |
| 254 | } |
| 255 | |
| 256 | uint32_t |
| 257 | DataBuilderCollationIterator::getCE32FromBuilderData(uint32_t ce32, UErrorCode &errorCode) { |
| 258 | if (U_FAILURE(errorCode)) { return 0; } |
| 259 | U_ASSERT(Collation::hasCE32Tag(ce32, Collation::BUILDER_DATA_TAG)); |
| 260 | if((ce32 & CollationDataBuilder::IS_BUILDER_JAMO_CE32) != 0) { |
| 261 | UChar32 jamo = Collation::indexFromCE32(ce32); |
| 262 | return utrie2_get32(builder.trie, jamo); |
| 263 | } else { |
| 264 | ConditionalCE32 *cond = builder.getConditionalCE32ForCE32(ce32); |
| 265 | if (cond == nullptr) { |
| 266 | errorCode = U_INTERNAL_PROGRAM_ERROR; |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | if(cond->builtCE32 == Collation::NO_CE32) { |
| 271 | |
| 272 | cond->builtCE32 = builder.buildContext(cond, errorCode); |
| 273 | if(errorCode == U_BUFFER_OVERFLOW_ERROR) { |
| 274 | errorCode = U_ZERO_ERROR; |
| 275 | builder.clearContexts(); |
| 276 | cond->builtCE32 = builder.buildContext(cond, errorCode); |
| 277 | } |
| 278 | builderData.contexts = builder.contexts.getBuffer(); |
| 279 | } |
| 280 | return cond->builtCE32; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | |
| 285 | |
| 286 | CollationDataBuilder::CollationDataBuilder(UErrorCode &errorCode) |
| 287 | : nfcImpl(*Normalizer2Factory::getNFCImpl(errorCode)), |
| 288 | base(NULL), baseSettings(NULL), |
| 289 | trie(NULL), |
| 290 | ce32s(errorCode), ce64s(errorCode), conditionalCE32s(errorCode), |
| 291 | modified(FALSE), |
| 292 | fastLatinEnabled(FALSE), fastLatinBuilder(NULL), |
| 293 | collIter(NULL) { |
| 294 | |
| 295 | ce32s.addElement(0, errorCode); |
| 296 | conditionalCE32s.setDeleter(uprv_deleteConditionalCE32); |
| 297 | } |
| 298 | |
| 299 | CollationDataBuilder::~CollationDataBuilder() { |
| 300 | utrie2_close(trie); |
| 301 | delete fastLatinBuilder; |
| 302 | delete collIter; |
| 303 | } |
| 304 | |
| 305 | void |
| 306 | CollationDataBuilder::initForTailoring(const CollationData *b, UErrorCode &errorCode) { |
| 307 | if(U_FAILURE(errorCode)) { return; } |
| 308 | if(trie != NULL) { |
| 309 | errorCode = U_INVALID_STATE_ERROR; |
| 310 | return; |
| 311 | } |
| 312 | if(b == NULL) { |
| 313 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
| 314 | return; |
| 315 | } |
| 316 | base = b; |
| 317 | |
| 318 | |
| 319 | trie = utrie2_open(Collation::FALLBACK_CE32, Collation::FFFD_CE32, &errorCode); |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | for(UChar32 c = 0xc0; c <= 0xff; ++c) { |
| 327 | utrie2_set32(trie, c, Collation::FALLBACK_CE32, &errorCode); |
| 328 | } |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | uint32_t hangulCE32 = Collation::makeCE32FromTagAndIndex(Collation::HANGUL_TAG, 0); |
| 335 | utrie2_setRange32(trie, Hangul::HANGUL_BASE, Hangul::HANGUL_END, hangulCE32, TRUE, &errorCode); |
| 336 | |
| 337 | |
| 338 | |
| 339 | unsafeBackwardSet.addAll(*b->unsafeBackwardSet); |
| 340 | |
| 341 | if(U_FAILURE(errorCode)) { return; } |
| 342 | } |
| 343 | |
| 344 | UBool |
| 345 | CollationDataBuilder::maybeSetPrimaryRange(UChar32 start, UChar32 end, |
| 346 | uint32_t primary, int32_t step, |
| 347 | UErrorCode &errorCode) { |
| 348 | if(U_FAILURE(errorCode)) { return FALSE; } |
| 349 | U_ASSERT(start <= end); |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | int32_t blockDelta = (end >> 5) - (start >> 5); |
| 361 | if(2 <= step && step <= 0x7f && |
| 362 | (blockDelta >= 3 || |
| 363 | (blockDelta > 0 && (start & 0x1f) <= 0x1c && (end & 0x1f) >= 3))) { |
| 364 | int64_t dataCE = ((int64_t)primary << 32) | (start << 8) | step; |
| 365 | if(isCompressiblePrimary(primary)) { dataCE |= 0x80; } |
| 366 | int32_t index = addCE(dataCE, errorCode); |
| 367 | if(U_FAILURE(errorCode)) { return 0; } |
| 368 | if(index > Collation::MAX_INDEX) { |
| 369 | errorCode = U_BUFFER_OVERFLOW_ERROR; |
| 370 | return 0; |
| 371 | } |
| 372 | uint32_t offsetCE32 = Collation::makeCE32FromTagAndIndex(Collation::OFFSET_TAG, index); |
| 373 | utrie2_setRange32(trie, start, end, offsetCE32, TRUE, &errorCode); |
| 374 | modified = TRUE; |
| 375 | return TRUE; |
| 376 | } else { |
| 377 | return FALSE; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | uint32_t |
| 382 | CollationDataBuilder::setPrimaryRangeAndReturnNext(UChar32 start, UChar32 end, |
| 383 | uint32_t primary, int32_t step, |
| 384 | UErrorCode &errorCode) { |
| 385 | if(U_FAILURE(errorCode)) { return 0; } |
| 386 | UBool isCompressible = isCompressiblePrimary(primary); |
| 387 | if(maybeSetPrimaryRange(start, end, primary, step, errorCode)) { |
| 388 | return Collation::incThreeBytePrimaryByOffset(primary, isCompressible, |
| 389 | (end - start + 1) * step); |
| 390 | } else { |
| 391 | |
| 392 | for(;;) { |
| 393 | utrie2_set32(trie, start, Collation::makeLongPrimaryCE32(primary), &errorCode); |
| 394 | ++start; |
| 395 | primary = Collation::incThreeBytePrimaryByOffset(primary, isCompressible, step); |
| 396 | if(start > end) { return primary; } |
| 397 | } |
| 398 | modified = TRUE; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | uint32_t |
| 403 | CollationDataBuilder::getCE32FromOffsetCE32(UBool fromBase, UChar32 c, uint32_t ce32) const { |
| 404 | int32_t i = Collation::indexFromCE32(ce32); |
| 405 | int64_t dataCE = fromBase ? base->ces[i] : ce64s.elementAti(i); |
| 406 | uint32_t p = Collation::getThreeBytePrimaryForOffsetData(c, dataCE); |
| 407 | return Collation::makeLongPrimaryCE32(p); |
| 408 | } |
| 409 | |
| 410 | UBool |
| 411 | CollationDataBuilder::isCompressibleLeadByte(uint32_t b) const { |
| 412 | return base->isCompressibleLeadByte(b); |
| 413 | } |
| 414 | |
| 415 | UBool |
| 416 | CollationDataBuilder::isAssigned(UChar32 c) const { |
| 417 | return Collation::isAssignedCE32(utrie2_get32(trie, c)); |
| 418 | } |
| 419 | |
| 420 | uint32_t |
| 421 | CollationDataBuilder::getLongPrimaryIfSingleCE(UChar32 c) const { |
| 422 | uint32_t ce32 = utrie2_get32(trie, c); |
| 423 | if(Collation::isLongPrimaryCE32(ce32)) { |
| 424 | return Collation::primaryFromLongPrimaryCE32(ce32); |
| 425 | } else { |
| 426 | return 0; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | int64_t |
| 431 | CollationDataBuilder::getSingleCE(UChar32 c, UErrorCode &errorCode) const { |
| 432 | if(U_FAILURE(errorCode)) { return 0; } |
| 433 | |
| 434 | UBool fromBase = FALSE; |
| 435 | uint32_t ce32 = utrie2_get32(trie, c); |
| 436 | if(ce32 == Collation::FALLBACK_CE32) { |
| 437 | fromBase = TRUE; |
| 438 | ce32 = base->getCE32(c); |
| 439 | } |
| 440 | while(Collation::isSpecialCE32(ce32)) { |
| 441 | switch(Collation::tagFromCE32(ce32)) { |
| 442 | case Collation::LATIN_EXPANSION_TAG: |
| 443 | case Collation::BUILDER_DATA_TAG: |
| 444 | case Collation::PREFIX_TAG: |
| 445 | case Collation::CONTRACTION_TAG: |
| 446 | case Collation::HANGUL_TAG: |
| 447 | case Collation::LEAD_SURROGATE_TAG: |
| 448 | errorCode = U_UNSUPPORTED_ERROR; |
| 449 | return 0; |
| 450 | case Collation::FALLBACK_TAG: |
| 451 | case Collation::RESERVED_TAG_3: |
| 452 | errorCode = U_INTERNAL_PROGRAM_ERROR; |
| 453 | return 0; |
| 454 | case Collation::LONG_PRIMARY_TAG: |
| 455 | return Collation::ceFromLongPrimaryCE32(ce32); |
| 456 | case Collation::LONG_SECONDARY_TAG: |
| 457 | return Collation::ceFromLongSecondaryCE32(ce32); |
| 458 | case Collation::EXPANSION32_TAG: |
| 459 | if(Collation::lengthFromCE32(ce32) == 1) { |
| 460 | int32_t i = Collation::indexFromCE32(ce32); |
| 461 | ce32 = fromBase ? base->ce32s[i] : ce32s.elementAti(i); |
| 462 | break; |
| 463 | } else { |
| 464 | errorCode = U_UNSUPPORTED_ERROR; |
| 465 | return 0; |
| 466 | } |
| 467 | case Collation::EXPANSION_TAG: { |
| 468 | if(Collation::lengthFromCE32(ce32) == 1) { |
| 469 | int32_t i = Collation::indexFromCE32(ce32); |
| 470 | return fromBase ? base->ces[i] : ce64s.elementAti(i); |
| 471 | } else { |
| 472 | errorCode = U_UNSUPPORTED_ERROR; |
| 473 | return 0; |
| 474 | } |
| 475 | } |
| 476 | case Collation::DIGIT_TAG: |
| 477 | |
| 478 | ce32 = ce32s.elementAti(Collation::indexFromCE32(ce32)); |
| 479 | break; |
| 480 | case Collation::U0000_TAG: |
| 481 | U_ASSERT(c == 0); |
| 482 | |
| 483 | ce32 = fromBase ? base->ce32s[0] : ce32s.elementAti(0); |
| 484 | break; |
| 485 | case Collation::OFFSET_TAG: |
| 486 | ce32 = getCE32FromOffsetCE32(fromBase, c, ce32); |
| 487 | break; |
| 488 | case Collation::IMPLICIT_TAG: |
| 489 | return Collation::unassignedCEFromCodePoint(c); |
| 490 | } |
| 491 | } |
| 492 | return Collation::ceFromSimpleCE32(ce32); |
| 493 | } |
| 494 | |
| 495 | int32_t |
| 496 | CollationDataBuilder::addCE(int64_t ce, UErrorCode &errorCode) { |
| 497 | int32_t length = ce64s.size(); |
| 498 | for(int32_t i = 0; i < length; ++i) { |
| 499 | if(ce == ce64s.elementAti(i)) { return i; } |
| 500 | } |
| 501 | ce64s.addElement(ce, errorCode); |
| 502 | return length; |
| 503 | } |
| 504 | |
| 505 | int32_t |
| 506 | CollationDataBuilder::addCE32(uint32_t ce32, UErrorCode &errorCode) { |
| 507 | int32_t length = ce32s.size(); |
| 508 | for(int32_t i = 0; i < length; ++i) { |
| 509 | if(ce32 == (uint32_t)ce32s.elementAti(i)) { return i; } |
| 510 | } |
| 511 | ce32s.addElement((int32_t)ce32, errorCode); |
| 512 | return length; |
| 513 | } |
| 514 | |
| 515 | int32_t |
| 516 | CollationDataBuilder::addConditionalCE32(const UnicodeString &context, uint32_t ce32, |
| 517 | UErrorCode &errorCode) { |
| 518 | if(U_FAILURE(errorCode)) { return -1; } |
| 519 | U_ASSERT(!context.isEmpty()); |
| 520 | int32_t index = conditionalCE32s.size(); |
| 521 | if(index > Collation::MAX_INDEX) { |
| 522 | errorCode = U_BUFFER_OVERFLOW_ERROR; |
| 523 | return -1; |
| 524 | } |
| 525 | LocalPointer<ConditionalCE32> cond(new ConditionalCE32(context, ce32), errorCode); |
| 526 | conditionalCE32s.adoptElement(cond.orphan(), errorCode); |
| 527 | if(U_FAILURE(errorCode)) { |
| 528 | return -1; |
| 529 | } |
| 530 | return index; |
| 531 | } |
| 532 | |
| 533 | void |
| 534 | CollationDataBuilder::add(const UnicodeString &prefix, const UnicodeString &s, |
| 535 | const int64_t ces[], int32_t cesLength, |
| 536 | UErrorCode &errorCode) { |
| 537 | uint32_t ce32 = encodeCEs(ces, cesLength, errorCode); |
| 538 | addCE32(prefix, s, ce32, errorCode); |
| 539 | } |
| 540 | |
| 541 | void |
| 542 | CollationDataBuilder::addCE32(const UnicodeString &prefix, const UnicodeString &s, |
| 543 | uint32_t ce32, UErrorCode &errorCode) { |
| 544 | if(U_FAILURE(errorCode)) { return; } |
| 545 | if(s.isEmpty()) { |
| 546 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
| 547 | return; |
| 548 | } |
| 549 | if(trie == NULL || utrie2_isFrozen(trie)) { |
| 550 | errorCode = U_INVALID_STATE_ERROR; |
| 551 | return; |
| 552 | } |
| 553 | UChar32 c = s.char32At(0); |
| 554 | int32_t cLength = U16_LENGTH(c); |
| 555 | uint32_t oldCE32 = utrie2_get32(trie, c); |
| 556 | UBool hasContext = !prefix.isEmpty() || s.length() > cLength; |
| 557 | if(oldCE32 == Collation::FALLBACK_CE32) { |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | uint32_t baseCE32 = base->getFinalCE32(base->getCE32(c)); |
| 563 | if(hasContext || Collation::ce32HasContext(baseCE32)) { |
| 564 | oldCE32 = copyFromBaseCE32(c, baseCE32, TRUE, errorCode); |
| 565 | utrie2_set32(trie, c, oldCE32, &errorCode); |
| 566 | if(U_FAILURE(errorCode)) { return; } |
| 567 | } |
| 568 | } |
| 569 | if(!hasContext) { |
| 570 | |
| 571 | if(!isBuilderContextCE32(oldCE32)) { |
| 572 | utrie2_set32(trie, c, ce32, &errorCode); |
| 573 | } else { |
| 574 | ConditionalCE32 *cond = getConditionalCE32ForCE32(oldCE32); |
| 575 | cond->builtCE32 = Collation::NO_CE32; |
| 576 | cond->ce32 = ce32; |
| 577 | } |
| 578 | } else { |
| 579 | ConditionalCE32 *cond; |
| 580 | if(!isBuilderContextCE32(oldCE32)) { |
| 581 | |
| 582 | |
| 583 | int32_t index = addConditionalCE32(UnicodeString((UChar)0), oldCE32, errorCode); |
| 584 | if(U_FAILURE(errorCode)) { return; } |
| 585 | uint32_t contextCE32 = makeBuilderContextCE32(index); |
| 586 | utrie2_set32(trie, c, contextCE32, &errorCode); |
| 587 | contextChars.add(c); |
| 588 | cond = getConditionalCE32(index); |
| 589 | } else { |
| 590 | cond = getConditionalCE32ForCE32(oldCE32); |
| 591 | cond->builtCE32 = Collation::NO_CE32; |
| 592 | } |
| 593 | UnicodeString suffix(s, cLength); |
| 594 | UnicodeString context((UChar)prefix.length()); |
| 595 | context.append(prefix).append(suffix); |
| 596 | unsafeBackwardSet.addAll(suffix); |
| 597 | for(;;) { |
| 598 | |
| 599 | int32_t next = cond->next; |
| 600 | if(next < 0) { |
| 601 | |
| 602 | int32_t index = addConditionalCE32(context, ce32, errorCode); |
| 603 | if(U_FAILURE(errorCode)) { return; } |
| 604 | cond->next = index; |
| 605 | break; |
| 606 | } |
| 607 | ConditionalCE32 *nextCond = getConditionalCE32(next); |
| 608 | int8_t cmp = context.compare(nextCond->context); |
| 609 | if(cmp < 0) { |
| 610 | |
| 611 | int32_t index = addConditionalCE32(context, ce32, errorCode); |
| 612 | if(U_FAILURE(errorCode)) { return; } |
| 613 | cond->next = index; |
| 614 | getConditionalCE32(index)->next = next; |
| 615 | break; |
| 616 | } else if(cmp == 0) { |
| 617 | |
| 618 | nextCond->ce32 = ce32; |
| 619 | break; |
| 620 | } |
| 621 | cond = nextCond; |
| 622 | } |
| 623 | } |
| 624 | modified = TRUE; |
| 625 | } |
| 626 | |
| 627 | uint32_t |
| 628 | CollationDataBuilder::encodeOneCEAsCE32(int64_t ce) { |
| 629 | uint32_t p = (uint32_t)(ce >> 32); |
| 630 | uint32_t lower32 = (uint32_t)ce; |
| 631 | uint32_t t = (uint32_t)(ce & 0xffff); |
| 632 | U_ASSERT((t & 0xc000) != 0xc000); |
| 633 | if((ce & INT64_C(0xffff00ff00ff)) == 0) { |
| 634 | |
| 635 | return p | (lower32 >> 16) | (t >> 8); |
| 636 | } else if((ce & INT64_C(0xffffffffff)) == Collation::COMMON_SEC_AND_TER_CE) { |
| 637 | |
| 638 | return Collation::makeLongPrimaryCE32(p); |
| 639 | } else if(p == 0 && (t & 0xff) == 0) { |
| 640 | |
| 641 | return Collation::makeLongSecondaryCE32(lower32); |
| 642 | } |
| 643 | return Collation::NO_CE32; |
| 644 | } |
| 645 | |
| 646 | uint32_t |
| 647 | CollationDataBuilder::encodeOneCE(int64_t ce, UErrorCode &errorCode) { |
| 648 | |
| 649 | uint32_t ce32 = encodeOneCEAsCE32(ce); |
| 650 | if(ce32 != Collation::NO_CE32) { return ce32; } |
| 651 | int32_t index = addCE(ce, errorCode); |
| 652 | if(U_FAILURE(errorCode)) { return 0; } |
| 653 | if(index > Collation::MAX_INDEX) { |
| 654 | errorCode = U_BUFFER_OVERFLOW_ERROR; |
| 655 | return 0; |
| 656 | } |
| 657 | return Collation::makeCE32FromTagIndexAndLength(Collation::EXPANSION_TAG, index, 1); |
| 658 | } |
| 659 | |
| 660 | uint32_t |
| 661 | CollationDataBuilder::encodeCEs(const int64_t ces[], int32_t cesLength, |
| 662 | UErrorCode &errorCode) { |
| 663 | if(U_FAILURE(errorCode)) { return 0; } |
| 664 | if(cesLength < 0 || cesLength > Collation::MAX_EXPANSION_LENGTH) { |
| 665 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
| 666 | return 0; |
| 667 | } |
| 668 | if(trie == NULL || utrie2_isFrozen(trie)) { |
| 669 | errorCode = U_INVALID_STATE_ERROR; |
| 670 | return 0; |
| 671 | } |
| 672 | if(cesLength == 0) { |
| 673 | |
| 674 | |
| 675 | return encodeOneCEAsCE32(0); |
| 676 | } else if(cesLength == 1) { |
| 677 | return encodeOneCE(ces[0], errorCode); |
| 678 | } else if(cesLength == 2) { |
| 679 | |
| 680 | int64_t ce0 = ces[0]; |
| 681 | int64_t ce1 = ces[1]; |
| 682 | uint32_t p0 = (uint32_t)(ce0 >> 32); |
| 683 | if((ce0 & INT64_C(0xffffffffff00ff)) == Collation::COMMON_SECONDARY_CE && |
| 684 | (ce1 & INT64_C(0xffffffff00ffffff)) == Collation::COMMON_TERTIARY_CE && |
| 685 | p0 != 0) { |
| 686 | |
| 687 | return |
| 688 | p0 | |
| 689 | (((uint32_t)ce0 & 0xff00u) << 8) | |
| 690 | (uint32_t)(ce1 >> 16) | |
| 691 | Collation::SPECIAL_CE32_LOW_BYTE | |
| 692 | Collation::LATIN_EXPANSION_TAG; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | int32_t newCE32s[Collation::MAX_EXPANSION_LENGTH]; |
| 697 | for(int32_t i = 0;; ++i) { |
| 698 | if(i == cesLength) { |
| 699 | return encodeExpansion32(newCE32s, cesLength, errorCode); |
| 700 | } |
| 701 | uint32_t ce32 = encodeOneCEAsCE32(ces[i]); |
| 702 | if(ce32 == Collation::NO_CE32) { break; } |
| 703 | newCE32s[i] = (int32_t)ce32; |
| 704 | } |
| 705 | return encodeExpansion(ces, cesLength, errorCode); |
| 706 | } |
| 707 | |
| 708 | uint32_t |
| 709 | CollationDataBuilder::encodeExpansion(const int64_t ces[], int32_t length, UErrorCode &errorCode) { |
| 710 | if(U_FAILURE(errorCode)) { return 0; } |
| 711 | |
| 712 | int64_t first = ces[0]; |
| 713 | int32_t ce64sMax = ce64s.size() - length; |
| 714 | for(int32_t i = 0; i <= ce64sMax; ++i) { |
| 715 | if(first == ce64s.elementAti(i)) { |
| 716 | if(i > Collation::MAX_INDEX) { |
| 717 | errorCode = U_BUFFER_OVERFLOW_ERROR; |
| 718 | return 0; |
| 719 | } |
| 720 | for(int32_t j = 1;; ++j) { |
| 721 | if(j == length) { |
| 722 | return Collation::makeCE32FromTagIndexAndLength( |
| 723 | Collation::EXPANSION_TAG, i, length); |
| 724 | } |
| 725 | if(ce64s.elementAti(i + j) != ces[j]) { break; } |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | int32_t i = ce64s.size(); |
| 731 | if(i > Collation::MAX_INDEX) { |
| 732 | errorCode = U_BUFFER_OVERFLOW_ERROR; |
| 733 | return 0; |
| 734 | } |
| 735 | for(int32_t j = 0; j < length; ++j) { |
| 736 | ce64s.addElement(ces[j], errorCode); |
| 737 | } |
| 738 | return Collation::makeCE32FromTagIndexAndLength(Collation::EXPANSION_TAG, i, length); |
| 739 | } |
| 740 | |
| 741 | uint32_t |
| 742 | CollationDataBuilder::encodeExpansion32(const int32_t newCE32s[], int32_t length, |
| 743 | UErrorCode &errorCode) { |
| 744 | if(U_FAILURE(errorCode)) { return 0; } |
| 745 | |
| 746 | int32_t first = newCE32s[0]; |
| 747 | int32_t ce32sMax = ce32s.size() - length; |
| 748 | for(int32_t i = 0; i <= ce32sMax; ++i) { |
| 749 | if(first == ce32s.elementAti(i)) { |
| 750 | if(i > Collation::MAX_INDEX) { |
| 751 | errorCode = U_BUFFER_OVERFLOW_ERROR; |
| 752 | return 0; |
| 753 | } |
| 754 | for(int32_t j = 1;; ++j) { |
| 755 | if(j == length) { |
| 756 | return Collation::makeCE32FromTagIndexAndLength( |
| 757 | Collation::EXPANSION32_TAG, i, length); |
| 758 | } |
| 759 | if(ce32s.elementAti(i + j) != newCE32s[j]) { break; } |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | int32_t i = ce32s.size(); |
| 765 | if(i > Collation::MAX_INDEX) { |
| 766 | errorCode = U_BUFFER_OVERFLOW_ERROR; |
| 767 | return 0; |
| 768 | } |
| 769 | for(int32_t j = 0; j < length; ++j) { |
| 770 | ce32s.addElement(newCE32s[j], errorCode); |
| 771 | } |
| 772 | return Collation::makeCE32FromTagIndexAndLength(Collation::EXPANSION32_TAG, i, length); |
| 773 | } |
| 774 | |
| 775 | uint32_t |
| 776 | CollationDataBuilder::copyFromBaseCE32(UChar32 c, uint32_t ce32, UBool withContext, |
| 777 | UErrorCode &errorCode) { |
| 778 | if(U_FAILURE(errorCode)) { return 0; } |
| 779 | if(!Collation::isSpecialCE32(ce32)) { return ce32; } |
| 780 | switch(Collation::tagFromCE32(ce32)) { |
| 781 | case Collation::LONG_PRIMARY_TAG: |
| 782 | case Collation::LONG_SECONDARY_TAG: |
| 783 | case Collation::LATIN_EXPANSION_TAG: |
| 784 | |
| 785 | break; |
| 786 | case Collation::EXPANSION32_TAG: { |
| 787 | const uint32_t *baseCE32s = base->ce32s + Collation::indexFromCE32(ce32); |
| 788 | int32_t length = Collation::lengthFromCE32(ce32); |
| 789 | ce32 = encodeExpansion32( |
| 790 | reinterpret_cast<const int32_t *>(baseCE32s), length, errorCode); |
| 791 | break; |
| 792 | } |
| 793 | case Collation::EXPANSION_TAG: { |
| 794 | const int64_t *baseCEs = base->ces + Collation::indexFromCE32(ce32); |
| 795 | int32_t length = Collation::lengthFromCE32(ce32); |
| 796 | ce32 = encodeExpansion(baseCEs, length, errorCode); |
| 797 | break; |
| 798 | } |
| 799 | case Collation::PREFIX_TAG: { |
| 800 | |
| 801 | |
| 802 | const UChar *p = base->contexts + Collation::indexFromCE32(ce32); |
| 803 | ce32 = CollationData::readCE32(p); |
| 804 | if(!withContext) { |
| 805 | return copyFromBaseCE32(c, ce32, FALSE, errorCode); |
| 806 | } |
| 807 | ConditionalCE32 head; |
| 808 | UnicodeString context((UChar)0); |
| 809 | int32_t index; |
| 810 | if(Collation::isContractionCE32(ce32)) { |
| 811 | index = copyContractionsFromBaseCE32(context, c, ce32, &head, errorCode); |
| 812 | } else { |
| 813 | ce32 = copyFromBaseCE32(c, ce32, TRUE, errorCode); |
| 814 | head.next = index = addConditionalCE32(context, ce32, errorCode); |
| 815 | } |
| 816 | if(U_FAILURE(errorCode)) { return 0; } |
| 817 | ConditionalCE32 *cond = getConditionalCE32(index); |
| 818 | UCharsTrie::Iterator prefixes(p + 2, 0, errorCode); |
| 819 | while(prefixes.next(errorCode)) { |
| 820 | context = prefixes.getString(); |
| 821 | context.reverse(); |
| 822 | context.insert(0, (UChar)context.length()); |
| 823 | ce32 = (uint32_t)prefixes.getValue(); |
| 824 | if(Collation::isContractionCE32(ce32)) { |
| 825 | index = copyContractionsFromBaseCE32(context, c, ce32, cond, errorCode); |
| 826 | } else { |
| 827 | ce32 = copyFromBaseCE32(c, ce32, TRUE, errorCode); |
| 828 | cond->next = index = addConditionalCE32(context, ce32, errorCode); |
| 829 | } |
| 830 | if(U_FAILURE(errorCode)) { return 0; } |
| 831 | cond = getConditionalCE32(index); |
| 832 | } |
| 833 | ce32 = makeBuilderContextCE32(head.next); |
| 834 | contextChars.add(c); |
| 835 | break; |
| 836 | } |
| 837 | case Collation::CONTRACTION_TAG: { |
| 838 | if(!withContext) { |
| 839 | const UChar *p = base->contexts + Collation::indexFromCE32(ce32); |
| 840 | ce32 = CollationData::readCE32(p); |
| 841 | return copyFromBaseCE32(c, ce32, FALSE, errorCode); |
| 842 | } |
| 843 | ConditionalCE32 head; |
| 844 | UnicodeString context((UChar)0); |
| 845 | copyContractionsFromBaseCE32(context, c, ce32, &head, errorCode); |
| 846 | ce32 = makeBuilderContextCE32(head.next); |
| 847 | contextChars.add(c); |
| 848 | break; |
| 849 | } |
| 850 | case Collation::HANGUL_TAG: |
| 851 | errorCode = U_UNSUPPORTED_ERROR; |
| 852 | break; |
| 853 | case Collation::OFFSET_TAG: |
| 854 | ce32 = getCE32FromOffsetCE32(TRUE, c, ce32); |
| 855 | break; |
| 856 | case Collation::IMPLICIT_TAG: |
| 857 | ce32 = encodeOneCE(Collation::unassignedCEFromCodePoint(c), errorCode); |
| 858 | break; |
| 859 | default: |
| 860 | UPRV_UNREACHABLE_EXIT; |
| 861 | } |
| 862 | return ce32; |
| 863 | } |
| 864 | |
| 865 | int32_t |
| 866 | CollationDataBuilder::copyContractionsFromBaseCE32(UnicodeString &context, UChar32 c, uint32_t ce32, |
| 867 | ConditionalCE32 *cond, UErrorCode &errorCode) { |
| 868 | if(U_FAILURE(errorCode)) { return 0; } |
| 869 | const UChar *p = base->contexts + Collation::indexFromCE32(ce32); |
| 870 | int32_t index; |
| 871 | if((ce32 & Collation::CONTRACT_SINGLE_CP_NO_MATCH) != 0) { |
| 872 | |
| 873 | |
| 874 | |
| 875 | U_ASSERT(context.length() > 1); |
| 876 | index = -1; |
| 877 | } else { |
| 878 | ce32 = CollationData::readCE32(p); |
| 879 | U_ASSERT(!Collation::isContractionCE32(ce32)); |
| 880 | ce32 = copyFromBaseCE32(c, ce32, TRUE, errorCode); |
| 881 | cond->next = index = addConditionalCE32(context, ce32, errorCode); |
| 882 | if(U_FAILURE(errorCode)) { return 0; } |
| 883 | cond = getConditionalCE32(index); |
| 884 | } |
| 885 | |
| 886 | int32_t suffixStart = context.length(); |
| 887 | UCharsTrie::Iterator suffixes(p + 2, 0, errorCode); |
| 888 | while(suffixes.next(errorCode)) { |
| 889 | context.append(suffixes.getString()); |
| 890 | ce32 = copyFromBaseCE32(c, (uint32_t)suffixes.getValue(), TRUE, errorCode); |
| 891 | cond->next = index = addConditionalCE32(context, ce32, errorCode); |
| 892 | if(U_FAILURE(errorCode)) { return 0; } |
| 893 | |
| 894 | |
| 895 | cond = getConditionalCE32(index); |
| 896 | context.truncate(suffixStart); |
| 897 | } |
| 898 | U_ASSERT(index >= 0); |
| 899 | return index; |
| 900 | } |
| 901 | |
| 902 | class CopyHelper { |
| 903 | public: |
| 904 | CopyHelper(const CollationDataBuilder &s, CollationDataBuilder &d, |
| 905 | const CollationDataBuilder::CEModifier &m, UErrorCode &initialErrorCode) |
| 906 | : src(s), dest(d), modifier(m), |
| 907 | errorCode(initialErrorCode) {} |
| 908 | |
| 909 | UBool copyRangeCE32(UChar32 start, UChar32 end, uint32_t ce32) { |
| 910 | ce32 = copyCE32(ce32); |
| 911 | utrie2_setRange32(dest.trie, start, end, ce32, TRUE, &errorCode); |
| 912 | if(CollationDataBuilder::isBuilderContextCE32(ce32)) { |
| 913 | dest.contextChars.add(start, end); |
| 914 | } |
| 915 | return U_SUCCESS(errorCode); |
| 916 | } |
| 917 | |
| 918 | uint32_t copyCE32(uint32_t ce32) { |
| 919 | if(!Collation::isSpecialCE32(ce32)) { |
| 920 | int64_t ce = modifier.modifyCE32(ce32); |
| 921 | if(ce != Collation::NO_CE) { |
| 922 | ce32 = dest.encodeOneCE(ce, errorCode); |
| 923 | } |
| 924 | } else { |
| 925 | int32_t tag = Collation::tagFromCE32(ce32); |
| 926 | if(tag == Collation::EXPANSION32_TAG) { |
| 927 | const uint32_t *srcCE32s = reinterpret_cast<uint32_t *>(src.ce32s.getBuffer()); |
| 928 | srcCE32s += Collation::indexFromCE32(ce32); |
| 929 | int32_t length = Collation::lengthFromCE32(ce32); |
| 930 | |
| 931 | |
| 932 | UBool isModified = FALSE; |
| 933 | for(int32_t i = 0; i < length; ++i) { |
| 934 | ce32 = srcCE32s[i]; |
| 935 | int64_t ce; |
| 936 | if(Collation::isSpecialCE32(ce32) || |
| 937 | (ce = modifier.modifyCE32(ce32)) == Collation::NO_CE) { |
| 938 | if(isModified) { |
| 939 | modifiedCEs[i] = Collation::ceFromCE32(ce32); |
| 940 | } |
| 941 | } else { |
| 942 | if(!isModified) { |
| 943 | for(int32_t j = 0; j < i; ++j) { |
| 944 | modifiedCEs[j] = Collation::ceFromCE32(srcCE32s[j]); |
| 945 | } |
| 946 | isModified = TRUE; |
| 947 | } |
| 948 | modifiedCEs[i] = ce; |
| 949 | } |
| 950 | } |
| 951 | if(isModified) { |
| 952 | ce32 = dest.encodeCEs(modifiedCEs, length, errorCode); |
| 953 | } else { |
| 954 | ce32 = dest.encodeExpansion32( |
| 955 | reinterpret_cast<const int32_t *>(srcCE32s), length, errorCode); |
| 956 | } |
| 957 | } else if(tag == Collation::EXPANSION_TAG) { |
| 958 | const int64_t *srcCEs = src.ce64s.getBuffer(); |
| 959 | srcCEs += Collation::indexFromCE32(ce32); |
| 960 | int32_t length = Collation::lengthFromCE32(ce32); |
| 961 | |
| 962 | |
| 963 | UBool isModified = FALSE; |
| 964 | for(int32_t i = 0; i < length; ++i) { |
| 965 | int64_t srcCE = srcCEs[i]; |
| 966 | int64_t ce = modifier.modifyCE(srcCE); |
| 967 | if(ce == Collation::NO_CE) { |
| 968 | if(isModified) { |
| 969 | modifiedCEs[i] = srcCE; |
| 970 | } |
| 971 | } else { |
| 972 | if(!isModified) { |
| 973 | for(int32_t j = 0; j < i; ++j) { |
| 974 | modifiedCEs[j] = srcCEs[j]; |
| 975 | } |
| 976 | isModified = TRUE; |
| 977 | } |
| 978 | modifiedCEs[i] = ce; |
| 979 | } |
| 980 | } |
| 981 | if(isModified) { |
| 982 | ce32 = dest.encodeCEs(modifiedCEs, length, errorCode); |
| 983 | } else { |
| 984 | ce32 = dest.encodeExpansion(srcCEs, length, errorCode); |
| 985 | } |
| 986 | } else if(tag == Collation::BUILDER_DATA_TAG) { |
| 987 | |
| 988 | ConditionalCE32 *cond = src.getConditionalCE32ForCE32(ce32); |
| 989 | U_ASSERT(!cond->hasContext()); |
| 990 | int32_t destIndex = dest.addConditionalCE32( |
| 991 | cond->context, copyCE32(cond->ce32), errorCode); |
| 992 | ce32 = CollationDataBuilder::makeBuilderContextCE32(destIndex); |
| 993 | while(cond->next >= 0) { |
| 994 | cond = src.getConditionalCE32(cond->next); |
| 995 | ConditionalCE32 *prevDestCond = dest.getConditionalCE32(destIndex); |
| 996 | destIndex = dest.addConditionalCE32( |
| 997 | cond->context, copyCE32(cond->ce32), errorCode); |
| 998 | int32_t suffixStart = cond->prefixLength() + 1; |
| 999 | dest.unsafeBackwardSet.addAll(cond->context.tempSubString(suffixStart)); |
| 1000 | prevDestCond->next = destIndex; |
| 1001 | } |
| 1002 | } else { |
| 1003 | |
| 1004 | |
| 1005 | U_ASSERT(tag == Collation::LONG_PRIMARY_TAG || |
| 1006 | tag == Collation::LONG_SECONDARY_TAG || |
| 1007 | tag == Collation::LATIN_EXPANSION_TAG || |
| 1008 | tag == Collation::HANGUL_TAG); |
| 1009 | } |
| 1010 | } |
| 1011 | return ce32; |
| 1012 | } |
| 1013 | |
| 1014 | const CollationDataBuilder &src; |
| 1015 | CollationDataBuilder &dest; |
| 1016 | const CollationDataBuilder::CEModifier &modifier; |
| 1017 | int64_t modifiedCEs[Collation::MAX_EXPANSION_LENGTH]; |
| 1018 | UErrorCode errorCode; |
| 1019 | }; |
| 1020 | |
| 1021 | U_CDECL_BEGIN |
| 1022 | |
| 1023 | static UBool U_CALLCONV |
| 1024 | enumRangeForCopy(const void *context, UChar32 start, UChar32 end, uint32_t value) { |
| 1025 | return |
| 1026 | value == Collation::UNASSIGNED_CE32 || value == Collation::FALLBACK_CE32 || |
| 1027 | ((CopyHelper *)context)->copyRangeCE32(start, end, value); |
| 1028 | } |
| 1029 | |
| 1030 | U_CDECL_END |
| 1031 | |
| 1032 | void |
| 1033 | CollationDataBuilder::copyFrom(const CollationDataBuilder &src, const CEModifier &modifier, |
| 1034 | UErrorCode &errorCode) { |
| 1035 | if(U_FAILURE(errorCode)) { return; } |
| 1036 | if(trie == NULL || utrie2_isFrozen(trie)) { |
| 1037 | errorCode = U_INVALID_STATE_ERROR; |
| 1038 | return; |
| 1039 | } |
| 1040 | CopyHelper helper(src, *this, modifier, errorCode); |
| 1041 | utrie2_enum(src.trie, NULL, enumRangeForCopy, &helper); |
| 1042 | errorCode = helper.errorCode; |
| 1043 | |
| 1044 | |
| 1045 | |
| 1046 | modified |= src.modified; |
| 1047 | } |
| 1048 | |
| 1049 | void |
| 1050 | CollationDataBuilder::optimize(const UnicodeSet &set, UErrorCode &errorCode) { |
| 1051 | if(U_FAILURE(errorCode) || set.isEmpty()) { return; } |
| 1052 | UnicodeSetIterator iter(set); |
| 1053 | while(iter.next() && !iter.isString()) { |
| 1054 | UChar32 c = iter.getCodepoint(); |
| 1055 | uint32_t ce32 = utrie2_get32(trie, c); |
| 1056 | if(ce32 == Collation::FALLBACK_CE32) { |
| 1057 | ce32 = base->getFinalCE32(base->getCE32(c)); |
| 1058 | ce32 = copyFromBaseCE32(c, ce32, TRUE, errorCode); |
| 1059 | utrie2_set32(trie, c, ce32, &errorCode); |
| 1060 | } |
| 1061 | } |
| 1062 | modified = TRUE; |
| 1063 | } |
| 1064 | |
| 1065 | void |
| 1066 | CollationDataBuilder::suppressContractions(const UnicodeSet &set, UErrorCode &errorCode) { |
| 1067 | if(U_FAILURE(errorCode) || set.isEmpty()) { return; } |
| 1068 | UnicodeSetIterator iter(set); |
| 1069 | while(iter.next() && !iter.isString()) { |
| 1070 | UChar32 c = iter.getCodepoint(); |
| 1071 | uint32_t ce32 = utrie2_get32(trie, c); |
| 1072 | if(ce32 == Collation::FALLBACK_CE32) { |
| 1073 | ce32 = base->getFinalCE32(base->getCE32(c)); |
| 1074 | if(Collation::ce32HasContext(ce32)) { |
| 1075 | ce32 = copyFromBaseCE32(c, ce32, FALSE , errorCode); |
| 1076 | utrie2_set32(trie, c, ce32, &errorCode); |
| 1077 | } |
| 1078 | } else if(isBuilderContextCE32(ce32)) { |
| 1079 | ce32 = getConditionalCE32ForCE32(ce32)->ce32; |
| 1080 | |
| 1081 | |
| 1082 | |
| 1083 | utrie2_set32(trie, c, ce32, &errorCode); |
| 1084 | contextChars.remove(c); |
| 1085 | } |
| 1086 | } |
| 1087 | modified = TRUE; |
| 1088 | } |
| 1089 | |
| 1090 | UBool |
| 1091 | CollationDataBuilder::getJamoCE32s(uint32_t jamoCE32s[], UErrorCode &errorCode) { |
| 1092 | if(U_FAILURE(errorCode)) { return FALSE; } |
| |
| 1093 | UBool anyJamoAssigned = base == NULL; |
| 7 | | Assuming field 'base' is equal to NULL | |
|
| 8 | | Assuming pointer value is null | |
|
| 1094 | UBool needToCopyFromBase = FALSE; |
| 1095 | for(int32_t j = 0; j < CollationData::JAMO_CE32S_LENGTH; ++j) { |
| 9 | | Loop condition is true. Entering loop body | |
|
| 1096 | UChar32 jamo = jamoCpFromIndex(j); |
| 1097 | UBool fromBase = FALSE; |
| 1098 | uint32_t ce32 = utrie2_get32(trie, jamo); |
| 1099 | anyJamoAssigned |= Collation::isAssignedCE32(ce32); |
| 1100 | |
| 1101 | |
| 1102 | if(ce32 == Collation::FALLBACK_CE32) { |
| |
| 1103 | fromBase = TRUE; |
| 1104 | ce32 = base->getCE32(jamo); |
| 11 | | Called C++ object pointer is null |
|
| 1105 | } |
| 1106 | if(Collation::isSpecialCE32(ce32)) { |
| 1107 | switch(Collation::tagFromCE32(ce32)) { |
| 1108 | case Collation::LONG_PRIMARY_TAG: |
| 1109 | case Collation::LONG_SECONDARY_TAG: |
| 1110 | case Collation::LATIN_EXPANSION_TAG: |
| 1111 | |
| 1112 | break; |
| 1113 | case Collation::EXPANSION32_TAG: |
| 1114 | case Collation::EXPANSION_TAG: |
| 1115 | case Collation::PREFIX_TAG: |
| 1116 | case Collation::CONTRACTION_TAG: |
| 1117 | if(fromBase) { |
| 1118 | |
| 1119 | ce32 = Collation::FALLBACK_CE32; |
| 1120 | needToCopyFromBase = TRUE; |
| 1121 | } |
| 1122 | break; |
| 1123 | case Collation::IMPLICIT_TAG: |
| 1124 | |
| 1125 | U_ASSERT(fromBase); |
| 1126 | ce32 = Collation::FALLBACK_CE32; |
| 1127 | needToCopyFromBase = TRUE; |
| 1128 | break; |
| 1129 | case Collation::OFFSET_TAG: |
| 1130 | ce32 = getCE32FromOffsetCE32(fromBase, jamo, ce32); |
| 1131 | break; |
| 1132 | case Collation::FALLBACK_TAG: |
| 1133 | case Collation::RESERVED_TAG_3: |
| 1134 | case Collation::BUILDER_DATA_TAG: |
| 1135 | case Collation::DIGIT_TAG: |
| 1136 | case Collation::U0000_TAG: |
| 1137 | case Collation::HANGUL_TAG: |
| 1138 | case Collation::LEAD_SURROGATE_TAG: |
| 1139 | errorCode = U_INTERNAL_PROGRAM_ERROR; |
| 1140 | return FALSE; |
| 1141 | } |
| 1142 | } |
| 1143 | jamoCE32s[j] = ce32; |
| 1144 | } |
| 1145 | if(anyJamoAssigned && needToCopyFromBase) { |
| 1146 | for(int32_t j = 0; j < CollationData::JAMO_CE32S_LENGTH; ++j) { |
| 1147 | if(jamoCE32s[j] == Collation::FALLBACK_CE32) { |
| 1148 | UChar32 jamo = jamoCpFromIndex(j); |
| 1149 | jamoCE32s[j] = copyFromBaseCE32(jamo, base->getCE32(jamo), |
| 1150 | TRUE, errorCode); |
| 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | return anyJamoAssigned && U_SUCCESS(errorCode); |
| 1155 | } |
| 1156 | |
| 1157 | void |
| 1158 | CollationDataBuilder::setDigitTags(UErrorCode &errorCode) { |
| 1159 | UnicodeSet digits(UNICODE_STRING_SIMPLE("[:Nd:]"), errorCode); |
| 1160 | if(U_FAILURE(errorCode)) { return; } |
| 1161 | UnicodeSetIterator iter(digits); |
| 1162 | while(iter.next()) { |
| 1163 | U_ASSERT(!iter.isString()); |
| 1164 | UChar32 c = iter.getCodepoint(); |
| 1165 | uint32_t ce32 = utrie2_get32(trie, c); |
| 1166 | if(ce32 != Collation::FALLBACK_CE32 && ce32 != Collation::UNASSIGNED_CE32) { |
| 1167 | int32_t index = addCE32(ce32, errorCode); |
| 1168 | if(U_FAILURE(errorCode)) { return; } |
| 1169 | if(index > Collation::MAX_INDEX) { |
| 1170 | errorCode = U_BUFFER_OVERFLOW_ERROR; |
| 1171 | return; |
| 1172 | } |
| 1173 | ce32 = Collation::makeCE32FromTagIndexAndLength( |
| 1174 | Collation::DIGIT_TAG, index, u_charDigitValue(c)); |
| 1175 | utrie2_set32(trie, c, ce32, &errorCode); |
| 1176 | } |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | U_CDECL_BEGIN |
| 1181 | |
| 1182 | static UBool U_CALLCONV |
| 1183 | enumRangeLeadValue(const void *context, UChar32 , UChar32 , uint32_t value) { |
| 1184 | int32_t *pValue = (int32_t *)context; |
| 1185 | if(value == Collation::UNASSIGNED_CE32) { |
| 1186 | value = Collation::LEAD_ALL_UNASSIGNED; |
| 1187 | } else if(value == Collation::FALLBACK_CE32) { |
| 1188 | value = Collation::LEAD_ALL_FALLBACK; |
| 1189 | } else { |
| 1190 | *pValue = Collation::LEAD_MIXED; |
| 1191 | return FALSE; |
| 1192 | } |
| 1193 | if(*pValue < 0) { |
| 1194 | *pValue = (int32_t)value; |
| 1195 | } else if(*pValue != (int32_t)value) { |
| 1196 | *pValue = Collation::LEAD_MIXED; |
| 1197 | return FALSE; |
| 1198 | } |
| 1199 | return TRUE; |
| 1200 | } |
| 1201 | |
| 1202 | U_CDECL_END |
| 1203 | |
| 1204 | void |
| 1205 | CollationDataBuilder::setLeadSurrogates(UErrorCode &errorCode) { |
| 1206 | for(UChar lead = 0xd800; lead < 0xdc00; ++lead) { |
| 1207 | int32_t value = -1; |
| 1208 | utrie2_enumForLeadSurrogate(trie, lead, NULL, enumRangeLeadValue, &value); |
| 1209 | utrie2_set32ForLeadSurrogateCodeUnit( |
| 1210 | trie, lead, |
| 1211 | Collation::makeCE32FromTagAndIndex(Collation::LEAD_SURROGATE_TAG, 0) | (uint32_t)value, |
| 1212 | &errorCode); |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | void |
| 1217 | CollationDataBuilder::build(CollationData &data, UErrorCode &errorCode) { |
| 1218 | buildMappings(data, errorCode); |
| 1 | Calling 'CollationDataBuilder::buildMappings' | |
|
| 1219 | if(base != NULL) { |
| 1220 | data.numericPrimary = base->numericPrimary; |
| 1221 | data.compressibleBytes = base->compressibleBytes; |
| 1222 | data.numScripts = base->numScripts; |
| 1223 | data.scriptsIndex = base->scriptsIndex; |
| 1224 | data.scriptStarts = base->scriptStarts; |
| 1225 | data.scriptStartsLength = base->scriptStartsLength; |
| 1226 | } |
| 1227 | buildFastLatinTable(data, errorCode); |
| 1228 | } |
| 1229 | |
| 1230 | void |
| 1231 | CollationDataBuilder::buildMappings(CollationData &data, UErrorCode &errorCode) { |
| 1232 | if(U_FAILURE(errorCode)) { return; } |
| 1233 | if(trie == NULL || utrie2_isFrozen(trie)) { |
| 2 | | Assuming field 'trie' is not equal to NULL | |
|
| 3 | | Assuming the condition is false | |
|
| |
| 1234 | errorCode = U_INVALID_STATE_ERROR; |
| 1235 | return; |
| 1236 | } |
| 1237 | |
| 1238 | buildContexts(errorCode); |
| 1239 | |
| 1240 | uint32_t jamoCE32s[CollationData::JAMO_CE32S_LENGTH]; |
| 1241 | int32_t jamoIndex = -1; |
| 1242 | if(getJamoCE32s(jamoCE32s, errorCode)) { |
| 5 | | Calling 'CollationDataBuilder::getJamoCE32s' | |
|
| 1243 | jamoIndex = ce32s.size(); |
| 1244 | for(int32_t i = 0; i < CollationData::JAMO_CE32S_LENGTH; ++i) { |
| 1245 | ce32s.addElement((int32_t)jamoCE32s[i], errorCode); |
| 1246 | } |
| 1247 | |
| 1248 | |
| 1249 | |
| 1250 | |
| 1251 | |
| 1252 | |
| 1253 | |
| 1254 | UBool isAnyJamoVTSpecial = FALSE; |
| 1255 | for(int32_t i = Hangul::JAMO_L_COUNT; i < CollationData::JAMO_CE32S_LENGTH; ++i) { |
| 1256 | if(Collation::isSpecialCE32(jamoCE32s[i])) { |
| 1257 | isAnyJamoVTSpecial = TRUE; |
| 1258 | break; |
| 1259 | } |
| 1260 | } |
| 1261 | uint32_t hangulCE32 = Collation::makeCE32FromTagAndIndex(Collation::HANGUL_TAG, 0); |
| 1262 | UChar32 c = Hangul::HANGUL_BASE; |
| 1263 | for(int32_t i = 0; i < Hangul::JAMO_L_COUNT; ++i) { |
| 1264 | uint32_t ce32 = hangulCE32; |
| 1265 | if(!isAnyJamoVTSpecial && !Collation::isSpecialCE32(jamoCE32s[i])) { |
| 1266 | ce32 |= Collation::HANGUL_NO_SPECIAL_JAMO; |
| 1267 | } |
| 1268 | UChar32 limit = c + Hangul::JAMO_VT_COUNT; |
| 1269 | utrie2_setRange32(trie, c, limit - 1, ce32, TRUE, &errorCode); |
| 1270 | c = limit; |
| 1271 | } |
| 1272 | } else { |
| 1273 | |
| 1274 | |
| 1275 | for(UChar32 c = Hangul::HANGUL_BASE; c < Hangul::HANGUL_LIMIT;) { |
| 1276 | uint32_t ce32 = base->getCE32(c); |
| 1277 | U_ASSERT(Collation::hasCE32Tag(ce32, Collation::HANGUL_TAG)); |
| 1278 | UChar32 limit = c + Hangul::JAMO_VT_COUNT; |
| 1279 | utrie2_setRange32(trie, c, limit - 1, ce32, TRUE, &errorCode); |
| 1280 | c = limit; |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | setDigitTags(errorCode); |
| 1285 | setLeadSurrogates(errorCode); |
| 1286 | |
| 1287 | |
| 1288 | ce32s.setElementAt((int32_t)utrie2_get32(trie, 0), 0); |
| 1289 | utrie2_set32(trie, 0, Collation::makeCE32FromTagAndIndex(Collation::U0000_TAG, 0), &errorCode); |
| 1290 | |
| 1291 | utrie2_freeze(trie, UTRIE2_32_VALUE_BITS, &errorCode); |
| 1292 | if(U_FAILURE(errorCode)) { return; } |
| 1293 | |
| 1294 | |
| 1295 | |
| 1296 | UChar32 c = 0x10000; |
| 1297 | for(UChar lead = 0xd800; lead < 0xdc00; ++lead, c += 0x400) { |
| 1298 | if(unsafeBackwardSet.containsSome(c, c + 0x3ff)) { |
| 1299 | unsafeBackwardSet.add(lead); |
| 1300 | } |
| 1301 | } |
| 1302 | unsafeBackwardSet.freeze(); |
| 1303 | |
| 1304 | data.trie = trie; |
| 1305 | data.ce32s = reinterpret_cast<const uint32_t *>(ce32s.getBuffer()); |
| 1306 | data.ces = ce64s.getBuffer(); |
| 1307 | data.contexts = contexts.getBuffer(); |
| 1308 | |
| 1309 | data.ce32sLength = ce32s.size(); |
| 1310 | data.cesLength = ce64s.size(); |
| 1311 | data.contextsLength = contexts.length(); |
| 1312 | |
| 1313 | data.base = base; |
| 1314 | if(jamoIndex >= 0) { |
| 1315 | data.jamoCE32s = data.ce32s + jamoIndex; |
| 1316 | } else { |
| 1317 | data.jamoCE32s = base->jamoCE32s; |
| 1318 | } |
| 1319 | data.unsafeBackwardSet = &unsafeBackwardSet; |
| 1320 | } |
| 1321 | |
| 1322 | void |
| 1323 | CollationDataBuilder::clearContexts() { |
| 1324 | contexts.remove(); |
| 1325 | UnicodeSetIterator iter(contextChars); |
| 1326 | while(iter.next()) { |
| 1327 | U_ASSERT(!iter.isString()); |
| 1328 | uint32_t ce32 = utrie2_get32(trie, iter.getCodepoint()); |
| 1329 | U_ASSERT(isBuilderContextCE32(ce32)); |
| 1330 | getConditionalCE32ForCE32(ce32)->builtCE32 = Collation::NO_CE32; |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | void |
| 1335 | CollationDataBuilder::buildContexts(UErrorCode &errorCode) { |
| 1336 | if(U_FAILURE(errorCode)) { return; } |
| 1337 | |
| 1338 | |
| 1339 | contexts.remove(); |
| 1340 | UnicodeSetIterator iter(contextChars); |
| 1341 | while(U_SUCCESS(errorCode) && iter.next()) { |
| 1342 | U_ASSERT(!iter.isString()); |
| 1343 | UChar32 c = iter.getCodepoint(); |
| 1344 | uint32_t ce32 = utrie2_get32(trie, c); |
| 1345 | if(!isBuilderContextCE32(ce32)) { |
| 1346 | |
| 1347 | errorCode = U_INTERNAL_PROGRAM_ERROR; |
| 1348 | return; |
| 1349 | } |
| 1350 | ConditionalCE32 *cond = getConditionalCE32ForCE32(ce32); |
| 1351 | ce32 = buildContext(cond, errorCode); |
| 1352 | utrie2_set32(trie, c, ce32, &errorCode); |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | uint32_t |
| 1357 | CollationDataBuilder::buildContext(ConditionalCE32 *head, UErrorCode &errorCode) { |
| 1358 | if(U_FAILURE(errorCode)) { return 0; } |
| 1359 | |
| 1360 | U_ASSERT(!head->hasContext()); |
| 1361 | |
| 1362 | U_ASSERT(head->next >= 0); |
| 1363 | UCharsTrieBuilder prefixBuilder(errorCode); |
| 1364 | UCharsTrieBuilder contractionBuilder(errorCode); |
| 1365 | for(ConditionalCE32 *cond = head;; cond = getConditionalCE32(cond->next)) { |
| 1366 | |
| 1367 | U_ASSERT(cond == head || cond->hasContext()); |
| 1368 | int32_t prefixLength = cond->prefixLength(); |
| 1369 | UnicodeString prefix(cond->context, 0, prefixLength + 1); |
| 1370 | |
| 1371 | ConditionalCE32 *firstCond = cond; |
| 1372 | ConditionalCE32 *lastCond = cond; |
| 1373 | while(cond->next >= 0 && |
| 1374 | (cond = getConditionalCE32(cond->next))->context.startsWith(prefix)) { |
| 1375 | lastCond = cond; |
| 1376 | } |
| 1377 | uint32_t ce32; |
| 1378 | int32_t suffixStart = prefixLength + 1; |
| 1379 | if(lastCond->context.length() == suffixStart) { |
| 1380 | |
| 1381 | U_ASSERT(firstCond == lastCond); |
| 1382 | ce32 = lastCond->ce32; |
| 1383 | cond = lastCond; |
| 1384 | } else { |
| 1385 | |
| 1386 | contractionBuilder.clear(); |
| 1387 | |
| 1388 | uint32_t emptySuffixCE32 = 0; |
| 1389 | uint32_t flags = 0; |
| 1390 | if(firstCond->context.length() == suffixStart) { |
| 1391 | |
| 1392 | |
| 1393 | emptySuffixCE32 = firstCond->ce32; |
| 1394 | cond = getConditionalCE32(firstCond->next); |
| 1395 | } else { |
| 1396 | |
| 1397 | |
| 1398 | flags |= Collation::CONTRACT_SINGLE_CP_NO_MATCH; |
| 1399 | |
| 1400 | |
| 1401 | |
| 1402 | |
| 1403 | |
| 1404 | |
| 1405 | for(cond = head;; cond = getConditionalCE32(cond->next)) { |
| 1406 | int32_t length = cond->prefixLength(); |
| 1407 | if(length == prefixLength) { break; } |
| 1408 | if(cond->defaultCE32 != Collation::NO_CE32 && |
| 1409 | (length==0 || prefix.endsWith(cond->context, 1, length))) { |
| 1410 | emptySuffixCE32 = cond->defaultCE32; |
| 1411 | } |
| 1412 | } |
| 1413 | cond = firstCond; |
| 1414 | } |
| 1415 | |
| 1416 | |
| 1417 | |
| 1418 | flags |= Collation::CONTRACT_NEXT_CCC; |
| 1419 | |
| 1420 | for(;;) { |
| 1421 | UnicodeString suffix(cond->context, suffixStart); |
| 1422 | uint16_t fcd16 = nfcImpl.getFCD16(suffix.char32At(0)); |
| 1423 | if(fcd16 <= 0xff) { |
| 1424 | flags &= ~Collation::CONTRACT_NEXT_CCC; |
| 1425 | } |
| 1426 | fcd16 = nfcImpl.getFCD16(suffix.char32At(suffix.length() - 1)); |
| 1427 | if(fcd16 > 0xff) { |
| 1428 | |
| 1429 | flags |= Collation::CONTRACT_TRAILING_CCC; |
| 1430 | } |
| 1431 | contractionBuilder.add(suffix, (int32_t)cond->ce32, errorCode); |
| 1432 | if(cond == lastCond) { break; } |
| 1433 | cond = getConditionalCE32(cond->next); |
| 1434 | } |
| 1435 | int32_t index = addContextTrie(emptySuffixCE32, contractionBuilder, errorCode); |
| 1436 | if(U_FAILURE(errorCode)) { return 0; } |
| 1437 | if(index > Collation::MAX_INDEX) { |
| 1438 | errorCode = U_BUFFER_OVERFLOW_ERROR; |
| 1439 | return 0; |
| 1440 | } |
| 1441 | ce32 = Collation::makeCE32FromTagAndIndex(Collation::CONTRACTION_TAG, index) | flags; |
| 1442 | } |
| 1443 | U_ASSERT(cond == lastCond); |
| 1444 | firstCond->defaultCE32 = ce32; |
| 1445 | if(prefixLength == 0) { |
| 1446 | if(cond->next < 0) { |
| 1447 | |
| 1448 | return ce32; |
| 1449 | } |
| 1450 | } else { |
| 1451 | prefix.remove(0, 1); |
| 1452 | prefix.reverse(); |
| 1453 | prefixBuilder.add(prefix, (int32_t)ce32, errorCode); |
| 1454 | if(cond->next < 0) { break; } |
| 1455 | } |
| 1456 | } |
| 1457 | U_ASSERT(head->defaultCE32 != Collation::NO_CE32); |
| 1458 | int32_t index = addContextTrie(head->defaultCE32, prefixBuilder, errorCode); |
| 1459 | if(U_FAILURE(errorCode)) { return 0; } |
| 1460 | if(index > Collation::MAX_INDEX) { |
| 1461 | errorCode = U_BUFFER_OVERFLOW_ERROR; |
| 1462 | return 0; |
| 1463 | } |
| 1464 | return Collation::makeCE32FromTagAndIndex(Collation::PREFIX_TAG, index); |
| 1465 | } |
| 1466 | |
| 1467 | int32_t |
| 1468 | CollationDataBuilder::addContextTrie(uint32_t defaultCE32, UCharsTrieBuilder &trieBuilder, |
| 1469 | UErrorCode &errorCode) { |
| 1470 | UnicodeString context; |
| 1471 | context.append((UChar)(defaultCE32 >> 16)).append((UChar)defaultCE32); |
| 1472 | UnicodeString trieString; |
| 1473 | context.append(trieBuilder.buildUnicodeString(USTRINGTRIE_BUILD_SMALL, trieString, errorCode)); |
| 1474 | if(U_FAILURE(errorCode)) { return -1; } |
| 1475 | int32_t index = contexts.indexOf(context); |
| 1476 | if(index < 0) { |
| 1477 | index = contexts.length(); |
| 1478 | contexts.append(context); |
| 1479 | } |
| 1480 | return index; |
| 1481 | } |
| 1482 | |
| 1483 | void |
| 1484 | CollationDataBuilder::buildFastLatinTable(CollationData &data, UErrorCode &errorCode) { |
| 1485 | if(U_FAILURE(errorCode) || !fastLatinEnabled) { return; } |
| 1486 | |
| 1487 | delete fastLatinBuilder; |
| 1488 | fastLatinBuilder = new CollationFastLatinBuilder(errorCode); |
| 1489 | if(fastLatinBuilder == NULL) { |
| 1490 | errorCode = U_MEMORY_ALLOCATION_ERROR; |
| 1491 | return; |
| 1492 | } |
| 1493 | if(fastLatinBuilder->forData(data, errorCode)) { |
| 1494 | const uint16_t *table = fastLatinBuilder->getTable(); |
| 1495 | int32_t length = fastLatinBuilder->lengthOfTable(); |
| 1496 | if(base != NULL && length == base->fastLatinTableLength && |
| 1497 | uprv_memcmp(table, base->fastLatinTable, length * 2) == 0) { |
| 1498 | |
| 1499 | delete fastLatinBuilder; |
| 1500 | fastLatinBuilder = NULL; |
| 1501 | table = base->fastLatinTable; |
| 1502 | } |
| 1503 | data.fastLatinTable = table; |
| 1504 | data.fastLatinTableLength = length; |
| 1505 | } else { |
| 1506 | delete fastLatinBuilder; |
| 1507 | fastLatinBuilder = NULL; |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | int32_t |
| 1512 | CollationDataBuilder::getCEs(const UnicodeString &s, int64_t ces[], int32_t cesLength) { |
| 1513 | return getCEs(s, 0, ces, cesLength); |
| 1514 | } |
| 1515 | |
| 1516 | int32_t |
| 1517 | CollationDataBuilder::getCEs(const UnicodeString &prefix, const UnicodeString &s, |
| 1518 | int64_t ces[], int32_t cesLength) { |
| 1519 | int32_t prefixLength = prefix.length(); |
| 1520 | if(prefixLength == 0) { |
| 1521 | return getCEs(s, 0, ces, cesLength); |
| 1522 | } else { |
| 1523 | return getCEs(prefix + s, prefixLength, ces, cesLength); |
| 1524 | } |
| 1525 | } |
| 1526 | |
| 1527 | int32_t |
| 1528 | CollationDataBuilder::getCEs(const UnicodeString &s, int32_t start, |
| 1529 | int64_t ces[], int32_t cesLength) { |
| 1530 | if(collIter == NULL) { |
| 1531 | collIter = new DataBuilderCollationIterator(*this); |
| 1532 | if(collIter == NULL) { return 0; } |
| 1533 | } |
| 1534 | return collIter->fetchCEs(s, start, ces, cesLength); |
| 1535 | } |
| 1536 | |
| 1537 | U_NAMESPACE_END |
| 1538 | |
| 1539 | #endif // !UCONFIG_NO_COLLATION |