]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/ucol_res.cpp
ICU-491.11.1.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 }
457 } else { /* no rules, but no error either */
458 // must be only options
459 // We will init the collator from UCA
460 result = ucol_initCollator(UCA->image, 0, UCA, status);
461 // Check for null result
462 if (U_FAILURE(*status)) {
463 goto cleanup;
464 }
465 // And set only the options
466 UColOptionSet *opts = (UColOptionSet *)uprv_malloc(sizeof(UColOptionSet));
467 /* test for NULL */
468 if (opts == NULL) {
469 *status = U_MEMORY_ALLOCATION_ERROR;
470 goto cleanup;
471 }
472 uprv_memcpy(opts, src.opts, sizeof(UColOptionSet));
473 ucol_setOptionsFromHeader(result, opts, status);
474 result->freeOptionsOnClose = TRUE;
475 result->hasRealData = FALSE;
476 result->freeImageOnClose = FALSE;
477 }
478
479 ucol_setReorderCodesFromParser(result, &src, status);
480
481 if(U_SUCCESS(*status)) {
482 UChar *newRules;
483 result->dataVersion[0] = UCOL_BUILDER_VERSION;
484 if(rulesLength > 0) {
485 newRules = (UChar *)uprv_malloc((rulesLength+1)*U_SIZEOF_UCHAR);
486 /* test for NULL */
487 if (newRules == NULL) {
488 *status = U_MEMORY_ALLOCATION_ERROR;
489 goto cleanup;
490 }
491 uprv_memcpy(newRules, rules, rulesLength*U_SIZEOF_UCHAR);
492 newRules[rulesLength]=0;
493 result->rules = newRules;
494 result->rulesLength = rulesLength;
495 result->freeRulesOnClose = TRUE;
496 }
497 result->ucaRules = NULL;
498 result->actualLocale = NULL;
499 result->validLocale = NULL;
500 result->requestedLocale = NULL;
501 ucol_buildPermutationTable(result, status);
502 ucol_setAttribute(result, UCOL_STRENGTH, strength, status);
503 ucol_setAttribute(result, UCOL_NORMALIZATION_MODE, norm, status);
504 } else {
505 cleanup:
506 if(result != NULL) {
507 ucol_close(result);
508 } else {
509 if(table != NULL) {
510 uprv_free(table);
511 }
512 }
513 result = NULL;
514 }
515
516 ucol_tok_closeTokenList(&src);
517
518 return result;
519 }
520
521 U_CAPI UCollator* U_EXPORT2
522 ucol_openRules( const UChar *rules,
523 int32_t rulesLength,
524 UColAttributeValue normalizationMode,
525 UCollationStrength strength,
526 UParseError *parseError,
527 UErrorCode *status)
528 {
529 return ucol_openRulesForImport(rules,
530 rulesLength,
531 normalizationMode,
532 strength,
533 parseError,
534 ucol_tok_getRulesFromBundle,
535 NULL,
536 status);
537 }
538
539 U_CAPI int32_t U_EXPORT2
540 ucol_getRulesEx(const UCollator *coll, UColRuleOption delta, UChar *buffer, int32_t bufferLen) {
541 UErrorCode status = U_ZERO_ERROR;
542 int32_t len = 0;
543 int32_t UCAlen = 0;
544 const UChar* ucaRules = 0;
545 const UChar *rules = ucol_getRules(coll, &len);
546 if(delta == UCOL_FULL_RULES) {
547 /* take the UCA rules and append real rules at the end */
548 /* UCA rules will be probably coming from the root RB */
549 ucaRules = coll->ucaRules;
550 if (ucaRules) {
551 UCAlen = u_strlen(ucaRules);
552 }
553 /*
554 ucaRules = ures_getStringByKey(coll->rb,"UCARules",&UCAlen,&status);
555 UResourceBundle* cresb = ures_getByKeyWithFallback(coll->rb, "collations", NULL, &status);
556 UResourceBundle* uca = ures_getByKeyWithFallback(cresb, "UCA", NULL, &status);
557 ucaRules = ures_getStringByKey(uca,"Sequence",&UCAlen,&status);
558 ures_close(uca);
559 ures_close(cresb);
560 */
561 }
562 if(U_FAILURE(status)) {
563 return 0;
564 }
565 if(buffer!=0 && bufferLen>0){
566 *buffer=0;
567 if(UCAlen > 0) {
568 u_memcpy(buffer, ucaRules, uprv_min(UCAlen, bufferLen));
569 }
570 if(len > 0 && bufferLen > UCAlen) {
571 u_memcpy(buffer+UCAlen, rules, uprv_min(len, bufferLen-UCAlen));
572 }
573 }
574 return u_terminateUChars(buffer, bufferLen, len+UCAlen, &status);
575 }
576
577 static const UChar _NUL = 0;
578
579 U_CAPI const UChar* U_EXPORT2
580 ucol_getRules( const UCollator *coll,
581 int32_t *length)
582 {
583 if(coll->rules != NULL) {
584 *length = coll->rulesLength;
585 return coll->rules;
586 }
587 else {
588 *length = 0;
589 return &_NUL;
590 }
591 }
592
593 U_CAPI UBool U_EXPORT2
594 ucol_equals(const UCollator *source, const UCollator *target) {
595 UErrorCode status = U_ZERO_ERROR;
596 // if pointers are equal, collators are equal
597 if(source == target) {
598 return TRUE;
599 }
600 int32_t i = 0, j = 0;
601 // if any of attributes are different, collators are not equal
602 for(i = 0; i < UCOL_ATTRIBUTE_COUNT; i++) {
603 if(ucol_getAttribute(source, (UColAttribute)i, &status) != ucol_getAttribute(target, (UColAttribute)i, &status) || U_FAILURE(status)) {
604 return FALSE;
605 }
606 }
607 if (source->reorderCodesLength != target->reorderCodesLength){
608 return FALSE;
609 }
610 for (i = 0; i < source->reorderCodesLength; i++) {
611 if(source->reorderCodes[i] != target->reorderCodes[i]) {
612 return FALSE;
613 }
614 }
615
616 int32_t sourceRulesLen = 0, targetRulesLen = 0;
617 const UChar *sourceRules = ucol_getRules(source, &sourceRulesLen);
618 const UChar *targetRules = ucol_getRules(target, &targetRulesLen);
619
620 if(sourceRulesLen == targetRulesLen && u_strncmp(sourceRules, targetRules, sourceRulesLen) == 0) {
621 // all the attributes are equal and the rules are equal - collators are equal
622 return(TRUE);
623 }
624 // hard part, need to construct tree from rules and see if they yield the same tailoring
625 UBool result = TRUE;
626 UParseError parseError;
627 UColTokenParser sourceParser, targetParser;
628 int32_t sourceListLen = 0, targetListLen = 0;
629 ucol_tok_initTokenList(&sourceParser, sourceRules, sourceRulesLen, source->UCA, ucol_tok_getRulesFromBundle, NULL, &status);
630 ucol_tok_initTokenList(&targetParser, targetRules, targetRulesLen, target->UCA, ucol_tok_getRulesFromBundle, NULL, &status);
631 sourceListLen = ucol_tok_assembleTokenList(&sourceParser, &parseError, &status);
632 targetListLen = ucol_tok_assembleTokenList(&targetParser, &parseError, &status);
633
634 if(sourceListLen != targetListLen) {
635 // different number of resets
636 result = FALSE;
637 } else {
638 UColToken *sourceReset = NULL, *targetReset = NULL;
639 UChar *sourceResetString = NULL, *targetResetString = NULL;
640 int32_t sourceStringLen = 0, targetStringLen = 0;
641 for(i = 0; i < sourceListLen; i++) {
642 sourceReset = sourceParser.lh[i].reset;
643 sourceResetString = sourceParser.source+(sourceReset->source & 0xFFFFFF);
644 sourceStringLen = sourceReset->source >> 24;
645 for(j = 0; j < sourceListLen; j++) {
646 targetReset = targetParser.lh[j].reset;
647 targetResetString = targetParser.source+(targetReset->source & 0xFFFFFF);
648 targetStringLen = targetReset->source >> 24;
649 if(sourceStringLen == targetStringLen && (u_strncmp(sourceResetString, targetResetString, sourceStringLen) == 0)) {
650 sourceReset = sourceParser.lh[i].first;
651 targetReset = targetParser.lh[j].first;
652 while(sourceReset != NULL && targetReset != NULL) {
653 sourceResetString = sourceParser.source+(sourceReset->source & 0xFFFFFF);
654 sourceStringLen = sourceReset->source >> 24;
655 targetResetString = targetParser.source+(targetReset->source & 0xFFFFFF);
656 targetStringLen = targetReset->source >> 24;
657 if(sourceStringLen != targetStringLen || (u_strncmp(sourceResetString, targetResetString, sourceStringLen) != 0)) {
658 result = FALSE;
659 goto returnResult;
660 }
661 // probably also need to check the expansions
662 if(sourceReset->expansion) {
663 if(!targetReset->expansion) {
664 result = FALSE;
665 goto returnResult;
666 } else {
667 // compare expansions
668 sourceResetString = sourceParser.source+(sourceReset->expansion& 0xFFFFFF);
669 sourceStringLen = sourceReset->expansion >> 24;
670 targetResetString = targetParser.source+(targetReset->expansion & 0xFFFFFF);
671 targetStringLen = targetReset->expansion >> 24;
672 if(sourceStringLen != targetStringLen || (u_strncmp(sourceResetString, targetResetString, sourceStringLen) != 0)) {
673 result = FALSE;
674 goto returnResult;
675 }
676 }
677 } else {
678 if(targetReset->expansion) {
679 result = FALSE;
680 goto returnResult;
681 }
682 }
683 sourceReset = sourceReset->next;
684 targetReset = targetReset->next;
685 }
686 if(sourceReset != targetReset) { // at least one is not NULL
687 // there are more tailored elements in one list
688 result = FALSE;
689 goto returnResult;
690 }
691
692
693 break;
694 }
695 }
696 // couldn't find the reset anchor, so the collators are not equal
697 if(j == sourceListLen) {
698 result = FALSE;
699 goto returnResult;
700 }
701 }
702 }
703
704 returnResult:
705 ucol_tok_closeTokenList(&sourceParser);
706 ucol_tok_closeTokenList(&targetParser);
707 return result;
708
709 }
710
711 U_CAPI int32_t U_EXPORT2
712 ucol_getDisplayName( const char *objLoc,
713 const char *dispLoc,
714 UChar *result,
715 int32_t resultLength,
716 UErrorCode *status)
717 {
718 U_NAMESPACE_USE
719
720 if(U_FAILURE(*status)) return -1;
721 UnicodeString dst;
722 if(!(result==NULL && resultLength==0)) {
723 // NULL destination for pure preflighting: empty dummy string
724 // otherwise, alias the destination buffer
725 dst.setTo(result, 0, resultLength);
726 }
727 Collator::getDisplayName(Locale(objLoc), Locale(dispLoc), dst);
728 return dst.extract(result, resultLength, *status);
729 }
730
731 U_CAPI const char* U_EXPORT2
732 ucol_getAvailable(int32_t index)
733 {
734 int32_t count = 0;
735 const Locale *loc = Collator::getAvailableLocales(count);
736 if (loc != NULL && index < count) {
737 return loc[index].getName();
738 }
739 return NULL;
740 }
741
742 U_CAPI int32_t U_EXPORT2
743 ucol_countAvailable()
744 {
745 int32_t count = 0;
746 Collator::getAvailableLocales(count);
747 return count;
748 }
749
750 #if !UCONFIG_NO_SERVICE
751 U_CAPI UEnumeration* U_EXPORT2
752 ucol_openAvailableLocales(UErrorCode *status) {
753 U_NAMESPACE_USE
754
755 // This is a wrapper over Collator::getAvailableLocales()
756 if (U_FAILURE(*status)) {
757 return NULL;
758 }
759 StringEnumeration *s = icu::Collator::getAvailableLocales();
760 if (s == NULL) {
761 *status = U_MEMORY_ALLOCATION_ERROR;
762 return NULL;
763 }
764 return uenum_openFromStringEnumeration(s, status);
765 }
766 #endif
767
768 // Note: KEYWORDS[0] != RESOURCE_NAME - alan
769
770 static const char RESOURCE_NAME[] = "collations";
771
772 static const char* const KEYWORDS[] = { "collation" };
773
774 #define KEYWORD_COUNT (sizeof(KEYWORDS)/sizeof(KEYWORDS[0]))
775
776 U_CAPI UEnumeration* U_EXPORT2
777 ucol_getKeywords(UErrorCode *status) {
778 UEnumeration *result = NULL;
779 if (U_SUCCESS(*status)) {
780 return uenum_openCharStringsEnumeration(KEYWORDS, KEYWORD_COUNT, status);
781 }
782 return result;
783 }
784
785 U_CAPI UEnumeration* U_EXPORT2
786 ucol_getKeywordValues(const char *keyword, UErrorCode *status) {
787 if (U_FAILURE(*status)) {
788 return NULL;
789 }
790 // hard-coded to accept exactly one collation keyword
791 // modify if additional collation keyword is added later
792 if (keyword==NULL || uprv_strcmp(keyword, KEYWORDS[0])!=0)
793 {
794 *status = U_ILLEGAL_ARGUMENT_ERROR;
795 return NULL;
796 }
797 return ures_getKeywordValues(U_ICUDATA_COLL, RESOURCE_NAME, status);
798 }
799
800 static const UEnumeration defaultKeywordValues = {
801 NULL,
802 NULL,
803 ulist_close_keyword_values_iterator,
804 ulist_count_keyword_values,
805 uenum_unextDefault,
806 ulist_next_keyword_value,
807 ulist_reset_keyword_values_iterator
808 };
809
810 #include <stdio.h>
811
812 U_CAPI UEnumeration* U_EXPORT2
813 ucol_getKeywordValuesForLocale(const char* /*key*/, const char* locale,
814 UBool /*commonlyUsed*/, UErrorCode* status) {
815 /* Get the locale base name. */
816 char localeBuffer[ULOC_FULLNAME_CAPACITY] = "";
817 uloc_getBaseName(locale, localeBuffer, sizeof(localeBuffer), status);
818
819 /* Create the 2 lists
820 * -values is the temp location for the keyword values
821 * -results hold the actual list used by the UEnumeration object
822 */
823 UList *values = ulist_createEmptyList(status);
824 UList *results = ulist_createEmptyList(status);
825 UEnumeration *en = (UEnumeration *)uprv_malloc(sizeof(UEnumeration));
826 if (U_FAILURE(*status) || en == NULL) {
827 if (en == NULL) {
828 *status = U_MEMORY_ALLOCATION_ERROR;
829 } else {
830 uprv_free(en);
831 }
832 ulist_deleteList(values);
833 ulist_deleteList(results);
834 return NULL;
835 }
836
837 memcpy(en, &defaultKeywordValues, sizeof(UEnumeration));
838 en->context = results;
839
840 /* Open the resource bundle for collation with the given locale. */
841 UResourceBundle bundle, collations, collres, defres;
842 ures_initStackObject(&bundle);
843 ures_initStackObject(&collations);
844 ures_initStackObject(&collres);
845 ures_initStackObject(&defres);
846
847 ures_openFillIn(&bundle, U_ICUDATA_COLL, localeBuffer, status);
848
849 while (U_SUCCESS(*status)) {
850 ures_getByKey(&bundle, RESOURCE_NAME, &collations, status);
851 ures_resetIterator(&collations);
852 while (U_SUCCESS(*status) && ures_hasNext(&collations)) {
853 ures_getNextResource(&collations, &collres, status);
854 const char *key = ures_getKey(&collres);
855 /* If the key is default, get the string and store it in results list only
856 * if results list is empty.
857 */
858 if (uprv_strcmp(key, "default") == 0) {
859 if (ulist_getListSize(results) == 0) {
860 char *defcoll = (char *)uprv_malloc(sizeof(char) * ULOC_KEYWORDS_CAPACITY);
861 int32_t defcollLength = ULOC_KEYWORDS_CAPACITY;
862
863 ures_getNextResource(&collres, &defres, status);
864 #if U_CHARSET_FAMILY==U_ASCII_FAMILY
865 /* optimize - use the utf-8 string */
866 ures_getUTF8String(&defres, defcoll, &defcollLength, TRUE, status);
867 #else
868 {
869 const UChar* defString = ures_getString(&defres, &defcollLength, status);
870 if(U_SUCCESS(*status)) {
871 if(defcollLength+1 > ULOC_KEYWORDS_CAPACITY) {
872 *status = U_BUFFER_OVERFLOW_ERROR;
873 } else {
874 u_UCharsToChars(defString, defcoll, defcollLength+1);
875 }
876 }
877 }
878 #endif
879
880 ulist_addItemBeginList(results, defcoll, TRUE, status);
881 }
882 } else {
883 ulist_addItemEndList(values, key, FALSE, status);
884 }
885 }
886
887 /* If the locale is "" this is root so exit. */
888 if (uprv_strlen(localeBuffer) == 0) {
889 break;
890 }
891 /* Get the parent locale and open a new resource bundle. */
892 uloc_getParent(localeBuffer, localeBuffer, sizeof(localeBuffer), status);
893 ures_openFillIn(&bundle, U_ICUDATA_COLL, localeBuffer, status);
894 }
895
896 ures_close(&defres);
897 ures_close(&collres);
898 ures_close(&collations);
899 ures_close(&bundle);
900
901 if (U_SUCCESS(*status)) {
902 char *value = NULL;
903 ulist_resetList(values);
904 while ((value = (char *)ulist_getNext(values)) != NULL) {
905 if (!ulist_containsString(results, value, (int32_t)uprv_strlen(value))) {
906 ulist_addItemEndList(results, value, FALSE, status);
907 if (U_FAILURE(*status)) {
908 break;
909 }
910 }
911 }
912 }
913
914 ulist_deleteList(values);
915
916 if (U_FAILURE(*status)){
917 uenum_close(en);
918 en = NULL;
919 } else {
920 ulist_resetList(results);
921 }
922
923 return en;
924 }
925
926 U_CAPI int32_t U_EXPORT2
927 ucol_getFunctionalEquivalent(char* result, int32_t resultCapacity,
928 const char* keyword, const char* locale,
929 UBool* isAvailable, UErrorCode* status)
930 {
931 // N.B.: Resource name is "collations" but keyword is "collation"
932 return ures_getFunctionalEquivalent(result, resultCapacity, U_ICUDATA_COLL,
933 "collations", keyword, locale,
934 isAvailable, TRUE, status);
935 }
936
937 /* returns the locale name the collation data comes from */
938 U_CAPI const char * U_EXPORT2
939 ucol_getLocale(const UCollator *coll, ULocDataLocaleType type, UErrorCode *status) {
940 return ucol_getLocaleByType(coll, type, status);
941 }
942
943 U_CAPI const char * U_EXPORT2
944 ucol_getLocaleByType(const UCollator *coll, ULocDataLocaleType type, UErrorCode *status) {
945 const char *result = NULL;
946 if(status == NULL || U_FAILURE(*status)) {
947 return NULL;
948 }
949 UTRACE_ENTRY(UTRACE_UCOL_GETLOCALE);
950 UTRACE_DATA1(UTRACE_INFO, "coll=%p", coll);
951
952 if(coll->delegate!=NULL) {
953 return ((const Collator*)coll->delegate)->getLocale(type, *status).getName();
954 }
955 switch(type) {
956 case ULOC_ACTUAL_LOCALE:
957 result = coll->actualLocale;
958 break;
959 case ULOC_VALID_LOCALE:
960 result = coll->validLocale;
961 break;
962 case ULOC_REQUESTED_LOCALE:
963 result = coll->requestedLocale;
964 break;
965 default:
966 *status = U_ILLEGAL_ARGUMENT_ERROR;
967 }
968 UTRACE_DATA1(UTRACE_INFO, "result = %s", result);
969 UTRACE_EXIT_STATUS(*status);
970 return result;
971 }
972
973 U_CFUNC void U_EXPORT2
974 ucol_setReqValidLocales(UCollator *coll, char *requestedLocaleToAdopt, char *validLocaleToAdopt, char *actualLocaleToAdopt)
975 {
976 if (coll) {
977 if (coll->validLocale) {
978 uprv_free(coll->validLocale);
979 }
980 coll->validLocale = validLocaleToAdopt;
981 if (coll->requestedLocale) { // should always have
982 uprv_free(coll->requestedLocale);
983 }
984 coll->requestedLocale = requestedLocaleToAdopt;
985 if (coll->actualLocale) {
986 uprv_free(coll->actualLocale);
987 }
988 coll->actualLocale = actualLocaleToAdopt;
989 }
990 }
991
992 U_CAPI USet * U_EXPORT2
993 ucol_getTailoredSet(const UCollator *coll, UErrorCode *status)
994 {
995 U_NAMESPACE_USE
996
997 if(status == NULL || U_FAILURE(*status)) {
998 return NULL;
999 }
1000 if(coll == NULL || coll->UCA == NULL) {
1001 *status = U_ILLEGAL_ARGUMENT_ERROR;
1002 return NULL;
1003 }
1004 UParseError parseError;
1005 UColTokenParser src;
1006 int32_t rulesLen = 0;
1007 const UChar *rules = ucol_getRules(coll, &rulesLen);
1008 UBool startOfRules = TRUE;
1009 // we internally use the C++ class, for the following reasons:
1010 // 1. we need to utilize canonical iterator, which is a C++ only class
1011 // 2. canonical iterator returns UnicodeStrings - USet cannot take them
1012 // 3. USet is internally really UnicodeSet, C is just a wrapper
1013 UnicodeSet *tailored = new UnicodeSet();
1014 UnicodeString pattern;
1015 UnicodeString empty;
1016 CanonicalIterator it(empty, *status);
1017
1018
1019 // The idea is to tokenize the rule set. For each non-reset token,
1020 // we add all the canonicaly equivalent FCD sequences
1021 ucol_tok_initTokenList(&src, rules, rulesLen, coll->UCA, ucol_tok_getRulesFromBundle, NULL, status);
1022 while (ucol_tok_parseNextToken(&src, startOfRules, &parseError, status) != NULL) {
1023 startOfRules = FALSE;
1024 if(src.parsedToken.strength != UCOL_TOK_RESET) {
1025 const UChar *stuff = src.source+(src.parsedToken.charsOffset);
1026 it.setSource(UnicodeString(stuff, src.parsedToken.charsLen), *status);
1027 pattern = it.next();
1028 while(!pattern.isBogus()) {
1029 if(Normalizer::quickCheck(pattern, UNORM_FCD, *status) != UNORM_NO) {
1030 tailored->add(pattern);
1031 }
1032 pattern = it.next();
1033 }
1034 }
1035 }
1036 ucol_tok_closeTokenList(&src);
1037 return (USet *)tailored;
1038 }
1039
1040 /*
1041 * Collation Reordering
1042 */
1043
1044 void ucol_setReorderCodesFromParser(UCollator *coll, UColTokenParser *parser, UErrorCode *status) {
1045 if (U_FAILURE(*status)) {
1046 return;
1047 }
1048
1049 if (parser->reorderCodesLength == 0 || parser->reorderCodes == NULL) {
1050 return;
1051 }
1052
1053 coll->reorderCodesLength = 0;
1054 if (coll->reorderCodes != NULL && coll->freeReorderCodesOnClose == TRUE) {
1055 uprv_free(coll->reorderCodes);
1056 }
1057
1058 if (coll->defaultReorderCodes != NULL && coll->freeDefaultReorderCodesOnClose == TRUE) {
1059 uprv_free(coll->defaultReorderCodes);
1060 }
1061 coll->defaultReorderCodesLength = parser->reorderCodesLength;
1062 coll->defaultReorderCodes = (int32_t*) uprv_malloc(coll->defaultReorderCodesLength * sizeof(int32_t));
1063 if (coll->defaultReorderCodes == NULL) {
1064 *status = U_MEMORY_ALLOCATION_ERROR;
1065 return;
1066 }
1067 uprv_memcpy(coll->defaultReorderCodes, parser->reorderCodes, coll->defaultReorderCodesLength * sizeof(int32_t));
1068 coll->freeDefaultReorderCodesOnClose = TRUE;
1069
1070 coll->reorderCodesLength = parser->reorderCodesLength;
1071 coll->reorderCodes = (int32_t*) uprv_malloc(coll->reorderCodesLength * sizeof(int32_t));
1072 if (coll->reorderCodes == NULL) {
1073 *status = U_MEMORY_ALLOCATION_ERROR;
1074 return;
1075 }
1076 uprv_memcpy(coll->reorderCodes, parser->reorderCodes, coll->reorderCodesLength * sizeof(int32_t));
1077 coll->freeReorderCodesOnClose = TRUE;
1078 }
1079
1080 /*
1081 * Data is stored in the reorder code to lead byte table as:
1082 * index count - unsigned short (2 bytes) - number of index entries
1083 * data size - unsigned short (2 bytes) - number of unsigned short data elements
1084 * index[index count] - array of 2 unsigned shorts (4 bytes each entry)
1085 * - reorder code, offset
1086 * - index is sorted by reorder code
1087 * - if an offset has the high bit set then it is not an offset but a single data entry
1088 * once the high bit is stripped off
1089 * data[data size] - array of unsigned short (2 bytes each entry)
1090 * - the data is an usigned short count followed by count number
1091 * of lead bytes stored in an unsigned short
1092 */
1093 U_CFUNC int U_EXPORT2
1094 ucol_getLeadBytesForReorderCode(const UCollator *uca, int reorderCode, uint16_t* returnLeadBytes, int returnCapacity) {
1095 uint16_t reorderCodeIndexLength = *((uint16_t*) ((uint8_t *)uca->image + uca->image->scriptToLeadByte));
1096 uint16_t* reorderCodeIndex = (uint16_t*) ((uint8_t *)uca->image + uca->image->scriptToLeadByte + 2 *sizeof(uint16_t));
1097
1098 // reorder code index is 2 uint16_t's - reorder code + offset
1099 for (int i = 0; i < reorderCodeIndexLength; i++) {
1100 if (reorderCode == reorderCodeIndex[i*2]) {
1101 uint16_t dataOffset = reorderCodeIndex[(i*2) + 1];
1102 if ((dataOffset & 0x8000) == 0x8000) {
1103 // offset isn't offset but instead is a single data element
1104 if (returnCapacity >= 1) {
1105 returnLeadBytes[0] = dataOffset & ~0x8000;
1106 return 1;
1107 }
1108 return 0;
1109 }
1110 uint16_t* dataOffsetBase = (uint16_t*) ((uint8_t *)reorderCodeIndex + reorderCodeIndexLength * (2 * sizeof(uint16_t)));
1111 uint16_t leadByteCount = *(dataOffsetBase + dataOffset);
1112 leadByteCount = leadByteCount > returnCapacity ? returnCapacity : leadByteCount;
1113 uprv_memcpy(returnLeadBytes, dataOffsetBase + dataOffset + 1, leadByteCount * sizeof(uint16_t));
1114 return leadByteCount;
1115 }
1116 }
1117 return 0;
1118 }
1119
1120 /*
1121 * Data is stored in the lead byte to reorder code table as:
1122 * index count - unsigned short (2 bytes) - number of index entries
1123 * data size - unsigned short (2 bytes) - number of unsigned short data elements
1124 * index[index count] - array of unsigned short (2 bytes each entry)
1125 * - index is sorted by lead byte
1126 * - if an index has the high bit set then it is not an index but a single data entry
1127 * once the high bit is stripped off
1128 * data[data size] - array of unsigned short (2 bytes each entry)
1129 * - the data is an usigned short count followed by count number of reorder codes
1130 */
1131 U_CFUNC int U_EXPORT2
1132 ucol_getReorderCodesForLeadByte(const UCollator *uca, int leadByte, int16_t* returnReorderCodes, int returnCapacity) {
1133 uint16_t* leadByteTable = ((uint16_t*) ((uint8_t *)uca->image + uca->image->leadByteToScript));
1134 uint16_t leadByteIndexLength = *leadByteTable;
1135 if (leadByte >= leadByteIndexLength) {
1136 return 0;
1137 }
1138 uint16_t leadByteIndex = *(leadByteTable + (2 + leadByte));
1139
1140 if ((leadByteIndex & 0x8000) == 0x8000) {
1141 // offset isn't offset but instead is a single data element
1142 if (returnCapacity >= 1) {
1143 returnReorderCodes[0] = leadByteIndex & ~0x8000;
1144 return 1;
1145 }
1146 return 0;
1147 }
1148 //uint16_t* dataOffsetBase = leadByteTable + (2 + leadByteIndexLength);
1149 uint16_t* reorderCodeData = leadByteTable + (2 + leadByteIndexLength) + leadByteIndex;
1150 uint16_t reorderCodeCount = *reorderCodeData > returnCapacity ? returnCapacity : *reorderCodeData;
1151 uprv_memcpy(returnReorderCodes, reorderCodeData + 1, reorderCodeCount * sizeof(uint16_t));
1152 return reorderCodeCount;
1153 }
1154
1155 // used to mark ignorable reorder code slots
1156 static const int32_t UCOL_REORDER_CODE_IGNORE = UCOL_REORDER_CODE_LIMIT + 1;
1157
1158 U_CFUNC void U_EXPORT2
1159 ucol_buildPermutationTable(UCollator *coll, UErrorCode *status) {
1160 uint16_t leadBytesSize = 256;
1161 uint16_t leadBytes[256];
1162 int32_t internalReorderCodesLength = coll->reorderCodesLength + (UCOL_REORDER_CODE_LIMIT - UCOL_REORDER_CODE_FIRST);
1163 int32_t* internalReorderCodes;
1164
1165 // The lowest byte that hasn't been assigned a mapping
1166 int toBottom = 0x03;
1167 // The highest byte that hasn't been assigned a mapping - don't include the special or trailing
1168 int toTop = 0xe4;
1169
1170 // are we filling from the bottom?
1171 bool fromTheBottom = true;
1172 int32_t reorderCodesIndex = -1;
1173
1174 // lead bytes that have alread been assigned to the permutation table
1175 bool newLeadByteUsed[256];
1176 // permutation table slots that have already been filled
1177 bool permutationSlotFilled[256];
1178
1179 // nothing to do
1180 if(U_FAILURE(*status) || coll == NULL) {
1181 return;
1182 }
1183
1184 // clear the reordering
1185 if (coll->reorderCodes == NULL || coll->reorderCodesLength == 0
1186 || (coll->reorderCodesLength == 1 && coll->reorderCodes[0] == UCOL_REORDER_CODE_NONE)) {
1187 if (coll->leadBytePermutationTable != NULL) {
1188 if (coll->freeLeadBytePermutationTableOnClose) {
1189 uprv_free(coll->leadBytePermutationTable);
1190 }
1191 coll->leadBytePermutationTable = NULL;
1192 coll->reorderCodesLength = 0;
1193 }
1194 return;
1195 }
1196
1197 // set reordering to the default reordering
1198 if (coll->reorderCodes[0] == UCOL_REORDER_CODE_DEFAULT) {
1199 if (coll->reorderCodesLength != 1) {
1200 *status = U_ILLEGAL_ARGUMENT_ERROR;
1201 return;
1202 }
1203 if (coll->freeReorderCodesOnClose == TRUE) {
1204 uprv_free(coll->reorderCodes);
1205 }
1206 coll->reorderCodes = NULL;
1207
1208 if (coll->leadBytePermutationTable != NULL && coll->freeLeadBytePermutationTableOnClose == TRUE) {
1209 uprv_free(coll->leadBytePermutationTable);
1210 }
1211 coll->leadBytePermutationTable = NULL;
1212
1213 if (coll->defaultReorderCodesLength == 0) {
1214 return;
1215 }
1216
1217 coll->reorderCodes = (int32_t*)uprv_malloc(coll->defaultReorderCodesLength * sizeof(int32_t));
1218 coll->freeReorderCodesOnClose = TRUE;
1219 if (coll->reorderCodes == NULL) {
1220 *status = U_MEMORY_ALLOCATION_ERROR;
1221 return;
1222 }
1223 coll->reorderCodesLength = coll->defaultReorderCodesLength;
1224 uprv_memcpy(coll->defaultReorderCodes, coll->reorderCodes, coll->reorderCodesLength * sizeof(int32_t));
1225 }
1226
1227 if (coll->leadBytePermutationTable == NULL) {
1228 coll->leadBytePermutationTable = (uint8_t*)uprv_malloc(256*sizeof(uint8_t));
1229 coll->freeLeadBytePermutationTableOnClose = TRUE;
1230 if (coll->leadBytePermutationTable == NULL) {
1231 *status = U_MEMORY_ALLOCATION_ERROR;
1232 return;
1233 }
1234 }
1235
1236 // prefill the reordering codes with the leading entries
1237 internalReorderCodes = (int32_t*)uprv_malloc(internalReorderCodesLength * sizeof(int32_t));
1238 if (internalReorderCodes == NULL) {
1239 *status = U_MEMORY_ALLOCATION_ERROR;
1240 if (coll->leadBytePermutationTable != NULL && coll->freeLeadBytePermutationTableOnClose == TRUE) {
1241 uprv_free(coll->leadBytePermutationTable);
1242 }
1243 coll->leadBytePermutationTable = NULL;
1244 return;
1245 }
1246
1247 for (uint32_t codeIndex = 0; codeIndex < (UCOL_REORDER_CODE_LIMIT - UCOL_REORDER_CODE_FIRST); codeIndex++) {
1248 internalReorderCodes[codeIndex] = UCOL_REORDER_CODE_FIRST + codeIndex;
1249 }
1250 for (int32_t codeIndex = 0; codeIndex < coll->reorderCodesLength; codeIndex++) {
1251 uint32_t reorderCodesCode = coll->reorderCodes[codeIndex];
1252 internalReorderCodes[codeIndex + (UCOL_REORDER_CODE_LIMIT - UCOL_REORDER_CODE_FIRST)] = reorderCodesCode;
1253 if (reorderCodesCode >= UCOL_REORDER_CODE_FIRST && reorderCodesCode < UCOL_REORDER_CODE_LIMIT) {
1254 internalReorderCodes[reorderCodesCode - UCOL_REORDER_CODE_FIRST] = UCOL_REORDER_CODE_IGNORE;
1255 }
1256 }
1257
1258 for (int i = 0; i < 256; i++) {
1259 if (i < toBottom || i > toTop) {
1260 permutationSlotFilled[i] = true;
1261 newLeadByteUsed[i] = true;
1262 coll->leadBytePermutationTable[i] = i;
1263 } else {
1264 permutationSlotFilled[i] = false;
1265 newLeadByteUsed[i] = false;
1266 coll->leadBytePermutationTable[i] = 0;
1267 }
1268 }
1269
1270 /* Start from the front of the list and place each script we encounter at the
1271 * earliest possible locatation in the permutation table. If we encounter
1272 * UNKNOWN, start processing from the back, and place each script in the last
1273 * possible location. At each step, we also need to make sure that any scripts
1274 * that need to not be moved are copied to their same location in the final table.
1275 */
1276 for (int reorderCodesCount = 0; reorderCodesCount < internalReorderCodesLength; reorderCodesCount++) {
1277 reorderCodesIndex += fromTheBottom ? 1 : -1;
1278 int32_t next = internalReorderCodes[reorderCodesIndex];
1279 if (next == UCOL_REORDER_CODE_IGNORE) {
1280 continue;
1281 }
1282 if (next == USCRIPT_UNKNOWN) {
1283 if (fromTheBottom == false) {
1284 // double turnaround
1285 *status = U_ILLEGAL_ARGUMENT_ERROR;
1286 if (coll->leadBytePermutationTable != NULL && coll->freeLeadBytePermutationTableOnClose == TRUE) {
1287 uprv_free(coll->leadBytePermutationTable);
1288 }
1289 coll->leadBytePermutationTable = NULL;
1290 coll->reorderCodesLength = 0;
1291 if (internalReorderCodes != NULL) {
1292 uprv_free(internalReorderCodes);
1293 }
1294 return;
1295 }
1296 fromTheBottom = false;
1297 reorderCodesIndex = internalReorderCodesLength;
1298 continue;
1299 }
1300
1301 uint16_t leadByteCount = ucol_getLeadBytesForReorderCode(coll->UCA, next, leadBytes, leadBytesSize);
1302 if (fromTheBottom) {
1303 for (int leadByteIndex = 0; leadByteIndex < leadByteCount; leadByteIndex++) {
1304 // don't place a lead byte twice in the permutation table
1305 if (permutationSlotFilled[leadBytes[leadByteIndex]]) {
1306 // lead byte already used
1307 *status = U_ILLEGAL_ARGUMENT_ERROR;
1308 if (coll->leadBytePermutationTable != NULL && coll->freeLeadBytePermutationTableOnClose == TRUE) {
1309 uprv_free(coll->leadBytePermutationTable);
1310 }
1311 coll->leadBytePermutationTable = NULL;
1312 coll->reorderCodesLength = 0;
1313 if (internalReorderCodes != NULL) {
1314 uprv_free(internalReorderCodes);
1315 }
1316 return;
1317 }
1318
1319 coll->leadBytePermutationTable[leadBytes[leadByteIndex]] = toBottom;
1320 newLeadByteUsed[toBottom] = true;
1321 permutationSlotFilled[leadBytes[leadByteIndex]] = true;
1322 toBottom++;
1323 }
1324 } else {
1325 for (int leadByteIndex = leadByteCount - 1; leadByteIndex >= 0; leadByteIndex--) {
1326 // don't place a lead byte twice in the permutation table
1327 if (permutationSlotFilled[leadBytes[leadByteIndex]]) {
1328 // lead byte already used
1329 *status = U_ILLEGAL_ARGUMENT_ERROR;
1330 if (coll->leadBytePermutationTable != NULL && coll->freeLeadBytePermutationTableOnClose == TRUE) {
1331 uprv_free(coll->leadBytePermutationTable);
1332 }
1333 coll->leadBytePermutationTable = NULL;
1334 coll->reorderCodesLength = 0;
1335 if (internalReorderCodes != NULL) {
1336 uprv_free(internalReorderCodes);
1337 }
1338 return;
1339 }
1340
1341 coll->leadBytePermutationTable[leadBytes[leadByteIndex]] = toTop;
1342 newLeadByteUsed[toTop] = true;
1343 permutationSlotFilled[leadBytes[leadByteIndex]] = true;
1344 toTop--;
1345 }
1346 }
1347 }
1348
1349 #ifdef REORDER_DEBUG
1350 fprintf(stdout, "\n@@@@ Partial Script Reordering Table\n");
1351 for (int i = 0; i < 256; i++) {
1352 fprintf(stdout, "\t%02x = %02x\n", i, coll->leadBytePermutationTable[i]);
1353 }
1354 fprintf(stdout, "\n@@@@ Lead Byte Used Table\n");
1355 for (int i = 0; i < 256; i++) {
1356 fprintf(stdout, "\t%02x = %02x\n", i, newLeadByteUsed[i]);
1357 }
1358 fprintf(stdout, "\n@@@@ Permutation Slot Filled Table\n");
1359 for (int i = 0; i < 256; i++) {
1360 fprintf(stdout, "\t%02x = %02x\n", i, permutationSlotFilled[i]);
1361 }
1362 #endif
1363
1364 /* Copy everything that's left over */
1365 int reorderCode = 0;
1366 for (int i = 0; i < 256; i++) {
1367 if (!permutationSlotFilled[i]) {
1368 while (reorderCode < 256 && newLeadByteUsed[reorderCode]) {
1369 reorderCode++;
1370 }
1371 coll->leadBytePermutationTable[i] = reorderCode;
1372 permutationSlotFilled[i] = true;
1373 newLeadByteUsed[reorderCode] = true;
1374 }
1375 }
1376
1377 #ifdef REORDER_DEBUG
1378 fprintf(stdout, "\n@@@@ Script Reordering Table\n");
1379 for (int i = 0; i < 256; i++) {
1380 fprintf(stdout, "\t%02x = %02x\n", i, coll->leadBytePermutationTable[i]);
1381 }
1382 #endif
1383
1384 if (internalReorderCodes != NULL) {
1385 uprv_free(internalReorderCodes);
1386 }
1387
1388 // force a regen of the latin one table since it is affected by the script reordering
1389 coll->latinOneRegenTable = TRUE;
1390 ucol_updateInternalState(coll, status);
1391 }
1392
1393 #endif /* #if !UCONFIG_NO_COLLATION */