michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 1996-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: * file name: ucol_res.cpp michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * Description: michael@0: * This file contains dependencies that the collation run-time doesn't normally michael@0: * need. This mainly contains resource bundle usage and collation meta information michael@0: * michael@0: * Modification history michael@0: * Date Name Comments michael@0: * 1996-1999 various members of ICU team maintained C API for collation framework michael@0: * 02/16/2001 synwee Added internal method getPrevSpecialCE michael@0: * 03/01/2001 synwee Added maxexpansion functionality. michael@0: * 03/16/2001 weiv Collation framework is rewritten in C and made UCA compliant michael@0: * 12/08/2004 grhoten Split part of ucol.cpp into ucol_res.cpp michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_COLLATION michael@0: #include "unicode/uloc.h" michael@0: #include "unicode/coll.h" michael@0: #include "unicode/tblcoll.h" michael@0: #include "unicode/caniter.h" michael@0: #include "unicode/uscript.h" michael@0: #include "unicode/ustring.h" michael@0: michael@0: #include "ucol_bld.h" michael@0: #include "ucol_imp.h" michael@0: #include "ucol_tok.h" michael@0: #include "ucol_elm.h" michael@0: #include "uresimp.h" michael@0: #include "ustr_imp.h" michael@0: #include "cstring.h" michael@0: #include "umutex.h" michael@0: #include "ucln_in.h" michael@0: #include "ustrenum.h" michael@0: #include "putilimp.h" michael@0: #include "utracimp.h" michael@0: #include "cmemory.h" michael@0: #include "uassert.h" michael@0: #include "uenumimp.h" michael@0: #include "ulist.h" michael@0: michael@0: U_NAMESPACE_USE michael@0: michael@0: static void ucol_setReorderCodesFromParser(UCollator *coll, UColTokenParser *parser, UErrorCode *status); michael@0: michael@0: // static UCA. There is only one. Collators don't use it. michael@0: // It is referenced only in ucol_initUCA and ucol_cleanup michael@0: static UCollator* _staticUCA = NULL; michael@0: static icu::UInitOnce gStaticUCAInitOnce = U_INITONCE_INITIALIZER; michael@0: // static pointer to udata memory. Inited in ucol_initUCA michael@0: // used for cleanup in ucol_cleanup michael@0: static UDataMemory* UCA_DATA_MEM = NULL; michael@0: michael@0: U_CDECL_BEGIN michael@0: static UBool U_CALLCONV michael@0: ucol_res_cleanup(void) michael@0: { michael@0: if (UCA_DATA_MEM) { michael@0: udata_close(UCA_DATA_MEM); michael@0: UCA_DATA_MEM = NULL; michael@0: } michael@0: if (_staticUCA) { michael@0: ucol_close(_staticUCA); michael@0: _staticUCA = NULL; michael@0: } michael@0: gStaticUCAInitOnce.reset(); michael@0: return TRUE; michael@0: } michael@0: michael@0: static UBool U_CALLCONV michael@0: isAcceptableUCA(void * /*context*/, michael@0: const char * /*type*/, const char * /*name*/, michael@0: const UDataInfo *pInfo){ michael@0: /* context, type & name are intentionally not used */ michael@0: if( pInfo->size>=20 && michael@0: pInfo->isBigEndian==U_IS_BIG_ENDIAN && michael@0: pInfo->charsetFamily==U_CHARSET_FAMILY && michael@0: pInfo->dataFormat[0]==UCA_DATA_FORMAT_0 && /* dataFormat="UCol" */ michael@0: pInfo->dataFormat[1]==UCA_DATA_FORMAT_1 && michael@0: pInfo->dataFormat[2]==UCA_DATA_FORMAT_2 && michael@0: pInfo->dataFormat[3]==UCA_DATA_FORMAT_3 && michael@0: pInfo->formatVersion[0]==UCA_FORMAT_VERSION_0 michael@0: #if UCA_FORMAT_VERSION_1!=0 michael@0: && pInfo->formatVersion[1]>=UCA_FORMAT_VERSION_1 michael@0: #endif michael@0: //pInfo->formatVersion[1]==UCA_FORMAT_VERSION_1 && michael@0: //pInfo->formatVersion[2]==UCA_FORMAT_VERSION_2 && // Too harsh michael@0: //pInfo->formatVersion[3]==UCA_FORMAT_VERSION_3 && // Too harsh michael@0: ) { michael@0: return TRUE; michael@0: // Note: In ICU 51 and earlier, michael@0: // we used to check that the UCA data version (pInfo->dataVersion) michael@0: // matches the UCD version (u_getUnicodeVersion()) michael@0: // but that complicated version updates, and michael@0: // a mismatch is "only" a problem for handling canonical equivalence. michael@0: // It need not be a fatal error. michael@0: } else { michael@0: return FALSE; michael@0: } michael@0: } michael@0: U_CDECL_END michael@0: michael@0: static void U_CALLCONV ucol_initStaticUCA(UErrorCode &status) { michael@0: U_ASSERT(_staticUCA == NULL); michael@0: U_ASSERT(UCA_DATA_MEM == NULL); michael@0: ucln_i18n_registerCleanup(UCLN_I18N_UCOL_RES, ucol_res_cleanup); michael@0: michael@0: UDataMemory *result = udata_openChoice(U_ICUDATA_COLL, UCA_DATA_TYPE, UCA_DATA_NAME, isAcceptableUCA, NULL, &status); michael@0: if(U_FAILURE(status)){ michael@0: udata_close(result); michael@0: return; michael@0: } michael@0: michael@0: _staticUCA = ucol_initCollator((const UCATableHeader *)udata_getMemory(result), NULL, NULL, &status); michael@0: if(U_SUCCESS(status)){ michael@0: // Initalize variables for implicit generation michael@0: uprv_uca_initImplicitConstants(&status); michael@0: UCA_DATA_MEM = result; michael@0: michael@0: }else{ michael@0: ucol_close(_staticUCA); michael@0: _staticUCA = NULL; michael@0: udata_close(result); michael@0: } michael@0: } michael@0: michael@0: michael@0: /* do not close UCA returned by ucol_initUCA! */ michael@0: UCollator * michael@0: ucol_initUCA(UErrorCode *status) { michael@0: umtx_initOnce(gStaticUCAInitOnce, &ucol_initStaticUCA, *status); michael@0: return _staticUCA; michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: ucol_forgetUCA(void) michael@0: { michael@0: _staticUCA = NULL; michael@0: UCA_DATA_MEM = NULL; michael@0: gStaticUCAInitOnce.reset(); michael@0: } michael@0: michael@0: /****************************************************************************/ michael@0: /* Following are the open/close functions */ michael@0: /* */ michael@0: /****************************************************************************/ michael@0: static UCollator* michael@0: tryOpeningFromRules(UResourceBundle *collElem, UErrorCode *status) { michael@0: int32_t rulesLen = 0; michael@0: const UChar *rules = ures_getStringByKey(collElem, "Sequence", &rulesLen, status); michael@0: return ucol_openRules(rules, rulesLen, UCOL_DEFAULT, UCOL_DEFAULT, NULL, status); michael@0: } michael@0: michael@0: michael@0: // API in ucol_imp.h michael@0: michael@0: U_CFUNC UCollator* michael@0: ucol_open_internal(const char *loc, michael@0: UErrorCode *status) michael@0: { michael@0: UErrorCode intStatus = U_ZERO_ERROR; michael@0: const UCollator* UCA = ucol_initUCA(status); michael@0: michael@0: /* New version */ michael@0: if(U_FAILURE(*status)) return 0; michael@0: michael@0: michael@0: michael@0: UCollator *result = NULL; michael@0: UResourceBundle *b = ures_open(U_ICUDATA_COLL, loc, status); michael@0: michael@0: /* we try to find stuff from keyword */ michael@0: UResourceBundle *collations = ures_getByKey(b, "collations", NULL, status); michael@0: UResourceBundle *collElem = NULL; michael@0: char keyBuffer[256]; michael@0: // if there is a keyword, we pick it up and try to get elements michael@0: if(!uloc_getKeywordValue(loc, "collation", keyBuffer, 256, status) || michael@0: !uprv_strcmp(keyBuffer,"default")) { /* Treat 'zz@collation=default' as 'zz'. */ michael@0: // no keyword. we try to find the default setting, which will give us the keyword value michael@0: intStatus = U_ZERO_ERROR; michael@0: // finding default value does not affect collation fallback status michael@0: UResourceBundle *defaultColl = ures_getByKeyWithFallback(collations, "default", NULL, &intStatus); michael@0: if(U_SUCCESS(intStatus)) { michael@0: int32_t defaultKeyLen = 0; michael@0: const UChar *defaultKey = ures_getString(defaultColl, &defaultKeyLen, &intStatus); michael@0: u_UCharsToChars(defaultKey, keyBuffer, defaultKeyLen); michael@0: keyBuffer[defaultKeyLen] = 0; michael@0: } else { michael@0: *status = U_INTERNAL_PROGRAM_ERROR; michael@0: return NULL; michael@0: } michael@0: ures_close(defaultColl); michael@0: } michael@0: collElem = ures_getByKeyWithFallback(collations, keyBuffer, collations, status); michael@0: collations = NULL; // We just reused the collations object as collElem. michael@0: michael@0: UResourceBundle *binary = NULL; michael@0: UResourceBundle *reorderRes = NULL; michael@0: michael@0: if(*status == U_MISSING_RESOURCE_ERROR) { /* We didn't find the tailoring data, we fallback to the UCA */ michael@0: *status = U_USING_DEFAULT_WARNING; michael@0: result = ucol_initCollator(UCA->image, result, UCA, status); michael@0: if (U_FAILURE(*status)) { michael@0: goto clean; michael@0: } michael@0: // if we use UCA, real locale is root michael@0: ures_close(b); michael@0: b = ures_open(U_ICUDATA_COLL, "", status); michael@0: ures_close(collElem); michael@0: collElem = ures_open(U_ICUDATA_COLL, "", status); michael@0: if(U_FAILURE(*status)) { michael@0: goto clean; michael@0: } michael@0: result->hasRealData = FALSE; michael@0: } else if(U_SUCCESS(*status)) { michael@0: intStatus = U_ZERO_ERROR; michael@0: michael@0: binary = ures_getByKey(collElem, "%%CollationBin", NULL, &intStatus); michael@0: michael@0: if(intStatus == U_MISSING_RESOURCE_ERROR) { /* we didn't find the binary image, we should use the rules */ michael@0: binary = NULL; michael@0: result = tryOpeningFromRules(collElem, status); michael@0: if(U_FAILURE(*status)) { michael@0: goto clean; michael@0: } michael@0: } else if(U_SUCCESS(intStatus)) { /* otherwise, we'll pick a collation data that exists */ michael@0: int32_t len = 0; michael@0: const uint8_t *inData = ures_getBinary(binary, &len, status); michael@0: if(U_FAILURE(*status)) { michael@0: goto clean; michael@0: } michael@0: UCATableHeader *colData = (UCATableHeader *)inData; michael@0: if(uprv_memcmp(colData->UCAVersion, UCA->image->UCAVersion, sizeof(UVersionInfo)) != 0 || michael@0: uprv_memcmp(colData->UCDVersion, UCA->image->UCDVersion, sizeof(UVersionInfo)) != 0 || michael@0: colData->version[0] != UCOL_BUILDER_VERSION) michael@0: { michael@0: *status = U_DIFFERENT_UCA_VERSION; michael@0: result = tryOpeningFromRules(collElem, status); michael@0: } else { michael@0: if(U_FAILURE(*status)){ michael@0: goto clean; michael@0: } michael@0: if((uint32_t)len > (paddedsize(sizeof(UCATableHeader)) + paddedsize(sizeof(UColOptionSet)))) { michael@0: result = ucol_initCollator((const UCATableHeader *)inData, result, UCA, status); michael@0: if(U_FAILURE(*status)){ michael@0: goto clean; michael@0: } michael@0: result->hasRealData = TRUE; michael@0: } else { michael@0: result = ucol_initCollator(UCA->image, result, UCA, status); michael@0: ucol_setOptionsFromHeader(result, (UColOptionSet *)(inData+((const UCATableHeader *)inData)->options), status); michael@0: if(U_FAILURE(*status)){ michael@0: goto clean; michael@0: } michael@0: result->hasRealData = FALSE; michael@0: } michael@0: result->freeImageOnClose = FALSE; michael@0: michael@0: reorderRes = ures_getByKey(collElem, "%%ReorderCodes", NULL, &intStatus); michael@0: if (U_SUCCESS(intStatus)) { michael@0: int32_t reorderCodesLen = 0; michael@0: const int32_t* reorderCodes = ures_getIntVector(reorderRes, &reorderCodesLen, status); michael@0: if (reorderCodesLen > 0) { michael@0: ucol_setReorderCodes(result, reorderCodes, reorderCodesLen, status); michael@0: // copy the reorder codes into the default reorder codes michael@0: result->defaultReorderCodesLength = result->reorderCodesLength; michael@0: result->defaultReorderCodes = (int32_t*) uprv_malloc(result->defaultReorderCodesLength * sizeof(int32_t)); michael@0: uprv_memcpy(result->defaultReorderCodes, result->reorderCodes, result->defaultReorderCodesLength * sizeof(int32_t)); michael@0: result->freeDefaultReorderCodesOnClose = TRUE; michael@0: } michael@0: if (U_FAILURE(*status)) { michael@0: goto clean; michael@0: } michael@0: } michael@0: } michael@0: michael@0: } else { // !U_SUCCESS(binaryStatus) michael@0: if(U_SUCCESS(*status)) { michael@0: *status = intStatus; // propagate underlying error michael@0: } michael@0: goto clean; michael@0: } michael@0: intStatus = U_ZERO_ERROR; michael@0: result->rules = ures_getStringByKey(collElem, "Sequence", &result->rulesLength, &intStatus); michael@0: result->freeRulesOnClose = FALSE; michael@0: } else { /* There is another error, and we're just gonna clean up */ michael@0: goto clean; michael@0: } michael@0: michael@0: intStatus = U_ZERO_ERROR; michael@0: result->ucaRules = ures_getStringByKey(b,"UCARules",NULL,&intStatus); michael@0: michael@0: if(loc == NULL) { michael@0: loc = ures_getLocaleByType(b, ULOC_ACTUAL_LOCALE, status); michael@0: } michael@0: result->requestedLocale = uprv_strdup(loc); michael@0: /* test for NULL */ michael@0: if (result->requestedLocale == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: goto clean; michael@0: } michael@0: loc = ures_getLocaleByType(collElem, ULOC_ACTUAL_LOCALE, status); michael@0: result->actualLocale = uprv_strdup(loc); michael@0: /* test for NULL */ michael@0: if (result->actualLocale == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: goto clean; michael@0: } michael@0: loc = ures_getLocaleByType(b, ULOC_ACTUAL_LOCALE, status); michael@0: result->validLocale = uprv_strdup(loc); michael@0: /* test for NULL */ michael@0: if (result->validLocale == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: goto clean; michael@0: } michael@0: michael@0: ures_close(b); michael@0: ures_close(collElem); michael@0: ures_close(binary); michael@0: ures_close(reorderRes); michael@0: return result; michael@0: michael@0: clean: michael@0: ures_close(b); michael@0: ures_close(collElem); michael@0: ures_close(binary); michael@0: ures_close(reorderRes); michael@0: ucol_close(result); michael@0: return NULL; michael@0: } michael@0: michael@0: U_CAPI UCollator* michael@0: ucol_open(const char *loc, michael@0: UErrorCode *status) michael@0: { michael@0: U_NAMESPACE_USE michael@0: michael@0: UTRACE_ENTRY_OC(UTRACE_UCOL_OPEN); michael@0: UTRACE_DATA1(UTRACE_INFO, "locale = \"%s\"", loc); michael@0: UCollator *result = NULL; michael@0: michael@0: #if !UCONFIG_NO_SERVICE michael@0: result = Collator::createUCollator(loc, status); michael@0: if (result == NULL) michael@0: #endif michael@0: { michael@0: result = ucol_open_internal(loc, status); michael@0: } michael@0: UTRACE_EXIT_PTR_STATUS(result, *status); michael@0: return result; michael@0: } michael@0: michael@0: michael@0: UCollator* michael@0: ucol_openRulesForImport( const UChar *rules, michael@0: int32_t rulesLength, michael@0: UColAttributeValue normalizationMode, michael@0: UCollationStrength strength, michael@0: UParseError *parseError, michael@0: GetCollationRulesFunction importFunc, michael@0: void* context, michael@0: UErrorCode *status) michael@0: { michael@0: UColTokenParser src; michael@0: UColAttributeValue norm; michael@0: UParseError tErr; michael@0: michael@0: if(status == NULL || U_FAILURE(*status)){ michael@0: return 0; michael@0: } michael@0: michael@0: if(rules == NULL || rulesLength < -1) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: if(rulesLength == -1) { michael@0: rulesLength = u_strlen(rules); michael@0: } michael@0: michael@0: if(parseError == NULL){ michael@0: parseError = &tErr; michael@0: } michael@0: michael@0: switch(normalizationMode) { michael@0: case UCOL_OFF: michael@0: case UCOL_ON: michael@0: case UCOL_DEFAULT: michael@0: norm = normalizationMode; michael@0: break; michael@0: default: michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: UCollator *result = NULL; michael@0: UCATableHeader *table = NULL; michael@0: UCollator *UCA = ucol_initUCA(status); michael@0: michael@0: if(U_FAILURE(*status)){ michael@0: return NULL; michael@0: } michael@0: michael@0: ucol_tok_initTokenList(&src, rules, rulesLength, UCA, importFunc, context, status); michael@0: ucol_tok_assembleTokenList(&src,parseError, status); michael@0: michael@0: if(U_FAILURE(*status)) { michael@0: /* if status is U_ILLEGAL_ARGUMENT_ERROR, src->current points at the offending option */ michael@0: /* if status is U_INVALID_FORMAT_ERROR, src->current points after the problematic part of the rules */ michael@0: /* so something might be done here... or on lower level */ michael@0: #ifdef UCOL_DEBUG michael@0: if(*status == U_ILLEGAL_ARGUMENT_ERROR) { michael@0: fprintf(stderr, "bad option starting at offset %i\n", (int)(src.current-src.source)); michael@0: } else { michael@0: fprintf(stderr, "invalid rule just before offset %i\n", (int)(src.current-src.source)); michael@0: } michael@0: #endif michael@0: goto cleanup; michael@0: } michael@0: michael@0: /* if we have a set of rules, let's make something of it */ michael@0: if(src.resultLen > 0 || src.removeSet != NULL) { michael@0: /* also, if we wanted to remove some contractions, we should make a tailoring */ michael@0: table = ucol_assembleTailoringTable(&src, status); michael@0: if(U_SUCCESS(*status)) { michael@0: // builder version michael@0: table->version[0] = UCOL_BUILDER_VERSION; michael@0: // no tailoring information on this level michael@0: table->version[1] = table->version[2] = table->version[3] = 0; michael@0: // set UCD version michael@0: u_getUnicodeVersion(table->UCDVersion); michael@0: // set UCA version michael@0: uprv_memcpy(table->UCAVersion, UCA->image->UCAVersion, sizeof(UVersionInfo)); michael@0: result = ucol_initCollator(table, 0, UCA, status); michael@0: if (U_FAILURE(*status)) { michael@0: goto cleanup; michael@0: } michael@0: result->hasRealData = TRUE; michael@0: result->freeImageOnClose = TRUE; michael@0: } else { michael@0: goto cleanup; michael@0: } michael@0: } else { /* no rules, but no error either */ michael@0: // must be only options michael@0: // We will init the collator from UCA michael@0: result = ucol_initCollator(UCA->image, 0, UCA, status); michael@0: // Check for null result michael@0: if (U_FAILURE(*status)) { michael@0: goto cleanup; michael@0: } michael@0: // And set only the options michael@0: UColOptionSet *opts = (UColOptionSet *)uprv_malloc(sizeof(UColOptionSet)); michael@0: /* test for NULL */ michael@0: if (opts == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: goto cleanup; michael@0: } michael@0: uprv_memcpy(opts, src.opts, sizeof(UColOptionSet)); michael@0: ucol_setOptionsFromHeader(result, opts, status); michael@0: result->freeOptionsOnClose = TRUE; michael@0: result->hasRealData = FALSE; michael@0: result->freeImageOnClose = FALSE; michael@0: } michael@0: michael@0: ucol_setReorderCodesFromParser(result, &src, status); michael@0: michael@0: if(U_SUCCESS(*status)) { michael@0: UChar *newRules; michael@0: result->dataVersion[0] = UCOL_BUILDER_VERSION; michael@0: if(rulesLength > 0) { michael@0: newRules = (UChar *)uprv_malloc((rulesLength+1)*U_SIZEOF_UCHAR); michael@0: /* test for NULL */ michael@0: if (newRules == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: goto cleanup; michael@0: } michael@0: uprv_memcpy(newRules, rules, rulesLength*U_SIZEOF_UCHAR); michael@0: newRules[rulesLength]=0; michael@0: result->rules = newRules; michael@0: result->rulesLength = rulesLength; michael@0: result->freeRulesOnClose = TRUE; michael@0: } michael@0: result->ucaRules = NULL; michael@0: result->actualLocale = NULL; michael@0: result->validLocale = NULL; michael@0: result->requestedLocale = NULL; michael@0: ucol_buildPermutationTable(result, status); michael@0: ucol_setAttribute(result, UCOL_STRENGTH, strength, status); michael@0: ucol_setAttribute(result, UCOL_NORMALIZATION_MODE, norm, status); michael@0: } else { michael@0: cleanup: michael@0: if(result != NULL) { michael@0: ucol_close(result); michael@0: } else { michael@0: if(table != NULL) { michael@0: uprv_free(table); michael@0: } michael@0: } michael@0: result = NULL; michael@0: } michael@0: michael@0: ucol_tok_closeTokenList(&src); michael@0: michael@0: return result; michael@0: } michael@0: michael@0: U_CAPI UCollator* U_EXPORT2 michael@0: ucol_openRules( const UChar *rules, michael@0: int32_t rulesLength, michael@0: UColAttributeValue normalizationMode, michael@0: UCollationStrength strength, michael@0: UParseError *parseError, michael@0: UErrorCode *status) michael@0: { michael@0: return ucol_openRulesForImport(rules, michael@0: rulesLength, michael@0: normalizationMode, michael@0: strength, michael@0: parseError, michael@0: ucol_tok_getRulesFromBundle, michael@0: NULL, michael@0: status); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucol_getRulesEx(const UCollator *coll, UColRuleOption delta, UChar *buffer, int32_t bufferLen) { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: int32_t len = 0; michael@0: int32_t UCAlen = 0; michael@0: const UChar* ucaRules = 0; michael@0: const UChar *rules = ucol_getRules(coll, &len); michael@0: if(delta == UCOL_FULL_RULES) { michael@0: /* take the UCA rules and append real rules at the end */ michael@0: /* UCA rules will be probably coming from the root RB */ michael@0: ucaRules = coll->ucaRules; michael@0: if (ucaRules) { michael@0: UCAlen = u_strlen(ucaRules); michael@0: } michael@0: /* michael@0: ucaRules = ures_getStringByKey(coll->rb,"UCARules",&UCAlen,&status); michael@0: UResourceBundle* cresb = ures_getByKeyWithFallback(coll->rb, "collations", NULL, &status); michael@0: UResourceBundle* uca = ures_getByKeyWithFallback(cresb, "UCA", NULL, &status); michael@0: ucaRules = ures_getStringByKey(uca,"Sequence",&UCAlen,&status); michael@0: ures_close(uca); michael@0: ures_close(cresb); michael@0: */ michael@0: } michael@0: if(U_FAILURE(status)) { michael@0: return 0; michael@0: } michael@0: if(buffer!=0 && bufferLen>0){ michael@0: *buffer=0; michael@0: if(UCAlen > 0) { michael@0: u_memcpy(buffer, ucaRules, uprv_min(UCAlen, bufferLen)); michael@0: } michael@0: if(len > 0 && bufferLen > UCAlen) { michael@0: u_memcpy(buffer+UCAlen, rules, uprv_min(len, bufferLen-UCAlen)); michael@0: } michael@0: } michael@0: return u_terminateUChars(buffer, bufferLen, len+UCAlen, &status); michael@0: } michael@0: michael@0: static const UChar _NUL = 0; michael@0: michael@0: U_CAPI const UChar* U_EXPORT2 michael@0: ucol_getRules( const UCollator *coll, michael@0: int32_t *length) michael@0: { michael@0: if(coll->rules != NULL) { michael@0: *length = coll->rulesLength; michael@0: return coll->rules; michael@0: } michael@0: else { michael@0: *length = 0; michael@0: return &_NUL; michael@0: } michael@0: } michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: ucol_equals(const UCollator *source, const UCollator *target) { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: // if pointers are equal, collators are equal michael@0: if(source == target) { michael@0: return TRUE; michael@0: } michael@0: int32_t i = 0, j = 0; michael@0: // if any of attributes are different, collators are not equal michael@0: for(i = 0; i < UCOL_ATTRIBUTE_COUNT; i++) { michael@0: if(ucol_getAttribute(source, (UColAttribute)i, &status) != ucol_getAttribute(target, (UColAttribute)i, &status) || U_FAILURE(status)) { michael@0: return FALSE; michael@0: } michael@0: } michael@0: if (source->reorderCodesLength != target->reorderCodesLength){ michael@0: return FALSE; michael@0: } michael@0: for (i = 0; i < source->reorderCodesLength; i++) { michael@0: if(source->reorderCodes[i] != target->reorderCodes[i]) { michael@0: return FALSE; michael@0: } michael@0: } michael@0: michael@0: int32_t sourceRulesLen = 0, targetRulesLen = 0; michael@0: const UChar *sourceRules = ucol_getRules(source, &sourceRulesLen); michael@0: const UChar *targetRules = ucol_getRules(target, &targetRulesLen); michael@0: michael@0: if(sourceRulesLen == targetRulesLen && u_strncmp(sourceRules, targetRules, sourceRulesLen) == 0) { michael@0: // all the attributes are equal and the rules are equal - collators are equal michael@0: return(TRUE); michael@0: } michael@0: // hard part, need to construct tree from rules and see if they yield the same tailoring michael@0: UBool result = TRUE; michael@0: UParseError parseError; michael@0: UColTokenParser sourceParser, targetParser; michael@0: int32_t sourceListLen = 0, targetListLen = 0; michael@0: ucol_tok_initTokenList(&sourceParser, sourceRules, sourceRulesLen, source->UCA, ucol_tok_getRulesFromBundle, NULL, &status); michael@0: ucol_tok_initTokenList(&targetParser, targetRules, targetRulesLen, target->UCA, ucol_tok_getRulesFromBundle, NULL, &status); michael@0: sourceListLen = ucol_tok_assembleTokenList(&sourceParser, &parseError, &status); michael@0: targetListLen = ucol_tok_assembleTokenList(&targetParser, &parseError, &status); michael@0: michael@0: if(sourceListLen != targetListLen) { michael@0: // different number of resets michael@0: result = FALSE; michael@0: } else { michael@0: UColToken *sourceReset = NULL, *targetReset = NULL; michael@0: UChar *sourceResetString = NULL, *targetResetString = NULL; michael@0: int32_t sourceStringLen = 0, targetStringLen = 0; michael@0: for(i = 0; i < sourceListLen; i++) { michael@0: sourceReset = sourceParser.lh[i].reset; michael@0: sourceResetString = sourceParser.source+(sourceReset->source & 0xFFFFFF); michael@0: sourceStringLen = sourceReset->source >> 24; michael@0: for(j = 0; j < sourceListLen; j++) { michael@0: targetReset = targetParser.lh[j].reset; michael@0: targetResetString = targetParser.source+(targetReset->source & 0xFFFFFF); michael@0: targetStringLen = targetReset->source >> 24; michael@0: if(sourceStringLen == targetStringLen && (u_strncmp(sourceResetString, targetResetString, sourceStringLen) == 0)) { michael@0: sourceReset = sourceParser.lh[i].first; michael@0: targetReset = targetParser.lh[j].first; michael@0: while(sourceReset != NULL && targetReset != NULL) { michael@0: sourceResetString = sourceParser.source+(sourceReset->source & 0xFFFFFF); michael@0: sourceStringLen = sourceReset->source >> 24; michael@0: targetResetString = targetParser.source+(targetReset->source & 0xFFFFFF); michael@0: targetStringLen = targetReset->source >> 24; michael@0: if(sourceStringLen != targetStringLen || (u_strncmp(sourceResetString, targetResetString, sourceStringLen) != 0)) { michael@0: result = FALSE; michael@0: goto returnResult; michael@0: } michael@0: // probably also need to check the expansions michael@0: if(sourceReset->expansion) { michael@0: if(!targetReset->expansion) { michael@0: result = FALSE; michael@0: goto returnResult; michael@0: } else { michael@0: // compare expansions michael@0: sourceResetString = sourceParser.source+(sourceReset->expansion& 0xFFFFFF); michael@0: sourceStringLen = sourceReset->expansion >> 24; michael@0: targetResetString = targetParser.source+(targetReset->expansion & 0xFFFFFF); michael@0: targetStringLen = targetReset->expansion >> 24; michael@0: if(sourceStringLen != targetStringLen || (u_strncmp(sourceResetString, targetResetString, sourceStringLen) != 0)) { michael@0: result = FALSE; michael@0: goto returnResult; michael@0: } michael@0: } michael@0: } else { michael@0: if(targetReset->expansion) { michael@0: result = FALSE; michael@0: goto returnResult; michael@0: } michael@0: } michael@0: sourceReset = sourceReset->next; michael@0: targetReset = targetReset->next; michael@0: } michael@0: if(sourceReset != targetReset) { // at least one is not NULL michael@0: // there are more tailored elements in one list michael@0: result = FALSE; michael@0: goto returnResult; michael@0: } michael@0: michael@0: michael@0: break; michael@0: } michael@0: } michael@0: // couldn't find the reset anchor, so the collators are not equal michael@0: if(j == sourceListLen) { michael@0: result = FALSE; michael@0: goto returnResult; michael@0: } michael@0: } michael@0: } michael@0: michael@0: returnResult: michael@0: ucol_tok_closeTokenList(&sourceParser); michael@0: ucol_tok_closeTokenList(&targetParser); michael@0: return result; michael@0: michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucol_getDisplayName( const char *objLoc, michael@0: const char *dispLoc, michael@0: UChar *result, michael@0: int32_t resultLength, michael@0: UErrorCode *status) michael@0: { michael@0: U_NAMESPACE_USE michael@0: michael@0: if(U_FAILURE(*status)) return -1; michael@0: UnicodeString dst; michael@0: if(!(result==NULL && resultLength==0)) { michael@0: // NULL destination for pure preflighting: empty dummy string michael@0: // otherwise, alias the destination buffer michael@0: dst.setTo(result, 0, resultLength); michael@0: } michael@0: Collator::getDisplayName(Locale(objLoc), Locale(dispLoc), dst); michael@0: return dst.extract(result, resultLength, *status); michael@0: } michael@0: michael@0: U_CAPI const char* U_EXPORT2 michael@0: ucol_getAvailable(int32_t index) michael@0: { michael@0: int32_t count = 0; michael@0: const Locale *loc = Collator::getAvailableLocales(count); michael@0: if (loc != NULL && index < count) { michael@0: return loc[index].getName(); michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucol_countAvailable() michael@0: { michael@0: int32_t count = 0; michael@0: Collator::getAvailableLocales(count); michael@0: return count; michael@0: } michael@0: michael@0: #if !UCONFIG_NO_SERVICE michael@0: U_CAPI UEnumeration* U_EXPORT2 michael@0: ucol_openAvailableLocales(UErrorCode *status) { michael@0: U_NAMESPACE_USE michael@0: michael@0: // This is a wrapper over Collator::getAvailableLocales() michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: StringEnumeration *s = icu::Collator::getAvailableLocales(); michael@0: if (s == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return NULL; michael@0: } michael@0: return uenum_openFromStringEnumeration(s, status); michael@0: } michael@0: #endif michael@0: michael@0: // Note: KEYWORDS[0] != RESOURCE_NAME - alan michael@0: michael@0: static const char RESOURCE_NAME[] = "collations"; michael@0: michael@0: static const char* const KEYWORDS[] = { "collation" }; michael@0: michael@0: #define KEYWORD_COUNT (sizeof(KEYWORDS)/sizeof(KEYWORDS[0])) michael@0: michael@0: U_CAPI UEnumeration* U_EXPORT2 michael@0: ucol_getKeywords(UErrorCode *status) { michael@0: UEnumeration *result = NULL; michael@0: if (U_SUCCESS(*status)) { michael@0: return uenum_openCharStringsEnumeration(KEYWORDS, KEYWORD_COUNT, status); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: U_CAPI UEnumeration* U_EXPORT2 michael@0: ucol_getKeywordValues(const char *keyword, UErrorCode *status) { michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: // hard-coded to accept exactly one collation keyword michael@0: // modify if additional collation keyword is added later michael@0: if (keyword==NULL || uprv_strcmp(keyword, KEYWORDS[0])!=0) michael@0: { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: return ures_getKeywordValues(U_ICUDATA_COLL, RESOURCE_NAME, status); michael@0: } michael@0: michael@0: static const UEnumeration defaultKeywordValues = { michael@0: NULL, michael@0: NULL, michael@0: ulist_close_keyword_values_iterator, michael@0: ulist_count_keyword_values, michael@0: uenum_unextDefault, michael@0: ulist_next_keyword_value, michael@0: ulist_reset_keyword_values_iterator michael@0: }; michael@0: michael@0: #include michael@0: michael@0: U_CAPI UEnumeration* U_EXPORT2 michael@0: ucol_getKeywordValuesForLocale(const char* /*key*/, const char* locale, michael@0: UBool /*commonlyUsed*/, UErrorCode* status) { michael@0: /* Get the locale base name. */ michael@0: char localeBuffer[ULOC_FULLNAME_CAPACITY] = ""; michael@0: uloc_getBaseName(locale, localeBuffer, sizeof(localeBuffer), status); michael@0: michael@0: /* Create the 2 lists michael@0: * -values is the temp location for the keyword values michael@0: * -results hold the actual list used by the UEnumeration object michael@0: */ michael@0: UList *values = ulist_createEmptyList(status); michael@0: UList *results = ulist_createEmptyList(status); michael@0: UEnumeration *en = (UEnumeration *)uprv_malloc(sizeof(UEnumeration)); michael@0: if (U_FAILURE(*status) || en == NULL) { michael@0: if (en == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: } else { michael@0: uprv_free(en); michael@0: } michael@0: ulist_deleteList(values); michael@0: ulist_deleteList(results); michael@0: return NULL; michael@0: } michael@0: michael@0: memcpy(en, &defaultKeywordValues, sizeof(UEnumeration)); michael@0: en->context = results; michael@0: michael@0: /* Open the resource bundle for collation with the given locale. */ michael@0: UResourceBundle bundle, collations, collres, defres; michael@0: ures_initStackObject(&bundle); michael@0: ures_initStackObject(&collations); michael@0: ures_initStackObject(&collres); michael@0: ures_initStackObject(&defres); michael@0: michael@0: ures_openFillIn(&bundle, U_ICUDATA_COLL, localeBuffer, status); michael@0: michael@0: while (U_SUCCESS(*status)) { michael@0: ures_getByKey(&bundle, RESOURCE_NAME, &collations, status); michael@0: ures_resetIterator(&collations); michael@0: while (U_SUCCESS(*status) && ures_hasNext(&collations)) { michael@0: ures_getNextResource(&collations, &collres, status); michael@0: const char *key = ures_getKey(&collres); michael@0: /* If the key is default, get the string and store it in results list only michael@0: * if results list is empty. michael@0: */ michael@0: if (uprv_strcmp(key, "default") == 0) { michael@0: if (ulist_getListSize(results) == 0) { michael@0: char *defcoll = (char *)uprv_malloc(sizeof(char) * ULOC_KEYWORDS_CAPACITY); michael@0: int32_t defcollLength = ULOC_KEYWORDS_CAPACITY; michael@0: michael@0: ures_getNextResource(&collres, &defres, status); michael@0: #if U_CHARSET_FAMILY==U_ASCII_FAMILY michael@0: /* optimize - use the utf-8 string */ michael@0: ures_getUTF8String(&defres, defcoll, &defcollLength, TRUE, status); michael@0: #else michael@0: { michael@0: const UChar* defString = ures_getString(&defres, &defcollLength, status); michael@0: if(U_SUCCESS(*status)) { michael@0: if(defcollLength+1 > ULOC_KEYWORDS_CAPACITY) { michael@0: *status = U_BUFFER_OVERFLOW_ERROR; michael@0: } else { michael@0: u_UCharsToChars(defString, defcoll, defcollLength+1); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: ulist_addItemBeginList(results, defcoll, TRUE, status); michael@0: } michael@0: } else { michael@0: ulist_addItemEndList(values, key, FALSE, status); michael@0: } michael@0: } michael@0: michael@0: /* If the locale is "" this is root so exit. */ michael@0: if (uprv_strlen(localeBuffer) == 0) { michael@0: break; michael@0: } michael@0: /* Get the parent locale and open a new resource bundle. */ michael@0: uloc_getParent(localeBuffer, localeBuffer, sizeof(localeBuffer), status); michael@0: ures_openFillIn(&bundle, U_ICUDATA_COLL, localeBuffer, status); michael@0: } michael@0: michael@0: ures_close(&defres); michael@0: ures_close(&collres); michael@0: ures_close(&collations); michael@0: ures_close(&bundle); michael@0: michael@0: if (U_SUCCESS(*status)) { michael@0: char *value = NULL; michael@0: ulist_resetList(values); michael@0: while ((value = (char *)ulist_getNext(values)) != NULL) { michael@0: if (!ulist_containsString(results, value, (int32_t)uprv_strlen(value))) { michael@0: ulist_addItemEndList(results, value, FALSE, status); michael@0: if (U_FAILURE(*status)) { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: ulist_deleteList(values); michael@0: michael@0: if (U_FAILURE(*status)){ michael@0: uenum_close(en); michael@0: en = NULL; michael@0: } else { michael@0: ulist_resetList(results); michael@0: } michael@0: michael@0: return en; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucol_getFunctionalEquivalent(char* result, int32_t resultCapacity, michael@0: const char* keyword, const char* locale, michael@0: UBool* isAvailable, UErrorCode* status) michael@0: { michael@0: // N.B.: Resource name is "collations" but keyword is "collation" michael@0: return ures_getFunctionalEquivalent(result, resultCapacity, U_ICUDATA_COLL, michael@0: "collations", keyword, locale, michael@0: isAvailable, TRUE, status); michael@0: } michael@0: michael@0: /* returns the locale name the collation data comes from */ michael@0: U_CAPI const char * U_EXPORT2 michael@0: ucol_getLocale(const UCollator *coll, ULocDataLocaleType type, UErrorCode *status) { michael@0: return ucol_getLocaleByType(coll, type, status); michael@0: } michael@0: michael@0: U_CAPI const char * U_EXPORT2 michael@0: ucol_getLocaleByType(const UCollator *coll, ULocDataLocaleType type, UErrorCode *status) { michael@0: const char *result = NULL; michael@0: if(status == NULL || U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: UTRACE_ENTRY(UTRACE_UCOL_GETLOCALE); michael@0: UTRACE_DATA1(UTRACE_INFO, "coll=%p", coll); michael@0: michael@0: if(coll->delegate!=NULL) { michael@0: return ((const Collator*)coll->delegate)->getLocale(type, *status).getName(); michael@0: } michael@0: switch(type) { michael@0: case ULOC_ACTUAL_LOCALE: michael@0: result = coll->actualLocale; michael@0: break; michael@0: case ULOC_VALID_LOCALE: michael@0: result = coll->validLocale; michael@0: break; michael@0: case ULOC_REQUESTED_LOCALE: michael@0: result = coll->requestedLocale; michael@0: break; michael@0: default: michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: } michael@0: UTRACE_DATA1(UTRACE_INFO, "result = %s", result); michael@0: UTRACE_EXIT_STATUS(*status); michael@0: return result; michael@0: } michael@0: michael@0: U_CFUNC void U_EXPORT2 michael@0: ucol_setReqValidLocales(UCollator *coll, char *requestedLocaleToAdopt, char *validLocaleToAdopt, char *actualLocaleToAdopt) michael@0: { michael@0: if (coll) { michael@0: if (coll->validLocale) { michael@0: uprv_free(coll->validLocale); michael@0: } michael@0: coll->validLocale = validLocaleToAdopt; michael@0: if (coll->requestedLocale) { // should always have michael@0: uprv_free(coll->requestedLocale); michael@0: } michael@0: coll->requestedLocale = requestedLocaleToAdopt; michael@0: if (coll->actualLocale) { michael@0: uprv_free(coll->actualLocale); michael@0: } michael@0: coll->actualLocale = actualLocaleToAdopt; michael@0: } michael@0: } michael@0: michael@0: U_CAPI USet * U_EXPORT2 michael@0: ucol_getTailoredSet(const UCollator *coll, UErrorCode *status) michael@0: { michael@0: U_NAMESPACE_USE michael@0: michael@0: if(status == NULL || U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: if(coll == NULL || coll->UCA == NULL) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: UParseError parseError; michael@0: UColTokenParser src; michael@0: int32_t rulesLen = 0; michael@0: const UChar *rules = ucol_getRules(coll, &rulesLen); michael@0: UBool startOfRules = TRUE; michael@0: // we internally use the C++ class, for the following reasons: michael@0: // 1. we need to utilize canonical iterator, which is a C++ only class michael@0: // 2. canonical iterator returns UnicodeStrings - USet cannot take them michael@0: // 3. USet is internally really UnicodeSet, C is just a wrapper michael@0: UnicodeSet *tailored = new UnicodeSet(); michael@0: UnicodeString pattern; michael@0: UnicodeString empty; michael@0: CanonicalIterator it(empty, *status); michael@0: michael@0: michael@0: // The idea is to tokenize the rule set. For each non-reset token, michael@0: // we add all the canonicaly equivalent FCD sequences michael@0: ucol_tok_initTokenList(&src, rules, rulesLen, coll->UCA, ucol_tok_getRulesFromBundle, NULL, status); michael@0: while (ucol_tok_parseNextToken(&src, startOfRules, &parseError, status) != NULL) { michael@0: startOfRules = FALSE; michael@0: if(src.parsedToken.strength != UCOL_TOK_RESET) { michael@0: const UChar *stuff = src.source+(src.parsedToken.charsOffset); michael@0: it.setSource(UnicodeString(stuff, src.parsedToken.charsLen), *status); michael@0: pattern = it.next(); michael@0: while(!pattern.isBogus()) { michael@0: if(Normalizer::quickCheck(pattern, UNORM_FCD, *status) != UNORM_NO) { michael@0: tailored->add(pattern); michael@0: } michael@0: pattern = it.next(); michael@0: } michael@0: } michael@0: } michael@0: ucol_tok_closeTokenList(&src); michael@0: return (USet *)tailored; michael@0: } michael@0: michael@0: /* michael@0: * Collation Reordering michael@0: */ michael@0: michael@0: void ucol_setReorderCodesFromParser(UCollator *coll, UColTokenParser *parser, UErrorCode *status) { michael@0: if (U_FAILURE(*status)) { michael@0: return; michael@0: } michael@0: michael@0: if (parser->reorderCodesLength == 0 || parser->reorderCodes == NULL) { michael@0: return; michael@0: } michael@0: michael@0: coll->reorderCodesLength = 0; michael@0: if (coll->reorderCodes != NULL && coll->freeReorderCodesOnClose == TRUE) { michael@0: uprv_free(coll->reorderCodes); michael@0: } michael@0: coll->reorderCodes = NULL; michael@0: coll->freeReorderCodesOnClose = FALSE; michael@0: michael@0: if (coll->defaultReorderCodes != NULL && coll->freeDefaultReorderCodesOnClose == TRUE) { michael@0: uprv_free(coll->defaultReorderCodes); michael@0: } michael@0: coll->freeDefaultReorderCodesOnClose = FALSE; michael@0: coll->defaultReorderCodesLength = parser->reorderCodesLength; michael@0: coll->defaultReorderCodes = (int32_t*) uprv_malloc(coll->defaultReorderCodesLength * sizeof(int32_t)); michael@0: if (coll->defaultReorderCodes == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return; michael@0: } michael@0: uprv_memcpy(coll->defaultReorderCodes, parser->reorderCodes, coll->defaultReorderCodesLength * sizeof(int32_t)); michael@0: coll->freeDefaultReorderCodesOnClose = TRUE; michael@0: michael@0: coll->reorderCodesLength = parser->reorderCodesLength; michael@0: coll->reorderCodes = (int32_t*) uprv_malloc(coll->reorderCodesLength * sizeof(int32_t)); michael@0: if (coll->reorderCodes == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return; michael@0: } michael@0: uprv_memcpy(coll->reorderCodes, parser->reorderCodes, coll->reorderCodesLength * sizeof(int32_t)); michael@0: coll->freeReorderCodesOnClose = TRUE; michael@0: } michael@0: michael@0: /* michael@0: * Data is stored in the reorder code to lead byte table as: michael@0: * index count - unsigned short (2 bytes) - number of index entries michael@0: * data size - unsigned short (2 bytes) - number of unsigned short data elements michael@0: * index[index count] - array of 2 unsigned shorts (4 bytes each entry) michael@0: * - reorder code, offset michael@0: * - index is sorted by reorder code michael@0: * - if an offset has the high bit set then it is not an offset but a single data entry michael@0: * once the high bit is stripped off michael@0: * data[data size] - array of unsigned short (2 bytes each entry) michael@0: * - the data is an usigned short count followed by count number michael@0: * of lead bytes stored in an unsigned short michael@0: */ michael@0: U_CFUNC int U_EXPORT2 michael@0: ucol_getLeadBytesForReorderCode(const UCollator *uca, int reorderCode, uint16_t* returnLeadBytes, int returnCapacity) { michael@0: uint16_t reorderCodeIndexLength = *((uint16_t*) ((uint8_t *)uca->image + uca->image->scriptToLeadByte)); michael@0: uint16_t* reorderCodeIndex = (uint16_t*) ((uint8_t *)uca->image + uca->image->scriptToLeadByte + 2 *sizeof(uint16_t)); michael@0: michael@0: // reorder code index is 2 uint16_t's - reorder code + offset michael@0: for (int i = 0; i < reorderCodeIndexLength; i++) { michael@0: if (reorderCode == reorderCodeIndex[i*2]) { michael@0: uint16_t dataOffset = reorderCodeIndex[(i*2) + 1]; michael@0: if ((dataOffset & 0x8000) == 0x8000) { michael@0: // offset isn't offset but instead is a single data element michael@0: if (returnCapacity >= 1) { michael@0: returnLeadBytes[0] = dataOffset & ~0x8000; michael@0: return 1; michael@0: } michael@0: return 0; michael@0: } michael@0: uint16_t* dataOffsetBase = (uint16_t*) ((uint8_t *)reorderCodeIndex + reorderCodeIndexLength * (2 * sizeof(uint16_t))); michael@0: uint16_t leadByteCount = *(dataOffsetBase + dataOffset); michael@0: leadByteCount = leadByteCount > returnCapacity ? returnCapacity : leadByteCount; michael@0: uprv_memcpy(returnLeadBytes, dataOffsetBase + dataOffset + 1, leadByteCount * sizeof(uint16_t)); michael@0: return leadByteCount; michael@0: } michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: /* michael@0: * Data is stored in the lead byte to reorder code table as: michael@0: * index count - unsigned short (2 bytes) - number of index entries michael@0: * data size - unsigned short (2 bytes) - number of unsigned short data elements michael@0: * index[index count] - array of unsigned short (2 bytes each entry) michael@0: * - index is sorted by lead byte michael@0: * - if an index has the high bit set then it is not an index but a single data entry michael@0: * once the high bit is stripped off michael@0: * data[data size] - array of unsigned short (2 bytes each entry) michael@0: * - the data is an usigned short count followed by count number of reorder codes michael@0: */ michael@0: U_CFUNC int U_EXPORT2 michael@0: ucol_getReorderCodesForLeadByte(const UCollator *uca, int leadByte, int16_t* returnReorderCodes, int returnCapacity) { michael@0: uint16_t* leadByteTable = ((uint16_t*) ((uint8_t *)uca->image + uca->image->leadByteToScript)); michael@0: uint16_t leadByteIndexLength = *leadByteTable; michael@0: if (leadByte >= leadByteIndexLength) { michael@0: return 0; michael@0: } michael@0: uint16_t leadByteIndex = *(leadByteTable + (2 + leadByte)); michael@0: michael@0: if ((leadByteIndex & 0x8000) == 0x8000) { michael@0: // offset isn't offset but instead is a single data element michael@0: if (returnCapacity >= 1) { michael@0: returnReorderCodes[0] = leadByteIndex & ~0x8000; michael@0: return 1; michael@0: } michael@0: return 0; michael@0: } michael@0: //uint16_t* dataOffsetBase = leadByteTable + (2 + leadByteIndexLength); michael@0: uint16_t* reorderCodeData = leadByteTable + (2 + leadByteIndexLength) + leadByteIndex; michael@0: uint16_t reorderCodeCount = *reorderCodeData > returnCapacity ? returnCapacity : *reorderCodeData; michael@0: uprv_memcpy(returnReorderCodes, reorderCodeData + 1, reorderCodeCount * sizeof(uint16_t)); michael@0: return reorderCodeCount; michael@0: } michael@0: michael@0: // used to mark ignorable reorder code slots michael@0: static const int32_t UCOL_REORDER_CODE_IGNORE = UCOL_REORDER_CODE_LIMIT + 1; michael@0: michael@0: U_CFUNC void U_EXPORT2 michael@0: ucol_buildPermutationTable(UCollator *coll, UErrorCode *status) { michael@0: uint16_t leadBytesSize = 256; michael@0: uint16_t leadBytes[256]; michael@0: michael@0: // The lowest byte that hasn't been assigned a mapping michael@0: int toBottom = 0x03; michael@0: // The highest byte that hasn't been assigned a mapping - don't include the special or trailing michael@0: int toTop = 0xe4; michael@0: michael@0: // are we filling from the bottom? michael@0: bool fromTheBottom = true; michael@0: int32_t reorderCodesIndex = -1; michael@0: michael@0: // lead bytes that have alread been assigned to the permutation table michael@0: bool newLeadByteUsed[256]; michael@0: // permutation table slots that have already been filled michael@0: bool permutationSlotFilled[256]; michael@0: michael@0: // nothing to do michael@0: if(U_FAILURE(*status) || coll == NULL) { michael@0: return; michael@0: } michael@0: michael@0: // clear the reordering michael@0: if (coll->reorderCodes == NULL || coll->reorderCodesLength == 0 michael@0: || (coll->reorderCodesLength == 1 && coll->reorderCodes[0] == UCOL_REORDER_CODE_NONE)) { michael@0: if (coll->leadBytePermutationTable != NULL) { michael@0: if (coll->freeLeadBytePermutationTableOnClose) { michael@0: uprv_free(coll->leadBytePermutationTable); michael@0: } michael@0: coll->leadBytePermutationTable = NULL; michael@0: coll->freeLeadBytePermutationTableOnClose = FALSE; michael@0: coll->reorderCodesLength = 0; michael@0: } michael@0: return; michael@0: } michael@0: michael@0: // set reordering to the default reordering michael@0: if (coll->reorderCodes[0] == UCOL_REORDER_CODE_DEFAULT) { michael@0: if (coll->reorderCodesLength != 1) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return; michael@0: } michael@0: if (coll->freeReorderCodesOnClose == TRUE) { michael@0: uprv_free(coll->reorderCodes); michael@0: } michael@0: coll->reorderCodes = NULL; michael@0: coll->freeReorderCodesOnClose = FALSE; michael@0: michael@0: if (coll->leadBytePermutationTable != NULL && coll->freeLeadBytePermutationTableOnClose == TRUE) { michael@0: uprv_free(coll->leadBytePermutationTable); michael@0: } michael@0: coll->leadBytePermutationTable = NULL; michael@0: coll->freeLeadBytePermutationTableOnClose = FALSE; michael@0: michael@0: if (coll->defaultReorderCodesLength == 0) { michael@0: return; michael@0: } michael@0: michael@0: coll->reorderCodes = (int32_t*)uprv_malloc(coll->defaultReorderCodesLength * sizeof(int32_t)); michael@0: if (coll->reorderCodes == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return; michael@0: } michael@0: coll->freeReorderCodesOnClose = TRUE; michael@0: coll->reorderCodesLength = coll->defaultReorderCodesLength; michael@0: uprv_memcpy(coll->reorderCodes, coll->defaultReorderCodes, coll->reorderCodesLength * sizeof(int32_t)); michael@0: } michael@0: michael@0: if (coll->leadBytePermutationTable == NULL) { michael@0: coll->leadBytePermutationTable = (uint8_t*)uprv_malloc(256*sizeof(uint8_t)); michael@0: if (coll->leadBytePermutationTable == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return; michael@0: } michael@0: coll->freeLeadBytePermutationTableOnClose = TRUE; michael@0: } michael@0: michael@0: int32_t internalReorderCodesLength = coll->reorderCodesLength + (UCOL_REORDER_CODE_LIMIT - UCOL_REORDER_CODE_FIRST); michael@0: LocalMemory internalReorderCodes((int32_t*)uprv_malloc(internalReorderCodesLength * sizeof(int32_t))); michael@0: if (internalReorderCodes.isNull()) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: if (coll->leadBytePermutationTable != NULL && coll->freeLeadBytePermutationTableOnClose == TRUE) { michael@0: uprv_free(coll->leadBytePermutationTable); michael@0: } michael@0: coll->leadBytePermutationTable = NULL; michael@0: coll->freeLeadBytePermutationTableOnClose = FALSE; michael@0: return; michael@0: } michael@0: michael@0: // prefill the reordering codes with the leading entries michael@0: for (uint32_t codeIndex = 0; codeIndex < (UCOL_REORDER_CODE_LIMIT - UCOL_REORDER_CODE_FIRST); codeIndex++) { michael@0: internalReorderCodes[codeIndex] = UCOL_REORDER_CODE_FIRST + codeIndex; michael@0: } michael@0: for (int32_t codeIndex = 0; codeIndex < coll->reorderCodesLength; codeIndex++) { michael@0: uint32_t reorderCodesCode = coll->reorderCodes[codeIndex]; michael@0: internalReorderCodes[codeIndex + (UCOL_REORDER_CODE_LIMIT - UCOL_REORDER_CODE_FIRST)] = reorderCodesCode; michael@0: if (reorderCodesCode >= UCOL_REORDER_CODE_FIRST && reorderCodesCode < UCOL_REORDER_CODE_LIMIT) { michael@0: internalReorderCodes[reorderCodesCode - UCOL_REORDER_CODE_FIRST] = UCOL_REORDER_CODE_IGNORE; michael@0: } michael@0: } michael@0: michael@0: for (int i = 0; i < 256; i++) { michael@0: if (i < toBottom || i > toTop) { michael@0: permutationSlotFilled[i] = true; michael@0: newLeadByteUsed[i] = true; michael@0: coll->leadBytePermutationTable[i] = i; michael@0: } else { michael@0: permutationSlotFilled[i] = false; michael@0: newLeadByteUsed[i] = false; michael@0: coll->leadBytePermutationTable[i] = 0; michael@0: } michael@0: } michael@0: michael@0: /* Start from the front of the list and place each script we encounter at the michael@0: * earliest possible locatation in the permutation table. If we encounter michael@0: * UNKNOWN, start processing from the back, and place each script in the last michael@0: * possible location. At each step, we also need to make sure that any scripts michael@0: * that need to not be moved are copied to their same location in the final table. michael@0: */ michael@0: for (int reorderCodesCount = 0; reorderCodesCount < internalReorderCodesLength; reorderCodesCount++) { michael@0: reorderCodesIndex += fromTheBottom ? 1 : -1; michael@0: int32_t next = internalReorderCodes[reorderCodesIndex]; michael@0: if (next == UCOL_REORDER_CODE_IGNORE) { michael@0: continue; michael@0: } michael@0: if (next == USCRIPT_UNKNOWN) { michael@0: if (fromTheBottom == false) { michael@0: // double turnaround michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: if (coll->leadBytePermutationTable != NULL && coll->freeLeadBytePermutationTableOnClose == TRUE) { michael@0: uprv_free(coll->leadBytePermutationTable); michael@0: } michael@0: coll->leadBytePermutationTable = NULL; michael@0: coll->freeLeadBytePermutationTableOnClose = FALSE; michael@0: coll->reorderCodesLength = 0; michael@0: return; michael@0: } michael@0: fromTheBottom = false; michael@0: reorderCodesIndex = internalReorderCodesLength; michael@0: continue; michael@0: } michael@0: michael@0: uint16_t leadByteCount = ucol_getLeadBytesForReorderCode(coll->UCA, next, leadBytes, leadBytesSize); michael@0: if (fromTheBottom) { michael@0: for (int leadByteIndex = 0; leadByteIndex < leadByteCount; leadByteIndex++) { michael@0: // don't place a lead byte twice in the permutation table michael@0: if (permutationSlotFilled[leadBytes[leadByteIndex]]) { michael@0: // lead byte already used michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: if (coll->leadBytePermutationTable != NULL && coll->freeLeadBytePermutationTableOnClose == TRUE) { michael@0: uprv_free(coll->leadBytePermutationTable); michael@0: } michael@0: coll->leadBytePermutationTable = NULL; michael@0: coll->freeLeadBytePermutationTableOnClose = FALSE; michael@0: coll->reorderCodesLength = 0; michael@0: return; michael@0: } michael@0: michael@0: coll->leadBytePermutationTable[leadBytes[leadByteIndex]] = toBottom; michael@0: newLeadByteUsed[toBottom] = true; michael@0: permutationSlotFilled[leadBytes[leadByteIndex]] = true; michael@0: toBottom++; michael@0: } michael@0: } else { michael@0: for (int leadByteIndex = leadByteCount - 1; leadByteIndex >= 0; leadByteIndex--) { michael@0: // don't place a lead byte twice in the permutation table michael@0: if (permutationSlotFilled[leadBytes[leadByteIndex]]) { michael@0: // lead byte already used michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: if (coll->leadBytePermutationTable != NULL && coll->freeLeadBytePermutationTableOnClose == TRUE) { michael@0: uprv_free(coll->leadBytePermutationTable); michael@0: } michael@0: coll->leadBytePermutationTable = NULL; michael@0: coll->freeLeadBytePermutationTableOnClose = FALSE; michael@0: coll->reorderCodesLength = 0; michael@0: return; michael@0: } michael@0: michael@0: coll->leadBytePermutationTable[leadBytes[leadByteIndex]] = toTop; michael@0: newLeadByteUsed[toTop] = true; michael@0: permutationSlotFilled[leadBytes[leadByteIndex]] = true; michael@0: toTop--; michael@0: } michael@0: } michael@0: } michael@0: michael@0: #ifdef REORDER_DEBUG michael@0: fprintf(stdout, "\n@@@@ Partial Script Reordering Table\n"); michael@0: for (int i = 0; i < 256; i++) { michael@0: fprintf(stdout, "\t%02x = %02x\n", i, coll->leadBytePermutationTable[i]); michael@0: } michael@0: fprintf(stdout, "\n@@@@ Lead Byte Used Table\n"); michael@0: for (int i = 0; i < 256; i++) { michael@0: fprintf(stdout, "\t%02x = %02x\n", i, newLeadByteUsed[i]); michael@0: } michael@0: fprintf(stdout, "\n@@@@ Permutation Slot Filled Table\n"); michael@0: for (int i = 0; i < 256; i++) { michael@0: fprintf(stdout, "\t%02x = %02x\n", i, permutationSlotFilled[i]); michael@0: } michael@0: #endif michael@0: michael@0: /* Copy everything that's left over */ michael@0: int reorderCode = 0; michael@0: for (int i = 0; i < 256; i++) { michael@0: if (!permutationSlotFilled[i]) { michael@0: while (reorderCode < 256 && newLeadByteUsed[reorderCode]) { michael@0: reorderCode++; michael@0: } michael@0: coll->leadBytePermutationTable[i] = reorderCode; michael@0: permutationSlotFilled[i] = true; michael@0: newLeadByteUsed[reorderCode] = true; michael@0: } michael@0: } michael@0: michael@0: #ifdef REORDER_DEBUG michael@0: fprintf(stdout, "\n@@@@ Script Reordering Table\n"); michael@0: for (int i = 0; i < 256; i++) { michael@0: fprintf(stdout, "\t%02x = %02x\n", i, coll->leadBytePermutationTable[i]); michael@0: } michael@0: #endif michael@0: michael@0: // force a regen of the latin one table since it is affected by the script reordering michael@0: coll->latinOneRegenTable = TRUE; michael@0: ucol_updateInternalState(coll, status); michael@0: } michael@0: michael@0: #endif /* #if !UCONFIG_NO_COLLATION */