xref: /aosp_15_r20/external/cronet/third_party/icu/source/common/uprops.cpp (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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) 2002-2016, International Business Machines
7 *   Corporation and others.  All Rights Reserved.
8 *
9 *******************************************************************************
10 *   file name:  uprops.cpp
11 *   encoding:   UTF-8
12 *   tab size:   8 (not used)
13 *   indentation:4
14 *
15 *   created on: 2002feb24
16 *   created by: Markus W. Scherer
17 *
18 *   Implementations for mostly non-core Unicode character properties
19 *   stored in uprops.icu.
20 *
21 *   With the APIs implemented here, almost all properties files and
22 *   their associated implementation files are used from this file,
23 *   including those for normalization and case mappings.
24 */
25 
26 #include "unicode/utypes.h"
27 #include "unicode/uchar.h"
28 #include "unicode/ucptrie.h"
29 #include "unicode/udata.h"
30 #include "unicode/unorm2.h"
31 #include "unicode/uscript.h"
32 #include "unicode/ustring.h"
33 #include "unicode/utf16.h"
34 #include "cstring.h"
35 #include "emojiprops.h"
36 #include "mutex.h"
37 #include "normalizer2impl.h"
38 #include "umutex.h"
39 #include "ubidi_props.h"
40 #include "uprops.h"
41 #include "ucase.h"
42 #include "ucln_cmn.h"
43 #include "ulayout_props.h"
44 #include "ustr_imp.h"
45 
46 U_NAMESPACE_USE
47 
48 // Unicode text layout properties data -----------------------------------------
49 
50 namespace {
51 
52 icu::UInitOnce gLayoutInitOnce {};
53 UDataMemory *gLayoutMemory = nullptr;
54 
55 UCPTrie *gInpcTrie = nullptr;  // Indic_Positional_Category
56 UCPTrie *gInscTrie = nullptr;  // Indic_Syllabic_Category
57 UCPTrie *gVoTrie = nullptr;  // Vertical_Orientation
58 
59 int32_t gMaxInpcValue = 0;
60 int32_t gMaxInscValue = 0;
61 int32_t gMaxVoValue = 0;
62 
uprops_cleanup()63 UBool U_CALLCONV uprops_cleanup() {
64     udata_close(gLayoutMemory);
65     gLayoutMemory = nullptr;
66 
67     ucptrie_close(gInpcTrie);
68     gInpcTrie = nullptr;
69     ucptrie_close(gInscTrie);
70     gInscTrie = nullptr;
71     ucptrie_close(gVoTrie);
72     gVoTrie = nullptr;
73 
74     gMaxInpcValue = 0;
75     gMaxInscValue = 0;
76     gMaxVoValue = 0;
77 
78     gLayoutInitOnce.reset();
79     return true;
80 }
81 
82 UBool U_CALLCONV
ulayout_isAcceptable(void *,const char *,const char *,const UDataInfo * pInfo)83 ulayout_isAcceptable(void * /*context*/,
84                      const char * /* type */, const char * /*name*/,
85                      const UDataInfo *pInfo) {
86     return pInfo->size >= 20 &&
87         pInfo->isBigEndian == U_IS_BIG_ENDIAN &&
88         pInfo->charsetFamily == U_CHARSET_FAMILY &&
89         pInfo->dataFormat[0] == ULAYOUT_FMT_0 &&
90         pInfo->dataFormat[1] == ULAYOUT_FMT_1 &&
91         pInfo->dataFormat[2] == ULAYOUT_FMT_2 &&
92         pInfo->dataFormat[3] == ULAYOUT_FMT_3 &&
93         pInfo->formatVersion[0] == 1;
94 }
95 
96 // UInitOnce singleton initialization function
ulayout_load(UErrorCode & errorCode)97 void U_CALLCONV ulayout_load(UErrorCode &errorCode) {
98     gLayoutMemory = udata_openChoice(
99         nullptr, ULAYOUT_DATA_TYPE, ULAYOUT_DATA_NAME,
100         ulayout_isAcceptable, nullptr, &errorCode);
101     if (U_FAILURE(errorCode)) { return; }
102 
103     const uint8_t *inBytes = (const uint8_t *)udata_getMemory(gLayoutMemory);
104     const int32_t *inIndexes = (const int32_t *)inBytes;
105     int32_t indexesLength = inIndexes[ULAYOUT_IX_INDEXES_LENGTH];
106     if (indexesLength < 12) {
107         errorCode = U_INVALID_FORMAT_ERROR;  // Not enough indexes.
108         return;
109     }
110     int32_t offset = indexesLength * 4;
111     int32_t top = inIndexes[ULAYOUT_IX_INPC_TRIE_TOP];
112     int32_t trieSize = top - offset;
113     if (trieSize >= 16) {
114         gInpcTrie = ucptrie_openFromBinary(
115             UCPTRIE_TYPE_ANY, UCPTRIE_VALUE_BITS_ANY,
116             inBytes + offset, trieSize, nullptr, &errorCode);
117     }
118     offset = top;
119     top = inIndexes[ULAYOUT_IX_INSC_TRIE_TOP];
120     trieSize = top - offset;
121     if (trieSize >= 16) {
122         gInscTrie = ucptrie_openFromBinary(
123             UCPTRIE_TYPE_ANY, UCPTRIE_VALUE_BITS_ANY,
124             inBytes + offset, trieSize, nullptr, &errorCode);
125     }
126     offset = top;
127     top = inIndexes[ULAYOUT_IX_VO_TRIE_TOP];
128     trieSize = top - offset;
129     if (trieSize >= 16) {
130         gVoTrie = ucptrie_openFromBinary(
131             UCPTRIE_TYPE_ANY, UCPTRIE_VALUE_BITS_ANY,
132             inBytes + offset, trieSize, nullptr, &errorCode);
133     }
134 
135     uint32_t maxValues = inIndexes[ULAYOUT_IX_MAX_VALUES];
136     gMaxInpcValue = maxValues >> ULAYOUT_MAX_INPC_SHIFT;
137     gMaxInscValue = (maxValues >> ULAYOUT_MAX_INSC_SHIFT) & 0xff;
138     gMaxVoValue = (maxValues >> ULAYOUT_MAX_VO_SHIFT) & 0xff;
139 
140     ucln_common_registerCleanup(UCLN_COMMON_UPROPS, uprops_cleanup);
141 }
142 
ulayout_ensureData(UErrorCode & errorCode)143 UBool ulayout_ensureData(UErrorCode &errorCode) {
144     if (U_FAILURE(errorCode)) { return false; }
145     umtx_initOnce(gLayoutInitOnce, &ulayout_load, errorCode);
146     return U_SUCCESS(errorCode);
147 }
148 
ulayout_ensureData()149 UBool ulayout_ensureData() {
150     UErrorCode errorCode = U_ZERO_ERROR;
151     return ulayout_ensureData(errorCode);
152 }
153 
154 }  // namespace
155 
156 /* general properties API functions ----------------------------------------- */
157 
158 struct BinaryProperty;
159 
160 typedef UBool BinaryPropertyContains(const BinaryProperty &prop, UChar32 c, UProperty which);
161 
162 struct BinaryProperty {
163     int32_t column;  // SRC_PROPSVEC column, or "source" if mask==0
164     uint32_t mask;
165     BinaryPropertyContains *contains;
166 };
167 
defaultContains(const BinaryProperty & prop,UChar32 c,UProperty)168 static UBool defaultContains(const BinaryProperty &prop, UChar32 c, UProperty /*which*/) {
169     /* systematic, directly stored properties */
170     return (u_getUnicodeProperties(c, prop.column)&prop.mask)!=0;
171 }
172 
caseBinaryPropertyContains(const BinaryProperty &,UChar32 c,UProperty which)173 static UBool caseBinaryPropertyContains(const BinaryProperty &/*prop*/, UChar32 c, UProperty which) {
174     return static_cast<UBool>(ucase_hasBinaryProperty(c, which));
175 }
176 
isBidiControl(const BinaryProperty &,UChar32 c,UProperty)177 static UBool isBidiControl(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
178     return ubidi_isBidiControl(c);
179 }
180 
isMirrored(const BinaryProperty &,UChar32 c,UProperty)181 static UBool isMirrored(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
182     return ubidi_isMirrored(c);
183 }
184 
isJoinControl(const BinaryProperty &,UChar32 c,UProperty)185 static UBool isJoinControl(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
186     return ubidi_isJoinControl(c);
187 }
188 
189 #if UCONFIG_NO_NORMALIZATION
hasFullCompositionExclusion(const BinaryProperty &,UChar32,UProperty)190 static UBool hasFullCompositionExclusion(const BinaryProperty &, UChar32, UProperty) {
191     return false;
192 }
193 #else
hasFullCompositionExclusion(const BinaryProperty &,UChar32 c,UProperty)194 static UBool hasFullCompositionExclusion(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
195     // By definition, Full_Composition_Exclusion is the same as NFC_QC=No.
196     UErrorCode errorCode=U_ZERO_ERROR;
197     const Normalizer2Impl *impl=Normalizer2Factory::getNFCImpl(errorCode);
198     return U_SUCCESS(errorCode) && impl->isCompNo(impl->getNorm16(c));
199 }
200 #endif
201 
202 // UCHAR_NF*_INERT properties
203 #if UCONFIG_NO_NORMALIZATION
isNormInert(const BinaryProperty &,UChar32,UProperty)204 static UBool isNormInert(const BinaryProperty &, UChar32, UProperty) {
205     return false;
206 }
207 #else
isNormInert(const BinaryProperty &,UChar32 c,UProperty which)208 static UBool isNormInert(const BinaryProperty &/*prop*/, UChar32 c, UProperty which) {
209     UErrorCode errorCode=U_ZERO_ERROR;
210     const Normalizer2 *norm2=Normalizer2Factory::getInstance(
211         (UNormalizationMode)(which-UCHAR_NFD_INERT+UNORM_NFD), errorCode);
212     return U_SUCCESS(errorCode) && norm2->isInert(c);
213 }
214 #endif
215 
216 #if UCONFIG_NO_NORMALIZATION
changesWhenCasefolded(const BinaryProperty &,UChar32,UProperty)217 static UBool changesWhenCasefolded(const BinaryProperty &, UChar32, UProperty) {
218     return false;
219 }
220 #else
changesWhenCasefolded(const BinaryProperty &,UChar32 c,UProperty)221 static UBool changesWhenCasefolded(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
222     UnicodeString nfd;
223     UErrorCode errorCode=U_ZERO_ERROR;
224     const Normalizer2 *nfcNorm2=Normalizer2::getNFCInstance(errorCode);
225     if(U_FAILURE(errorCode)) {
226         return false;
227     }
228     if(nfcNorm2->getDecomposition(c, nfd)) {
229         /* c has a decomposition */
230         if(nfd.length()==1) {
231             c=nfd[0];  /* single BMP code point */
232         } else if(nfd.length()<=U16_MAX_LENGTH &&
233                   nfd.length()==U16_LENGTH(c=nfd.char32At(0))
234         ) {
235             /* single supplementary code point */
236         } else {
237             c=U_SENTINEL;
238         }
239     } else if(c<0) {
240         return false;  /* protect against bad input */
241     }
242     if(c>=0) {
243         /* single code point */
244         const char16_t *resultString;
245         return (UBool)(ucase_toFullFolding(c, &resultString, U_FOLD_CASE_DEFAULT)>=0);
246     } else {
247         /* guess some large but stack-friendly capacity */
248         char16_t dest[2*UCASE_MAX_STRING_LENGTH];
249         int32_t destLength;
250         destLength=u_strFoldCase(dest, UPRV_LENGTHOF(dest),
251                                   nfd.getBuffer(), nfd.length(),
252                                   U_FOLD_CASE_DEFAULT, &errorCode);
253         return (UBool)(U_SUCCESS(errorCode) &&
254                        0!=u_strCompare(nfd.getBuffer(), nfd.length(),
255                                        dest, destLength, false));
256     }
257 }
258 #endif
259 
260 #if UCONFIG_NO_NORMALIZATION
changesWhenNFKC_Casefolded(const BinaryProperty &,UChar32,UProperty)261 static UBool changesWhenNFKC_Casefolded(const BinaryProperty &, UChar32, UProperty) {
262     return false;
263 }
264 #else
changesWhenNFKC_Casefolded(const BinaryProperty &,UChar32 c,UProperty)265 static UBool changesWhenNFKC_Casefolded(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
266     UErrorCode errorCode=U_ZERO_ERROR;
267     const Normalizer2Impl *kcf=Normalizer2Factory::getNFKC_CFImpl(errorCode);
268     if(U_FAILURE(errorCode)) {
269         return false;
270     }
271     UnicodeString src(c);
272     UnicodeString dest;
273     {
274         // The ReorderingBuffer must be in a block because its destructor
275         // needs to release dest's buffer before we look at its contents.
276         ReorderingBuffer buffer(*kcf, dest);
277         // Small destCapacity for NFKC_CF(c).
278         if(buffer.init(5, errorCode)) {
279             const char16_t *srcArray=src.getBuffer();
280             kcf->compose(srcArray, srcArray+src.length(), false,
281                           true, buffer, errorCode);
282         }
283     }
284     return U_SUCCESS(errorCode) && dest!=src;
285 }
286 #endif
287 
288 #if UCONFIG_NO_NORMALIZATION
isCanonSegmentStarter(const BinaryProperty &,UChar32,UProperty)289 static UBool isCanonSegmentStarter(const BinaryProperty &, UChar32, UProperty) {
290     return false;
291 }
292 #else
isCanonSegmentStarter(const BinaryProperty &,UChar32 c,UProperty)293 static UBool isCanonSegmentStarter(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
294     UErrorCode errorCode=U_ZERO_ERROR;
295     const Normalizer2Impl *impl=Normalizer2Factory::getNFCImpl(errorCode);
296     return
297         U_SUCCESS(errorCode) && impl->ensureCanonIterData(errorCode) &&
298         impl->isCanonSegmentStarter(c);
299 }
300 #endif
301 
isPOSIX_alnum(const BinaryProperty &,UChar32 c,UProperty)302 static UBool isPOSIX_alnum(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
303     return u_isalnumPOSIX(c);
304 }
305 
isPOSIX_blank(const BinaryProperty &,UChar32 c,UProperty)306 static UBool isPOSIX_blank(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
307     return u_isblank(c);
308 }
309 
isPOSIX_graph(const BinaryProperty &,UChar32 c,UProperty)310 static UBool isPOSIX_graph(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
311     return u_isgraphPOSIX(c);
312 }
313 
isPOSIX_print(const BinaryProperty &,UChar32 c,UProperty)314 static UBool isPOSIX_print(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
315     return u_isprintPOSIX(c);
316 }
317 
isPOSIX_xdigit(const BinaryProperty &,UChar32 c,UProperty)318 static UBool isPOSIX_xdigit(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
319     return u_isxdigit(c);
320 }
321 
isRegionalIndicator(const BinaryProperty &,UChar32 c,UProperty)322 static UBool isRegionalIndicator(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
323     // Property starts are a subset of lb=RI etc.
324     return 0x1F1E6<=c && c<=0x1F1FF;
325 }
326 
hasEmojiProperty(const BinaryProperty &,UChar32 c,UProperty which)327 static UBool hasEmojiProperty(const BinaryProperty &/*prop*/, UChar32 c, UProperty which) {
328     return EmojiProps::hasBinaryProperty(c, which);
329 }
330 
isIDSUnaryOperator(const BinaryProperty &,UChar32 c,UProperty)331 static UBool isIDSUnaryOperator(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
332     // New in Unicode 15.1 for just two characters.
333     return 0x2FFE<=c && c<=0x2FFF;
334 }
335 
336 /** Ranges (start/limit pairs) of ID_Compat_Math_Continue (only), from UCD PropList.txt. */
337 static constexpr UChar32 ID_COMPAT_MATH_CONTINUE[] = {
338     0x00B2, 0x00B3 + 1,
339     0x00B9, 0x00B9 + 1,
340     0x2070, 0x2070 + 1,
341     0x2074, 0x207E + 1,
342     0x2080, 0x208E + 1
343 };
344 
345 /** ID_Compat_Math_Start characters, from UCD PropList.txt. */
346 static constexpr UChar32 ID_COMPAT_MATH_START[] = {
347     0x2202,
348     0x2207,
349     0x221E,
350     0x1D6C1,
351     0x1D6DB,
352     0x1D6FB,
353     0x1D715,
354     0x1D735,
355     0x1D74F,
356     0x1D76F,
357     0x1D789,
358     0x1D7A9,
359     0x1D7C3
360 };
361 
isIDCompatMathStart(const BinaryProperty &,UChar32 c,UProperty)362 static UBool isIDCompatMathStart(const BinaryProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
363     if (c < ID_COMPAT_MATH_START[0]) { return false; }  // fastpath for common scripts
364     for (UChar32 startChar : ID_COMPAT_MATH_START) {
365         if (c == startChar) { return true; }
366     }
367     return false;
368 }
369 
isIDCompatMathContinue(const BinaryProperty & prop,UChar32 c,UProperty)370 static UBool isIDCompatMathContinue(const BinaryProperty &prop, UChar32 c, UProperty /*which*/) {
371     for (int32_t i = 0; i < UPRV_LENGTHOF(ID_COMPAT_MATH_CONTINUE); i += 2) {
372         if (c < ID_COMPAT_MATH_CONTINUE[i]) { return false; }  // below range start
373         if (c < ID_COMPAT_MATH_CONTINUE[i + 1]) { return true; }  // below range limit
374     }
375     return isIDCompatMathStart(prop, c, UCHAR_ID_COMPAT_MATH_START);
376 }
377 
378 static const BinaryProperty binProps[UCHAR_BINARY_LIMIT]={
379     /*
380      * column and mask values for binary properties from u_getUnicodeProperties().
381      * Must be in order of corresponding UProperty,
382      * and there must be exactly one entry per binary UProperty.
383      *
384      * Properties with mask==0 are handled in code.
385      * For them, column is the UPropertySource value.
386      */
387     { 1,                U_MASK(UPROPS_ALPHABETIC), defaultContains },
388     { 1,                U_MASK(UPROPS_ASCII_HEX_DIGIT), defaultContains },
389     { UPROPS_SRC_BIDI,  0, isBidiControl },
390     { UPROPS_SRC_BIDI,  0, isMirrored },
391     { 1,                U_MASK(UPROPS_DASH), defaultContains },
392     { 1,                U_MASK(UPROPS_DEFAULT_IGNORABLE_CODE_POINT), defaultContains },
393     { 1,                U_MASK(UPROPS_DEPRECATED), defaultContains },
394     { 1,                U_MASK(UPROPS_DIACRITIC), defaultContains },
395     { 1,                U_MASK(UPROPS_EXTENDER), defaultContains },
396     { UPROPS_SRC_NFC,   0, hasFullCompositionExclusion },
397     { 1,                U_MASK(UPROPS_GRAPHEME_BASE), defaultContains },
398     { 1,                U_MASK(UPROPS_GRAPHEME_EXTEND), defaultContains },
399     { 1,                U_MASK(UPROPS_GRAPHEME_LINK), defaultContains },
400     { 1,                U_MASK(UPROPS_HEX_DIGIT), defaultContains },
401     { 1,                U_MASK(UPROPS_HYPHEN), defaultContains },
402     { 1,                U_MASK(UPROPS_ID_CONTINUE), defaultContains },
403     { 1,                U_MASK(UPROPS_ID_START), defaultContains },
404     { 1,                U_MASK(UPROPS_IDEOGRAPHIC), defaultContains },
405     { 1,                U_MASK(UPROPS_IDS_BINARY_OPERATOR), defaultContains },
406     { 1,                U_MASK(UPROPS_IDS_TRINARY_OPERATOR), defaultContains },
407     { UPROPS_SRC_BIDI,  0, isJoinControl },
408     { 1,                U_MASK(UPROPS_LOGICAL_ORDER_EXCEPTION), defaultContains },
409     { UPROPS_SRC_CASE,  0, caseBinaryPropertyContains },  // UCHAR_LOWERCASE
410     { 1,                U_MASK(UPROPS_MATH), defaultContains },
411     { 1,                U_MASK(UPROPS_NONCHARACTER_CODE_POINT), defaultContains },
412     { 1,                U_MASK(UPROPS_QUOTATION_MARK), defaultContains },
413     { 1,                U_MASK(UPROPS_RADICAL), defaultContains },
414     { UPROPS_SRC_CASE,  0, caseBinaryPropertyContains },  // UCHAR_SOFT_DOTTED
415     { 1,                U_MASK(UPROPS_TERMINAL_PUNCTUATION), defaultContains },
416     { 1,                U_MASK(UPROPS_UNIFIED_IDEOGRAPH), defaultContains },
417     { UPROPS_SRC_CASE,  0, caseBinaryPropertyContains },  // UCHAR_UPPERCASE
418     { 1,                U_MASK(UPROPS_WHITE_SPACE), defaultContains },
419     { 1,                U_MASK(UPROPS_XID_CONTINUE), defaultContains },
420     { 1,                U_MASK(UPROPS_XID_START), defaultContains },
421     { UPROPS_SRC_CASE,  0, caseBinaryPropertyContains },  // UCHAR_CASE_SENSITIVE
422     { 1,                U_MASK(UPROPS_S_TERM), defaultContains },
423     { 1,                U_MASK(UPROPS_VARIATION_SELECTOR), defaultContains },
424     { UPROPS_SRC_NFC,   0, isNormInert },  // UCHAR_NFD_INERT
425     { UPROPS_SRC_NFKC,  0, isNormInert },  // UCHAR_NFKD_INERT
426     { UPROPS_SRC_NFC,   0, isNormInert },  // UCHAR_NFC_INERT
427     { UPROPS_SRC_NFKC,  0, isNormInert },  // UCHAR_NFKC_INERT
428     { UPROPS_SRC_NFC_CANON_ITER, 0, isCanonSegmentStarter },
429     { 1,                U_MASK(UPROPS_PATTERN_SYNTAX), defaultContains },
430     { 1,                U_MASK(UPROPS_PATTERN_WHITE_SPACE), defaultContains },
431     { UPROPS_SRC_CHAR_AND_PROPSVEC,  0, isPOSIX_alnum },
432     { UPROPS_SRC_CHAR,  0, isPOSIX_blank },
433     { UPROPS_SRC_CHAR,  0, isPOSIX_graph },
434     { UPROPS_SRC_CHAR,  0, isPOSIX_print },
435     { UPROPS_SRC_CHAR,  0, isPOSIX_xdigit },
436     { UPROPS_SRC_CASE,  0, caseBinaryPropertyContains },  // UCHAR_CASED
437     { UPROPS_SRC_CASE,  0, caseBinaryPropertyContains },  // UCHAR_CASE_IGNORABLE
438     { UPROPS_SRC_CASE,  0, caseBinaryPropertyContains },  // UCHAR_CHANGES_WHEN_LOWERCASED
439     { UPROPS_SRC_CASE,  0, caseBinaryPropertyContains },  // UCHAR_CHANGES_WHEN_UPPERCASED
440     { UPROPS_SRC_CASE,  0, caseBinaryPropertyContains },  // UCHAR_CHANGES_WHEN_TITLECASED
441     { UPROPS_SRC_CASE_AND_NORM,  0, changesWhenCasefolded },
442     { UPROPS_SRC_CASE,  0, caseBinaryPropertyContains },  // UCHAR_CHANGES_WHEN_CASEMAPPED
443     { UPROPS_SRC_NFKC_CF, 0, changesWhenNFKC_Casefolded },
444     { UPROPS_SRC_EMOJI, 0, hasEmojiProperty },  // UCHAR_EMOJI
445     { UPROPS_SRC_EMOJI, 0, hasEmojiProperty },  // UCHAR_EMOJI_PRESENTATION
446     { UPROPS_SRC_EMOJI, 0, hasEmojiProperty },  // UCHAR_EMOJI_MODIFIER
447     { UPROPS_SRC_EMOJI, 0, hasEmojiProperty },  // UCHAR_EMOJI_MODIFIER_BASE
448     { UPROPS_SRC_EMOJI, 0, hasEmojiProperty },  // UCHAR_EMOJI_COMPONENT
449     { 2,                0, isRegionalIndicator },
450     { 1,                U_MASK(UPROPS_PREPENDED_CONCATENATION_MARK), defaultContains },
451     { UPROPS_SRC_EMOJI, 0, hasEmojiProperty },  // UCHAR_EXTENDED_PICTOGRAPHIC
452     { UPROPS_SRC_EMOJI, 0, hasEmojiProperty },  // UCHAR_BASIC_EMOJI
453     { UPROPS_SRC_EMOJI, 0, hasEmojiProperty },  // UCHAR_EMOJI_KEYCAP_SEQUENCE
454     { UPROPS_SRC_EMOJI, 0, hasEmojiProperty },  // UCHAR_RGI_EMOJI_MODIFIER_SEQUENCE
455     { UPROPS_SRC_EMOJI, 0, hasEmojiProperty },  // UCHAR_RGI_EMOJI_FLAG_SEQUENCE
456     { UPROPS_SRC_EMOJI, 0, hasEmojiProperty },  // UCHAR_RGI_EMOJI_TAG_SEQUENCE
457     { UPROPS_SRC_EMOJI, 0, hasEmojiProperty },  // UCHAR_RGI_EMOJI_ZWJ_SEQUENCE
458     { UPROPS_SRC_EMOJI, 0, hasEmojiProperty },  // UCHAR_RGI_EMOJI
459     { UPROPS_SRC_IDSU, 0, isIDSUnaryOperator }, // UCHAR_IDS_UNARY_OPERATOR
460     { UPROPS_SRC_ID_COMPAT_MATH, 0, isIDCompatMathStart }, // UCHAR_ID_COMPAT_MATH_START
461     { UPROPS_SRC_ID_COMPAT_MATH, 0, isIDCompatMathContinue }, // UCHAR_ID_COMPAT_MATH_CONTINUE
462 };
463 
464 U_CAPI UBool U_EXPORT2
u_hasBinaryProperty(UChar32 c,UProperty which)465 u_hasBinaryProperty(UChar32 c, UProperty which) {
466     /* c is range-checked in the functions that are called from here */
467     if(which<UCHAR_BINARY_START || UCHAR_BINARY_LIMIT<=which) {
468         /* not a known binary property */
469         return false;
470     } else {
471         const BinaryProperty &prop=binProps[which];
472         return prop.contains(prop, c, which);
473     }
474 }
475 
476 /* Checks if the Unicode character can start a Unicode identifier.*/
477 U_CAPI UBool U_EXPORT2
u_isIDStart(UChar32 c)478 u_isIDStart(UChar32 c) {
479     return u_hasBinaryProperty(c, UCHAR_ID_START);
480 }
481 
482 /* Checks if the Unicode character can be a Unicode identifier part other than starting the
483  identifier.*/
484 U_CAPI UBool U_EXPORT2
u_isIDPart(UChar32 c)485 u_isIDPart(UChar32 c) {
486     return u_hasBinaryProperty(c, UCHAR_ID_CONTINUE);
487 }
488 
489 U_CAPI UBool U_EXPORT2
u_stringHasBinaryProperty(const char16_t * s,int32_t length,UProperty which)490 u_stringHasBinaryProperty(const char16_t *s, int32_t length, UProperty which) {
491     if (s == nullptr && length != 0) { return false; }
492     if (length == 1) {
493         return u_hasBinaryProperty(s[0], which);  // single code point
494     } else if (length == 2 || (length < 0 && *s != 0)) {  // not empty string
495         // first code point
496         int32_t i = 0;
497         UChar32 c;
498         U16_NEXT(s, i, length, c);
499         if (length > 0 ? i == length : s[i] == 0) {
500             return u_hasBinaryProperty(c, which);  // single code point
501         }
502     }
503     // Only call into EmojiProps for a relevant property,
504     // so that we not unnecessarily try to load its data file.
505     return UCHAR_BASIC_EMOJI <= which && which <= UCHAR_RGI_EMOJI &&
506         EmojiProps::hasBinaryProperty(s, length, which);
507 }
508 
509 struct IntProperty;
510 
511 typedef int32_t IntPropertyGetValue(const IntProperty &prop, UChar32 c, UProperty which);
512 typedef int32_t IntPropertyGetMaxValue(const IntProperty &prop, UProperty which);
513 
514 struct IntProperty {
515     int32_t column;  // SRC_PROPSVEC column, or "source" if mask==0
516     uint32_t mask;
517     int32_t shift;  // =maxValue if getMaxValueFromShift() is used
518     IntPropertyGetValue *getValue;
519     IntPropertyGetMaxValue *getMaxValue;
520 };
521 
defaultGetValue(const IntProperty & prop,UChar32 c,UProperty)522 static int32_t defaultGetValue(const IntProperty &prop, UChar32 c, UProperty /*which*/) {
523     /* systematic, directly stored properties */
524     return (int32_t)(u_getUnicodeProperties(c, prop.column)&prop.mask)>>prop.shift;
525 }
526 
defaultGetMaxValue(const IntProperty & prop,UProperty)527 static int32_t defaultGetMaxValue(const IntProperty &prop, UProperty /*which*/) {
528     return (uprv_getMaxValues(prop.column)&prop.mask)>>prop.shift;
529 }
530 
getMaxValueFromShift(const IntProperty & prop,UProperty)531 static int32_t getMaxValueFromShift(const IntProperty &prop, UProperty /*which*/) {
532     return prop.shift;
533 }
534 
getBiDiClass(const IntProperty &,UChar32 c,UProperty)535 static int32_t getBiDiClass(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
536     return (int32_t)u_charDirection(c);
537 }
538 
getBiDiPairedBracketType(const IntProperty &,UChar32 c,UProperty)539 static int32_t getBiDiPairedBracketType(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
540     return (int32_t)ubidi_getPairedBracketType(c);
541 }
542 
biDiGetMaxValue(const IntProperty &,UProperty which)543 static int32_t biDiGetMaxValue(const IntProperty &/*prop*/, UProperty which) {
544     return ubidi_getMaxValue(which);
545 }
546 
547 #if UCONFIG_NO_NORMALIZATION
getCombiningClass(const IntProperty &,UChar32,UProperty)548 static int32_t getCombiningClass(const IntProperty &, UChar32, UProperty) {
549     return 0;
550 }
551 #else
getCombiningClass(const IntProperty &,UChar32 c,UProperty)552 static int32_t getCombiningClass(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
553     return u_getCombiningClass(c);
554 }
555 #endif
556 
getGeneralCategory(const IntProperty &,UChar32 c,UProperty)557 static int32_t getGeneralCategory(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
558     return (int32_t)u_charType(c);
559 }
560 
getJoiningGroup(const IntProperty &,UChar32 c,UProperty)561 static int32_t getJoiningGroup(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
562     return ubidi_getJoiningGroup(c);
563 }
564 
getJoiningType(const IntProperty &,UChar32 c,UProperty)565 static int32_t getJoiningType(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
566     return ubidi_getJoiningType(c);
567 }
568 
getNumericType(const IntProperty &,UChar32 c,UProperty)569 static int32_t getNumericType(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
570     int32_t ntv=(int32_t)GET_NUMERIC_TYPE_VALUE(u_getMainProperties(c));
571     return UPROPS_NTV_GET_TYPE(ntv);
572 }
573 
getScript(const IntProperty &,UChar32 c,UProperty)574 static int32_t getScript(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
575     UErrorCode errorCode=U_ZERO_ERROR;
576     return (int32_t)uscript_getScript(c, &errorCode);
577 }
578 
scriptGetMaxValue(const IntProperty &,UProperty)579 static int32_t scriptGetMaxValue(const IntProperty &/*prop*/, UProperty /*which*/) {
580     uint32_t scriptX=uprv_getMaxValues(0)&UPROPS_SCRIPT_X_MASK;
581     return uprops_mergeScriptCodeOrIndex(scriptX);
582 }
583 
584 /*
585  * Map some of the Grapheme Cluster Break values to Hangul Syllable Types.
586  * Hangul_Syllable_Type is fully redundant with a subset of Grapheme_Cluster_Break.
587  */
588 static const UHangulSyllableType gcbToHst[]={
589     U_HST_NOT_APPLICABLE,   /* U_GCB_OTHER */
590     U_HST_NOT_APPLICABLE,   /* U_GCB_CONTROL */
591     U_HST_NOT_APPLICABLE,   /* U_GCB_CR */
592     U_HST_NOT_APPLICABLE,   /* U_GCB_EXTEND */
593     U_HST_LEADING_JAMO,     /* U_GCB_L */
594     U_HST_NOT_APPLICABLE,   /* U_GCB_LF */
595     U_HST_LV_SYLLABLE,      /* U_GCB_LV */
596     U_HST_LVT_SYLLABLE,     /* U_GCB_LVT */
597     U_HST_TRAILING_JAMO,    /* U_GCB_T */
598     U_HST_VOWEL_JAMO        /* U_GCB_V */
599     /*
600      * Omit GCB values beyond what we need for hst.
601      * The code below checks for the array length.
602      */
603 };
604 
getHangulSyllableType(const IntProperty &,UChar32 c,UProperty)605 static int32_t getHangulSyllableType(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
606     /* see comments on gcbToHst[] above */
607     int32_t gcb=(int32_t)(u_getUnicodeProperties(c, 2)&UPROPS_GCB_MASK)>>UPROPS_GCB_SHIFT;
608     if(gcb<UPRV_LENGTHOF(gcbToHst)) {
609         return gcbToHst[gcb];
610     } else {
611         return U_HST_NOT_APPLICABLE;
612     }
613 }
614 
615 #if UCONFIG_NO_NORMALIZATION
getNormQuickCheck(const IntProperty &,UChar32,UProperty)616 static int32_t getNormQuickCheck(const IntProperty &, UChar32, UProperty) {
617     return 0;
618 }
619 #else
getNormQuickCheck(const IntProperty &,UChar32 c,UProperty which)620 static int32_t getNormQuickCheck(const IntProperty &/*prop*/, UChar32 c, UProperty which) {
621     return (int32_t)unorm_getQuickCheck(c, (UNormalizationMode)(which-UCHAR_NFD_QUICK_CHECK+UNORM_NFD));
622 }
623 #endif
624 
625 #if UCONFIG_NO_NORMALIZATION
getLeadCombiningClass(const IntProperty &,UChar32,UProperty)626 static int32_t getLeadCombiningClass(const IntProperty &, UChar32, UProperty) {
627     return 0;
628 }
629 #else
getLeadCombiningClass(const IntProperty &,UChar32 c,UProperty)630 static int32_t getLeadCombiningClass(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
631     return unorm_getFCD16(c)>>8;
632 }
633 #endif
634 
635 #if UCONFIG_NO_NORMALIZATION
getTrailCombiningClass(const IntProperty &,UChar32,UProperty)636 static int32_t getTrailCombiningClass(const IntProperty &, UChar32, UProperty) {
637     return 0;
638 }
639 #else
getTrailCombiningClass(const IntProperty &,UChar32 c,UProperty)640 static int32_t getTrailCombiningClass(const IntProperty &/*prop*/, UChar32 c, UProperty /*which*/) {
641     return unorm_getFCD16(c)&0xff;
642 }
643 #endif
644 
getInPC(const IntProperty &,UChar32 c,UProperty)645 static int32_t getInPC(const IntProperty &, UChar32 c, UProperty) {
646     return ulayout_ensureData() && gInpcTrie != nullptr ? ucptrie_get(gInpcTrie, c) : 0;
647 }
648 
getInSC(const IntProperty &,UChar32 c,UProperty)649 static int32_t getInSC(const IntProperty &, UChar32 c, UProperty) {
650     return ulayout_ensureData() && gInscTrie != nullptr ? ucptrie_get(gInscTrie, c) : 0;
651 }
652 
getVo(const IntProperty &,UChar32 c,UProperty)653 static int32_t getVo(const IntProperty &, UChar32 c, UProperty) {
654     return ulayout_ensureData() && gVoTrie != nullptr ? ucptrie_get(gVoTrie, c) : 0;
655 }
656 
layoutGetMaxValue(const IntProperty &,UProperty which)657 static int32_t layoutGetMaxValue(const IntProperty &/*prop*/, UProperty which) {
658     if (!ulayout_ensureData()) { return 0; }
659     switch (which) {
660     case UCHAR_INDIC_POSITIONAL_CATEGORY:
661         return gMaxInpcValue;
662     case UCHAR_INDIC_SYLLABIC_CATEGORY:
663         return gMaxInscValue;
664     case UCHAR_VERTICAL_ORIENTATION:
665         return gMaxVoValue;
666     default:
667         return 0;
668     }
669 }
670 
671 static const IntProperty intProps[UCHAR_INT_LIMIT-UCHAR_INT_START]={
672     /*
673      * column, mask and shift values for int-value properties from u_getUnicodeProperties().
674      * Must be in order of corresponding UProperty,
675      * and there must be exactly one entry per int UProperty.
676      *
677      * Properties with mask==0 are handled in code.
678      * For them, column is the UPropertySource value.
679      */
680     { UPROPS_SRC_BIDI,  0, 0,                               getBiDiClass, biDiGetMaxValue },
681     { 0,                UPROPS_BLOCK_MASK, UPROPS_BLOCK_SHIFT, defaultGetValue, defaultGetMaxValue },
682     { UPROPS_SRC_NFC,   0, 0xff,                            getCombiningClass, getMaxValueFromShift },
683     { 2,                UPROPS_DT_MASK, 0,                  defaultGetValue, defaultGetMaxValue },
684     { 0,                UPROPS_EA_MASK, UPROPS_EA_SHIFT,    defaultGetValue, defaultGetMaxValue },
685     { UPROPS_SRC_CHAR,  0, (int32_t)U_CHAR_CATEGORY_COUNT-1,getGeneralCategory, getMaxValueFromShift },
686     { UPROPS_SRC_BIDI,  0, 0,                               getJoiningGroup, biDiGetMaxValue },
687     { UPROPS_SRC_BIDI,  0, 0,                               getJoiningType, biDiGetMaxValue },
688     { 2,                UPROPS_LB_MASK, UPROPS_LB_SHIFT,    defaultGetValue, defaultGetMaxValue },
689     { UPROPS_SRC_CHAR,  0, (int32_t)U_NT_COUNT-1,           getNumericType, getMaxValueFromShift },
690     { UPROPS_SRC_PROPSVEC, 0, 0,                            getScript, scriptGetMaxValue },
691     { UPROPS_SRC_PROPSVEC, 0, (int32_t)U_HST_COUNT-1,       getHangulSyllableType, getMaxValueFromShift },
692     // UCHAR_NFD_QUICK_CHECK: max=1=YES -- never "maybe", only "no" or "yes"
693     { UPROPS_SRC_NFC,   0, (int32_t)UNORM_YES,              getNormQuickCheck, getMaxValueFromShift },
694     // UCHAR_NFKD_QUICK_CHECK: max=1=YES -- never "maybe", only "no" or "yes"
695     { UPROPS_SRC_NFKC,  0, (int32_t)UNORM_YES,              getNormQuickCheck, getMaxValueFromShift },
696     // UCHAR_NFC_QUICK_CHECK: max=2=MAYBE
697     { UPROPS_SRC_NFC,   0, (int32_t)UNORM_MAYBE,            getNormQuickCheck, getMaxValueFromShift },
698     // UCHAR_NFKC_QUICK_CHECK: max=2=MAYBE
699     { UPROPS_SRC_NFKC,  0, (int32_t)UNORM_MAYBE,            getNormQuickCheck, getMaxValueFromShift },
700     { UPROPS_SRC_NFC,   0, 0xff,                            getLeadCombiningClass, getMaxValueFromShift },
701     { UPROPS_SRC_NFC,   0, 0xff,                            getTrailCombiningClass, getMaxValueFromShift },
702     { 2,                UPROPS_GCB_MASK, UPROPS_GCB_SHIFT,  defaultGetValue, defaultGetMaxValue },
703     { 2,                UPROPS_SB_MASK, UPROPS_SB_SHIFT,    defaultGetValue, defaultGetMaxValue },
704     { 2,                UPROPS_WB_MASK, UPROPS_WB_SHIFT,    defaultGetValue, defaultGetMaxValue },
705     { UPROPS_SRC_BIDI,  0, 0,                               getBiDiPairedBracketType, biDiGetMaxValue },
706     { UPROPS_SRC_INPC,  0, 0,                               getInPC, layoutGetMaxValue },
707     { UPROPS_SRC_INSC,  0, 0,                               getInSC, layoutGetMaxValue },
708     { UPROPS_SRC_VO,    0, 0,                               getVo, layoutGetMaxValue },
709 };
710 
711 U_CAPI int32_t U_EXPORT2
u_getIntPropertyValue(UChar32 c,UProperty which)712 u_getIntPropertyValue(UChar32 c, UProperty which) {
713     if(which<UCHAR_INT_START) {
714         if(UCHAR_BINARY_START<=which && which<UCHAR_BINARY_LIMIT) {
715             const BinaryProperty &prop=binProps[which];
716             return prop.contains(prop, c, which);
717         }
718     } else if(which<UCHAR_INT_LIMIT) {
719         const IntProperty &prop=intProps[which-UCHAR_INT_START];
720         return prop.getValue(prop, c, which);
721     } else if(which==UCHAR_GENERAL_CATEGORY_MASK) {
722         return U_MASK(u_charType(c));
723     }
724     return 0;  // undefined
725 }
726 
727 U_CAPI int32_t U_EXPORT2
u_getIntPropertyMinValue(UProperty)728 u_getIntPropertyMinValue(UProperty /*which*/) {
729     return 0; /* all binary/enum/int properties have a minimum value of 0 */
730 }
731 
732 U_CAPI int32_t U_EXPORT2
u_getIntPropertyMaxValue(UProperty which)733 u_getIntPropertyMaxValue(UProperty which) {
734     if(which<UCHAR_INT_START) {
735         if(UCHAR_BINARY_START<=which && which<UCHAR_BINARY_LIMIT) {
736             return 1;  // maximum true for all binary properties
737         }
738     } else if(which<UCHAR_INT_LIMIT) {
739         const IntProperty &prop=intProps[which-UCHAR_INT_START];
740         return prop.getMaxValue(prop, which);
741     }
742     return -1;  // undefined
743 }
744 
745 U_CFUNC UPropertySource U_EXPORT2
uprops_getSource(UProperty which)746 uprops_getSource(UProperty which) {
747     if(which<UCHAR_BINARY_START) {
748         return UPROPS_SRC_NONE; /* undefined */
749     } else if(which<UCHAR_BINARY_LIMIT) {
750         const BinaryProperty &prop=binProps[which];
751         if(prop.mask!=0) {
752             return UPROPS_SRC_PROPSVEC;
753         } else {
754             return (UPropertySource)prop.column;
755         }
756     } else if(which<UCHAR_INT_START) {
757         return UPROPS_SRC_NONE; /* undefined */
758     } else if(which<UCHAR_INT_LIMIT) {
759         const IntProperty &prop=intProps[which-UCHAR_INT_START];
760         if(prop.mask!=0) {
761             return UPROPS_SRC_PROPSVEC;
762         } else {
763             return (UPropertySource)prop.column;
764         }
765     } else if(which<UCHAR_STRING_START) {
766         switch(which) {
767         case UCHAR_GENERAL_CATEGORY_MASK:
768         case UCHAR_NUMERIC_VALUE:
769             return UPROPS_SRC_CHAR;
770 
771         default:
772             return UPROPS_SRC_NONE;
773         }
774     } else if(which<UCHAR_STRING_LIMIT) {
775         switch(which) {
776         case UCHAR_AGE:
777             return UPROPS_SRC_PROPSVEC;
778 
779         case UCHAR_BIDI_MIRRORING_GLYPH:
780             return UPROPS_SRC_BIDI;
781 
782         case UCHAR_CASE_FOLDING:
783         case UCHAR_LOWERCASE_MAPPING:
784         case UCHAR_SIMPLE_CASE_FOLDING:
785         case UCHAR_SIMPLE_LOWERCASE_MAPPING:
786         case UCHAR_SIMPLE_TITLECASE_MAPPING:
787         case UCHAR_SIMPLE_UPPERCASE_MAPPING:
788         case UCHAR_TITLECASE_MAPPING:
789         case UCHAR_UPPERCASE_MAPPING:
790             return UPROPS_SRC_CASE;
791 
792         case UCHAR_ISO_COMMENT:
793         case UCHAR_NAME:
794         case UCHAR_UNICODE_1_NAME:
795             return UPROPS_SRC_NAMES;
796 
797         default:
798             return UPROPS_SRC_NONE;
799         }
800     } else {
801         switch(which) {
802         case UCHAR_SCRIPT_EXTENSIONS:
803             return UPROPS_SRC_PROPSVEC;
804         default:
805             return UPROPS_SRC_NONE; /* undefined */
806         }
807     }
808 }
809 
810 U_CFUNC void U_EXPORT2
uprops_addPropertyStarts(UPropertySource src,const USetAdder * sa,UErrorCode * pErrorCode)811 uprops_addPropertyStarts(UPropertySource src, const USetAdder *sa, UErrorCode *pErrorCode) {
812     if (U_FAILURE(*pErrorCode)) { return; }
813     if (src == UPROPS_SRC_ID_COMPAT_MATH) {
814         // range limits
815         for (UChar32 c : ID_COMPAT_MATH_CONTINUE) {
816             sa->add(sa->set, c);
817         }
818         // single characters
819         for (UChar32 c : ID_COMPAT_MATH_START) {
820             sa->add(sa->set, c);
821             sa->add(sa->set, c + 1);
822         }
823         return;
824     }
825     if (!ulayout_ensureData(*pErrorCode)) { return; }
826     const UCPTrie *trie;
827     switch (src) {
828     case UPROPS_SRC_INPC:
829         trie = gInpcTrie;
830         break;
831     case UPROPS_SRC_INSC:
832         trie = gInscTrie;
833         break;
834     case UPROPS_SRC_VO:
835         trie = gVoTrie;
836         break;
837     default:
838         *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
839         return;
840     }
841 
842     if (trie == nullptr) {
843         *pErrorCode = U_MISSING_RESOURCE_ERROR;
844         return;
845     }
846 
847     // Add the start code point of each same-value range of the trie.
848     UChar32 start = 0, end;
849     while ((end = ucptrie_getRange(trie, start, UCPMAP_RANGE_NORMAL, 0,
850                                    nullptr, nullptr, nullptr)) >= 0) {
851         sa->add(sa->set, start);
852         start = end + 1;
853     }
854 }
855 
856 #if !UCONFIG_NO_NORMALIZATION
857 
858 U_CAPI int32_t U_EXPORT2
u_getFC_NFKC_Closure(UChar32 c,char16_t * dest,int32_t destCapacity,UErrorCode * pErrorCode)859 u_getFC_NFKC_Closure(UChar32 c, char16_t *dest, int32_t destCapacity, UErrorCode *pErrorCode) {
860     if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
861         return 0;
862     }
863     if(destCapacity<0 || (dest==nullptr && destCapacity>0)) {
864         *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
865         return 0;
866     }
867     // Compute the FC_NFKC_Closure on the fly:
868     // We have the API for complete coverage of Unicode properties, although
869     // this value by itself is not useful via API.
870     // (What could be useful is a custom normalization table that combines
871     // case folding and NFKC.)
872     // For the derivation, see Unicode's DerivedNormalizationProps.txt.
873     const Normalizer2 *nfkc=Normalizer2::getNFKCInstance(*pErrorCode);
874     if(U_FAILURE(*pErrorCode)) {
875         return 0;
876     }
877     // first: b = NFKC(Fold(a))
878     UnicodeString folded1String;
879     const char16_t *folded1;
880     int32_t folded1Length=ucase_toFullFolding(c, &folded1, U_FOLD_CASE_DEFAULT);
881     if(folded1Length<0) {
882         const Normalizer2Impl *nfkcImpl=Normalizer2Factory::getImpl(nfkc);
883         if(nfkcImpl->getCompQuickCheck(nfkcImpl->getNorm16(c))!=UNORM_NO) {
884             return u_terminateUChars(dest, destCapacity, 0, pErrorCode);  // c does not change at all under CaseFolding+NFKC
885         }
886         folded1String.setTo(c);
887     } else {
888         if(folded1Length>UCASE_MAX_STRING_LENGTH) {
889             folded1String.setTo(folded1Length);
890         } else {
891             folded1String.setTo(false, folded1, folded1Length);
892         }
893     }
894     UnicodeString kc1=nfkc->normalize(folded1String, *pErrorCode);
895     // second: c = NFKC(Fold(b))
896     UnicodeString folded2String(kc1);
897     UnicodeString kc2=nfkc->normalize(folded2String.foldCase(), *pErrorCode);
898     // if (c != b) add the mapping from a to c
899     if(U_FAILURE(*pErrorCode) || kc1==kc2) {
900         return u_terminateUChars(dest, destCapacity, 0, pErrorCode);
901     } else {
902         return kc2.extract(dest, destCapacity, *pErrorCode);
903     }
904 }
905 
906 #endif
907