Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************* |
michael@0 | 3 | * Copyright (C) 1996-2013, International Business Machines |
michael@0 | 4 | * Corporation and others. All Rights Reserved. |
michael@0 | 5 | ******************************************************************************* |
michael@0 | 6 | * file name: ucol_res.cpp |
michael@0 | 7 | * encoding: US-ASCII |
michael@0 | 8 | * tab size: 8 (not used) |
michael@0 | 9 | * indentation:4 |
michael@0 | 10 | * |
michael@0 | 11 | * Description: |
michael@0 | 12 | * This file contains dependencies that the collation run-time doesn't normally |
michael@0 | 13 | * need. This mainly contains resource bundle usage and collation meta information |
michael@0 | 14 | * |
michael@0 | 15 | * Modification history |
michael@0 | 16 | * Date Name Comments |
michael@0 | 17 | * 1996-1999 various members of ICU team maintained C API for collation framework |
michael@0 | 18 | * 02/16/2001 synwee Added internal method getPrevSpecialCE |
michael@0 | 19 | * 03/01/2001 synwee Added maxexpansion functionality. |
michael@0 | 20 | * 03/16/2001 weiv Collation framework is rewritten in C and made UCA compliant |
michael@0 | 21 | * 12/08/2004 grhoten Split part of ucol.cpp into ucol_res.cpp |
michael@0 | 22 | */ |
michael@0 | 23 | |
michael@0 | 24 | #include "unicode/utypes.h" |
michael@0 | 25 | |
michael@0 | 26 | #if !UCONFIG_NO_COLLATION |
michael@0 | 27 | #include "unicode/uloc.h" |
michael@0 | 28 | #include "unicode/coll.h" |
michael@0 | 29 | #include "unicode/tblcoll.h" |
michael@0 | 30 | #include "unicode/caniter.h" |
michael@0 | 31 | #include "unicode/uscript.h" |
michael@0 | 32 | #include "unicode/ustring.h" |
michael@0 | 33 | |
michael@0 | 34 | #include "ucol_bld.h" |
michael@0 | 35 | #include "ucol_imp.h" |
michael@0 | 36 | #include "ucol_tok.h" |
michael@0 | 37 | #include "ucol_elm.h" |
michael@0 | 38 | #include "uresimp.h" |
michael@0 | 39 | #include "ustr_imp.h" |
michael@0 | 40 | #include "cstring.h" |
michael@0 | 41 | #include "umutex.h" |
michael@0 | 42 | #include "ucln_in.h" |
michael@0 | 43 | #include "ustrenum.h" |
michael@0 | 44 | #include "putilimp.h" |
michael@0 | 45 | #include "utracimp.h" |
michael@0 | 46 | #include "cmemory.h" |
michael@0 | 47 | #include "uassert.h" |
michael@0 | 48 | #include "uenumimp.h" |
michael@0 | 49 | #include "ulist.h" |
michael@0 | 50 | |
michael@0 | 51 | U_NAMESPACE_USE |
michael@0 | 52 | |
michael@0 | 53 | static void ucol_setReorderCodesFromParser(UCollator *coll, UColTokenParser *parser, UErrorCode *status); |
michael@0 | 54 | |
michael@0 | 55 | // static UCA. There is only one. Collators don't use it. |
michael@0 | 56 | // It is referenced only in ucol_initUCA and ucol_cleanup |
michael@0 | 57 | static UCollator* _staticUCA = NULL; |
michael@0 | 58 | static icu::UInitOnce gStaticUCAInitOnce = U_INITONCE_INITIALIZER; |
michael@0 | 59 | // static pointer to udata memory. Inited in ucol_initUCA |
michael@0 | 60 | // used for cleanup in ucol_cleanup |
michael@0 | 61 | static UDataMemory* UCA_DATA_MEM = NULL; |
michael@0 | 62 | |
michael@0 | 63 | U_CDECL_BEGIN |
michael@0 | 64 | static UBool U_CALLCONV |
michael@0 | 65 | ucol_res_cleanup(void) |
michael@0 | 66 | { |
michael@0 | 67 | if (UCA_DATA_MEM) { |
michael@0 | 68 | udata_close(UCA_DATA_MEM); |
michael@0 | 69 | UCA_DATA_MEM = NULL; |
michael@0 | 70 | } |
michael@0 | 71 | if (_staticUCA) { |
michael@0 | 72 | ucol_close(_staticUCA); |
michael@0 | 73 | _staticUCA = NULL; |
michael@0 | 74 | } |
michael@0 | 75 | gStaticUCAInitOnce.reset(); |
michael@0 | 76 | return TRUE; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | static UBool U_CALLCONV |
michael@0 | 80 | isAcceptableUCA(void * /*context*/, |
michael@0 | 81 | const char * /*type*/, const char * /*name*/, |
michael@0 | 82 | const UDataInfo *pInfo){ |
michael@0 | 83 | /* context, type & name are intentionally not used */ |
michael@0 | 84 | if( pInfo->size>=20 && |
michael@0 | 85 | pInfo->isBigEndian==U_IS_BIG_ENDIAN && |
michael@0 | 86 | pInfo->charsetFamily==U_CHARSET_FAMILY && |
michael@0 | 87 | pInfo->dataFormat[0]==UCA_DATA_FORMAT_0 && /* dataFormat="UCol" */ |
michael@0 | 88 | pInfo->dataFormat[1]==UCA_DATA_FORMAT_1 && |
michael@0 | 89 | pInfo->dataFormat[2]==UCA_DATA_FORMAT_2 && |
michael@0 | 90 | pInfo->dataFormat[3]==UCA_DATA_FORMAT_3 && |
michael@0 | 91 | pInfo->formatVersion[0]==UCA_FORMAT_VERSION_0 |
michael@0 | 92 | #if UCA_FORMAT_VERSION_1!=0 |
michael@0 | 93 | && pInfo->formatVersion[1]>=UCA_FORMAT_VERSION_1 |
michael@0 | 94 | #endif |
michael@0 | 95 | //pInfo->formatVersion[1]==UCA_FORMAT_VERSION_1 && |
michael@0 | 96 | //pInfo->formatVersion[2]==UCA_FORMAT_VERSION_2 && // Too harsh |
michael@0 | 97 | //pInfo->formatVersion[3]==UCA_FORMAT_VERSION_3 && // Too harsh |
michael@0 | 98 | ) { |
michael@0 | 99 | return TRUE; |
michael@0 | 100 | // Note: In ICU 51 and earlier, |
michael@0 | 101 | // we used to check that the UCA data version (pInfo->dataVersion) |
michael@0 | 102 | // matches the UCD version (u_getUnicodeVersion()) |
michael@0 | 103 | // but that complicated version updates, and |
michael@0 | 104 | // a mismatch is "only" a problem for handling canonical equivalence. |
michael@0 | 105 | // It need not be a fatal error. |
michael@0 | 106 | } else { |
michael@0 | 107 | return FALSE; |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | U_CDECL_END |
michael@0 | 111 | |
michael@0 | 112 | static void U_CALLCONV ucol_initStaticUCA(UErrorCode &status) { |
michael@0 | 113 | U_ASSERT(_staticUCA == NULL); |
michael@0 | 114 | U_ASSERT(UCA_DATA_MEM == NULL); |
michael@0 | 115 | ucln_i18n_registerCleanup(UCLN_I18N_UCOL_RES, ucol_res_cleanup); |
michael@0 | 116 | |
michael@0 | 117 | UDataMemory *result = udata_openChoice(U_ICUDATA_COLL, UCA_DATA_TYPE, UCA_DATA_NAME, isAcceptableUCA, NULL, &status); |
michael@0 | 118 | if(U_FAILURE(status)){ |
michael@0 | 119 | udata_close(result); |
michael@0 | 120 | return; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | _staticUCA = ucol_initCollator((const UCATableHeader *)udata_getMemory(result), NULL, NULL, &status); |
michael@0 | 124 | if(U_SUCCESS(status)){ |
michael@0 | 125 | // Initalize variables for implicit generation |
michael@0 | 126 | uprv_uca_initImplicitConstants(&status); |
michael@0 | 127 | UCA_DATA_MEM = result; |
michael@0 | 128 | |
michael@0 | 129 | }else{ |
michael@0 | 130 | ucol_close(_staticUCA); |
michael@0 | 131 | _staticUCA = NULL; |
michael@0 | 132 | udata_close(result); |
michael@0 | 133 | } |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | |
michael@0 | 137 | /* do not close UCA returned by ucol_initUCA! */ |
michael@0 | 138 | UCollator * |
michael@0 | 139 | ucol_initUCA(UErrorCode *status) { |
michael@0 | 140 | umtx_initOnce(gStaticUCAInitOnce, &ucol_initStaticUCA, *status); |
michael@0 | 141 | return _staticUCA; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | U_CAPI void U_EXPORT2 |
michael@0 | 145 | ucol_forgetUCA(void) |
michael@0 | 146 | { |
michael@0 | 147 | _staticUCA = NULL; |
michael@0 | 148 | UCA_DATA_MEM = NULL; |
michael@0 | 149 | gStaticUCAInitOnce.reset(); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | /****************************************************************************/ |
michael@0 | 153 | /* Following are the open/close functions */ |
michael@0 | 154 | /* */ |
michael@0 | 155 | /****************************************************************************/ |
michael@0 | 156 | static UCollator* |
michael@0 | 157 | tryOpeningFromRules(UResourceBundle *collElem, UErrorCode *status) { |
michael@0 | 158 | int32_t rulesLen = 0; |
michael@0 | 159 | const UChar *rules = ures_getStringByKey(collElem, "Sequence", &rulesLen, status); |
michael@0 | 160 | return ucol_openRules(rules, rulesLen, UCOL_DEFAULT, UCOL_DEFAULT, NULL, status); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | |
michael@0 | 164 | // API in ucol_imp.h |
michael@0 | 165 | |
michael@0 | 166 | U_CFUNC UCollator* |
michael@0 | 167 | ucol_open_internal(const char *loc, |
michael@0 | 168 | UErrorCode *status) |
michael@0 | 169 | { |
michael@0 | 170 | UErrorCode intStatus = U_ZERO_ERROR; |
michael@0 | 171 | const UCollator* UCA = ucol_initUCA(status); |
michael@0 | 172 | |
michael@0 | 173 | /* New version */ |
michael@0 | 174 | if(U_FAILURE(*status)) return 0; |
michael@0 | 175 | |
michael@0 | 176 | |
michael@0 | 177 | |
michael@0 | 178 | UCollator *result = NULL; |
michael@0 | 179 | UResourceBundle *b = ures_open(U_ICUDATA_COLL, loc, status); |
michael@0 | 180 | |
michael@0 | 181 | /* we try to find stuff from keyword */ |
michael@0 | 182 | UResourceBundle *collations = ures_getByKey(b, "collations", NULL, status); |
michael@0 | 183 | UResourceBundle *collElem = NULL; |
michael@0 | 184 | char keyBuffer[256]; |
michael@0 | 185 | // if there is a keyword, we pick it up and try to get elements |
michael@0 | 186 | if(!uloc_getKeywordValue(loc, "collation", keyBuffer, 256, status) || |
michael@0 | 187 | !uprv_strcmp(keyBuffer,"default")) { /* Treat 'zz@collation=default' as 'zz'. */ |
michael@0 | 188 | // no keyword. we try to find the default setting, which will give us the keyword value |
michael@0 | 189 | intStatus = U_ZERO_ERROR; |
michael@0 | 190 | // finding default value does not affect collation fallback status |
michael@0 | 191 | UResourceBundle *defaultColl = ures_getByKeyWithFallback(collations, "default", NULL, &intStatus); |
michael@0 | 192 | if(U_SUCCESS(intStatus)) { |
michael@0 | 193 | int32_t defaultKeyLen = 0; |
michael@0 | 194 | const UChar *defaultKey = ures_getString(defaultColl, &defaultKeyLen, &intStatus); |
michael@0 | 195 | u_UCharsToChars(defaultKey, keyBuffer, defaultKeyLen); |
michael@0 | 196 | keyBuffer[defaultKeyLen] = 0; |
michael@0 | 197 | } else { |
michael@0 | 198 | *status = U_INTERNAL_PROGRAM_ERROR; |
michael@0 | 199 | return NULL; |
michael@0 | 200 | } |
michael@0 | 201 | ures_close(defaultColl); |
michael@0 | 202 | } |
michael@0 | 203 | collElem = ures_getByKeyWithFallback(collations, keyBuffer, collations, status); |
michael@0 | 204 | collations = NULL; // We just reused the collations object as collElem. |
michael@0 | 205 | |
michael@0 | 206 | UResourceBundle *binary = NULL; |
michael@0 | 207 | UResourceBundle *reorderRes = NULL; |
michael@0 | 208 | |
michael@0 | 209 | if(*status == U_MISSING_RESOURCE_ERROR) { /* We didn't find the tailoring data, we fallback to the UCA */ |
michael@0 | 210 | *status = U_USING_DEFAULT_WARNING; |
michael@0 | 211 | result = ucol_initCollator(UCA->image, result, UCA, status); |
michael@0 | 212 | if (U_FAILURE(*status)) { |
michael@0 | 213 | goto clean; |
michael@0 | 214 | } |
michael@0 | 215 | // if we use UCA, real locale is root |
michael@0 | 216 | ures_close(b); |
michael@0 | 217 | b = ures_open(U_ICUDATA_COLL, "", status); |
michael@0 | 218 | ures_close(collElem); |
michael@0 | 219 | collElem = ures_open(U_ICUDATA_COLL, "", status); |
michael@0 | 220 | if(U_FAILURE(*status)) { |
michael@0 | 221 | goto clean; |
michael@0 | 222 | } |
michael@0 | 223 | result->hasRealData = FALSE; |
michael@0 | 224 | } else if(U_SUCCESS(*status)) { |
michael@0 | 225 | intStatus = U_ZERO_ERROR; |
michael@0 | 226 | |
michael@0 | 227 | binary = ures_getByKey(collElem, "%%CollationBin", NULL, &intStatus); |
michael@0 | 228 | |
michael@0 | 229 | if(intStatus == U_MISSING_RESOURCE_ERROR) { /* we didn't find the binary image, we should use the rules */ |
michael@0 | 230 | binary = NULL; |
michael@0 | 231 | result = tryOpeningFromRules(collElem, status); |
michael@0 | 232 | if(U_FAILURE(*status)) { |
michael@0 | 233 | goto clean; |
michael@0 | 234 | } |
michael@0 | 235 | } else if(U_SUCCESS(intStatus)) { /* otherwise, we'll pick a collation data that exists */ |
michael@0 | 236 | int32_t len = 0; |
michael@0 | 237 | const uint8_t *inData = ures_getBinary(binary, &len, status); |
michael@0 | 238 | if(U_FAILURE(*status)) { |
michael@0 | 239 | goto clean; |
michael@0 | 240 | } |
michael@0 | 241 | UCATableHeader *colData = (UCATableHeader *)inData; |
michael@0 | 242 | if(uprv_memcmp(colData->UCAVersion, UCA->image->UCAVersion, sizeof(UVersionInfo)) != 0 || |
michael@0 | 243 | uprv_memcmp(colData->UCDVersion, UCA->image->UCDVersion, sizeof(UVersionInfo)) != 0 || |
michael@0 | 244 | colData->version[0] != UCOL_BUILDER_VERSION) |
michael@0 | 245 | { |
michael@0 | 246 | *status = U_DIFFERENT_UCA_VERSION; |
michael@0 | 247 | result = tryOpeningFromRules(collElem, status); |
michael@0 | 248 | } else { |
michael@0 | 249 | if(U_FAILURE(*status)){ |
michael@0 | 250 | goto clean; |
michael@0 | 251 | } |
michael@0 | 252 | if((uint32_t)len > (paddedsize(sizeof(UCATableHeader)) + paddedsize(sizeof(UColOptionSet)))) { |
michael@0 | 253 | result = ucol_initCollator((const UCATableHeader *)inData, result, UCA, status); |
michael@0 | 254 | if(U_FAILURE(*status)){ |
michael@0 | 255 | goto clean; |
michael@0 | 256 | } |
michael@0 | 257 | result->hasRealData = TRUE; |
michael@0 | 258 | } else { |
michael@0 | 259 | result = ucol_initCollator(UCA->image, result, UCA, status); |
michael@0 | 260 | ucol_setOptionsFromHeader(result, (UColOptionSet *)(inData+((const UCATableHeader *)inData)->options), status); |
michael@0 | 261 | if(U_FAILURE(*status)){ |
michael@0 | 262 | goto clean; |
michael@0 | 263 | } |
michael@0 | 264 | result->hasRealData = FALSE; |
michael@0 | 265 | } |
michael@0 | 266 | result->freeImageOnClose = FALSE; |
michael@0 | 267 | |
michael@0 | 268 | reorderRes = ures_getByKey(collElem, "%%ReorderCodes", NULL, &intStatus); |
michael@0 | 269 | if (U_SUCCESS(intStatus)) { |
michael@0 | 270 | int32_t reorderCodesLen = 0; |
michael@0 | 271 | const int32_t* reorderCodes = ures_getIntVector(reorderRes, &reorderCodesLen, status); |
michael@0 | 272 | if (reorderCodesLen > 0) { |
michael@0 | 273 | ucol_setReorderCodes(result, reorderCodes, reorderCodesLen, status); |
michael@0 | 274 | // copy the reorder codes into the default reorder codes |
michael@0 | 275 | result->defaultReorderCodesLength = result->reorderCodesLength; |
michael@0 | 276 | result->defaultReorderCodes = (int32_t*) uprv_malloc(result->defaultReorderCodesLength * sizeof(int32_t)); |
michael@0 | 277 | uprv_memcpy(result->defaultReorderCodes, result->reorderCodes, result->defaultReorderCodesLength * sizeof(int32_t)); |
michael@0 | 278 | result->freeDefaultReorderCodesOnClose = TRUE; |
michael@0 | 279 | } |
michael@0 | 280 | if (U_FAILURE(*status)) { |
michael@0 | 281 | goto clean; |
michael@0 | 282 | } |
michael@0 | 283 | } |
michael@0 | 284 | } |
michael@0 | 285 | |
michael@0 | 286 | } else { // !U_SUCCESS(binaryStatus) |
michael@0 | 287 | if(U_SUCCESS(*status)) { |
michael@0 | 288 | *status = intStatus; // propagate underlying error |
michael@0 | 289 | } |
michael@0 | 290 | goto clean; |
michael@0 | 291 | } |
michael@0 | 292 | intStatus = U_ZERO_ERROR; |
michael@0 | 293 | result->rules = ures_getStringByKey(collElem, "Sequence", &result->rulesLength, &intStatus); |
michael@0 | 294 | result->freeRulesOnClose = FALSE; |
michael@0 | 295 | } else { /* There is another error, and we're just gonna clean up */ |
michael@0 | 296 | goto clean; |
michael@0 | 297 | } |
michael@0 | 298 | |
michael@0 | 299 | intStatus = U_ZERO_ERROR; |
michael@0 | 300 | result->ucaRules = ures_getStringByKey(b,"UCARules",NULL,&intStatus); |
michael@0 | 301 | |
michael@0 | 302 | if(loc == NULL) { |
michael@0 | 303 | loc = ures_getLocaleByType(b, ULOC_ACTUAL_LOCALE, status); |
michael@0 | 304 | } |
michael@0 | 305 | result->requestedLocale = uprv_strdup(loc); |
michael@0 | 306 | /* test for NULL */ |
michael@0 | 307 | if (result->requestedLocale == NULL) { |
michael@0 | 308 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 309 | goto clean; |
michael@0 | 310 | } |
michael@0 | 311 | loc = ures_getLocaleByType(collElem, ULOC_ACTUAL_LOCALE, status); |
michael@0 | 312 | result->actualLocale = uprv_strdup(loc); |
michael@0 | 313 | /* test for NULL */ |
michael@0 | 314 | if (result->actualLocale == NULL) { |
michael@0 | 315 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 316 | goto clean; |
michael@0 | 317 | } |
michael@0 | 318 | loc = ures_getLocaleByType(b, ULOC_ACTUAL_LOCALE, status); |
michael@0 | 319 | result->validLocale = uprv_strdup(loc); |
michael@0 | 320 | /* test for NULL */ |
michael@0 | 321 | if (result->validLocale == NULL) { |
michael@0 | 322 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 323 | goto clean; |
michael@0 | 324 | } |
michael@0 | 325 | |
michael@0 | 326 | ures_close(b); |
michael@0 | 327 | ures_close(collElem); |
michael@0 | 328 | ures_close(binary); |
michael@0 | 329 | ures_close(reorderRes); |
michael@0 | 330 | return result; |
michael@0 | 331 | |
michael@0 | 332 | clean: |
michael@0 | 333 | ures_close(b); |
michael@0 | 334 | ures_close(collElem); |
michael@0 | 335 | ures_close(binary); |
michael@0 | 336 | ures_close(reorderRes); |
michael@0 | 337 | ucol_close(result); |
michael@0 | 338 | return NULL; |
michael@0 | 339 | } |
michael@0 | 340 | |
michael@0 | 341 | U_CAPI UCollator* |
michael@0 | 342 | ucol_open(const char *loc, |
michael@0 | 343 | UErrorCode *status) |
michael@0 | 344 | { |
michael@0 | 345 | U_NAMESPACE_USE |
michael@0 | 346 | |
michael@0 | 347 | UTRACE_ENTRY_OC(UTRACE_UCOL_OPEN); |
michael@0 | 348 | UTRACE_DATA1(UTRACE_INFO, "locale = \"%s\"", loc); |
michael@0 | 349 | UCollator *result = NULL; |
michael@0 | 350 | |
michael@0 | 351 | #if !UCONFIG_NO_SERVICE |
michael@0 | 352 | result = Collator::createUCollator(loc, status); |
michael@0 | 353 | if (result == NULL) |
michael@0 | 354 | #endif |
michael@0 | 355 | { |
michael@0 | 356 | result = ucol_open_internal(loc, status); |
michael@0 | 357 | } |
michael@0 | 358 | UTRACE_EXIT_PTR_STATUS(result, *status); |
michael@0 | 359 | return result; |
michael@0 | 360 | } |
michael@0 | 361 | |
michael@0 | 362 | |
michael@0 | 363 | UCollator* |
michael@0 | 364 | ucol_openRulesForImport( const UChar *rules, |
michael@0 | 365 | int32_t rulesLength, |
michael@0 | 366 | UColAttributeValue normalizationMode, |
michael@0 | 367 | UCollationStrength strength, |
michael@0 | 368 | UParseError *parseError, |
michael@0 | 369 | GetCollationRulesFunction importFunc, |
michael@0 | 370 | void* context, |
michael@0 | 371 | UErrorCode *status) |
michael@0 | 372 | { |
michael@0 | 373 | UColTokenParser src; |
michael@0 | 374 | UColAttributeValue norm; |
michael@0 | 375 | UParseError tErr; |
michael@0 | 376 | |
michael@0 | 377 | if(status == NULL || U_FAILURE(*status)){ |
michael@0 | 378 | return 0; |
michael@0 | 379 | } |
michael@0 | 380 | |
michael@0 | 381 | if(rules == NULL || rulesLength < -1) { |
michael@0 | 382 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 383 | return 0; |
michael@0 | 384 | } |
michael@0 | 385 | |
michael@0 | 386 | if(rulesLength == -1) { |
michael@0 | 387 | rulesLength = u_strlen(rules); |
michael@0 | 388 | } |
michael@0 | 389 | |
michael@0 | 390 | if(parseError == NULL){ |
michael@0 | 391 | parseError = &tErr; |
michael@0 | 392 | } |
michael@0 | 393 | |
michael@0 | 394 | switch(normalizationMode) { |
michael@0 | 395 | case UCOL_OFF: |
michael@0 | 396 | case UCOL_ON: |
michael@0 | 397 | case UCOL_DEFAULT: |
michael@0 | 398 | norm = normalizationMode; |
michael@0 | 399 | break; |
michael@0 | 400 | default: |
michael@0 | 401 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 402 | return 0; |
michael@0 | 403 | } |
michael@0 | 404 | |
michael@0 | 405 | UCollator *result = NULL; |
michael@0 | 406 | UCATableHeader *table = NULL; |
michael@0 | 407 | UCollator *UCA = ucol_initUCA(status); |
michael@0 | 408 | |
michael@0 | 409 | if(U_FAILURE(*status)){ |
michael@0 | 410 | return NULL; |
michael@0 | 411 | } |
michael@0 | 412 | |
michael@0 | 413 | ucol_tok_initTokenList(&src, rules, rulesLength, UCA, importFunc, context, status); |
michael@0 | 414 | ucol_tok_assembleTokenList(&src,parseError, status); |
michael@0 | 415 | |
michael@0 | 416 | if(U_FAILURE(*status)) { |
michael@0 | 417 | /* if status is U_ILLEGAL_ARGUMENT_ERROR, src->current points at the offending option */ |
michael@0 | 418 | /* if status is U_INVALID_FORMAT_ERROR, src->current points after the problematic part of the rules */ |
michael@0 | 419 | /* so something might be done here... or on lower level */ |
michael@0 | 420 | #ifdef UCOL_DEBUG |
michael@0 | 421 | if(*status == U_ILLEGAL_ARGUMENT_ERROR) { |
michael@0 | 422 | fprintf(stderr, "bad option starting at offset %i\n", (int)(src.current-src.source)); |
michael@0 | 423 | } else { |
michael@0 | 424 | fprintf(stderr, "invalid rule just before offset %i\n", (int)(src.current-src.source)); |
michael@0 | 425 | } |
michael@0 | 426 | #endif |
michael@0 | 427 | goto cleanup; |
michael@0 | 428 | } |
michael@0 | 429 | |
michael@0 | 430 | /* if we have a set of rules, let's make something of it */ |
michael@0 | 431 | if(src.resultLen > 0 || src.removeSet != NULL) { |
michael@0 | 432 | /* also, if we wanted to remove some contractions, we should make a tailoring */ |
michael@0 | 433 | table = ucol_assembleTailoringTable(&src, status); |
michael@0 | 434 | if(U_SUCCESS(*status)) { |
michael@0 | 435 | // builder version |
michael@0 | 436 | table->version[0] = UCOL_BUILDER_VERSION; |
michael@0 | 437 | // no tailoring information on this level |
michael@0 | 438 | table->version[1] = table->version[2] = table->version[3] = 0; |
michael@0 | 439 | // set UCD version |
michael@0 | 440 | u_getUnicodeVersion(table->UCDVersion); |
michael@0 | 441 | // set UCA version |
michael@0 | 442 | uprv_memcpy(table->UCAVersion, UCA->image->UCAVersion, sizeof(UVersionInfo)); |
michael@0 | 443 | result = ucol_initCollator(table, 0, UCA, status); |
michael@0 | 444 | if (U_FAILURE(*status)) { |
michael@0 | 445 | goto cleanup; |
michael@0 | 446 | } |
michael@0 | 447 | result->hasRealData = TRUE; |
michael@0 | 448 | result->freeImageOnClose = TRUE; |
michael@0 | 449 | } else { |
michael@0 | 450 | goto cleanup; |
michael@0 | 451 | } |
michael@0 | 452 | } else { /* no rules, but no error either */ |
michael@0 | 453 | // must be only options |
michael@0 | 454 | // We will init the collator from UCA |
michael@0 | 455 | result = ucol_initCollator(UCA->image, 0, UCA, status); |
michael@0 | 456 | // Check for null result |
michael@0 | 457 | if (U_FAILURE(*status)) { |
michael@0 | 458 | goto cleanup; |
michael@0 | 459 | } |
michael@0 | 460 | // And set only the options |
michael@0 | 461 | UColOptionSet *opts = (UColOptionSet *)uprv_malloc(sizeof(UColOptionSet)); |
michael@0 | 462 | /* test for NULL */ |
michael@0 | 463 | if (opts == NULL) { |
michael@0 | 464 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 465 | goto cleanup; |
michael@0 | 466 | } |
michael@0 | 467 | uprv_memcpy(opts, src.opts, sizeof(UColOptionSet)); |
michael@0 | 468 | ucol_setOptionsFromHeader(result, opts, status); |
michael@0 | 469 | result->freeOptionsOnClose = TRUE; |
michael@0 | 470 | result->hasRealData = FALSE; |
michael@0 | 471 | result->freeImageOnClose = FALSE; |
michael@0 | 472 | } |
michael@0 | 473 | |
michael@0 | 474 | ucol_setReorderCodesFromParser(result, &src, status); |
michael@0 | 475 | |
michael@0 | 476 | if(U_SUCCESS(*status)) { |
michael@0 | 477 | UChar *newRules; |
michael@0 | 478 | result->dataVersion[0] = UCOL_BUILDER_VERSION; |
michael@0 | 479 | if(rulesLength > 0) { |
michael@0 | 480 | newRules = (UChar *)uprv_malloc((rulesLength+1)*U_SIZEOF_UCHAR); |
michael@0 | 481 | /* test for NULL */ |
michael@0 | 482 | if (newRules == NULL) { |
michael@0 | 483 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 484 | goto cleanup; |
michael@0 | 485 | } |
michael@0 | 486 | uprv_memcpy(newRules, rules, rulesLength*U_SIZEOF_UCHAR); |
michael@0 | 487 | newRules[rulesLength]=0; |
michael@0 | 488 | result->rules = newRules; |
michael@0 | 489 | result->rulesLength = rulesLength; |
michael@0 | 490 | result->freeRulesOnClose = TRUE; |
michael@0 | 491 | } |
michael@0 | 492 | result->ucaRules = NULL; |
michael@0 | 493 | result->actualLocale = NULL; |
michael@0 | 494 | result->validLocale = NULL; |
michael@0 | 495 | result->requestedLocale = NULL; |
michael@0 | 496 | ucol_buildPermutationTable(result, status); |
michael@0 | 497 | ucol_setAttribute(result, UCOL_STRENGTH, strength, status); |
michael@0 | 498 | ucol_setAttribute(result, UCOL_NORMALIZATION_MODE, norm, status); |
michael@0 | 499 | } else { |
michael@0 | 500 | cleanup: |
michael@0 | 501 | if(result != NULL) { |
michael@0 | 502 | ucol_close(result); |
michael@0 | 503 | } else { |
michael@0 | 504 | if(table != NULL) { |
michael@0 | 505 | uprv_free(table); |
michael@0 | 506 | } |
michael@0 | 507 | } |
michael@0 | 508 | result = NULL; |
michael@0 | 509 | } |
michael@0 | 510 | |
michael@0 | 511 | ucol_tok_closeTokenList(&src); |
michael@0 | 512 | |
michael@0 | 513 | return result; |
michael@0 | 514 | } |
michael@0 | 515 | |
michael@0 | 516 | U_CAPI UCollator* U_EXPORT2 |
michael@0 | 517 | ucol_openRules( const UChar *rules, |
michael@0 | 518 | int32_t rulesLength, |
michael@0 | 519 | UColAttributeValue normalizationMode, |
michael@0 | 520 | UCollationStrength strength, |
michael@0 | 521 | UParseError *parseError, |
michael@0 | 522 | UErrorCode *status) |
michael@0 | 523 | { |
michael@0 | 524 | return ucol_openRulesForImport(rules, |
michael@0 | 525 | rulesLength, |
michael@0 | 526 | normalizationMode, |
michael@0 | 527 | strength, |
michael@0 | 528 | parseError, |
michael@0 | 529 | ucol_tok_getRulesFromBundle, |
michael@0 | 530 | NULL, |
michael@0 | 531 | status); |
michael@0 | 532 | } |
michael@0 | 533 | |
michael@0 | 534 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 535 | ucol_getRulesEx(const UCollator *coll, UColRuleOption delta, UChar *buffer, int32_t bufferLen) { |
michael@0 | 536 | UErrorCode status = U_ZERO_ERROR; |
michael@0 | 537 | int32_t len = 0; |
michael@0 | 538 | int32_t UCAlen = 0; |
michael@0 | 539 | const UChar* ucaRules = 0; |
michael@0 | 540 | const UChar *rules = ucol_getRules(coll, &len); |
michael@0 | 541 | if(delta == UCOL_FULL_RULES) { |
michael@0 | 542 | /* take the UCA rules and append real rules at the end */ |
michael@0 | 543 | /* UCA rules will be probably coming from the root RB */ |
michael@0 | 544 | ucaRules = coll->ucaRules; |
michael@0 | 545 | if (ucaRules) { |
michael@0 | 546 | UCAlen = u_strlen(ucaRules); |
michael@0 | 547 | } |
michael@0 | 548 | /* |
michael@0 | 549 | ucaRules = ures_getStringByKey(coll->rb,"UCARules",&UCAlen,&status); |
michael@0 | 550 | UResourceBundle* cresb = ures_getByKeyWithFallback(coll->rb, "collations", NULL, &status); |
michael@0 | 551 | UResourceBundle* uca = ures_getByKeyWithFallback(cresb, "UCA", NULL, &status); |
michael@0 | 552 | ucaRules = ures_getStringByKey(uca,"Sequence",&UCAlen,&status); |
michael@0 | 553 | ures_close(uca); |
michael@0 | 554 | ures_close(cresb); |
michael@0 | 555 | */ |
michael@0 | 556 | } |
michael@0 | 557 | if(U_FAILURE(status)) { |
michael@0 | 558 | return 0; |
michael@0 | 559 | } |
michael@0 | 560 | if(buffer!=0 && bufferLen>0){ |
michael@0 | 561 | *buffer=0; |
michael@0 | 562 | if(UCAlen > 0) { |
michael@0 | 563 | u_memcpy(buffer, ucaRules, uprv_min(UCAlen, bufferLen)); |
michael@0 | 564 | } |
michael@0 | 565 | if(len > 0 && bufferLen > UCAlen) { |
michael@0 | 566 | u_memcpy(buffer+UCAlen, rules, uprv_min(len, bufferLen-UCAlen)); |
michael@0 | 567 | } |
michael@0 | 568 | } |
michael@0 | 569 | return u_terminateUChars(buffer, bufferLen, len+UCAlen, &status); |
michael@0 | 570 | } |
michael@0 | 571 | |
michael@0 | 572 | static const UChar _NUL = 0; |
michael@0 | 573 | |
michael@0 | 574 | U_CAPI const UChar* U_EXPORT2 |
michael@0 | 575 | ucol_getRules( const UCollator *coll, |
michael@0 | 576 | int32_t *length) |
michael@0 | 577 | { |
michael@0 | 578 | if(coll->rules != NULL) { |
michael@0 | 579 | *length = coll->rulesLength; |
michael@0 | 580 | return coll->rules; |
michael@0 | 581 | } |
michael@0 | 582 | else { |
michael@0 | 583 | *length = 0; |
michael@0 | 584 | return &_NUL; |
michael@0 | 585 | } |
michael@0 | 586 | } |
michael@0 | 587 | |
michael@0 | 588 | U_CAPI UBool U_EXPORT2 |
michael@0 | 589 | ucol_equals(const UCollator *source, const UCollator *target) { |
michael@0 | 590 | UErrorCode status = U_ZERO_ERROR; |
michael@0 | 591 | // if pointers are equal, collators are equal |
michael@0 | 592 | if(source == target) { |
michael@0 | 593 | return TRUE; |
michael@0 | 594 | } |
michael@0 | 595 | int32_t i = 0, j = 0; |
michael@0 | 596 | // if any of attributes are different, collators are not equal |
michael@0 | 597 | for(i = 0; i < UCOL_ATTRIBUTE_COUNT; i++) { |
michael@0 | 598 | if(ucol_getAttribute(source, (UColAttribute)i, &status) != ucol_getAttribute(target, (UColAttribute)i, &status) || U_FAILURE(status)) { |
michael@0 | 599 | return FALSE; |
michael@0 | 600 | } |
michael@0 | 601 | } |
michael@0 | 602 | if (source->reorderCodesLength != target->reorderCodesLength){ |
michael@0 | 603 | return FALSE; |
michael@0 | 604 | } |
michael@0 | 605 | for (i = 0; i < source->reorderCodesLength; i++) { |
michael@0 | 606 | if(source->reorderCodes[i] != target->reorderCodes[i]) { |
michael@0 | 607 | return FALSE; |
michael@0 | 608 | } |
michael@0 | 609 | } |
michael@0 | 610 | |
michael@0 | 611 | int32_t sourceRulesLen = 0, targetRulesLen = 0; |
michael@0 | 612 | const UChar *sourceRules = ucol_getRules(source, &sourceRulesLen); |
michael@0 | 613 | const UChar *targetRules = ucol_getRules(target, &targetRulesLen); |
michael@0 | 614 | |
michael@0 | 615 | if(sourceRulesLen == targetRulesLen && u_strncmp(sourceRules, targetRules, sourceRulesLen) == 0) { |
michael@0 | 616 | // all the attributes are equal and the rules are equal - collators are equal |
michael@0 | 617 | return(TRUE); |
michael@0 | 618 | } |
michael@0 | 619 | // hard part, need to construct tree from rules and see if they yield the same tailoring |
michael@0 | 620 | UBool result = TRUE; |
michael@0 | 621 | UParseError parseError; |
michael@0 | 622 | UColTokenParser sourceParser, targetParser; |
michael@0 | 623 | int32_t sourceListLen = 0, targetListLen = 0; |
michael@0 | 624 | ucol_tok_initTokenList(&sourceParser, sourceRules, sourceRulesLen, source->UCA, ucol_tok_getRulesFromBundle, NULL, &status); |
michael@0 | 625 | ucol_tok_initTokenList(&targetParser, targetRules, targetRulesLen, target->UCA, ucol_tok_getRulesFromBundle, NULL, &status); |
michael@0 | 626 | sourceListLen = ucol_tok_assembleTokenList(&sourceParser, &parseError, &status); |
michael@0 | 627 | targetListLen = ucol_tok_assembleTokenList(&targetParser, &parseError, &status); |
michael@0 | 628 | |
michael@0 | 629 | if(sourceListLen != targetListLen) { |
michael@0 | 630 | // different number of resets |
michael@0 | 631 | result = FALSE; |
michael@0 | 632 | } else { |
michael@0 | 633 | UColToken *sourceReset = NULL, *targetReset = NULL; |
michael@0 | 634 | UChar *sourceResetString = NULL, *targetResetString = NULL; |
michael@0 | 635 | int32_t sourceStringLen = 0, targetStringLen = 0; |
michael@0 | 636 | for(i = 0; i < sourceListLen; i++) { |
michael@0 | 637 | sourceReset = sourceParser.lh[i].reset; |
michael@0 | 638 | sourceResetString = sourceParser.source+(sourceReset->source & 0xFFFFFF); |
michael@0 | 639 | sourceStringLen = sourceReset->source >> 24; |
michael@0 | 640 | for(j = 0; j < sourceListLen; j++) { |
michael@0 | 641 | targetReset = targetParser.lh[j].reset; |
michael@0 | 642 | targetResetString = targetParser.source+(targetReset->source & 0xFFFFFF); |
michael@0 | 643 | targetStringLen = targetReset->source >> 24; |
michael@0 | 644 | if(sourceStringLen == targetStringLen && (u_strncmp(sourceResetString, targetResetString, sourceStringLen) == 0)) { |
michael@0 | 645 | sourceReset = sourceParser.lh[i].first; |
michael@0 | 646 | targetReset = targetParser.lh[j].first; |
michael@0 | 647 | while(sourceReset != NULL && targetReset != NULL) { |
michael@0 | 648 | sourceResetString = sourceParser.source+(sourceReset->source & 0xFFFFFF); |
michael@0 | 649 | sourceStringLen = sourceReset->source >> 24; |
michael@0 | 650 | targetResetString = targetParser.source+(targetReset->source & 0xFFFFFF); |
michael@0 | 651 | targetStringLen = targetReset->source >> 24; |
michael@0 | 652 | if(sourceStringLen != targetStringLen || (u_strncmp(sourceResetString, targetResetString, sourceStringLen) != 0)) { |
michael@0 | 653 | result = FALSE; |
michael@0 | 654 | goto returnResult; |
michael@0 | 655 | } |
michael@0 | 656 | // probably also need to check the expansions |
michael@0 | 657 | if(sourceReset->expansion) { |
michael@0 | 658 | if(!targetReset->expansion) { |
michael@0 | 659 | result = FALSE; |
michael@0 | 660 | goto returnResult; |
michael@0 | 661 | } else { |
michael@0 | 662 | // compare expansions |
michael@0 | 663 | sourceResetString = sourceParser.source+(sourceReset->expansion& 0xFFFFFF); |
michael@0 | 664 | sourceStringLen = sourceReset->expansion >> 24; |
michael@0 | 665 | targetResetString = targetParser.source+(targetReset->expansion & 0xFFFFFF); |
michael@0 | 666 | targetStringLen = targetReset->expansion >> 24; |
michael@0 | 667 | if(sourceStringLen != targetStringLen || (u_strncmp(sourceResetString, targetResetString, sourceStringLen) != 0)) { |
michael@0 | 668 | result = FALSE; |
michael@0 | 669 | goto returnResult; |
michael@0 | 670 | } |
michael@0 | 671 | } |
michael@0 | 672 | } else { |
michael@0 | 673 | if(targetReset->expansion) { |
michael@0 | 674 | result = FALSE; |
michael@0 | 675 | goto returnResult; |
michael@0 | 676 | } |
michael@0 | 677 | } |
michael@0 | 678 | sourceReset = sourceReset->next; |
michael@0 | 679 | targetReset = targetReset->next; |
michael@0 | 680 | } |
michael@0 | 681 | if(sourceReset != targetReset) { // at least one is not NULL |
michael@0 | 682 | // there are more tailored elements in one list |
michael@0 | 683 | result = FALSE; |
michael@0 | 684 | goto returnResult; |
michael@0 | 685 | } |
michael@0 | 686 | |
michael@0 | 687 | |
michael@0 | 688 | break; |
michael@0 | 689 | } |
michael@0 | 690 | } |
michael@0 | 691 | // couldn't find the reset anchor, so the collators are not equal |
michael@0 | 692 | if(j == sourceListLen) { |
michael@0 | 693 | result = FALSE; |
michael@0 | 694 | goto returnResult; |
michael@0 | 695 | } |
michael@0 | 696 | } |
michael@0 | 697 | } |
michael@0 | 698 | |
michael@0 | 699 | returnResult: |
michael@0 | 700 | ucol_tok_closeTokenList(&sourceParser); |
michael@0 | 701 | ucol_tok_closeTokenList(&targetParser); |
michael@0 | 702 | return result; |
michael@0 | 703 | |
michael@0 | 704 | } |
michael@0 | 705 | |
michael@0 | 706 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 707 | ucol_getDisplayName( const char *objLoc, |
michael@0 | 708 | const char *dispLoc, |
michael@0 | 709 | UChar *result, |
michael@0 | 710 | int32_t resultLength, |
michael@0 | 711 | UErrorCode *status) |
michael@0 | 712 | { |
michael@0 | 713 | U_NAMESPACE_USE |
michael@0 | 714 | |
michael@0 | 715 | if(U_FAILURE(*status)) return -1; |
michael@0 | 716 | UnicodeString dst; |
michael@0 | 717 | if(!(result==NULL && resultLength==0)) { |
michael@0 | 718 | // NULL destination for pure preflighting: empty dummy string |
michael@0 | 719 | // otherwise, alias the destination buffer |
michael@0 | 720 | dst.setTo(result, 0, resultLength); |
michael@0 | 721 | } |
michael@0 | 722 | Collator::getDisplayName(Locale(objLoc), Locale(dispLoc), dst); |
michael@0 | 723 | return dst.extract(result, resultLength, *status); |
michael@0 | 724 | } |
michael@0 | 725 | |
michael@0 | 726 | U_CAPI const char* U_EXPORT2 |
michael@0 | 727 | ucol_getAvailable(int32_t index) |
michael@0 | 728 | { |
michael@0 | 729 | int32_t count = 0; |
michael@0 | 730 | const Locale *loc = Collator::getAvailableLocales(count); |
michael@0 | 731 | if (loc != NULL && index < count) { |
michael@0 | 732 | return loc[index].getName(); |
michael@0 | 733 | } |
michael@0 | 734 | return NULL; |
michael@0 | 735 | } |
michael@0 | 736 | |
michael@0 | 737 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 738 | ucol_countAvailable() |
michael@0 | 739 | { |
michael@0 | 740 | int32_t count = 0; |
michael@0 | 741 | Collator::getAvailableLocales(count); |
michael@0 | 742 | return count; |
michael@0 | 743 | } |
michael@0 | 744 | |
michael@0 | 745 | #if !UCONFIG_NO_SERVICE |
michael@0 | 746 | U_CAPI UEnumeration* U_EXPORT2 |
michael@0 | 747 | ucol_openAvailableLocales(UErrorCode *status) { |
michael@0 | 748 | U_NAMESPACE_USE |
michael@0 | 749 | |
michael@0 | 750 | // This is a wrapper over Collator::getAvailableLocales() |
michael@0 | 751 | if (U_FAILURE(*status)) { |
michael@0 | 752 | return NULL; |
michael@0 | 753 | } |
michael@0 | 754 | StringEnumeration *s = icu::Collator::getAvailableLocales(); |
michael@0 | 755 | if (s == NULL) { |
michael@0 | 756 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 757 | return NULL; |
michael@0 | 758 | } |
michael@0 | 759 | return uenum_openFromStringEnumeration(s, status); |
michael@0 | 760 | } |
michael@0 | 761 | #endif |
michael@0 | 762 | |
michael@0 | 763 | // Note: KEYWORDS[0] != RESOURCE_NAME - alan |
michael@0 | 764 | |
michael@0 | 765 | static const char RESOURCE_NAME[] = "collations"; |
michael@0 | 766 | |
michael@0 | 767 | static const char* const KEYWORDS[] = { "collation" }; |
michael@0 | 768 | |
michael@0 | 769 | #define KEYWORD_COUNT (sizeof(KEYWORDS)/sizeof(KEYWORDS[0])) |
michael@0 | 770 | |
michael@0 | 771 | U_CAPI UEnumeration* U_EXPORT2 |
michael@0 | 772 | ucol_getKeywords(UErrorCode *status) { |
michael@0 | 773 | UEnumeration *result = NULL; |
michael@0 | 774 | if (U_SUCCESS(*status)) { |
michael@0 | 775 | return uenum_openCharStringsEnumeration(KEYWORDS, KEYWORD_COUNT, status); |
michael@0 | 776 | } |
michael@0 | 777 | return result; |
michael@0 | 778 | } |
michael@0 | 779 | |
michael@0 | 780 | U_CAPI UEnumeration* U_EXPORT2 |
michael@0 | 781 | ucol_getKeywordValues(const char *keyword, UErrorCode *status) { |
michael@0 | 782 | if (U_FAILURE(*status)) { |
michael@0 | 783 | return NULL; |
michael@0 | 784 | } |
michael@0 | 785 | // hard-coded to accept exactly one collation keyword |
michael@0 | 786 | // modify if additional collation keyword is added later |
michael@0 | 787 | if (keyword==NULL || uprv_strcmp(keyword, KEYWORDS[0])!=0) |
michael@0 | 788 | { |
michael@0 | 789 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 790 | return NULL; |
michael@0 | 791 | } |
michael@0 | 792 | return ures_getKeywordValues(U_ICUDATA_COLL, RESOURCE_NAME, status); |
michael@0 | 793 | } |
michael@0 | 794 | |
michael@0 | 795 | static const UEnumeration defaultKeywordValues = { |
michael@0 | 796 | NULL, |
michael@0 | 797 | NULL, |
michael@0 | 798 | ulist_close_keyword_values_iterator, |
michael@0 | 799 | ulist_count_keyword_values, |
michael@0 | 800 | uenum_unextDefault, |
michael@0 | 801 | ulist_next_keyword_value, |
michael@0 | 802 | ulist_reset_keyword_values_iterator |
michael@0 | 803 | }; |
michael@0 | 804 | |
michael@0 | 805 | #include <stdio.h> |
michael@0 | 806 | |
michael@0 | 807 | U_CAPI UEnumeration* U_EXPORT2 |
michael@0 | 808 | ucol_getKeywordValuesForLocale(const char* /*key*/, const char* locale, |
michael@0 | 809 | UBool /*commonlyUsed*/, UErrorCode* status) { |
michael@0 | 810 | /* Get the locale base name. */ |
michael@0 | 811 | char localeBuffer[ULOC_FULLNAME_CAPACITY] = ""; |
michael@0 | 812 | uloc_getBaseName(locale, localeBuffer, sizeof(localeBuffer), status); |
michael@0 | 813 | |
michael@0 | 814 | /* Create the 2 lists |
michael@0 | 815 | * -values is the temp location for the keyword values |
michael@0 | 816 | * -results hold the actual list used by the UEnumeration object |
michael@0 | 817 | */ |
michael@0 | 818 | UList *values = ulist_createEmptyList(status); |
michael@0 | 819 | UList *results = ulist_createEmptyList(status); |
michael@0 | 820 | UEnumeration *en = (UEnumeration *)uprv_malloc(sizeof(UEnumeration)); |
michael@0 | 821 | if (U_FAILURE(*status) || en == NULL) { |
michael@0 | 822 | if (en == NULL) { |
michael@0 | 823 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 824 | } else { |
michael@0 | 825 | uprv_free(en); |
michael@0 | 826 | } |
michael@0 | 827 | ulist_deleteList(values); |
michael@0 | 828 | ulist_deleteList(results); |
michael@0 | 829 | return NULL; |
michael@0 | 830 | } |
michael@0 | 831 | |
michael@0 | 832 | memcpy(en, &defaultKeywordValues, sizeof(UEnumeration)); |
michael@0 | 833 | en->context = results; |
michael@0 | 834 | |
michael@0 | 835 | /* Open the resource bundle for collation with the given locale. */ |
michael@0 | 836 | UResourceBundle bundle, collations, collres, defres; |
michael@0 | 837 | ures_initStackObject(&bundle); |
michael@0 | 838 | ures_initStackObject(&collations); |
michael@0 | 839 | ures_initStackObject(&collres); |
michael@0 | 840 | ures_initStackObject(&defres); |
michael@0 | 841 | |
michael@0 | 842 | ures_openFillIn(&bundle, U_ICUDATA_COLL, localeBuffer, status); |
michael@0 | 843 | |
michael@0 | 844 | while (U_SUCCESS(*status)) { |
michael@0 | 845 | ures_getByKey(&bundle, RESOURCE_NAME, &collations, status); |
michael@0 | 846 | ures_resetIterator(&collations); |
michael@0 | 847 | while (U_SUCCESS(*status) && ures_hasNext(&collations)) { |
michael@0 | 848 | ures_getNextResource(&collations, &collres, status); |
michael@0 | 849 | const char *key = ures_getKey(&collres); |
michael@0 | 850 | /* If the key is default, get the string and store it in results list only |
michael@0 | 851 | * if results list is empty. |
michael@0 | 852 | */ |
michael@0 | 853 | if (uprv_strcmp(key, "default") == 0) { |
michael@0 | 854 | if (ulist_getListSize(results) == 0) { |
michael@0 | 855 | char *defcoll = (char *)uprv_malloc(sizeof(char) * ULOC_KEYWORDS_CAPACITY); |
michael@0 | 856 | int32_t defcollLength = ULOC_KEYWORDS_CAPACITY; |
michael@0 | 857 | |
michael@0 | 858 | ures_getNextResource(&collres, &defres, status); |
michael@0 | 859 | #if U_CHARSET_FAMILY==U_ASCII_FAMILY |
michael@0 | 860 | /* optimize - use the utf-8 string */ |
michael@0 | 861 | ures_getUTF8String(&defres, defcoll, &defcollLength, TRUE, status); |
michael@0 | 862 | #else |
michael@0 | 863 | { |
michael@0 | 864 | const UChar* defString = ures_getString(&defres, &defcollLength, status); |
michael@0 | 865 | if(U_SUCCESS(*status)) { |
michael@0 | 866 | if(defcollLength+1 > ULOC_KEYWORDS_CAPACITY) { |
michael@0 | 867 | *status = U_BUFFER_OVERFLOW_ERROR; |
michael@0 | 868 | } else { |
michael@0 | 869 | u_UCharsToChars(defString, defcoll, defcollLength+1); |
michael@0 | 870 | } |
michael@0 | 871 | } |
michael@0 | 872 | } |
michael@0 | 873 | #endif |
michael@0 | 874 | |
michael@0 | 875 | ulist_addItemBeginList(results, defcoll, TRUE, status); |
michael@0 | 876 | } |
michael@0 | 877 | } else { |
michael@0 | 878 | ulist_addItemEndList(values, key, FALSE, status); |
michael@0 | 879 | } |
michael@0 | 880 | } |
michael@0 | 881 | |
michael@0 | 882 | /* If the locale is "" this is root so exit. */ |
michael@0 | 883 | if (uprv_strlen(localeBuffer) == 0) { |
michael@0 | 884 | break; |
michael@0 | 885 | } |
michael@0 | 886 | /* Get the parent locale and open a new resource bundle. */ |
michael@0 | 887 | uloc_getParent(localeBuffer, localeBuffer, sizeof(localeBuffer), status); |
michael@0 | 888 | ures_openFillIn(&bundle, U_ICUDATA_COLL, localeBuffer, status); |
michael@0 | 889 | } |
michael@0 | 890 | |
michael@0 | 891 | ures_close(&defres); |
michael@0 | 892 | ures_close(&collres); |
michael@0 | 893 | ures_close(&collations); |
michael@0 | 894 | ures_close(&bundle); |
michael@0 | 895 | |
michael@0 | 896 | if (U_SUCCESS(*status)) { |
michael@0 | 897 | char *value = NULL; |
michael@0 | 898 | ulist_resetList(values); |
michael@0 | 899 | while ((value = (char *)ulist_getNext(values)) != NULL) { |
michael@0 | 900 | if (!ulist_containsString(results, value, (int32_t)uprv_strlen(value))) { |
michael@0 | 901 | ulist_addItemEndList(results, value, FALSE, status); |
michael@0 | 902 | if (U_FAILURE(*status)) { |
michael@0 | 903 | break; |
michael@0 | 904 | } |
michael@0 | 905 | } |
michael@0 | 906 | } |
michael@0 | 907 | } |
michael@0 | 908 | |
michael@0 | 909 | ulist_deleteList(values); |
michael@0 | 910 | |
michael@0 | 911 | if (U_FAILURE(*status)){ |
michael@0 | 912 | uenum_close(en); |
michael@0 | 913 | en = NULL; |
michael@0 | 914 | } else { |
michael@0 | 915 | ulist_resetList(results); |
michael@0 | 916 | } |
michael@0 | 917 | |
michael@0 | 918 | return en; |
michael@0 | 919 | } |
michael@0 | 920 | |
michael@0 | 921 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 922 | ucol_getFunctionalEquivalent(char* result, int32_t resultCapacity, |
michael@0 | 923 | const char* keyword, const char* locale, |
michael@0 | 924 | UBool* isAvailable, UErrorCode* status) |
michael@0 | 925 | { |
michael@0 | 926 | // N.B.: Resource name is "collations" but keyword is "collation" |
michael@0 | 927 | return ures_getFunctionalEquivalent(result, resultCapacity, U_ICUDATA_COLL, |
michael@0 | 928 | "collations", keyword, locale, |
michael@0 | 929 | isAvailable, TRUE, status); |
michael@0 | 930 | } |
michael@0 | 931 | |
michael@0 | 932 | /* returns the locale name the collation data comes from */ |
michael@0 | 933 | U_CAPI const char * U_EXPORT2 |
michael@0 | 934 | ucol_getLocale(const UCollator *coll, ULocDataLocaleType type, UErrorCode *status) { |
michael@0 | 935 | return ucol_getLocaleByType(coll, type, status); |
michael@0 | 936 | } |
michael@0 | 937 | |
michael@0 | 938 | U_CAPI const char * U_EXPORT2 |
michael@0 | 939 | ucol_getLocaleByType(const UCollator *coll, ULocDataLocaleType type, UErrorCode *status) { |
michael@0 | 940 | const char *result = NULL; |
michael@0 | 941 | if(status == NULL || U_FAILURE(*status)) { |
michael@0 | 942 | return NULL; |
michael@0 | 943 | } |
michael@0 | 944 | UTRACE_ENTRY(UTRACE_UCOL_GETLOCALE); |
michael@0 | 945 | UTRACE_DATA1(UTRACE_INFO, "coll=%p", coll); |
michael@0 | 946 | |
michael@0 | 947 | if(coll->delegate!=NULL) { |
michael@0 | 948 | return ((const Collator*)coll->delegate)->getLocale(type, *status).getName(); |
michael@0 | 949 | } |
michael@0 | 950 | switch(type) { |
michael@0 | 951 | case ULOC_ACTUAL_LOCALE: |
michael@0 | 952 | result = coll->actualLocale; |
michael@0 | 953 | break; |
michael@0 | 954 | case ULOC_VALID_LOCALE: |
michael@0 | 955 | result = coll->validLocale; |
michael@0 | 956 | break; |
michael@0 | 957 | case ULOC_REQUESTED_LOCALE: |
michael@0 | 958 | result = coll->requestedLocale; |
michael@0 | 959 | break; |
michael@0 | 960 | default: |
michael@0 | 961 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 962 | } |
michael@0 | 963 | UTRACE_DATA1(UTRACE_INFO, "result = %s", result); |
michael@0 | 964 | UTRACE_EXIT_STATUS(*status); |
michael@0 | 965 | return result; |
michael@0 | 966 | } |
michael@0 | 967 | |
michael@0 | 968 | U_CFUNC void U_EXPORT2 |
michael@0 | 969 | ucol_setReqValidLocales(UCollator *coll, char *requestedLocaleToAdopt, char *validLocaleToAdopt, char *actualLocaleToAdopt) |
michael@0 | 970 | { |
michael@0 | 971 | if (coll) { |
michael@0 | 972 | if (coll->validLocale) { |
michael@0 | 973 | uprv_free(coll->validLocale); |
michael@0 | 974 | } |
michael@0 | 975 | coll->validLocale = validLocaleToAdopt; |
michael@0 | 976 | if (coll->requestedLocale) { // should always have |
michael@0 | 977 | uprv_free(coll->requestedLocale); |
michael@0 | 978 | } |
michael@0 | 979 | coll->requestedLocale = requestedLocaleToAdopt; |
michael@0 | 980 | if (coll->actualLocale) { |
michael@0 | 981 | uprv_free(coll->actualLocale); |
michael@0 | 982 | } |
michael@0 | 983 | coll->actualLocale = actualLocaleToAdopt; |
michael@0 | 984 | } |
michael@0 | 985 | } |
michael@0 | 986 | |
michael@0 | 987 | U_CAPI USet * U_EXPORT2 |
michael@0 | 988 | ucol_getTailoredSet(const UCollator *coll, UErrorCode *status) |
michael@0 | 989 | { |
michael@0 | 990 | U_NAMESPACE_USE |
michael@0 | 991 | |
michael@0 | 992 | if(status == NULL || U_FAILURE(*status)) { |
michael@0 | 993 | return NULL; |
michael@0 | 994 | } |
michael@0 | 995 | if(coll == NULL || coll->UCA == NULL) { |
michael@0 | 996 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 997 | return NULL; |
michael@0 | 998 | } |
michael@0 | 999 | UParseError parseError; |
michael@0 | 1000 | UColTokenParser src; |
michael@0 | 1001 | int32_t rulesLen = 0; |
michael@0 | 1002 | const UChar *rules = ucol_getRules(coll, &rulesLen); |
michael@0 | 1003 | UBool startOfRules = TRUE; |
michael@0 | 1004 | // we internally use the C++ class, for the following reasons: |
michael@0 | 1005 | // 1. we need to utilize canonical iterator, which is a C++ only class |
michael@0 | 1006 | // 2. canonical iterator returns UnicodeStrings - USet cannot take them |
michael@0 | 1007 | // 3. USet is internally really UnicodeSet, C is just a wrapper |
michael@0 | 1008 | UnicodeSet *tailored = new UnicodeSet(); |
michael@0 | 1009 | UnicodeString pattern; |
michael@0 | 1010 | UnicodeString empty; |
michael@0 | 1011 | CanonicalIterator it(empty, *status); |
michael@0 | 1012 | |
michael@0 | 1013 | |
michael@0 | 1014 | // The idea is to tokenize the rule set. For each non-reset token, |
michael@0 | 1015 | // we add all the canonicaly equivalent FCD sequences |
michael@0 | 1016 | ucol_tok_initTokenList(&src, rules, rulesLen, coll->UCA, ucol_tok_getRulesFromBundle, NULL, status); |
michael@0 | 1017 | while (ucol_tok_parseNextToken(&src, startOfRules, &parseError, status) != NULL) { |
michael@0 | 1018 | startOfRules = FALSE; |
michael@0 | 1019 | if(src.parsedToken.strength != UCOL_TOK_RESET) { |
michael@0 | 1020 | const UChar *stuff = src.source+(src.parsedToken.charsOffset); |
michael@0 | 1021 | it.setSource(UnicodeString(stuff, src.parsedToken.charsLen), *status); |
michael@0 | 1022 | pattern = it.next(); |
michael@0 | 1023 | while(!pattern.isBogus()) { |
michael@0 | 1024 | if(Normalizer::quickCheck(pattern, UNORM_FCD, *status) != UNORM_NO) { |
michael@0 | 1025 | tailored->add(pattern); |
michael@0 | 1026 | } |
michael@0 | 1027 | pattern = it.next(); |
michael@0 | 1028 | } |
michael@0 | 1029 | } |
michael@0 | 1030 | } |
michael@0 | 1031 | ucol_tok_closeTokenList(&src); |
michael@0 | 1032 | return (USet *)tailored; |
michael@0 | 1033 | } |
michael@0 | 1034 | |
michael@0 | 1035 | /* |
michael@0 | 1036 | * Collation Reordering |
michael@0 | 1037 | */ |
michael@0 | 1038 | |
michael@0 | 1039 | void ucol_setReorderCodesFromParser(UCollator *coll, UColTokenParser *parser, UErrorCode *status) { |
michael@0 | 1040 | if (U_FAILURE(*status)) { |
michael@0 | 1041 | return; |
michael@0 | 1042 | } |
michael@0 | 1043 | |
michael@0 | 1044 | if (parser->reorderCodesLength == 0 || parser->reorderCodes == NULL) { |
michael@0 | 1045 | return; |
michael@0 | 1046 | } |
michael@0 | 1047 | |
michael@0 | 1048 | coll->reorderCodesLength = 0; |
michael@0 | 1049 | if (coll->reorderCodes != NULL && coll->freeReorderCodesOnClose == TRUE) { |
michael@0 | 1050 | uprv_free(coll->reorderCodes); |
michael@0 | 1051 | } |
michael@0 | 1052 | coll->reorderCodes = NULL; |
michael@0 | 1053 | coll->freeReorderCodesOnClose = FALSE; |
michael@0 | 1054 | |
michael@0 | 1055 | if (coll->defaultReorderCodes != NULL && coll->freeDefaultReorderCodesOnClose == TRUE) { |
michael@0 | 1056 | uprv_free(coll->defaultReorderCodes); |
michael@0 | 1057 | } |
michael@0 | 1058 | coll->freeDefaultReorderCodesOnClose = FALSE; |
michael@0 | 1059 | coll->defaultReorderCodesLength = parser->reorderCodesLength; |
michael@0 | 1060 | coll->defaultReorderCodes = (int32_t*) uprv_malloc(coll->defaultReorderCodesLength * sizeof(int32_t)); |
michael@0 | 1061 | if (coll->defaultReorderCodes == NULL) { |
michael@0 | 1062 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 1063 | return; |
michael@0 | 1064 | } |
michael@0 | 1065 | uprv_memcpy(coll->defaultReorderCodes, parser->reorderCodes, coll->defaultReorderCodesLength * sizeof(int32_t)); |
michael@0 | 1066 | coll->freeDefaultReorderCodesOnClose = TRUE; |
michael@0 | 1067 | |
michael@0 | 1068 | coll->reorderCodesLength = parser->reorderCodesLength; |
michael@0 | 1069 | coll->reorderCodes = (int32_t*) uprv_malloc(coll->reorderCodesLength * sizeof(int32_t)); |
michael@0 | 1070 | if (coll->reorderCodes == NULL) { |
michael@0 | 1071 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 1072 | return; |
michael@0 | 1073 | } |
michael@0 | 1074 | uprv_memcpy(coll->reorderCodes, parser->reorderCodes, coll->reorderCodesLength * sizeof(int32_t)); |
michael@0 | 1075 | coll->freeReorderCodesOnClose = TRUE; |
michael@0 | 1076 | } |
michael@0 | 1077 | |
michael@0 | 1078 | /* |
michael@0 | 1079 | * Data is stored in the reorder code to lead byte table as: |
michael@0 | 1080 | * index count - unsigned short (2 bytes) - number of index entries |
michael@0 | 1081 | * data size - unsigned short (2 bytes) - number of unsigned short data elements |
michael@0 | 1082 | * index[index count] - array of 2 unsigned shorts (4 bytes each entry) |
michael@0 | 1083 | * - reorder code, offset |
michael@0 | 1084 | * - index is sorted by reorder code |
michael@0 | 1085 | * - if an offset has the high bit set then it is not an offset but a single data entry |
michael@0 | 1086 | * once the high bit is stripped off |
michael@0 | 1087 | * data[data size] - array of unsigned short (2 bytes each entry) |
michael@0 | 1088 | * - the data is an usigned short count followed by count number |
michael@0 | 1089 | * of lead bytes stored in an unsigned short |
michael@0 | 1090 | */ |
michael@0 | 1091 | U_CFUNC int U_EXPORT2 |
michael@0 | 1092 | ucol_getLeadBytesForReorderCode(const UCollator *uca, int reorderCode, uint16_t* returnLeadBytes, int returnCapacity) { |
michael@0 | 1093 | uint16_t reorderCodeIndexLength = *((uint16_t*) ((uint8_t *)uca->image + uca->image->scriptToLeadByte)); |
michael@0 | 1094 | uint16_t* reorderCodeIndex = (uint16_t*) ((uint8_t *)uca->image + uca->image->scriptToLeadByte + 2 *sizeof(uint16_t)); |
michael@0 | 1095 | |
michael@0 | 1096 | // reorder code index is 2 uint16_t's - reorder code + offset |
michael@0 | 1097 | for (int i = 0; i < reorderCodeIndexLength; i++) { |
michael@0 | 1098 | if (reorderCode == reorderCodeIndex[i*2]) { |
michael@0 | 1099 | uint16_t dataOffset = reorderCodeIndex[(i*2) + 1]; |
michael@0 | 1100 | if ((dataOffset & 0x8000) == 0x8000) { |
michael@0 | 1101 | // offset isn't offset but instead is a single data element |
michael@0 | 1102 | if (returnCapacity >= 1) { |
michael@0 | 1103 | returnLeadBytes[0] = dataOffset & ~0x8000; |
michael@0 | 1104 | return 1; |
michael@0 | 1105 | } |
michael@0 | 1106 | return 0; |
michael@0 | 1107 | } |
michael@0 | 1108 | uint16_t* dataOffsetBase = (uint16_t*) ((uint8_t *)reorderCodeIndex + reorderCodeIndexLength * (2 * sizeof(uint16_t))); |
michael@0 | 1109 | uint16_t leadByteCount = *(dataOffsetBase + dataOffset); |
michael@0 | 1110 | leadByteCount = leadByteCount > returnCapacity ? returnCapacity : leadByteCount; |
michael@0 | 1111 | uprv_memcpy(returnLeadBytes, dataOffsetBase + dataOffset + 1, leadByteCount * sizeof(uint16_t)); |
michael@0 | 1112 | return leadByteCount; |
michael@0 | 1113 | } |
michael@0 | 1114 | } |
michael@0 | 1115 | return 0; |
michael@0 | 1116 | } |
michael@0 | 1117 | |
michael@0 | 1118 | /* |
michael@0 | 1119 | * Data is stored in the lead byte to reorder code table as: |
michael@0 | 1120 | * index count - unsigned short (2 bytes) - number of index entries |
michael@0 | 1121 | * data size - unsigned short (2 bytes) - number of unsigned short data elements |
michael@0 | 1122 | * index[index count] - array of unsigned short (2 bytes each entry) |
michael@0 | 1123 | * - index is sorted by lead byte |
michael@0 | 1124 | * - if an index has the high bit set then it is not an index but a single data entry |
michael@0 | 1125 | * once the high bit is stripped off |
michael@0 | 1126 | * data[data size] - array of unsigned short (2 bytes each entry) |
michael@0 | 1127 | * - the data is an usigned short count followed by count number of reorder codes |
michael@0 | 1128 | */ |
michael@0 | 1129 | U_CFUNC int U_EXPORT2 |
michael@0 | 1130 | ucol_getReorderCodesForLeadByte(const UCollator *uca, int leadByte, int16_t* returnReorderCodes, int returnCapacity) { |
michael@0 | 1131 | uint16_t* leadByteTable = ((uint16_t*) ((uint8_t *)uca->image + uca->image->leadByteToScript)); |
michael@0 | 1132 | uint16_t leadByteIndexLength = *leadByteTable; |
michael@0 | 1133 | if (leadByte >= leadByteIndexLength) { |
michael@0 | 1134 | return 0; |
michael@0 | 1135 | } |
michael@0 | 1136 | uint16_t leadByteIndex = *(leadByteTable + (2 + leadByte)); |
michael@0 | 1137 | |
michael@0 | 1138 | if ((leadByteIndex & 0x8000) == 0x8000) { |
michael@0 | 1139 | // offset isn't offset but instead is a single data element |
michael@0 | 1140 | if (returnCapacity >= 1) { |
michael@0 | 1141 | returnReorderCodes[0] = leadByteIndex & ~0x8000; |
michael@0 | 1142 | return 1; |
michael@0 | 1143 | } |
michael@0 | 1144 | return 0; |
michael@0 | 1145 | } |
michael@0 | 1146 | //uint16_t* dataOffsetBase = leadByteTable + (2 + leadByteIndexLength); |
michael@0 | 1147 | uint16_t* reorderCodeData = leadByteTable + (2 + leadByteIndexLength) + leadByteIndex; |
michael@0 | 1148 | uint16_t reorderCodeCount = *reorderCodeData > returnCapacity ? returnCapacity : *reorderCodeData; |
michael@0 | 1149 | uprv_memcpy(returnReorderCodes, reorderCodeData + 1, reorderCodeCount * sizeof(uint16_t)); |
michael@0 | 1150 | return reorderCodeCount; |
michael@0 | 1151 | } |
michael@0 | 1152 | |
michael@0 | 1153 | // used to mark ignorable reorder code slots |
michael@0 | 1154 | static const int32_t UCOL_REORDER_CODE_IGNORE = UCOL_REORDER_CODE_LIMIT + 1; |
michael@0 | 1155 | |
michael@0 | 1156 | U_CFUNC void U_EXPORT2 |
michael@0 | 1157 | ucol_buildPermutationTable(UCollator *coll, UErrorCode *status) { |
michael@0 | 1158 | uint16_t leadBytesSize = 256; |
michael@0 | 1159 | uint16_t leadBytes[256]; |
michael@0 | 1160 | |
michael@0 | 1161 | // The lowest byte that hasn't been assigned a mapping |
michael@0 | 1162 | int toBottom = 0x03; |
michael@0 | 1163 | // The highest byte that hasn't been assigned a mapping - don't include the special or trailing |
michael@0 | 1164 | int toTop = 0xe4; |
michael@0 | 1165 | |
michael@0 | 1166 | // are we filling from the bottom? |
michael@0 | 1167 | bool fromTheBottom = true; |
michael@0 | 1168 | int32_t reorderCodesIndex = -1; |
michael@0 | 1169 | |
michael@0 | 1170 | // lead bytes that have alread been assigned to the permutation table |
michael@0 | 1171 | bool newLeadByteUsed[256]; |
michael@0 | 1172 | // permutation table slots that have already been filled |
michael@0 | 1173 | bool permutationSlotFilled[256]; |
michael@0 | 1174 | |
michael@0 | 1175 | // nothing to do |
michael@0 | 1176 | if(U_FAILURE(*status) || coll == NULL) { |
michael@0 | 1177 | return; |
michael@0 | 1178 | } |
michael@0 | 1179 | |
michael@0 | 1180 | // clear the reordering |
michael@0 | 1181 | if (coll->reorderCodes == NULL || coll->reorderCodesLength == 0 |
michael@0 | 1182 | || (coll->reorderCodesLength == 1 && coll->reorderCodes[0] == UCOL_REORDER_CODE_NONE)) { |
michael@0 | 1183 | if (coll->leadBytePermutationTable != NULL) { |
michael@0 | 1184 | if (coll->freeLeadBytePermutationTableOnClose) { |
michael@0 | 1185 | uprv_free(coll->leadBytePermutationTable); |
michael@0 | 1186 | } |
michael@0 | 1187 | coll->leadBytePermutationTable = NULL; |
michael@0 | 1188 | coll->freeLeadBytePermutationTableOnClose = FALSE; |
michael@0 | 1189 | coll->reorderCodesLength = 0; |
michael@0 | 1190 | } |
michael@0 | 1191 | return; |
michael@0 | 1192 | } |
michael@0 | 1193 | |
michael@0 | 1194 | // set reordering to the default reordering |
michael@0 | 1195 | if (coll->reorderCodes[0] == UCOL_REORDER_CODE_DEFAULT) { |
michael@0 | 1196 | if (coll->reorderCodesLength != 1) { |
michael@0 | 1197 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 1198 | return; |
michael@0 | 1199 | } |
michael@0 | 1200 | if (coll->freeReorderCodesOnClose == TRUE) { |
michael@0 | 1201 | uprv_free(coll->reorderCodes); |
michael@0 | 1202 | } |
michael@0 | 1203 | coll->reorderCodes = NULL; |
michael@0 | 1204 | coll->freeReorderCodesOnClose = FALSE; |
michael@0 | 1205 | |
michael@0 | 1206 | if (coll->leadBytePermutationTable != NULL && coll->freeLeadBytePermutationTableOnClose == TRUE) { |
michael@0 | 1207 | uprv_free(coll->leadBytePermutationTable); |
michael@0 | 1208 | } |
michael@0 | 1209 | coll->leadBytePermutationTable = NULL; |
michael@0 | 1210 | coll->freeLeadBytePermutationTableOnClose = FALSE; |
michael@0 | 1211 | |
michael@0 | 1212 | if (coll->defaultReorderCodesLength == 0) { |
michael@0 | 1213 | return; |
michael@0 | 1214 | } |
michael@0 | 1215 | |
michael@0 | 1216 | coll->reorderCodes = (int32_t*)uprv_malloc(coll->defaultReorderCodesLength * sizeof(int32_t)); |
michael@0 | 1217 | if (coll->reorderCodes == NULL) { |
michael@0 | 1218 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 1219 | return; |
michael@0 | 1220 | } |
michael@0 | 1221 | coll->freeReorderCodesOnClose = TRUE; |
michael@0 | 1222 | coll->reorderCodesLength = coll->defaultReorderCodesLength; |
michael@0 | 1223 | uprv_memcpy(coll->reorderCodes, coll->defaultReorderCodes, coll->reorderCodesLength * sizeof(int32_t)); |
michael@0 | 1224 | } |
michael@0 | 1225 | |
michael@0 | 1226 | if (coll->leadBytePermutationTable == NULL) { |
michael@0 | 1227 | coll->leadBytePermutationTable = (uint8_t*)uprv_malloc(256*sizeof(uint8_t)); |
michael@0 | 1228 | if (coll->leadBytePermutationTable == NULL) { |
michael@0 | 1229 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 1230 | return; |
michael@0 | 1231 | } |
michael@0 | 1232 | coll->freeLeadBytePermutationTableOnClose = TRUE; |
michael@0 | 1233 | } |
michael@0 | 1234 | |
michael@0 | 1235 | int32_t internalReorderCodesLength = coll->reorderCodesLength + (UCOL_REORDER_CODE_LIMIT - UCOL_REORDER_CODE_FIRST); |
michael@0 | 1236 | LocalMemory<int32_t> internalReorderCodes((int32_t*)uprv_malloc(internalReorderCodesLength * sizeof(int32_t))); |
michael@0 | 1237 | if (internalReorderCodes.isNull()) { |
michael@0 | 1238 | *status = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 1239 | if (coll->leadBytePermutationTable != NULL && coll->freeLeadBytePermutationTableOnClose == TRUE) { |
michael@0 | 1240 | uprv_free(coll->leadBytePermutationTable); |
michael@0 | 1241 | } |
michael@0 | 1242 | coll->leadBytePermutationTable = NULL; |
michael@0 | 1243 | coll->freeLeadBytePermutationTableOnClose = FALSE; |
michael@0 | 1244 | return; |
michael@0 | 1245 | } |
michael@0 | 1246 | |
michael@0 | 1247 | // prefill the reordering codes with the leading entries |
michael@0 | 1248 | for (uint32_t codeIndex = 0; codeIndex < (UCOL_REORDER_CODE_LIMIT - UCOL_REORDER_CODE_FIRST); codeIndex++) { |
michael@0 | 1249 | internalReorderCodes[codeIndex] = UCOL_REORDER_CODE_FIRST + codeIndex; |
michael@0 | 1250 | } |
michael@0 | 1251 | for (int32_t codeIndex = 0; codeIndex < coll->reorderCodesLength; codeIndex++) { |
michael@0 | 1252 | uint32_t reorderCodesCode = coll->reorderCodes[codeIndex]; |
michael@0 | 1253 | internalReorderCodes[codeIndex + (UCOL_REORDER_CODE_LIMIT - UCOL_REORDER_CODE_FIRST)] = reorderCodesCode; |
michael@0 | 1254 | if (reorderCodesCode >= UCOL_REORDER_CODE_FIRST && reorderCodesCode < UCOL_REORDER_CODE_LIMIT) { |
michael@0 | 1255 | internalReorderCodes[reorderCodesCode - UCOL_REORDER_CODE_FIRST] = UCOL_REORDER_CODE_IGNORE; |
michael@0 | 1256 | } |
michael@0 | 1257 | } |
michael@0 | 1258 | |
michael@0 | 1259 | for (int i = 0; i < 256; i++) { |
michael@0 | 1260 | if (i < toBottom || i > toTop) { |
michael@0 | 1261 | permutationSlotFilled[i] = true; |
michael@0 | 1262 | newLeadByteUsed[i] = true; |
michael@0 | 1263 | coll->leadBytePermutationTable[i] = i; |
michael@0 | 1264 | } else { |
michael@0 | 1265 | permutationSlotFilled[i] = false; |
michael@0 | 1266 | newLeadByteUsed[i] = false; |
michael@0 | 1267 | coll->leadBytePermutationTable[i] = 0; |
michael@0 | 1268 | } |
michael@0 | 1269 | } |
michael@0 | 1270 | |
michael@0 | 1271 | /* Start from the front of the list and place each script we encounter at the |
michael@0 | 1272 | * earliest possible locatation in the permutation table. If we encounter |
michael@0 | 1273 | * UNKNOWN, start processing from the back, and place each script in the last |
michael@0 | 1274 | * possible location. At each step, we also need to make sure that any scripts |
michael@0 | 1275 | * that need to not be moved are copied to their same location in the final table. |
michael@0 | 1276 | */ |
michael@0 | 1277 | for (int reorderCodesCount = 0; reorderCodesCount < internalReorderCodesLength; reorderCodesCount++) { |
michael@0 | 1278 | reorderCodesIndex += fromTheBottom ? 1 : -1; |
michael@0 | 1279 | int32_t next = internalReorderCodes[reorderCodesIndex]; |
michael@0 | 1280 | if (next == UCOL_REORDER_CODE_IGNORE) { |
michael@0 | 1281 | continue; |
michael@0 | 1282 | } |
michael@0 | 1283 | if (next == USCRIPT_UNKNOWN) { |
michael@0 | 1284 | if (fromTheBottom == false) { |
michael@0 | 1285 | // double turnaround |
michael@0 | 1286 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 1287 | if (coll->leadBytePermutationTable != NULL && coll->freeLeadBytePermutationTableOnClose == TRUE) { |
michael@0 | 1288 | uprv_free(coll->leadBytePermutationTable); |
michael@0 | 1289 | } |
michael@0 | 1290 | coll->leadBytePermutationTable = NULL; |
michael@0 | 1291 | coll->freeLeadBytePermutationTableOnClose = FALSE; |
michael@0 | 1292 | coll->reorderCodesLength = 0; |
michael@0 | 1293 | return; |
michael@0 | 1294 | } |
michael@0 | 1295 | fromTheBottom = false; |
michael@0 | 1296 | reorderCodesIndex = internalReorderCodesLength; |
michael@0 | 1297 | continue; |
michael@0 | 1298 | } |
michael@0 | 1299 | |
michael@0 | 1300 | uint16_t leadByteCount = ucol_getLeadBytesForReorderCode(coll->UCA, next, leadBytes, leadBytesSize); |
michael@0 | 1301 | if (fromTheBottom) { |
michael@0 | 1302 | for (int leadByteIndex = 0; leadByteIndex < leadByteCount; leadByteIndex++) { |
michael@0 | 1303 | // don't place a lead byte twice in the permutation table |
michael@0 | 1304 | if (permutationSlotFilled[leadBytes[leadByteIndex]]) { |
michael@0 | 1305 | // lead byte already used |
michael@0 | 1306 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 1307 | if (coll->leadBytePermutationTable != NULL && coll->freeLeadBytePermutationTableOnClose == TRUE) { |
michael@0 | 1308 | uprv_free(coll->leadBytePermutationTable); |
michael@0 | 1309 | } |
michael@0 | 1310 | coll->leadBytePermutationTable = NULL; |
michael@0 | 1311 | coll->freeLeadBytePermutationTableOnClose = FALSE; |
michael@0 | 1312 | coll->reorderCodesLength = 0; |
michael@0 | 1313 | return; |
michael@0 | 1314 | } |
michael@0 | 1315 | |
michael@0 | 1316 | coll->leadBytePermutationTable[leadBytes[leadByteIndex]] = toBottom; |
michael@0 | 1317 | newLeadByteUsed[toBottom] = true; |
michael@0 | 1318 | permutationSlotFilled[leadBytes[leadByteIndex]] = true; |
michael@0 | 1319 | toBottom++; |
michael@0 | 1320 | } |
michael@0 | 1321 | } else { |
michael@0 | 1322 | for (int leadByteIndex = leadByteCount - 1; leadByteIndex >= 0; leadByteIndex--) { |
michael@0 | 1323 | // don't place a lead byte twice in the permutation table |
michael@0 | 1324 | if (permutationSlotFilled[leadBytes[leadByteIndex]]) { |
michael@0 | 1325 | // lead byte already used |
michael@0 | 1326 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 1327 | if (coll->leadBytePermutationTable != NULL && coll->freeLeadBytePermutationTableOnClose == TRUE) { |
michael@0 | 1328 | uprv_free(coll->leadBytePermutationTable); |
michael@0 | 1329 | } |
michael@0 | 1330 | coll->leadBytePermutationTable = NULL; |
michael@0 | 1331 | coll->freeLeadBytePermutationTableOnClose = FALSE; |
michael@0 | 1332 | coll->reorderCodesLength = 0; |
michael@0 | 1333 | return; |
michael@0 | 1334 | } |
michael@0 | 1335 | |
michael@0 | 1336 | coll->leadBytePermutationTable[leadBytes[leadByteIndex]] = toTop; |
michael@0 | 1337 | newLeadByteUsed[toTop] = true; |
michael@0 | 1338 | permutationSlotFilled[leadBytes[leadByteIndex]] = true; |
michael@0 | 1339 | toTop--; |
michael@0 | 1340 | } |
michael@0 | 1341 | } |
michael@0 | 1342 | } |
michael@0 | 1343 | |
michael@0 | 1344 | #ifdef REORDER_DEBUG |
michael@0 | 1345 | fprintf(stdout, "\n@@@@ Partial Script Reordering Table\n"); |
michael@0 | 1346 | for (int i = 0; i < 256; i++) { |
michael@0 | 1347 | fprintf(stdout, "\t%02x = %02x\n", i, coll->leadBytePermutationTable[i]); |
michael@0 | 1348 | } |
michael@0 | 1349 | fprintf(stdout, "\n@@@@ Lead Byte Used Table\n"); |
michael@0 | 1350 | for (int i = 0; i < 256; i++) { |
michael@0 | 1351 | fprintf(stdout, "\t%02x = %02x\n", i, newLeadByteUsed[i]); |
michael@0 | 1352 | } |
michael@0 | 1353 | fprintf(stdout, "\n@@@@ Permutation Slot Filled Table\n"); |
michael@0 | 1354 | for (int i = 0; i < 256; i++) { |
michael@0 | 1355 | fprintf(stdout, "\t%02x = %02x\n", i, permutationSlotFilled[i]); |
michael@0 | 1356 | } |
michael@0 | 1357 | #endif |
michael@0 | 1358 | |
michael@0 | 1359 | /* Copy everything that's left over */ |
michael@0 | 1360 | int reorderCode = 0; |
michael@0 | 1361 | for (int i = 0; i < 256; i++) { |
michael@0 | 1362 | if (!permutationSlotFilled[i]) { |
michael@0 | 1363 | while (reorderCode < 256 && newLeadByteUsed[reorderCode]) { |
michael@0 | 1364 | reorderCode++; |
michael@0 | 1365 | } |
michael@0 | 1366 | coll->leadBytePermutationTable[i] = reorderCode; |
michael@0 | 1367 | permutationSlotFilled[i] = true; |
michael@0 | 1368 | newLeadByteUsed[reorderCode] = true; |
michael@0 | 1369 | } |
michael@0 | 1370 | } |
michael@0 | 1371 | |
michael@0 | 1372 | #ifdef REORDER_DEBUG |
michael@0 | 1373 | fprintf(stdout, "\n@@@@ Script Reordering Table\n"); |
michael@0 | 1374 | for (int i = 0; i < 256; i++) { |
michael@0 | 1375 | fprintf(stdout, "\t%02x = %02x\n", i, coll->leadBytePermutationTable[i]); |
michael@0 | 1376 | } |
michael@0 | 1377 | #endif |
michael@0 | 1378 | |
michael@0 | 1379 | // force a regen of the latin one table since it is affected by the script reordering |
michael@0 | 1380 | coll->latinOneRegenTable = TRUE; |
michael@0 | 1381 | ucol_updateInternalState(coll, status); |
michael@0 | 1382 | } |
michael@0 | 1383 | |
michael@0 | 1384 | #endif /* #if !UCONFIG_NO_COLLATION */ |