Coverage Report

Created: 2023-02-22 06:51

/src/icu/source/common/uchar.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
*   Copyright (C) 1996-2016, International Business Machines
6
*   Corporation and others.  All Rights Reserved.
7
********************************************************************************
8
*
9
* File UCHAR.C
10
*
11
* Modification History:
12
*
13
*   Date        Name        Description
14
*   04/02/97    aliu        Creation.
15
*   4/15/99     Madhu       Updated all the function definitions for C Implementation
16
*   5/20/99     Madhu       Added the function u_getVersion()
17
*   8/19/1999   srl         Upgraded scripts to Unicode3.0 
18
*   11/11/1999  weiv        added u_isalnum(), cleaned comments
19
*   01/11/2000  helena      Renamed u_getVersion to u_getUnicodeVersion.
20
*   06/20/2000  helena      OS/400 port changes; mostly typecast.
21
******************************************************************************
22
*/
23
24
#include "unicode/utypes.h"
25
#include "unicode/uchar.h"
26
#include "unicode/uscript.h"
27
#include "unicode/udata.h"
28
#include "uassert.h"
29
#include "cmemory.h"
30
#include "ucln_cmn.h"
31
#include "utrie2.h"
32
#include "udataswp.h"
33
#include "uprops.h"
34
#include "ustr_imp.h"
35
36
/* uchar_props_data.h is machine-generated by genprops --csource */
37
#define INCLUDED_FROM_UCHAR_C
38
#include "uchar_props_data.h"
39
40
/* constants and macros for access to the data ------------------------------ */
41
42
/* getting a uint32_t properties word from the data */
43
0
#define GET_PROPS(c, result) ((result)=UTRIE2_GET16(&propsTrie, c))
44
45
/* API functions ------------------------------------------------------------ */
46
47
/* Gets the Unicode character's general category.*/
48
U_CAPI int8_t U_EXPORT2
49
0
u_charType(UChar32 c) {
50
0
    uint32_t props;
51
0
    GET_PROPS(c, props);
52
0
    return (int8_t)GET_CATEGORY(props);
53
0
}
54
55
/* Enumerate all code points with their general categories. */
56
struct _EnumTypeCallback {
57
    UCharEnumTypeRange *enumRange;
58
    const void *context;
59
};
60
61
static uint32_t U_CALLCONV
62
0
_enumTypeValue(const void *context, uint32_t value) {
63
0
    (void)context;
64
0
    return GET_CATEGORY(value);
65
0
}
66
67
static UBool U_CALLCONV
68
0
_enumTypeRange(const void *context, UChar32 start, UChar32 end, uint32_t value) {
69
    /* just cast the value to UCharCategory */
70
0
    return ((struct _EnumTypeCallback *)context)->
71
0
        enumRange(((struct _EnumTypeCallback *)context)->context,
72
0
                  start, end+1, (UCharCategory)value);
73
0
}
74
75
U_CAPI void U_EXPORT2
76
0
u_enumCharTypes(UCharEnumTypeRange *enumRange, const void *context) {
77
0
    struct _EnumTypeCallback callback;
78
79
0
    if(enumRange==NULL) {
80
0
        return;
81
0
    }
82
83
0
    callback.enumRange=enumRange;
84
0
    callback.context=context;
85
0
    utrie2_enum(&propsTrie, _enumTypeValue, _enumTypeRange, &callback);
86
0
}
87
88
/* Checks if ch is a lower case letter.*/
89
U_CAPI UBool U_EXPORT2
90
0
u_islower(UChar32 c) {
91
0
    uint32_t props;
92
0
    GET_PROPS(c, props);
93
0
    return (UBool)(GET_CATEGORY(props)==U_LOWERCASE_LETTER);
94
0
}
95
96
/* Checks if ch is an upper case letter.*/
97
U_CAPI UBool U_EXPORT2
98
0
u_isupper(UChar32 c) {
99
0
    uint32_t props;
100
0
    GET_PROPS(c, props);
101
0
    return (UBool)(GET_CATEGORY(props)==U_UPPERCASE_LETTER);
102
0
}
103
104
/* Checks if ch is a title case letter; usually upper case letters.*/
105
U_CAPI UBool U_EXPORT2
106
0
u_istitle(UChar32 c) {
107
0
    uint32_t props;
108
0
    GET_PROPS(c, props);
109
0
    return (UBool)(GET_CATEGORY(props)==U_TITLECASE_LETTER);
110
0
}
111
112
/* Checks if ch is a decimal digit. */
113
U_CAPI UBool U_EXPORT2
114
0
u_isdigit(UChar32 c) {
115
0
    uint32_t props;
116
0
    GET_PROPS(c, props);
117
0
    return (UBool)(GET_CATEGORY(props)==U_DECIMAL_DIGIT_NUMBER);
118
0
}
119
120
U_CAPI UBool U_EXPORT2
121
0
u_isxdigit(UChar32 c) {
122
0
    uint32_t props;
123
124
    /* check ASCII and Fullwidth ASCII a-fA-F */
125
0
    if(
126
0
        (c<=0x66 && c>=0x41 && (c<=0x46 || c>=0x61)) ||
127
0
        (c>=0xff21 && c<=0xff46 && (c<=0xff26 || c>=0xff41))
128
0
    ) {
129
0
        return TRUE;
130
0
    }
131
132
0
    GET_PROPS(c, props);
133
0
    return (UBool)(GET_CATEGORY(props)==U_DECIMAL_DIGIT_NUMBER);
134
0
}
135
136
/* Checks if the Unicode character is a letter.*/
137
U_CAPI UBool U_EXPORT2
138
0
u_isalpha(UChar32 c) {
139
0
    uint32_t props;
140
0
    GET_PROPS(c, props);
141
0
    return (UBool)((CAT_MASK(props)&U_GC_L_MASK)!=0);
142
0
}
143
144
U_CAPI UBool U_EXPORT2
145
0
u_isUAlphabetic(UChar32 c) {
146
0
    return (u_getUnicodeProperties(c, 1)&U_MASK(UPROPS_ALPHABETIC))!=0;
147
0
}
148
149
/* Checks if c is a letter or a decimal digit */
150
U_CAPI UBool U_EXPORT2
151
0
u_isalnum(UChar32 c) {
152
0
    uint32_t props;
153
0
    GET_PROPS(c, props);
154
0
    return (UBool)((CAT_MASK(props)&(U_GC_L_MASK|U_GC_ND_MASK))!=0);
155
0
}
156
157
/**
158
 * Checks if c is alphabetic, or a decimal digit; implements UCHAR_POSIX_ALNUM.
159
 * @internal
160
 */
161
U_CFUNC UBool
162
0
u_isalnumPOSIX(UChar32 c) {
163
0
    return (UBool)(u_isUAlphabetic(c) || u_isdigit(c));
164
0
}
165
166
/* Checks if ch is a unicode character with assigned character type.*/
167
U_CAPI UBool U_EXPORT2
168
0
u_isdefined(UChar32 c) {
169
0
    uint32_t props;
170
0
    GET_PROPS(c, props);
171
0
    return (UBool)(GET_CATEGORY(props)!=0);
172
0
}
173
174
/* Checks if the Unicode character is a base form character that can take a diacritic.*/
175
U_CAPI UBool U_EXPORT2
176
0
u_isbase(UChar32 c) {
177
0
    uint32_t props;
178
0
    GET_PROPS(c, props);
179
0
    return (UBool)((CAT_MASK(props)&(U_GC_L_MASK|U_GC_N_MASK|U_GC_MC_MASK|U_GC_ME_MASK))!=0);
180
0
}
181
182
/* Checks if the Unicode character is a control character.*/
183
U_CAPI UBool U_EXPORT2
184
0
u_iscntrl(UChar32 c) {
185
0
    uint32_t props;
186
0
    GET_PROPS(c, props);
187
0
    return (UBool)((CAT_MASK(props)&(U_GC_CC_MASK|U_GC_CF_MASK|U_GC_ZL_MASK|U_GC_ZP_MASK))!=0);
188
0
}
189
190
U_CAPI UBool U_EXPORT2
191
0
u_isISOControl(UChar32 c) {
192
0
    return (uint32_t)c<=0x9f && (c<=0x1f || c>=0x7f);
193
0
}
194
195
/* Some control characters that are used as space. */
196
#define IS_THAT_CONTROL_SPACE(c) \
197
0
    (c<=0x9f && ((c>=TAB && c<=CR) || (c>=0x1c && c <=0x1f) || c==0x85))
198
199
/* Java has decided that U+0085 New Line is not whitespace any more. */
200
#define IS_THAT_ASCII_CONTROL_SPACE(c) \
201
0
    (c<=0x1f && c>=TAB && (c<=CR || c>=0x1c))
202
203
/* Checks if the Unicode character is a space character.*/
204
U_CAPI UBool U_EXPORT2
205
0
u_isspace(UChar32 c) {
206
0
    uint32_t props;
207
0
    GET_PROPS(c, props);
208
0
    return (UBool)((CAT_MASK(props)&U_GC_Z_MASK)!=0 || IS_THAT_CONTROL_SPACE(c));
209
0
}
210
211
U_CAPI UBool U_EXPORT2
212
0
u_isJavaSpaceChar(UChar32 c) {
213
0
    uint32_t props;
214
0
    GET_PROPS(c, props);
215
0
    return (UBool)((CAT_MASK(props)&U_GC_Z_MASK)!=0);
216
0
}
217
218
/* Checks if the Unicode character is a whitespace character.*/
219
U_CAPI UBool U_EXPORT2
220
0
u_isWhitespace(UChar32 c) {
221
0
    uint32_t props;
222
0
    GET_PROPS(c, props);
223
0
    return (UBool)(
224
0
                ((CAT_MASK(props)&U_GC_Z_MASK)!=0 &&
225
0
                    c!=NBSP && c!=FIGURESP && c!=NNBSP) || /* exclude no-break spaces */
226
0
                IS_THAT_ASCII_CONTROL_SPACE(c)
227
0
           );
228
0
}
229
230
U_CAPI UBool U_EXPORT2
231
0
u_isblank(UChar32 c) {
232
0
    if((uint32_t)c<=0x9f) {
233
0
        return c==9 || c==0x20; /* TAB or SPACE */
234
0
    } else {
235
        /* Zs */
236
0
        uint32_t props;
237
0
        GET_PROPS(c, props);
238
0
        return (UBool)(GET_CATEGORY(props)==U_SPACE_SEPARATOR);
239
0
    }
240
0
}
241
242
U_CAPI UBool U_EXPORT2
243
0
u_isUWhiteSpace(UChar32 c) {
244
0
    return (u_getUnicodeProperties(c, 1)&U_MASK(UPROPS_WHITE_SPACE))!=0;
245
0
}
246
247
/* Checks if the Unicode character is printable.*/
248
U_CAPI UBool U_EXPORT2
249
0
u_isprint(UChar32 c) {
250
0
    uint32_t props;
251
0
    GET_PROPS(c, props);
252
    /* comparing ==0 returns FALSE for the categories mentioned */
253
0
    return (UBool)((CAT_MASK(props)&U_GC_C_MASK)==0);
254
0
}
255
256
/**
257
 * Checks if c is in \p{graph}\p{blank} - \p{cntrl}.
258
 * Implements UCHAR_POSIX_PRINT.
259
 * @internal
260
 */
261
U_CFUNC UBool
262
0
u_isprintPOSIX(UChar32 c) {
263
0
    uint32_t props;
264
0
    GET_PROPS(c, props);
265
    /*
266
     * The only cntrl character in graph+blank is TAB (in blank).
267
     * Here we implement (blank-TAB)=Zs instead of calling u_isblank().
268
     */
269
0
    return (UBool)((GET_CATEGORY(props)==U_SPACE_SEPARATOR) || u_isgraphPOSIX(c));
270
0
}
271
272
U_CAPI UBool U_EXPORT2
273
0
u_isgraph(UChar32 c) {
274
0
    uint32_t props;
275
0
    GET_PROPS(c, props);
276
    /* comparing ==0 returns FALSE for the categories mentioned */
277
0
    return (UBool)((CAT_MASK(props)&
278
0
                    (U_GC_CC_MASK|U_GC_CF_MASK|U_GC_CS_MASK|U_GC_CN_MASK|U_GC_Z_MASK))
279
0
                   ==0);
280
0
}
281
282
/**
283
 * Checks if c is in
284
 * [^\p{space}\p{gc=Control}\p{gc=Surrogate}\p{gc=Unassigned}]
285
 * with space=\p{Whitespace} and Control=Cc.
286
 * Implements UCHAR_POSIX_GRAPH.
287
 * @internal
288
 */
289
U_CFUNC UBool
290
0
u_isgraphPOSIX(UChar32 c) {
291
0
    uint32_t props;
292
0
    GET_PROPS(c, props);
293
    /* \p{space}\p{gc=Control} == \p{gc=Z}\p{Control} */
294
    /* comparing ==0 returns FALSE for the categories mentioned */
295
0
    return (UBool)((CAT_MASK(props)&
296
0
                    (U_GC_CC_MASK|U_GC_CS_MASK|U_GC_CN_MASK|U_GC_Z_MASK))
297
0
                   ==0);
298
0
}
299
300
U_CAPI UBool U_EXPORT2
301
0
u_ispunct(UChar32 c) {
302
0
    uint32_t props;
303
0
    GET_PROPS(c, props);
304
0
    return (UBool)((CAT_MASK(props)&U_GC_P_MASK)!=0);
305
0
}
306
307
/* Checks if the Unicode character can start a Unicode identifier.*/
308
U_CAPI UBool U_EXPORT2
309
0
u_isIDStart(UChar32 c) {
310
    /* same as u_isalpha() */
311
0
    uint32_t props;
312
0
    GET_PROPS(c, props);
313
0
    return (UBool)((CAT_MASK(props)&(U_GC_L_MASK|U_GC_NL_MASK))!=0);
314
0
}
315
316
/* Checks if the Unicode character can be a Unicode identifier part other than starting the
317
 identifier.*/
318
U_CAPI UBool U_EXPORT2
319
0
u_isIDPart(UChar32 c) {
320
0
    uint32_t props;
321
0
    GET_PROPS(c, props);
322
0
    return (UBool)(
323
0
           (CAT_MASK(props)&
324
0
            (U_GC_ND_MASK|U_GC_NL_MASK|
325
0
             U_GC_L_MASK|
326
0
             U_GC_PC_MASK|U_GC_MC_MASK|U_GC_MN_MASK)
327
0
           )!=0 ||
328
0
           u_isIDIgnorable(c));
329
0
}
330
331
/*Checks if the Unicode character can be ignorable in a Java or Unicode identifier.*/
332
U_CAPI UBool U_EXPORT2
333
0
u_isIDIgnorable(UChar32 c) {
334
0
    if(c<=0x9f) {
335
0
        return u_isISOControl(c) && !IS_THAT_ASCII_CONTROL_SPACE(c);
336
0
    } else {
337
0
        uint32_t props;
338
0
        GET_PROPS(c, props);
339
0
        return (UBool)(GET_CATEGORY(props)==U_FORMAT_CHAR);
340
0
    }
341
0
}
342
343
/*Checks if the Unicode character can start a Java identifier.*/
344
U_CAPI UBool U_EXPORT2
345
0
u_isJavaIDStart(UChar32 c) {
346
0
    uint32_t props;
347
0
    GET_PROPS(c, props);
348
0
    return (UBool)((CAT_MASK(props)&(U_GC_L_MASK|U_GC_SC_MASK|U_GC_PC_MASK))!=0);
349
0
}
350
351
/*Checks if the Unicode character can be a Java identifier part other than starting the
352
 * identifier.
353
 */
354
U_CAPI UBool U_EXPORT2
355
0
u_isJavaIDPart(UChar32 c) {
356
0
    uint32_t props;
357
0
    GET_PROPS(c, props);
358
0
    return (UBool)(
359
0
           (CAT_MASK(props)&
360
0
            (U_GC_ND_MASK|U_GC_NL_MASK|
361
0
             U_GC_L_MASK|
362
0
             U_GC_SC_MASK|U_GC_PC_MASK|
363
0
             U_GC_MC_MASK|U_GC_MN_MASK)
364
0
           )!=0 ||
365
0
           u_isIDIgnorable(c));
366
0
}
367
368
U_CAPI int32_t U_EXPORT2
369
0
u_charDigitValue(UChar32 c) {
370
0
    uint32_t props;
371
0
    int32_t value;
372
0
    GET_PROPS(c, props);
373
0
    value=(int32_t)GET_NUMERIC_TYPE_VALUE(props)-UPROPS_NTV_DECIMAL_START;
374
0
    if(value<=9) {
375
0
        return value;
376
0
    } else {
377
0
        return -1;
378
0
    }
379
0
}
380
381
U_CAPI double U_EXPORT2
382
0
u_getNumericValue(UChar32 c) {
383
0
    uint32_t props;
384
0
    int32_t ntv;
385
0
    GET_PROPS(c, props);
386
0
    ntv=(int32_t)GET_NUMERIC_TYPE_VALUE(props);
387
388
0
    if(ntv==UPROPS_NTV_NONE) {
389
0
        return U_NO_NUMERIC_VALUE;
390
0
    } else if(ntv<UPROPS_NTV_DIGIT_START) {
391
        /* decimal digit */
392
0
        return ntv-UPROPS_NTV_DECIMAL_START;
393
0
    } else if(ntv<UPROPS_NTV_NUMERIC_START) {
394
        /* other digit */
395
0
        return ntv-UPROPS_NTV_DIGIT_START;
396
0
    } else if(ntv<UPROPS_NTV_FRACTION_START) {
397
        /* small integer */
398
0
        return ntv-UPROPS_NTV_NUMERIC_START;
399
0
    } else if(ntv<UPROPS_NTV_LARGE_START) {
400
        /* fraction */
401
0
        int32_t numerator=(ntv>>4)-12;
402
0
        int32_t denominator=(ntv&0xf)+1;
403
0
        return (double)numerator/denominator;
404
0
    } else if(ntv<UPROPS_NTV_BASE60_START) {
405
        /* large, single-significant-digit integer */
406
0
        double numValue;
407
0
        int32_t mant=(ntv>>5)-14;
408
0
        int32_t exp=(ntv&0x1f)+2;
409
0
        numValue=mant;
410
411
        /* multiply by 10^exp without math.h */
412
0
        while(exp>=4) {
413
0
            numValue*=10000.;
414
0
            exp-=4;
415
0
        }
416
0
        switch(exp) {
417
0
        case 3:
418
0
            numValue*=1000.;
419
0
            break;
420
0
        case 2:
421
0
            numValue*=100.;
422
0
            break;
423
0
        case 1:
424
0
            numValue*=10.;
425
0
            break;
426
0
        case 0:
427
0
        default:
428
0
            break;
429
0
        }
430
431
0
        return numValue;
432
0
    } else if(ntv<UPROPS_NTV_FRACTION20_START) {
433
        /* sexagesimal (base 60) integer */
434
0
        int32_t numValue=(ntv>>2)-0xbf;
435
0
        int32_t exp=(ntv&3)+1;
436
437
0
        switch(exp) {
438
0
        case 4:
439
0
            numValue*=60*60*60*60;
440
0
            break;
441
0
        case 3:
442
0
            numValue*=60*60*60;
443
0
            break;
444
0
        case 2:
445
0
            numValue*=60*60;
446
0
            break;
447
0
        case 1:
448
0
            numValue*=60;
449
0
            break;
450
0
        case 0:
451
0
        default:
452
0
            break;
453
0
        }
454
455
0
        return numValue;
456
0
    } else if(ntv<UPROPS_NTV_FRACTION32_START) {
457
        // fraction-20 e.g. 3/80
458
0
        int32_t frac20=ntv-UPROPS_NTV_FRACTION20_START;  // 0..0x17
459
0
        int32_t numerator=2*(frac20&3)+1;
460
0
        int32_t denominator=20<<(frac20>>2);
461
0
        return (double)numerator/denominator;
462
0
    } else if(ntv<UPROPS_NTV_RESERVED_START) {
463
        // fraction-32 e.g. 3/64
464
0
        int32_t frac32=ntv-UPROPS_NTV_FRACTION32_START;  // 0..15
465
0
        int32_t numerator=2*(frac32&3)+1;
466
0
        int32_t denominator=32<<(frac32>>2);
467
0
        return (double)numerator/denominator;
468
0
    } else {
469
        /* reserved */
470
0
        return U_NO_NUMERIC_VALUE;
471
0
    }
472
0
}
473
474
U_CAPI int32_t U_EXPORT2
475
0
u_digit(UChar32 ch, int8_t radix) {
476
0
    int8_t value;
477
0
    if((uint8_t)(radix-2)<=(36-2)) {
478
0
        value=(int8_t)u_charDigitValue(ch);
479
0
        if(value<0) {
480
            /* ch is not a decimal digit, try latin letters */
481
0
            if(ch>=0x61 && ch<=0x7A) {
482
0
                value=(int8_t)(ch-0x57);  /* ch - 'a' + 10 */
483
0
            } else if(ch>=0x41 && ch<=0x5A) {
484
0
                value=(int8_t)(ch-0x37);  /* ch - 'A' + 10 */
485
0
            } else if(ch>=0xFF41 && ch<=0xFF5A) {
486
0
                value=(int8_t)(ch-0xFF37);  /* fullwidth ASCII a-z */
487
0
            } else if(ch>=0xFF21 && ch<=0xFF3A) {
488
0
                value=(int8_t)(ch-0xFF17);  /* fullwidth ASCII A-Z */
489
0
            }
490
0
        }
491
0
    } else {
492
0
        value=-1;   /* invalid radix */
493
0
    }
494
0
    return (int8_t)((value<radix) ? value : -1);
495
0
}
496
497
U_CAPI UChar32 U_EXPORT2
498
0
u_forDigit(int32_t digit, int8_t radix) {
499
0
    if((uint8_t)(radix-2)>(36-2) || (uint32_t)digit>=(uint32_t)radix) {
500
0
        return 0;
501
0
    } else if(digit<10) {
502
0
        return (UChar32)(0x30+digit);
503
0
    } else {
504
0
        return (UChar32)((0x61-10)+digit);
505
0
    }
506
0
}
507
508
/* miscellaneous, and support for uprops.cpp -------------------------------- */
509
510
U_CAPI void U_EXPORT2
511
0
u_getUnicodeVersion(UVersionInfo versionArray) {
512
0
    if(versionArray!=NULL) {
513
0
        uprv_memcpy(versionArray, dataVersion, U_MAX_VERSION_LENGTH);
514
0
    }
515
0
}
516
517
U_CFUNC uint32_t
518
0
u_getMainProperties(UChar32 c) {
519
0
    uint32_t props;
520
0
    GET_PROPS(c, props);
521
0
    return props;
522
0
}
523
524
U_CFUNC uint32_t
525
0
u_getUnicodeProperties(UChar32 c, int32_t column) {
526
0
    U_ASSERT(column>=0);
527
0
    if(column>=propsVectorsColumns) {
528
0
        return 0;
529
0
    } else {
530
0
        uint16_t vecIndex=UTRIE2_GET16(&propsVectorsTrie, c);
531
0
        return propsVectors[vecIndex+column];
532
0
    }
533
0
}
534
535
U_CFUNC int32_t
536
0
uprv_getMaxValues(int32_t column) {
537
0
    switch(column) {
538
0
    case 0:
539
0
        return indexes[UPROPS_MAX_VALUES_INDEX];
540
0
    case 2:
541
0
        return indexes[UPROPS_MAX_VALUES_2_INDEX];
542
0
    default:
543
0
        return 0;
544
0
    }
545
0
}
546
547
U_CAPI void U_EXPORT2
548
0
u_charAge(UChar32 c, UVersionInfo versionArray) {
549
0
    if(versionArray!=NULL) {
550
0
        uint32_t version=u_getUnicodeProperties(c, 0)>>UPROPS_AGE_SHIFT;
551
0
        versionArray[0]=(uint8_t)(version>>4);
552
0
        versionArray[1]=(uint8_t)(version&0xf);
553
0
        versionArray[2]=versionArray[3]=0;
554
0
    }
555
0
}
556
557
U_CAPI UScriptCode U_EXPORT2
558
0
uscript_getScript(UChar32 c, UErrorCode *pErrorCode) {
559
0
    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
560
0
        return USCRIPT_INVALID_CODE;
561
0
    }
562
0
    if((uint32_t)c>0x10ffff) {
563
0
        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
564
0
        return USCRIPT_INVALID_CODE;
565
0
    }
566
0
    uint32_t scriptX=u_getUnicodeProperties(c, 0)&UPROPS_SCRIPT_X_MASK;
567
0
    uint32_t codeOrIndex=uprops_mergeScriptCodeOrIndex(scriptX);
568
0
    if(scriptX<UPROPS_SCRIPT_X_WITH_COMMON) {
569
0
        return (UScriptCode)codeOrIndex;
570
0
    } else if(scriptX<UPROPS_SCRIPT_X_WITH_INHERITED) {
571
0
        return USCRIPT_COMMON;
572
0
    } else if(scriptX<UPROPS_SCRIPT_X_WITH_OTHER) {
573
0
        return USCRIPT_INHERITED;
574
0
    } else {
575
0
        return (UScriptCode)scriptExtensions[codeOrIndex];
576
0
    }
577
0
}
578
579
U_CAPI UBool U_EXPORT2
580
0
uscript_hasScript(UChar32 c, UScriptCode sc) {
581
0
    uint32_t scriptX=u_getUnicodeProperties(c, 0)&UPROPS_SCRIPT_X_MASK;
582
0
    uint32_t codeOrIndex=uprops_mergeScriptCodeOrIndex(scriptX);
583
0
    if(scriptX<UPROPS_SCRIPT_X_WITH_COMMON) {
584
0
        return sc==(UScriptCode)codeOrIndex;
585
0
    }
586
587
0
    const uint16_t *scx=scriptExtensions+codeOrIndex;
588
0
    if(scriptX>=UPROPS_SCRIPT_X_WITH_OTHER) {
589
0
        scx=scriptExtensions+scx[1];
590
0
    }
591
0
    uint32_t sc32=sc;
592
0
    if(sc32>0x7fff) {
593
        /* Guard against bogus input that would make us go past the Script_Extensions terminator. */
594
0
        return FALSE;
595
0
    }
596
0
    while(sc32>*scx) {
597
0
        ++scx;
598
0
    }
599
0
    return sc32==(*scx&0x7fff);
600
0
}
601
602
U_CAPI int32_t U_EXPORT2
603
uscript_getScriptExtensions(UChar32 c,
604
                            UScriptCode *scripts, int32_t capacity,
605
0
                            UErrorCode *pErrorCode) {
606
0
    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
607
0
        return 0;
608
0
    }
609
0
    if(capacity<0 || (capacity>0 && scripts==NULL)) {
610
0
        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
611
0
        return 0;
612
0
    }
613
0
    uint32_t scriptX=u_getUnicodeProperties(c, 0)&UPROPS_SCRIPT_X_MASK;
614
0
    uint32_t codeOrIndex=uprops_mergeScriptCodeOrIndex(scriptX);
615
0
    if(scriptX<UPROPS_SCRIPT_X_WITH_COMMON) {
616
0
        if(capacity==0) {
617
0
            *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
618
0
        } else {
619
0
            scripts[0]=(UScriptCode)codeOrIndex;
620
0
        }
621
0
        return 1;
622
0
    }
623
624
0
    const uint16_t *scx=scriptExtensions+codeOrIndex;
625
0
    if(scriptX>=UPROPS_SCRIPT_X_WITH_OTHER) {
626
0
        scx=scriptExtensions+scx[1];
627
0
    }
628
0
    int32_t length=0;
629
0
    uint16_t sx;
630
0
    do {
631
0
        sx=*scx++;
632
0
        if(length<capacity) {
633
0
            scripts[length]=(UScriptCode)(sx&0x7fff);
634
0
        }
635
0
        ++length;
636
0
    } while(sx<0x8000);
637
0
    if(length>capacity) {
638
0
        *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
639
0
    }
640
0
    return length;
641
0
}
642
643
U_CAPI UBlockCode U_EXPORT2
644
0
ublock_getCode(UChar32 c) {
645
0
    return (UBlockCode)((u_getUnicodeProperties(c, 0)&UPROPS_BLOCK_MASK)>>UPROPS_BLOCK_SHIFT);
646
0
}
647
648
/* property starts for UnicodeSet ------------------------------------------- */
649
650
static UBool U_CALLCONV
651
0
_enumPropertyStartsRange(const void *context, UChar32 start, UChar32 end, uint32_t value) {
652
    /* add the start code point to the USet */
653
0
    const USetAdder *sa=(const USetAdder *)context;
654
0
    sa->add(sa->set, start);
655
0
    (void)end;
656
0
    (void)value;
657
0
    return TRUE;
658
0
}
659
660
0
#define USET_ADD_CP_AND_NEXT(sa, cp) sa->add(sa->set, cp); sa->add(sa->set, cp+1)
661
662
U_CFUNC void U_EXPORT2
663
0
uchar_addPropertyStarts(const USetAdder *sa, UErrorCode *pErrorCode) {
664
0
    if(U_FAILURE(*pErrorCode)) {
665
0
        return;
666
0
    }
667
668
    /* add the start code point of each same-value range of the main trie */
669
0
    utrie2_enum(&propsTrie, NULL, _enumPropertyStartsRange, sa);
670
671
    /* add code points with hardcoded properties, plus the ones following them */
672
673
    /* add for u_isblank() */
674
0
    USET_ADD_CP_AND_NEXT(sa, TAB);
675
676
    /* add for IS_THAT_CONTROL_SPACE() */
677
0
    sa->add(sa->set, CR+1); /* range TAB..CR */
678
0
    sa->add(sa->set, 0x1c);
679
0
    sa->add(sa->set, 0x1f+1);
680
0
    USET_ADD_CP_AND_NEXT(sa, 0x85);  // NEXT LINE (NEL)
681
682
    /* add for u_isIDIgnorable() what was not added above */
683
0
    sa->add(sa->set, 0x7f); /* range DEL..NBSP-1, NBSP added below */
684
0
    sa->add(sa->set, HAIRSP);
685
0
    sa->add(sa->set, RLM+1);
686
0
    sa->add(sa->set, 0x206a);  // INHIBIT SYMMETRIC SWAPPING
687
0
    sa->add(sa->set, 0x206f+1);  // NOMINAL DIGIT SHAPES
688
0
    USET_ADD_CP_AND_NEXT(sa, ZWNBSP);
689
690
    /* add no-break spaces for u_isWhitespace() what was not added above */
691
0
    USET_ADD_CP_AND_NEXT(sa, NBSP);
692
0
    USET_ADD_CP_AND_NEXT(sa, FIGURESP);
693
0
    USET_ADD_CP_AND_NEXT(sa, NNBSP);
694
695
    /* add for u_digit() */
696
0
    sa->add(sa->set, u'a');
697
0
    sa->add(sa->set, u'z'+1);
698
0
    sa->add(sa->set, u'A');
699
0
    sa->add(sa->set, u'Z'+1);
700
    // fullwidth
701
0
    sa->add(sa->set, u'a');
702
0
    sa->add(sa->set, u'z'+1);
703
0
    sa->add(sa->set, u'A');
704
0
    sa->add(sa->set, u'Z'+1);
705
706
    /* add for u_isxdigit() */
707
0
    sa->add(sa->set, u'f'+1);
708
0
    sa->add(sa->set, u'F'+1);
709
    // fullwidth
710
0
    sa->add(sa->set, u'f'+1);
711
0
    sa->add(sa->set, u'F'+1);
712
713
    /* add for UCHAR_DEFAULT_IGNORABLE_CODE_POINT what was not added above */
714
0
    sa->add(sa->set, 0x2060); /* range 2060..206f */
715
0
    sa->add(sa->set, 0xfff0);
716
0
    sa->add(sa->set, 0xfffb+1);
717
0
    sa->add(sa->set, 0xe0000);
718
0
    sa->add(sa->set, 0xe0fff+1);
719
720
    /* add for UCHAR_GRAPHEME_BASE and others */
721
0
    USET_ADD_CP_AND_NEXT(sa, CGJ);
722
0
}
723
724
U_CFUNC void U_EXPORT2
725
0
upropsvec_addPropertyStarts(const USetAdder *sa, UErrorCode *pErrorCode) {
726
0
    if(U_FAILURE(*pErrorCode)) {
727
0
        return;
728
0
    }
729
730
    /* add the start code point of each same-value range of the properties vectors trie */
731
0
    utrie2_enum(&propsVectorsTrie, NULL, _enumPropertyStartsRange, sa);
732
0
}