2 ********************************************************************************
3 * Copyright (C) 1996-2012, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ********************************************************************************
9 * Modification History:
11 * Date Name Description
12 * 04/02/97 aliu Creation.
13 * 4/15/99 Madhu Updated all the function definitions for C Implementation
14 * 5/20/99 Madhu Added the function u_getVersion()
15 * 8/19/1999 srl Upgraded scripts to Unicode3.0
16 * 11/11/1999 weiv added u_isalnum(), cleaned comments
17 * 01/11/2000 helena Renamed u_getVersion to u_getUnicodeVersion.
18 * 06/20/2000 helena OS/400 port changes; mostly typecast.
19 ******************************************************************************
22 #include "unicode/utypes.h"
23 #include "unicode/uchar.h"
24 #include "unicode/uscript.h"
25 #include "unicode/udata.h"
34 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
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
) {
71 return GET_CATEGORY(value
);
74 static UBool U_CALLCONV
75 _enumTypeRange(const void *context
, UChar32 start
, UChar32 end
, uint32_t value
) {
76 /* just cast the value to UCharCategory */
77 return ((struct _EnumTypeCallback
*)context
)->
78 enumRange(((struct _EnumTypeCallback
*)context
)->context
,
79 start
, end
+1, (UCharCategory
)value
);
83 u_enumCharTypes(UCharEnumTypeRange
*enumRange
, const void *context
) {
84 struct _EnumTypeCallback callback
;
90 callback
.enumRange
=enumRange
;
91 callback
.context
=context
;
92 utrie2_enum(&propsTrie
, _enumTypeValue
, _enumTypeRange
, &callback
);
95 /* Checks if ch is a lower case letter.*/
96 U_CAPI UBool U_EXPORT2
97 u_islower(UChar32 c
) {
100 return (UBool
)(GET_CATEGORY(props
)==U_LOWERCASE_LETTER
);
103 /* Checks if ch is an upper case letter.*/
104 U_CAPI UBool U_EXPORT2
105 u_isupper(UChar32 c
) {
108 return (UBool
)(GET_CATEGORY(props
)==U_UPPERCASE_LETTER
);
111 /* Checks if ch is a title case letter; usually upper case letters.*/
112 U_CAPI UBool U_EXPORT2
113 u_istitle(UChar32 c
) {
116 return (UBool
)(GET_CATEGORY(props
)==U_TITLECASE_LETTER
);
119 /* Checks if ch is a decimal digit. */
120 U_CAPI UBool U_EXPORT2
121 u_isdigit(UChar32 c
) {
124 return (UBool
)(GET_CATEGORY(props
)==U_DECIMAL_DIGIT_NUMBER
);
127 U_CAPI UBool U_EXPORT2
128 u_isxdigit(UChar32 c
) {
131 /* check ASCII and Fullwidth ASCII a-fA-F */
133 (c
<=0x66 && c
>=0x41 && (c
<=0x46 || c
>=0x61)) ||
134 (c
>=0xff21 && c
<=0xff46 && (c
<=0xff26 || c
>=0xff41))
140 return (UBool
)(GET_CATEGORY(props
)==U_DECIMAL_DIGIT_NUMBER
);
143 /* Checks if the Unicode character is a letter.*/
144 U_CAPI UBool U_EXPORT2
145 u_isalpha(UChar32 c
) {
148 return (UBool
)((CAT_MASK(props
)&U_GC_L_MASK
)!=0);
151 U_CAPI UBool U_EXPORT2
152 u_isUAlphabetic(UChar32 c
) {
153 return (u_getUnicodeProperties(c
, 1)&U_MASK(UPROPS_ALPHABETIC
))!=0;
156 /* Checks if c is a letter or a decimal digit */
157 U_CAPI UBool U_EXPORT2
158 u_isalnum(UChar32 c
) {
161 return (UBool
)((CAT_MASK(props
)&(U_GC_L_MASK
|U_GC_ND_MASK
))!=0);
165 * Checks if c is alphabetic, or a decimal digit; implements UCHAR_POSIX_ALNUM.
169 u_isalnumPOSIX(UChar32 c
) {
170 return (UBool
)(u_isUAlphabetic(c
) || u_isdigit(c
));
173 /* Checks if ch is a unicode character with assigned character type.*/
174 U_CAPI UBool U_EXPORT2
175 u_isdefined(UChar32 c
) {
178 return (UBool
)(GET_CATEGORY(props
)!=0);
181 /* Checks if the Unicode character is a base form character that can take a diacritic.*/
182 U_CAPI UBool U_EXPORT2
183 u_isbase(UChar32 c
) {
186 return (UBool
)((CAT_MASK(props
)&(U_GC_L_MASK
|U_GC_N_MASK
|U_GC_MC_MASK
|U_GC_ME_MASK
))!=0);
189 /* Checks if the Unicode character is a control character.*/
190 U_CAPI UBool U_EXPORT2
191 u_iscntrl(UChar32 c
) {
194 return (UBool
)((CAT_MASK(props
)&(U_GC_CC_MASK
|U_GC_CF_MASK
|U_GC_ZL_MASK
|U_GC_ZP_MASK
))!=0);
197 U_CAPI UBool U_EXPORT2
198 u_isISOControl(UChar32 c
) {
199 return (uint32_t)c
<=0x9f && (c
<=0x1f || c
>=0x7f);
202 /* Some control characters that are used as space. */
203 #define IS_THAT_CONTROL_SPACE(c) \
204 (c<=0x9f && ((c>=TAB && c<=CR) || (c>=0x1c && c <=0x1f) || c==NL))
206 /* Java has decided that U+0085 New Line is not whitespace any more. */
207 #define IS_THAT_ASCII_CONTROL_SPACE(c) \
208 (c<=0x1f && c>=TAB && (c<=CR || c>=0x1c))
210 /* Checks if the Unicode character is a space character.*/
211 U_CAPI UBool U_EXPORT2
212 u_isspace(UChar32 c
) {
215 return (UBool
)((CAT_MASK(props
)&U_GC_Z_MASK
)!=0 || IS_THAT_CONTROL_SPACE(c
));
218 U_CAPI UBool U_EXPORT2
219 u_isJavaSpaceChar(UChar32 c
) {
222 return (UBool
)((CAT_MASK(props
)&U_GC_Z_MASK
)!=0);
225 /* Checks if the Unicode character is a whitespace character.*/
226 U_CAPI UBool U_EXPORT2
227 u_isWhitespace(UChar32 c
) {
231 ((CAT_MASK(props
)&U_GC_Z_MASK
)!=0 &&
232 c
!=NBSP
&& c
!=FIGURESP
&& c
!=NNBSP
) || /* exclude no-break spaces */
233 IS_THAT_ASCII_CONTROL_SPACE(c
)
237 U_CAPI UBool U_EXPORT2
238 u_isblank(UChar32 c
) {
239 if((uint32_t)c
<=0x9f) {
240 return c
==9 || c
==0x20; /* TAB or SPACE */
245 return (UBool
)(GET_CATEGORY(props
)==U_SPACE_SEPARATOR
);
249 U_CAPI UBool U_EXPORT2
250 u_isUWhiteSpace(UChar32 c
) {
251 return (u_getUnicodeProperties(c
, 1)&U_MASK(UPROPS_WHITE_SPACE
))!=0;
254 /* Checks if the Unicode character is printable.*/
255 U_CAPI UBool U_EXPORT2
256 u_isprint(UChar32 c
) {
259 /* comparing ==0 returns FALSE for the categories mentioned */
260 return (UBool
)((CAT_MASK(props
)&U_GC_C_MASK
)==0);
264 * Checks if c is in \p{graph}\p{blank} - \p{cntrl}.
265 * Implements UCHAR_POSIX_PRINT.
269 u_isprintPOSIX(UChar32 c
) {
273 * The only cntrl character in graph+blank is TAB (in blank).
274 * Here we implement (blank-TAB)=Zs instead of calling u_isblank().
276 return (UBool
)((GET_CATEGORY(props
)==U_SPACE_SEPARATOR
) || u_isgraphPOSIX(c
));
279 U_CAPI UBool U_EXPORT2
280 u_isgraph(UChar32 c
) {
283 /* comparing ==0 returns FALSE for the categories mentioned */
284 return (UBool
)((CAT_MASK(props
)&
285 (U_GC_CC_MASK
|U_GC_CF_MASK
|U_GC_CS_MASK
|U_GC_CN_MASK
|U_GC_Z_MASK
))
291 * [^\p{space}\p{gc=Control}\p{gc=Surrogate}\p{gc=Unassigned}]
292 * with space=\p{Whitespace} and Control=Cc.
293 * Implements UCHAR_POSIX_GRAPH.
297 u_isgraphPOSIX(UChar32 c
) {
300 /* \p{space}\p{gc=Control} == \p{gc=Z}\p{Control} */
301 /* comparing ==0 returns FALSE for the categories mentioned */
302 return (UBool
)((CAT_MASK(props
)&
303 (U_GC_CC_MASK
|U_GC_CS_MASK
|U_GC_CN_MASK
|U_GC_Z_MASK
))
307 U_CAPI UBool U_EXPORT2
308 u_ispunct(UChar32 c
) {
311 return (UBool
)((CAT_MASK(props
)&U_GC_P_MASK
)!=0);
314 /* Checks if the Unicode character can start a Unicode identifier.*/
315 U_CAPI UBool U_EXPORT2
316 u_isIDStart(UChar32 c
) {
317 /* same as u_isalpha() */
320 return (UBool
)((CAT_MASK(props
)&(U_GC_L_MASK
|U_GC_NL_MASK
))!=0);
323 /* Checks if the Unicode character can be a Unicode identifier part other than starting the
325 U_CAPI UBool U_EXPORT2
326 u_isIDPart(UChar32 c
) {
331 (U_GC_ND_MASK
|U_GC_NL_MASK
|
333 U_GC_PC_MASK
|U_GC_MC_MASK
|U_GC_MN_MASK
)
338 /*Checks if the Unicode character can be ignorable in a Java or Unicode identifier.*/
339 U_CAPI UBool U_EXPORT2
340 u_isIDIgnorable(UChar32 c
) {
342 return u_isISOControl(c
) && !IS_THAT_ASCII_CONTROL_SPACE(c
);
346 return (UBool
)(GET_CATEGORY(props
)==U_FORMAT_CHAR
);
350 /*Checks if the Unicode character can start a Java identifier.*/
351 U_CAPI UBool U_EXPORT2
352 u_isJavaIDStart(UChar32 c
) {
355 return (UBool
)((CAT_MASK(props
)&(U_GC_L_MASK
|U_GC_SC_MASK
|U_GC_PC_MASK
))!=0);
358 /*Checks if the Unicode character can be a Java identifier part other than starting the
361 U_CAPI UBool U_EXPORT2
362 u_isJavaIDPart(UChar32 c
) {
367 (U_GC_ND_MASK
|U_GC_NL_MASK
|
369 U_GC_SC_MASK
|U_GC_PC_MASK
|
370 U_GC_MC_MASK
|U_GC_MN_MASK
)
375 U_CAPI
int32_t U_EXPORT2
376 u_charDigitValue(UChar32 c
) {
380 value
=(int32_t)GET_NUMERIC_TYPE_VALUE(props
)-UPROPS_NTV_DECIMAL_START
;
388 U_CAPI
double U_EXPORT2
389 u_getNumericValue(UChar32 c
) {
393 ntv
=(int32_t)GET_NUMERIC_TYPE_VALUE(props
);
395 if(ntv
==UPROPS_NTV_NONE
) {
396 return U_NO_NUMERIC_VALUE
;
397 } else if(ntv
<UPROPS_NTV_DIGIT_START
) {
399 return ntv
-UPROPS_NTV_DECIMAL_START
;
400 } else if(ntv
<UPROPS_NTV_NUMERIC_START
) {
402 return ntv
-UPROPS_NTV_DIGIT_START
;
403 } else if(ntv
<UPROPS_NTV_FRACTION_START
) {
405 return ntv
-UPROPS_NTV_NUMERIC_START
;
406 } else if(ntv
<UPROPS_NTV_LARGE_START
) {
408 int32_t numerator
=(ntv
>>4)-12;
409 int32_t denominator
=(ntv
&0xf)+1;
410 return (double)numerator
/denominator
;
411 } else if(ntv
<UPROPS_NTV_BASE60_START
) {
412 /* large, single-significant-digit integer */
414 int32_t mant
=(ntv
>>5)-14;
415 int32_t exp
=(ntv
&0x1f)+2;
418 /* multiply by 10^exp without math.h */
439 } else if(ntv
<UPROPS_NTV_RESERVED_START
) {
440 /* sexagesimal (base 60) integer */
441 int32_t numValue
=(ntv
>>2)-0xbf;
442 int32_t exp
=(ntv
&3)+1;
446 numValue
*=60*60*60*60;
465 return U_NO_NUMERIC_VALUE
;
469 U_CAPI
int32_t U_EXPORT2
470 u_digit(UChar32 ch
, int8_t radix
) {
472 if((uint8_t)(radix
-2)<=(36-2)) {
473 value
=(int8_t)u_charDigitValue(ch
);
475 /* ch is not a decimal digit, try latin letters */
476 if(ch
>=0x61 && ch
<=0x7A) {
477 value
=(int8_t)(ch
-0x57); /* ch - 'a' + 10 */
478 } else if(ch
>=0x41 && ch
<=0x5A) {
479 value
=(int8_t)(ch
-0x37); /* ch - 'A' + 10 */
480 } else if(ch
>=0xFF41 && ch
<=0xFF5A) {
481 value
=(int8_t)(ch
-0xFF37); /* fullwidth ASCII a-z */
482 } else if(ch
>=0xFF21 && ch
<=0xFF3A) {
483 value
=(int8_t)(ch
-0xFF17); /* fullwidth ASCII A-Z */
487 value
=-1; /* invalid radix */
489 return (int8_t)((value
<radix
) ? value
: -1);
492 U_CAPI UChar32 U_EXPORT2
493 u_forDigit(int32_t digit
, int8_t radix
) {
494 if((uint8_t)(radix
-2)>(36-2) || (uint32_t)digit
>=(uint32_t)radix
) {
496 } else if(digit
<10) {
497 return (UChar32
)(0x30+digit
);
499 return (UChar32
)((0x61-10)+digit
);
503 /* miscellaneous, and support for uprops.cpp -------------------------------- */
505 U_CAPI
void U_EXPORT2
506 u_getUnicodeVersion(UVersionInfo versionArray
) {
507 if(versionArray
!=NULL
) {
508 uprv_memcpy(versionArray
, dataVersion
, U_MAX_VERSION_LENGTH
);
513 u_getMainProperties(UChar32 c
) {
520 u_getUnicodeProperties(UChar32 c
, int32_t column
) {
522 if(column
>=propsVectorsColumns
) {
525 uint16_t vecIndex
=UTRIE2_GET16(&propsVectorsTrie
, c
);
526 return propsVectors
[vecIndex
+column
];
531 uprv_getMaxValues(int32_t column
) {
534 return indexes
[UPROPS_MAX_VALUES_INDEX
];
536 return indexes
[UPROPS_MAX_VALUES_2_INDEX
];
542 U_CAPI
void U_EXPORT2
543 u_charAge(UChar32 c
, UVersionInfo versionArray
) {
544 if(versionArray
!=NULL
) {
545 uint32_t version
=u_getUnicodeProperties(c
, 0)>>UPROPS_AGE_SHIFT
;
546 versionArray
[0]=(uint8_t)(version
>>4);
547 versionArray
[1]=(uint8_t)(version
&0xf);
548 versionArray
[2]=versionArray
[3]=0;
552 U_CAPI UScriptCode U_EXPORT2
553 uscript_getScript(UChar32 c
, UErrorCode
*pErrorCode
) {
555 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
556 return USCRIPT_INVALID_CODE
;
558 if((uint32_t)c
>0x10ffff) {
559 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
560 return USCRIPT_INVALID_CODE
;
562 scriptX
=u_getUnicodeProperties(c
, 0)&UPROPS_SCRIPT_X_MASK
;
563 if(scriptX
<UPROPS_SCRIPT_X_WITH_COMMON
) {
564 return (UScriptCode
)scriptX
;
565 } else if(scriptX
<UPROPS_SCRIPT_X_WITH_INHERITED
) {
566 return USCRIPT_COMMON
;
567 } else if(scriptX
<UPROPS_SCRIPT_X_WITH_OTHER
) {
568 return USCRIPT_INHERITED
;
570 return (UScriptCode
)scriptExtensions
[scriptX
&UPROPS_SCRIPT_MASK
];
574 U_CAPI UBool U_EXPORT2
575 uscript_hasScript(UChar32 c
, UScriptCode sc
) {
577 uint32_t scriptX
=u_getUnicodeProperties(c
, 0)&UPROPS_SCRIPT_X_MASK
;
578 if(scriptX
<UPROPS_SCRIPT_X_WITH_COMMON
) {
579 return sc
==(UScriptCode
)scriptX
;
582 scx
=scriptExtensions
+(scriptX
&UPROPS_SCRIPT_MASK
);
583 if(scriptX
>=UPROPS_SCRIPT_X_WITH_OTHER
) {
584 scx
=scriptExtensions
+scx
[1];
586 if(sc
>=USCRIPT_CODE_LIMIT
) {
587 /* Guard against bogus input that would make us go past the Script_Extensions terminator. */
593 return sc
==(*scx
&0x7fff);
596 U_CAPI
int32_t U_EXPORT2
597 uscript_getScriptExtensions(UChar32 c
,
598 UScriptCode
*scripts
, int32_t capacity
,
599 UErrorCode
*pErrorCode
) {
604 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
607 if(capacity
<0 || (capacity
>0 && scripts
==NULL
)) {
608 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
611 scriptX
=u_getUnicodeProperties(c
, 0)&UPROPS_SCRIPT_X_MASK
;
612 if(scriptX
<UPROPS_SCRIPT_X_WITH_COMMON
) {
614 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
616 scripts
[0]=(UScriptCode
)scriptX
;
621 scx
=scriptExtensions
+(scriptX
&UPROPS_SCRIPT_MASK
);
622 if(scriptX
>=UPROPS_SCRIPT_X_WITH_OTHER
) {
623 scx
=scriptExtensions
+scx
[1];
628 if(length
<capacity
) {
629 scripts
[length
]=(UScriptCode
)(sx
&0x7fff);
633 if(length
>capacity
) {
634 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
639 U_CAPI UBlockCode U_EXPORT2
640 ublock_getCode(UChar32 c
) {
641 return (UBlockCode
)((u_getUnicodeProperties(c
, 0)&UPROPS_BLOCK_MASK
)>>UPROPS_BLOCK_SHIFT
);
644 /* property starts for UnicodeSet ------------------------------------------- */
646 static UBool U_CALLCONV
647 _enumPropertyStartsRange(const void *context
, UChar32 start
, UChar32 end
, uint32_t value
) {
648 /* add the start code point to the USet */
649 const USetAdder
*sa
=(const USetAdder
*)context
;
650 sa
->add(sa
->set
, start
);
654 #define USET_ADD_CP_AND_NEXT(sa, cp) sa->add(sa->set, cp); sa->add(sa->set, cp+1)
656 U_CFUNC
void U_EXPORT2
657 uchar_addPropertyStarts(const USetAdder
*sa
, UErrorCode
*pErrorCode
) {
658 if(U_FAILURE(*pErrorCode
)) {
662 /* add the start code point of each same-value range of the main trie */
663 utrie2_enum(&propsTrie
, NULL
, _enumPropertyStartsRange
, sa
);
665 /* add code points with hardcoded properties, plus the ones following them */
667 /* add for u_isblank() */
668 USET_ADD_CP_AND_NEXT(sa
, TAB
);
670 /* add for IS_THAT_CONTROL_SPACE() */
671 sa
->add(sa
->set
, CR
+1); /* range TAB..CR */
672 sa
->add(sa
->set
, 0x1c);
673 sa
->add(sa
->set
, 0x1f+1);
674 USET_ADD_CP_AND_NEXT(sa
, NL
);
676 /* add for u_isIDIgnorable() what was not added above */
677 sa
->add(sa
->set
, DEL
); /* range DEL..NBSP-1, NBSP added below */
678 sa
->add(sa
->set
, HAIRSP
);
679 sa
->add(sa
->set
, RLM
+1);
680 sa
->add(sa
->set
, INHSWAP
);
681 sa
->add(sa
->set
, NOMDIG
+1);
682 USET_ADD_CP_AND_NEXT(sa
, ZWNBSP
);
684 /* add no-break spaces for u_isWhitespace() what was not added above */
685 USET_ADD_CP_AND_NEXT(sa
, NBSP
);
686 USET_ADD_CP_AND_NEXT(sa
, FIGURESP
);
687 USET_ADD_CP_AND_NEXT(sa
, NNBSP
);
689 /* add for u_digit() */
690 sa
->add(sa
->set
, U_a
);
691 sa
->add(sa
->set
, U_z
+1);
692 sa
->add(sa
->set
, U_A
);
693 sa
->add(sa
->set
, U_Z
+1);
694 sa
->add(sa
->set
, U_FW_a
);
695 sa
->add(sa
->set
, U_FW_z
+1);
696 sa
->add(sa
->set
, U_FW_A
);
697 sa
->add(sa
->set
, U_FW_Z
+1);
699 /* add for u_isxdigit() */
700 sa
->add(sa
->set
, U_f
+1);
701 sa
->add(sa
->set
, U_F
+1);
702 sa
->add(sa
->set
, U_FW_f
+1);
703 sa
->add(sa
->set
, U_FW_F
+1);
705 /* add for UCHAR_DEFAULT_IGNORABLE_CODE_POINT what was not added above */
706 sa
->add(sa
->set
, WJ
); /* range WJ..NOMDIG */
707 sa
->add(sa
->set
, 0xfff0);
708 sa
->add(sa
->set
, 0xfffb+1);
709 sa
->add(sa
->set
, 0xe0000);
710 sa
->add(sa
->set
, 0xe0fff+1);
712 /* add for UCHAR_GRAPHEME_BASE and others */
713 USET_ADD_CP_AND_NEXT(sa
, CGJ
);
716 U_CFUNC
void U_EXPORT2
717 upropsvec_addPropertyStarts(const USetAdder
*sa
, UErrorCode
*pErrorCode
) {
718 if(U_FAILURE(*pErrorCode
)) {
722 /* add the start code point of each same-value range of the properties vectors trie */
723 if(propsVectorsColumns
>0) {
724 /* if propsVectorsColumns==0 then the properties vectors trie may not be there at all */
725 utrie2_enum(&propsVectorsTrie
, NULL
, _enumPropertyStartsRange
, sa
);