• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *******************************************************************************
3 *   Copyright (C) 2004-2006, International Business Machines
4 *   Corporation and others.  All Rights Reserved.
5 *******************************************************************************
6 *   file name:  ucol_sit.cpp
7 *   encoding:   US-ASCII
8 *   tab size:   8 (not used)
9 *   indentation:4
10 *
11 * Modification history
12 * Date        Name      Comments
13 * 03/12/2004  weiv      Creation
14 */
15 
16 #include "unicode/ustring.h"
17 
18 #include "utracimp.h"
19 #include "ucol_imp.h"
20 #include "ucol_tok.h"
21 #include "unormimp.h"
22 #include "cmemory.h"
23 #include "cstring.h"
24 
25 #if !UCONFIG_NO_COLLATION
26 
27 enum OptionsList {
28     UCOL_SIT_LANGUAGE = 0,
29     UCOL_SIT_SCRIPT,
30     UCOL_SIT_REGION,
31     UCOL_SIT_VARIANT,
32     UCOL_SIT_KEYWORD,
33     UCOL_SIT_RFC3166BIS,
34     UCOL_SIT_STRENGTH,
35     UCOL_SIT_CASE_LEVEL,
36     UCOL_SIT_CASE_FIRST,
37     UCOL_SIT_NUMERIC_COLLATION,
38     UCOL_SIT_ALTERNATE_HANDLING,
39     UCOL_SIT_NORMALIZATION_MODE,
40     UCOL_SIT_FRENCH_COLLATION,
41     UCOL_SIT_HIRAGANA_QUATERNARY,
42     UCOL_SIT_VARIABLE_TOP,
43     UCOL_SIT_VARIABLE_TOP_VALUE,
44     UCOL_SIT_ITEMS_COUNT
45 };
46 
47 /* list of locales for packing of a collator to an integer.
48  * This list corresponds to ICU 3.0. If more collation bearing
49  * locales are added in the future, this won't be a simple array
50  * but a mapping allowing forward and reverse lookup would have to
51  * be established. Currently, the mapping is from locale name to
52  * index.
53  */
54 static const char* const locales[] = {
55 /* 00 - 09 */ "ar", "be", "bg", "ca", "cs", "da", "de", "de__PHONEBOOK", "el", "en",
56 /* 10 - 19 */ "en_BE", "eo", "es", "es__TRADITIONAL", "et", "fa", "fa_AF", "fi", "fo", "fr",
57 /* 20 - 29 */ "gu", "he", "hi", "hi__DIRECT", "hr", "hu", "is", "it", "ja", "kk",
58 /* 30 - 39 */ "kl", "kn", "ko", "lt", "lv", "mk", "mr", "mt", "nb", "nn",
59 /* 40 - 49 */ "om", "pa", "pl", "ps", "ro", "root", "ru", "sh", "sk", "sl",
60 /* 50 - 59 */ "sq", "sr", "sv", "ta", "te", "th", "tr", "uk", "vi", "zh",
61 /* 60 - 64 */ "zh_HK", "zh_MO", "zh_TW", "zh_TW_STROKE", "zh__PINYIN"
62 };
63 
64 static const char* const keywords[] = {
65 /* 00 */ "",
66 /* 01 */ "direct",
67 /* 02 */ "phonebook",
68 /* 03 */ "pinyin",
69 /* 04 */ "standard",
70 /* 05 */ "stroke",
71 /* 06 */ "traditional"
72 };
73 
74 
75 /* option starters chars. */
76 static const char alternateHArg     = 'A';
77 static const char variableTopValArg = 'B';
78 static const char caseFirstArg      = 'C';
79 static const char numericCollArg    = 'D';
80 static const char caseLevelArg      = 'E';
81 static const char frenchCollArg     = 'F';
82 static const char hiraganaQArg      = 'H';
83 static const char keywordArg        = 'K';
84 static const char languageArg       = 'L';
85 static const char normArg           = 'N';
86 static const char regionArg         = 'R';
87 static const char strengthArg       = 'S';
88 static const char variableTopArg    = 'T';
89 static const char variantArg        = 'V';
90 static const char RFC3066Arg        = 'X';
91 static const char scriptArg         = 'Z';
92 
93 static const char collationKeyword[]  = "@collation=";
94 
95 static const int32_t locElementCount = 5;
96 static const int32_t locElementCapacity = 32;
97 static const int32_t loc3066Capacity = 256;
98 static const int32_t internalBufferSize = 512;
99 
100 /* structure containing specification of a collator. Initialized
101  * from a short string. Also used to construct a short string from a
102  * collator instance
103  */
104 struct CollatorSpec {
105     char locElements[locElementCount][locElementCapacity];
106     char locale[loc3066Capacity];
107     UColAttributeValue options[UCOL_ATTRIBUTE_COUNT];
108     uint32_t variableTopValue;
109     UChar variableTopString[locElementCapacity];
110     int32_t variableTopStringLen;
111     UBool variableTopSet;
112     struct {
113         const char *start;
114         int32_t len;
115     } entries[UCOL_SIT_ITEMS_COUNT];
116 };
117 
118 
119 /* structure for converting between character attribute
120  * representation and real collation attribute value.
121  */
122 struct AttributeConversion {
123     char letter;
124     UColAttributeValue value;
125 };
126 
127 static const AttributeConversion conversions[12] = {
128     { '1', UCOL_PRIMARY },
129     { '2', UCOL_SECONDARY },
130     { '3', UCOL_TERTIARY },
131     { '4', UCOL_QUATERNARY },
132     { 'D', UCOL_DEFAULT },
133     { 'I', UCOL_IDENTICAL },
134     { 'L', UCOL_LOWER_FIRST },
135     { 'N', UCOL_NON_IGNORABLE },
136     { 'O', UCOL_ON },
137     { 'S', UCOL_SHIFTED },
138     { 'U', UCOL_UPPER_FIRST },
139     { 'X', UCOL_OFF }
140 };
141 
142 
143 static char
ucol_sit_attributeValueToLetter(UColAttributeValue value,UErrorCode * status)144 ucol_sit_attributeValueToLetter(UColAttributeValue value, UErrorCode *status) {
145     uint32_t i = 0;
146     for(i = 0; i < sizeof(conversions)/sizeof(conversions[0]); i++) {
147         if(conversions[i].value == value) {
148             return conversions[i].letter;
149         }
150     }
151     *status = U_ILLEGAL_ARGUMENT_ERROR;
152     return 0;
153 }
154 
155 static UColAttributeValue
ucol_sit_letterToAttributeValue(char letter,UErrorCode * status)156 ucol_sit_letterToAttributeValue(char letter, UErrorCode *status) {
157     uint32_t i = 0;
158     for(i = 0; i < sizeof(conversions)/sizeof(conversions[0]); i++) {
159         if(conversions[i].letter == letter) {
160             return conversions[i].value;
161         }
162     }
163     *status = U_ILLEGAL_ARGUMENT_ERROR;
164     return UCOL_DEFAULT;
165 }
166 
167 /* function prototype for functions used to parse a short string */
168 U_CDECL_BEGIN
169 typedef const char* U_CALLCONV
170 ActionFunction(CollatorSpec *spec, uint32_t value1, const char* string,
171                UErrorCode *status);
172 U_CDECL_END
173 
174 U_CDECL_BEGIN
175 static const char* U_CALLCONV
_processLocaleElement(CollatorSpec * spec,uint32_t value,const char * string,UErrorCode * status)176 _processLocaleElement(CollatorSpec *spec, uint32_t value, const char* string,
177                       UErrorCode *status)
178 {
179     int32_t len = 0;
180     do {
181         if(value == 0 || value == 4) {
182             spec->locElements[value][len++] = uprv_tolower(*string);
183         } else {
184             spec->locElements[value][len++] = *string;
185         }
186     } while(*(++string) != '_' && *string && len < locElementCapacity);
187     if(len >= locElementCapacity) {
188         *status = U_BUFFER_OVERFLOW_ERROR;
189         return string;
190     }
191     // don't skip the underscore at the end
192     return string;
193 }
194 U_CDECL_END
195 
196 U_CDECL_BEGIN
197 static const char* U_CALLCONV
_processRFC3066Locale(CollatorSpec * spec,uint32_t,const char * string,UErrorCode * status)198 _processRFC3066Locale(CollatorSpec *spec, uint32_t, const char* string,
199                       UErrorCode *status)
200 {
201     char terminator = *string;
202     string++;
203     const char *end = uprv_strchr(string+1, terminator);
204     if(end == NULL || end - string >= loc3066Capacity) {
205         *status = U_BUFFER_OVERFLOW_ERROR;
206         return string;
207     } else {
208         uprv_strncpy(spec->locale, string, end-string);
209         return end+1;
210     }
211 }
212 
213 U_CDECL_END
214 
215 U_CDECL_BEGIN
216 static const char* U_CALLCONV
_processCollatorOption(CollatorSpec * spec,uint32_t option,const char * string,UErrorCode * status)217 _processCollatorOption(CollatorSpec *spec, uint32_t option, const char* string,
218                        UErrorCode *status)
219 {
220     spec->options[option] = ucol_sit_letterToAttributeValue(*string, status);
221     if((*(++string) != '_' && *string) || U_FAILURE(*status)) {
222         *status = U_ILLEGAL_ARGUMENT_ERROR;
223     }
224     return string;
225 }
226 U_CDECL_END
227 
228 
229 static UChar
readHexCodeUnit(const char ** string,UErrorCode * status)230 readHexCodeUnit(const char **string, UErrorCode *status)
231 {
232     UChar result = 0;
233     int32_t value = 0;
234     char c;
235     int32_t noDigits = 0;
236     while((c = **string) != 0 && noDigits < 4) {
237         if( c >= '0' && c <= '9') {
238             value = c - '0';
239         } else if ( c >= 'a' && c <= 'f') {
240             value = c - 'a' + 10;
241         } else if ( c >= 'A' && c <= 'F') {
242             value = c - 'A' + 10;
243         } else {
244             *status = U_ILLEGAL_ARGUMENT_ERROR;
245             return 0;
246         }
247         result = (result << 4) | (UChar)value;
248         noDigits++;
249         (*string)++;
250     }
251     // if the string was terminated before we read 4 digits, set an error
252     if(noDigits < 4) {
253         *status = U_ILLEGAL_ARGUMENT_ERROR;
254     }
255     return result;
256 }
257 
258 U_CDECL_BEGIN
259 static const char* U_CALLCONV
_processVariableTop(CollatorSpec * spec,uint32_t value1,const char * string,UErrorCode * status)260 _processVariableTop(CollatorSpec *spec, uint32_t value1, const char* string, UErrorCode *status)
261 {
262     // get four digits
263     int32_t i = 0;
264     if(!value1) {
265         while(U_SUCCESS(*status) && i < locElementCapacity && *string != 0 && *string != '_') {
266             spec->variableTopString[i++] = readHexCodeUnit(&string, status);
267         }
268         spec->variableTopStringLen = i;
269         if(i == locElementCapacity && (*string != 0 || *string != '_')) {
270             *status = U_BUFFER_OVERFLOW_ERROR;
271         }
272     } else {
273         spec->variableTopValue = readHexCodeUnit(&string, status);
274     }
275     if(U_SUCCESS(*status)) {
276         spec->variableTopSet = TRUE;
277     }
278     return string;
279 }
280 U_CDECL_END
281 
282 
283 /* Table for parsing short strings */
284 struct ShortStringOptions {
285     char optionStart;
286     ActionFunction *action;
287     uint32_t attr;
288 };
289 
290 static const ShortStringOptions options[UCOL_SIT_ITEMS_COUNT] =
291 {
292 /* 10 ALTERNATE_HANDLING */   {alternateHArg,     _processCollatorOption, UCOL_ALTERNATE_HANDLING }, // alternate  N, S, D
293 /* 15 VARIABLE_TOP_VALUE */   {variableTopValArg, _processVariableTop,    1 },
294 /* 08 CASE_FIRST */           {caseFirstArg,      _processCollatorOption, UCOL_CASE_FIRST }, // case first L, U, X, D
295 /* 09 NUMERIC_COLLATION */    {numericCollArg,    _processCollatorOption, UCOL_NUMERIC_COLLATION }, // codan      O, X, D
296 /* 07 CASE_LEVEL */           {caseLevelArg,      _processCollatorOption, UCOL_CASE_LEVEL }, // case level O, X, D
297 /* 12 FRENCH_COLLATION */     {frenchCollArg,     _processCollatorOption, UCOL_FRENCH_COLLATION }, // french     O, X, D
298 /* 13 HIRAGANA_QUATERNARY] */ {hiraganaQArg,      _processCollatorOption, UCOL_HIRAGANA_QUATERNARY_MODE }, // hiragana   O, X, D
299 /* 04 KEYWORD */              {keywordArg,        _processLocaleElement,  4 }, // keyword
300 /* 00 LANGUAGE */             {languageArg,       _processLocaleElement,  0 }, // language
301 /* 11 NORMALIZATION_MODE */   {normArg,           _processCollatorOption, UCOL_NORMALIZATION_MODE }, // norm       O, X, D
302 /* 02 REGION */               {regionArg,         _processLocaleElement,  2 }, // region
303 /* 06 STRENGTH */             {strengthArg,       _processCollatorOption, UCOL_STRENGTH }, // strength   1, 2, 3, 4, I, D
304 /* 14 VARIABLE_TOP */         {variableTopArg,    _processVariableTop,    0 },
305 /* 03 VARIANT */              {variantArg,        _processLocaleElement,  3 }, // variant
306 /* 05 RFC3066BIS */           {RFC3066Arg,        _processRFC3066Locale,  0 }, // rfc3066bis locale name
307 /* 01 SCRIPT */               {scriptArg,         _processLocaleElement,  1 }  // script
308 };
309 
310 
311 static
ucol_sit_readOption(const char * start,CollatorSpec * spec,UErrorCode * status)312 const char* ucol_sit_readOption(const char *start, CollatorSpec *spec,
313                             UErrorCode *status)
314 {
315   int32_t i = 0;
316 
317   for(i = 0; i < UCOL_SIT_ITEMS_COUNT; i++) {
318       if(*start == options[i].optionStart) {
319           spec->entries[i].start = start;
320           const char* end = options[i].action(spec, options[i].attr, start+1, status);
321           spec->entries[i].len = end - start;
322           return end;
323       }
324   }
325   *status = U_ILLEGAL_ARGUMENT_ERROR;
326   return start;
327 }
328 
329 static
ucol_sit_initCollatorSpecs(CollatorSpec * spec)330 void ucol_sit_initCollatorSpecs(CollatorSpec *spec)
331 {
332     // reset everything
333     uprv_memset(spec, 0, sizeof(CollatorSpec));
334     // set collation options to default
335     int32_t i = 0;
336     for(i = 0; i < UCOL_ATTRIBUTE_COUNT; i++) {
337         spec->options[i] = UCOL_DEFAULT;
338     }
339 }
340 
341 static const char*
ucol_sit_readSpecs(CollatorSpec * s,const char * string,UParseError * parseError,UErrorCode * status)342 ucol_sit_readSpecs(CollatorSpec *s, const char *string,
343                         UParseError *parseError, UErrorCode *status)
344 {
345     const char *definition = string;
346     while(U_SUCCESS(*status) && *string) {
347         string = ucol_sit_readOption(string, s, status);
348         // advance over '_'
349         while(*string && *string == '_') {
350             string++;
351         }
352     }
353     if(U_FAILURE(*status)) {
354         parseError->offset = string - definition;
355     }
356     return string;
357 }
358 
359 static
ucol_sit_dumpSpecs(CollatorSpec * s,char * destination,int32_t capacity,UErrorCode * status)360 int32_t ucol_sit_dumpSpecs(CollatorSpec *s, char *destination, int32_t capacity, UErrorCode *status)
361 {
362     int32_t i = 0, j = 0;
363     int32_t len = 0;
364     char optName;
365     if(U_SUCCESS(*status)) {
366         for(i = 0; i < UCOL_SIT_ITEMS_COUNT; i++) {
367             if(s->entries[i].start) {
368                 if(len) {
369                     if(len < capacity) {
370                         uprv_strcat(destination, "_");
371                     }
372                     len++;
373                 }
374                 optName = *(s->entries[i].start);
375                 if(optName == languageArg || optName == regionArg || optName == variantArg || optName == keywordArg) {
376                     for(j = 0; j < s->entries[i].len; j++) {
377                         if(len + j < capacity) {
378                             destination[len+j] = uprv_toupper(*(s->entries[i].start+j));
379                         }
380                     }
381                     len += s->entries[i].len;
382                 } else {
383                     len += s->entries[i].len;
384                     if(len < capacity) {
385                         uprv_strncat(destination,s->entries[i].start, s->entries[i].len);
386                     }
387                 }
388             }
389         }
390         return len;
391     } else {
392         return 0;
393     }
394 }
395 
396 static void
ucol_sit_calculateWholeLocale(CollatorSpec * s)397 ucol_sit_calculateWholeLocale(CollatorSpec *s) {
398     // put the locale together, unless we have a done
399     // locale
400     if(s->locale[0] == 0) {
401         // first the language
402         uprv_strcat(s->locale, s->locElements[0]);
403         // then the script, if present
404         if(*(s->locElements[1])) {
405             uprv_strcat(s->locale, "_");
406             uprv_strcat(s->locale, s->locElements[1]);
407         }
408         // then the region, if present
409         if(*(s->locElements[2])) {
410             uprv_strcat(s->locale, "_");
411             uprv_strcat(s->locale, s->locElements[2]);
412         } else if(*(s->locElements[3])) { // if there is a variant, we need an underscore
413             uprv_strcat(s->locale, "_");
414         }
415         // add variant, if there
416         if(*(s->locElements[3])) {
417             uprv_strcat(s->locale, "_");
418             uprv_strcat(s->locale, s->locElements[3]);
419         }
420 
421         // if there is a collation keyword, add that too
422         if(*(s->locElements[4])) {
423             uprv_strcat(s->locale, collationKeyword);
424             uprv_strcat(s->locale, s->locElements[4]);
425         }
426     }
427 }
428 
429 
430 U_CAPI void U_EXPORT2
ucol_prepareShortStringOpen(const char * definition,UBool,UParseError * parseError,UErrorCode * status)431 ucol_prepareShortStringOpen( const char *definition,
432                           UBool,
433                           UParseError *parseError,
434                           UErrorCode *status)
435 {
436     if(U_FAILURE(*status)) return;
437 
438     UParseError internalParseError;
439 
440     if(!parseError) {
441         parseError = &internalParseError;
442     }
443     parseError->line = 0;
444     parseError->offset = 0;
445     parseError->preContext[0] = 0;
446     parseError->postContext[0] = 0;
447 
448 
449     // first we want to pick stuff out of short string.
450     // we'll end up with an UCA version, locale and a bunch of
451     // settings
452 
453     // analyse the string in order to get everything we need.
454     CollatorSpec s;
455     ucol_sit_initCollatorSpecs(&s);
456     ucol_sit_readSpecs(&s, definition, parseError, status);
457     ucol_sit_calculateWholeLocale(&s);
458 
459     char buffer[internalBufferSize];
460     uprv_memset(buffer, 0, internalBufferSize);
461     uloc_canonicalize(s.locale, buffer, internalBufferSize, status);
462 
463     UResourceBundle *b = ures_open(U_ICUDATA_COLL, buffer, status);
464     /* we try to find stuff from keyword */
465     UResourceBundle *collations = ures_getByKey(b, "collations", NULL, status);
466     UResourceBundle *collElem = NULL;
467     char keyBuffer[256];
468     // if there is a keyword, we pick it up and try to get elements
469     if(!uloc_getKeywordValue(buffer, "collation", keyBuffer, 256, status)) {
470       // no keyword. we try to find the default setting, which will give us the keyword value
471       UResourceBundle *defaultColl = ures_getByKeyWithFallback(collations, "default", NULL, status);
472       if(U_SUCCESS(*status)) {
473         int32_t defaultKeyLen = 0;
474         const UChar *defaultKey = ures_getString(defaultColl, &defaultKeyLen, status);
475         u_UCharsToChars(defaultKey, keyBuffer, defaultKeyLen);
476         keyBuffer[defaultKeyLen] = 0;
477       } else {
478         *status = U_INTERNAL_PROGRAM_ERROR;
479         return;
480       }
481       ures_close(defaultColl);
482     }
483     collElem = ures_getByKeyWithFallback(collations, keyBuffer, collElem, status);
484     ures_close(collElem);
485     ures_close(collations);
486     ures_close(b);
487 }
488 
489 
490 U_CAPI UCollator* U_EXPORT2
ucol_openFromShortString(const char * definition,UBool forceDefaults,UParseError * parseError,UErrorCode * status)491 ucol_openFromShortString( const char *definition,
492                           UBool forceDefaults,
493                           UParseError *parseError,
494                           UErrorCode *status)
495 {
496     UTRACE_ENTRY_OC(UTRACE_UCOL_OPEN_FROM_SHORT_STRING);
497     UTRACE_DATA1(UTRACE_INFO, "short string = \"%s\"", definition);
498 
499     if(U_FAILURE(*status)) return 0;
500 
501     UParseError internalParseError;
502 
503     if(!parseError) {
504         parseError = &internalParseError;
505     }
506     parseError->line = 0;
507     parseError->offset = 0;
508     parseError->preContext[0] = 0;
509     parseError->postContext[0] = 0;
510 
511 
512     // first we want to pick stuff out of short string.
513     // we'll end up with an UCA version, locale and a bunch of
514     // settings
515 
516     // analyse the string in order to get everything we need.
517     const char *string = definition;
518     CollatorSpec s;
519     ucol_sit_initCollatorSpecs(&s);
520     string = ucol_sit_readSpecs(&s, definition, parseError, status);
521     ucol_sit_calculateWholeLocale(&s);
522 
523     char buffer[internalBufferSize];
524     uprv_memset(buffer, 0, internalBufferSize);
525     uloc_canonicalize(s.locale, buffer, internalBufferSize, status);
526 
527     UCollator *result = ucol_open(buffer, status);
528     int32_t i = 0;
529 
530     for(i = 0; i < UCOL_ATTRIBUTE_COUNT; i++) {
531         if(s.options[i] != UCOL_DEFAULT) {
532             if(forceDefaults || ucol_getAttribute(result, (UColAttribute)i, status) != s.options[i]) {
533                 ucol_setAttribute(result, (UColAttribute)i, s.options[i], status);
534             }
535 
536             if(U_FAILURE(*status)) {
537                 parseError->offset = string - definition;
538                 ucol_close(result);
539                 return NULL;
540             }
541 
542         }
543     }
544     if(s.variableTopSet) {
545         if(s.variableTopString[0]) {
546             ucol_setVariableTop(result, s.variableTopString, s.variableTopStringLen, status);
547         } else { // we set by value, using 'B'
548             ucol_restoreVariableTop(result, s.variableTopValue, status);
549         }
550     }
551 
552 
553     if(U_FAILURE(*status)) { // here it can only be a bogus value
554         ucol_close(result);
555         result = NULL;
556     }
557 
558     UTRACE_EXIT_PTR_STATUS(result, *status);
559     return result;
560 }
561 
562 
appendShortStringElement(const char * src,int32_t len,char * result,int32_t * resultSize,int32_t capacity,char arg)563 static void appendShortStringElement(const char *src, int32_t len, char *result, int32_t *resultSize, int32_t capacity, char arg)
564 {
565     if(len) {
566         if(*resultSize) {
567             if(*resultSize < capacity) {
568                 uprv_strcat(result, "_");
569             }
570             (*resultSize)++;
571         }
572         *resultSize += len + 1;
573         if(*resultSize < capacity) {
574             uprv_strncat(result, &arg, 1);
575             uprv_strncat(result, src, len);
576         }
577     }
578 }
579 
580 U_CAPI int32_t U_EXPORT2
ucol_getShortDefinitionString(const UCollator * coll,const char * locale,char * dst,int32_t capacity,UErrorCode * status)581 ucol_getShortDefinitionString(const UCollator *coll,
582                               const char *locale,
583                               char *dst,
584                               int32_t capacity,
585                               UErrorCode *status)
586 {
587     if(U_FAILURE(*status)) return 0;
588     char buffer[internalBufferSize];
589     uprv_memset(buffer, 0, internalBufferSize*sizeof(char));
590     int32_t resultSize = 0;
591     char tempbuff[internalBufferSize];
592     char locBuff[internalBufferSize];
593     uprv_memset(buffer, 0, internalBufferSize*sizeof(char));
594     int32_t elementSize = 0;
595     UBool isAvailable = 0;
596     CollatorSpec s;
597     ucol_sit_initCollatorSpecs(&s);
598 
599     if(!locale) {
600         locale = ucol_getLocale(coll, ULOC_VALID_LOCALE, status);
601     }
602     elementSize = ucol_getFunctionalEquivalent(locBuff, internalBufferSize, "collation", locale, &isAvailable, status);
603 
604     if(elementSize) {
605         // we should probably canonicalize here...
606         elementSize = uloc_getLanguage(locBuff, tempbuff, internalBufferSize, status);
607         appendShortStringElement(tempbuff, elementSize, buffer, &resultSize, capacity, languageArg);
608         elementSize = uloc_getCountry(locBuff, tempbuff, internalBufferSize, status);
609         appendShortStringElement(tempbuff, elementSize, buffer, &resultSize, capacity, regionArg);
610         elementSize = uloc_getScript(locBuff, tempbuff, internalBufferSize, status);
611         appendShortStringElement(tempbuff, elementSize, buffer, &resultSize, capacity, scriptArg);
612         elementSize = uloc_getVariant(locBuff, tempbuff, internalBufferSize, status);
613         appendShortStringElement(tempbuff, elementSize, buffer, &resultSize, capacity, variantArg);
614         elementSize = uloc_getKeywordValue(locBuff, "collation", tempbuff, internalBufferSize, status);
615         appendShortStringElement(tempbuff, elementSize, buffer, &resultSize, capacity, keywordArg);
616     }
617 
618     int32_t i = 0;
619     UColAttributeValue attribute = UCOL_DEFAULT;
620     for(i = 0; i < UCOL_SIT_ITEMS_COUNT; i++) {
621         if(options[i].action == _processCollatorOption) {
622             attribute = ucol_getAttributeOrDefault(coll, (UColAttribute)options[i].attr, status);
623             if(attribute != UCOL_DEFAULT) {
624                 char letter = ucol_sit_attributeValueToLetter(attribute, status);
625                 appendShortStringElement(&letter, 1,
626                     buffer, &resultSize, capacity, options[i].optionStart);
627             }
628         }
629     }
630     if(coll->variableTopValueisDefault == FALSE) {
631         //s.variableTopValue = ucol_getVariableTop(coll, status);
632         elementSize = T_CString_integerToString(tempbuff, coll->variableTopValue, 16);
633         appendShortStringElement(tempbuff, elementSize, buffer, &resultSize, capacity, variableTopValArg);
634     }
635 
636     UParseError parseError;
637     return ucol_normalizeShortDefinitionString(buffer, dst, capacity, &parseError, status);
638 }
639 
640 U_CAPI int32_t U_EXPORT2
ucol_normalizeShortDefinitionString(const char * definition,char * destination,int32_t capacity,UParseError * parseError,UErrorCode * status)641 ucol_normalizeShortDefinitionString(const char *definition,
642                                     char *destination,
643                                     int32_t capacity,
644                                     UParseError *parseError,
645                                     UErrorCode *status)
646 {
647 
648     if(U_FAILURE(*status)) {
649         return 0;
650     }
651 
652     if(destination) {
653         uprv_memset(destination, 0, capacity*sizeof(char));
654     }
655 
656     UParseError pe;
657     if(!parseError) {
658         parseError = &pe;
659     }
660 
661     // validate
662     CollatorSpec s;
663     ucol_sit_initCollatorSpecs(&s);
664     ucol_sit_readSpecs(&s, definition, parseError, status);
665     return ucol_sit_dumpSpecs(&s, destination, capacity, status);
666 }
667 
668 // structure for packing the bits of the attributes in the
669 // identifier number.
670 // locale is packed separately
671 struct bitPacking {
672     char letter;
673     uint32_t offset;
674     uint32_t width;
675     UColAttribute attribute;
676     UColAttributeValue values[6];
677 };
678 
679 static const bitPacking attributesToBits[UCOL_ATTRIBUTE_COUNT] = {
680     /* french */        { frenchCollArg,    29, 2, UCOL_FRENCH_COLLATION,         { UCOL_DEFAULT, UCOL_OFF, UCOL_ON }},
681     /* alternate */     { alternateHArg,    27, 2, UCOL_ALTERNATE_HANDLING,       { UCOL_DEFAULT, UCOL_NON_IGNORABLE, UCOL_SHIFTED }},
682     /* case first */    { caseFirstArg,     25, 2, UCOL_CASE_FIRST,               { UCOL_DEFAULT, UCOL_OFF, UCOL_LOWER_FIRST, UCOL_UPPER_FIRST }},
683     /* case level */    { caseLevelArg,     23, 2, UCOL_CASE_LEVEL,               { UCOL_DEFAULT, UCOL_OFF, UCOL_ON }},
684     /* normalization */ { normArg,          21, 2, UCOL_NORMALIZATION_MODE,       { UCOL_DEFAULT, UCOL_OFF, UCOL_ON }},
685     /* strength */      { strengthArg,      18, 3, UCOL_STRENGTH,                 { UCOL_DEFAULT, UCOL_PRIMARY, UCOL_SECONDARY, UCOL_TERTIARY, UCOL_QUATERNARY, UCOL_IDENTICAL }},
686     /* hiragana */      { hiraganaQArg,     16, 2, UCOL_HIRAGANA_QUATERNARY_MODE, { UCOL_DEFAULT, UCOL_OFF, UCOL_ON }},
687     /* numeric coll */  { numericCollArg,   14, 2, UCOL_NUMERIC_COLLATION,        { UCOL_DEFAULT, UCOL_OFF, UCOL_ON }}
688 };
689 
690 static const uint32_t keywordShift =   9;
691 static const uint32_t keywordWidth =   5;
692 static const uint32_t localeShift =    0;
693 static const uint32_t localeWidth =    7;
694 
695 
ucol_sit_putLocaleInIdentifier(uint32_t result,const char * locale,UErrorCode * status)696 static uint32_t ucol_sit_putLocaleInIdentifier(uint32_t result, const char* locale, UErrorCode* status) {
697     char buffer[internalBufferSize], keywordBuffer[internalBufferSize],
698         baseName[internalBufferSize], localeBuffer[internalBufferSize];
699     int32_t len = 0, keywordLen = 0,
700         baseNameLen = 0, localeLen = 0;
701     uint32_t i = 0;
702     UBool isAvailable = FALSE;
703     if(locale) {
704         len = uloc_canonicalize(locale, buffer, internalBufferSize, status);
705         localeLen = ucol_getFunctionalEquivalent(localeBuffer, internalBufferSize, "collation", buffer, &isAvailable, status);
706         keywordLen = uloc_getKeywordValue(buffer, "collation", keywordBuffer, internalBufferSize, status);
707         baseNameLen = uloc_getBaseName(buffer, baseName, internalBufferSize, status);
708 
709         /*Binary search for the map entry for normal cases */
710 
711         uint32_t   low     = 0;
712         uint32_t   high    = sizeof(locales)/sizeof(locales[0]);
713         uint32_t   mid     = high;
714         uint32_t   oldmid  = 0;
715         int32_t    compVal = 0;
716 
717 
718         while (high > low)  /*binary search*/{
719 
720             mid = (high+low) >> 1; /*Finds median*/
721 
722             if (mid == oldmid)
723                 return UCOL_SIT_COLLATOR_NOT_ENCODABLE; // we didn't find it
724 
725             compVal = uprv_strcmp(baseName, locales[mid]);
726             if (compVal < 0){
727                 high = mid;
728             }
729             else if (compVal > 0){
730                 low = mid;
731             }
732             else /*we found it*/{
733                 break;
734             }
735             oldmid = mid;
736         }
737 
738         result |= (mid & ((1 << localeWidth) - 1)) << localeShift;
739     }
740 
741     if(keywordLen) {
742         for(i = 1; i < sizeof(keywords)/sizeof(keywords[0]); i++) {
743             if(uprv_strcmp(keywords[i], keywordBuffer) == 0) {
744                 result |= (i & ((1 << keywordWidth) - 1)) << keywordShift;
745                 break;
746             }
747         }
748     }
749     return result;
750 }
751 
752 U_CAPI uint32_t U_EXPORT2
ucol_collatorToIdentifier(const UCollator * coll,const char * locale,UErrorCode * status)753 ucol_collatorToIdentifier(const UCollator *coll,
754                           const char *locale,
755                           UErrorCode *status)
756 {
757     uint32_t result = 0;
758     uint32_t i = 0, j = 0;
759     UColAttributeValue attrValue = UCOL_DEFAULT;
760 
761     // if variable top is not default, we need to use strings
762     if(coll->variableTopValueisDefault != TRUE) {
763         return UCOL_SIT_COLLATOR_NOT_ENCODABLE;
764     }
765 
766     if(locale == NULL) {
767         locale = ucol_getLocale(coll, ULOC_VALID_LOCALE, status);
768     }
769 
770     result = ucol_sit_putLocaleInIdentifier(result, locale, status);
771 
772     for(i = 0; i < sizeof(attributesToBits)/sizeof(attributesToBits[0]); i++) {
773         attrValue = ucol_getAttributeOrDefault(coll, attributesToBits[i].attribute, status);
774         j = 0;
775         while(attributesToBits[i].values[j] != attrValue) {
776             j++;
777         }
778         result |= (j & ((1 << attributesToBits[i].width) - 1)) << attributesToBits[i].offset;
779     }
780 
781     return result;
782 }
783 
784 U_CAPI UCollator* U_EXPORT2
ucol_openFromIdentifier(uint32_t identifier,UBool forceDefaults,UErrorCode * status)785 ucol_openFromIdentifier(uint32_t identifier,
786                         UBool forceDefaults,
787                         UErrorCode *status)
788 {
789     uint32_t i = 0;
790     int32_t value = 0, keyword = 0;
791     char locale[internalBufferSize];
792 
793     value = (identifier >> localeShift) & ((1 << localeWidth) - 1);
794     keyword = (identifier >> keywordShift) & ((1 << keywordWidth) - 1);
795 
796     uprv_strcpy(locale, locales[value]);
797 
798     if(keyword) {
799         uprv_strcat(locale, collationKeyword);
800         uprv_strcat(locale, keywords[keyword]);
801     }
802 
803     UColAttributeValue attrValue = UCOL_DEFAULT;
804 
805     UCollator *result = ucol_open(locale, status);
806 
807     // variable top is not set in the identifier, so we can easily skip that on
808 
809     for(i = 0; i < sizeof(attributesToBits)/sizeof(attributesToBits[0]); i++) {
810         value = (identifier >> attributesToBits[i].offset) & ((1 << attributesToBits[i].width) - 1);
811         attrValue = attributesToBits[i].values[value];
812         // the collator is all default, so we will set only the values that will differ from
813         // the default values.
814         if(attrValue != UCOL_DEFAULT) {
815             if(forceDefaults ||
816                 ucol_getAttribute(result, attributesToBits[i].attribute, status) != attrValue) {
817                 ucol_setAttribute(result, attributesToBits[i].attribute, attrValue, status);
818             }
819         }
820     }
821 
822     return result;
823 }
824 
825 U_CAPI int32_t U_EXPORT2
ucol_identifierToShortString(uint32_t identifier,char * buffer,int32_t capacity,UBool forceDefaults,UErrorCode * status)826 ucol_identifierToShortString(uint32_t identifier,
827                              char *buffer,
828                              int32_t capacity,
829                              UBool forceDefaults,
830                              UErrorCode *status)
831 {
832     int32_t locIndex = (identifier >> localeShift) & ((1 << localeWidth) - 1);
833     int32_t keywordIndex = (identifier >> keywordShift) & ((1 << keywordWidth) - 1);
834     CollatorSpec s;
835     ucol_sit_initCollatorSpecs(&s);
836     uprv_strcpy(s.locale, locales[locIndex]);
837     if(keywordIndex) {
838         uprv_strcat(s.locale, collationKeyword);
839         uprv_strcat(s.locale, keywords[keywordIndex]);
840     }
841     UCollator *coll = ucol_openFromIdentifier(identifier, forceDefaults, status);
842     int32_t resultLen = ucol_getShortDefinitionString(coll, s.locale, buffer, capacity, status);
843     ucol_close(coll);
844     return resultLen;
845 
846 #if 0
847     // TODO: Crumy, crumy, crumy... Very hard to currently go algorithmically from
848     // identifier to short string. Do rethink
849     if(forceDefaults == FALSE) {
850         UCollator *coll = ucol_openFromIdentifier(identifier, FALSE, status);
851         int32_t resultLen = ucol_getShortDefinitionString(coll, s.locale, buffer, capacity, status);
852         ucol_close(coll);
853         return resultLen;
854     } else { // forceDefaults == TRUE
855         char letter;
856         UColAttributeValue value;
857         int32_t i = 0;
858         for(i = 0; i < sizeof(attributesToBits)/sizeof(attributesToBits[0]); i++) {
859             value = attributesToBits[i].values[(identifier >> attributesToBits[i].offset) & ((1 << attributesToBits[i].width) - 1)];
860             if(value != UCOL_DEFAULT) {
861                 uprv_strcat(buffer, "_");
862                 uprv_strncat(buffer, &attributesToBits[i].letter, 1);
863                 letter = ucol_sit_attributeValueToLetter(value, status);
864                 uprv_strncat(buffer, &letter, 1);
865             }
866         }
867         return ucol_sit_dumpSpecs(&s, buffer, capacity, status);
868     }
869 #endif
870 }
871 
872 U_CAPI uint32_t U_EXPORT2
ucol_shortStringToIdentifier(const char * definition,UBool forceDefaults,UErrorCode * status)873 ucol_shortStringToIdentifier(const char *definition,
874                              UBool forceDefaults,
875                              UErrorCode *status)
876 {
877     UParseError parseError;
878     CollatorSpec s;
879     uint32_t result = 0;
880     uint32_t i = 0, j = 0;
881     ucol_sit_initCollatorSpecs(&s);
882 
883     ucol_sit_readSpecs(&s, definition, &parseError, status);
884     ucol_sit_calculateWholeLocale(&s);
885 
886     char locBuffer[internalBufferSize];
887     UBool isAvailable = FALSE;
888     UColAttributeValue attrValue = UCOL_DEFAULT;
889 
890     ucol_getFunctionalEquivalent(locBuffer, internalBufferSize, "collation", s.locale, &isAvailable, status);
891 
892     if(forceDefaults == FALSE) {
893         UCollator *coll = ucol_openFromShortString(definition, FALSE, &parseError, status);
894         result = ucol_collatorToIdentifier(coll, locBuffer, status);
895         ucol_close(coll);
896     } else { // forceDefaults == TRUE
897         result = ucol_sit_putLocaleInIdentifier(result, locBuffer, status);
898 
899         for(i = 0; i < sizeof(attributesToBits)/sizeof(attributesToBits[0]); i++) {
900             attrValue = s.options[i];
901             j = 0;
902             while(attributesToBits[i].values[j] != attrValue) {
903                 j++;
904             }
905             result |= (j & ((1 << attributesToBits[i].width) - 1)) << attributesToBits[i].offset;
906         }
907 
908     }
909     return result;
910 
911 }
912 
913 U_CAPI UColAttributeValue  U_EXPORT2
ucol_getAttributeOrDefault(const UCollator * coll,UColAttribute attr,UErrorCode * status)914 ucol_getAttributeOrDefault(const UCollator *coll, UColAttribute attr, UErrorCode *status)
915 {
916     if(U_FAILURE(*status) || coll == NULL) {
917       return UCOL_DEFAULT;
918     }
919     switch(attr) {
920     case UCOL_NUMERIC_COLLATION:
921         return coll->numericCollationisDefault?UCOL_DEFAULT:coll->numericCollation;
922     case UCOL_HIRAGANA_QUATERNARY_MODE:
923         return coll->hiraganaQisDefault?UCOL_DEFAULT:coll->hiraganaQ;
924     case UCOL_FRENCH_COLLATION: /* attribute for direction of secondary weights*/
925         return coll->frenchCollationisDefault?UCOL_DEFAULT:coll->frenchCollation;
926     case UCOL_ALTERNATE_HANDLING: /* attribute for handling variable elements*/
927         return coll->alternateHandlingisDefault?UCOL_DEFAULT:coll->alternateHandling;
928     case UCOL_CASE_FIRST: /* who goes first, lower case or uppercase */
929         return coll->caseFirstisDefault?UCOL_DEFAULT:coll->caseFirst;
930     case UCOL_CASE_LEVEL: /* do we have an extra case level */
931         return coll->caseLevelisDefault?UCOL_DEFAULT:coll->caseLevel;
932     case UCOL_NORMALIZATION_MODE: /* attribute for normalization */
933         return coll->normalizationModeisDefault?UCOL_DEFAULT:coll->normalizationMode;
934     case UCOL_STRENGTH:         /* attribute for strength */
935         return coll->strengthisDefault?UCOL_DEFAULT:coll->strength;
936     case UCOL_ATTRIBUTE_COUNT:
937     default:
938         *status = U_ILLEGAL_ARGUMENT_ERROR;
939         break;
940     }
941     return UCOL_DEFAULT;
942 }
943 
944 
945 struct contContext {
946     const UCollator *coll;
947     USet            *conts;
948     USet            *expansions;
949     USet            *removedContractions;
950     UBool           addPrefixes;
951     UErrorCode      *status;
952 };
953 
954 
955 
956 static void
addSpecial(contContext * context,UChar * buffer,int32_t bufLen,uint32_t CE,int32_t leftIndex,int32_t rightIndex,UErrorCode * status)957 addSpecial(contContext *context, UChar *buffer, int32_t bufLen,
958                uint32_t CE, int32_t leftIndex, int32_t rightIndex, UErrorCode *status)
959 {
960   const UCollator *coll = context->coll;
961   USet *contractions = context->conts;
962   USet *expansions = context->expansions;
963   UBool addPrefixes = context->addPrefixes;
964 
965     const UChar *UCharOffset = (UChar *)coll->image+getContractOffset(CE);
966     uint32_t newCE = *(coll->contractionCEs + (UCharOffset - coll->contractionIndex));
967     // we might have a contraction that ends from previous level
968     if(newCE != UCOL_NOT_FOUND) {
969       if(isSpecial(CE) && getCETag(CE) == CONTRACTION_TAG && isSpecial(newCE) && getCETag(newCE) == SPEC_PROC_TAG && addPrefixes) {
970         addSpecial(context, buffer, bufLen, newCE, leftIndex, rightIndex, status);
971       }
972       if(contractions && rightIndex-leftIndex > 1) {
973             uset_addString(contractions, buffer+leftIndex, rightIndex-leftIndex);
974             if(expansions && isSpecial(CE) && getCETag(CE) == EXPANSION_TAG) {
975               uset_addString(expansions, buffer+leftIndex, rightIndex-leftIndex);
976             }
977       }
978     }
979 
980     UCharOffset++;
981     // check whether we're doing contraction or prefix
982     if(getCETag(CE) == SPEC_PROC_TAG && addPrefixes) {
983       if(leftIndex == 0) {
984           *status = U_INTERNAL_PROGRAM_ERROR;
985           return;
986       }
987       --leftIndex;
988       while(*UCharOffset != 0xFFFF) {
989           newCE = *(coll->contractionCEs + (UCharOffset - coll->contractionIndex));
990           buffer[leftIndex] = *UCharOffset;
991           if(isSpecial(newCE) && (getCETag(newCE) == CONTRACTION_TAG || getCETag(newCE) == SPEC_PROC_TAG)) {
992               addSpecial(context, buffer, bufLen, newCE, leftIndex, rightIndex, status);
993           } else {
994             if(contractions) {
995                 uset_addString(contractions, buffer+leftIndex, rightIndex-leftIndex);
996             }
997             if(expansions && isSpecial(newCE) && getCETag(newCE) == EXPANSION_TAG) {
998               uset_addString(expansions, buffer+leftIndex, rightIndex-leftIndex);
999             }
1000           }
1001           UCharOffset++;
1002       }
1003     } else if(getCETag(CE) == CONTRACTION_TAG) {
1004       if(rightIndex == bufLen-1) {
1005           *status = U_INTERNAL_PROGRAM_ERROR;
1006           return;
1007       }
1008       while(*UCharOffset != 0xFFFF) {
1009           newCE = *(coll->contractionCEs + (UCharOffset - coll->contractionIndex));
1010           buffer[rightIndex] = *UCharOffset;
1011           if(isSpecial(newCE) && (getCETag(newCE) == CONTRACTION_TAG || getCETag(newCE) == SPEC_PROC_TAG)) {
1012               addSpecial(context, buffer, bufLen, newCE, leftIndex, rightIndex+1, status);
1013           } else {
1014             if(contractions) {
1015               uset_addString(contractions, buffer+leftIndex, rightIndex+1-leftIndex);
1016             }
1017             if(expansions && isSpecial(newCE) && getCETag(newCE) == EXPANSION_TAG) {
1018               uset_addString(expansions, buffer+leftIndex, rightIndex+1-leftIndex);
1019             }
1020           }
1021           UCharOffset++;
1022       }
1023     }
1024 
1025 }
1026 
1027 U_CDECL_BEGIN
1028 static UBool U_CALLCONV
_processSpecials(const void * context,UChar32 start,UChar32 limit,uint32_t CE)1029 _processSpecials(const void *context, UChar32 start, UChar32 limit, uint32_t CE)
1030 {
1031     UErrorCode *status = ((contContext *)context)->status;
1032     USet *expansions = ((contContext *)context)->expansions;
1033     USet *removed = ((contContext *)context)->removedContractions;
1034     UBool addPrefixes = ((contContext *)context)->addPrefixes;
1035     UChar contraction[internalBufferSize];
1036     if(isSpecial(CE)) {
1037       if(((getCETag(CE) == SPEC_PROC_TAG && addPrefixes) || getCETag(CE) == CONTRACTION_TAG)) {
1038         while(start < limit && U_SUCCESS(*status)) {
1039             // if there are suppressed contractions, we don't
1040             // want to add them.
1041             if(removed && uset_contains(removed, start)) {
1042                 start++;
1043                 continue;
1044             }
1045             // we start our contraction from middle, since we don't know if it
1046             // will grow toward right or left
1047             contraction[internalBufferSize/2] = (UChar)start;
1048             addSpecial(((contContext *)context), contraction, internalBufferSize, CE, internalBufferSize/2, internalBufferSize/2+1, status);
1049             start++;
1050         }
1051       } else if(expansions && getCETag(CE) == EXPANSION_TAG) {
1052         while(start < limit && U_SUCCESS(*status)) {
1053           uset_add(expansions, start++);
1054         }
1055       }
1056     }
1057     if(U_FAILURE(*status)) {
1058         return FALSE;
1059     } else {
1060         return TRUE;
1061     }
1062 }
1063 
1064 U_CDECL_END
1065 
1066 
1067 
1068 /**
1069  * Get a set containing the contractions defined by the collator. The set includes
1070  * both the UCA contractions and the contractions defined by the collator
1071  * @param coll collator
1072  * @param conts the set to hold the result
1073  * @param status to hold the error code
1074  * @return the size of the contraction set
1075  *
1076  * @draft ICU 3.0
1077  */
1078 U_CAPI int32_t U_EXPORT2
ucol_getContractions(const UCollator * coll,USet * contractions,UErrorCode * status)1079 ucol_getContractions( const UCollator *coll,
1080                   USet *contractions,
1081                   UErrorCode *status)
1082 {
1083   ucol_getContractionsAndExpansions(coll, contractions, NULL, FALSE, status);
1084   return uset_getItemCount(contractions);
1085 }
1086 
1087 /**
1088  * Get a set containing the expansions defined by the collator. The set includes
1089  * both the UCA expansions and the expansions defined by the tailoring
1090  * @param coll collator
1091  * @param conts the set to hold the result
1092  * @param addPrefixes add the prefix contextual elements to contractions
1093  * @param status to hold the error code
1094  *
1095  * @draft ICU 3.4
1096  */
1097 U_CAPI void U_EXPORT2
ucol_getContractionsAndExpansions(const UCollator * coll,USet * contractions,USet * expansions,UBool addPrefixes,UErrorCode * status)1098 ucol_getContractionsAndExpansions( const UCollator *coll,
1099                   USet *contractions,
1100                   USet *expansions,
1101                   UBool addPrefixes,
1102                   UErrorCode *status)
1103 {
1104     if(U_FAILURE(*status)) {
1105         return;
1106     }
1107     if(coll == NULL) {
1108         *status = U_ILLEGAL_ARGUMENT_ERROR;
1109         return;
1110     }
1111 
1112     if(contractions) {
1113       uset_clear(contractions);
1114     }
1115     if(expansions) {
1116       uset_clear(expansions);
1117     }
1118     int32_t rulesLen = 0;
1119     const UChar* rules = ucol_getRules(coll, &rulesLen);
1120     UColTokenParser src;
1121     ucol_tok_initTokenList(&src, rules, rulesLen, coll->UCA, status);
1122 
1123     contContext c = { NULL, contractions, expansions, src.removeSet, addPrefixes, status };
1124 
1125     // Add the UCA contractions
1126     c.coll = coll->UCA;
1127     utrie_enum(&coll->UCA->mapping, NULL, _processSpecials, &c);
1128 
1129     // This is collator specific. Add contractions from a collator
1130     c.coll = coll;
1131     c.removedContractions =  NULL;
1132     utrie_enum(&coll->mapping, NULL, _processSpecials, &c);
1133     ucol_tok_closeTokenList(&src);
1134 }
1135 
1136 U_CAPI int32_t U_EXPORT2
ucol_getUnsafeSet(const UCollator * coll,USet * unsafe,UErrorCode * status)1137 ucol_getUnsafeSet( const UCollator *coll,
1138                   USet *unsafe,
1139                   UErrorCode *status)
1140 {
1141     UChar buffer[internalBufferSize];
1142     int32_t len = 0;
1143 
1144     uset_clear(unsafe);
1145 
1146     // cccpattern = "[[:^tccc=0:][:^lccc=0:]]", unfortunately variant
1147     static const UChar cccpattern[25] = { 0x5b, 0x5b, 0x3a, 0x5e, 0x74, 0x63, 0x63, 0x63, 0x3d, 0x30, 0x3a, 0x5d,
1148                                     0x5b, 0x3a, 0x5e, 0x6c, 0x63, 0x63, 0x63, 0x3d, 0x30, 0x3a, 0x5d, 0x5d, 0x00 };
1149 
1150     // add chars that fail the fcd check
1151     uset_applyPattern(unsafe, cccpattern, 24, USET_IGNORE_SPACE, status);
1152 
1153     // add Thai/Lao prevowels
1154     uset_addRange(unsafe, 0xe40, 0xe44);
1155     uset_addRange(unsafe, 0xec0, 0xec4);
1156     // add lead/trail surrogates
1157     uset_addRange(unsafe, 0xd800, 0xdfff);
1158 
1159     USet *contractions = uset_open(0,0);
1160 
1161     int32_t i = 0, j = 0;
1162     int32_t contsSize = ucol_getContractions(coll, contractions, status);
1163     UChar32 c = 0;
1164     // Contraction set consists only of strings
1165     // to get unsafe code points, we need to
1166     // break the strings apart and add them to the unsafe set
1167     for(i = 0; i < contsSize; i++) {
1168         len = uset_getItem(contractions, i, NULL, NULL, buffer, internalBufferSize, status);
1169         if(len > 0) {
1170             j = 0;
1171             while(j < len) {
1172                 U16_NEXT(buffer, j, len, c);
1173                 if(j < len) {
1174                     uset_add(unsafe, c);
1175                 }
1176             }
1177         }
1178     }
1179 
1180     uset_close(contractions);
1181 
1182     return uset_size(unsafe);
1183 }
1184 #endif
1185