1 /* 2 ******************************************************************************* 3 * 4 * Copyright (C) 2009-2011, International Business Machines 5 * Corporation and others. All Rights Reserved. 6 * 7 ******************************************************************************* 8 * file name: normalizer2impl.h 9 * encoding: US-ASCII 10 * tab size: 8 (not used) 11 * indentation:4 12 * 13 * created on: 2009nov22 14 * created by: Markus W. Scherer 15 */ 16 17 #ifndef __NORMALIZER2IMPL_H__ 18 #define __NORMALIZER2IMPL_H__ 19 20 #include "unicode/utypes.h" 21 22 #if !UCONFIG_NO_NORMALIZATION 23 24 #include "unicode/normalizer2.h" 25 #include "unicode/udata.h" 26 #include "unicode/unistr.h" 27 #include "unicode/unorm.h" 28 #include "mutex.h" 29 #include "uset_imp.h" 30 #include "utrie2.h" 31 32 U_NAMESPACE_BEGIN 33 34 struct CanonIterData; 35 36 class Hangul { 37 public: 38 /* Korean Hangul and Jamo constants */ 39 enum { 40 JAMO_L_BASE=0x1100, /* "lead" jamo */ 41 JAMO_V_BASE=0x1161, /* "vowel" jamo */ 42 JAMO_T_BASE=0x11a7, /* "trail" jamo */ 43 44 HANGUL_BASE=0xac00, 45 46 JAMO_L_COUNT=19, 47 JAMO_V_COUNT=21, 48 JAMO_T_COUNT=28, 49 50 JAMO_VT_COUNT=JAMO_V_COUNT*JAMO_T_COUNT, 51 52 HANGUL_COUNT=JAMO_L_COUNT*JAMO_V_COUNT*JAMO_T_COUNT, 53 HANGUL_LIMIT=HANGUL_BASE+HANGUL_COUNT 54 }; 55 56 static inline UBool isHangul(UChar32 c) { 57 return HANGUL_BASE<=c && c<HANGUL_LIMIT; 58 } 59 static inline UBool 60 isHangulWithoutJamoT(UChar c) { 61 c-=HANGUL_BASE; 62 return c<HANGUL_COUNT && c%JAMO_T_COUNT==0; 63 } 64 static inline UBool isJamoL(UChar32 c) { 65 return (uint32_t)(c-JAMO_L_BASE)<JAMO_L_COUNT; 66 } 67 static inline UBool isJamoV(UChar32 c) { 68 return (uint32_t)(c-JAMO_V_BASE)<JAMO_V_COUNT; 69 } 70 71 /** 72 * Decomposes c, which must be a Hangul syllable, into buffer 73 * and returns the length of the decomposition (2 or 3). 74 */ 75 static inline int32_t decompose(UChar32 c, UChar buffer[3]) { 76 c-=HANGUL_BASE; 77 UChar32 c2=c%JAMO_T_COUNT; 78 c/=JAMO_T_COUNT; 79 buffer[0]=(UChar)(JAMO_L_BASE+c/JAMO_V_COUNT); 80 buffer[1]=(UChar)(JAMO_V_BASE+c%JAMO_V_COUNT); 81 if(c2==0) { 82 return 2; 83 } else { 84 buffer[2]=(UChar)(JAMO_T_BASE+c2); 85 return 3; 86 } 87 } 88 private: 89 Hangul(); // no instantiation 90 }; 91 92 class Normalizer2Impl; 93 94 class ReorderingBuffer : public UMemory { 95 public: 96 ReorderingBuffer(const Normalizer2Impl &ni, UnicodeString &dest) : 97 impl(ni), str(dest), 98 start(NULL), reorderStart(NULL), limit(NULL), 99 remainingCapacity(0), lastCC(0) {} 100 ~ReorderingBuffer() { 101 if(start!=NULL) { 102 str.releaseBuffer((int32_t)(limit-start)); 103 } 104 } 105 UBool init(int32_t destCapacity, UErrorCode &errorCode); 106 107 UBool isEmpty() const { return start==limit; } 108 int32_t length() const { return (int32_t)(limit-start); } 109 UChar *getStart() { return start; } 110 UChar *getLimit() { return limit; } 111 uint8_t getLastCC() const { return lastCC; } 112 113 UBool equals(const UChar *start, const UChar *limit) const; 114 115 // For Hangul composition, replacing the Leading consonant Jamo with the syllable. 116 void setLastChar(UChar c) { 117 *(limit-1)=c; 118 } 119 120 UBool append(UChar32 c, uint8_t cc, UErrorCode &errorCode) { 121 return (c<=0xffff) ? 122 appendBMP((UChar)c, cc, errorCode) : 123 appendSupplementary(c, cc, errorCode); 124 } 125 // s must be in NFD, otherwise change the implementation. 126 UBool append(const UChar *s, int32_t length, 127 uint8_t leadCC, uint8_t trailCC, 128 UErrorCode &errorCode); 129 UBool appendBMP(UChar c, uint8_t cc, UErrorCode &errorCode) { 130 if(remainingCapacity==0 && !resize(1, errorCode)) { 131 return FALSE; 132 } 133 if(lastCC<=cc || cc==0) { 134 *limit++=c; 135 lastCC=cc; 136 if(cc<=1) { 137 reorderStart=limit; 138 } 139 } else { 140 insert(c, cc); 141 } 142 --remainingCapacity; 143 return TRUE; 144 } 145 UBool appendZeroCC(UChar32 c, UErrorCode &errorCode); 146 UBool appendZeroCC(const UChar *s, const UChar *sLimit, UErrorCode &errorCode); 147 void remove(); 148 void removeSuffix(int32_t suffixLength); 149 void setReorderingLimit(UChar *newLimit) { 150 remainingCapacity+=(int32_t)(limit-newLimit); 151 reorderStart=limit=newLimit; 152 lastCC=0; 153 } 154 void copyReorderableSuffixTo(UnicodeString &s) const { 155 s.setTo(reorderStart, (int32_t)(limit-reorderStart)); 156 } 157 private: 158 /* 159 * TODO: Revisit whether it makes sense to track reorderStart. 160 * It is set to after the last known character with cc<=1, 161 * which stops previousCC() before it reads that character and looks up its cc. 162 * previousCC() is normally only called from insert(). 163 * In other words, reorderStart speeds up the insertion of a combining mark 164 * into a multi-combining mark sequence where it does not belong at the end. 165 * This might not be worth the trouble. 166 * On the other hand, it's not a huge amount of trouble. 167 * 168 * We probably need it for UNORM_SIMPLE_APPEND. 169 */ 170 171 UBool appendSupplementary(UChar32 c, uint8_t cc, UErrorCode &errorCode); 172 void insert(UChar32 c, uint8_t cc); 173 static void writeCodePoint(UChar *p, UChar32 c) { 174 if(c<=0xffff) { 175 *p=(UChar)c; 176 } else { 177 p[0]=U16_LEAD(c); 178 p[1]=U16_TRAIL(c); 179 } 180 } 181 UBool resize(int32_t appendLength, UErrorCode &errorCode); 182 183 const Normalizer2Impl &impl; 184 UnicodeString &str; 185 UChar *start, *reorderStart, *limit; 186 int32_t remainingCapacity; 187 uint8_t lastCC; 188 189 // private backward iterator 190 void setIterator() { codePointStart=limit; } 191 void skipPrevious(); // Requires start<codePointStart. 192 uint8_t previousCC(); // Returns 0 if there is no previous character. 193 194 UChar *codePointStart, *codePointLimit; 195 }; 196 197 class U_COMMON_API Normalizer2Impl : public UMemory { 198 public: 199 Normalizer2Impl() : memory(NULL), normTrie(NULL) { 200 fcdTrieSingleton.fInstance=NULL; 201 canonIterDataSingleton.fInstance=NULL; 202 } 203 ~Normalizer2Impl(); 204 205 void load(const char *packageName, const char *name, UErrorCode &errorCode); 206 207 void addPropertyStarts(const USetAdder *sa, UErrorCode &errorCode) const; 208 void addCanonIterPropertyStarts(const USetAdder *sa, UErrorCode &errorCode) const; 209 210 // low-level properties ------------------------------------------------ *** 211 212 const UTrie2 *getNormTrie() const { return normTrie; } 213 const UTrie2 *getFCDTrie(UErrorCode &errorCode) const ; 214 215 UBool ensureCanonIterData(UErrorCode &errorCode) const; 216 217 uint16_t getNorm16(UChar32 c) const { return UTRIE2_GET16(normTrie, c); } 218 219 UNormalizationCheckResult getCompQuickCheck(uint16_t norm16) const { 220 if(norm16<minNoNo || MIN_YES_YES_WITH_CC<=norm16) { 221 return UNORM_YES; 222 } else if(minMaybeYes<=norm16) { 223 return UNORM_MAYBE; 224 } else { 225 return UNORM_NO; 226 } 227 } 228 UBool isCompNo(uint16_t norm16) const { return minNoNo<=norm16 && norm16<minMaybeYes; } 229 UBool isDecompYes(uint16_t norm16) const { return norm16<minYesNo || minMaybeYes<=norm16; } 230 231 uint8_t getCC(uint16_t norm16) const { 232 if(norm16>=MIN_NORMAL_MAYBE_YES) { 233 return (uint8_t)norm16; 234 } 235 if(norm16<minNoNo || limitNoNo<=norm16) { 236 return 0; 237 } 238 return getCCFromNoNo(norm16); 239 } 240 static uint8_t getCCFromYesOrMaybe(uint16_t norm16) { 241 return norm16>=MIN_NORMAL_MAYBE_YES ? (uint8_t)norm16 : 0; 242 } 243 244 uint16_t getFCD16(UChar32 c) const { return UTRIE2_GET16(fcdTrie(), c); } 245 uint16_t getFCD16FromSingleLead(UChar c) const { 246 return UTRIE2_GET16_FROM_U16_SINGLE_LEAD(fcdTrie(), c); 247 } 248 uint16_t getFCD16FromSupplementary(UChar32 c) const { 249 return UTRIE2_GET16_FROM_SUPP(fcdTrie(), c); 250 } 251 uint16_t getFCD16FromSurrogatePair(UChar c, UChar c2) const { 252 return getFCD16FromSupplementary(U16_GET_SUPPLEMENTARY(c, c2)); 253 } 254 255 void setFCD16FromNorm16(UChar32 start, UChar32 end, uint16_t norm16, 256 UTrie2 *newFCDTrie, UErrorCode &errorCode) const; 257 258 void makeCanonIterDataFromNorm16(UChar32 start, UChar32 end, uint16_t norm16, 259 CanonIterData &newData, UErrorCode &errorCode) const; 260 261 /** 262 * Get the decomposition for one code point. 263 * @param c code point 264 * @param buffer out-only buffer for algorithmic decompositions 265 * @param length out-only, takes the length of the decomposition, if any 266 * @return pointer to the decomposition, or NULL if none 267 */ 268 const UChar *getDecomposition(UChar32 c, UChar buffer[4], int32_t &length) const; 269 270 UBool isCanonSegmentStarter(UChar32 c) const; 271 UBool getCanonStartSet(UChar32 c, UnicodeSet &set) const; 272 273 enum { 274 MIN_CCC_LCCC_CP=0x300 275 }; 276 277 enum { 278 MIN_YES_YES_WITH_CC=0xff01, 279 JAMO_VT=0xff00, 280 MIN_NORMAL_MAYBE_YES=0xfe00, 281 JAMO_L=1, 282 MAX_DELTA=0x40 283 }; 284 285 enum { 286 // Byte offsets from the start of the data, after the generic header. 287 IX_NORM_TRIE_OFFSET, 288 IX_EXTRA_DATA_OFFSET, 289 IX_RESERVED2_OFFSET, 290 IX_RESERVED3_OFFSET, 291 IX_RESERVED4_OFFSET, 292 IX_RESERVED5_OFFSET, 293 IX_RESERVED6_OFFSET, 294 IX_TOTAL_SIZE, 295 296 // Code point thresholds for quick check codes. 297 IX_MIN_DECOMP_NO_CP, 298 IX_MIN_COMP_NO_MAYBE_CP, 299 300 // Norm16 value thresholds for quick check combinations and types of extra data. 301 IX_MIN_YES_NO, 302 IX_MIN_NO_NO, 303 IX_LIMIT_NO_NO, 304 IX_MIN_MAYBE_YES, 305 306 IX_RESERVED14, 307 IX_RESERVED15, 308 IX_COUNT 309 }; 310 311 enum { 312 MAPPING_HAS_CCC_LCCC_WORD=0x80, 313 MAPPING_PLUS_COMPOSITION_LIST=0x40, 314 MAPPING_NO_COMP_BOUNDARY_AFTER=0x20, 315 MAPPING_LENGTH_MASK=0x1f 316 }; 317 318 enum { 319 COMP_1_LAST_TUPLE=0x8000, 320 COMP_1_TRIPLE=1, 321 COMP_1_TRAIL_LIMIT=0x3400, 322 COMP_1_TRAIL_MASK=0x7ffe, 323 COMP_1_TRAIL_SHIFT=9, // 10-1 for the "triple" bit 324 COMP_2_TRAIL_SHIFT=6, 325 COMP_2_TRAIL_MASK=0xffc0 326 }; 327 328 // higher-level functionality ------------------------------------------ *** 329 330 const UChar *decompose(const UChar *src, const UChar *limit, 331 ReorderingBuffer *buffer, UErrorCode &errorCode) const; 332 void decomposeAndAppend(const UChar *src, const UChar *limit, 333 UBool doDecompose, 334 UnicodeString &safeMiddle, 335 ReorderingBuffer &buffer, 336 UErrorCode &errorCode) const; 337 UBool compose(const UChar *src, const UChar *limit, 338 UBool onlyContiguous, 339 UBool doCompose, 340 ReorderingBuffer &buffer, 341 UErrorCode &errorCode) const; 342 const UChar *composeQuickCheck(const UChar *src, const UChar *limit, 343 UBool onlyContiguous, 344 UNormalizationCheckResult *pQCResult) const; 345 void composeAndAppend(const UChar *src, const UChar *limit, 346 UBool doCompose, 347 UBool onlyContiguous, 348 UnicodeString &safeMiddle, 349 ReorderingBuffer &buffer, 350 UErrorCode &errorCode) const; 351 const UChar *makeFCD(const UChar *src, const UChar *limit, 352 ReorderingBuffer *buffer, UErrorCode &errorCode) const; 353 void makeFCDAndAppend(const UChar *src, const UChar *limit, 354 UBool doMakeFCD, 355 UnicodeString &safeMiddle, 356 ReorderingBuffer &buffer, 357 UErrorCode &errorCode) const; 358 359 UBool hasDecompBoundary(UChar32 c, UBool before) const; 360 UBool isDecompInert(UChar32 c) const { return isDecompYesAndZeroCC(getNorm16(c)); } 361 362 UBool hasCompBoundaryBefore(UChar32 c) const { 363 return c<minCompNoMaybeCP || hasCompBoundaryBefore(c, getNorm16(c)); 364 } 365 UBool hasCompBoundaryAfter(UChar32 c, UBool onlyContiguous, UBool testInert) const; 366 367 UBool hasFCDBoundaryBefore(UChar32 c) const { return c<MIN_CCC_LCCC_CP || getFCD16(c)<=0xff; } 368 UBool hasFCDBoundaryAfter(UChar32 c) const { 369 uint16_t fcd16=getFCD16(c); 370 return fcd16<=1 || (fcd16&0xff)==0; 371 } 372 UBool isFCDInert(UChar32 c) const { return getFCD16(c)<=1; } 373 private: 374 static UBool U_CALLCONV 375 isAcceptable(void *context, const char *type, const char *name, const UDataInfo *pInfo); 376 377 UBool isMaybe(uint16_t norm16) const { return minMaybeYes<=norm16 && norm16<=JAMO_VT; } 378 UBool isMaybeOrNonZeroCC(uint16_t norm16) const { return norm16>=minMaybeYes; } 379 static UBool isInert(uint16_t norm16) { return norm16==0; } 380 // static UBool isJamoL(uint16_t norm16) const { return norm16==1; } 381 static UBool isJamoVT(uint16_t norm16) { return norm16==JAMO_VT; } 382 UBool isHangul(uint16_t norm16) const { return norm16==minYesNo; } 383 UBool isCompYesAndZeroCC(uint16_t norm16) const { return norm16<minNoNo; } 384 // UBool isCompYes(uint16_t norm16) const { 385 // return norm16>=MIN_YES_YES_WITH_CC || norm16<minNoNo; 386 // } 387 // UBool isCompYesOrMaybe(uint16_t norm16) const { 388 // return norm16<minNoNo || minMaybeYes<=norm16; 389 // } 390 // UBool hasZeroCCFromDecompYes(uint16_t norm16) const { 391 // return norm16<=MIN_NORMAL_MAYBE_YES || norm16==JAMO_VT; 392 // } 393 UBool isDecompYesAndZeroCC(uint16_t norm16) const { 394 return norm16<minYesNo || 395 norm16==JAMO_VT || 396 (minMaybeYes<=norm16 && norm16<=MIN_NORMAL_MAYBE_YES); 397 } 398 /** 399 * A little faster and simpler than isDecompYesAndZeroCC() but does not include 400 * the MaybeYes which combine-forward and have ccc=0. 401 * (Standard Unicode 5.2 normalization does not have such characters.) 402 */ 403 UBool isMostDecompYesAndZeroCC(uint16_t norm16) const { 404 return norm16<minYesNo || norm16==MIN_NORMAL_MAYBE_YES || norm16==JAMO_VT; 405 } 406 UBool isDecompNoAlgorithmic(uint16_t norm16) const { return norm16>=limitNoNo; } 407 408 // For use with isCompYes(). 409 // Perhaps the compiler can combine the two tests for MIN_YES_YES_WITH_CC. 410 // static uint8_t getCCFromYes(uint16_t norm16) { 411 // return norm16>=MIN_YES_YES_WITH_CC ? (uint8_t)norm16 : 0; 412 // } 413 uint8_t getCCFromNoNo(uint16_t norm16) const { 414 const uint16_t *mapping=getMapping(norm16); 415 if(*mapping&MAPPING_HAS_CCC_LCCC_WORD) { 416 return (uint8_t)mapping[1]; 417 } else { 418 return 0; 419 } 420 } 421 // requires that the [cpStart..cpLimit[ character passes isCompYesAndZeroCC() 422 uint8_t getTrailCCFromCompYesAndZeroCC(const UChar *cpStart, const UChar *cpLimit) const; 423 424 // Requires algorithmic-NoNo. 425 UChar32 mapAlgorithmic(UChar32 c, uint16_t norm16) const { 426 return c+norm16-(minMaybeYes-MAX_DELTA-1); 427 } 428 429 // Requires minYesNo<norm16<limitNoNo. 430 const uint16_t *getMapping(uint16_t norm16) const { return extraData+norm16; } 431 const uint16_t *getCompositionsListForDecompYes(uint16_t norm16) const { 432 if(norm16==0 || MIN_NORMAL_MAYBE_YES<=norm16) { 433 return NULL; 434 } else if(norm16<minMaybeYes) { 435 return extraData+norm16; // for yesYes; if Jamo L: harmless empty list 436 } else { 437 return maybeYesCompositions+norm16-minMaybeYes; 438 } 439 } 440 const uint16_t *getCompositionsListForComposite(uint16_t norm16) const { 441 const uint16_t *list=extraData+norm16; // composite has both mapping & compositions list 442 return list+ // mapping pointer 443 1+ // +1 to skip the first unit with the mapping lenth 444 (*list&MAPPING_LENGTH_MASK)+ // + mapping length 445 ((*list>>7)&1); // +1 if MAPPING_HAS_CCC_LCCC_WORD 446 } 447 /** 448 * @param c code point must have compositions 449 * @return compositions list pointer 450 */ 451 const uint16_t *getCompositionsList(uint16_t norm16) const { 452 return isDecompYes(norm16) ? 453 getCompositionsListForDecompYes(norm16) : 454 getCompositionsListForComposite(norm16); 455 } 456 457 const UChar *copyLowPrefixFromNulTerminated(const UChar *src, 458 UChar32 minNeedDataCP, 459 ReorderingBuffer *buffer, 460 UErrorCode &errorCode) const; 461 UBool decomposeShort(const UChar *src, const UChar *limit, 462 ReorderingBuffer &buffer, UErrorCode &errorCode) const; 463 UBool decompose(UChar32 c, uint16_t norm16, 464 ReorderingBuffer &buffer, UErrorCode &errorCode) const; 465 466 static int32_t combine(const uint16_t *list, UChar32 trail); 467 void addComposites(const uint16_t *list, UnicodeSet &set) const; 468 void recompose(ReorderingBuffer &buffer, int32_t recomposeStartIndex, 469 UBool onlyContiguous) const; 470 471 UBool hasCompBoundaryBefore(UChar32 c, uint16_t norm16) const; 472 const UChar *findPreviousCompBoundary(const UChar *start, const UChar *p) const; 473 const UChar *findNextCompBoundary(const UChar *p, const UChar *limit) const; 474 475 const UTrie2 *fcdTrie() const { return (const UTrie2 *)fcdTrieSingleton.fInstance; } 476 477 const UChar *findPreviousFCDBoundary(const UChar *start, const UChar *p) const; 478 const UChar *findNextFCDBoundary(const UChar *p, const UChar *limit) const; 479 480 int32_t getCanonValue(UChar32 c) const; 481 const UnicodeSet &getCanonStartSet(int32_t n) const; 482 483 UDataMemory *memory; 484 UVersionInfo dataVersion; 485 486 // Code point thresholds for quick check codes. 487 UChar32 minDecompNoCP; 488 UChar32 minCompNoMaybeCP; 489 490 // Norm16 value thresholds for quick check combinations and types of extra data. 491 uint16_t minYesNo; 492 uint16_t minNoNo; 493 uint16_t limitNoNo; 494 uint16_t minMaybeYes; 495 496 UTrie2 *normTrie; 497 const uint16_t *maybeYesCompositions; 498 const uint16_t *extraData; // mappings and/or compositions for yesYes, yesNo & noNo characters 499 500 SimpleSingleton fcdTrieSingleton; 501 SimpleSingleton canonIterDataSingleton; 502 }; 503 504 // bits in canonIterData 505 #define CANON_NOT_SEGMENT_STARTER 0x80000000 506 #define CANON_HAS_COMPOSITIONS 0x40000000 507 #define CANON_HAS_SET 0x200000 508 #define CANON_VALUE_MASK 0x1fffff 509 510 /** 511 * ICU-internal shortcut for quick access to standard Unicode normalization. 512 */ 513 class U_COMMON_API Normalizer2Factory { 514 public: 515 static const Normalizer2 *getNFCInstance(UErrorCode &errorCode); 516 static const Normalizer2 *getNFDInstance(UErrorCode &errorCode); 517 static const Normalizer2 *getFCDInstance(UErrorCode &errorCode); 518 static const Normalizer2 *getFCCInstance(UErrorCode &errorCode); 519 static const Normalizer2 *getNFKCInstance(UErrorCode &errorCode); 520 static const Normalizer2 *getNFKDInstance(UErrorCode &errorCode); 521 static const Normalizer2 *getNFKC_CFInstance(UErrorCode &errorCode); 522 static const Normalizer2 *getNoopInstance(UErrorCode &errorCode); 523 524 static const Normalizer2 *getInstance(UNormalizationMode mode, UErrorCode &errorCode); 525 526 static const Normalizer2Impl *getNFCImpl(UErrorCode &errorCode); 527 static const Normalizer2Impl *getNFKCImpl(UErrorCode &errorCode); 528 static const Normalizer2Impl *getNFKC_CFImpl(UErrorCode &errorCode); 529 530 // Get the Impl instance of the Normalizer2. 531 // Must be used only when it is known that norm2 is a Normalizer2WithImpl instance. 532 static const Normalizer2Impl *getImpl(const Normalizer2 *norm2); 533 534 static const UTrie2 *getFCDTrie(UErrorCode &errorCode); 535 private: 536 Normalizer2Factory(); // No instantiation. 537 }; 538 539 U_NAMESPACE_END 540 541 U_CAPI int32_t U_EXPORT2 542 unorm2_swap(const UDataSwapper *ds, 543 const void *inData, int32_t length, void *outData, 544 UErrorCode *pErrorCode); 545 546 /** 547 * Get the NF*_QC property for a code point, for u_getIntPropertyValue(). 548 * @internal 549 */ 550 U_CFUNC UNormalizationCheckResult U_EXPORT2 551 unorm_getQuickCheck(UChar32 c, UNormalizationMode mode); 552 553 /** 554 * Internal API, used by collation code. 555 * Get access to the internal FCD trie table to be able to perform 556 * incremental, per-code unit, FCD checks in collation. 557 * One pointer is sufficient because the trie index values are offset 558 * by the index size, so that the same pointer is used to access the trie data. 559 * Code points at fcdHighStart and above have a zero FCD value. 560 * @internal 561 */ 562 U_CAPI const uint16_t * U_EXPORT2 563 unorm_getFCDTrieIndex(UChar32 &fcdHighStart, UErrorCode *pErrorCode); 564 565 /** 566 * Internal API, used by collation code. 567 * Get the FCD value for a code unit, with 568 * bits 15..8 lead combining class 569 * bits 7..0 trail combining class 570 * 571 * If c is a lead surrogate and the value is not 0, 572 * then some of c's associated supplementary code points have a non-zero FCD value. 573 * 574 * @internal 575 */ 576 static inline uint16_t 577 unorm_getFCD16(const uint16_t *fcdTrieIndex, UChar c) { 578 return fcdTrieIndex[_UTRIE2_INDEX_FROM_U16_SINGLE_LEAD(fcdTrieIndex, c)]; 579 } 580 581 /** 582 * Internal API, used by collation code. 583 * Get the FCD value of the next code point (post-increment), with 584 * bits 15..8 lead combining class 585 * bits 7..0 trail combining class 586 * 587 * @internal 588 */ 589 static inline uint16_t 590 unorm_nextFCD16(const uint16_t *fcdTrieIndex, UChar32 fcdHighStart, 591 const UChar *&s, const UChar *limit) { 592 UChar32 c=*s++; 593 uint16_t fcd=fcdTrieIndex[_UTRIE2_INDEX_FROM_U16_SINGLE_LEAD(fcdTrieIndex, c)]; 594 if(fcd!=0 && U16_IS_LEAD(c)) { 595 UChar c2; 596 if(s!=limit && U16_IS_TRAIL(c2=*s)) { 597 ++s; 598 c=U16_GET_SUPPLEMENTARY(c, c2); 599 if(c<fcdHighStart) { 600 fcd=fcdTrieIndex[_UTRIE2_INDEX_FROM_SUPP(fcdTrieIndex, c)]; 601 } else { 602 fcd=0; 603 } 604 } else /* unpaired lead surrogate */ { 605 fcd=0; 606 } 607 } 608 return fcd; 609 } 610 611 /** 612 * Internal API, used by collation code. 613 * Get the FCD value of the previous code point (pre-decrement), with 614 * bits 15..8 lead combining class 615 * bits 7..0 trail combining class 616 * 617 * @internal 618 */ 619 static inline uint16_t 620 unorm_prevFCD16(const uint16_t *fcdTrieIndex, UChar32 fcdHighStart, 621 const UChar *start, const UChar *&s) { 622 UChar32 c=*--s; 623 uint16_t fcd; 624 if(!U16_IS_SURROGATE(c)) { 625 fcd=fcdTrieIndex[_UTRIE2_INDEX_FROM_U16_SINGLE_LEAD(fcdTrieIndex, c)]; 626 } else { 627 UChar c2; 628 if(U16_IS_SURROGATE_TRAIL(c) && s!=start && U16_IS_LEAD(c2=*(s-1))) { 629 --s; 630 c=U16_GET_SUPPLEMENTARY(c2, c); 631 if(c<fcdHighStart) { 632 fcd=fcdTrieIndex[_UTRIE2_INDEX_FROM_SUPP(fcdTrieIndex, c)]; 633 } else { 634 fcd=0; 635 } 636 } else /* unpaired surrogate */ { 637 fcd=0; 638 } 639 } 640 return fcd; 641 } 642 643 /** 644 * Format of Normalizer2 .nrm data files. 645 * Format version 1.0. 646 * 647 * Normalizer2 .nrm data files provide data for the Unicode Normalization algorithms. 648 * ICU ships with data files for standard Unicode Normalization Forms 649 * NFC and NFD (nfc.nrm), NFKC and NFKD (nfkc.nrm) and NFKC_Casefold (nfkc_cf.nrm). 650 * Custom (application-specific) data can be built into additional .nrm files 651 * with the gennorm2 build tool. 652 * 653 * Normalizer2.getInstance() causes a .nrm file to be loaded, unless it has been 654 * cached already. Internally, Normalizer2Impl.load() reads the .nrm file. 655 * 656 * A .nrm file begins with a standard ICU data file header 657 * (DataHeader, see ucmndata.h and unicode/udata.h). 658 * The UDataInfo.dataVersion field usually contains the Unicode version 659 * for which the data was generated. 660 * 661 * After the header, the file contains the following parts. 662 * Constants are defined as enum values of the Normalizer2Impl class. 663 * 664 * Many details of the data structures are described in the design doc 665 * which is at http://site.icu-project.org/design/normalization/custom 666 * 667 * int32_t indexes[indexesLength]; -- indexesLength=indexes[IX_NORM_TRIE_OFFSET]/4; 668 * 669 * The first eight indexes are byte offsets in ascending order. 670 * Each byte offset marks the start of the next part in the data file, 671 * and the end of the previous one. 672 * When two consecutive byte offsets are the same, then the corresponding part is empty. 673 * Byte offsets are offsets from after the header, 674 * that is, from the beginning of the indexes[]. 675 * Each part starts at an offset with proper alignment for its data. 676 * If necessary, the previous part may include padding bytes to achieve this alignment. 677 * 678 * minDecompNoCP=indexes[IX_MIN_DECOMP_NO_CP] is the lowest code point 679 * with a decomposition mapping, that is, with NF*D_QC=No. 680 * minCompNoMaybeCP=indexes[IX_MIN_COMP_NO_MAYBE_CP] is the lowest code point 681 * with NF*C_QC=No (has a one-way mapping) or Maybe (combines backward). 682 * 683 * The next four indexes are thresholds of 16-bit trie values for ranges of 684 * values indicating multiple normalization properties. 685 * minYesNo=indexes[IX_MIN_YES_NO]; 686 * minNoNo=indexes[IX_MIN_NO_NO]; 687 * limitNoNo=indexes[IX_LIMIT_NO_NO]; 688 * minMaybeYes=indexes[IX_MIN_MAYBE_YES]; 689 * See the normTrie description below and the design doc for details. 690 * 691 * UTrie2 normTrie; -- see utrie2_impl.h and utrie2.h 692 * 693 * The trie holds the main normalization data. Each code point is mapped to a 16-bit value. 694 * Rather than using independent bits in the value (which would require more than 16 bits), 695 * information is extracted primarily via range checks. 696 * For example, a 16-bit value norm16 in the range minYesNo<=norm16<minNoNo 697 * means that the character has NF*C_QC=Yes and NF*D_QC=No properties, 698 * which means it has a two-way (round-trip) decomposition mapping. 699 * Values in the range 2<=norm16<limitNoNo are also directly indexes into the extraData 700 * pointing to mappings, composition lists, or both. 701 * Value norm16==0 means that the character is normalization-inert, that is, 702 * it does not have a mapping, does not participate in composition, has a zero 703 * canonical combining class, and forms a boundary where text before it and after it 704 * can be normalized independently. 705 * For details about how multiple properties are encoded in 16-bit values 706 * see the design doc. 707 * Note that the encoding cannot express all combinations of the properties involved; 708 * it only supports those combinations that are allowed by 709 * the Unicode Normalization algorithms. Details are in the design doc as well. 710 * The gennorm2 tool only builds .nrm files for data that conforms to the limitations. 711 * 712 * The trie has a value for each lead surrogate code unit representing the "worst case" 713 * properties of the 1024 supplementary characters whose UTF-16 form starts with 714 * the lead surrogate. If all of the 1024 supplementary characters are normalization-inert, 715 * then their lead surrogate code unit has the trie value 0. 716 * When the lead surrogate unit's value exceeds the quick check minimum during processing, 717 * the properties for the full supplementary code point need to be looked up. 718 * 719 * uint16_t maybeYesCompositions[MIN_NORMAL_MAYBE_YES-minMaybeYes]; 720 * uint16_t extraData[]; 721 * 722 * There is only one byte offset for the end of these two arrays. 723 * The split between them is given by the constant and variable mentioned above. 724 * 725 * The maybeYesCompositions array contains composition lists for characters that 726 * combine both forward (as starters in composition pairs) 727 * and backward (as trailing characters in composition pairs). 728 * Such characters do not occur in Unicode 5.2 but are allowed by 729 * the Unicode Normalization algorithms. 730 * If there are no such characters, then minMaybeYes==MIN_NORMAL_MAYBE_YES 731 * and the maybeYesCompositions array is empty. 732 * If there are such characters, then minMaybeYes is subtracted from their norm16 values 733 * to get the index into this array. 734 * 735 * The extraData array contains composition lists for "YesYes" characters, 736 * followed by mappings and optional composition lists for "YesNo" characters, 737 * followed by only mappings for "NoNo" characters. 738 * (Referring to pairs of NFC/NFD quick check values.) 739 * The norm16 values of those characters are directly indexes into the extraData array. 740 * 741 * The data structures for composition lists and mappings are described in the design doc. 742 */ 743 744 #endif /* !UCONFIG_NO_NORMALIZATION */ 745 #endif /* __NORMALIZER2IMPL_H__ */ 746