2 *******************************************************************************
3 * Copyright (C) 2004-2016, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
6 * file name: ucol_sit.cpp
8 * tab size: 8 (not used)
11 * Modification history
13 * 03/12/2004 weiv Creation
16 #include "unicode/ustring.h"
17 #include "unicode/udata.h"
18 #include "unicode/utf16.h"
24 #include "unicode/coll.h"
30 #if !UCONFIG_NO_COLLATION
32 #include "unicode/tblcoll.h"
35 UCOL_SIT_LANGUAGE
= 0,
40 UCOL_SIT_PROVIDER
= 5,
41 UCOL_SIT_LOCELEMENT_MAX
= UCOL_SIT_PROVIDER
, /* the last element that's part of LocElements */
47 UCOL_SIT_NUMERIC_COLLATION
,
48 UCOL_SIT_ALTERNATE_HANDLING
,
49 UCOL_SIT_NORMALIZATION_MODE
,
50 UCOL_SIT_FRENCH_COLLATION
,
51 UCOL_SIT_HIRAGANA_QUATERNARY
,
52 UCOL_SIT_VARIABLE_TOP
,
53 UCOL_SIT_VARIABLE_TOP_VALUE
,
57 /* option starters chars. */
58 static const char alternateHArg
= 'A';
59 static const char variableTopValArg
= 'B';
60 static const char caseFirstArg
= 'C';
61 static const char numericCollArg
= 'D';
62 static const char caseLevelArg
= 'E';
63 static const char frenchCollArg
= 'F';
64 static const char hiraganaQArg
= 'H';
65 static const char keywordArg
= 'K';
66 static const char languageArg
= 'L';
67 static const char normArg
= 'N';
68 static const char providerArg
= 'P';
69 static const char regionArg
= 'R';
70 static const char strengthArg
= 'S';
71 static const char variableTopArg
= 'T';
72 static const char variantArg
= 'V';
73 static const char RFC3066Arg
= 'X';
74 static const char scriptArg
= 'Z';
76 static const char collationKeyword
[] = "@collation=";
77 static const char providerKeyword
[] = "@sp=";
80 static const int32_t locElementCount
= UCOL_SIT_LOCELEMENT_MAX
+1;
81 static const int32_t locElementCapacity
= 32;
82 static const int32_t loc3066Capacity
= 256;
83 static const int32_t locProviderCapacity
= 10;
84 static const int32_t internalBufferSize
= 512;
86 /* structure containing specification of a collator. Initialized
87 * from a short string. Also used to construct a short string from a
91 char locElements
[locElementCount
][locElementCapacity
];
92 char locale
[loc3066Capacity
];
93 char provider
[locProviderCapacity
];
94 UColAttributeValue options
[UCOL_ATTRIBUTE_COUNT
];
95 uint32_t variableTopValue
;
96 UChar variableTopString
[locElementCapacity
];
97 int32_t variableTopStringLen
;
102 } entries
[UCOL_SIT_ITEMS_COUNT
];
106 /* structure for converting between character attribute
107 * representation and real collation attribute value.
109 struct AttributeConversion
{
111 UColAttributeValue value
;
114 static const AttributeConversion conversions
[12] = {
115 { '1', UCOL_PRIMARY
},
116 { '2', UCOL_SECONDARY
},
117 { '3', UCOL_TERTIARY
},
118 { '4', UCOL_QUATERNARY
},
119 { 'D', UCOL_DEFAULT
},
120 { 'I', UCOL_IDENTICAL
},
121 { 'L', UCOL_LOWER_FIRST
},
122 { 'N', UCOL_NON_IGNORABLE
},
124 { 'S', UCOL_SHIFTED
},
125 { 'U', UCOL_UPPER_FIRST
},
130 static UColAttributeValue
131 ucol_sit_letterToAttributeValue(char letter
, UErrorCode
*status
) {
133 for(i
= 0; i
< UPRV_LENGTHOF(conversions
); i
++) {
134 if(conversions
[i
].letter
== letter
) {
135 return conversions
[i
].value
;
138 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
139 #ifdef UCOL_TRACE_SIT
140 fprintf(stderr
, "%s:%d: unknown letter %c: %s\n", __FILE__
, __LINE__
, letter
, u_errorName(*status
));
145 /* function prototype for functions used to parse a short string */
147 typedef const char* U_CALLCONV
148 ActionFunction(CollatorSpec
*spec
, uint32_t value1
, const char* string
,
153 static const char* U_CALLCONV
154 _processLocaleElement(CollatorSpec
*spec
, uint32_t value
, const char* string
,
159 if(value
== UCOL_SIT_LANGUAGE
|| value
== UCOL_SIT_KEYWORD
|| value
== UCOL_SIT_PROVIDER
) {
160 spec
->locElements
[value
][len
++] = uprv_tolower(*string
);
162 spec
->locElements
[value
][len
++] = *string
;
164 } while(*(++string
) != '_' && *string
&& len
< locElementCapacity
);
165 if(len
>= locElementCapacity
) {
166 *status
= U_BUFFER_OVERFLOW_ERROR
;
169 // don't skip the underscore at the end
175 static const char* U_CALLCONV
176 _processRFC3066Locale(CollatorSpec
*spec
, uint32_t, const char* string
,
179 char terminator
= *string
;
181 const char *end
= uprv_strchr(string
+1, terminator
);
182 if(end
== NULL
|| end
- string
>= loc3066Capacity
) {
183 *status
= U_BUFFER_OVERFLOW_ERROR
;
186 uprv_strncpy(spec
->locale
, string
, end
-string
);
194 static const char* U_CALLCONV
195 _processCollatorOption(CollatorSpec
*spec
, uint32_t option
, const char* string
,
198 spec
->options
[option
] = ucol_sit_letterToAttributeValue(*string
, status
);
199 if((*(++string
) != '_' && *string
) || U_FAILURE(*status
)) {
200 #ifdef UCOL_TRACE_SIT
201 fprintf(stderr
, "%s:%d: unknown collator option at '%s': %s\n", __FILE__
, __LINE__
, string
, u_errorName(*status
));
203 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
211 readHexCodeUnit(const char **string
, UErrorCode
*status
)
216 int32_t noDigits
= 0;
217 while((c
= **string
) != 0 && noDigits
< 4) {
218 if( c
>= '0' && c
<= '9') {
220 } else if ( c
>= 'a' && c
<= 'f') {
221 value
= c
- 'a' + 10;
222 } else if ( c
>= 'A' && c
<= 'F') {
223 value
= c
- 'A' + 10;
225 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
226 #ifdef UCOL_TRACE_SIT
227 fprintf(stderr
, "%s:%d: Bad hex char at '%s': %s\n", __FILE__
, __LINE__
, *string
, u_errorName(*status
));
231 result
= (result
<< 4) | (UChar
)value
;
235 // if the string was terminated before we read 4 digits, set an error
237 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
238 #ifdef UCOL_TRACE_SIT
239 fprintf(stderr
, "%s:%d: Short (only %d digits, wanted 4) at '%s': %s\n", __FILE__
, __LINE__
, noDigits
,*string
, u_errorName(*status
));
246 static const char* U_CALLCONV
247 _processVariableTop(CollatorSpec
*spec
, uint32_t value1
, const char* string
, UErrorCode
*status
)
252 while(U_SUCCESS(*status
) && i
< locElementCapacity
&& *string
!= 0 && *string
!= '_') {
253 spec
->variableTopString
[i
++] = readHexCodeUnit(&string
, status
);
255 spec
->variableTopStringLen
= i
;
256 if(i
== locElementCapacity
&& *string
!= 0 && *string
!= '_') {
257 *status
= U_BUFFER_OVERFLOW_ERROR
;
260 spec
->variableTopValue
= readHexCodeUnit(&string
, status
);
262 if(U_SUCCESS(*status
)) {
263 spec
->variableTopSet
= TRUE
;
270 /* Table for parsing short strings */
271 struct ShortStringOptions
{
273 ActionFunction
*action
;
277 static const ShortStringOptions options
[UCOL_SIT_ITEMS_COUNT
] =
279 /* 10 ALTERNATE_HANDLING */ {alternateHArg
, _processCollatorOption
, UCOL_ALTERNATE_HANDLING
}, // alternate N, S, D
280 /* 15 VARIABLE_TOP_VALUE */ {variableTopValArg
, _processVariableTop
, 1 },
281 /* 08 CASE_FIRST */ {caseFirstArg
, _processCollatorOption
, UCOL_CASE_FIRST
}, // case first L, U, X, D
282 /* 09 NUMERIC_COLLATION */ {numericCollArg
, _processCollatorOption
, UCOL_NUMERIC_COLLATION
}, // codan O, X, D
283 /* 07 CASE_LEVEL */ {caseLevelArg
, _processCollatorOption
, UCOL_CASE_LEVEL
}, // case level O, X, D
284 /* 12 FRENCH_COLLATION */ {frenchCollArg
, _processCollatorOption
, UCOL_FRENCH_COLLATION
}, // french O, X, D
285 /* 13 HIRAGANA_QUATERNARY] */ {hiraganaQArg
, _processCollatorOption
, UCOL_HIRAGANA_QUATERNARY_MODE
}, // hiragana O, X, D
286 /* 04 KEYWORD */ {keywordArg
, _processLocaleElement
, UCOL_SIT_KEYWORD
}, // keyword
287 /* 00 LANGUAGE */ {languageArg
, _processLocaleElement
, UCOL_SIT_LANGUAGE
}, // language
288 /* 11 NORMALIZATION_MODE */ {normArg
, _processCollatorOption
, UCOL_NORMALIZATION_MODE
}, // norm O, X, D
289 /* 02 REGION */ {regionArg
, _processLocaleElement
, UCOL_SIT_REGION
}, // region
290 /* 06 STRENGTH */ {strengthArg
, _processCollatorOption
, UCOL_STRENGTH
}, // strength 1, 2, 3, 4, I, D
291 /* 14 VARIABLE_TOP */ {variableTopArg
, _processVariableTop
, 0 },
292 /* 03 VARIANT */ {variantArg
, _processLocaleElement
, UCOL_SIT_VARIANT
}, // variant
293 /* 05 RFC3066BIS */ {RFC3066Arg
, _processRFC3066Locale
, 0 }, // rfc3066bis locale name
294 /* 01 SCRIPT */ {scriptArg
, _processLocaleElement
, UCOL_SIT_SCRIPT
}, // script
295 /* PROVIDER */ {providerArg
, _processLocaleElement
, UCOL_SIT_PROVIDER
}
300 const char* ucol_sit_readOption(const char *start
, CollatorSpec
*spec
,
305 for(i
= 0; i
< UCOL_SIT_ITEMS_COUNT
; i
++) {
306 if(*start
== options
[i
].optionStart
) {
307 spec
->entries
[i
].start
= start
;
308 const char* end
= options
[i
].action(spec
, options
[i
].attr
, start
+1, status
);
309 spec
->entries
[i
].len
= (int32_t)(end
- start
);
313 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
314 #ifdef UCOL_TRACE_SIT
315 fprintf(stderr
, "%s:%d: Unknown option at '%s': %s\n", __FILE__
, __LINE__
, start
, u_errorName(*status
));
321 void ucol_sit_initCollatorSpecs(CollatorSpec
*spec
)
324 uprv_memset(spec
, 0, sizeof(CollatorSpec
));
325 // set collation options to default
327 for(i
= 0; i
< UCOL_ATTRIBUTE_COUNT
; i
++) {
328 spec
->options
[i
] = UCOL_DEFAULT
;
333 ucol_sit_readSpecs(CollatorSpec
*s
, const char *string
,
334 UParseError
*parseError
, UErrorCode
*status
)
336 const char *definition
= string
;
337 while(U_SUCCESS(*status
) && *string
) {
338 string
= ucol_sit_readOption(string
, s
, status
);
340 while(*string
&& *string
== '_') {
344 if(U_FAILURE(*status
)) {
345 parseError
->offset
= (int32_t)(string
- definition
);
351 int32_t ucol_sit_dumpSpecs(CollatorSpec
*s
, char *destination
, int32_t capacity
, UErrorCode
*status
)
353 int32_t i
= 0, j
= 0;
356 if(U_SUCCESS(*status
)) {
357 for(i
= 0; i
< UCOL_SIT_ITEMS_COUNT
; i
++) {
358 if(s
->entries
[i
].start
) {
361 uprv_strcat(destination
, "_");
365 optName
= *(s
->entries
[i
].start
);
366 if(optName
== languageArg
|| optName
== regionArg
|| optName
== variantArg
|| optName
== keywordArg
) {
367 for(j
= 0; j
< s
->entries
[i
].len
; j
++) {
368 if(len
+ j
< capacity
) {
369 destination
[len
+j
] = uprv_toupper(*(s
->entries
[i
].start
+j
));
372 len
+= s
->entries
[i
].len
;
374 len
+= s
->entries
[i
].len
;
376 uprv_strncat(destination
,s
->entries
[i
].start
, s
->entries
[i
].len
);
388 ucol_sit_calculateWholeLocale(CollatorSpec
*s
) {
389 // put the locale together, unless we have a done
391 if(s
->locale
[0] == 0) {
392 // first the language
393 uprv_strcat(s
->locale
, s
->locElements
[UCOL_SIT_LANGUAGE
]);
394 // then the script, if present
395 if(*(s
->locElements
[UCOL_SIT_SCRIPT
])) {
396 uprv_strcat(s
->locale
, "_");
397 uprv_strcat(s
->locale
, s
->locElements
[UCOL_SIT_SCRIPT
]);
399 // then the region, if present
400 if(*(s
->locElements
[UCOL_SIT_REGION
])) {
401 uprv_strcat(s
->locale
, "_");
402 uprv_strcat(s
->locale
, s
->locElements
[UCOL_SIT_REGION
]);
403 } else if(*(s
->locElements
[UCOL_SIT_VARIANT
])) { // if there is a variant, we need an underscore
404 uprv_strcat(s
->locale
, "_");
406 // add variant, if there
407 if(*(s
->locElements
[UCOL_SIT_VARIANT
])) {
408 uprv_strcat(s
->locale
, "_");
409 uprv_strcat(s
->locale
, s
->locElements
[UCOL_SIT_VARIANT
]);
412 // if there is a collation keyword, add that too
413 if(*(s
->locElements
[UCOL_SIT_KEYWORD
])) {
414 uprv_strcat(s
->locale
, collationKeyword
);
415 uprv_strcat(s
->locale
, s
->locElements
[UCOL_SIT_KEYWORD
]);
418 // if there is a provider keyword, add that too
419 if(*(s
->locElements
[UCOL_SIT_PROVIDER
])) {
420 uprv_strcat(s
->locale
, providerKeyword
);
421 uprv_strcat(s
->locale
, s
->locElements
[UCOL_SIT_PROVIDER
]);
427 U_CAPI
void U_EXPORT2
428 ucol_prepareShortStringOpen( const char *definition
,
430 UParseError
*parseError
,
433 if(U_FAILURE(*status
)) return;
435 UParseError internalParseError
;
438 parseError
= &internalParseError
;
440 parseError
->line
= 0;
441 parseError
->offset
= 0;
442 parseError
->preContext
[0] = 0;
443 parseError
->postContext
[0] = 0;
446 // first we want to pick stuff out of short string.
447 // we'll end up with an UCA version, locale and a bunch of
450 // analyse the string in order to get everything we need.
452 ucol_sit_initCollatorSpecs(&s
);
453 ucol_sit_readSpecs(&s
, definition
, parseError
, status
);
454 ucol_sit_calculateWholeLocale(&s
);
456 char buffer
[internalBufferSize
];
457 uprv_memset(buffer
, 0, internalBufferSize
);
458 uloc_canonicalize(s
.locale
, buffer
, internalBufferSize
, status
);
460 UResourceBundle
*b
= ures_open(U_ICUDATA_COLL
, buffer
, status
);
461 /* we try to find stuff from keyword */
462 UResourceBundle
*collations
= ures_getByKey(b
, "collations", NULL
, status
);
463 UResourceBundle
*collElem
= NULL
;
465 // if there is a keyword, we pick it up and try to get elements
466 if(!uloc_getKeywordValue(buffer
, "collation", keyBuffer
, 256, status
)) {
467 // no keyword. we try to find the default setting, which will give us the keyword value
468 UResourceBundle
*defaultColl
= ures_getByKeyWithFallback(collations
, "default", NULL
, status
);
469 if(U_SUCCESS(*status
)) {
470 int32_t defaultKeyLen
= 0;
471 const UChar
*defaultKey
= ures_getString(defaultColl
, &defaultKeyLen
, status
);
472 u_UCharsToChars(defaultKey
, keyBuffer
, defaultKeyLen
);
473 keyBuffer
[defaultKeyLen
] = 0;
475 *status
= U_INTERNAL_PROGRAM_ERROR
;
478 ures_close(defaultColl
);
480 collElem
= ures_getByKeyWithFallback(collations
, keyBuffer
, collElem
, status
);
481 ures_close(collElem
);
482 ures_close(collations
);
487 U_CAPI UCollator
* U_EXPORT2
488 ucol_openFromShortString( const char *definition
,
490 UParseError
*parseError
,
493 UTRACE_ENTRY_OC(UTRACE_UCOL_OPEN_FROM_SHORT_STRING
);
494 UTRACE_DATA1(UTRACE_INFO
, "short string = \"%s\"", definition
);
496 if(U_FAILURE(*status
)) return 0;
498 UParseError internalParseError
;
501 parseError
= &internalParseError
;
503 parseError
->line
= 0;
504 parseError
->offset
= 0;
505 parseError
->preContext
[0] = 0;
506 parseError
->postContext
[0] = 0;
509 // first we want to pick stuff out of short string.
510 // we'll end up with an UCA version, locale and a bunch of
513 // analyse the string in order to get everything we need.
514 const char *string
= definition
;
516 ucol_sit_initCollatorSpecs(&s
);
517 string
= ucol_sit_readSpecs(&s
, definition
, parseError
, status
);
518 ucol_sit_calculateWholeLocale(&s
);
520 char buffer
[internalBufferSize
];
521 uprv_memset(buffer
, 0, internalBufferSize
);
522 uloc_canonicalize(s
.locale
, buffer
, internalBufferSize
, status
);
524 UCollator
*result
= ucol_open(buffer
, status
);
527 for(i
= 0; i
< UCOL_ATTRIBUTE_COUNT
; i
++) {
528 if(s
.options
[i
] != UCOL_DEFAULT
) {
529 if(forceDefaults
|| ucol_getAttribute(result
, (UColAttribute
)i
, status
) != s
.options
[i
]) {
530 ucol_setAttribute(result
, (UColAttribute
)i
, s
.options
[i
], status
);
533 if(U_FAILURE(*status
)) {
534 parseError
->offset
= (int32_t)(string
- definition
);
541 if(s
.variableTopSet
) {
542 if(s
.variableTopString
[0]) {
543 ucol_setVariableTop(result
, s
.variableTopString
, s
.variableTopStringLen
, status
);
544 } else { // we set by value, using 'B'
545 ucol_restoreVariableTop(result
, s
.variableTopValue
, status
);
550 if(U_FAILURE(*status
)) { // here it can only be a bogus value
555 UTRACE_EXIT_PTR_STATUS(result
, *status
);
560 U_CAPI
int32_t U_EXPORT2
561 ucol_getShortDefinitionString(const UCollator
*coll
,
567 if(U_FAILURE(*status
)) return 0;
569 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
572 return ((icu::Collator
*)coll
)->internalGetShortDefinitionString(locale
,dst
,capacity
,*status
);
575 U_CAPI
int32_t U_EXPORT2
576 ucol_normalizeShortDefinitionString(const char *definition
,
579 UParseError
*parseError
,
583 if(U_FAILURE(*status
)) {
588 uprv_memset(destination
, 0, capacity
*sizeof(char));
598 ucol_sit_initCollatorSpecs(&s
);
599 ucol_sit_readSpecs(&s
, definition
, parseError
, status
);
600 return ucol_sit_dumpSpecs(&s
, destination
, capacity
, status
);
604 * Get a set containing the contractions defined by the collator. The set includes
605 * both the UCA contractions and the contractions defined by the collator
606 * @param coll collator
607 * @param conts the set to hold the result
608 * @param status to hold the error code
609 * @return the size of the contraction set
611 U_CAPI
int32_t U_EXPORT2
612 ucol_getContractions( const UCollator
*coll
,
616 ucol_getContractionsAndExpansions(coll
, contractions
, NULL
, FALSE
, status
);
617 return uset_getItemCount(contractions
);
621 * Get a set containing the expansions defined by the collator. The set includes
622 * both the UCA expansions and the expansions defined by the tailoring
623 * @param coll collator
624 * @param conts the set to hold the result
625 * @param addPrefixes add the prefix contextual elements to contractions
626 * @param status to hold the error code
630 U_CAPI
void U_EXPORT2
631 ucol_getContractionsAndExpansions( const UCollator
*coll
,
637 if(U_FAILURE(*status
)) {
641 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
644 const icu::RuleBasedCollator
*rbc
= icu::RuleBasedCollator::rbcFromUCollator(coll
);
646 *status
= U_UNSUPPORTED_ERROR
;
649 rbc
->internalGetContractionsAndExpansions(
650 icu::UnicodeSet::fromUSet(contractions
),
651 icu::UnicodeSet::fromUSet(expansions
),
652 addPrefixes
, *status
);