Coverage Report

Created: 2025-10-24 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/icu/icu4c/source/common/normalizer2impl.h
Line
Count
Source
1
// © 2016 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
/*
4
*******************************************************************************
5
*
6
*   Copyright (C) 2009-2014, International Business Machines
7
*   Corporation and others.  All Rights Reserved.
8
*
9
*******************************************************************************
10
*   file name:  normalizer2impl.h
11
*   encoding:   UTF-8
12
*   tab size:   8 (not used)
13
*   indentation:4
14
*
15
*   created on: 2009nov22
16
*   created by: Markus W. Scherer
17
*/
18
19
#ifndef __NORMALIZER2IMPL_H__
20
#define __NORMALIZER2IMPL_H__
21
22
#include "unicode/utypes.h"
23
24
#if !UCONFIG_NO_NORMALIZATION
25
26
#include "unicode/normalizer2.h"
27
#include "unicode/ucptrie.h"
28
#include "unicode/unistr.h"
29
#include "unicode/unorm.h"
30
#include "unicode/utf.h"
31
#include "unicode/utf16.h"
32
#include "mutex.h"
33
#include "udataswp.h"
34
#include "uset_imp.h"
35
36
// When the nfc.nrm data is *not* hardcoded into the common library
37
// (with this constant set to 0),
38
// then it needs to be built into the data package:
39
// Add nfc.nrm to icu4c/source/data/Makefile.in DAT_FILES_SHORT
40
#define NORM2_HARDCODE_NFC_DATA 1
41
42
U_NAMESPACE_BEGIN
43
44
struct CanonIterData;
45
46
class ByteSink;
47
class Edits;
48
class InitCanonIterData;
49
class LcccContext;
50
51
class U_COMMON_API Hangul {
52
public:
53
    /* Korean Hangul and Jamo constants */
54
    enum {
55
        JAMO_L_BASE=0x1100,     /* "lead" jamo */
56
        JAMO_L_END=0x1112,
57
        JAMO_V_BASE=0x1161,     /* "vowel" jamo */
58
        JAMO_V_END=0x1175,
59
        JAMO_T_BASE=0x11a7,     /* "trail" jamo */
60
        JAMO_T_END=0x11c2,
61
62
        HANGUL_BASE=0xac00,
63
        HANGUL_END=0xd7a3,
64
65
        JAMO_L_COUNT=19,
66
        JAMO_V_COUNT=21,
67
        JAMO_T_COUNT=28,
68
69
        JAMO_VT_COUNT=JAMO_V_COUNT*JAMO_T_COUNT,
70
71
        HANGUL_COUNT=JAMO_L_COUNT*JAMO_V_COUNT*JAMO_T_COUNT,
72
        HANGUL_LIMIT=HANGUL_BASE+HANGUL_COUNT
73
    };
74
75
7.74M
    static inline UBool isHangul(UChar32 c) {
76
7.74M
        return HANGUL_BASE<=c && c<HANGUL_LIMIT;
77
7.74M
    }
78
    static inline UBool
79
3.05k
    isHangulLV(UChar32 c) {
80
3.05k
        c-=HANGUL_BASE;
81
3.05k
        return 0<=c && c<HANGUL_COUNT && c%JAMO_T_COUNT==0;
82
3.05k
    }
83
6.61M
    static inline UBool isJamoL(UChar32 c) {
84
6.61M
        return static_cast<uint32_t>(c - JAMO_L_BASE) < JAMO_L_COUNT;
85
6.61M
    }
86
1.71M
    static inline UBool isJamoV(UChar32 c) {
87
1.71M
        return static_cast<uint32_t>(c - JAMO_V_BASE) < JAMO_V_COUNT;
88
1.71M
    }
89
0
    static inline UBool isJamoT(UChar32 c) {
90
0
        int32_t t=c-JAMO_T_BASE;
91
0
        return 0<t && t<JAMO_T_COUNT;  // not JAMO_T_BASE itself
92
0
    }
93
0
    static UBool isJamo(UChar32 c) {
94
0
        return JAMO_L_BASE<=c && c<=JAMO_T_END &&
95
0
            (c<=JAMO_L_END || (JAMO_V_BASE<=c && c<=JAMO_V_END) || JAMO_T_BASE<c);
96
0
    }
97
98
    /**
99
     * Decomposes c, which must be a Hangul syllable, into buffer
100
     * and returns the length of the decomposition (2 or 3).
101
     */
102
52.6M
    static inline int32_t decompose(UChar32 c, char16_t buffer[3]) {
103
52.6M
        c-=HANGUL_BASE;
104
52.6M
        UChar32 c2=c%JAMO_T_COUNT;
105
52.6M
        c/=JAMO_T_COUNT;
106
52.6M
        buffer[0] = static_cast<char16_t>(JAMO_L_BASE + c / JAMO_V_COUNT);
107
52.6M
        buffer[1] = static_cast<char16_t>(JAMO_V_BASE + c % JAMO_V_COUNT);
108
52.6M
        if(c2==0) {
109
1.91M
            return 2;
110
50.7M
        } else {
111
50.7M
            buffer[2] = static_cast<char16_t>(JAMO_T_BASE + c2);
112
50.7M
            return 3;
113
50.7M
        }
114
52.6M
    }
115
116
    /**
117
     * Decomposes c, which must be a Hangul syllable, into buffer.
118
     * This is the raw, not recursive, decomposition. Its length is always 2.
119
     */
120
152
    static inline void getRawDecomposition(UChar32 c, char16_t buffer[2]) {
121
152
        UChar32 orig=c;
122
152
        c-=HANGUL_BASE;
123
152
        UChar32 c2=c%JAMO_T_COUNT;
124
152
        if(c2==0) {
125
74
            c/=JAMO_T_COUNT;
126
74
            buffer[0] = static_cast<char16_t>(JAMO_L_BASE + c / JAMO_V_COUNT);
127
74
            buffer[1] = static_cast<char16_t>(JAMO_V_BASE + c % JAMO_V_COUNT);
128
78
        } else {
129
78
            buffer[0] = static_cast<char16_t>(orig - c2); // LV syllable
130
78
            buffer[1] = static_cast<char16_t>(JAMO_T_BASE + c2);
131
78
        }
132
152
    }
133
private:
134
    Hangul() = delete;  // no instantiation
135
};
136
137
class Normalizer2Impl;
138
139
class U_COMMON_API ReorderingBuffer : public UMemory {
140
public:
141
    /** Constructs only; init() should be called. */
142
    ReorderingBuffer(const Normalizer2Impl &ni, UnicodeString &dest) :
143
127M
        impl(ni), str(dest),
144
127M
        start(nullptr), reorderStart(nullptr), limit(nullptr),
145
127M
        remainingCapacity(0), lastCC(0) {}
146
    /** Constructs, removes the string contents, and initializes for a small initial capacity. */
147
    ReorderingBuffer(const Normalizer2Impl &ni, UnicodeString &dest, UErrorCode &errorCode);
148
127M
    ~ReorderingBuffer() {
149
127M
        if (start != nullptr) {
150
127M
            str.releaseBuffer(static_cast<int32_t>(limit - start));
151
127M
        }
152
127M
    }
153
    UBool init(int32_t destCapacity, UErrorCode &errorCode);
154
155
4.18k
    UBool isEmpty() const { return start==limit; }
156
9.87M
    int32_t length() const { return static_cast<int32_t>(limit - start); }
157
9.87M
    char16_t *getStart() { return start; }
158
9.88M
    char16_t *getLimit() { return limit; }
159
0
    uint8_t getLastCC() const { return lastCC; }
160
161
    UBool equals(const char16_t *start, const char16_t *limit) const;
162
    UBool equals(const uint8_t *otherStart, const uint8_t *otherLimit) const;
163
164
40.6M
    UBool append(UChar32 c, uint8_t cc, UErrorCode &errorCode) {
165
40.6M
        return (c<=0xffff) ?
166
39.0M
            appendBMP(static_cast<char16_t>(c), cc, errorCode) :
167
40.6M
            appendSupplementary(c, cc, errorCode);
168
40.6M
    }
169
    UBool append(const char16_t *s, int32_t length, UBool isNFD,
170
                 uint8_t leadCC, uint8_t trailCC,
171
                 UErrorCode &errorCode);
172
39.0M
    UBool appendBMP(char16_t c, uint8_t cc, UErrorCode &errorCode) {
173
39.0M
        if(remainingCapacity==0 && !resize(1, errorCode)) {
174
0
            return false;
175
0
        }
176
39.0M
        if(lastCC<=cc || cc==0) {
177
29.5M
            *limit++=c;
178
29.5M
            lastCC=cc;
179
29.5M
            if(cc<=1) {
180
57.8k
                reorderStart=limit;
181
57.8k
            }
182
29.5M
        } else {
183
9.51M
            insert(c, cc);
184
9.51M
        }
185
39.0M
        --remainingCapacity;
186
39.0M
        return true;
187
39.0M
    }
188
    UBool appendZeroCC(UChar32 c, UErrorCode &errorCode);
189
    UBool appendZeroCC(const char16_t *s, const char16_t *sLimit, UErrorCode &errorCode);
190
    void remove();
191
    void removeSuffix(int32_t suffixLength);
192
9.87M
    void setReorderingLimit(char16_t *newLimit) {
193
9.87M
        remainingCapacity += static_cast<int32_t>(limit - newLimit);
194
9.87M
        reorderStart=limit=newLimit;
195
9.87M
        lastCC=0;
196
9.87M
    }
197
1.70k
    void copyReorderableSuffixTo(UnicodeString &s) const {
198
1.70k
        s.setTo(ConstChar16Ptr(reorderStart), static_cast<int32_t>(limit - reorderStart));
199
1.70k
    }
200
private:
201
    /*
202
     * TODO: Revisit whether it makes sense to track reorderStart.
203
     * It is set to after the last known character with cc<=1,
204
     * which stops previousCC() before it reads that character and looks up its cc.
205
     * previousCC() is normally only called from insert().
206
     * In other words, reorderStart speeds up the insertion of a combining mark
207
     * into a multi-combining mark sequence where it does not belong at the end.
208
     * This might not be worth the trouble.
209
     * On the other hand, it's not a huge amount of trouble.
210
     *
211
     * We probably need it for UNORM_SIMPLE_APPEND.
212
     */
213
214
    UBool appendSupplementary(UChar32 c, uint8_t cc, UErrorCode &errorCode);
215
    void insert(UChar32 c, uint8_t cc);
216
13.9M
    static void writeCodePoint(char16_t *p, UChar32 c) {
217
13.9M
        if(c<=0xffff) {
218
13.7M
            *p = static_cast<char16_t>(c);
219
13.7M
        } else {
220
219k
            p[0]=U16_LEAD(c);
221
219k
            p[1]=U16_TRAIL(c);
222
219k
        }
223
13.9M
    }
224
    UBool resize(int32_t appendLength, UErrorCode &errorCode);
225
226
    const Normalizer2Impl &impl;
227
    UnicodeString &str;
228
    char16_t *start, *reorderStart, *limit;
229
    int32_t remainingCapacity;
230
    uint8_t lastCC;
231
232
    // private backward iterator
233
14.0M
    void setIterator() { codePointStart=limit; }
234
    void skipPrevious();  // Requires start<codePointStart.
235
    uint8_t previousCC();  // Returns 0 if there is no previous character.
236
237
    char16_t *codePointStart, *codePointLimit;
238
};
239
240
/**
241
 * Low-level implementation of the Unicode Normalization Algorithm.
242
 * For the data structure and details see the documentation at the end of
243
 * this normalizer2impl.h and in the design doc at
244
 * https://unicode-org.github.io/icu/design/normalization/custom.html
245
 */
246
class U_COMMON_API_CLASS Normalizer2Impl : public UObject {
247
public:
248
15
    U_COMMON_API Normalizer2Impl() : normTrie(nullptr), fCanonIterData(nullptr) {}
249
    U_COMMON_API virtual ~Normalizer2Impl();
250
251
    U_COMMON_API void init(const int32_t* inIndexes,
252
                           const UCPTrie* inTrie,
253
                           const uint16_t* inExtraData,
254
                           const uint8_t* inSmallFCD);
255
256
    U_COMMON_API void addLcccChars(UnicodeSet& set) const;
257
    U_COMMON_API void addPropertyStarts(const USetAdder* sa, UErrorCode& errorCode) const;
258
    U_COMMON_API void addCanonIterPropertyStarts(const USetAdder* sa, UErrorCode& errorCode) const;
259
260
    // low-level properties ------------------------------------------------ ***
261
262
    U_COMMON_API UBool ensureCanonIterData(UErrorCode& errorCode) const;
263
264
    // The trie stores values for lead surrogate code *units*.
265
    // Surrogate code *points* are inert.
266
1.14G
    U_COMMON_API uint16_t getNorm16(UChar32 c) const {
267
1.14G
        return U_IS_LEAD(c) ?
268
5.34k
            static_cast<uint16_t>(INERT) :
269
1.14G
            UCPTRIE_FAST_GET(normTrie, UCPTRIE_16, c);
270
1.14G
    }
271
4.30M
    U_COMMON_API uint16_t getRawNorm16(UChar32 c) const {
272
4.30M
        return UCPTRIE_FAST_GET(normTrie, UCPTRIE_16, c);
273
4.30M
    }
274
275
12.7k
    U_COMMON_API UNormalizationCheckResult getCompQuickCheck(uint16_t norm16) const {
276
12.7k
        if(norm16<minNoNo || MIN_YES_YES_WITH_CC<=norm16) {
277
6.33k
            return UNORM_YES;
278
6.40k
        } else if(minMaybeNo<=norm16) {
279
227
            return UNORM_MAYBE;
280
6.17k
        } else {
281
6.17k
            return UNORM_NO;
282
6.17k
        }
283
12.7k
    }
284
15.0M
    U_COMMON_API UBool isAlgorithmicNoNo(uint16_t norm16) const {
285
15.0M
        return limitNoNo <= norm16 && norm16 < minMaybeNo;
286
15.0M
    }
287
4.16k
    U_COMMON_API UBool isCompNo(uint16_t norm16) const {
288
4.16k
        return minNoNo <= norm16 && norm16 < minMaybeNo;
289
4.16k
    }
290
3.07M
    U_COMMON_API UBool isDecompYes(uint16_t norm16) const {
291
3.07M
        return norm16 < minYesNo || minMaybeYes <= norm16;
292
3.07M
    }
293
294
16.8M
    U_COMMON_API uint8_t getCC(uint16_t norm16) const {
295
16.8M
        if(norm16>=MIN_NORMAL_MAYBE_YES) {
296
9.55M
            return getCCFromNormalYesOrMaybe(norm16);
297
9.55M
        }
298
7.26M
        if(norm16<minNoNo || limitNoNo<=norm16) {
299
5.92M
            return 0;
300
5.92M
        }
301
1.34M
        return getCCFromNoNo(norm16);
302
7.26M
    }
303
1.03G
    U_COMMON_API static uint8_t getCCFromNormalYesOrMaybe(uint16_t norm16) {
304
1.03G
        return static_cast<uint8_t>(norm16 >> OFFSET_SHIFT);
305
1.03G
    }
306
956M
    U_COMMON_API static uint8_t getCCFromYesOrMaybeYes(uint16_t norm16) {
307
956M
        return norm16>=MIN_NORMAL_MAYBE_YES ? getCCFromNormalYesOrMaybe(norm16) : 0;
308
956M
    }
309
854M
    U_COMMON_API uint8_t getCCFromYesOrMaybeYesCP(UChar32 c) const {
310
854M
        if (c < minCompNoMaybeCP) { return 0; }
311
854M
        return getCCFromYesOrMaybeYes(getNorm16(c));
312
854M
    }
313
314
    /**
315
     * Returns the FCD data for code point c.
316
     * @param c A Unicode code point.
317
     * @return The lccc(c) in bits 15..8 and tccc(c) in bits 7..0.
318
     */
319
166M
    U_COMMON_API uint16_t getFCD16(UChar32 c) const {
320
166M
        if(c<minDecompNoCP) {
321
13.8M
            return 0;
322
152M
        } else if(c<=0xffff) {
323
148M
            if(!singleLeadMightHaveNonZeroFCD16(c)) { return 0; }
324
148M
        }
325
119M
        return getFCD16FromNormData(c);
326
166M
    }
327
    /**
328
     * Returns the FCD data for the next code point (post-increment).
329
     * Might skip only a lead surrogate rather than the whole surrogate pair if none of
330
     * the supplementary code points associated with the lead surrogate have non-zero FCD data.
331
     * @param s A valid pointer into a string. Requires s!=limit.
332
     * @param limit The end of the string, or NULL.
333
     * @return The lccc(c) in bits 15..8 and tccc(c) in bits 7..0.
334
     */
335
566k
    U_COMMON_API uint16_t nextFCD16(const char16_t*& s, const char16_t* limit) const {
336
566k
        UChar32 c=*s++;
337
566k
        if(c<minDecompNoCP || !singleLeadMightHaveNonZeroFCD16(c)) {
338
15.9k
            return 0;
339
15.9k
        }
340
550k
        char16_t c2;
341
550k
        if(U16_IS_LEAD(c) && s!=limit && U16_IS_TRAIL(c2=*s)) {
342
15.3k
            c=U16_GET_SUPPLEMENTARY(c, c2);
343
15.3k
            ++s;
344
15.3k
        }
345
550k
        return getFCD16FromNormData(c);
346
566k
    }
347
    /**
348
     * Returns the FCD data for the previous code point (pre-decrement).
349
     * @param start The start of the string.
350
     * @param s A valid pointer into a string. Requires start<s.
351
     * @return The lccc(c) in bits 15..8 and tccc(c) in bits 7..0.
352
     */
353
3.94M
    U_COMMON_API uint16_t previousFCD16(const char16_t* start, const char16_t*& s) const {
354
3.94M
        UChar32 c=*--s;
355
3.94M
        if(c<minDecompNoCP) {
356
1.59k
            return 0;
357
1.59k
        }
358
3.94M
        if(!U16_IS_TRAIL(c)) {
359
3.92M
            if(!singleLeadMightHaveNonZeroFCD16(c)) {
360
7.69k
                return 0;
361
7.69k
            }
362
3.92M
        } else {
363
20.6k
            char16_t c2;
364
20.6k
            if(start<s && U16_IS_LEAD(c2=*(s-1))) {
365
19.8k
                c=U16_GET_SUPPLEMENTARY(c2, c);
366
19.8k
                --s;
367
19.8k
            }
368
20.6k
        }
369
3.93M
        return getFCD16FromNormData(c);
370
3.94M
    }
371
372
    /** Returns true if the single-or-lead code unit c might have non-zero FCD data. */
373
308M
    U_COMMON_API UBool singleLeadMightHaveNonZeroFCD16(UChar32 lead) const {
374
        // 0<=lead<=0xffff
375
308M
        uint8_t bits=smallFCD[lead>>8];
376
308M
        if(bits==0) { return false; }
377
189M
        return (bits >> ((lead >> 5) & 7)) & 1;
378
308M
    }
379
    /** Returns the FCD value from the regular normalization data. */
380
    U_COMMON_API uint16_t getFCD16FromNormData(UChar32 c) const;
381
382
    U_COMMON_API uint16_t getFCD16FromMaybeOrNonZeroCC(uint16_t norm16) const;
383
384
    /**
385
     * Gets the decomposition for one code point.
386
     * @param c code point
387
     * @param buffer out-only buffer for algorithmic decompositions
388
     * @param length out-only, takes the length of the decomposition, if any
389
     * @return pointer to the decomposition, or NULL if none
390
     */
391
    U_COMMON_API const char16_t* getDecomposition(UChar32 c, char16_t buffer[4], int32_t& length) const;
392
393
    /**
394
     * Gets the raw decomposition for one code point.
395
     * @param c code point
396
     * @param buffer out-only buffer for algorithmic decompositions
397
     * @param length out-only, takes the length of the decomposition, if any
398
     * @return pointer to the decomposition, or NULL if none
399
     */
400
    U_COMMON_API const char16_t* getRawDecomposition(UChar32 c,
401
                                                     char16_t buffer[30],
402
                                                     int32_t& length) const;
403
404
    U_COMMON_API UChar32 composePair(UChar32 a, UChar32 b) const;
405
406
    U_COMMON_API UBool isCanonSegmentStarter(UChar32 c) const;
407
    U_COMMON_API UBool getCanonStartSet(UChar32 c, UnicodeSet& set) const;
408
409
    enum {
410
        // Fixed norm16 values.
411
        MIN_YES_YES_WITH_CC=0xfe02,
412
        JAMO_VT=0xfe00,
413
        MIN_NORMAL_MAYBE_YES=0xfc00,
414
        JAMO_L=2,  // offset=1 hasCompBoundaryAfter=false
415
        INERT=1,  // offset=0 hasCompBoundaryAfter=true
416
417
        // norm16 bit 0 is comp-boundary-after.
418
        HAS_COMP_BOUNDARY_AFTER=1,
419
        OFFSET_SHIFT=1,
420
421
        // For algorithmic one-way mappings, norm16 bits 2..1 indicate the
422
        // tccc (0, 1, >1) for quick FCC boundary-after tests.
423
        DELTA_TCCC_0=0,
424
        DELTA_TCCC_1=2,
425
        DELTA_TCCC_GT_1=4,
426
        DELTA_TCCC_MASK=6,
427
        DELTA_SHIFT=3,
428
429
        MAX_DELTA=0x40
430
    };
431
432
    enum {
433
        // Byte offsets from the start of the data, after the generic header.
434
        IX_NORM_TRIE_OFFSET,
435
        IX_EXTRA_DATA_OFFSET,
436
        IX_SMALL_FCD_OFFSET,
437
        IX_RESERVED3_OFFSET,
438
        IX_RESERVED4_OFFSET,
439
        IX_RESERVED5_OFFSET,
440
        IX_RESERVED6_OFFSET,
441
        IX_TOTAL_SIZE,
442
443
        // Code point thresholds for quick check codes.
444
        IX_MIN_DECOMP_NO_CP,
445
        IX_MIN_COMP_NO_MAYBE_CP,
446
447
        // Norm16 value thresholds for quick check combinations and types of extra data.
448
449
        /** Mappings & compositions in [minYesNo..minYesNoMappingsOnly[. */
450
        IX_MIN_YES_NO,
451
        /** Mappings are comp-normalized. */
452
        IX_MIN_NO_NO,
453
        IX_LIMIT_NO_NO,
454
        IX_MIN_MAYBE_YES,
455
456
        /** Mappings only in [minYesNoMappingsOnly..minNoNo[. */
457
        IX_MIN_YES_NO_MAPPINGS_ONLY,
458
        /** Mappings are not comp-normalized but have a comp boundary before. */
459
        IX_MIN_NO_NO_COMP_BOUNDARY_BEFORE,
460
        /** Mappings do not have a comp boundary before. */
461
        IX_MIN_NO_NO_COMP_NO_MAYBE_CC,
462
        /** Mappings to the empty string. */
463
        IX_MIN_NO_NO_EMPTY,
464
465
        IX_MIN_LCCC_CP,
466
        IX_RESERVED19,
467
468
        /** Two-way mappings; each starts with a character that combines backward. */
469
        IX_MIN_MAYBE_NO,  // 20
470
        /** Two-way mappings & compositions. */
471
        IX_MIN_MAYBE_NO_COMBINES_FWD,
472
473
        IX_COUNT  // 22
474
    };
475
476
    enum {
477
        MAPPING_HAS_CCC_LCCC_WORD=0x80,
478
        MAPPING_HAS_RAW_MAPPING=0x40,
479
        // unused bit 0x20,
480
        MAPPING_LENGTH_MASK=0x1f
481
    };
482
483
    enum {
484
        COMP_1_LAST_TUPLE=0x8000,
485
        COMP_1_TRIPLE=1,
486
        COMP_1_TRAIL_LIMIT=0x3400,
487
        COMP_1_TRAIL_MASK=0x7ffe,
488
        COMP_1_TRAIL_SHIFT=9,  // 10-1 for the "triple" bit
489
        COMP_2_TRAIL_SHIFT=6,
490
        COMP_2_TRAIL_MASK=0xffc0
491
    };
492
493
    // higher-level functionality ------------------------------------------ ***
494
495
    // NFD without an NFD Normalizer2 instance.
496
    U_COMMON_API UnicodeString& decompose(const UnicodeString& src,
497
                                          UnicodeString& dest,
498
                                          UErrorCode& errorCode) const;
499
    /**
500
     * Decomposes [src, limit[ and writes the result to dest.
501
     * limit can be NULL if src is NUL-terminated.
502
     * destLengthEstimate is the initial dest buffer capacity and can be -1.
503
     */
504
    U_COMMON_API void decompose(const char16_t* src,
505
                                const char16_t* limit,
506
                                UnicodeString& dest,
507
                                int32_t destLengthEstimate,
508
                                UErrorCode& errorCode) const;
509
510
    U_COMMON_API const char16_t* decompose(const char16_t* src,
511
                                           const char16_t* limit,
512
                                           ReorderingBuffer* buffer,
513
                                           UErrorCode& errorCode) const;
514
    U_COMMON_API void decomposeAndAppend(const char16_t* src,
515
                                         const char16_t* limit,
516
                                         UBool doDecompose,
517
                                         UnicodeString& safeMiddle,
518
                                         ReorderingBuffer& buffer,
519
                                         UErrorCode& errorCode) const;
520
521
    /** sink==nullptr: isNormalized()/spanQuickCheckYes() */
522
    U_COMMON_API const uint8_t* decomposeUTF8(uint32_t options,
523
                                              const uint8_t* src,
524
                                              const uint8_t* limit,
525
                                              ByteSink* sink,
526
                                              Edits* edits,
527
                                              UErrorCode& errorCode) const;
528
529
    U_COMMON_API UBool compose(const char16_t* src,
530
                               const char16_t* limit,
531
                               UBool onlyContiguous,
532
                               UBool doCompose,
533
                               ReorderingBuffer& buffer,
534
                               UErrorCode& errorCode) const;
535
    U_COMMON_API const char16_t* composeQuickCheck(const char16_t* src,
536
                                                   const char16_t* limit,
537
                                                   UBool onlyContiguous,
538
                                                   UNormalizationCheckResult* pQCResult) const;
539
    U_COMMON_API void composeAndAppend(const char16_t* src,
540
                                       const char16_t* limit,
541
                                       UBool doCompose,
542
                                       UBool onlyContiguous,
543
                                       UnicodeString& safeMiddle,
544
                                       ReorderingBuffer& buffer,
545
                                       UErrorCode& errorCode) const;
546
547
    /** sink==nullptr: isNormalized() */
548
    U_COMMON_API UBool composeUTF8(uint32_t options,
549
                                   UBool onlyContiguous,
550
                                   const uint8_t* src,
551
                                   const uint8_t* limit,
552
                                   ByteSink* sink,
553
                                   icu::Edits* edits,
554
                                   UErrorCode& errorCode) const;
555
556
    U_COMMON_API const char16_t* makeFCD(const char16_t* src,
557
                                         const char16_t* limit,
558
                                         ReorderingBuffer* buffer,
559
                                         UErrorCode& errorCode) const;
560
    U_COMMON_API void makeFCDAndAppend(const char16_t* src,
561
                                       const char16_t* limit,
562
                                       UBool doMakeFCD,
563
                                       UnicodeString& safeMiddle,
564
                                       ReorderingBuffer& buffer,
565
                                       UErrorCode& errorCode) const;
566
567
    U_COMMON_API UBool hasDecompBoundaryBefore(UChar32 c) const;
568
    U_COMMON_API UBool norm16HasDecompBoundaryBefore(uint16_t norm16) const;
569
    U_COMMON_API UBool hasDecompBoundaryAfter(UChar32 c) const;
570
    U_COMMON_API UBool norm16HasDecompBoundaryAfter(uint16_t norm16) const;
571
4.24M
    U_COMMON_API UBool isDecompInert(UChar32 c) const { return isDecompYesAndZeroCC(getNorm16(c)); }
572
573
75.2M
    U_COMMON_API UBool hasCompBoundaryBefore(UChar32 c) const {
574
75.2M
        return c<minCompNoMaybeCP || norm16HasCompBoundaryBefore(getNorm16(c));
575
75.2M
    }
576
2.09k
    U_COMMON_API UBool hasCompBoundaryAfter(UChar32 c, UBool onlyContiguous) const {
577
2.09k
        return norm16HasCompBoundaryAfter(getNorm16(c), onlyContiguous);
578
2.09k
    }
579
14.0k
    U_COMMON_API UBool isCompInert(UChar32 c, UBool onlyContiguous) const {
580
14.0k
        uint16_t norm16=getNorm16(c);
581
14.0k
        return isCompYesAndZeroCC(norm16) &&
582
7.14k
            (norm16 & HAS_COMP_BOUNDARY_AFTER) != 0 &&
583
4.63k
            (!onlyContiguous || isInert(norm16) || *getDataForYesOrNo(norm16) <= 0x1ff);
584
            // The last check fetches the mapping's first unit and checks tccc<=1.
585
14.0k
    }
586
587
0
    U_COMMON_API UBool hasFCDBoundaryBefore(UChar32 c) const { return hasDecompBoundaryBefore(c); }
588
0
    U_COMMON_API UBool hasFCDBoundaryAfter(UChar32 c) const { return hasDecompBoundaryAfter(c); }
589
0
    U_COMMON_API UBool isFCDInert(UChar32 c) const { return getFCD16(c) <= 1; }
590
591
  private:
592
    friend class InitCanonIterData;
593
    friend class LcccContext;
594
595
65.4M
    UBool isMaybe(uint16_t norm16) const { return minMaybeNo<=norm16 && norm16<=JAMO_VT; }
596
45.8M
    UBool isMaybeYesOrNonZeroCC(uint16_t norm16) const { return norm16>=minMaybeYes; }
597
10.0k
    static UBool isInert(uint16_t norm16) { return norm16==INERT; }
598
251
    static UBool isJamoL(uint16_t norm16) { return norm16==JAMO_L; }
599
9.94M
    static UBool isJamoVT(uint16_t norm16) { return norm16==JAMO_VT; }
600
121M
    uint16_t hangulLVT() const { return minYesNoMappingsOnly|HAS_COMP_BOUNDARY_AFTER; }
601
64.5M
    UBool isHangulLV(uint16_t norm16) const { return norm16==minYesNo; }
602
121M
    UBool isHangulLVT(uint16_t norm16) const {
603
121M
        return norm16==hangulLVT();
604
121M
    }
605
128M
    UBool isCompYesAndZeroCC(uint16_t norm16) const { return norm16<minNoNo; }
606
    // UBool isCompYes(uint16_t norm16) const {
607
    //     return norm16>=MIN_YES_YES_WITH_CC || norm16<minNoNo;
608
    // }
609
    // UBool isCompYesOrMaybe(uint16_t norm16) const {
610
    //     return norm16<minNoNo || minMaybeNo<=norm16;
611
    // }
612
    // UBool hasZeroCCFromDecompYes(uint16_t norm16) const {
613
    //     return norm16<=MIN_NORMAL_MAYBE_YES || norm16==JAMO_VT;
614
    // }
615
4.24M
    UBool isDecompYesAndZeroCC(uint16_t norm16) const {
616
4.24M
        return norm16<minYesNo ||
617
12.9k
               norm16==JAMO_VT ||
618
10.6k
               (minMaybeYes<=norm16 && norm16<=MIN_NORMAL_MAYBE_YES);
619
4.24M
    }
620
    /**
621
     * A little faster and simpler than isDecompYesAndZeroCC() but does not include
622
     * the MaybeYes which combine-forward and have ccc=0.
623
     */
624
94.0M
    UBool isMostDecompYesAndZeroCC(uint16_t norm16) const {
625
94.0M
        return norm16<minYesNo || norm16==MIN_NORMAL_MAYBE_YES || norm16==JAMO_VT;
626
94.0M
    }
627
    /** Since formatVersion 5: same as isAlgorithmicNoNo() */
628
52.0M
    UBool isDecompNoAlgorithmic(uint16_t norm16) const { return limitNoNo<=norm16 && norm16<minMaybeNo; }
629
630
    // For use with isCompYes().
631
    // Perhaps the compiler can combine the two tests for MIN_YES_YES_WITH_CC.
632
    // static uint8_t getCCFromYes(uint16_t norm16) {
633
    //     return norm16>=MIN_YES_YES_WITH_CC ? getCCFromNormalYesOrMaybe(norm16) : 0;
634
    // }
635
1.34M
    uint8_t getCCFromNoNo(uint16_t norm16) const {
636
1.34M
        const uint16_t *mapping=getDataForYesOrNo(norm16);
637
1.34M
        if(*mapping&MAPPING_HAS_CCC_LCCC_WORD) {
638
1.33M
            return static_cast<uint8_t>(*(mapping - 1));
639
1.33M
        } else {
640
9.73k
            return 0;
641
9.73k
        }
642
1.34M
    }
643
    // requires that the [cpStart..cpLimit[ character passes isCompYesAndZeroCC()
644
0
    uint8_t getTrailCCFromCompYesAndZeroCC(uint16_t norm16) const {
645
0
        if(norm16<=minYesNo) {
646
0
            return 0;  // yesYes and Hangul LV have ccc=tccc=0
647
0
        } else {
648
            // For Hangul LVT we harmlessly fetch a firstUnit with tccc=0 here.
649
0
            return static_cast<uint8_t>(*getDataForYesOrNo(norm16) >> 8); // tccc from yesNo
650
0
        }
651
0
    }
652
    uint8_t getPreviousTrailCC(const char16_t *start, const char16_t *p) const;
653
    uint8_t getPreviousTrailCC(const uint8_t *start, const uint8_t *p) const;
654
655
    // Requires algorithmic-NoNo.
656
31.5k
    UChar32 mapAlgorithmic(UChar32 c, uint16_t norm16) const {
657
31.5k
        return c+(norm16>>DELTA_SHIFT)-centerNoNoDelta;
658
31.5k
    }
659
0
    UChar32 getAlgorithmicDelta(uint16_t norm16) const {
660
0
        return (norm16>>DELTA_SHIFT)-centerNoNoDelta;
661
0
    }
662
663
34.2M
    const uint16_t *getDataForYesOrNo(uint16_t norm16) const {
664
34.2M
        return extraData+(norm16>>OFFSET_SHIFT);
665
34.2M
    }
666
962
    const uint16_t *getDataForMaybe(uint16_t norm16) const {
667
962
        return extraData+((norm16-minMaybeNo+limitNoNo)>>OFFSET_SHIFT);
668
962
    }
669
135M
    const uint16_t *getData(uint16_t norm16) const {
670
135M
        if(norm16>=minMaybeNo) {
671
120k
            norm16=norm16-minMaybeNo+limitNoNo;
672
120k
        }
673
135M
        return extraData+(norm16>>OFFSET_SHIFT);
674
135M
    }
675
39.9M
    const uint16_t *getCompositionsListForDecompYes(uint16_t norm16) const {
676
39.9M
        if(norm16<JAMO_L || MIN_NORMAL_MAYBE_YES<=norm16) {
677
19.4M
            return nullptr;
678
20.4M
        } else {
679
            // if yesYes: if Jamo L: harmless empty list
680
20.4M
            return getData(norm16);
681
20.4M
        }
682
39.9M
    }
683
3.07M
    const uint16_t *getCompositionsListForComposite(uint16_t norm16) const {
684
        // A composite has both mapping & compositions list.
685
3.07M
        const uint16_t *list=getData(norm16);
686
3.07M
        return list+  // mapping pointer
687
3.07M
            1+  // +1 to skip the first unit with the mapping length
688
3.07M
            (*list&MAPPING_LENGTH_MASK);  // + mapping length
689
3.07M
    }
690
    /**
691
     * @param c code point must have compositions
692
     * @return compositions list pointer
693
     */
694
1.18M
    const uint16_t *getCompositionsList(uint16_t norm16) const {
695
1.18M
        return isDecompYes(norm16) ?
696
1.18M
                getCompositionsListForDecompYes(norm16) :
697
1.18M
                getCompositionsListForComposite(norm16);
698
1.18M
    }
699
700
    const char16_t *copyLowPrefixFromNulTerminated(const char16_t *src,
701
                                                UChar32 minNeedDataCP,
702
                                                ReorderingBuffer *buffer,
703
                                                UErrorCode &errorCode) const;
704
705
    enum StopAt { STOP_AT_LIMIT, STOP_AT_DECOMP_BOUNDARY, STOP_AT_COMP_BOUNDARY };
706
707
    const char16_t *decomposeShort(const char16_t *src, const char16_t *limit,
708
                                UBool stopAtCompBoundary, UBool onlyContiguous,
709
                                ReorderingBuffer &buffer, UErrorCode &errorCode) const;
710
    UBool decompose(UChar32 c, uint16_t norm16,
711
                    ReorderingBuffer &buffer, UErrorCode &errorCode) const;
712
713
    const uint8_t *decomposeShort(const uint8_t *src, const uint8_t *limit,
714
                                  StopAt stopAt, UBool onlyContiguous,
715
                                  ReorderingBuffer &buffer, UErrorCode &errorCode) const;
716
717
    static int32_t combine(const uint16_t *list, UChar32 trail);
718
    void addComposites(const uint16_t *list, UnicodeSet &set) const;
719
    void recompose(ReorderingBuffer &buffer, int32_t recomposeStartIndex,
720
                   UBool onlyContiguous) const;
721
722
308k
    UBool hasCompBoundaryBefore(UChar32 c, uint16_t norm16) const {
723
308k
        return c<minCompNoMaybeCP || norm16HasCompBoundaryBefore(norm16);
724
308k
    }
725
82.0M
    UBool norm16HasCompBoundaryBefore(uint16_t norm16) const  {
726
82.0M
        return norm16 < minNoNoCompNoMaybeCC || isAlgorithmicNoNo(norm16);
727
82.0M
    }
728
    UBool hasCompBoundaryBefore(const char16_t *src, const char16_t *limit) const;
729
    UBool hasCompBoundaryBefore(const uint8_t *src, const uint8_t *limit) const;
730
    UBool hasCompBoundaryAfter(const char16_t *start, const char16_t *p,
731
                               UBool onlyContiguous) const;
732
    UBool hasCompBoundaryAfter(const uint8_t *start, const uint8_t *p,
733
                               UBool onlyContiguous) const;
734
39.5M
    UBool norm16HasCompBoundaryAfter(uint16_t norm16, UBool onlyContiguous) const {
735
39.5M
        return (norm16 & HAS_COMP_BOUNDARY_AFTER) != 0 &&
736
26.0M
            (!onlyContiguous || isTrailCC01ForCompBoundaryAfter(norm16));
737
39.5M
    }
738
    /** For FCC: Given norm16 HAS_COMP_BOUNDARY_AFTER, does it have tccc<=1? */
739
0
    UBool isTrailCC01ForCompBoundaryAfter(uint16_t norm16) const {
740
0
        return isInert(norm16) || (isDecompNoAlgorithmic(norm16) ?
741
0
            (norm16 & DELTA_TCCC_MASK) <= DELTA_TCCC_1 : *getDataForYesOrNo(norm16) <= 0x1ff);
742
0
    }
743
744
    const char16_t *findPreviousCompBoundary(const char16_t *start, const char16_t *p,
745
                                             UBool onlyContiguous) const;
746
    const char16_t *findNextCompBoundary(const char16_t *p, const char16_t *limit,
747
                                         UBool onlyContiguous) const;
748
749
    const char16_t *findPreviousFCDBoundary(const char16_t *start, const char16_t *p) const;
750
    const char16_t *findNextFCDBoundary(const char16_t *p, const char16_t *limit) const;
751
752
    void makeCanonIterDataFromNorm16(UChar32 start, UChar32 end, const uint16_t norm16,
753
                                     CanonIterData &newData, UErrorCode &errorCode) const;
754
755
    int32_t getCanonValue(UChar32 c) const;
756
    const UnicodeSet &getCanonStartSet(int32_t n) const;
757
758
    // UVersionInfo dataVersion;
759
760
    // BMP code point thresholds for quick check loops looking at single UTF-16 code units.
761
    char16_t minDecompNoCP;
762
    char16_t minCompNoMaybeCP;
763
    char16_t minLcccCP;
764
765
    // Norm16 value thresholds for quick check combinations and types of extra data.
766
    uint16_t minYesNo;
767
    uint16_t minYesNoMappingsOnly;
768
    uint16_t minNoNo;
769
    uint16_t minNoNoCompBoundaryBefore;
770
    uint16_t minNoNoCompNoMaybeCC;
771
    uint16_t minNoNoEmpty;
772
    uint16_t limitNoNo;
773
    uint16_t centerNoNoDelta;
774
    uint16_t minMaybeNo;
775
    uint16_t minMaybeNoCombinesFwd;
776
    uint16_t minMaybeYes;
777
778
    const UCPTrie *normTrie;
779
    const uint16_t *extraData;  // mappings and/or compositions
780
    const uint8_t *smallFCD;  // [0x100] one bit per 32 BMP code points, set if any FCD!=0
781
782
    UInitOnce       fCanonIterDataInitOnce {};
783
    CanonIterData  *fCanonIterData;
784
};
785
786
// bits in canonIterData
787
16.4M
#define CANON_NOT_SEGMENT_STARTER 0x80000000
788
2.52M
#define CANON_HAS_COMPOSITIONS 0x40000000
789
2.52M
#define CANON_HAS_SET 0x200000
790
2.52M
#define CANON_VALUE_MASK 0x1fffff
791
792
/**
793
 * ICU-internal shortcut for quick access to standard Unicode normalization.
794
 */
795
class U_COMMON_API Normalizer2Factory {
796
public:
797
    static const Normalizer2 *getFCDInstance(UErrorCode &errorCode);
798
    static const Normalizer2 *getFCCInstance(UErrorCode &errorCode);
799
    static const Normalizer2 *getNoopInstance(UErrorCode &errorCode);
800
801
    static const Normalizer2 *getInstance(UNormalizationMode mode, UErrorCode &errorCode);
802
803
    static const Normalizer2Impl *getNFCImpl(UErrorCode &errorCode);
804
    static const Normalizer2Impl *getNFKCImpl(UErrorCode &errorCode);
805
    static const Normalizer2Impl *getNFKC_CFImpl(UErrorCode &errorCode);
806
807
    // Get the Impl instance of the Normalizer2.
808
    // Must be used only when it is known that norm2 is a Normalizer2WithImpl instance.
809
    static const Normalizer2Impl *getImpl(const Normalizer2 *norm2);
810
private:
811
    Normalizer2Factory() = delete;  // No instantiation.
812
};
813
814
U_NAMESPACE_END
815
816
U_CAPI int32_t U_EXPORT2
817
unorm2_swap(const UDataSwapper *ds,
818
            const void *inData, int32_t length, void *outData,
819
            UErrorCode *pErrorCode);
820
821
/**
822
 * Get the NF*_QC property for a code point, for u_getIntPropertyValue().
823
 * @internal
824
 */
825
U_CFUNC UNormalizationCheckResult
826
unorm_getQuickCheck(UChar32 c, UNormalizationMode mode);
827
828
/**
829
 * Gets the 16-bit FCD value (lead & trail CCs) for a code point, for u_getIntPropertyValue().
830
 * @internal
831
 */
832
U_CFUNC uint16_t
833
unorm_getFCD16(UChar32 c);
834
835
/**
836
 * Format of Normalizer2 .nrm data files.
837
 * Format version 5.0.
838
 *
839
 * Normalizer2 .nrm data files provide data for the Unicode Normalization algorithms.
840
 * ICU ships with data files for standard Unicode Normalization Forms
841
 * NFC and NFD (nfc.nrm), NFKC and NFKD (nfkc.nrm),
842
 * NFKC_Casefold (nfkc_cf.nrm) and NFKC_Simple_Casefold (nfkc_scf.nrm).
843
 * Custom (application-specific) data can be built into additional .nrm files
844
 * with the gennorm2 build tool.
845
 * ICU ships with one such file, uts46.nrm, for the implementation of UTS #46.
846
 *
847
 * Normalizer2.getInstance() causes a .nrm file to be loaded, unless it has been
848
 * cached already. Internally, Normalizer2Impl.load() reads the .nrm file.
849
 *
850
 * A .nrm file begins with a standard ICU data file header
851
 * (DataHeader, see ucmndata.h and unicode/udata.h).
852
 * The UDataInfo.dataVersion field usually contains the Unicode version
853
 * for which the data was generated.
854
 *
855
 * After the header, the file contains the following parts.
856
 * Constants are defined as enum values of the Normalizer2Impl class.
857
 *
858
 * Many details of the data structures are described in the design doc
859
 * which is at https://unicode-org.github.io/icu/design/normalization/custom.html
860
 *
861
 * int32_t indexes[indexesLength]; -- indexesLength=indexes[IX_NORM_TRIE_OFFSET]/4;
862
 *
863
 *      The first eight indexes are byte offsets in ascending order.
864
 *      Each byte offset marks the start of the next part in the data file,
865
 *      and the end of the previous one.
866
 *      When two consecutive byte offsets are the same, then the corresponding part is empty.
867
 *      Byte offsets are offsets from after the header,
868
 *      that is, from the beginning of the indexes[].
869
 *      Each part starts at an offset with proper alignment for its data.
870
 *      If necessary, the previous part may include padding bytes to achieve this alignment.
871
 *
872
 *      minDecompNoCP=indexes[IX_MIN_DECOMP_NO_CP] is the lowest code point
873
 *      with a decomposition mapping, that is, with NF*D_QC=No.
874
 *      minCompNoMaybeCP=indexes[IX_MIN_COMP_NO_MAYBE_CP] is the lowest code point
875
 *      with NF*C_QC=No (has a one-way mapping) or Maybe (combines backward).
876
 *      minLcccCP=indexes[IX_MIN_LCCC_CP] (index 18, new in formatVersion 3)
877
 *      is the lowest code point with lccc!=0.
878
 *
879
 *      The next eight indexes are thresholds of 16-bit trie values for ranges of
880
 *      values indicating multiple normalization properties.
881
 *      Format version 5 adds the two minMaybeNo* threshold indexes.
882
 *      The thresholds are listed here in threshold order,
883
 *      not in the order they are stored in the indexes.
884
 *          minYesNo=indexes[IX_MIN_YES_NO];
885
 *          minYesNoMappingsOnly=indexes[IX_MIN_YES_NO_MAPPINGS_ONLY];
886
 *          minNoNo=indexes[IX_MIN_NO_NO];
887
 *          minNoNoCompBoundaryBefore=indexes[IX_MIN_NO_NO_COMP_BOUNDARY_BEFORE];
888
 *          minNoNoCompNoMaybeCC=indexes[IX_MIN_NO_NO_COMP_NO_MAYBE_CC];
889
 *          minNoNoEmpty=indexes[IX_MIN_NO_NO_EMPTY];
890
 *          limitNoNo=indexes[IX_LIMIT_NO_NO];
891
 *          minMaybeNo=indexes[IX_MIN_MAYBE_NO];
892
 *          minMaybeNoCombinesFwd=indexes[IX_MIN_MAYBE_NO_COMBINES_FWD];
893
 *          minMaybeYes=indexes[IX_MIN_MAYBE_YES];
894
 *      See the normTrie description below and the design doc for details.
895
 *
896
 * UCPTrie normTrie; -- see ucptrie_impl.h and ucptrie.h, same as Java CodePointTrie
897
 *
898
 *      The trie holds the main normalization data. Each code point is mapped to a 16-bit value.
899
 *      Rather than using independent bits in the value (which would require more than 16 bits),
900
 *      information is extracted primarily via range checks.
901
 *      Except, format version 3+ uses bit 0 for hasCompBoundaryAfter().
902
 *      For example, a 16-bit value norm16 in the range minYesNo<=norm16<minNoNo
903
 *      means that the character has NF*C_QC=Yes and NF*D_QC=No properties,
904
 *      which means it has a two-way (round-trip) decomposition mapping.
905
 *      Values in the ranges 2<=norm16<limitNoNo and minMaybeNo<=norm16<minMaybeYes
906
 *      are also directly indexes into the extraData
907
 *      pointing to mappings, compositions lists, or both.
908
 *      Value norm16==INERT (0 in versions 1 & 2, 1 in version 3+)
909
 *      means that the character is normalization-inert, that is,
910
 *      it does not have a mapping, does not participate in composition, has a zero
911
 *      canonical combining class, and forms a boundary where text before it and after it
912
 *      can be normalized independently.
913
 *      For details about how multiple properties are encoded in 16-bit values
914
 *      see the design doc.
915
 *      Note that the encoding cannot express all combinations of the properties involved;
916
 *      it only supports those combinations that are allowed by
917
 *      the Unicode Normalization algorithms. Details are in the design doc as well.
918
 *      The gennorm2 tool only builds .nrm files for data that conforms to the limitations.
919
 *
920
 *      The trie has a value for each lead surrogate code unit representing the "worst case"
921
 *      properties of the 1024 supplementary characters whose UTF-16 form starts with
922
 *      the lead surrogate. If all of the 1024 supplementary characters are normalization-inert,
923
 *      then their lead surrogate code unit has the trie value INERT.
924
 *      When the lead surrogate unit's value exceeds the quick check minimum during processing,
925
 *      the properties for the full supplementary code point need to be looked up.
926
 *
927
 * uint16_t extraData[];
928
 *
929
 *      The extraData array contains many per-character data sections.
930
 *      Each section contains mappings and/or composition lists.
931
 *      The norm16 value of each character that has such data is directly an index to
932
 *      a section of the extraData array.
933
 *
934
 *      In version 3+, the norm16 values must be shifted right by OFFSET_SHIFT
935
 *      for accessing extraData.
936
 *
937
 *      The data structures for compositions lists and mappings are described in the design doc.
938
 *
939
 *      In version 4 and below, the composition lists for MaybeYes characters were stored before
940
 *      the data for other characters.
941
 *      This sub-array had a length of MIN_NORMAL_MAYBE_YES-minMaybeYes.
942
 *      In version 3 & 4, the difference must be shifted right by OFFSET_SHIFT.
943
 *
944
 *      In version 5, the data for MaybeNo and MaybeYes characters is stored after
945
 *      the data for other characters.
946
 *
947
 *      If there are no MaybeNo and no MaybeYes characters,
948
 *      then minMaybeYes==minMaybeNo==MIN_NORMAL_MAYBE_YES.
949
 *      If there are such characters, then minMaybeNo is subtracted from their norm16 values
950
 *      to get the index into the extraData.
951
 *      In version 4 and below, the data index for Yes* and No* characters needs to be
952
 *      offset by the length of the MaybeYes data.
953
 *      In version 5, the data index for Maybe* characters needs to be offset by limitNoNo.
954
 *
955
 *      Version 5 is the first to support MaybeNo characters, and
956
 *      adds the minMaybeNo and minMaybeNoCombinesFwd thresholds and
957
 *      the corresponding sections of the extraData.
958
 *
959
 * uint8_t smallFCD[0x100]; -- new in format version 2
960
 *
961
 *      This is a bit set to help speed up FCD value lookups in the absence of a full
962
 *      UTrie2 or other large data structure with the full FCD value mapping.
963
 *
964
 *      Each smallFCD bit is set if any of the corresponding 32 BMP code points
965
 *      has a non-zero FCD value (lccc!=0 or tccc!=0).
966
 *      Bit 0 of smallFCD[0] is for U+0000..U+001F. Bit 7 of smallFCD[0xff] is for U+FFE0..U+FFFF.
967
 *      A bit for 32 lead surrogates is set if any of the 32k corresponding
968
 *      _supplementary_ code points has a non-zero FCD value.
969
 *
970
 *      This bit set is most useful for the large blocks of CJK characters with FCD=0.
971
 *
972
 * Changes from format version 1 to format version 2 ---------------------------
973
 *
974
 * - Addition of data for raw (not recursively decomposed) mappings.
975
 *   + The MAPPING_NO_COMP_BOUNDARY_AFTER bit in the extraData is now also set when
976
 *     the mapping is to an empty string or when the character combines-forward.
977
 *     This subsumes the one actual use of the MAPPING_PLUS_COMPOSITION_LIST bit which
978
 *     is then repurposed for the MAPPING_HAS_RAW_MAPPING bit.
979
 *   + For details see the design doc.
980
 * - Addition of indexes[IX_MIN_YES_NO_MAPPINGS_ONLY] and separation of the yesNo extraData into
981
 *   distinct ranges (combines-forward vs. not)
982
 *   so that a range check can be used to find out if there is a compositions list.
983
 *   This is fully equivalent with formatVersion 1's MAPPING_PLUS_COMPOSITION_LIST flag.
984
 *   It is needed for the new (in ICU 49) composePair(), not for other normalization.
985
 * - Addition of the smallFCD[] bit set.
986
 *
987
 * Changes from format version 2 to format version 3 (ICU 60) ------------------
988
 *
989
 * - norm16 bit 0 indicates hasCompBoundaryAfter(),
990
 *   except that for contiguous composition (FCC) the tccc must be checked as well.
991
 *   Data indexes and ccc values are shifted left by one (OFFSET_SHIFT).
992
 *   Thresholds like minNoNo are tested before shifting.
993
 *
994
 * - Algorithmic mapping deltas are shifted left by two more bits (total DELTA_SHIFT),
995
 *   to make room for two bits (three values) indicating whether the tccc is 0, 1, or greater.
996
 *   See DELTA_TCCC_MASK etc.
997
 *   This helps with fetching tccc/FCD values and FCC hasCompBoundaryAfter().
998
 *   minMaybeNo is 8-aligned so that the DELTA_TCCC_MASK bits can be tested directly.
999
 *
1000
 * - Algorithmic mappings are only used for mapping to "comp yes and ccc=0" characters,
1001
 *   and ASCII characters are mapped algorithmically only to other ASCII characters.
1002
 *   This helps with hasCompBoundaryBefore() and compose() fast paths.
1003
 *   It is never necessary any more to loop for algorithmic mappings.
1004
 *
1005
 * - Addition of indexes[IX_MIN_NO_NO_COMP_BOUNDARY_BEFORE],
1006
 *   indexes[IX_MIN_NO_NO_COMP_NO_MAYBE_CC], and indexes[IX_MIN_NO_NO_EMPTY],
1007
 *   and separation of the noNo extraData into distinct ranges.
1008
 *   With this, the noNo norm16 value indicates whether the mapping is
1009
 *   compose-normalized, not normalized but hasCompBoundaryBefore(),
1010
 *   not even that, or maps to an empty string.
1011
 *   hasCompBoundaryBefore() can be determined solely from the norm16 value.
1012
 *
1013
 * - The norm16 value for Hangul LVT is now different from that for Hangul LV,
1014
 *   so that hasCompBoundaryAfter() need not check for the syllable type.
1015
 *   For Hangul LV, minYesNo continues to be used (no comp-boundary-after).
1016
 *   For Hangul LVT, minYesNoMappingsOnly|HAS_COMP_BOUNDARY_AFTER is used.
1017
 *   The extraData units at these indexes are set to firstUnit=2 and firstUnit=3, respectively,
1018
 *   to simplify some code.
1019
 *
1020
 * - The extraData firstUnit bit 5 is no longer necessary
1021
 *   (norm16 bit 0 used instead of firstUnit MAPPING_NO_COMP_BOUNDARY_AFTER),
1022
 *   is reserved again, and always set to 0.
1023
 *
1024
 * - Addition of indexes[IX_MIN_LCCC_CP], the first code point where lccc!=0.
1025
 *   This used to be hardcoded to U+0300, but in data like NFKC_Casefold it is lower:
1026
 *   U+00AD Soft Hyphen maps to an empty string,
1027
 *   which is artificially assigned "worst case" values lccc=1 and tccc=255.
1028
 *
1029
 * - A mapping to an empty string has explicit lccc=1 and tccc=255 values.
1030
 *
1031
 * Changes from format version 3 to format version 4 (ICU 63) ------------------
1032
 *
1033
 * Switched from UTrie2 to UCPTrie/CodePointTrie.
1034
 *
1035
 * The new trie no longer stores different values for surrogate code *units* vs.
1036
 * surrogate code *points*.
1037
 * Lead surrogates still have values for optimized UTF-16 string processing.
1038
 * When looking up code point properties, the code now checks for lead surrogates and
1039
 * treats them as inert.
1040
 *
1041
 * gennorm2 now has to reject mappings for surrogate code points.
1042
 * UTS #46 maps unpaired surrogates to U+FFFD in code rather than via its
1043
 * custom normalization data file.
1044
 *
1045
 * Changes from format version 4 to format version 5 (ICU 76) ------------------
1046
 *
1047
 * Unicode 16 adds the first MaybeYes characters which combine both backward and forward,
1048
 * taking this formerly theoretical data structure into reality.
1049
 *
1050
 * Unicode 16 also adds the first characters that have two-way mappings whose first characters
1051
 * combine backward. In order for normalization and the quick check to work properly,
1052
 * these composite characters also must be marked as NFC_QC=Maybe,
1053
 * corresponding to "combines back", although the composites themselves do not combine backward.
1054
 * Format version 5 adds two new ranges between "algorithmic NoNo" and MaybeYes,
1055
 * with thresholds minMaybeNo and minMaybeNoCombinesFwd,
1056
 * and indexes[IX_MIN_MAYBE_NO] and indexes[IX_MIN_MAYBE_NO_COMBINES_FWD],
1057
 * and corresponding mappings and composition lists in the extraData.
1058
 *
1059
 * Format version 5 moves the data for Maybe* characters from the start of the extraData array
1060
 * to its end.
1061
 */
1062
1063
#endif  /* !UCONFIG_NO_NORMALIZATION */
1064
#endif  /* __NORMALIZER2IMPL_H__ */