intl/icu/source/common/normalizer2impl.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial