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