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