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 | #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 | u_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. */ |
56 | struct _EnumTypeCallback { |
57 | UCharEnumTypeRange *enumRange; |
58 | const void *context; |
59 | }; |
60 | |
61 | static uint32_t U_CALLCONV |
62 | _enumTypeValue(const void *context, uint32_t value) { |
63 | (void)context; |
64 | return GET_CATEGORY(value); |
65 | } |
66 | |
67 | static UBool U_CALLCONV |
68 | _enumTypeRange(const void *context, UChar32 start, UChar32 end, uint32_t value) { |
69 | /* just cast the value to UCharCategory */ |
70 | return ((struct _EnumTypeCallback *)context)-> |
71 | enumRange(((struct _EnumTypeCallback *)context)->context, |
72 | start, end+1, (UCharCategory)value); |
73 | } |
74 | |
75 | U_CAPI void U_EXPORT2 |
76 | u_enumCharTypes(UCharEnumTypeRange *enumRange, const void *context) { |
77 | struct _EnumTypeCallback callback; |
78 | |
79 | if(enumRange==nullptr) { |
80 | return; |
81 | } |
82 | |
83 | callback.enumRange=enumRange; |
84 | callback.context=context; |
85 | utrie2_enum(&propsTrie, _enumTypeValue, _enumTypeRange, &callback); |
86 | } |
87 | |
88 | /* Checks if ch is a lower case letter.*/ |
89 | U_CAPI UBool U_EXPORT2 |
90 | u_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.*/ |
97 | U_CAPI UBool U_EXPORT2 |
98 | u_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.*/ |
105 | U_CAPI UBool U_EXPORT2 |
106 | u_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. */ |
113 | U_CAPI UBool U_EXPORT2 |
114 | u_isdigit(UChar32 c) { |
115 | uint32_t props; |
116 | GET_PROPS(c, props); |
117 | return (UBool)(GET_CATEGORY(props)==U_DECIMAL_DIGIT_NUMBER); |
118 | } |
119 | |
120 | U_CAPI UBool U_EXPORT2 |
121 | u_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.*/ |
137 | U_CAPI UBool U_EXPORT2 |
138 | u_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 | |
144 | U_CAPI UBool U_EXPORT2 |
145 | u_isUAlphabetic(UChar32 c) { |
146 | return (u_getUnicodeProperties(c, 1)&U_MASK(UPROPS_ALPHABETIC))!=0; |
147 | } |
148 | |
149 | /* Checks if c is a letter or a decimal digit */ |
150 | U_CAPI UBool U_EXPORT2 |
151 | u_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 | |
157 | /** |
158 | * Checks if c is alphabetic, or a decimal digit; implements UCHAR_POSIX_ALNUM. |
159 | * @internal |
160 | */ |
161 | U_CFUNC UBool |
162 | u_isalnumPOSIX(UChar32 c) { |
163 | return (UBool)(u_isUAlphabetic(c) || u_isdigit(c)); |
164 | } |
165 | |
166 | /* Checks if ch is a unicode character with assigned character type.*/ |
167 | U_CAPI UBool U_EXPORT2 |
168 | u_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.*/ |
175 | U_CAPI UBool U_EXPORT2 |
176 | u_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.*/ |
183 | U_CAPI UBool U_EXPORT2 |
184 | u_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 | |
190 | U_CAPI UBool U_EXPORT2 |
191 | u_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==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 | (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 | u_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 | |
211 | U_CAPI UBool U_EXPORT2 |
212 | u_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.*/ |
219 | U_CAPI UBool U_EXPORT2 |
220 | u_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 */ |
226 | IS_THAT_ASCII_CONTROL_SPACE(c) |
227 | ); |
228 | } |
229 | |
230 | U_CAPI UBool U_EXPORT2 |
231 | u_isblank(UChar32 c) { |
232 | if((uint32_t)c<=0x9f) { |
233 | return c==9 || c==0x20; /* TAB or SPACE */ |
234 | } else { |
235 | /* Zs */ |
236 | uint32_t props; |
237 | GET_PROPS(c, props); |
238 | return (UBool)(GET_CATEGORY(props)==U_SPACE_SEPARATOR); |
239 | } |
240 | } |
241 | |
242 | U_CAPI UBool U_EXPORT2 |
243 | u_isUWhiteSpace(UChar32 c) { |
244 | return (u_getUnicodeProperties(c, 1)&U_MASK(UPROPS_WHITE_SPACE))!=0; |
245 | } |
246 | |
247 | /* Checks if the Unicode character is printable.*/ |
248 | U_CAPI UBool U_EXPORT2 |
249 | u_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 | |
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 | u_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 | |
272 | U_CAPI UBool U_EXPORT2 |
273 | u_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 | |
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 | u_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 | |
300 | U_CAPI UBool U_EXPORT2 |
301 | u_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 be ignorable in a Java or Unicode identifier.*/ |
308 | U_CAPI UBool U_EXPORT2 |
309 | u_isIDIgnorable(UChar32 c) { |
310 | if(c<=0x9f) { |
311 | return u_isISOControl(c) && !IS_THAT_ASCII_CONTROL_SPACE(c); |
312 | } else { |
313 | uint32_t props; |
314 | GET_PROPS(c, props); |
315 | return (UBool)(GET_CATEGORY(props)==U_FORMAT_CHAR); |
316 | } |
317 | } |
318 | |
319 | /*Checks if the Unicode character can start a Java identifier.*/ |
320 | U_CAPI UBool U_EXPORT2 |
321 | u_isJavaIDStart(UChar32 c) { |
322 | uint32_t props; |
323 | GET_PROPS(c, props); |
324 | return (UBool)((CAT_MASK(props)&(U_GC_L_MASK|U_GC_SC_MASK|U_GC_PC_MASK))!=0); |
325 | } |
326 | |
327 | /*Checks if the Unicode character can be a Java identifier part other than starting the |
328 | * identifier. |
329 | */ |
330 | U_CAPI UBool U_EXPORT2 |
331 | u_isJavaIDPart(UChar32 c) { |
332 | uint32_t props; |
333 | GET_PROPS(c, props); |
334 | return (UBool)( |
335 | (CAT_MASK(props)& |
336 | (U_GC_ND_MASK|U_GC_NL_MASK| |
337 | U_GC_L_MASK| |
338 | U_GC_SC_MASK|U_GC_PC_MASK| |
339 | U_GC_MC_MASK|U_GC_MN_MASK) |
340 | )!=0 || |
341 | u_isIDIgnorable(c)); |
342 | } |
343 | |
344 | U_CAPI int32_t U_EXPORT2 |
345 | u_charDigitValue(UChar32 c) { |
346 | uint32_t props; |
347 | int32_t value; |
348 | GET_PROPS(c, props); |
349 | value=(int32_t)GET_NUMERIC_TYPE_VALUE(props)-UPROPS_NTV_DECIMAL_START; |
350 | if(value<=9) { |
351 | return value; |
352 | } else { |
353 | return -1; |
354 | } |
355 | } |
356 | |
357 | U_CAPI double U_EXPORT2 |
358 | u_getNumericValue(UChar32 c) { |
359 | uint32_t props; |
360 | int32_t ntv; |
361 | GET_PROPS(c, props); |
362 | ntv=(int32_t)GET_NUMERIC_TYPE_VALUE(props); |
363 | |
364 | if(ntv==UPROPS_NTV_NONE) { |
365 | return U_NO_NUMERIC_VALUE; |
366 | } else if(ntv<UPROPS_NTV_DIGIT_START) { |
367 | /* decimal digit */ |
368 | return ntv-UPROPS_NTV_DECIMAL_START; |
369 | } else if(ntv<UPROPS_NTV_NUMERIC_START) { |
370 | /* other digit */ |
371 | return ntv-UPROPS_NTV_DIGIT_START; |
372 | } else if(ntv<UPROPS_NTV_FRACTION_START) { |
373 | /* small integer */ |
374 | return ntv-UPROPS_NTV_NUMERIC_START; |
375 | } else if(ntv<UPROPS_NTV_LARGE_START) { |
376 | /* fraction */ |
377 | int32_t numerator=(ntv>>4)-12; |
378 | int32_t denominator=(ntv&0xf)+1; |
379 | return (double)numerator/denominator; |
380 | } else if(ntv<UPROPS_NTV_BASE60_START) { |
381 | /* large, single-significant-digit integer */ |
382 | double numValue; |
383 | int32_t mant=(ntv>>5)-14; |
384 | int32_t exp=(ntv&0x1f)+2; |
385 | numValue=mant; |
386 | |
387 | /* multiply by 10^exp without math.h */ |
388 | while(exp>=4) { |
389 | numValue*=10000.; |
390 | exp-=4; |
391 | } |
392 | switch(exp) { |
393 | case 3: |
394 | numValue*=1000.; |
395 | break; |
396 | case 2: |
397 | numValue*=100.; |
398 | break; |
399 | case 1: |
400 | numValue*=10.; |
401 | break; |
402 | case 0: |
403 | default: |
404 | break; |
405 | } |
406 | |
407 | return numValue; |
408 | } else if(ntv<UPROPS_NTV_FRACTION20_START) { |
409 | /* sexagesimal (base 60) integer */ |
410 | int32_t numValue=(ntv>>2)-0xbf; |
411 | int32_t exp=(ntv&3)+1; |
412 | |
413 | switch(exp) { |
414 | case 4: |
415 | numValue*=60*60*60*60; |
416 | break; |
417 | case 3: |
418 | numValue*=60*60*60; |
419 | break; |
420 | case 2: |
421 | numValue*=60*60; |
422 | break; |
423 | case 1: |
424 | numValue*=60; |
425 | break; |
426 | case 0: |
427 | default: |
428 | break; |
429 | } |
430 | |
431 | return numValue; |
432 | } else if(ntv<UPROPS_NTV_FRACTION32_START) { |
433 | // fraction-20 e.g. 3/80 |
434 | int32_t frac20=ntv-UPROPS_NTV_FRACTION20_START; // 0..0x17 |
435 | int32_t numerator=2*(frac20&3)+1; |
436 | int32_t denominator=20<<(frac20>>2); |
437 | return (double)numerator/denominator; |
438 | } else if(ntv<UPROPS_NTV_RESERVED_START) { |
439 | // fraction-32 e.g. 3/64 |
440 | int32_t frac32=ntv-UPROPS_NTV_FRACTION32_START; // 0..15 |
441 | int32_t numerator=2*(frac32&3)+1; |
442 | int32_t denominator=32<<(frac32>>2); |
443 | return (double)numerator/denominator; |
444 | } else { |
445 | /* reserved */ |
446 | return U_NO_NUMERIC_VALUE; |
447 | } |
448 | } |
449 | |
450 | U_CAPI int32_t U_EXPORT2 |
451 | u_digit(UChar32 ch, int8_t radix) { |
452 | int8_t value; |
453 | if((uint8_t)(radix-2)<=(36-2)) { |
454 | value=(int8_t)u_charDigitValue(ch); |
455 | if(value<0) { |
456 | /* ch is not a decimal digit, try latin letters */ |
457 | if(ch>=0x61 && ch<=0x7A) { |
458 | value=(int8_t)(ch-0x57); /* ch - 'a' + 10 */ |
459 | } else if(ch>=0x41 && ch<=0x5A) { |
460 | value=(int8_t)(ch-0x37); /* ch - 'A' + 10 */ |
461 | } else if(ch>=0xFF41 && ch<=0xFF5A) { |
462 | value=(int8_t)(ch-0xFF37); /* fullwidth ASCII a-z */ |
463 | } else if(ch>=0xFF21 && ch<=0xFF3A) { |
464 | value=(int8_t)(ch-0xFF17); /* fullwidth ASCII A-Z */ |
465 | } |
466 | } |
467 | } else { |
468 | value=-1; /* invalid radix */ |
469 | } |
470 | return (int8_t)((value<radix) ? value : -1); |
471 | } |
472 | |
473 | U_CAPI UChar32 U_EXPORT2 |
474 | u_forDigit(int32_t digit, int8_t radix) { |
475 | if((uint8_t)(radix-2)>(36-2) || (uint32_t)digit>=(uint32_t)radix) { |
476 | return 0; |
477 | } else if(digit<10) { |
478 | return (UChar32)(0x30+digit); |
479 | } else { |
480 | return (UChar32)((0x61-10)+digit); |
481 | } |
482 | } |
483 | |
484 | /* miscellaneous, and support for uprops.cpp -------------------------------- */ |
485 | |
486 | U_CAPI void U_EXPORT2 |
487 | u_getUnicodeVersion(UVersionInfo versionArray) { |
488 | if(versionArray!=nullptr) { |
489 | uprv_memcpy(versionArray, dataVersion, U_MAX_VERSION_LENGTH); |
490 | } |
491 | } |
492 | |
493 | U_CFUNC uint32_t |
494 | u_getMainProperties(UChar32 c) { |
495 | uint32_t props; |
496 | GET_PROPS(c, props); |
497 | return props; |
498 | } |
499 | |
500 | U_CFUNC uint32_t |
501 | u_getUnicodeProperties(UChar32 c, int32_t column) { |
502 | U_ASSERT(column>=0); |
503 | if(column>=propsVectorsColumns) { |
504 | return 0; |
505 | } else { |
506 | uint16_t vecIndex=UTRIE2_GET16(&propsVectorsTrie, c); |
507 | return propsVectors[vecIndex+column]; |
508 | } |
509 | } |
510 | |
511 | U_CFUNC int32_t |
512 | uprv_getMaxValues(int32_t column) { |
513 | switch(column) { |
514 | case 0: |
515 | return indexes[UPROPS_MAX_VALUES_INDEX]; |
516 | case 2: |
517 | return indexes[UPROPS_MAX_VALUES_2_INDEX]; |
518 | default: |
519 | return 0; |
520 | } |
521 | } |
522 | |
523 | U_CAPI void U_EXPORT2 |
524 | u_charAge(UChar32 c, UVersionInfo versionArray) { |
525 | if(versionArray!=nullptr) { |
526 | uint32_t version=u_getUnicodeProperties(c, 0)>>UPROPS_AGE_SHIFT; |
527 | versionArray[0]=(uint8_t)(version>>4); |
528 | versionArray[1]=(uint8_t)(version&0xf); |
529 | versionArray[2]=versionArray[3]=0; |
530 | } |
531 | } |
532 | |
533 | U_CAPI UScriptCode U_EXPORT2 |
534 | uscript_getScript(UChar32 c, UErrorCode *pErrorCode) { |
535 | if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) { |
536 | return USCRIPT_INVALID_CODE; |
537 | } |
538 | if((uint32_t)c>0x10ffff) { |
539 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
540 | return USCRIPT_INVALID_CODE; |
541 | } |
542 | uint32_t scriptX=u_getUnicodeProperties(c, 0)&UPROPS_SCRIPT_X_MASK; |
543 | uint32_t codeOrIndex=uprops_mergeScriptCodeOrIndex(scriptX); |
544 | if(scriptX<UPROPS_SCRIPT_X_WITH_COMMON) { |
545 | return (UScriptCode)codeOrIndex; |
546 | } else if(scriptX<UPROPS_SCRIPT_X_WITH_INHERITED) { |
547 | return USCRIPT_COMMON; |
548 | } else if(scriptX<UPROPS_SCRIPT_X_WITH_OTHER) { |
549 | return USCRIPT_INHERITED; |
550 | } else { |
551 | return (UScriptCode)scriptExtensions[codeOrIndex]; |
552 | } |
553 | } |
554 | |
555 | U_CAPI UBool U_EXPORT2 |
556 | uscript_hasScript(UChar32 c, UScriptCode sc) UPRV_NO_SANITIZE_UNDEFINED { |
557 | uint32_t scriptX=u_getUnicodeProperties(c, 0)&UPROPS_SCRIPT_X_MASK; |
558 | uint32_t codeOrIndex=uprops_mergeScriptCodeOrIndex(scriptX); |
559 | if(scriptX<UPROPS_SCRIPT_X_WITH_COMMON) { |
560 | return sc==(UScriptCode)codeOrIndex; |
561 | } |
562 | |
563 | const uint16_t *scx=scriptExtensions+codeOrIndex; |
564 | if(scriptX>=UPROPS_SCRIPT_X_WITH_OTHER) { |
565 | scx=scriptExtensions+scx[1]; |
566 | } |
567 | uint32_t sc32=sc; |
568 | if(sc32>0x7fff) { |
569 | /* Guard against bogus input that would make us go past the Script_Extensions terminator. */ |
570 | return false; |
571 | } |
572 | while(sc32>*scx) { |
573 | ++scx; |
574 | } |
575 | return sc32==(*scx&0x7fff); |
576 | } |
577 | |
578 | U_CAPI int32_t U_EXPORT2 |
579 | uscript_getScriptExtensions(UChar32 c, |
580 | UScriptCode *scripts, int32_t capacity, |
581 | UErrorCode *pErrorCode) { |
582 | if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) { |
583 | return 0; |
584 | } |
585 | if(capacity<0 || (capacity>0 && scripts==nullptr)) { |
586 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
587 | return 0; |
588 | } |
589 | uint32_t scriptX=u_getUnicodeProperties(c, 0)&UPROPS_SCRIPT_X_MASK; |
590 | uint32_t codeOrIndex=uprops_mergeScriptCodeOrIndex(scriptX); |
591 | if(scriptX<UPROPS_SCRIPT_X_WITH_COMMON) { |
592 | if(capacity==0) { |
593 | *pErrorCode=U_BUFFER_OVERFLOW_ERROR; |
594 | } else { |
595 | scripts[0]=(UScriptCode)codeOrIndex; |
596 | } |
597 | return 1; |
598 | } |
599 | |
600 | const uint16_t *scx=scriptExtensions+codeOrIndex; |
601 | if(scriptX>=UPROPS_SCRIPT_X_WITH_OTHER) { |
602 | scx=scriptExtensions+scx[1]; |
603 | } |
604 | int32_t length=0; |
605 | uint16_t sx; |
606 | do { |
607 | sx=*scx++; |
608 | if(length<capacity) { |
609 | scripts[length]=(UScriptCode)(sx&0x7fff); |
610 | } |
611 | ++length; |
612 | } while(sx<0x8000); |
613 | if(length>capacity) { |
614 | *pErrorCode=U_BUFFER_OVERFLOW_ERROR; |
615 | } |
616 | return length; |
617 | } |
618 | |
619 | U_CAPI UBlockCode U_EXPORT2 |
620 | ublock_getCode(UChar32 c) { |
621 | return (UBlockCode)((u_getUnicodeProperties(c, 0)&UPROPS_BLOCK_MASK)>>UPROPS_BLOCK_SHIFT); |
622 | } |
623 | |
624 | /* property starts for UnicodeSet ------------------------------------------- */ |
625 | |
626 | static UBool U_CALLCONV |
627 | _enumPropertyStartsRange(const void *context, UChar32 start, UChar32 end, uint32_t value) { |
628 | /* add the start code point to the USet */ |
629 | const USetAdder *sa=(const USetAdder *)context; |
630 | sa->add(sa->set, start); |
631 | (void)end; |
632 | (void)value; |
633 | return true; |
634 | } |
635 | |
636 | #define USET_ADD_CP_AND_NEXT(sa, cp) sa->add(sa->set, cp); sa->add(sa->set, cp+1) |
637 | |
638 | U_CFUNC void U_EXPORT2 |
639 | uchar_addPropertyStarts(const USetAdder *sa, UErrorCode *pErrorCode) { |
640 | if(U_FAILURE(*pErrorCode)) { |
641 | return; |
642 | } |
643 | |
644 | /* add the start code point of each same-value range of the main trie */ |
645 | utrie2_enum(&propsTrie, nullptr, _enumPropertyStartsRange, sa); |
646 | |
647 | /* add code points with hardcoded properties, plus the ones following them */ |
648 | |
649 | /* add for u_isblank() */ |
650 | USET_ADD_CP_AND_NEXT(sa, TAB); |
651 | |
652 | /* add for IS_THAT_CONTROL_SPACE() */ |
653 | sa->add(sa->set, CR+1); /* range TAB..CR */ |
654 | sa->add(sa->set, 0x1c); |
655 | sa->add(sa->set, 0x1f+1); |
656 | USET_ADD_CP_AND_NEXT(sa, 0x85); // NEXT LINE (NEL) |
657 | |
658 | /* add for u_isIDIgnorable() what was not added above */ |
659 | sa->add(sa->set, 0x7f); /* range DEL..NBSP-1, NBSP added below */ |
660 | sa->add(sa->set, HAIRSP); |
661 | sa->add(sa->set, RLM+1); |
662 | sa->add(sa->set, 0x206a); // INHIBIT SYMMETRIC SWAPPING |
663 | sa->add(sa->set, 0x206f+1); // NOMINAL DIGIT SHAPES |
664 | USET_ADD_CP_AND_NEXT(sa, ZWNBSP); |
665 | |
666 | /* add no-break spaces for u_isWhitespace() what was not added above */ |
667 | USET_ADD_CP_AND_NEXT(sa, NBSP); |
668 | USET_ADD_CP_AND_NEXT(sa, FIGURESP); |
669 | USET_ADD_CP_AND_NEXT(sa, NNBSP); |
670 | |
671 | /* add for u_digit() */ |
672 | sa->add(sa->set, u'a'); |
673 | sa->add(sa->set, u'z'+1); |
674 | sa->add(sa->set, u'A'); |
675 | sa->add(sa->set, u'Z'+1); |
676 | // fullwidth |
677 | sa->add(sa->set, u'a'); |
678 | sa->add(sa->set, u'z'+1); |
679 | sa->add(sa->set, u'A'); |
680 | sa->add(sa->set, u'Z'+1); |
681 | |
682 | /* add for u_isxdigit() */ |
683 | sa->add(sa->set, u'f'+1); |
684 | sa->add(sa->set, u'F'+1); |
685 | // fullwidth |
686 | sa->add(sa->set, u'f'+1); |
687 | sa->add(sa->set, u'F'+1); |
688 | |
689 | /* add for UCHAR_DEFAULT_IGNORABLE_CODE_POINT what was not added above */ |
690 | sa->add(sa->set, 0x2060); /* range 2060..206f */ |
691 | sa->add(sa->set, 0xfff0); |
692 | sa->add(sa->set, 0xfffb+1); |
693 | sa->add(sa->set, 0xe0000); |
694 | sa->add(sa->set, 0xe0fff+1); |
695 | |
696 | /* add for UCHAR_GRAPHEME_BASE and others */ |
697 | USET_ADD_CP_AND_NEXT(sa, CGJ); |
698 | } |
699 | |
700 | U_CFUNC void U_EXPORT2 |
701 | upropsvec_addPropertyStarts(const USetAdder *sa, UErrorCode *pErrorCode) { |
702 | if(U_FAILURE(*pErrorCode)) { |
703 | return; |
704 | } |
705 | |
706 | /* add the start code point of each same-value range of the properties vectors trie */ |
707 | utrie2_enum(&propsVectorsTrie, nullptr, _enumPropertyStartsRange, sa); |
708 | } |
709 | |