| 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 | #include "unicode/stringpiece.h" |
| 28 | #include "charstr.h" |
| 29 | |
| 30 | U_NAMESPACE_USE |
| 31 | |
| 32 | #ifdef UCOL_TRACE_SIT |
| 33 | # include <stdio.h> |
| 34 | #endif |
| 35 | |
| 36 | #if !UCONFIG_NO_COLLATION |
| 37 | |
| 38 | #include "unicode/tblcoll.h" |
| 39 | |
| 40 | enum OptionsList { |
| 41 | UCOL_SIT_LANGUAGE = 0, |
| 42 | UCOL_SIT_SCRIPT = 1, |
| 43 | UCOL_SIT_REGION = 2, |
| 44 | UCOL_SIT_VARIANT = 3, |
| 45 | UCOL_SIT_KEYWORD = 4, |
| 46 | UCOL_SIT_PROVIDER = 5, |
| 47 | UCOL_SIT_LOCELEMENT_MAX = UCOL_SIT_PROVIDER, /* the last element that's part of LocElements */ |
| 48 | |
| 49 | UCOL_SIT_BCP47, |
| 50 | UCOL_SIT_STRENGTH, |
| 51 | UCOL_SIT_CASE_LEVEL, |
| 52 | UCOL_SIT_CASE_FIRST, |
| 53 | UCOL_SIT_NUMERIC_COLLATION, |
| 54 | UCOL_SIT_ALTERNATE_HANDLING, |
| 55 | UCOL_SIT_NORMALIZATION_MODE, |
| 56 | UCOL_SIT_FRENCH_COLLATION, |
| 57 | UCOL_SIT_HIRAGANA_QUATERNARY, |
| 58 | UCOL_SIT_VARIABLE_TOP, |
| 59 | UCOL_SIT_VARIABLE_TOP_VALUE, |
| 60 | UCOL_SIT_ITEMS_COUNT |
| 61 | }; |
| 62 | |
| 63 | /* option starters chars. */ |
| 64 | static const char alternateHArg = 'A'; |
| 65 | static const char variableTopValArg = 'B'; |
| 66 | static const char caseFirstArg = 'C'; |
| 67 | static const char numericCollArg = 'D'; |
| 68 | static const char caseLevelArg = 'E'; |
| 69 | static const char frenchCollArg = 'F'; |
| 70 | static const char hiraganaQArg = 'H'; |
| 71 | static const char keywordArg = 'K'; |
| 72 | static const char languageArg = 'L'; |
| 73 | static const char normArg = 'N'; |
| 74 | static const char providerArg = 'P'; |
| 75 | static const char regionArg = 'R'; |
| 76 | static const char strengthArg = 'S'; |
| 77 | static const char variableTopArg = 'T'; |
| 78 | static const char variantArg = 'V'; |
| 79 | static const char RFC3066Arg = 'X'; |
| 80 | static const char scriptArg = 'Z'; |
| 81 | |
| 82 | static const char collationKeyword[] = "@collation=" ; |
| 83 | static const char providerKeyword[] = "@sp=" ; |
| 84 | |
| 85 | |
| 86 | static const int32_t locElementCount = UCOL_SIT_LOCELEMENT_MAX+1; |
| 87 | static const int32_t locElementCapacity = 32; |
| 88 | static const int32_t loc3066Capacity = 256; |
| 89 | static const int32_t internalBufferSize = 512; |
| 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 | UChar variableTopString[locElementCapacity]; |
| 103 | int32_t variableTopStringLen; |
| 104 | UBool variableTopSet; |
| 105 | CharString entries[UCOL_SIT_ITEMS_COUNT]; |
| 106 | }; |
| 107 | |
| 108 | CollatorSpec::CollatorSpec() : |
| 109 | locale(), |
| 110 | variableTopValue(0), |
| 111 | variableTopString(), |
| 112 | variableTopSet(FALSE) |
| 113 | { |
| 114 | // set collation options to default |
| 115 | for(int32_t i = 0; i < UCOL_ATTRIBUTE_COUNT; i++) { |
| 116 | options[i] = UCOL_DEFAULT; |
| 117 | } |
| 118 | } |
| 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 | ucol_sit_letterToAttributeValue(char letter, UErrorCode *status) { |
| 147 | uint32_t i = 0; |
| 148 | for(i = 0; i < UPRV_LENGTHOF(conversions); i++) { |
| 149 | if(conversions[i].letter == letter) { |
| 150 | return conversions[i].value; |
| 151 | } |
| 152 | } |
| 153 | *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 | return UCOL_DEFAULT; |
| 158 | } |
| 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 | { |
| 172 | do { |
| 173 | if(value == UCOL_SIT_LANGUAGE || value == UCOL_SIT_KEYWORD || value == UCOL_SIT_PROVIDER) { |
| 174 | spec->locElements[value].append(uprv_tolower(*string), *status); |
| 175 | } else { |
| 176 | spec->locElements[value].append(*string, *status); |
| 177 | } |
| 178 | } while(*(++string) != '_' && *string && U_SUCCESS(*status)); |
| 179 | // don't skip the underscore at the end |
| 180 | return string; |
| 181 | } |
| 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 | { |
| 189 | char terminator = *string; |
| 190 | string++; |
| 191 | const char *end = uprv_strchr(string+1, terminator); |
| 192 | if(end == NULL || end - string >= loc3066Capacity) { |
| 193 | *status = U_BUFFER_OVERFLOW_ERROR; |
| 194 | return string; |
| 195 | } else { |
| 196 | spec->locale.copyFrom(CharString(string, static_cast<int32_t>(end-string), *status), *status); |
| 197 | return end+1; |
| 198 | } |
| 199 | } |
| 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 | { |
| 208 | spec->options[option] = ucol_sit_letterToAttributeValue(*string, status); |
| 209 | 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 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
| 214 | } |
| 215 | return string; |
| 216 | } |
| 217 | U_CDECL_END |
| 218 | |
| 219 | |
| 220 | static UChar |
| 221 | readHexCodeUnit(const char **string, UErrorCode *status) |
| 222 | { |
| 223 | UChar result = 0; |
| 224 | int32_t value = 0; |
| 225 | char c; |
| 226 | int32_t noDigits = 0; |
| 227 | while((c = **string) != 0 && noDigits < 4) { |
| 228 | if( c >= '0' && c <= '9') { |
| 229 | value = c - '0'; |
| 230 | } else if ( c >= 'a' && c <= 'f') { |
| 231 | value = c - 'a' + 10; |
| 232 | } else if ( c >= 'A' && c <= 'F') { |
| 233 | value = c - 'A' + 10; |
| 234 | } else { |
| 235 | *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 | return 0; |
| 240 | } |
| 241 | result = (result << 4) | (UChar)value; |
| 242 | noDigits++; |
| 243 | (*string)++; |
| 244 | } |
| 245 | // if the string was terminated before we read 4 digits, set an error |
| 246 | if(noDigits < 4) { |
| 247 | *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 | } |
| 252 | return result; |
| 253 | } |
| 254 | |
| 255 | U_CDECL_BEGIN |
| 256 | static const char* U_CALLCONV |
| 257 | _processVariableTop(CollatorSpec *spec, uint32_t value1, const char* string, UErrorCode *status) |
| 258 | { |
| 259 | // get four digits |
| 260 | int32_t i = 0; |
| 261 | if(!value1) { |
| 262 | while(U_SUCCESS(*status) && i < locElementCapacity && *string != 0 && *string != '_') { |
| 263 | spec->variableTopString[i++] = readHexCodeUnit(&string, status); |
| 264 | } |
| 265 | spec->variableTopStringLen = i; |
| 266 | if(i == locElementCapacity && *string != 0 && *string != '_') { |
| 267 | *status = U_BUFFER_OVERFLOW_ERROR; |
| 268 | } |
| 269 | } else { |
| 270 | spec->variableTopValue = readHexCodeUnit(&string, status); |
| 271 | } |
| 272 | if(U_SUCCESS(*status)) { |
| 273 | spec->variableTopSet = TRUE; |
| 274 | } |
| 275 | return string; |
| 276 | } |
| 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 | { |
| 313 | int32_t i = 0; |
| 314 | |
| 315 | for(i = 0; i < UCOL_SIT_ITEMS_COUNT; i++) { |
| 316 | if(*start == options[i].optionStart) { |
| 317 | 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 | spec->entries[i].copyFrom(CharString(start, (int32_t)(end - start), *status), *status); |
| 323 | return end; |
| 324 | } |
| 325 | } |
| 326 | *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 | return start; |
| 331 | } |
| 332 | |
| 333 | static const char* |
| 334 | ucol_sit_readSpecs(CollatorSpec *s, const char *string, |
| 335 | UParseError *parseError, UErrorCode *status) |
| 336 | { |
| 337 | const char *definition = string; |
| 338 | while(U_SUCCESS(*status) && *string) { |
| 339 | string = ucol_sit_readOption(string, s, status); |
| 340 | // advance over '_' |
| 341 | while(*string && *string == '_') { |
| 342 | string++; |
| 343 | } |
| 344 | } |
| 345 | if(U_FAILURE(*status)) { |
| 346 | parseError->offset = (int32_t)(string - definition); |
| 347 | } |
| 348 | return string; |
| 349 | } |
| 350 | |
| 351 | static |
| 352 | int32_t ucol_sit_dumpSpecs(CollatorSpec *s, char *destination, int32_t capacity, UErrorCode *status) |
| 353 | { |
| 354 | int32_t i = 0, j = 0; |
| 355 | int32_t len = 0; |
| 356 | char optName; |
| 357 | if(U_SUCCESS(*status)) { |
| 358 | for(i = 0; i < UCOL_SIT_ITEMS_COUNT; i++) { |
| 359 | if(!s->entries[i].isEmpty()) { |
| 360 | if(len) { |
| 361 | if(len < capacity) { |
| 362 | uprv_strcat(destination, "_" ); |
| 363 | } |
| 364 | len++; |
| 365 | } |
| 366 | optName = s->entries[i][0]; |
| 367 | if(optName == languageArg || optName == regionArg || optName == variantArg || optName == keywordArg) { |
| 368 | for(j = 0; j < s->entries[i].length(); j++) { |
| 369 | if(len + j < capacity) { |
| 370 | destination[len+j] = uprv_toupper(s->entries[i][j]); |
| 371 | } |
| 372 | } |
| 373 | len += s->entries[i].length(); |
| 374 | } else { |
| 375 | len += s->entries[i].length(); |
| 376 | if(len < capacity) { |
| 377 | uprv_strncat(destination,s->entries[i].data(), s->entries[i].length()); |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | return len; |
| 383 | } else { |
| 384 | return 0; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | static void |
| 389 | ucol_sit_calculateWholeLocale(CollatorSpec *s, UErrorCode &status) { |
| 390 | // put the locale together, unless we have a done |
| 391 | // locale |
| 392 | if(s->locale.isEmpty()) { |
| 393 | // first the language |
| 394 | s->locale.append(s->locElements[UCOL_SIT_LANGUAGE], status); |
| 395 | // then the script, if present |
| 396 | if(!s->locElements[UCOL_SIT_SCRIPT].isEmpty()) { |
| 397 | s->locale.append("_" , status); |
| 398 | s->locale.append(s->locElements[UCOL_SIT_SCRIPT], status); |
| 399 | } |
| 400 | // then the region, if present |
| 401 | if(!s->locElements[UCOL_SIT_REGION].isEmpty()) { |
| 402 | s->locale.append("_" , status); |
| 403 | s->locale.append(s->locElements[UCOL_SIT_REGION], status); |
| 404 | } else if(!s->locElements[UCOL_SIT_VARIANT].isEmpty()) { // if there is a variant, we need an underscore |
| 405 | s->locale.append("_" , status); |
| 406 | } |
| 407 | // add variant, if there |
| 408 | if(!s->locElements[UCOL_SIT_VARIANT].isEmpty()) { |
| 409 | s->locale.append("_" , status); |
| 410 | s->locale.append(s->locElements[UCOL_SIT_VARIANT], status); |
| 411 | } |
| 412 | |
| 413 | // if there is a collation keyword, add that too |
| 414 | if(!s->locElements[UCOL_SIT_KEYWORD].isEmpty()) { |
| 415 | s->locale.append(collationKeyword, status); |
| 416 | s->locale.append(s->locElements[UCOL_SIT_KEYWORD], status); |
| 417 | } |
| 418 | |
| 419 | // if there is a provider keyword, add that too |
| 420 | if(!s->locElements[UCOL_SIT_PROVIDER].isEmpty()) { |
| 421 | s->locale.append(providerKeyword, status); |
| 422 | s->locale.append(s->locElements[UCOL_SIT_PROVIDER], status); |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | |
| 428 | U_CAPI void U_EXPORT2 |
| 429 | ucol_prepareShortStringOpen( const char *definition, |
| 430 | UBool, |
| 431 | UParseError *parseError, |
| 432 | UErrorCode *status) |
| 433 | { |
| 434 | if(U_FAILURE(*status)) return; |
| 435 | |
| 436 | UParseError internalParseError; |
| 437 | |
| 438 | if(!parseError) { |
| 439 | parseError = &internalParseError; |
| 440 | } |
| 441 | parseError->line = 0; |
| 442 | parseError->offset = 0; |
| 443 | parseError->preContext[0] = 0; |
| 444 | parseError->postContext[0] = 0; |
| 445 | |
| 446 | |
| 447 | // first we want to pick stuff out of short string. |
| 448 | // we'll end up with an UCA version, locale and a bunch of |
| 449 | // settings |
| 450 | |
| 451 | // analyse the string in order to get everything we need. |
| 452 | CollatorSpec s; |
| 453 | ucol_sit_readSpecs(&s, definition, parseError, status); |
| 454 | ucol_sit_calculateWholeLocale(&s, *status); |
| 455 | |
| 456 | char buffer[internalBufferSize]; |
| 457 | uprv_memset(buffer, 0, internalBufferSize); |
| 458 | uloc_canonicalize(s.locale.data(), buffer, internalBufferSize, status); |
| 459 | |
| 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; |
| 464 | char keyBuffer[256]; |
| 465 | // if there is a keyword, we pick it up and try to get elements |
| 466 | int32_t keyLen = uloc_getKeywordValue(buffer, "collation" , keyBuffer, sizeof(keyBuffer), status); |
| 467 | // Treat too long a value as no keyword. |
| 468 | if(keyLen >= (int32_t)sizeof(keyBuffer)) { |
| 469 | keyLen = 0; |
| 470 | *status = U_ZERO_ERROR; |
| 471 | } |
| 472 | if(keyLen == 0) { |
| 473 | // no keyword |
| 474 | // we try to find the default setting, which will give us the keyword value |
| 475 | UResourceBundle *defaultColl = ures_getByKeyWithFallback(collations, "default" , NULL, status); |
| 476 | if(U_SUCCESS(*status)) { |
| 477 | int32_t defaultKeyLen = 0; |
| 478 | const UChar *defaultKey = ures_getString(defaultColl, &defaultKeyLen, status); |
| 479 | u_UCharsToChars(defaultKey, keyBuffer, defaultKeyLen); |
| 480 | keyBuffer[defaultKeyLen] = 0; |
| 481 | } else { |
| 482 | *status = U_INTERNAL_PROGRAM_ERROR; |
| 483 | return; |
| 484 | } |
| 485 | ures_close(defaultColl); |
| 486 | } |
| 487 | collElem = ures_getByKeyWithFallback(collations, keyBuffer, collElem, status); |
| 488 | ures_close(collElem); |
| 489 | ures_close(collations); |
| 490 | ures_close(b); |
| 491 | } |
| 492 | |
| 493 | |
| 494 | U_CAPI UCollator* U_EXPORT2 |
| 495 | ucol_openFromShortString( const char *definition, |
| 496 | UBool forceDefaults, |
| 497 | UParseError *parseError, |
| 498 | UErrorCode *status) |
| 499 | { |
| 500 | UTRACE_ENTRY_OC(UTRACE_UCOL_OPEN_FROM_SHORT_STRING); |
| 501 | UTRACE_DATA1(UTRACE_INFO, "short string = \"%s\"" , definition); |
| 502 | |
| 503 | if(U_FAILURE(*status)) return 0; |
| 504 | |
| 505 | UParseError internalParseError; |
| 506 | |
| 507 | if(!parseError) { |
| 508 | parseError = &internalParseError; |
| 509 | } |
| 510 | parseError->line = 0; |
| 511 | parseError->offset = 0; |
| 512 | parseError->preContext[0] = 0; |
| 513 | parseError->postContext[0] = 0; |
| 514 | |
| 515 | |
| 516 | // first we want to pick stuff out of short string. |
| 517 | // we'll end up with an UCA version, locale and a bunch of |
| 518 | // settings |
| 519 | |
| 520 | // analyse the string in order to get everything we need. |
| 521 | const char *string = definition; |
| 522 | CollatorSpec s; |
| 523 | string = ucol_sit_readSpecs(&s, definition, parseError, status); |
| 524 | ucol_sit_calculateWholeLocale(&s, *status); |
| 525 | |
| 526 | char buffer[internalBufferSize]; |
| 527 | uprv_memset(buffer, 0, internalBufferSize); |
| 528 | #ifdef UCOL_TRACE_SIT |
| 529 | fprintf(stderr, "DEF %s, DATA %s, ERR %s\n" , definition, s.locale.data(), u_errorName(*status)); |
| 530 | #endif |
| 531 | uloc_canonicalize(s.locale.data(), buffer, internalBufferSize, status); |
| 532 | |
| 533 | UCollator *result = ucol_open(buffer, status); |
| 534 | int32_t i = 0; |
| 535 | |
| 536 | for(i = 0; i < UCOL_ATTRIBUTE_COUNT; i++) { |
| 537 | if(s.options[i] != UCOL_DEFAULT) { |
| 538 | if(forceDefaults || ucol_getAttribute(result, (UColAttribute)i, status) != s.options[i]) { |
| 539 | ucol_setAttribute(result, (UColAttribute)i, s.options[i], status); |
| 540 | } |
| 541 | |
| 542 | if(U_FAILURE(*status)) { |
| 543 | parseError->offset = (int32_t)(string - definition); |
| 544 | ucol_close(result); |
| 545 | return NULL; |
| 546 | } |
| 547 | |
| 548 | } |
| 549 | } |
| 550 | if(s.variableTopSet) { |
| 551 | if(s.variableTopString[0]) { |
| 552 | ucol_setVariableTop(result, s.variableTopString, s.variableTopStringLen, status); |
| 553 | } else { // we set by value, using 'B' |
| 554 | ucol_restoreVariableTop(result, s.variableTopValue, status); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | |
| 559 | if(U_FAILURE(*status)) { // here it can only be a bogus value |
| 560 | ucol_close(result); |
| 561 | result = NULL; |
| 562 | } |
| 563 | |
| 564 | UTRACE_EXIT_PTR_STATUS(result, *status); |
| 565 | return result; |
| 566 | } |
| 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 | { |
| 576 | if(U_FAILURE(*status)) return 0; |
| 577 | if(coll == NULL) { |
| 578 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
| 579 | return 0; |
| 580 | } |
| 581 | return ((icu::Collator*)coll)->internalGetShortDefinitionString(locale,dst,capacity,*status); |
| 582 | } |
| 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 | { |
| 591 | |
| 592 | if(U_FAILURE(*status)) { |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | if(destination) { |
| 597 | uprv_memset(destination, 0, capacity*sizeof(char)); |
| 598 | } |
| 599 | |
| 600 | UParseError pe; |
| 601 | if(!parseError) { |
| 602 | parseError = &pe; |
| 603 | } |
| 604 | |
| 605 | // validate |
| 606 | CollatorSpec s; |
| 607 | ucol_sit_readSpecs(&s, definition, parseError, status); |
| 608 | return ucol_sit_dumpSpecs(&s, destination, capacity, status); |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Get a set containing the contractions defined by the collator. The set includes |
| 613 | * both the UCA contractions and the contractions defined by the collator |
| 614 | * @param coll collator |
| 615 | * @param conts the set to hold the result |
| 616 | * @param status to hold the error code |
| 617 | * @return the size of the contraction set |
| 618 | */ |
| 619 | U_CAPI int32_t U_EXPORT2 |
| 620 | ucol_getContractions( const UCollator *coll, |
| 621 | USet *contractions, |
| 622 | UErrorCode *status) |
| 623 | { |
| 624 | ucol_getContractionsAndExpansions(coll, contractions, NULL, FALSE, status); |
| 625 | return uset_getItemCount(contractions); |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * Get a set containing the expansions defined by the collator. The set includes |
| 630 | * both the UCA expansions and the expansions defined by the tailoring |
| 631 | * @param coll collator |
| 632 | * @param conts the set to hold the result |
| 633 | * @param addPrefixes add the prefix contextual elements to contractions |
| 634 | * @param status to hold the error code |
| 635 | * |
| 636 | * @draft ICU 3.4 |
| 637 | */ |
| 638 | U_CAPI void U_EXPORT2 |
| 639 | ucol_getContractionsAndExpansions( const UCollator *coll, |
| 640 | USet *contractions, |
| 641 | USet *expansions, |
| 642 | UBool addPrefixes, |
| 643 | UErrorCode *status) |
| 644 | { |
| 645 | if(U_FAILURE(*status)) { |
| 646 | return; |
| 647 | } |
| 648 | if(coll == NULL) { |
| 649 | *status = U_ILLEGAL_ARGUMENT_ERROR; |
| 650 | return; |
| 651 | } |
| 652 | const icu::RuleBasedCollator *rbc = icu::RuleBasedCollator::rbcFromUCollator(coll); |
| 653 | if(rbc == NULL) { |
| 654 | *status = U_UNSUPPORTED_ERROR; |
| 655 | return; |
| 656 | } |
| 657 | rbc->internalGetContractionsAndExpansions( |
| 658 | icu::UnicodeSet::fromUSet(contractions), |
| 659 | icu::UnicodeSet::fromUSet(expansions), |
| 660 | addPrefixes, *status); |
| 661 | } |
| 662 | #endif |
| 663 | |