/* ******************************************************************************* * * Copyright (C) 2002-2005, International Business Machines * Corporation and others. All Rights Reserved. * ******************************************************************************* * file name: uprops.h * encoding: US-ASCII * tab size: 8 (not used) * indentation:4 * * created on: 2002feb24 * created by: Markus W. Scherer * * Implementations for mostly non-core Unicode character properties * stored in uprops.icu. * * With the APIs implemented here, almost all properties files and * their associated implementation files are used from this file, * including those for normalization and case mappings. */ #include "unicode/utypes.h" #include "unicode/uchar.h" #include "unicode/uscript.h" #include "cstring.h" #include "ucln_cmn.h" #include "umutex.h" #include "unormimp.h" #include "ubidi_props.h" #include "uprops.h" #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) /* cleanup ------------------------------------------------------------------ */ static const UCaseProps *gCsp=NULL; static const UBiDiProps *gBdp=NULL; static UBool U_CALLCONV uprops_cleanup(void) { gCsp=NULL; gBdp=NULL; return TRUE; } /* case mapping properties API ---------------------------------------------- */ /* get the UCaseProps singleton, or else its dummy, once and for all */ static const UCaseProps * getCaseProps() { /* * This lazy intialization with double-checked locking (without mutex protection for * the initial check) is transiently unsafe under certain circumstances. * Check the readme and use u_init() if necessary. */ /* the initial check is performed by the GET_CASE_PROPS() macro */ const UCaseProps *csp; UErrorCode errorCode=U_ZERO_ERROR; csp=ucase_getSingleton(&errorCode); if(U_FAILURE(errorCode)) { errorCode=U_ZERO_ERROR; csp=ucase_getDummy(&errorCode); if(U_FAILURE(errorCode)) { return NULL; } } umtx_lock(NULL); if(gCsp==NULL) { gCsp=csp; csp=NULL; ucln_common_registerCleanup(UCLN_COMMON_UPROPS, uprops_cleanup); } umtx_unlock(NULL); return gCsp; } /* * In ICU 3.0, most Unicode properties were loaded from uprops.icu. * ICU 3.2 adds ucase.icu for case mapping properties. * ICU 3.4 adds ubidi.icu for bidi/shaping properties and * removes case/bidi/shaping properties from uprops.icu. * * Loading of uprops.icu was never mutex-protected and required u_init() * for thread safety. * In order to maintain performance for all such properties, * ucase.icu and ubidi.icu are loaded lazily, without mutexing. * u_init() will try to load them for thread safety, * but u_init() will not fail if they are missing. * * uchar.c maintains a tri-state flag for (not loaded/loaded/failed to load) * and an error code for load failure. * Instead, here we try to load at most once. * If it works, we use the resulting singleton object. * If it fails, then we get a dummy object, which always works unless * we are seriously out of memory. * After the first try, we have a never-changing pointer to either the * real singleton or the dummy. * * This method is used in Unicode properties APIs (uchar.h) that * do not have a service object and also do not have an error code parameter. * Other API implementations get the singleton themselves * (with mutexing), store it in the service object, and report errors. */ #define GET_CASE_PROPS() (gCsp!=NULL ? gCsp : getCaseProps()) /* public API (see uchar.h) */ U_CAPI UBool U_EXPORT2 u_isULowercase(UChar32 c) { return (UBool)(UCASE_LOWER==ucase_getType(GET_CASE_PROPS(), c)); } U_CAPI UBool U_EXPORT2 u_isUUppercase(UChar32 c) { return (UBool)(UCASE_UPPER==ucase_getType(GET_CASE_PROPS(), c)); } /* Transforms the Unicode character to its lower case equivalent.*/ U_CAPI UChar32 U_EXPORT2 u_tolower(UChar32 c) { return ucase_tolower(GET_CASE_PROPS(), c); } /* Transforms the Unicode character to its upper case equivalent.*/ U_CAPI UChar32 U_EXPORT2 u_toupper(UChar32 c) { return ucase_toupper(GET_CASE_PROPS(), c); } /* Transforms the Unicode character to its title case equivalent.*/ U_CAPI UChar32 U_EXPORT2 u_totitle(UChar32 c) { return ucase_totitle(GET_CASE_PROPS(), c); } /* return the simple case folding mapping for c */ U_CAPI UChar32 U_EXPORT2 u_foldCase(UChar32 c, uint32_t options) { return ucase_fold(GET_CASE_PROPS(), c, options); } /* bidi/shaping properties API ---------------------------------------------- */ /* get the UBiDiProps singleton, or else its dummy, once and for all */ static const UBiDiProps * getBiDiProps() { /* * This lazy intialization with double-checked locking (without mutex protection for * the initial check) is transiently unsafe under certain circumstances. * Check the readme and use u_init() if necessary. */ /* the initial check is performed by the GET_BIDI_PROPS() macro */ const UBiDiProps *bdp; UErrorCode errorCode=U_ZERO_ERROR; bdp=ubidi_getSingleton(&errorCode); if(U_FAILURE(errorCode)) { errorCode=U_ZERO_ERROR; bdp=ubidi_getDummy(&errorCode); if(U_FAILURE(errorCode)) { return NULL; } } umtx_lock(NULL); if(gBdp==NULL) { gBdp=bdp; bdp=NULL; ucln_common_registerCleanup(UCLN_COMMON_UPROPS, uprops_cleanup); } umtx_unlock(NULL); return gBdp; } /* see comment for GET_CASE_PROPS() */ #define GET_BIDI_PROPS() (gBdp!=NULL ? gBdp : getBiDiProps()) /* general properties API functions ----------------------------------------- */ static const struct { int32_t column; uint32_t mask; } binProps[UCHAR_BINARY_LIMIT]={ /* * column and mask values for binary properties from u_getUnicodeProperties(). * Must be in order of corresponding UProperty, * and there must be exacly one entry per binary UProperty. * * Properties with mask 0 are handled in code. * For them, column is the UPropertySource value. */ { 1, U_MASK(UPROPS_ALPHABETIC) }, { 1, U_MASK(UPROPS_ASCII_HEX_DIGIT) }, { UPROPS_SRC_BIDI, 0 }, /* UCHAR_BIDI_CONTROL */ { UPROPS_SRC_BIDI, 0 }, /* UCHAR_BIDI_MIRRORED */ { 1, U_MASK(UPROPS_DASH) }, { 1, U_MASK(UPROPS_DEFAULT_IGNORABLE_CODE_POINT) }, { 1, U_MASK(UPROPS_DEPRECATED) }, { 1, U_MASK(UPROPS_DIACRITIC) }, { 1, U_MASK(UPROPS_EXTENDER) }, { UPROPS_SRC_NORM, 0 }, /* UCHAR_FULL_COMPOSITION_EXCLUSION */ { 1, U_MASK(UPROPS_GRAPHEME_BASE) }, { 1, U_MASK(UPROPS_GRAPHEME_EXTEND) }, { 1, U_MASK(UPROPS_GRAPHEME_LINK) }, { 1, U_MASK(UPROPS_HEX_DIGIT) }, { 1, U_MASK(UPROPS_HYPHEN) }, { 1, U_MASK(UPROPS_ID_CONTINUE) }, { 1, U_MASK(UPROPS_ID_START) }, { 1, U_MASK(UPROPS_IDEOGRAPHIC) }, { 1, U_MASK(UPROPS_IDS_BINARY_OPERATOR) }, { 1, U_MASK(UPROPS_IDS_TRINARY_OPERATOR) }, { UPROPS_SRC_BIDI, 0 }, /* UCHAR_JOIN_CONTROL */ { 1, U_MASK(UPROPS_LOGICAL_ORDER_EXCEPTION) }, { UPROPS_SRC_CASE, 0 }, /* UCHAR_LOWERCASE */ { 1, U_MASK(UPROPS_MATH) }, { 1, U_MASK(UPROPS_NONCHARACTER_CODE_POINT) }, { 1, U_MASK(UPROPS_QUOTATION_MARK) }, { 1, U_MASK(UPROPS_RADICAL) }, { UPROPS_SRC_CASE, 0 }, /* UCHAR_SOFT_DOTTED */ { 1, U_MASK(UPROPS_TERMINAL_PUNCTUATION) }, { 1, U_MASK(UPROPS_UNIFIED_IDEOGRAPH) }, { UPROPS_SRC_CASE, 0 }, /* UCHAR_UPPERCASE */ { 1, U_MASK(UPROPS_WHITE_SPACE) }, { 1, U_MASK(UPROPS_XID_CONTINUE) }, { 1, U_MASK(UPROPS_XID_START) }, { UPROPS_SRC_CASE, 0 }, /* UCHAR_CASE_SENSITIVE */ { 2, U_MASK(UPROPS_V2_S_TERM) }, { 2, U_MASK(UPROPS_V2_VARIATION_SELECTOR) }, { UPROPS_SRC_NORM, 0 }, /* UCHAR_NFD_INERT */ { UPROPS_SRC_NORM, 0 }, /* UCHAR_NFKD_INERT */ { UPROPS_SRC_NORM, 0 }, /* UCHAR_NFC_INERT */ { UPROPS_SRC_NORM, 0 }, /* UCHAR_NFKC_INERT */ { UPROPS_SRC_NORM, 0 }, /* UCHAR_SEGMENT_STARTER */ { 2, U_MASK(UPROPS_V2_PATTERN_SYNTAX) }, { 2, U_MASK(UPROPS_V2_PATTERN_WHITE_SPACE) }, { UPROPS_SRC_CHAR_AND_PROPSVEC, 0 }, /* UCHAR_POSIX_ALNUM */ { UPROPS_SRC_CHAR, 0 }, /* UCHAR_POSIX_BLANK */ { UPROPS_SRC_CHAR, 0 }, /* UCHAR_POSIX_GRAPH */ { UPROPS_SRC_CHAR, 0 }, /* UCHAR_POSIX_PRINT */ { UPROPS_SRC_CHAR, 0 } /* UCHAR_POSIX_XDIGIT */ }; U_CAPI UBool U_EXPORT2 u_hasBinaryProperty(UChar32 c, UProperty which) { /* c is range-checked in the functions that are called from here */ if(which>UPROPS_EA_SHIFT; case UCHAR_GENERAL_CATEGORY: return (int32_t)u_charType(c); case UCHAR_JOINING_GROUP: return ubidi_getJoiningGroup(GET_BIDI_PROPS(), c); case UCHAR_JOINING_TYPE: return ubidi_getJoiningType(GET_BIDI_PROPS(), c); case UCHAR_LINE_BREAK: return (int32_t)(u_getUnicodeProperties(c, 0)&UPROPS_LB_MASK)>>UPROPS_LB_SHIFT; case UCHAR_NUMERIC_TYPE: type=(int32_t)GET_NUMERIC_TYPE(u_getUnicodeProperties(c, -1)); if(type>U_NT_NUMERIC) { /* keep internal variants of U_NT_NUMERIC from becoming visible */ type=U_NT_NUMERIC; } return type; case UCHAR_SCRIPT: errorCode=U_ZERO_ERROR; return (int32_t)uscript_getScript(c, &errorCode); case UCHAR_HANGUL_SYLLABLE_TYPE: return uchar_getHST(c); #if !UCONFIG_NO_NORMALIZATION case UCHAR_NFD_QUICK_CHECK: case UCHAR_NFKD_QUICK_CHECK: case UCHAR_NFC_QUICK_CHECK: case UCHAR_NFKC_QUICK_CHECK: return (int32_t)unorm_getQuickCheck(c, (UNormalizationMode)(which-UCHAR_NFD_QUICK_CHECK)+UNORM_NFD); case UCHAR_LEAD_CANONICAL_COMBINING_CLASS: return unorm_getFCD16FromCodePoint(c)>>8; case UCHAR_TRAIL_CANONICAL_COMBINING_CLASS: return unorm_getFCD16FromCodePoint(c)&0xff; #endif case UCHAR_GRAPHEME_CLUSTER_BREAK: return (int32_t)(u_getUnicodeProperties(c, 2)&UPROPS_GCB_MASK)>>UPROPS_GCB_SHIFT; case UCHAR_SENTENCE_BREAK: return (int32_t)(u_getUnicodeProperties(c, 2)&UPROPS_SB_MASK)>>UPROPS_SB_SHIFT; case UCHAR_WORD_BREAK: return (int32_t)(u_getUnicodeProperties(c, 2)&UPROPS_WB_MASK)>>UPROPS_WB_SHIFT; default: return 0; /* undefined */ } } else if(which==UCHAR_GENERAL_CATEGORY_MASK) { return U_MASK(u_charType(c)); } else { return 0; /* undefined */ } } U_CAPI int32_t U_EXPORT2 u_getIntPropertyMinValue(UProperty which) { return 0; /* all binary/enum/int properties have a minimum value of 0 */ } U_CAPI int32_t U_EXPORT2 u_getIntPropertyMaxValue(UProperty which) { if(which>UPROPS_BLOCK_SHIFT; case UCHAR_CANONICAL_COMBINING_CLASS: case UCHAR_LEAD_CANONICAL_COMBINING_CLASS: case UCHAR_TRAIL_CANONICAL_COMBINING_CLASS: return 0xff; /* TODO do we need to be more precise, getting the actual maximum? */ case UCHAR_DECOMPOSITION_TYPE: return uprv_getMaxValues(2)&UPROPS_DT_MASK; case UCHAR_EAST_ASIAN_WIDTH: return (uprv_getMaxValues(0)&UPROPS_EA_MASK)>>UPROPS_EA_SHIFT; case UCHAR_GENERAL_CATEGORY: return (int32_t)U_CHAR_CATEGORY_COUNT-1; case UCHAR_LINE_BREAK: return (uprv_getMaxValues(0)&UPROPS_LB_MASK)>>UPROPS_LB_SHIFT; case UCHAR_NUMERIC_TYPE: return (int32_t)U_NT_COUNT-1; case UCHAR_SCRIPT: return uprv_getMaxValues(0)&UPROPS_SCRIPT_MASK; case UCHAR_HANGUL_SYLLABLE_TYPE: return (int32_t)U_HST_COUNT-1; #if !UCONFIG_NO_NORMALIZATION case UCHAR_NFD_QUICK_CHECK: case UCHAR_NFKD_QUICK_CHECK: return (int32_t)UNORM_YES; /* these are never "maybe", only "no" or "yes" */ case UCHAR_NFC_QUICK_CHECK: case UCHAR_NFKC_QUICK_CHECK: return (int32_t)UNORM_MAYBE; #endif case UCHAR_GRAPHEME_CLUSTER_BREAK: return (uprv_getMaxValues(2)&UPROPS_GCB_MASK)>>UPROPS_GCB_SHIFT; case UCHAR_SENTENCE_BREAK: return (uprv_getMaxValues(2)&UPROPS_SB_MASK)>>UPROPS_SB_SHIFT; case UCHAR_WORD_BREAK: return (uprv_getMaxValues(2)&UPROPS_WB_MASK)>>UPROPS_WB_SHIFT; default: return -1; /* undefined */ } } else { return -1; /* undefined */ } } U_CAPI UPropertySource U_EXPORT2 uprops_getSource(UProperty which) { if(which