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