/src/mozilla-central/intl/icu/source/i18n/ucol_sit.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // © 2016 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | /* |
4 | | ******************************************************************************* |
5 | | * Copyright (C) 2004-2016, International Business Machines |
6 | | * Corporation and others. All Rights Reserved. |
7 | | ******************************************************************************* |
8 | | * file name: ucol_sit.cpp |
9 | | * encoding: UTF-8 |
10 | | * tab size: 8 (not used) |
11 | | * indentation:4 |
12 | | * |
13 | | * Modification history |
14 | | * Date Name Comments |
15 | | * 03/12/2004 weiv Creation |
16 | | */ |
17 | | |
18 | | #include "unicode/ustring.h" |
19 | | #include "unicode/udata.h" |
20 | | #include "unicode/utf16.h" |
21 | | #include "utracimp.h" |
22 | | #include "ucol_imp.h" |
23 | | #include "cmemory.h" |
24 | | #include "cstring.h" |
25 | | #include "uresimp.h" |
26 | | #include "unicode/coll.h" |
27 | | |
28 | | #ifdef UCOL_TRACE_SIT |
29 | | # include <stdio.h> |
30 | | #endif |
31 | | |
32 | | #if !UCONFIG_NO_COLLATION |
33 | | |
34 | | #include "unicode/tblcoll.h" |
35 | | |
36 | | enum OptionsList { |
37 | | UCOL_SIT_LANGUAGE = 0, |
38 | | UCOL_SIT_SCRIPT = 1, |
39 | | UCOL_SIT_REGION = 2, |
40 | | UCOL_SIT_VARIANT = 3, |
41 | | UCOL_SIT_KEYWORD = 4, |
42 | | UCOL_SIT_PROVIDER = 5, |
43 | | UCOL_SIT_LOCELEMENT_MAX = UCOL_SIT_PROVIDER, /* the last element that's part of LocElements */ |
44 | | |
45 | | UCOL_SIT_BCP47, |
46 | | UCOL_SIT_STRENGTH, |
47 | | UCOL_SIT_CASE_LEVEL, |
48 | | UCOL_SIT_CASE_FIRST, |
49 | | UCOL_SIT_NUMERIC_COLLATION, |
50 | | UCOL_SIT_ALTERNATE_HANDLING, |
51 | | UCOL_SIT_NORMALIZATION_MODE, |
52 | | UCOL_SIT_FRENCH_COLLATION, |
53 | | UCOL_SIT_HIRAGANA_QUATERNARY, |
54 | | UCOL_SIT_VARIABLE_TOP, |
55 | | UCOL_SIT_VARIABLE_TOP_VALUE, |
56 | | UCOL_SIT_ITEMS_COUNT |
57 | | }; |
58 | | |
59 | | /* option starters chars. */ |
60 | | static const char alternateHArg = 'A'; |
61 | | static const char variableTopValArg = 'B'; |
62 | | static const char caseFirstArg = 'C'; |
63 | | static const char numericCollArg = 'D'; |
64 | | static const char caseLevelArg = 'E'; |
65 | | static const char frenchCollArg = 'F'; |
66 | | static const char hiraganaQArg = 'H'; |
67 | | static const char keywordArg = 'K'; |
68 | | static const char languageArg = 'L'; |
69 | | static const char normArg = 'N'; |
70 | | static const char providerArg = 'P'; |
71 | | static const char regionArg = 'R'; |
72 | | static const char strengthArg = 'S'; |
73 | | static const char variableTopArg = 'T'; |
74 | | static const char variantArg = 'V'; |
75 | | static const char RFC3066Arg = 'X'; |
76 | | static const char scriptArg = 'Z'; |
77 | | |
78 | | static const char collationKeyword[] = "@collation="; |
79 | | static const char providerKeyword[] = "@sp="; |
80 | | |
81 | | |
82 | | static const int32_t locElementCount = UCOL_SIT_LOCELEMENT_MAX+1; |
83 | | static const int32_t locElementCapacity = 32; |
84 | | static const int32_t loc3066Capacity = 256; |
85 | | static const int32_t locProviderCapacity = 10; |
86 | | static const int32_t internalBufferSize = 512; |
87 | | |
88 | | /* structure containing specification of a collator. Initialized |
89 | | * from a short string. Also used to construct a short string from a |
90 | | * collator instance |
91 | | */ |
92 | | struct CollatorSpec { |
93 | | char locElements[locElementCount][locElementCapacity]; |
94 | | char locale[loc3066Capacity]; |
95 | | char provider[locProviderCapacity]; |
96 | | UColAttributeValue options[UCOL_ATTRIBUTE_COUNT]; |
97 | | uint32_t variableTopValue; |
98 | | UChar variableTopString[locElementCapacity]; |
99 | | int32_t variableTopStringLen; |
100 | | UBool variableTopSet; |
101 | | struct { |
102 | | const char *start; |
103 | | int32_t len; |
104 | | } entries[UCOL_SIT_ITEMS_COUNT]; |
105 | | }; |
106 | | |
107 | | |
108 | | /* structure for converting between character attribute |
109 | | * representation and real collation attribute value. |
110 | | */ |
111 | | struct AttributeConversion { |
112 | | char letter; |
113 | | UColAttributeValue value; |
114 | | }; |
115 | | |
116 | | static const AttributeConversion conversions[12] = { |
117 | | { '1', UCOL_PRIMARY }, |
118 | | { '2', UCOL_SECONDARY }, |
119 | | { '3', UCOL_TERTIARY }, |
120 | | { '4', UCOL_QUATERNARY }, |
121 | | { 'D', UCOL_DEFAULT }, |
122 | | { 'I', UCOL_IDENTICAL }, |
123 | | { 'L', UCOL_LOWER_FIRST }, |
124 | | { 'N', UCOL_NON_IGNORABLE }, |
125 | | { 'O', UCOL_ON }, |
126 | | { 'S', UCOL_SHIFTED }, |
127 | | { 'U', UCOL_UPPER_FIRST }, |
128 | | { 'X', UCOL_OFF } |
129 | | }; |
130 | | |
131 | | |
132 | | static UColAttributeValue |
133 | 0 | ucol_sit_letterToAttributeValue(char letter, UErrorCode *status) { |
134 | 0 | uint32_t i = 0; |
135 | 0 | for(i = 0; i < UPRV_LENGTHOF(conversions); i++) { |
136 | 0 | if(conversions[i].letter == letter) { |
137 | 0 | return conversions[i].value; |
138 | 0 | } |
139 | 0 | } |
140 | 0 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
141 | | #ifdef UCOL_TRACE_SIT |
142 | | fprintf(stderr, "%s:%d: unknown letter %c: %s\n", __FILE__, __LINE__, letter, u_errorName(*status)); |
143 | | #endif |
144 | | return UCOL_DEFAULT; |
145 | 0 | } |
146 | | |
147 | | /* function prototype for functions used to parse a short string */ |
148 | | U_CDECL_BEGIN |
149 | | typedef const char* U_CALLCONV |
150 | | ActionFunction(CollatorSpec *spec, uint32_t value1, const char* string, |
151 | | UErrorCode *status); |
152 | | U_CDECL_END |
153 | | |
154 | | U_CDECL_BEGIN |
155 | | static const char* U_CALLCONV |
156 | | _processLocaleElement(CollatorSpec *spec, uint32_t value, const char* string, |
157 | | UErrorCode *status) |
158 | 0 | { |
159 | 0 | int32_t len = 0; |
160 | 0 | do { |
161 | 0 | if(value == UCOL_SIT_LANGUAGE || value == UCOL_SIT_KEYWORD || value == UCOL_SIT_PROVIDER) { |
162 | 0 | spec->locElements[value][len++] = uprv_tolower(*string); |
163 | 0 | } else { |
164 | 0 | spec->locElements[value][len++] = *string; |
165 | 0 | } |
166 | 0 | } while(*(++string) != '_' && *string && len < locElementCapacity); |
167 | 0 | if(len >= locElementCapacity) { |
168 | 0 | *status = U_BUFFER_OVERFLOW_ERROR; |
169 | 0 | return string; |
170 | 0 | } |
171 | 0 | // don't skip the underscore at the end |
172 | 0 | return string; |
173 | 0 | } |
174 | | U_CDECL_END |
175 | | |
176 | | U_CDECL_BEGIN |
177 | | static const char* U_CALLCONV |
178 | | _processRFC3066Locale(CollatorSpec *spec, uint32_t, const char* string, |
179 | | UErrorCode *status) |
180 | 0 | { |
181 | 0 | char terminator = *string; |
182 | 0 | string++; |
183 | 0 | const char *end = uprv_strchr(string+1, terminator); |
184 | 0 | if(end == NULL || end - string >= loc3066Capacity) { |
185 | 0 | *status = U_BUFFER_OVERFLOW_ERROR; |
186 | 0 | return string; |
187 | 0 | } else { |
188 | 0 | uprv_strncpy(spec->locale, string, end-string); |
189 | 0 | return end+1; |
190 | 0 | } |
191 | 0 | } |
192 | | |
193 | | U_CDECL_END |
194 | | |
195 | | U_CDECL_BEGIN |
196 | | static const char* U_CALLCONV |
197 | | _processCollatorOption(CollatorSpec *spec, uint32_t option, const char* string, |
198 | | UErrorCode *status) |
199 | 0 | { |
200 | 0 | spec->options[option] = ucol_sit_letterToAttributeValue(*string, status); |
201 | 0 | if((*(++string) != '_' && *string) || U_FAILURE(*status)) { |
202 | | #ifdef UCOL_TRACE_SIT |
203 | | fprintf(stderr, "%s:%d: unknown collator option at '%s': %s\n", __FILE__, __LINE__, string, u_errorName(*status)); |
204 | | #endif |
205 | | *status = U_ILLEGAL_ARGUMENT_ERROR; |
206 | 0 | } |
207 | 0 | return string; |
208 | 0 | } |
209 | | U_CDECL_END |
210 | | |
211 | | |
212 | | static UChar |
213 | | readHexCodeUnit(const char **string, UErrorCode *status) |
214 | 0 | { |
215 | 0 | UChar result = 0; |
216 | 0 | int32_t value = 0; |
217 | 0 | char c; |
218 | 0 | int32_t noDigits = 0; |
219 | 0 | while((c = **string) != 0 && noDigits < 4) { |
220 | 0 | if( c >= '0' && c <= '9') { |
221 | 0 | value = c - '0'; |
222 | 0 | } else if ( c >= 'a' && c <= 'f') { |
223 | 0 | value = c - 'a' + 10; |
224 | 0 | } else if ( c >= 'A' && c <= 'F') { |
225 | 0 | value = c - 'A' + 10; |
226 | 0 | } else { |
227 | 0 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
228 | | #ifdef UCOL_TRACE_SIT |
229 | | fprintf(stderr, "%s:%d: Bad hex char at '%s': %s\n", __FILE__, __LINE__, *string, u_errorName(*status)); |
230 | | #endif |
231 | | return 0; |
232 | 0 | } |
233 | 0 | result = (result << 4) | (UChar)value; |
234 | 0 | noDigits++; |
235 | 0 | (*string)++; |
236 | 0 | } |
237 | 0 | // if the string was terminated before we read 4 digits, set an error |
238 | 0 | if(noDigits < 4) { |
239 | 0 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
240 | | #ifdef UCOL_TRACE_SIT |
241 | | fprintf(stderr, "%s:%d: Short (only %d digits, wanted 4) at '%s': %s\n", __FILE__, __LINE__, noDigits,*string, u_errorName(*status)); |
242 | | #endif |
243 | | } |
244 | 0 | return result; |
245 | 0 | } |
246 | | |
247 | | U_CDECL_BEGIN |
248 | | static const char* U_CALLCONV |
249 | | _processVariableTop(CollatorSpec *spec, uint32_t value1, const char* string, UErrorCode *status) |
250 | 0 | { |
251 | 0 | // get four digits |
252 | 0 | int32_t i = 0; |
253 | 0 | if(!value1) { |
254 | 0 | while(U_SUCCESS(*status) && i < locElementCapacity && *string != 0 && *string != '_') { |
255 | 0 | spec->variableTopString[i++] = readHexCodeUnit(&string, status); |
256 | 0 | } |
257 | 0 | spec->variableTopStringLen = i; |
258 | 0 | if(i == locElementCapacity && *string != 0 && *string != '_') { |
259 | 0 | *status = U_BUFFER_OVERFLOW_ERROR; |
260 | 0 | } |
261 | 0 | } else { |
262 | 0 | spec->variableTopValue = readHexCodeUnit(&string, status); |
263 | 0 | } |
264 | 0 | if(U_SUCCESS(*status)) { |
265 | 0 | spec->variableTopSet = TRUE; |
266 | 0 | } |
267 | 0 | return string; |
268 | 0 | } |
269 | | U_CDECL_END |
270 | | |
271 | | |
272 | | /* Table for parsing short strings */ |
273 | | struct ShortStringOptions { |
274 | | char optionStart; |
275 | | ActionFunction *action; |
276 | | uint32_t attr; |
277 | | }; |
278 | | |
279 | | static const ShortStringOptions options[UCOL_SIT_ITEMS_COUNT] = |
280 | | { |
281 | | /* 10 ALTERNATE_HANDLING */ {alternateHArg, _processCollatorOption, UCOL_ALTERNATE_HANDLING }, // alternate N, S, D |
282 | | /* 15 VARIABLE_TOP_VALUE */ {variableTopValArg, _processVariableTop, 1 }, |
283 | | /* 08 CASE_FIRST */ {caseFirstArg, _processCollatorOption, UCOL_CASE_FIRST }, // case first L, U, X, D |
284 | | /* 09 NUMERIC_COLLATION */ {numericCollArg, _processCollatorOption, UCOL_NUMERIC_COLLATION }, // codan O, X, D |
285 | | /* 07 CASE_LEVEL */ {caseLevelArg, _processCollatorOption, UCOL_CASE_LEVEL }, // case level O, X, D |
286 | | /* 12 FRENCH_COLLATION */ {frenchCollArg, _processCollatorOption, UCOL_FRENCH_COLLATION }, // french O, X, D |
287 | | /* 13 HIRAGANA_QUATERNARY] */ {hiraganaQArg, _processCollatorOption, UCOL_HIRAGANA_QUATERNARY_MODE }, // hiragana O, X, D |
288 | | /* 04 KEYWORD */ {keywordArg, _processLocaleElement, UCOL_SIT_KEYWORD }, // keyword |
289 | | /* 00 LANGUAGE */ {languageArg, _processLocaleElement, UCOL_SIT_LANGUAGE }, // language |
290 | | /* 11 NORMALIZATION_MODE */ {normArg, _processCollatorOption, UCOL_NORMALIZATION_MODE }, // norm O, X, D |
291 | | /* 02 REGION */ {regionArg, _processLocaleElement, UCOL_SIT_REGION }, // region |
292 | | /* 06 STRENGTH */ {strengthArg, _processCollatorOption, UCOL_STRENGTH }, // strength 1, 2, 3, 4, I, D |
293 | | /* 14 VARIABLE_TOP */ {variableTopArg, _processVariableTop, 0 }, |
294 | | /* 03 VARIANT */ {variantArg, _processLocaleElement, UCOL_SIT_VARIANT }, // variant |
295 | | /* 05 RFC3066BIS */ {RFC3066Arg, _processRFC3066Locale, 0 }, // rfc3066bis locale name |
296 | | /* 01 SCRIPT */ {scriptArg, _processLocaleElement, UCOL_SIT_SCRIPT }, // script |
297 | | /* PROVIDER */ {providerArg, _processLocaleElement, UCOL_SIT_PROVIDER } |
298 | | }; |
299 | | |
300 | | |
301 | | static |
302 | | const char* ucol_sit_readOption(const char *start, CollatorSpec *spec, |
303 | | UErrorCode *status) |
304 | 0 | { |
305 | 0 | int32_t i = 0; |
306 | 0 |
|
307 | 0 | for(i = 0; i < UCOL_SIT_ITEMS_COUNT; i++) { |
308 | 0 | if(*start == options[i].optionStart) { |
309 | 0 | spec->entries[i].start = start; |
310 | 0 | const char* end = options[i].action(spec, options[i].attr, start+1, status); |
311 | 0 | spec->entries[i].len = (int32_t)(end - start); |
312 | 0 | return end; |
313 | 0 | } |
314 | 0 | } |
315 | 0 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
316 | | #ifdef UCOL_TRACE_SIT |
317 | | fprintf(stderr, "%s:%d: Unknown option at '%s': %s\n", __FILE__, __LINE__, start, u_errorName(*status)); |
318 | | #endif |
319 | | return start; |
320 | 0 | } |
321 | | |
322 | | static |
323 | | void ucol_sit_initCollatorSpecs(CollatorSpec *spec) |
324 | 0 | { |
325 | 0 | // reset everything |
326 | 0 | uprv_memset(spec, 0, sizeof(CollatorSpec)); |
327 | 0 | // set collation options to default |
328 | 0 | int32_t i = 0; |
329 | 0 | for(i = 0; i < UCOL_ATTRIBUTE_COUNT; i++) { |
330 | 0 | spec->options[i] = UCOL_DEFAULT; |
331 | 0 | } |
332 | 0 | } |
333 | | |
334 | | static const char* |
335 | | ucol_sit_readSpecs(CollatorSpec *s, const char *string, |
336 | | UParseError *parseError, UErrorCode *status) |
337 | 0 | { |
338 | 0 | const char *definition = string; |
339 | 0 | while(U_SUCCESS(*status) && *string) { |
340 | 0 | string = ucol_sit_readOption(string, s, status); |
341 | 0 | // advance over '_' |
342 | 0 | while(*string && *string == '_') { |
343 | 0 | string++; |
344 | 0 | } |
345 | 0 | } |
346 | 0 | if(U_FAILURE(*status)) { |
347 | 0 | parseError->offset = (int32_t)(string - definition); |
348 | 0 | } |
349 | 0 | return string; |
350 | 0 | } |
351 | | |
352 | | static |
353 | | int32_t ucol_sit_dumpSpecs(CollatorSpec *s, char *destination, int32_t capacity, UErrorCode *status) |
354 | 0 | { |
355 | 0 | int32_t i = 0, j = 0; |
356 | 0 | int32_t len = 0; |
357 | 0 | char optName; |
358 | 0 | if(U_SUCCESS(*status)) { |
359 | 0 | for(i = 0; i < UCOL_SIT_ITEMS_COUNT; i++) { |
360 | 0 | if(s->entries[i].start) { |
361 | 0 | if(len) { |
362 | 0 | if(len < capacity) { |
363 | 0 | uprv_strcat(destination, "_"); |
364 | 0 | } |
365 | 0 | len++; |
366 | 0 | } |
367 | 0 | optName = *(s->entries[i].start); |
368 | 0 | if(optName == languageArg || optName == regionArg || optName == variantArg || optName == keywordArg) { |
369 | 0 | for(j = 0; j < s->entries[i].len; j++) { |
370 | 0 | if(len + j < capacity) { |
371 | 0 | destination[len+j] = uprv_toupper(*(s->entries[i].start+j)); |
372 | 0 | } |
373 | 0 | } |
374 | 0 | len += s->entries[i].len; |
375 | 0 | } else { |
376 | 0 | len += s->entries[i].len; |
377 | 0 | if(len < capacity) { |
378 | 0 | uprv_strncat(destination,s->entries[i].start, s->entries[i].len); |
379 | 0 | } |
380 | 0 | } |
381 | 0 | } |
382 | 0 | } |
383 | 0 | return len; |
384 | 0 | } else { |
385 | 0 | return 0; |
386 | 0 | } |
387 | 0 | } |
388 | | |
389 | | static void |
390 | 0 | ucol_sit_calculateWholeLocale(CollatorSpec *s) { |
391 | 0 | // put the locale together, unless we have a done |
392 | 0 | // locale |
393 | 0 | if(s->locale[0] == 0) { |
394 | 0 | // first the language |
395 | 0 | uprv_strcat(s->locale, s->locElements[UCOL_SIT_LANGUAGE]); |
396 | 0 | // then the script, if present |
397 | 0 | if(*(s->locElements[UCOL_SIT_SCRIPT])) { |
398 | 0 | uprv_strcat(s->locale, "_"); |
399 | 0 | uprv_strcat(s->locale, s->locElements[UCOL_SIT_SCRIPT]); |
400 | 0 | } |
401 | 0 | // then the region, if present |
402 | 0 | if(*(s->locElements[UCOL_SIT_REGION])) { |
403 | 0 | uprv_strcat(s->locale, "_"); |
404 | 0 | uprv_strcat(s->locale, s->locElements[UCOL_SIT_REGION]); |
405 | 0 | } else if(*(s->locElements[UCOL_SIT_VARIANT])) { // if there is a variant, we need an underscore |
406 | 0 | uprv_strcat(s->locale, "_"); |
407 | 0 | } |
408 | 0 | // add variant, if there |
409 | 0 | if(*(s->locElements[UCOL_SIT_VARIANT])) { |
410 | 0 | uprv_strcat(s->locale, "_"); |
411 | 0 | uprv_strcat(s->locale, s->locElements[UCOL_SIT_VARIANT]); |
412 | 0 | } |
413 | 0 |
|
414 | 0 | // if there is a collation keyword, add that too |
415 | 0 | if(*(s->locElements[UCOL_SIT_KEYWORD])) { |
416 | 0 | uprv_strcat(s->locale, collationKeyword); |
417 | 0 | uprv_strcat(s->locale, s->locElements[UCOL_SIT_KEYWORD]); |
418 | 0 | } |
419 | 0 |
|
420 | 0 | // if there is a provider keyword, add that too |
421 | 0 | if(*(s->locElements[UCOL_SIT_PROVIDER])) { |
422 | 0 | uprv_strcat(s->locale, providerKeyword); |
423 | 0 | uprv_strcat(s->locale, s->locElements[UCOL_SIT_PROVIDER]); |
424 | 0 | } |
425 | 0 | } |
426 | 0 | } |
427 | | |
428 | | |
429 | | U_CAPI void U_EXPORT2 |
430 | | ucol_prepareShortStringOpen( const char *definition, |
431 | | UBool, |
432 | | UParseError *parseError, |
433 | | UErrorCode *status) |
434 | 0 | { |
435 | 0 | if(U_FAILURE(*status)) return; |
436 | 0 | |
437 | 0 | UParseError internalParseError; |
438 | 0 |
|
439 | 0 | if(!parseError) { |
440 | 0 | parseError = &internalParseError; |
441 | 0 | } |
442 | 0 | parseError->line = 0; |
443 | 0 | parseError->offset = 0; |
444 | 0 | parseError->preContext[0] = 0; |
445 | 0 | parseError->postContext[0] = 0; |
446 | 0 |
|
447 | 0 |
|
448 | 0 | // first we want to pick stuff out of short string. |
449 | 0 | // we'll end up with an UCA version, locale and a bunch of |
450 | 0 | // settings |
451 | 0 |
|
452 | 0 | // analyse the string in order to get everything we need. |
453 | 0 | CollatorSpec s; |
454 | 0 | ucol_sit_initCollatorSpecs(&s); |
455 | 0 | ucol_sit_readSpecs(&s, definition, parseError, status); |
456 | 0 | ucol_sit_calculateWholeLocale(&s); |
457 | 0 |
|
458 | 0 | char buffer[internalBufferSize]; |
459 | 0 | uprv_memset(buffer, 0, internalBufferSize); |
460 | 0 | uloc_canonicalize(s.locale, buffer, internalBufferSize, status); |
461 | 0 |
|
462 | 0 | UResourceBundle *b = ures_open(U_ICUDATA_COLL, buffer, status); |
463 | 0 | /* we try to find stuff from keyword */ |
464 | 0 | UResourceBundle *collations = ures_getByKey(b, "collations", NULL, status); |
465 | 0 | UResourceBundle *collElem = NULL; |
466 | 0 | char keyBuffer[256]; |
467 | 0 | // if there is a keyword, we pick it up and try to get elements |
468 | 0 | int32_t keyLen = uloc_getKeywordValue(buffer, "collation", keyBuffer, sizeof(keyBuffer), status); |
469 | 0 | // Treat too long a value as no keyword. |
470 | 0 | if(keyLen >= (int32_t)sizeof(keyBuffer)) { |
471 | 0 | keyLen = 0; |
472 | 0 | *status = U_ZERO_ERROR; |
473 | 0 | } |
474 | 0 | if(keyLen == 0) { |
475 | 0 | // no keyword |
476 | 0 | // we try to find the default setting, which will give us the keyword value |
477 | 0 | UResourceBundle *defaultColl = ures_getByKeyWithFallback(collations, "default", NULL, status); |
478 | 0 | if(U_SUCCESS(*status)) { |
479 | 0 | int32_t defaultKeyLen = 0; |
480 | 0 | const UChar *defaultKey = ures_getString(defaultColl, &defaultKeyLen, status); |
481 | 0 | u_UCharsToChars(defaultKey, keyBuffer, defaultKeyLen); |
482 | 0 | keyBuffer[defaultKeyLen] = 0; |
483 | 0 | } else { |
484 | 0 | *status = U_INTERNAL_PROGRAM_ERROR; |
485 | 0 | return; |
486 | 0 | } |
487 | 0 | ures_close(defaultColl); |
488 | 0 | } |
489 | 0 | collElem = ures_getByKeyWithFallback(collations, keyBuffer, collElem, status); |
490 | 0 | ures_close(collElem); |
491 | 0 | ures_close(collations); |
492 | 0 | ures_close(b); |
493 | 0 | } |
494 | | |
495 | | |
496 | | U_CAPI UCollator* U_EXPORT2 |
497 | | ucol_openFromShortString( const char *definition, |
498 | | UBool forceDefaults, |
499 | | UParseError *parseError, |
500 | | UErrorCode *status) |
501 | 0 | { |
502 | 0 | UTRACE_ENTRY_OC(UTRACE_UCOL_OPEN_FROM_SHORT_STRING); |
503 | 0 | UTRACE_DATA1(UTRACE_INFO, "short string = \"%s\"", definition); |
504 | 0 |
|
505 | 0 | if(U_FAILURE(*status)) return 0; |
506 | 0 | |
507 | 0 | UParseError internalParseError; |
508 | 0 |
|
509 | 0 | if(!parseError) { |
510 | 0 | parseError = &internalParseError; |
511 | 0 | } |
512 | 0 | parseError->line = 0; |
513 | 0 | parseError->offset = 0; |
514 | 0 | parseError->preContext[0] = 0; |
515 | 0 | parseError->postContext[0] = 0; |
516 | 0 |
|
517 | 0 |
|
518 | 0 | // first we want to pick stuff out of short string. |
519 | 0 | // we'll end up with an UCA version, locale and a bunch of |
520 | 0 | // settings |
521 | 0 |
|
522 | 0 | // analyse the string in order to get everything we need. |
523 | 0 | const char *string = definition; |
524 | 0 | CollatorSpec s; |
525 | 0 | ucol_sit_initCollatorSpecs(&s); |
526 | 0 | string = ucol_sit_readSpecs(&s, definition, parseError, status); |
527 | 0 | ucol_sit_calculateWholeLocale(&s); |
528 | 0 |
|
529 | 0 | char buffer[internalBufferSize]; |
530 | 0 | uprv_memset(buffer, 0, internalBufferSize); |
531 | 0 | uloc_canonicalize(s.locale, buffer, internalBufferSize, status); |
532 | 0 |
|
533 | 0 | UCollator *result = ucol_open(buffer, status); |
534 | 0 | int32_t i = 0; |
535 | 0 |
|
536 | 0 | for(i = 0; i < UCOL_ATTRIBUTE_COUNT; i++) { |
537 | 0 | if(s.options[i] != UCOL_DEFAULT) { |
538 | 0 | if(forceDefaults || ucol_getAttribute(result, (UColAttribute)i, status) != s.options[i]) { |
539 | 0 | ucol_setAttribute(result, (UColAttribute)i, s.options[i], status); |
540 | 0 | } |
541 | 0 |
|
542 | 0 | if(U_FAILURE(*status)) { |
543 | 0 | parseError->offset = (int32_t)(string - definition); |
544 | 0 | ucol_close(result); |
545 | 0 | return NULL; |
546 | 0 | } |
547 | 0 |
|
548 | 0 | } |
549 | 0 | } |
550 | 0 | if(s.variableTopSet) { |
551 | 0 | if(s.variableTopString[0]) { |
552 | 0 | ucol_setVariableTop(result, s.variableTopString, s.variableTopStringLen, status); |
553 | 0 | } else { // we set by value, using 'B' |
554 | 0 | ucol_restoreVariableTop(result, s.variableTopValue, status); |
555 | 0 | } |
556 | 0 | } |
557 | 0 |
|
558 | 0 |
|
559 | 0 | if(U_FAILURE(*status)) { // here it can only be a bogus value |
560 | 0 | ucol_close(result); |
561 | 0 | result = NULL; |
562 | 0 | } |
563 | 0 |
|
564 | 0 | UTRACE_EXIT_PTR_STATUS(result, *status); |
565 | 0 | return result; |
566 | 0 | } |
567 | | |
568 | | |
569 | | U_CAPI int32_t U_EXPORT2 |
570 | | ucol_getShortDefinitionString(const UCollator *coll, |
571 | | const char *locale, |
572 | | char *dst, |
573 | | int32_t capacity, |
574 | | UErrorCode *status) |
575 | 0 | { |
576 | 0 | if(U_FAILURE(*status)) return 0; |
577 | 0 | if(coll == NULL) { |
578 | 0 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
579 | 0 | return 0; |
580 | 0 | } |
581 | 0 | return ((icu::Collator*)coll)->internalGetShortDefinitionString(locale,dst,capacity,*status); |
582 | 0 | } |
583 | | |
584 | | U_CAPI int32_t U_EXPORT2 |
585 | | ucol_normalizeShortDefinitionString(const char *definition, |
586 | | char *destination, |
587 | | int32_t capacity, |
588 | | UParseError *parseError, |
589 | | UErrorCode *status) |
590 | 0 | { |
591 | 0 |
|
592 | 0 | if(U_FAILURE(*status)) { |
593 | 0 | return 0; |
594 | 0 | } |
595 | 0 | |
596 | 0 | if(destination) { |
597 | 0 | uprv_memset(destination, 0, capacity*sizeof(char)); |
598 | 0 | } |
599 | 0 |
|
600 | 0 | UParseError pe; |
601 | 0 | if(!parseError) { |
602 | 0 | parseError = &pe; |
603 | 0 | } |
604 | 0 |
|
605 | 0 | // validate |
606 | 0 | CollatorSpec s; |
607 | 0 | ucol_sit_initCollatorSpecs(&s); |
608 | 0 | ucol_sit_readSpecs(&s, definition, parseError, status); |
609 | 0 | return ucol_sit_dumpSpecs(&s, destination, capacity, status); |
610 | 0 | } |
611 | | |
612 | | /** |
613 | | * Get a set containing the contractions defined by the collator. The set includes |
614 | | * both the UCA contractions and the contractions defined by the collator |
615 | | * @param coll collator |
616 | | * @param conts the set to hold the result |
617 | | * @param status to hold the error code |
618 | | * @return the size of the contraction set |
619 | | */ |
620 | | U_CAPI int32_t U_EXPORT2 |
621 | | ucol_getContractions( const UCollator *coll, |
622 | | USet *contractions, |
623 | | UErrorCode *status) |
624 | 0 | { |
625 | 0 | ucol_getContractionsAndExpansions(coll, contractions, NULL, FALSE, status); |
626 | 0 | return uset_getItemCount(contractions); |
627 | 0 | } |
628 | | |
629 | | /** |
630 | | * Get a set containing the expansions defined by the collator. The set includes |
631 | | * both the UCA expansions and the expansions defined by the tailoring |
632 | | * @param coll collator |
633 | | * @param conts the set to hold the result |
634 | | * @param addPrefixes add the prefix contextual elements to contractions |
635 | | * @param status to hold the error code |
636 | | * |
637 | | * @draft ICU 3.4 |
638 | | */ |
639 | | U_CAPI void U_EXPORT2 |
640 | | ucol_getContractionsAndExpansions( const UCollator *coll, |
641 | | USet *contractions, |
642 | | USet *expansions, |
643 | | UBool addPrefixes, |
644 | | UErrorCode *status) |
645 | 0 | { |
646 | 0 | if(U_FAILURE(*status)) { |
647 | 0 | return; |
648 | 0 | } |
649 | 0 | if(coll == NULL) { |
650 | 0 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
651 | 0 | return; |
652 | 0 | } |
653 | 0 | const icu::RuleBasedCollator *rbc = icu::RuleBasedCollator::rbcFromUCollator(coll); |
654 | 0 | if(rbc == NULL) { |
655 | 0 | *status = U_UNSUPPORTED_ERROR; |
656 | 0 | return; |
657 | 0 | } |
658 | 0 | rbc->internalGetContractionsAndExpansions( |
659 | 0 | icu::UnicodeSet::fromUSet(contractions), |
660 | 0 | icu::UnicodeSet::fromUSet(expansions), |
661 | 0 | addPrefixes, *status); |
662 | 0 | } |
663 | | #endif |