Coverage Report

Created: 2025-06-13 06:34

/src/icu/icu4c/source/common/ucase.cpp
Line
Count
Source (jump to first uncovered line)
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) 2004-2014, International Business Machines
7
*   Corporation and others.  All Rights Reserved.
8
*
9
*******************************************************************************
10
*   file name:  ucase.cpp
11
*   encoding:   UTF-8
12
*   tab size:   8 (not used)
13
*   indentation:4
14
*
15
*   created on: 2004aug30
16
*   created by: Markus W. Scherer
17
*
18
*   Low-level Unicode character/string case mapping code.
19
*   Much code moved here (and modified) from uchar.c.
20
*/
21
22
#include "unicode/utypes.h"
23
#include "unicode/unistr.h"
24
#include "unicode/uset.h"
25
#include "unicode/utf16.h"
26
#include "cmemory.h"
27
#include "uassert.h"
28
#include "ucase.h"
29
#include "umutex.h"
30
#include "utrie2.h"
31
32
/* ucase_props_data.h is machine-generated by genprops/casepropsbuilder.cpp */
33
#define INCLUDED_FROM_UCASE_CPP
34
#include "ucase_props_data.h"
35
36
/* set of property starts for UnicodeSet ------------------------------------ */
37
38
static UBool U_CALLCONV
39
6.19k
_enumPropertyStartsRange(const void *context, UChar32 start, UChar32 /*end*/, uint32_t /*value*/) {
40
    /* add the start code point to the USet */
41
6.19k
    const USetAdder* sa = static_cast<const USetAdder*>(context);
42
6.19k
    sa->add(sa->set, start);
43
6.19k
    return true;
44
6.19k
}
45
46
U_CFUNC void U_EXPORT2
47
2
ucase_addPropertyStarts(const USetAdder *sa, UErrorCode *pErrorCode) {
48
2
    if(U_FAILURE(*pErrorCode)) {
49
0
        return;
50
0
    }
51
52
    /* add the start code point of each same-value range of the trie */
53
2
    utrie2_enum(&ucase_props_singleton.trie, nullptr, _enumPropertyStartsRange, sa);
54
55
    /* add code points with hardcoded properties, plus the ones following them */
56
57
    /* (none right now, see comment below) */
58
59
    /*
60
     * Omit code points with hardcoded specialcasing properties
61
     * because we do not build property UnicodeSets for them right now.
62
     */
63
2
}
64
65
/* data access primitives --------------------------------------------------- */
66
67
U_CAPI const struct UCaseProps * U_EXPORT2
68
0
ucase_getSingleton(int32_t *pExceptionsLength, int32_t *pUnfoldLength) {
69
0
    *pExceptionsLength = UPRV_LENGTHOF(ucase_props_exceptions);
70
0
    *pUnfoldLength = UPRV_LENGTHOF(ucase_props_unfold);
71
0
    return &ucase_props_singleton;
72
0
}
73
74
U_CFUNC const UTrie2 * U_EXPORT2
75
1.93k
ucase_getTrie() {
76
1.93k
    return &ucase_props_singleton.trie;
77
1.93k
}
78
79
3.35k
#define GET_EXCEPTIONS(csp, props) ((csp)->exceptions+((props)>>UCASE_EXC_SHIFT))
80
81
/* number of bits in an 8-bit integer value */
82
static const uint8_t flagsOffset[256]={
83
    0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
84
    1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
85
    1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
86
    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
87
    1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
88
    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
89
    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
90
    3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
91
    1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
92
    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
93
    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
94
    3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
95
    2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
96
    3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
97
    3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
98
    4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
99
};
100
101
9.49k
#define HAS_SLOT(flags, idx) ((flags)&(1<<(idx)))
102
1.79k
#define SLOT_OFFSET(flags, idx) flagsOffset[(flags)&((1<<(idx))-1)]
103
104
/*
105
 * Get the value of an optional-value slot where HAS_SLOT(excWord, idx).
106
 *
107
 * @param excWord (in) initial exceptions word
108
 * @param idx (in) desired slot index
109
 * @param pExc16 (in/out) const uint16_t * after excWord=*pExc16++;
110
 *               moved to the last uint16_t of the value, use +1 for beginning of next slot
111
 * @param value (out) int32_t or uint32_t output if hasSlot, otherwise not modified
112
 */
113
1.79k
#define GET_SLOT_VALUE(excWord, idx, pExc16, value) UPRV_BLOCK_MACRO_BEGIN { \
114
1.79k
    if(((excWord)&UCASE_EXC_DOUBLE_SLOTS)==0) { \
115
1.79k
        (pExc16)+=SLOT_OFFSET(excWord, idx); \
116
1.79k
        (value)=*pExc16; \
117
1.79k
    } else { \
118
0
        (pExc16)+=2*SLOT_OFFSET(excWord, idx); \
119
0
        (value)=*pExc16++; \
120
0
        (value)=((value)<<16)|*pExc16; \
121
0
    } \
122
2.21k
} UPRV_BLOCK_MACRO_END
123
124
/* simple case mappings ----------------------------------------------------- */
125
126
U_CAPI UChar32 U_EXPORT2
127
0
ucase_tolower(UChar32 c) {
128
0
    uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
129
0
    if(!UCASE_HAS_EXCEPTION(props)) {
130
0
        if(UCASE_IS_UPPER_OR_TITLE(props)) {
131
0
            c+=UCASE_GET_DELTA(props);
132
0
        }
133
0
    } else {
134
0
        const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props);
135
0
        uint16_t excWord=*pe++;
136
0
        if(HAS_SLOT(excWord, UCASE_EXC_DELTA) && UCASE_IS_UPPER_OR_TITLE(props)) {
137
0
            int32_t delta;
138
0
            GET_SLOT_VALUE(excWord, UCASE_EXC_DELTA, pe, delta);
139
0
            return (excWord&UCASE_EXC_DELTA_IS_NEGATIVE)==0 ? c+delta : c-delta;
140
0
        }
141
0
        if(HAS_SLOT(excWord, UCASE_EXC_LOWER)) {
142
0
            GET_SLOT_VALUE(excWord, UCASE_EXC_LOWER, pe, c);
143
0
        }
144
0
    }
145
0
    return c;
146
0
}
147
148
U_CAPI UChar32 U_EXPORT2
149
0
ucase_toupper(UChar32 c) {
150
0
    uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
151
0
    if(!UCASE_HAS_EXCEPTION(props)) {
152
0
        if(UCASE_GET_TYPE(props)==UCASE_LOWER) {
153
0
            c+=UCASE_GET_DELTA(props);
154
0
        }
155
0
    } else {
156
0
        const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props);
157
0
        uint16_t excWord=*pe++;
158
0
        if(HAS_SLOT(excWord, UCASE_EXC_DELTA) && UCASE_GET_TYPE(props)==UCASE_LOWER) {
159
0
            int32_t delta;
160
0
            GET_SLOT_VALUE(excWord, UCASE_EXC_DELTA, pe, delta);
161
0
            return (excWord&UCASE_EXC_DELTA_IS_NEGATIVE)==0 ? c+delta : c-delta;
162
0
        }
163
0
        if(HAS_SLOT(excWord, UCASE_EXC_UPPER)) {
164
0
            GET_SLOT_VALUE(excWord, UCASE_EXC_UPPER, pe, c);
165
0
        }
166
0
    }
167
0
    return c;
168
0
}
169
170
U_CAPI UChar32 U_EXPORT2
171
0
ucase_totitle(UChar32 c) {
172
0
    uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
173
0
    if(!UCASE_HAS_EXCEPTION(props)) {
174
0
        if(UCASE_GET_TYPE(props)==UCASE_LOWER) {
175
0
            c+=UCASE_GET_DELTA(props);
176
0
        }
177
0
    } else {
178
0
        const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props);
179
0
        uint16_t excWord=*pe++;
180
0
        if(HAS_SLOT(excWord, UCASE_EXC_DELTA) && UCASE_GET_TYPE(props)==UCASE_LOWER) {
181
0
            int32_t delta;
182
0
            GET_SLOT_VALUE(excWord, UCASE_EXC_DELTA, pe, delta);
183
0
            return (excWord&UCASE_EXC_DELTA_IS_NEGATIVE)==0 ? c+delta : c-delta;
184
0
        }
185
0
        int32_t idx;
186
0
        if(HAS_SLOT(excWord, UCASE_EXC_TITLE)) {
187
0
            idx=UCASE_EXC_TITLE;
188
0
        } else if(HAS_SLOT(excWord, UCASE_EXC_UPPER)) {
189
0
            idx=UCASE_EXC_UPPER;
190
0
        } else {
191
0
            return c;
192
0
        }
193
0
        GET_SLOT_VALUE(excWord, idx, pe, c);
194
0
    }
195
0
    return c;
196
0
}
197
198
static const char16_t iDot[2] = { 0x69, 0x307 };
199
static const char16_t jDot[2] = { 0x6a, 0x307 };
200
static const char16_t iOgonekDot[3] = { 0x12f, 0x307 };
201
static const char16_t iDotGrave[3] = { 0x69, 0x307, 0x300 };
202
static const char16_t iDotAcute[3] = { 0x69, 0x307, 0x301 };
203
static const char16_t iDotTilde[3] = { 0x69, 0x307, 0x303 };
204
205
206
U_CFUNC void U_EXPORT2
207
0
ucase_addCaseClosure(UChar32 c, const USetAdder *sa) {
208
0
    uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
209
0
    if(!UCASE_HAS_EXCEPTION(props)) {
210
0
        if(UCASE_GET_TYPE(props)!=UCASE_NONE) {
211
            /* add the one simple case mapping, no matter what type it is */
212
0
            int32_t delta=UCASE_GET_DELTA(props);
213
0
            if(delta!=0) {
214
0
                sa->add(sa->set, c+delta);
215
0
            }
216
0
        }
217
0
    } else {
218
        /*
219
         * c has exceptions, so there may be multiple simple and/or
220
         * full case mappings. Add them all.
221
         */
222
0
        const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props);
223
0
        uint16_t excWord=*pe++;
224
0
        const uint16_t *pe0=pe;
225
226
        // Hardcode the case closure of i and its relatives and ignore the
227
        // data file data for these characters.
228
        // The Turkic dotless i and dotted I with their case mapping conditions
229
        // and case folding option make the related characters behave specially.
230
        // This code matches their closure behavior to their case folding behavior.
231
0
        if (excWord&UCASE_EXC_CONDITIONAL_FOLD) {
232
            // These characters have Turkic case foldings. Hardcode their closure.
233
0
            if (c == 0x49) {
234
                // Regular i and I are in one equivalence class.
235
0
                sa->add(sa->set, 0x69);
236
0
                return;
237
0
            } else if (c == 0x130) {
238
                // Dotted I is in a class with <0069 0307>
239
                // (for canonical equivalence with <0049 0307>).
240
0
                sa->addString(sa->set, iDot, 2);
241
0
                return;
242
0
            }
243
0
        } else if (c == 0x69) {
244
0
            sa->add(sa->set, 0x49);
245
0
            return;
246
0
        } else if (c == 0x131) {
247
            // Dotless i is in a class by itself.
248
0
            return;
249
0
        }
250
251
        /* add all simple case mappings */
252
0
        for(int32_t idx=UCASE_EXC_LOWER; idx<=UCASE_EXC_TITLE; ++idx) {
253
0
            if(HAS_SLOT(excWord, idx)) {
254
0
                pe=pe0;
255
0
                UChar32 mapping;
256
0
                GET_SLOT_VALUE(excWord, idx, pe, mapping);
257
0
                sa->add(sa->set, mapping);
258
0
            }
259
0
        }
260
0
        if(HAS_SLOT(excWord, UCASE_EXC_DELTA)) {
261
0
            pe=pe0;
262
0
            int32_t delta;
263
0
            GET_SLOT_VALUE(excWord, UCASE_EXC_DELTA, pe, delta);
264
0
            sa->add(sa->set, (excWord&UCASE_EXC_DELTA_IS_NEGATIVE)==0 ? c+delta : c-delta);
265
0
        }
266
267
        /* get the closure string pointer & length */
268
0
        const char16_t *closure;
269
0
        int32_t closureLength;
270
0
        if(HAS_SLOT(excWord, UCASE_EXC_CLOSURE)) {
271
0
            pe=pe0;
272
0
            GET_SLOT_VALUE(excWord, UCASE_EXC_CLOSURE, pe, closureLength);
273
0
            closureLength&=UCASE_CLOSURE_MAX_LENGTH; /* higher bits are reserved */
274
0
            closure=(const char16_t *)pe+1; /* behind this slot, unless there are full case mappings */
275
0
        } else {
276
0
            closureLength=0;
277
0
            closure=nullptr;
278
0
        }
279
280
        /* add the full case folding */
281
0
        if(HAS_SLOT(excWord, UCASE_EXC_FULL_MAPPINGS)) {
282
0
            pe=pe0;
283
0
            int32_t fullLength;
284
0
            GET_SLOT_VALUE(excWord, UCASE_EXC_FULL_MAPPINGS, pe, fullLength);
285
286
            /* start of full case mapping strings */
287
0
            ++pe;
288
289
0
            fullLength&=0xffff; /* bits 16 and higher are reserved */
290
291
            /* skip the lowercase result string */
292
0
            pe+=fullLength&UCASE_FULL_LOWER;
293
0
            fullLength>>=4;
294
295
            /* add the full case folding string */
296
0
            int32_t length=fullLength&0xf;
297
0
            if(length!=0) {
298
0
                sa->addString(sa->set, (const char16_t *)pe, length);
299
0
                pe+=length;
300
0
            }
301
302
            /* skip the uppercase and titlecase strings */
303
0
            fullLength>>=4;
304
0
            pe+=fullLength&0xf;
305
0
            fullLength>>=4;
306
0
            pe+=fullLength;
307
308
0
            closure=(const char16_t *)pe; /* behind full case mappings */
309
0
        }
310
311
        /* add each code point in the closure string */
312
0
        for(int32_t idx=0; idx<closureLength;) {
313
0
            UChar32 mapping;
314
0
            U16_NEXT_UNSAFE(closure, idx, mapping);
315
0
            sa->add(sa->set, mapping);
316
0
        }
317
0
    }
318
0
}
319
320
U_CFUNC void U_EXPORT2
321
0
ucase_addSimpleCaseClosure(UChar32 c, const USetAdder *sa) {
322
0
    uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
323
0
    if(!UCASE_HAS_EXCEPTION(props)) {
324
0
        if(UCASE_GET_TYPE(props)!=UCASE_NONE) {
325
            /* add the one simple case mapping, no matter what type it is */
326
0
            int32_t delta=UCASE_GET_DELTA(props);
327
0
            if(delta!=0) {
328
0
                sa->add(sa->set, c+delta);
329
0
            }
330
0
        }
331
0
    } else {
332
        // c has exceptions. Add the mappings relevant for scf=Simple_Case_Folding.
333
0
        const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props);
334
0
        uint16_t excWord=*pe++;
335
0
        const uint16_t *pe0=pe;
336
337
        // Hardcode the case closure of i and its relatives and ignore the
338
        // data file data for these characters, like in ucase_addCaseClosure().
339
0
        if (excWord&UCASE_EXC_CONDITIONAL_FOLD) {
340
            // These characters have Turkic case foldings. Hardcode their closure.
341
0
            if (c == 0x49) {
342
                // Regular i and I are in one equivalence class.
343
0
                sa->add(sa->set, 0x69);
344
0
                return;
345
0
            } else if (c == 0x130) {
346
                // For scf=Simple_Case_Folding, dotted I is in a class by itself.
347
0
                return;
348
0
            }
349
0
        } else if (c == 0x69) {
350
0
            sa->add(sa->set, 0x49);
351
0
            return;
352
0
        } else if (c == 0x131) {
353
            // Dotless i is in a class by itself.
354
0
            return;
355
0
        }
356
357
        // Add all simple case mappings.
358
0
        for(int32_t idx=UCASE_EXC_LOWER; idx<=UCASE_EXC_TITLE; ++idx) {
359
0
            if(HAS_SLOT(excWord, idx)) {
360
0
                pe=pe0;
361
0
                UChar32 mapping;
362
0
                GET_SLOT_VALUE(excWord, idx, pe, mapping);
363
0
                sa->add(sa->set, mapping);
364
0
            }
365
0
        }
366
0
        if(HAS_SLOT(excWord, UCASE_EXC_DELTA)) {
367
0
            pe=pe0;
368
0
            int32_t delta;
369
0
            GET_SLOT_VALUE(excWord, UCASE_EXC_DELTA, pe, delta);
370
0
            UChar32 mapping = (excWord&UCASE_EXC_DELTA_IS_NEGATIVE)==0 ? c+delta : c-delta;
371
0
            sa->add(sa->set, mapping);
372
0
        }
373
374
        /* get the closure string pointer & length */
375
0
        const char16_t *closure;
376
0
        int32_t closureLength;
377
0
        if(HAS_SLOT(excWord, UCASE_EXC_CLOSURE)) {
378
0
            pe=pe0;
379
0
            GET_SLOT_VALUE(excWord, UCASE_EXC_CLOSURE, pe, closureLength);
380
0
            closureLength&=UCASE_CLOSURE_MAX_LENGTH; /* higher bits are reserved */
381
0
            closure=(const char16_t *)pe+1; /* behind this slot, unless there are full case mappings */
382
0
        } else {
383
0
            closureLength=0;
384
0
            closure=nullptr;
385
0
        }
386
387
        // Skip the full case mappings.
388
0
        if(closureLength > 0 && HAS_SLOT(excWord, UCASE_EXC_FULL_MAPPINGS)) {
389
0
            pe=pe0;
390
0
            int32_t fullLength;
391
0
            GET_SLOT_VALUE(excWord, UCASE_EXC_FULL_MAPPINGS, pe, fullLength);
392
393
            /* start of full case mapping strings */
394
0
            ++pe;
395
396
0
            fullLength&=0xffff; /* bits 16 and higher are reserved */
397
398
            // Skip all 4 full case mappings.
399
0
            pe+=fullLength&UCASE_FULL_LOWER;
400
0
            fullLength>>=4;
401
0
            pe+=fullLength&0xf;
402
0
            fullLength>>=4;
403
0
            pe+=fullLength&0xf;
404
0
            fullLength>>=4;
405
0
            pe+=fullLength;
406
407
0
            closure=(const char16_t *)pe; /* behind full case mappings */
408
0
        }
409
410
        // Add each code point in the closure string whose scf maps back to c.
411
0
        for(int32_t idx=0; idx<closureLength;) {
412
0
            UChar32 mapping;
413
0
            U16_NEXT_UNSAFE(closure, idx, mapping);
414
0
            sa->add(sa->set, mapping);
415
0
        }
416
0
    }
417
0
}
418
419
/*
420
 * compare s, which has a length, with t, which has a maximum length or is NUL-terminated
421
 * must be length>0 and max>0 and length<=max
422
 */
423
static inline int32_t
424
0
strcmpMax(const char16_t *s, int32_t length, const char16_t *t, int32_t max) {
425
0
    int32_t c1, c2;
426
427
0
    max-=length; /* we require length<=max, so no need to decrement max in the loop */
428
0
    do {
429
0
        c1=*s++;
430
0
        c2=*t++;
431
0
        if(c2==0) {
432
0
            return 1; /* reached the end of t but not of s */
433
0
        }
434
0
        c1-=c2;
435
0
        if(c1!=0) {
436
0
            return c1; /* return difference result */
437
0
        }
438
0
    } while(--length>0);
439
    /* ends with length==0 */
440
441
0
    if(max==0 || *t==0) {
442
0
        return 0; /* equal to length of both strings */
443
0
    } else {
444
0
        return -max; /* return length difference */
445
0
    }
446
0
}
447
448
U_CFUNC UBool U_EXPORT2
449
0
ucase_addStringCaseClosure(const char16_t *s, int32_t length, const USetAdder *sa) {
450
0
    int32_t i, start, limit, result, unfoldRows, unfoldRowWidth, unfoldStringWidth;
451
452
0
    if(ucase_props_singleton.unfold==nullptr || s==nullptr) {
453
0
        return false; /* no reverse case folding data, or no string */
454
0
    }
455
0
    if(length<=1) {
456
        /* the string is too short to find any match */
457
        /*
458
         * more precise would be:
459
         * if(!u_strHasMoreChar32Than(s, length, 1))
460
         * but this does not make much practical difference because
461
         * a single supplementary code point would just not be found
462
         */
463
0
        return false;
464
0
    }
465
466
0
    const uint16_t *unfold=ucase_props_singleton.unfold;
467
0
    unfoldRows=unfold[UCASE_UNFOLD_ROWS];
468
0
    unfoldRowWidth=unfold[UCASE_UNFOLD_ROW_WIDTH];
469
0
    unfoldStringWidth=unfold[UCASE_UNFOLD_STRING_WIDTH];
470
0
    unfold+=unfoldRowWidth;
471
472
0
    if(length>unfoldStringWidth) {
473
        /* the string is too long to find any match */
474
0
        return false;
475
0
    }
476
477
    /* do a binary search for the string */
478
0
    start=0;
479
0
    limit=unfoldRows;
480
0
    while(start<limit) {
481
0
        i=(start+limit)/2;
482
0
        const char16_t *p=reinterpret_cast<const char16_t *>(unfold+(i*unfoldRowWidth));
483
0
        result=strcmpMax(s, length, p, unfoldStringWidth);
484
485
0
        if(result==0) {
486
            /* found the string: add each code point, and its case closure */
487
0
            UChar32 c;
488
489
0
            for(i=unfoldStringWidth; i<unfoldRowWidth && p[i]!=0;) {
490
0
                U16_NEXT_UNSAFE(p, i, c);
491
0
                sa->add(sa->set, c);
492
0
                ucase_addCaseClosure(c, sa);
493
0
            }
494
0
            return true;
495
0
        } else if(result<0) {
496
0
            limit=i;
497
0
        } else /* result>0 */ {
498
0
            start=i+1;
499
0
        }
500
0
    }
501
502
0
    return false; /* string not found */
503
0
}
504
505
U_NAMESPACE_BEGIN
506
507
FullCaseFoldingIterator::FullCaseFoldingIterator()
508
0
        : unfold(reinterpret_cast<const char16_t *>(ucase_props_singleton.unfold)),
509
0
          unfoldRows(unfold[UCASE_UNFOLD_ROWS]),
510
0
          unfoldRowWidth(unfold[UCASE_UNFOLD_ROW_WIDTH]),
511
0
          unfoldStringWidth(unfold[UCASE_UNFOLD_STRING_WIDTH]),
512
0
          currentRow(0),
513
0
          rowCpIndex(unfoldStringWidth) {
514
0
    unfold+=unfoldRowWidth;
515
0
}
516
517
UChar32
518
0
FullCaseFoldingIterator::next(UnicodeString &full) {
519
    // Advance past the last-delivered code point.
520
0
    const char16_t *p=unfold+(currentRow*unfoldRowWidth);
521
0
    if(rowCpIndex>=unfoldRowWidth || p[rowCpIndex]==0) {
522
0
        ++currentRow;
523
0
        p+=unfoldRowWidth;
524
0
        rowCpIndex=unfoldStringWidth;
525
0
    }
526
0
    if(currentRow>=unfoldRows) { return U_SENTINEL; }
527
    // Set "full" to the NUL-terminated string in the first unfold column.
528
0
    int32_t length=unfoldStringWidth;
529
0
    while(length>0 && p[length-1]==0) { --length; }
530
0
    full.setTo(false, p, length);
531
    // Return the code point.
532
0
    UChar32 c;
533
0
    U16_NEXT_UNSAFE(p, rowCpIndex, c);
534
0
    return c;
535
0
}
536
537
namespace LatinCase {
538
539
const int8_t TO_LOWER_NORMAL[LIMIT] = {
540
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
541
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
542
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
543
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
544
545
    0, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
546
    32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 0, 0, 0, 0, 0,
547
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
548
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
549
550
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
551
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
552
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
553
    0, 0, 0, 0, 0, EXC, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
554
555
    32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
556
    32, 32, 32, 32, 32, 32, 32, 0, 32, 32, 32, 32, 32, 32, 32, EXC,
557
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
558
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
559
560
    1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
561
    1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
562
    1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
563
    EXC, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1,
564
565
    0, 1, 0, 1, 0, 1, 0, 1, 0, EXC, 1, 0, 1, 0, 1, 0,
566
    1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
567
    1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
568
    1, 0, 1, 0, 1, 0, 1, 0, -121, 1, 0, 1, 0, 1, 0, EXC
569
};
570
571
const int8_t TO_LOWER_TR_LT[LIMIT] = {
572
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
573
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
574
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
575
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
576
577
    0, 32, 32, 32, 32, 32, 32, 32, 32, EXC, EXC, 32, 32, 32, 32, 32,
578
    32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 0, 0, 0, 0, 0,
579
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
580
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
581
582
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
583
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
584
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
585
    0, 0, 0, 0, 0, EXC, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
586
587
    32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, EXC, EXC, 32, 32,
588
    32, 32, 32, 32, 32, 32, 32, 0, 32, 32, 32, 32, 32, 32, 32, EXC,
589
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
590
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
591
592
    1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
593
    1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
594
    1, 0, 1, 0, 1, 0, 1, 0, EXC, 0, 1, 0, 1, 0, EXC, 0,
595
    EXC, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1,
596
597
    0, 1, 0, 1, 0, 1, 0, 1, 0, EXC, 1, 0, 1, 0, 1, 0,
598
    1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
599
    1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
600
    1, 0, 1, 0, 1, 0, 1, 0, -121, 1, 0, 1, 0, 1, 0, EXC
601
};
602
603
const int8_t TO_UPPER_NORMAL[LIMIT] = {
604
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
605
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
606
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
607
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
608
609
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
610
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
611
    0, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32,
612
    -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, 0, 0, 0, 0, 0,
613
614
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
615
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
616
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
617
    0, 0, 0, 0, 0, EXC, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
618
619
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
620
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, EXC,
621
    -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32,
622
    -32, -32, -32, -32, -32, -32, -32, 0, -32, -32, -32, -32, -32, -32, -32, 121,
623
624
    0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1,
625
    0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1,
626
    0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1,
627
    0, EXC, 0, -1, 0, -1, 0, -1, 0, 0, -1, 0, -1, 0, -1, 0,
628
629
    -1, 0, -1, 0, -1, 0, -1, 0, -1, EXC, 0, -1, 0, -1, 0, -1,
630
    0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1,
631
    0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1,
632
    0, -1, 0, -1, 0, -1, 0, -1, 0, 0, -1, 0, -1, 0, -1, EXC
633
};
634
635
const int8_t TO_UPPER_TR[LIMIT] = {
636
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
637
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
638
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
639
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
640
641
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
642
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
643
    0, -32, -32, -32, -32, -32, -32, -32, -32, EXC, -32, -32, -32, -32, -32, -32,
644
    -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, 0, 0, 0, 0, 0,
645
646
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
647
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
648
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
649
    0, 0, 0, 0, 0, EXC, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
650
651
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
652
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, EXC,
653
    -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32, -32,
654
    -32, -32, -32, -32, -32, -32, -32, 0, -32, -32, -32, -32, -32, -32, -32, 121,
655
656
    0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1,
657
    0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1,
658
    0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1,
659
    0, EXC, 0, -1, 0, -1, 0, -1, 0, 0, -1, 0, -1, 0, -1, 0,
660
661
    -1, 0, -1, 0, -1, 0, -1, 0, -1, EXC, 0, -1, 0, -1, 0, -1,
662
    0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1,
663
    0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1,
664
    0, -1, 0, -1, 0, -1, 0, -1, 0, 0, -1, 0, -1, 0, -1, EXC
665
};
666
667
}  // namespace LatinCase
668
669
U_NAMESPACE_END
670
671
/** @return UCASE_NONE, UCASE_LOWER, UCASE_UPPER, UCASE_TITLE */
672
U_CAPI int32_t U_EXPORT2
673
9.82k
ucase_getType(UChar32 c) {
674
9.82k
    uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
675
9.82k
    return UCASE_GET_TYPE(props);
676
9.82k
}
677
678
/** @return same as ucase_getType() and set bit 2 if c is case-ignorable */
679
U_CAPI int32_t U_EXPORT2
680
3.29k
ucase_getTypeOrIgnorable(UChar32 c) {
681
3.29k
    uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
682
3.29k
    return UCASE_GET_TYPE_AND_IGNORABLE(props);
683
3.29k
}
684
685
/** @return UCASE_NO_DOT, UCASE_SOFT_DOTTED, UCASE_ABOVE, UCASE_OTHER_ACCENT */
686
static inline int32_t
687
3.28k
getDotType(UChar32 c) {
688
3.28k
    uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
689
3.28k
    if(!UCASE_HAS_EXCEPTION(props)) {
690
2.88k
        return props&UCASE_DOT_MASK;
691
2.88k
    } else {
692
398
        const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props);
693
398
        return (*pe>>UCASE_EXC_DOT_SHIFT)&UCASE_DOT_MASK;
694
398
    }
695
3.28k
}
696
697
U_CAPI UBool U_EXPORT2
698
3.28k
ucase_isSoftDotted(UChar32 c) {
699
3.28k
    return getDotType(c)==UCASE_SOFT_DOTTED;
700
3.28k
}
701
702
U_CAPI UBool U_EXPORT2
703
3.28k
ucase_isCaseSensitive(UChar32 c) {
704
3.28k
    uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
705
3.28k
    if(!UCASE_HAS_EXCEPTION(props)) {
706
2.89k
        return (props&UCASE_SENSITIVE)!=0;
707
2.89k
    } else {
708
396
        const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props);
709
396
        return (*pe&UCASE_EXC_SENSITIVE)!=0;
710
396
    }
711
3.28k
}
712
713
/* string casing ------------------------------------------------------------ */
714
715
/*
716
 * These internal functions form the core of string case mappings.
717
 * They map single code points to result code points or strings and take
718
 * all necessary conditions (context, locale ID, options) into account.
719
 *
720
 * They do not iterate over the source or write to the destination
721
 * so that the same functions are useful for non-standard string storage,
722
 * such as in a Replaceable (for Transliterator) or UTF-8/32 strings etc.
723
 * For the same reason, the "surrounding text" context is passed in as a
724
 * UCaseContextIterator which does not make any assumptions about
725
 * the underlying storage.
726
 *
727
 * This section contains helper functions that check for conditions
728
 * in the input text surrounding the current code point
729
 * according to SpecialCasing.txt.
730
 *
731
 * Each helper function gets the index
732
 * - after the current code point if it looks at following text
733
 * - before the current code point if it looks at preceding text
734
 *
735
 * Unicode 3.2 UAX 21 "Case Mappings" defines the conditions as follows:
736
 *
737
 * Final_Sigma
738
 *   C is preceded by a sequence consisting of
739
 *     a cased letter and a case-ignorable sequence,
740
 *   and C is not followed by a sequence consisting of
741
 *     an ignorable sequence and then a cased letter.
742
 *
743
 * More_Above
744
 *   C is followed by one or more characters of combining class 230 (ABOVE)
745
 *   in the combining character sequence.
746
 *
747
 * After_Soft_Dotted
748
 *   The last preceding character with combining class of zero before C
749
 *   was Soft_Dotted,
750
 *   and there is no intervening combining character class 230 (ABOVE).
751
 *
752
 * Before_Dot
753
 *   C is followed by combining dot above (U+0307).
754
 *   Any sequence of characters with a combining class that is neither 0 nor 230
755
 *   may intervene between the current character and the combining dot above.
756
 *
757
 * The erratum from 2002-10-31 adds the condition
758
 *
759
 * After_I
760
 *   The last preceding base character was an uppercase I, and there is no
761
 *   intervening combining character class 230 (ABOVE).
762
 *
763
 *   (See Jitterbug 2344 and the comments on After_I below.)
764
 *
765
 * Helper definitions in Unicode 3.2 UAX 21:
766
 *
767
 * D1. A character C is defined to be cased
768
 *     if it meets any of the following criteria:
769
 *
770
 *   - The general category of C is Titlecase Letter (Lt)
771
 *   - In [CoreProps], C has one of the properties Uppercase, or Lowercase
772
 *   - Given D = NFD(C), then it is not the case that:
773
 *     D = UCD_lower(D) = UCD_upper(D) = UCD_title(D)
774
 *     (This third criterion does not add any characters to the list
775
 *      for Unicode 3.2. Ignored.)
776
 *
777
 * D2. A character C is defined to be case-ignorable
778
 *     if it meets either of the following criteria:
779
 *
780
 *   - The general category of C is
781
 *     Nonspacing Mark (Mn), or Enclosing Mark (Me), or Format Control (Cf), or
782
 *     Letter Modifier (Lm), or Symbol Modifier (Sk)
783
 *   - C is one of the following characters
784
 *     U+0027 APOSTROPHE
785
 *     U+00AD SOFT HYPHEN (SHY)
786
 *     U+2019 RIGHT SINGLE QUOTATION MARK
787
 *            (the preferred character for apostrophe)
788
 *
789
 * D3. A case-ignorable sequence is a sequence of
790
 *     zero or more case-ignorable characters.
791
 */
792
793
0
#define is_d(c) ((c)=='d' || (c)=='D')
794
0
#define is_e(c) ((c)=='e' || (c)=='E')
795
0
#define is_i(c) ((c)=='i' || (c)=='I')
796
0
#define is_l(c) ((c)=='l' || (c)=='L')
797
0
#define is_r(c) ((c)=='r' || (c)=='R')
798
0
#define is_t(c) ((c)=='t' || (c)=='T')
799
0
#define is_u(c) ((c)=='u' || (c)=='U')
800
0
#define is_y(c) ((c)=='y' || (c)=='Y')
801
0
#define is_z(c) ((c)=='z' || (c)=='Z')
802
803
/* separator? */
804
0
#define is_sep(c) ((c)=='_' || (c)=='-' || (c)==0)
805
806
/**
807
 * Requires non-nullptr locale ID but otherwise does the equivalent of
808
 * checking for language codes as if uloc_getLanguage() were called:
809
 * Accepts both 2- and 3-letter codes and accepts case variants.
810
 */
811
U_CFUNC int32_t
812
0
ucase_getCaseLocale(const char *locale) {
813
    /*
814
     * This function used to use uloc_getLanguage(), but the current code
815
     * removes the dependency of this low-level code on uloc implementation code
816
     * and is faster because not the whole locale ID has to be
817
     * examined and copied/transformed.
818
     *
819
     * Because this code does not want to depend on uloc, the caller must
820
     * pass in a non-nullptr locale, i.e., may need to call uloc_getDefault().
821
     */
822
0
    char c=*locale++;
823
    // Fastpath for English "en" which is often used for default (=root locale) case mappings,
824
    // and for Chinese "zh": Very common but no special case mapping behavior.
825
    // Then check lowercase vs. uppercase to reduce the number of comparisons
826
    // for other locales without special behavior.
827
0
    if(c=='e') {
828
        /* el or ell? */
829
0
        c=*locale++;
830
0
        if(is_l(c)) {
831
0
            c=*locale++;
832
0
            if(is_l(c)) {
833
0
                c=*locale;
834
0
            }
835
0
            if(is_sep(c)) {
836
0
                return UCASE_LOC_GREEK;
837
0
            }
838
0
        }
839
        // en, es, ... -> root
840
0
    } else if(c=='z') {
841
0
        return UCASE_LOC_ROOT;
842
0
#if U_CHARSET_FAMILY==U_ASCII_FAMILY
843
0
    } else if(c>='a') {  // ASCII a-z = 0x61..0x7a, after A-Z
844
#elif U_CHARSET_FAMILY==U_EBCDIC_FAMILY
845
    } else if(c<='z') {  // EBCDIC a-z = 0x81..0xa9 with two gaps, before A-Z
846
#else
847
#   error Unknown charset family!
848
#endif
849
        // lowercase c
850
0
        if(c=='t') {
851
            /* tr or tur? */
852
0
            c=*locale++;
853
0
            if(is_u(c)) {
854
0
                c=*locale++;
855
0
            }
856
0
            if(is_r(c)) {
857
0
                c=*locale;
858
0
                if(is_sep(c)) {
859
0
                    return UCASE_LOC_TURKISH;
860
0
                }
861
0
            }
862
0
        } else if(c=='a') {
863
            /* az or aze? */
864
0
            c=*locale++;
865
0
            if(is_z(c)) {
866
0
                c=*locale++;
867
0
                if(is_e(c)) {
868
0
                    c=*locale;
869
0
                }
870
0
                if(is_sep(c)) {
871
0
                    return UCASE_LOC_TURKISH;
872
0
                }
873
0
            }
874
0
        } else if(c=='l') {
875
            /* lt or lit? */
876
0
            c=*locale++;
877
0
            if(is_i(c)) {
878
0
                c=*locale++;
879
0
            }
880
0
            if(is_t(c)) {
881
0
                c=*locale;
882
0
                if(is_sep(c)) {
883
0
                    return UCASE_LOC_LITHUANIAN;
884
0
                }
885
0
            }
886
0
        } else if(c=='n') {
887
            /* nl or nld? */
888
0
            c=*locale++;
889
0
            if(is_l(c)) {
890
0
                c=*locale++;
891
0
                if(is_d(c)) {
892
0
                    c=*locale;
893
0
                }
894
0
                if(is_sep(c)) {
895
0
                    return UCASE_LOC_DUTCH;
896
0
                }
897
0
            }
898
0
        } else if(c=='h') {
899
            /* hy or hye? *not* hyw */
900
0
            c=*locale++;
901
0
            if(is_y(c)) {
902
0
                c=*locale++;
903
0
                if(is_e(c)) {
904
0
                    c=*locale;
905
0
                }
906
0
                if(is_sep(c)) {
907
0
                    return UCASE_LOC_ARMENIAN;
908
0
                }
909
0
            }
910
0
        }
911
0
    } else {
912
        // uppercase c
913
        // Same code as for lowercase c but also check for 'E'.
914
0
        if(c=='T') {
915
            /* tr or tur? */
916
0
            c=*locale++;
917
0
            if(is_u(c)) {
918
0
                c=*locale++;
919
0
            }
920
0
            if(is_r(c)) {
921
0
                c=*locale;
922
0
                if(is_sep(c)) {
923
0
                    return UCASE_LOC_TURKISH;
924
0
                }
925
0
            }
926
0
        } else if(c=='A') {
927
            /* az or aze? */
928
0
            c=*locale++;
929
0
            if(is_z(c)) {
930
0
                c=*locale++;
931
0
                if(is_e(c)) {
932
0
                    c=*locale;
933
0
                }
934
0
                if(is_sep(c)) {
935
0
                    return UCASE_LOC_TURKISH;
936
0
                }
937
0
            }
938
0
        } else if(c=='L') {
939
            /* lt or lit? */
940
0
            c=*locale++;
941
0
            if(is_i(c)) {
942
0
                c=*locale++;
943
0
            }
944
0
            if(is_t(c)) {
945
0
                c=*locale;
946
0
                if(is_sep(c)) {
947
0
                    return UCASE_LOC_LITHUANIAN;
948
0
                }
949
0
            }
950
0
        } else if(c=='E') {
951
            /* el or ell? */
952
0
            c=*locale++;
953
0
            if(is_l(c)) {
954
0
                c=*locale++;
955
0
                if(is_l(c)) {
956
0
                    c=*locale;
957
0
                }
958
0
                if(is_sep(c)) {
959
0
                    return UCASE_LOC_GREEK;
960
0
                }
961
0
            }
962
0
        } else if(c=='N') {
963
            /* nl or nld? */
964
0
            c=*locale++;
965
0
            if(is_l(c)) {
966
0
                c=*locale++;
967
0
                if(is_d(c)) {
968
0
                    c=*locale;
969
0
                }
970
0
                if(is_sep(c)) {
971
0
                    return UCASE_LOC_DUTCH;
972
0
                }
973
0
            }
974
0
        } else if(c=='H') {
975
            /* hy or hye? *not* hyw */
976
0
            c=*locale++;
977
0
            if(is_y(c)) {
978
0
                c=*locale++;
979
0
                if(is_e(c)) {
980
0
                    c=*locale;
981
0
                }
982
0
                if(is_sep(c)) {
983
0
                    return UCASE_LOC_ARMENIAN;
984
0
                }
985
0
            }
986
0
        }
987
0
    }
988
0
    return UCASE_LOC_ROOT;
989
0
}
990
991
/*
992
 * Is followed by
993
 *   {case-ignorable}* cased
994
 * ?
995
 * (dir determines looking forward/backward)
996
 * If a character is case-ignorable, it is skipped regardless of whether
997
 * it is also cased or not.
998
 */
999
static UBool
1000
14
isFollowedByCasedLetter(UCaseContextIterator *iter, void *context, int8_t dir) {
1001
14
    UChar32 c;
1002
1003
14
    if(iter==nullptr) {
1004
14
        return false;
1005
14
    }
1006
1007
0
    for(/* dir!=0 sets direction */; (c=iter(context, dir))>=0; dir=0) {
1008
0
        int32_t type=ucase_getTypeOrIgnorable(c);
1009
0
        if(type&4) {
1010
            /* case-ignorable, continue with the loop */
1011
0
        } else if(type!=UCASE_NONE) {
1012
0
            return true; /* followed by cased letter */
1013
0
        } else {
1014
0
            return false; /* uncased and not case-ignorable */
1015
0
        }
1016
0
    }
1017
1018
0
    return false; /* not followed by cased letter */
1019
0
}
1020
1021
/* Is preceded by Soft_Dotted character with no intervening cc=230 ? */
1022
static UBool
1023
0
isPrecededBySoftDotted(UCaseContextIterator *iter, void *context) {
1024
0
    UChar32 c;
1025
0
    int32_t dotType;
1026
0
    int8_t dir;
1027
1028
0
    if(iter==nullptr) {
1029
0
        return false;
1030
0
    }
1031
1032
0
    for(dir=-1; (c=iter(context, dir))>=0; dir=0) {
1033
0
        dotType=getDotType(c);
1034
0
        if(dotType==UCASE_SOFT_DOTTED) {
1035
0
            return true; /* preceded by TYPE_i */
1036
0
        } else if(dotType!=UCASE_OTHER_ACCENT) {
1037
0
            return false; /* preceded by different base character (not TYPE_i), or intervening cc==230 */
1038
0
        }
1039
0
    }
1040
1041
0
    return false; /* not preceded by TYPE_i */
1042
0
}
1043
1044
/*
1045
 * See Jitterbug 2344:
1046
 * The condition After_I for Turkic-lowercasing of U+0307 combining dot above
1047
 * is checked in ICU 2.0, 2.1, 2.6 but was not in 2.2 & 2.4 because
1048
 * we made those releases compatible with Unicode 3.2 which had not fixed
1049
 * a related bug in SpecialCasing.txt.
1050
 *
1051
 * From the Jitterbug 2344 text:
1052
 * ... this bug is listed as a Unicode erratum
1053
 * from 2002-10-31 at http://www.unicode.org/uni2errata/UnicodeErrata.html
1054
 * <quote>
1055
 * There are two errors in SpecialCasing.txt.
1056
 * 1. Missing semicolons on two lines. ... [irrelevant for ICU]
1057
 * 2. An incorrect context definition. Correct as follows:
1058
 * < 0307; ; 0307; 0307; tr After_Soft_Dotted; # COMBINING DOT ABOVE
1059
 * < 0307; ; 0307; 0307; az After_Soft_Dotted; # COMBINING DOT ABOVE
1060
 * ---
1061
 * > 0307; ; 0307; 0307; tr After_I; # COMBINING DOT ABOVE
1062
 * > 0307; ; 0307; 0307; az After_I; # COMBINING DOT ABOVE
1063
 * where the context After_I is defined as:
1064
 * The last preceding base character was an uppercase I, and there is no
1065
 * intervening combining character class 230 (ABOVE).
1066
 * </quote>
1067
 *
1068
 * Note that SpecialCasing.txt even in Unicode 3.2 described the condition as:
1069
 *
1070
 * # When lowercasing, remove dot_above in the sequence I + dot_above, which will turn into i.
1071
 * # This matches the behavior of the canonically equivalent I-dot_above
1072
 *
1073
 * See also the description in this place in older versions of uchar.c (revision 1.100).
1074
 *
1075
 * Markus W. Scherer 2003-feb-15
1076
 */
1077
1078
/* Is preceded by base character 'I' with no intervening cc=230 ? */
1079
static UBool
1080
0
isPrecededBy_I(UCaseContextIterator *iter, void *context) {
1081
0
    UChar32 c;
1082
0
    int32_t dotType;
1083
0
    int8_t dir;
1084
1085
0
    if(iter==nullptr) {
1086
0
        return false;
1087
0
    }
1088
1089
0
    for(dir=-1; (c=iter(context, dir))>=0; dir=0) {
1090
0
        if(c==0x49) {
1091
0
            return true; /* preceded by I */
1092
0
        }
1093
0
        dotType=getDotType(c);
1094
0
        if(dotType!=UCASE_OTHER_ACCENT) {
1095
0
            return false; /* preceded by different base character (not I), or intervening cc==230 */
1096
0
        }
1097
0
    }
1098
1099
0
    return false; /* not preceded by I */
1100
0
}
1101
1102
/* Is followed by one or more cc==230 ? */
1103
static UBool
1104
0
isFollowedByMoreAbove(UCaseContextIterator *iter, void *context) {
1105
0
    UChar32 c;
1106
0
    int32_t dotType;
1107
0
    int8_t dir;
1108
1109
0
    if(iter==nullptr) {
1110
0
        return false;
1111
0
    }
1112
1113
0
    for(dir=1; (c=iter(context, dir))>=0; dir=0) {
1114
0
        dotType=getDotType(c);
1115
0
        if(dotType==UCASE_ABOVE) {
1116
0
            return true; /* at least one cc==230 following */
1117
0
        } else if(dotType!=UCASE_OTHER_ACCENT) {
1118
0
            return false; /* next base character, no more cc==230 following */
1119
0
        }
1120
0
    }
1121
1122
0
    return false; /* no more cc==230 following */
1123
0
}
1124
1125
/* Is followed by a dot above (without cc==230 in between) ? */
1126
static UBool
1127
0
isFollowedByDotAbove(UCaseContextIterator *iter, void *context) {
1128
0
    UChar32 c;
1129
0
    int32_t dotType;
1130
0
    int8_t dir;
1131
1132
0
    if(iter==nullptr) {
1133
0
        return false;
1134
0
    }
1135
1136
0
    for(dir=1; (c=iter(context, dir))>=0; dir=0) {
1137
0
        if(c==0x307) {
1138
0
            return true;
1139
0
        }
1140
0
        dotType=getDotType(c);
1141
0
        if(dotType!=UCASE_OTHER_ACCENT) {
1142
0
            return false; /* next base character or cc==230 in between */
1143
0
        }
1144
0
    }
1145
1146
0
    return false; /* no dot above following */
1147
0
}
1148
1149
U_CAPI int32_t U_EXPORT2
1150
ucase_toFullLower(UChar32 c,
1151
                  UCaseContextIterator *iter, void *context,
1152
                  const char16_t **pString,
1153
6.51k
                  int32_t loc) {
1154
    // The sign of the result has meaning, input must be non-negative so that it can be returned as is.
1155
6.51k
    U_ASSERT(c >= 0);
1156
6.51k
    UChar32 result=c;
1157
    // Reset the output pointer in case it was uninitialized.
1158
6.51k
    *pString=nullptr;
1159
6.51k
    uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
1160
6.51k
    if(!UCASE_HAS_EXCEPTION(props)) {
1161
5.68k
        if(UCASE_IS_UPPER_OR_TITLE(props)) {
1162
1.37k
            result=c+UCASE_GET_DELTA(props);
1163
1.37k
        }
1164
5.68k
    } else {
1165
838
        const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props), *pe2;
1166
838
        uint16_t excWord=*pe++;
1167
838
        int32_t full;
1168
1169
838
        pe2=pe;
1170
1171
838
        if(excWord&UCASE_EXC_CONDITIONAL_SPECIAL) {
1172
            /* use hardcoded conditions and mappings */
1173
1174
            /*
1175
             * Test for conditional mappings first
1176
             *   (otherwise the unconditional default mappings are always taken),
1177
             * then test for characters that have unconditional mappings in SpecialCasing.txt,
1178
             * then get the UnicodeData.txt mappings.
1179
             */
1180
45
            if( loc==UCASE_LOC_LITHUANIAN &&
1181
                    /* base characters, find accents above */
1182
45
                    (((c==0x49 || c==0x4a || c==0x12e) &&
1183
0
                        isFollowedByMoreAbove(iter, context)) ||
1184
                    /* precomposed with accent above, no need to find one */
1185
0
                    (c==0xcc || c==0xcd || c==0x128))
1186
45
            ) {
1187
                /*
1188
                    # Lithuanian
1189
1190
                    # Lithuanian retains the dot in a lowercase i when followed by accents.
1191
1192
                    # Introduce an explicit dot above when lowercasing capital I's and J's
1193
                    # whenever there are more accents above.
1194
                    # (of the accents used in Lithuanian: grave, acute, tilde above, and ogonek)
1195
1196
                    0049; 0069 0307; 0049; 0049; lt More_Above; # LATIN CAPITAL LETTER I
1197
                    004A; 006A 0307; 004A; 004A; lt More_Above; # LATIN CAPITAL LETTER J
1198
                    012E; 012F 0307; 012E; 012E; lt More_Above; # LATIN CAPITAL LETTER I WITH OGONEK
1199
                    00CC; 0069 0307 0300; 00CC; 00CC; lt; # LATIN CAPITAL LETTER I WITH GRAVE
1200
                    00CD; 0069 0307 0301; 00CD; 00CD; lt; # LATIN CAPITAL LETTER I WITH ACUTE
1201
                    0128; 0069 0307 0303; 0128; 0128; lt; # LATIN CAPITAL LETTER I WITH TILDE
1202
                 */
1203
0
                switch(c) {
1204
0
                case 0x49:  /* LATIN CAPITAL LETTER I */
1205
0
                    *pString=iDot;
1206
0
                    return 2;
1207
0
                case 0x4a:  /* LATIN CAPITAL LETTER J */
1208
0
                    *pString=jDot;
1209
0
                    return 2;
1210
0
                case 0x12e: /* LATIN CAPITAL LETTER I WITH OGONEK */
1211
0
                    *pString=iOgonekDot;
1212
0
                    return 2;
1213
0
                case 0xcc:  /* LATIN CAPITAL LETTER I WITH GRAVE */
1214
0
                    *pString=iDotGrave;
1215
0
                    return 3;
1216
0
                case 0xcd:  /* LATIN CAPITAL LETTER I WITH ACUTE */
1217
0
                    *pString=iDotAcute;
1218
0
                    return 3;
1219
0
                case 0x128: /* LATIN CAPITAL LETTER I WITH TILDE */
1220
0
                    *pString=iDotTilde;
1221
0
                    return 3;
1222
0
                default:
1223
0
                    return 0; /* will not occur */
1224
0
                }
1225
            /* # Turkish and Azeri */
1226
45
            } else if(loc==UCASE_LOC_TURKISH && c==0x130) {
1227
                /*
1228
                    # I and i-dotless; I-dot and i are case pairs in Turkish and Azeri
1229
                    # The following rules handle those cases.
1230
1231
                    0130; 0069; 0130; 0130; tr # LATIN CAPITAL LETTER I WITH DOT ABOVE
1232
                    0130; 0069; 0130; 0130; az # LATIN CAPITAL LETTER I WITH DOT ABOVE
1233
                 */
1234
0
                return 0x69;
1235
45
            } else if(loc==UCASE_LOC_TURKISH && c==0x307 && isPrecededBy_I(iter, context)) {
1236
                /*
1237
                    # When lowercasing, remove dot_above in the sequence I + dot_above, which will turn into i.
1238
                    # This matches the behavior of the canonically equivalent I-dot_above
1239
1240
                    0307; ; 0307; 0307; tr After_I; # COMBINING DOT ABOVE
1241
                    0307; ; 0307; 0307; az After_I; # COMBINING DOT ABOVE
1242
                 */
1243
0
                return 0; /* remove the dot (continue without output) */
1244
45
            } else if(loc==UCASE_LOC_TURKISH && c==0x49 && !isFollowedByDotAbove(iter, context)) {
1245
                /*
1246
                    # When lowercasing, unless an I is before a dot_above, it turns into a dotless i.
1247
1248
                    0049; 0131; 0049; 0049; tr Not_Before_Dot; # LATIN CAPITAL LETTER I
1249
                    0049; 0131; 0049; 0049; az Not_Before_Dot; # LATIN CAPITAL LETTER I
1250
                 */
1251
0
                return 0x131;
1252
45
            } else if(c==0x130) {
1253
                /*
1254
                    # Preserve canonical equivalence for I with dot. Turkic is handled below.
1255
1256
                    0130; 0069 0307; 0130; 0130; # LATIN CAPITAL LETTER I WITH DOT ABOVE
1257
                 */
1258
9
                *pString=iDot;
1259
9
                return 2;
1260
36
            } else if(  c==0x3a3 &&
1261
36
                        !isFollowedByCasedLetter(iter, context, 1) &&
1262
36
                        isFollowedByCasedLetter(iter, context, -1) /* -1=preceded */
1263
36
            ) {
1264
                /* greek capital sigma maps depending on surrounding cased letters (see SpecialCasing.txt) */
1265
                /*
1266
                    # Special case for final form of sigma
1267
1268
                    03A3; 03C2; 03A3; 03A3; Final_Sigma; # GREEK CAPITAL LETTER SIGMA
1269
                 */
1270
0
                return 0x3c2; /* greek small final sigma */
1271
36
            } else {
1272
                /* no known conditional special case mapping, use a normal mapping */
1273
36
            }
1274
793
        } else if(HAS_SLOT(excWord, UCASE_EXC_FULL_MAPPINGS)) {
1275
221
            GET_SLOT_VALUE(excWord, UCASE_EXC_FULL_MAPPINGS, pe, full);
1276
221
            full&=UCASE_FULL_LOWER;
1277
221
            if(full!=0) {
1278
                /* set the output pointer to the lowercase mapping */
1279
0
                *pString=reinterpret_cast<const char16_t *>(pe+1);
1280
1281
                /* return the string length */
1282
0
                return full;
1283
0
            }
1284
221
        }
1285
1286
829
        if(HAS_SLOT(excWord, UCASE_EXC_DELTA) && UCASE_IS_UPPER_OR_TITLE(props)) {
1287
156
            int32_t delta;
1288
156
            GET_SLOT_VALUE(excWord, UCASE_EXC_DELTA, pe2, delta);
1289
156
            return (excWord&UCASE_EXC_DELTA_IS_NEGATIVE)==0 ? c+delta : c-delta;
1290
156
        }
1291
673
        if(HAS_SLOT(excWord, UCASE_EXC_LOWER)) {
1292
70
            GET_SLOT_VALUE(excWord, UCASE_EXC_LOWER, pe2, result);
1293
70
        }
1294
673
    }
1295
1296
6.35k
    return (result==c) ? ~result : result;
1297
6.51k
}
1298
1299
/* internal */
1300
static int32_t
1301
toUpperOrTitle(UChar32 c,
1302
               UCaseContextIterator *iter, void *context,
1303
               const char16_t **pString,
1304
               int32_t loc,
1305
10.4k
               UBool upperNotTitle) {
1306
    // The sign of the result has meaning, input must be non-negative so that it can be returned as is.
1307
10.4k
    U_ASSERT(c >= 0);
1308
10.4k
    UChar32 result=c;
1309
    // Reset the output pointer in case it was uninitialized.
1310
10.4k
    *pString=nullptr;
1311
10.4k
    uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
1312
10.4k
    if(!UCASE_HAS_EXCEPTION(props)) {
1313
9.33k
        if(UCASE_GET_TYPE(props)==UCASE_LOWER) {
1314
2.54k
            result=c+UCASE_GET_DELTA(props);
1315
2.54k
        }
1316
9.33k
    } else {
1317
1.13k
        const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props), *pe2;
1318
1.13k
        uint16_t excWord=*pe++;
1319
1.13k
        int32_t full, idx;
1320
1321
1.13k
        pe2=pe;
1322
1323
1.13k
        if(excWord&UCASE_EXC_CONDITIONAL_SPECIAL) {
1324
            /* use hardcoded conditions and mappings */
1325
54
            if(loc==UCASE_LOC_TURKISH && c==0x69) {
1326
                /*
1327
                    # Turkish and Azeri
1328
1329
                    # I and i-dotless; I-dot and i are case pairs in Turkish and Azeri
1330
                    # The following rules handle those cases.
1331
1332
                    # When uppercasing, i turns into a dotted capital I
1333
1334
                    0069; 0069; 0130; 0130; tr; # LATIN SMALL LETTER I
1335
                    0069; 0069; 0130; 0130; az; # LATIN SMALL LETTER I
1336
                */
1337
0
                return 0x130;
1338
54
            } else if(loc==UCASE_LOC_LITHUANIAN && c==0x307 && isPrecededBySoftDotted(iter, context)) {
1339
                /*
1340
                    # Lithuanian
1341
1342
                    # Lithuanian retains the dot in a lowercase i when followed by accents.
1343
1344
                    # Remove DOT ABOVE after "i" with upper or titlecase
1345
1346
                    0307; 0307; ; ; lt After_Soft_Dotted; # COMBINING DOT ABOVE
1347
                 */
1348
0
                return 0; /* remove the dot (continue without output) */
1349
54
            } else if(c==0x0587) {
1350
                // See ICU-13416:
1351
                // և ligature ech-yiwn
1352
                // uppercases to ԵՒ=ech+yiwn by default and in Western Armenian,
1353
                // but to ԵՎ=ech+vew in Eastern Armenian.
1354
13
                if(loc==UCASE_LOC_ARMENIAN) {
1355
0
                    *pString=upperNotTitle ? u"ԵՎ" : u"Եվ";
1356
13
                } else {
1357
13
                    *pString=upperNotTitle ? u"ԵՒ" : u"Եւ";
1358
13
                }
1359
13
                return 2;
1360
41
            } else {
1361
                /* no known conditional special case mapping, use a normal mapping */
1362
41
            }
1363
1.08k
        } else if(HAS_SLOT(excWord, UCASE_EXC_FULL_MAPPINGS)) {
1364
299
            GET_SLOT_VALUE(excWord, UCASE_EXC_FULL_MAPPINGS, pe, full);
1365
1366
            /* start of full case mapping strings */
1367
299
            ++pe;
1368
1369
            /* skip the lowercase and case-folding result strings */
1370
299
            pe+=full&UCASE_FULL_LOWER;
1371
299
            full>>=4;
1372
299
            pe+=full&0xf;
1373
299
            full>>=4;
1374
1375
299
            if(upperNotTitle) {
1376
188
                full&=0xf;
1377
188
            } else {
1378
                /* skip the uppercase result string */
1379
111
                pe+=full&0xf;
1380
111
                full=(full>>4)&0xf;
1381
111
            }
1382
1383
299
            if(full!=0) {
1384
                /* set the output pointer to the result string */
1385
235
                *pString=reinterpret_cast<const char16_t *>(pe);
1386
1387
                /* return the string length */
1388
235
                return full;
1389
235
            }
1390
299
        }
1391
1392
888
        if(HAS_SLOT(excWord, UCASE_EXC_DELTA) && UCASE_GET_TYPE(props)==UCASE_LOWER) {
1393
130
            int32_t delta;
1394
130
            GET_SLOT_VALUE(excWord, UCASE_EXC_DELTA, pe2, delta);
1395
130
            return (excWord&UCASE_EXC_DELTA_IS_NEGATIVE)==0 ? c+delta : c-delta;
1396
130
        }
1397
758
        if(!upperNotTitle && HAS_SLOT(excWord, UCASE_EXC_TITLE)) {
1398
64
            idx=UCASE_EXC_TITLE;
1399
694
        } else if(HAS_SLOT(excWord, UCASE_EXC_UPPER)) {
1400
            /* here, titlecase is same as uppercase */
1401
508
            idx=UCASE_EXC_UPPER;
1402
508
        } else {
1403
186
            return ~c;
1404
186
        }
1405
758
        GET_SLOT_VALUE(excWord, idx, pe2, result);
1406
572
    }
1407
1408
9.90k
    return (result==c) ? ~result : result;
1409
10.4k
}
1410
1411
U_CAPI int32_t U_EXPORT2
1412
ucase_toFullUpper(UChar32 c,
1413
                  UCaseContextIterator *iter, void *context,
1414
                  const char16_t **pString,
1415
5.69k
                  int32_t caseLocale) {
1416
5.69k
    return toUpperOrTitle(c, iter, context, pString, caseLocale, true);
1417
5.69k
}
1418
1419
U_CAPI int32_t U_EXPORT2
1420
ucase_toFullTitle(UChar32 c,
1421
                  UCaseContextIterator *iter, void *context,
1422
                  const char16_t **pString,
1423
4.77k
                  int32_t caseLocale) {
1424
4.77k
    return toUpperOrTitle(c, iter, context, pString, caseLocale, false);
1425
4.77k
}
1426
1427
/* case folding ------------------------------------------------------------- */
1428
1429
/*
1430
 * Case folding is similar to lowercasing.
1431
 * The result may be a simple mapping, i.e., a single code point, or
1432
 * a full mapping, i.e., a string.
1433
 * If the case folding for a code point is the same as its simple (1:1) lowercase mapping,
1434
 * then only the lowercase mapping is stored.
1435
 *
1436
 * Some special cases are hardcoded because their conditions cannot be
1437
 * parsed and processed from CaseFolding.txt.
1438
 *
1439
 * Unicode 3.2 CaseFolding.txt specifies for its status field:
1440
1441
# C: common case folding, common mappings shared by both simple and full mappings.
1442
# F: full case folding, mappings that cause strings to grow in length. Multiple characters are separated by spaces.
1443
# S: simple case folding, mappings to single characters where different from F.
1444
# T: special case for uppercase I and dotted uppercase I
1445
#    - For non-Turkic languages, this mapping is normally not used.
1446
#    - For Turkic languages (tr, az), this mapping can be used instead of the normal mapping for these characters.
1447
#
1448
# Usage:
1449
#  A. To do a simple case folding, use the mappings with status C + S.
1450
#  B. To do a full case folding, use the mappings with status C + F.
1451
#
1452
#    The mappings with status T can be used or omitted depending on the desired case-folding
1453
#    behavior. (The default option is to exclude them.)
1454
1455
 * Unicode 3.2 has 'T' mappings as follows:
1456
1457
0049; T; 0131; # LATIN CAPITAL LETTER I
1458
0130; T; 0069; # LATIN CAPITAL LETTER I WITH DOT ABOVE
1459
1460
 * while the default mappings for these code points are:
1461
1462
0049; C; 0069; # LATIN CAPITAL LETTER I
1463
0130; F; 0069 0307; # LATIN CAPITAL LETTER I WITH DOT ABOVE
1464
1465
 * U+0130 has no simple case folding (simple-case-folds to itself).
1466
 */
1467
1468
/* return the simple case folding mapping for c */
1469
U_CAPI UChar32 U_EXPORT2
1470
0
ucase_fold(UChar32 c, uint32_t options) {
1471
0
    uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
1472
0
    if(!UCASE_HAS_EXCEPTION(props)) {
1473
0
        if(UCASE_IS_UPPER_OR_TITLE(props)) {
1474
0
            c+=UCASE_GET_DELTA(props);
1475
0
        }
1476
0
    } else {
1477
0
        const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props);
1478
0
        uint16_t excWord=*pe++;
1479
0
        int32_t idx;
1480
0
        if(excWord&UCASE_EXC_CONDITIONAL_FOLD) {
1481
            /* special case folding mappings, hardcoded */
1482
0
            if((options&_FOLD_CASE_OPTIONS_MASK)==U_FOLD_CASE_DEFAULT) {
1483
                /* default mappings */
1484
0
                if(c==0x49) {
1485
                    /* 0049; C; 0069; # LATIN CAPITAL LETTER I */
1486
0
                    return 0x69;
1487
0
                } else if(c==0x130) {
1488
                    /* no simple case folding for U+0130 */
1489
0
                    return c;
1490
0
                }
1491
0
            } else {
1492
                /* Turkic mappings */
1493
0
                if(c==0x49) {
1494
                    /* 0049; T; 0131; # LATIN CAPITAL LETTER I */
1495
0
                    return 0x131;
1496
0
                } else if(c==0x130) {
1497
                    /* 0130; T; 0069; # LATIN CAPITAL LETTER I WITH DOT ABOVE */
1498
0
                    return 0x69;
1499
0
                }
1500
0
            }
1501
0
        }
1502
0
        if((excWord&UCASE_EXC_NO_SIMPLE_CASE_FOLDING)!=0) {
1503
0
            return c;
1504
0
        }
1505
0
        if(HAS_SLOT(excWord, UCASE_EXC_DELTA) && UCASE_IS_UPPER_OR_TITLE(props)) {
1506
0
            int32_t delta;
1507
0
            GET_SLOT_VALUE(excWord, UCASE_EXC_DELTA, pe, delta);
1508
0
            return (excWord&UCASE_EXC_DELTA_IS_NEGATIVE)==0 ? c+delta : c-delta;
1509
0
        }
1510
0
        if(HAS_SLOT(excWord, UCASE_EXC_FOLD)) {
1511
0
            idx=UCASE_EXC_FOLD;
1512
0
        } else if(HAS_SLOT(excWord, UCASE_EXC_LOWER)) {
1513
0
            idx=UCASE_EXC_LOWER;
1514
0
        } else {
1515
0
            return c;
1516
0
        }
1517
0
        GET_SLOT_VALUE(excWord, idx, pe, c);
1518
0
    }
1519
0
    return c;
1520
0
}
1521
1522
/*
1523
 * Issue for canonical caseless match (UAX #21):
1524
 * Turkic casefolding (using "T" mappings in CaseFolding.txt) does not preserve
1525
 * canonical equivalence, unlike default-option casefolding.
1526
 * For example, I-grave and I + grave fold to strings that are not canonically
1527
 * equivalent.
1528
 * For more details, see the comment in unorm_compare() in unorm.cpp
1529
 * and the intermediate prototype changes for Jitterbug 2021.
1530
 * (For example, revision 1.104 of uchar.c and 1.4 of CaseFolding.txt.)
1531
 *
1532
 * This did not get fixed because it appears that it is not possible to fix
1533
 * it for uppercase and lowercase characters (I-grave vs. i-grave)
1534
 * together in a way that they still fold to common result strings.
1535
 */
1536
1537
U_CAPI int32_t U_EXPORT2
1538
ucase_toFullFolding(UChar32 c,
1539
                    const char16_t **pString,
1540
4.57k
                    uint32_t options) {
1541
    // The sign of the result has meaning, input must be non-negative so that it can be returned as is.
1542
4.57k
    U_ASSERT(c >= 0);
1543
4.57k
    UChar32 result=c;
1544
    // Reset the output pointer in case it was uninitialized.
1545
4.57k
    *pString=nullptr;
1546
4.57k
    uint16_t props=UTRIE2_GET16(&ucase_props_singleton.trie, c);
1547
4.57k
    if(!UCASE_HAS_EXCEPTION(props)) {
1548
3.98k
        if(UCASE_IS_UPPER_OR_TITLE(props)) {
1549
467
            result=c+UCASE_GET_DELTA(props);
1550
467
        }
1551
3.98k
    } else {
1552
588
        const uint16_t *pe=GET_EXCEPTIONS(&ucase_props_singleton, props), *pe2;
1553
588
        uint16_t excWord=*pe++;
1554
588
        int32_t full, idx;
1555
1556
588
        pe2=pe;
1557
1558
588
        if(excWord&UCASE_EXC_CONDITIONAL_FOLD) {
1559
            /* use hardcoded conditions and mappings */
1560
6
            if((options&_FOLD_CASE_OPTIONS_MASK)==U_FOLD_CASE_DEFAULT) {
1561
                /* default mappings */
1562
6
                if(c==0x49) {
1563
                    /* 0049; C; 0069; # LATIN CAPITAL LETTER I */
1564
6
                    return 0x69;
1565
6
                } else if(c==0x130) {
1566
                    /* 0130; F; 0069 0307; # LATIN CAPITAL LETTER I WITH DOT ABOVE */
1567
0
                    *pString=iDot;
1568
0
                    return 2;
1569
0
                }
1570
6
            } else {
1571
                /* Turkic mappings */
1572
0
                if(c==0x49) {
1573
                    /* 0049; T; 0131; # LATIN CAPITAL LETTER I */
1574
0
                    return 0x131;
1575
0
                } else if(c==0x130) {
1576
                    /* 0130; T; 0069; # LATIN CAPITAL LETTER I WITH DOT ABOVE */
1577
0
                    return 0x69;
1578
0
                }
1579
0
            }
1580
582
        } else if(HAS_SLOT(excWord, UCASE_EXC_FULL_MAPPINGS)) {
1581
23
            GET_SLOT_VALUE(excWord, UCASE_EXC_FULL_MAPPINGS, pe, full);
1582
1583
            /* start of full case mapping strings */
1584
23
            ++pe;
1585
1586
            /* skip the lowercase result string */
1587
23
            pe+=full&UCASE_FULL_LOWER;
1588
23
            full=(full>>4)&0xf;
1589
1590
23
            if(full!=0) {
1591
                /* set the output pointer to the result string */
1592
23
                *pString=reinterpret_cast<const char16_t *>(pe);
1593
1594
                /* return the string length */
1595
23
                return full;
1596
23
            }
1597
23
        }
1598
1599
559
        if((excWord&UCASE_EXC_NO_SIMPLE_CASE_FOLDING)!=0) {
1600
6
            return ~c;
1601
6
        }
1602
553
        if(HAS_SLOT(excWord, UCASE_EXC_DELTA) && UCASE_IS_UPPER_OR_TITLE(props)) {
1603
44
            int32_t delta;
1604
44
            GET_SLOT_VALUE(excWord, UCASE_EXC_DELTA, pe2, delta);
1605
44
            return (excWord&UCASE_EXC_DELTA_IS_NEGATIVE)==0 ? c+delta : c-delta;
1606
44
        }
1607
509
        if(HAS_SLOT(excWord, UCASE_EXC_FOLD)) {
1608
189
            idx=UCASE_EXC_FOLD;
1609
320
        } else if(HAS_SLOT(excWord, UCASE_EXC_LOWER)) {
1610
90
            idx=UCASE_EXC_LOWER;
1611
230
        } else {
1612
230
            return ~c;
1613
230
        }
1614
509
        GET_SLOT_VALUE(excWord, idx, pe2, result);
1615
279
    }
1616
1617
4.26k
    return (result==c) ? ~result : result;
1618
4.57k
}
1619
1620
/* case mapping properties API ---------------------------------------------- */
1621
1622
/* public API (see uchar.h) */
1623
1624
U_CAPI UBool U_EXPORT2
1625
0
u_isULowercase(UChar32 c) {
1626
0
    return UCASE_LOWER==ucase_getType(c);
1627
0
}
1628
1629
U_CAPI UBool U_EXPORT2
1630
0
u_isUUppercase(UChar32 c) {
1631
0
    return UCASE_UPPER==ucase_getType(c);
1632
0
}
1633
1634
/* Transforms the Unicode character to its lower case equivalent.*/
1635
U_CAPI UChar32 U_EXPORT2
1636
0
u_tolower(UChar32 c) {
1637
0
    return ucase_tolower(c);
1638
0
}
1639
1640
/* Transforms the Unicode character to its upper case equivalent.*/
1641
U_CAPI UChar32 U_EXPORT2
1642
0
u_toupper(UChar32 c) {
1643
0
    return ucase_toupper(c);
1644
0
}
1645
1646
/* Transforms the Unicode character to its title case equivalent.*/
1647
U_CAPI UChar32 U_EXPORT2
1648
0
u_totitle(UChar32 c) {
1649
0
    return ucase_totitle(c);
1650
0
}
1651
1652
/* return the simple case folding mapping for c */
1653
U_CAPI UChar32 U_EXPORT2
1654
0
u_foldCase(UChar32 c, uint32_t options) {
1655
0
    return ucase_fold(c, options);
1656
0
}
1657
1658
U_CFUNC int32_t U_EXPORT2
1659
32.5k
ucase_hasBinaryProperty(UChar32 c, UProperty which) {
1660
    /* case mapping properties */
1661
32.5k
    const char16_t *resultString;
1662
32.5k
    switch(which) {
1663
3.28k
    case UCHAR_LOWERCASE:
1664
3.28k
        return (UBool)(UCASE_LOWER==ucase_getType(c));
1665
3.28k
    case UCHAR_UPPERCASE:
1666
3.28k
        return (UBool)(UCASE_UPPER==ucase_getType(c));
1667
3.28k
    case UCHAR_SOFT_DOTTED:
1668
3.28k
        return ucase_isSoftDotted(c);
1669
3.28k
    case UCHAR_CASE_SENSITIVE:
1670
3.28k
        return ucase_isCaseSensitive(c);
1671
3.25k
    case UCHAR_CASED:
1672
3.25k
        return (UBool)(UCASE_NONE!=ucase_getType(c));
1673
3.29k
    case UCHAR_CASE_IGNORABLE:
1674
3.29k
        return (UBool)(ucase_getTypeOrIgnorable(c)>>2);
1675
    /*
1676
     * Note: The following Changes_When_Xyz are defined as testing whether
1677
     * the NFD form of the input changes when Xyz-case-mapped.
1678
     * However, this simpler implementation of these properties,
1679
     * ignoring NFD, passes the tests.
1680
     * The implementation needs to be changed if the tests start failing.
1681
     * When that happens, optimizations should be used to work with the
1682
     * per-single-code point ucase_toFullXyz() functions unless
1683
     * the NFD form has more than one code point,
1684
     * and the property starts set needs to be the union of the
1685
     * start sets for normalization and case mappings.
1686
     */
1687
3.18k
    case UCHAR_CHANGES_WHEN_LOWERCASED:
1688
3.18k
        return (UBool)(ucase_toFullLower(c, nullptr, nullptr, &resultString, UCASE_LOC_ROOT)>=0);
1689
3.15k
    case UCHAR_CHANGES_WHEN_UPPERCASED:
1690
3.15k
        return (UBool)(ucase_toFullUpper(c, nullptr, nullptr, &resultString, UCASE_LOC_ROOT)>=0);
1691
3.19k
    case UCHAR_CHANGES_WHEN_TITLECASED:
1692
3.19k
        return (UBool)(ucase_toFullTitle(c, nullptr, nullptr, &resultString, UCASE_LOC_ROOT)>=0);
1693
    /* case UCHAR_CHANGES_WHEN_CASEFOLDED: -- in uprops.c */
1694
3.33k
    case UCHAR_CHANGES_WHEN_CASEMAPPED:
1695
3.33k
        return (UBool)(
1696
3.33k
            ucase_toFullLower(c, nullptr, nullptr, &resultString, UCASE_LOC_ROOT)>=0 ||
1697
3.33k
            ucase_toFullUpper(c, nullptr, nullptr, &resultString, UCASE_LOC_ROOT)>=0 ||
1698
3.33k
            ucase_toFullTitle(c, nullptr, nullptr, &resultString, UCASE_LOC_ROOT)>=0);
1699
0
    default:
1700
0
        return false;
1701
32.5k
    }
1702
32.5k
}