1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ********************************************************************************
5 * Copyright (C) 1996-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 ********************************************************************************
11 * Modification History:
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 ******************************************************************************
24 #include "unicode/utypes.h"
25 #include "unicode/uchar.h"
26 #include "unicode/uscript.h"
27 #include "unicode/udata.h"
36 /* uchar_props_data.h is machine-generated by genprops --csource */
37 #define INCLUDED_FROM_UCHAR_C
38 #include "uchar_props_data.h"
40 /* constants and macros for access to the data ------------------------------ */
42 /* getting a uint32_t properties word from the data */
43 #define GET_PROPS(c, result) ((result)=UTRIE2_GET16(&propsTrie, c));
46 uprv_haveProperties(UErrorCode
*pErrorCode
) {
47 if(U_FAILURE(*pErrorCode
)) {
53 /* API functions ------------------------------------------------------------ */
55 /* Gets the Unicode character's general category.*/
56 U_CAPI
int8_t U_EXPORT2
57 u_charType(UChar32 c
) {
60 return (int8_t)GET_CATEGORY(props
);
63 /* Enumerate all code points with their general categories. */
64 struct _EnumTypeCallback
{
65 UCharEnumTypeRange
*enumRange
;
69 static uint32_t U_CALLCONV
70 _enumTypeValue(const void *context
, uint32_t value
) {
72 return GET_CATEGORY(value
);
75 static UBool U_CALLCONV
76 _enumTypeRange(const void *context
, UChar32 start
, UChar32 end
, uint32_t value
) {
77 /* just cast the value to UCharCategory */
78 return ((struct _EnumTypeCallback
*)context
)->
79 enumRange(((struct _EnumTypeCallback
*)context
)->context
,
80 start
, end
+1, (UCharCategory
)value
);
84 u_enumCharTypes(UCharEnumTypeRange
*enumRange
, const void *context
) {
85 struct _EnumTypeCallback callback
;
91 callback
.enumRange
=enumRange
;
92 callback
.context
=context
;
93 utrie2_enum(&propsTrie
, _enumTypeValue
, _enumTypeRange
, &callback
);
96 /* Checks if ch is a lower case letter.*/
97 U_CAPI UBool U_EXPORT2
98 u_islower(UChar32 c
) {
101 return (UBool
)(GET_CATEGORY(props
)==U_LOWERCASE_LETTER
);
104 /* Checks if ch is an upper case letter.*/
105 U_CAPI UBool U_EXPORT2
106 u_isupper(UChar32 c
) {
109 return (UBool
)(GET_CATEGORY(props
)==U_UPPERCASE_LETTER
);
112 /* Checks if ch is a title case letter; usually upper case letters.*/
113 U_CAPI UBool U_EXPORT2
114 u_istitle(UChar32 c
) {
117 return (UBool
)(GET_CATEGORY(props
)==U_TITLECASE_LETTER
);
120 /* Checks if ch is a decimal digit. */
121 U_CAPI UBool U_EXPORT2
122 u_isdigit(UChar32 c
) {
125 return (UBool
)(GET_CATEGORY(props
)==U_DECIMAL_DIGIT_NUMBER
);
128 U_CAPI UBool U_EXPORT2
129 u_isxdigit(UChar32 c
) {
132 /* check ASCII and Fullwidth ASCII a-fA-F */
134 (c
<=0x66 && c
>=0x41 && (c
<=0x46 || c
>=0x61)) ||
135 (c
>=0xff21 && c
<=0xff46 && (c
<=0xff26 || c
>=0xff41))
141 return (UBool
)(GET_CATEGORY(props
)==U_DECIMAL_DIGIT_NUMBER
);
144 /* Checks if the Unicode character is a letter.*/
145 U_CAPI UBool U_EXPORT2
146 u_isalpha(UChar32 c
) {
149 return (UBool
)((CAT_MASK(props
)&U_GC_L_MASK
)!=0);
152 U_CAPI UBool U_EXPORT2
153 u_isUAlphabetic(UChar32 c
) {
154 return (u_getUnicodeProperties(c
, 1)&U_MASK(UPROPS_ALPHABETIC
))!=0;
157 /* Checks if c is a letter or a decimal digit */
158 U_CAPI UBool U_EXPORT2
159 u_isalnum(UChar32 c
) {
162 return (UBool
)((CAT_MASK(props
)&(U_GC_L_MASK
|U_GC_ND_MASK
))!=0);
166 * Checks if c is alphabetic, or a decimal digit; implements UCHAR_POSIX_ALNUM.
170 u_isalnumPOSIX(UChar32 c
) {
171 return (UBool
)(u_isUAlphabetic(c
) || u_isdigit(c
));
174 /* Checks if ch is a unicode character with assigned character type.*/
175 U_CAPI UBool U_EXPORT2
176 u_isdefined(UChar32 c
) {
179 return (UBool
)(GET_CATEGORY(props
)!=0);
182 /* Checks if the Unicode character is a base form character that can take a diacritic.*/
183 U_CAPI UBool U_EXPORT2
184 u_isbase(UChar32 c
) {
187 return (UBool
)((CAT_MASK(props
)&(U_GC_L_MASK
|U_GC_N_MASK
|U_GC_MC_MASK
|U_GC_ME_MASK
))!=0);
190 /* Checks if the Unicode character is a control character.*/
191 U_CAPI UBool U_EXPORT2
192 u_iscntrl(UChar32 c
) {
195 return (UBool
)((CAT_MASK(props
)&(U_GC_CC_MASK
|U_GC_CF_MASK
|U_GC_ZL_MASK
|U_GC_ZP_MASK
))!=0);
198 U_CAPI UBool U_EXPORT2
199 u_isISOControl(UChar32 c
) {
200 return (uint32_t)c
<=0x9f && (c
<=0x1f || c
>=0x7f);
203 /* Some control characters that are used as space. */
204 #define IS_THAT_CONTROL_SPACE(c) \
205 (c<=0x9f && ((c>=TAB && c<=CR) || (c>=0x1c && c <=0x1f) || c==NL))
207 /* Java has decided that U+0085 New Line is not whitespace any more. */
208 #define IS_THAT_ASCII_CONTROL_SPACE(c) \
209 (c<=0x1f && c>=TAB && (c<=CR || c>=0x1c))
211 /* Checks if the Unicode character is a space character.*/
212 U_CAPI UBool U_EXPORT2
213 u_isspace(UChar32 c
) {
216 return (UBool
)((CAT_MASK(props
)&U_GC_Z_MASK
)!=0 || IS_THAT_CONTROL_SPACE(c
));
219 U_CAPI UBool U_EXPORT2
220 u_isJavaSpaceChar(UChar32 c
) {
223 return (UBool
)((CAT_MASK(props
)&U_GC_Z_MASK
)!=0);
226 /* Checks if the Unicode character is a whitespace character.*/
227 U_CAPI UBool U_EXPORT2
228 u_isWhitespace(UChar32 c
) {
232 ((CAT_MASK(props
)&U_GC_Z_MASK
)!=0 &&
233 c
!=NBSP
&& c
!=FIGURESP
&& c
!=NNBSP
) || /* exclude no-break spaces */
234 IS_THAT_ASCII_CONTROL_SPACE(c
)
238 U_CAPI UBool U_EXPORT2
239 u_isblank(UChar32 c
) {
240 if((uint32_t)c
<=0x9f) {
241 return c
==9 || c
==0x20; /* TAB or SPACE */
246 return (UBool
)(GET_CATEGORY(props
)==U_SPACE_SEPARATOR
);
250 U_CAPI UBool U_EXPORT2
251 u_isUWhiteSpace(UChar32 c
) {
252 return (u_getUnicodeProperties(c
, 1)&U_MASK(UPROPS_WHITE_SPACE
))!=0;
255 /* Checks if the Unicode character is printable.*/
256 U_CAPI UBool U_EXPORT2
257 u_isprint(UChar32 c
) {
260 /* comparing ==0 returns FALSE for the categories mentioned */
261 return (UBool
)((CAT_MASK(props
)&U_GC_C_MASK
)==0);
265 * Checks if c is in \p{graph}\p{blank} - \p{cntrl}.
266 * Implements UCHAR_POSIX_PRINT.
270 u_isprintPOSIX(UChar32 c
) {
274 * The only cntrl character in graph+blank is TAB (in blank).
275 * Here we implement (blank-TAB)=Zs instead of calling u_isblank().
277 return (UBool
)((GET_CATEGORY(props
)==U_SPACE_SEPARATOR
) || u_isgraphPOSIX(c
));
280 U_CAPI UBool U_EXPORT2
281 u_isgraph(UChar32 c
) {
284 /* comparing ==0 returns FALSE for the categories mentioned */
285 return (UBool
)((CAT_MASK(props
)&
286 (U_GC_CC_MASK
|U_GC_CF_MASK
|U_GC_CS_MASK
|U_GC_CN_MASK
|U_GC_Z_MASK
))
292 * [^\p{space}\p{gc=Control}\p{gc=Surrogate}\p{gc=Unassigned}]
293 * with space=\p{Whitespace} and Control=Cc.
294 * Implements UCHAR_POSIX_GRAPH.
298 u_isgraphPOSIX(UChar32 c
) {
301 /* \p{space}\p{gc=Control} == \p{gc=Z}\p{Control} */
302 /* comparing ==0 returns FALSE for the categories mentioned */
303 return (UBool
)((CAT_MASK(props
)&
304 (U_GC_CC_MASK
|U_GC_CS_MASK
|U_GC_CN_MASK
|U_GC_Z_MASK
))
308 U_CAPI UBool U_EXPORT2
309 u_ispunct(UChar32 c
) {
312 return (UBool
)((CAT_MASK(props
)&U_GC_P_MASK
)!=0);
315 /* Checks if the Unicode character can start a Unicode identifier.*/
316 U_CAPI UBool U_EXPORT2
317 u_isIDStart(UChar32 c
) {
318 /* same as u_isalpha() */
321 return (UBool
)((CAT_MASK(props
)&(U_GC_L_MASK
|U_GC_NL_MASK
))!=0);
324 /* Checks if the Unicode character can be a Unicode identifier part other than starting the
326 U_CAPI UBool U_EXPORT2
327 u_isIDPart(UChar32 c
) {
332 (U_GC_ND_MASK
|U_GC_NL_MASK
|
334 U_GC_PC_MASK
|U_GC_MC_MASK
|U_GC_MN_MASK
)
339 /*Checks if the Unicode character can be ignorable in a Java or Unicode identifier.*/
340 U_CAPI UBool U_EXPORT2
341 u_isIDIgnorable(UChar32 c
) {
343 return u_isISOControl(c
) && !IS_THAT_ASCII_CONTROL_SPACE(c
);
347 return (UBool
)(GET_CATEGORY(props
)==U_FORMAT_CHAR
);
351 /*Checks if the Unicode character can start a Java identifier.*/
352 U_CAPI UBool U_EXPORT2
353 u_isJavaIDStart(UChar32 c
) {
356 return (UBool
)((CAT_MASK(props
)&(U_GC_L_MASK
|U_GC_SC_MASK
|U_GC_PC_MASK
))!=0);
359 /*Checks if the Unicode character can be a Java identifier part other than starting the
362 U_CAPI UBool U_EXPORT2
363 u_isJavaIDPart(UChar32 c
) {
368 (U_GC_ND_MASK
|U_GC_NL_MASK
|
370 U_GC_SC_MASK
|U_GC_PC_MASK
|
371 U_GC_MC_MASK
|U_GC_MN_MASK
)
376 U_CAPI
int32_t U_EXPORT2
377 u_charDigitValue(UChar32 c
) {
381 value
=(int32_t)GET_NUMERIC_TYPE_VALUE(props
)-UPROPS_NTV_DECIMAL_START
;
389 U_CAPI
double U_EXPORT2
390 u_getNumericValue(UChar32 c
) {
394 ntv
=(int32_t)GET_NUMERIC_TYPE_VALUE(props
);
396 if(ntv
==UPROPS_NTV_NONE
) {
397 return U_NO_NUMERIC_VALUE
;
398 } else if(ntv
<UPROPS_NTV_DIGIT_START
) {
400 return ntv
-UPROPS_NTV_DECIMAL_START
;
401 } else if(ntv
<UPROPS_NTV_NUMERIC_START
) {
403 return ntv
-UPROPS_NTV_DIGIT_START
;
404 } else if(ntv
<UPROPS_NTV_FRACTION_START
) {
406 return ntv
-UPROPS_NTV_NUMERIC_START
;
407 } else if(ntv
<UPROPS_NTV_LARGE_START
) {
409 int32_t numerator
=(ntv
>>4)-12;
410 int32_t denominator
=(ntv
&0xf)+1;
411 return (double)numerator
/denominator
;
412 } else if(ntv
<UPROPS_NTV_BASE60_START
) {
413 /* large, single-significant-digit integer */
415 int32_t mant
=(ntv
>>5)-14;
416 int32_t exp
=(ntv
&0x1f)+2;
419 /* multiply by 10^exp without math.h */
440 } else if(ntv
<UPROPS_NTV_FRACTION20_START
) {
441 /* sexagesimal (base 60) integer */
442 int32_t numValue
=(ntv
>>2)-0xbf;
443 int32_t exp
=(ntv
&3)+1;
447 numValue
*=60*60*60*60;
464 } else if(ntv
<UPROPS_NTV_RESERVED_START
) {
465 // fraction-20 e.g. 3/80
466 int32_t frac20
=ntv
-UPROPS_NTV_FRACTION20_START
; // 0..0x17
467 int32_t numerator
=2*(frac20
&3)+1;
468 int32_t denominator
=20<<(frac20
>>2);
469 return (double)numerator
/denominator
;
472 return U_NO_NUMERIC_VALUE
;
476 U_CAPI
int32_t U_EXPORT2
477 u_digit(UChar32 ch
, int8_t radix
) {
479 if((uint8_t)(radix
-2)<=(36-2)) {
480 value
=(int8_t)u_charDigitValue(ch
);
482 /* ch is not a decimal digit, try latin letters */
483 if(ch
>=0x61 && ch
<=0x7A) {
484 value
=(int8_t)(ch
-0x57); /* ch - 'a' + 10 */
485 } else if(ch
>=0x41 && ch
<=0x5A) {
486 value
=(int8_t)(ch
-0x37); /* ch - 'A' + 10 */
487 } else if(ch
>=0xFF41 && ch
<=0xFF5A) {
488 value
=(int8_t)(ch
-0xFF37); /* fullwidth ASCII a-z */
489 } else if(ch
>=0xFF21 && ch
<=0xFF3A) {
490 value
=(int8_t)(ch
-0xFF17); /* fullwidth ASCII A-Z */
494 value
=-1; /* invalid radix */
496 return (int8_t)((value
<radix
) ? value
: -1);
499 U_CAPI UChar32 U_EXPORT2
500 u_forDigit(int32_t digit
, int8_t radix
) {
501 if((uint8_t)(radix
-2)>(36-2) || (uint32_t)digit
>=(uint32_t)radix
) {
503 } else if(digit
<10) {
504 return (UChar32
)(0x30+digit
);
506 return (UChar32
)((0x61-10)+digit
);
510 /* miscellaneous, and support for uprops.cpp -------------------------------- */
512 U_CAPI
void U_EXPORT2
513 u_getUnicodeVersion(UVersionInfo versionArray
) {
514 if(versionArray
!=NULL
) {
515 uprv_memcpy(versionArray
, dataVersion
, U_MAX_VERSION_LENGTH
);
520 u_getMainProperties(UChar32 c
) {
527 u_getUnicodeProperties(UChar32 c
, int32_t column
) {
529 if(column
>=propsVectorsColumns
) {
532 uint16_t vecIndex
=UTRIE2_GET16(&propsVectorsTrie
, c
);
533 return propsVectors
[vecIndex
+column
];
538 uprv_getMaxValues(int32_t column
) {
541 return indexes
[UPROPS_MAX_VALUES_INDEX
];
543 return indexes
[UPROPS_MAX_VALUES_2_INDEX
];
549 U_CAPI
void U_EXPORT2
550 u_charAge(UChar32 c
, UVersionInfo versionArray
) {
551 if(versionArray
!=NULL
) {
552 uint32_t version
=u_getUnicodeProperties(c
, 0)>>UPROPS_AGE_SHIFT
;
553 versionArray
[0]=(uint8_t)(version
>>4);
554 versionArray
[1]=(uint8_t)(version
&0xf);
555 versionArray
[2]=versionArray
[3]=0;
559 U_CAPI UScriptCode U_EXPORT2
560 uscript_getScript(UChar32 c
, UErrorCode
*pErrorCode
) {
562 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
563 return USCRIPT_INVALID_CODE
;
565 if((uint32_t)c
>0x10ffff) {
566 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
567 return USCRIPT_INVALID_CODE
;
569 scriptX
=u_getUnicodeProperties(c
, 0)&UPROPS_SCRIPT_X_MASK
;
570 if(scriptX
<UPROPS_SCRIPT_X_WITH_COMMON
) {
571 return (UScriptCode
)scriptX
;
572 } else if(scriptX
<UPROPS_SCRIPT_X_WITH_INHERITED
) {
573 return USCRIPT_COMMON
;
574 } else if(scriptX
<UPROPS_SCRIPT_X_WITH_OTHER
) {
575 return USCRIPT_INHERITED
;
577 return (UScriptCode
)scriptExtensions
[scriptX
&UPROPS_SCRIPT_MASK
];
581 U_CAPI UBool U_EXPORT2
582 uscript_hasScript(UChar32 c
, UScriptCode sc
) {
584 uint32_t scriptX
=u_getUnicodeProperties(c
, 0)&UPROPS_SCRIPT_X_MASK
;
585 if(scriptX
<UPROPS_SCRIPT_X_WITH_COMMON
) {
586 return sc
==(UScriptCode
)scriptX
;
589 scx
=scriptExtensions
+(scriptX
&UPROPS_SCRIPT_MASK
);
590 if(scriptX
>=UPROPS_SCRIPT_X_WITH_OTHER
) {
591 scx
=scriptExtensions
+scx
[1];
593 if(sc
>=USCRIPT_CODE_LIMIT
) {
594 /* Guard against bogus input that would make us go past the Script_Extensions terminator. */
600 return sc
==(*scx
&0x7fff);
603 U_CAPI
int32_t U_EXPORT2
604 uscript_getScriptExtensions(UChar32 c
,
605 UScriptCode
*scripts
, int32_t capacity
,
606 UErrorCode
*pErrorCode
) {
611 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
614 if(capacity
<0 || (capacity
>0 && scripts
==NULL
)) {
615 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
618 scriptX
=u_getUnicodeProperties(c
, 0)&UPROPS_SCRIPT_X_MASK
;
619 if(scriptX
<UPROPS_SCRIPT_X_WITH_COMMON
) {
621 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
623 scripts
[0]=(UScriptCode
)scriptX
;
628 scx
=scriptExtensions
+(scriptX
&UPROPS_SCRIPT_MASK
);
629 if(scriptX
>=UPROPS_SCRIPT_X_WITH_OTHER
) {
630 scx
=scriptExtensions
+scx
[1];
635 if(length
<capacity
) {
636 scripts
[length
]=(UScriptCode
)(sx
&0x7fff);
640 if(length
>capacity
) {
641 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
646 U_CAPI UBlockCode U_EXPORT2
647 ublock_getCode(UChar32 c
) {
648 return (UBlockCode
)((u_getUnicodeProperties(c
, 0)&UPROPS_BLOCK_MASK
)>>UPROPS_BLOCK_SHIFT
);
651 /* property starts for UnicodeSet ------------------------------------------- */
653 static UBool U_CALLCONV
654 _enumPropertyStartsRange(const void *context
, UChar32 start
, UChar32 end
, uint32_t value
) {
655 /* add the start code point to the USet */
656 const USetAdder
*sa
=(const USetAdder
*)context
;
657 sa
->add(sa
->set
, start
);
663 #define USET_ADD_CP_AND_NEXT(sa, cp) sa->add(sa->set, cp); sa->add(sa->set, cp+1)
665 U_CFUNC
void U_EXPORT2
666 uchar_addPropertyStarts(const USetAdder
*sa
, UErrorCode
*pErrorCode
) {
667 if(U_FAILURE(*pErrorCode
)) {
671 /* add the start code point of each same-value range of the main trie */
672 utrie2_enum(&propsTrie
, NULL
, _enumPropertyStartsRange
, sa
);
674 /* add code points with hardcoded properties, plus the ones following them */
676 /* add for u_isblank() */
677 USET_ADD_CP_AND_NEXT(sa
, TAB
);
679 /* add for IS_THAT_CONTROL_SPACE() */
680 sa
->add(sa
->set
, CR
+1); /* range TAB..CR */
681 sa
->add(sa
->set
, 0x1c);
682 sa
->add(sa
->set
, 0x1f+1);
683 USET_ADD_CP_AND_NEXT(sa
, NL
);
685 /* add for u_isIDIgnorable() what was not added above */
686 sa
->add(sa
->set
, DEL
); /* range DEL..NBSP-1, NBSP added below */
687 sa
->add(sa
->set
, HAIRSP
);
688 sa
->add(sa
->set
, RLM
+1);
689 sa
->add(sa
->set
, INHSWAP
);
690 sa
->add(sa
->set
, NOMDIG
+1);
691 USET_ADD_CP_AND_NEXT(sa
, ZWNBSP
);
693 /* add no-break spaces for u_isWhitespace() what was not added above */
694 USET_ADD_CP_AND_NEXT(sa
, NBSP
);
695 USET_ADD_CP_AND_NEXT(sa
, FIGURESP
);
696 USET_ADD_CP_AND_NEXT(sa
, NNBSP
);
698 /* add for u_digit() */
699 sa
->add(sa
->set
, U_a
);
700 sa
->add(sa
->set
, U_z
+1);
701 sa
->add(sa
->set
, U_A
);
702 sa
->add(sa
->set
, U_Z
+1);
703 sa
->add(sa
->set
, U_FW_a
);
704 sa
->add(sa
->set
, U_FW_z
+1);
705 sa
->add(sa
->set
, U_FW_A
);
706 sa
->add(sa
->set
, U_FW_Z
+1);
708 /* add for u_isxdigit() */
709 sa
->add(sa
->set
, U_f
+1);
710 sa
->add(sa
->set
, U_F
+1);
711 sa
->add(sa
->set
, U_FW_f
+1);
712 sa
->add(sa
->set
, U_FW_F
+1);
714 /* add for UCHAR_DEFAULT_IGNORABLE_CODE_POINT what was not added above */
715 sa
->add(sa
->set
, WJ
); /* range WJ..NOMDIG */
716 sa
->add(sa
->set
, 0xfff0);
717 sa
->add(sa
->set
, 0xfffb+1);
718 sa
->add(sa
->set
, 0xe0000);
719 sa
->add(sa
->set
, 0xe0fff+1);
721 /* add for UCHAR_GRAPHEME_BASE and others */
722 USET_ADD_CP_AND_NEXT(sa
, CGJ
);
725 U_CFUNC
void U_EXPORT2
726 upropsvec_addPropertyStarts(const USetAdder
*sa
, UErrorCode
*pErrorCode
) {
727 if(U_FAILURE(*pErrorCode
)) {
731 /* add the start code point of each same-value range of the properties vectors trie */
732 if(propsVectorsColumns
>0) {
733 /* if propsVectorsColumns==0 then the properties vectors trie may not be there at all */
734 utrie2_enum(&propsVectorsTrie
, NULL
, _enumPropertyStartsRange
, sa
);