U_CAPI void U_EXPORT2 u_init(UErrorCode *status) { UTRACE_ENTRY_OC(UTRACE_U_INIT); /* Make sure the global mutexes are initialized. */ umtx_init(NULL); umtx_lock(&gICUInitMutex); if (gICUInitialized || U_FAILURE(*status)) { umtx_unlock(&gICUInitMutex); UTRACE_EXIT_STATUS(*status); return; } #if 1 /* * 2005-may-02 * * ICU4C 3.4 (jitterbug 4497) hardcodes the data for Unicode character * properties for APIs that want to be fast. * Therefore, we need not load them here nor check for errors. * Instead, we load the converter alias table to see if any ICU data * is available. * Users should really open the service objects they need and check * for errors there, to make sure that the actual items they need are * available. */ #if !UCONFIG_NO_CONVERSION ucnv_io_countKnownConverters(status); #endif #else /* Do any required init for services that don't have open operations * and use "only" the double-check initialization method for performance * reasons (avoiding a mutex lock even for _checking_ whether the * initialization had occurred). */ /* Char Properties */ uprv_haveProperties(status); /* load the case and bidi properties but don't fail if they are not available */ u_isULowercase(0x61); u_getIntPropertyValue(0x200D, UCHAR_JOINING_TYPE); /* ZERO WIDTH JOINER: Join_Causing */ #if !UCONFIG_NO_NORMALIZATION /* Normalization */ unorm_haveData(status); #endif #endif gICUInitialized = TRUE; /* TODO: don't set if U_FAILURE? */ umtx_unlock(&gICUInitMutex); UTRACE_EXIT_STATUS(*status); }
U_CAPI int32_t U_EXPORT2 unorm_compare(const UChar *s1, int32_t length1, const UChar *s2, int32_t length2, uint32_t options, UErrorCode *pErrorCode) { UChar fcd1[300], fcd2[300]; UChar *d1, *d2; const UnicodeSet *nx; UNormalizationMode mode; int32_t normOptions; int32_t result; /* argument checking */ if(pErrorCode==0 || U_FAILURE(*pErrorCode)) { return 0; } if(s1==0 || length1<-1 || s2==0 || length2<-1) { *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; return 0; } if(!unorm_haveData(pErrorCode)) { return 0; } if(!uprv_haveProperties(pErrorCode)) { return 0; } normOptions=(int32_t)(options>>UNORM_COMPARE_NORM_OPTIONS_SHIFT); nx=unorm_getNX(normOptions, pErrorCode); if(U_FAILURE(*pErrorCode)) { return 0; } d1=d2=0; options|=_COMPARE_EQUIV; result=0; /* * UAX #21 Case Mappings, as fixed for Unicode version 4 * (see Jitterbug 2021), defines a canonical caseless match as * * A string X is a canonical caseless match * for a string Y if and only if * NFD(toCasefold(NFD(X))) = NFD(toCasefold(NFD(Y))) * * For better performance, we check for FCD (or let the caller tell us that * both strings are in FCD) for the inner normalization. * BasicNormalizerTest::FindFoldFCDExceptions() makes sure that * case-folding preserves the FCD-ness of a string. * The outer normalization is then only performed by unorm_cmpEquivFold() * when there is a difference. * * Exception: When using the Turkic case-folding option, we do perform * full NFD first. This is because in the Turkic case precomposed characters * with 0049 capital I or 0069 small i fold differently whether they * are first decomposed or not, so an FCD check - a check only for * canonical order - is not sufficient. */ if(options&U_FOLD_CASE_EXCLUDE_SPECIAL_I) { mode=UNORM_NFD; options&=~UNORM_INPUT_IS_FCD; } else { mode=UNORM_FCD; } if(!(options&UNORM_INPUT_IS_FCD)) { int32_t _len1, _len2; UBool isFCD1, isFCD2; // check if s1 and/or s2 fulfill the FCD conditions isFCD1= UNORM_YES==unorm_internalQuickCheck(s1, length1, mode, TRUE, nx, pErrorCode); isFCD2= UNORM_YES==unorm_internalQuickCheck(s2, length2, mode, TRUE, nx, pErrorCode); if(U_FAILURE(*pErrorCode)) { return 0; } /* * ICU 2.4 had a further optimization: * If both strings were not in FCD, then they were both NFD'ed, * and the _COMPARE_EQUIV option was turned off. * It is not entirely clear that this is valid with the current * definition of the canonical caseless match. * Therefore, ICU 2.6 removes that optimization. */ if(!isFCD1) { _len1=unorm_internalNormalizeWithNX(fcd1, LENGTHOF(fcd1), s1, length1, mode, normOptions, nx, pErrorCode); if(*pErrorCode!=U_BUFFER_OVERFLOW_ERROR) { s1=fcd1; } else { d1=(UChar *)uprv_malloc(_len1*U_SIZEOF_UCHAR); if(d1==0) { *pErrorCode=U_MEMORY_ALLOCATION_ERROR; goto cleanup; } *pErrorCode=U_ZERO_ERROR; _len1=unorm_internalNormalizeWithNX(d1, _len1, s1, length1, mode, normOptions, nx, pErrorCode); if(U_FAILURE(*pErrorCode)) { goto cleanup; } s1=d1; } length1=_len1; } if(!isFCD2) { _len2=unorm_internalNormalizeWithNX(fcd2, LENGTHOF(fcd2), s2, length2, mode, normOptions, nx, pErrorCode); if(*pErrorCode!=U_BUFFER_OVERFLOW_ERROR) { s2=fcd2; } else { d2=(UChar *)uprv_malloc(_len2*U_SIZEOF_UCHAR); if(d2==0) { *pErrorCode=U_MEMORY_ALLOCATION_ERROR; goto cleanup; } *pErrorCode=U_ZERO_ERROR; _len2=unorm_internalNormalizeWithNX(d2, _len2, s2, length2, mode, normOptions, nx, pErrorCode); if(U_FAILURE(*pErrorCode)) { goto cleanup; } s2=d2; } length2=_len2; } } if(U_SUCCESS(*pErrorCode)) { result=unorm_cmpEquivFold(s1, length1, s2, length2, options, pErrorCode); } cleanup: if(d1!=0) { uprv_free(d1); } if(d2!=0) { uprv_free(d2); } return result; }