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