michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2004-2012, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: * file name: ucol_sit.cpp michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * Modification history michael@0: * Date Name Comments michael@0: * 03/12/2004 weiv Creation michael@0: */ michael@0: michael@0: #include "unicode/ustring.h" michael@0: #include "unicode/udata.h" michael@0: michael@0: #include "utracimp.h" michael@0: #include "ucol_imp.h" michael@0: #include "ucol_tok.h" michael@0: #include "cmemory.h" michael@0: #include "cstring.h" michael@0: #include "uresimp.h" michael@0: #include "unicode/coll.h" michael@0: michael@0: #ifdef UCOL_TRACE_SIT michael@0: # include michael@0: #endif michael@0: michael@0: #if !UCONFIG_NO_COLLATION michael@0: michael@0: enum OptionsList { michael@0: UCOL_SIT_LANGUAGE = 0, michael@0: UCOL_SIT_SCRIPT = 1, michael@0: UCOL_SIT_REGION = 2, michael@0: UCOL_SIT_VARIANT = 3, michael@0: UCOL_SIT_KEYWORD = 4, michael@0: UCOL_SIT_PROVIDER = 5, michael@0: UCOL_SIT_LOCELEMENT_MAX = UCOL_SIT_PROVIDER, /* the last element that's part of LocElements */ michael@0: michael@0: UCOL_SIT_BCP47, michael@0: UCOL_SIT_STRENGTH, michael@0: UCOL_SIT_CASE_LEVEL, michael@0: UCOL_SIT_CASE_FIRST, michael@0: UCOL_SIT_NUMERIC_COLLATION, michael@0: UCOL_SIT_ALTERNATE_HANDLING, michael@0: UCOL_SIT_NORMALIZATION_MODE, michael@0: UCOL_SIT_FRENCH_COLLATION, michael@0: UCOL_SIT_HIRAGANA_QUATERNARY, michael@0: UCOL_SIT_VARIABLE_TOP, michael@0: UCOL_SIT_VARIABLE_TOP_VALUE, michael@0: UCOL_SIT_ITEMS_COUNT michael@0: }; michael@0: michael@0: /* option starters chars. */ michael@0: static const char alternateHArg = 'A'; michael@0: static const char variableTopValArg = 'B'; michael@0: static const char caseFirstArg = 'C'; michael@0: static const char numericCollArg = 'D'; michael@0: static const char caseLevelArg = 'E'; michael@0: static const char frenchCollArg = 'F'; michael@0: static const char hiraganaQArg = 'H'; michael@0: static const char keywordArg = 'K'; michael@0: static const char languageArg = 'L'; michael@0: static const char normArg = 'N'; michael@0: static const char providerArg = 'P'; michael@0: static const char regionArg = 'R'; michael@0: static const char strengthArg = 'S'; michael@0: static const char variableTopArg = 'T'; michael@0: static const char variantArg = 'V'; michael@0: static const char RFC3066Arg = 'X'; michael@0: static const char scriptArg = 'Z'; michael@0: michael@0: static const char collationKeyword[] = "@collation="; michael@0: static const char providerKeyword[] = "@sp="; michael@0: michael@0: michael@0: static const int32_t locElementCount = UCOL_SIT_LOCELEMENT_MAX+1; michael@0: static const int32_t locElementCapacity = 32; michael@0: static const int32_t loc3066Capacity = 256; michael@0: static const int32_t locProviderCapacity = 10; michael@0: static const int32_t internalBufferSize = 512; michael@0: michael@0: /* structure containing specification of a collator. Initialized michael@0: * from a short string. Also used to construct a short string from a michael@0: * collator instance michael@0: */ michael@0: struct CollatorSpec { michael@0: char locElements[locElementCount][locElementCapacity]; michael@0: char locale[loc3066Capacity]; michael@0: char provider[locProviderCapacity]; michael@0: UColAttributeValue options[UCOL_ATTRIBUTE_COUNT]; michael@0: uint32_t variableTopValue; michael@0: UChar variableTopString[locElementCapacity]; michael@0: int32_t variableTopStringLen; michael@0: UBool variableTopSet; michael@0: struct { michael@0: const char *start; michael@0: int32_t len; michael@0: } entries[UCOL_SIT_ITEMS_COUNT]; michael@0: }; michael@0: michael@0: michael@0: /* structure for converting between character attribute michael@0: * representation and real collation attribute value. michael@0: */ michael@0: struct AttributeConversion { michael@0: char letter; michael@0: UColAttributeValue value; michael@0: }; michael@0: michael@0: static const AttributeConversion conversions[12] = { michael@0: { '1', UCOL_PRIMARY }, michael@0: { '2', UCOL_SECONDARY }, michael@0: { '3', UCOL_TERTIARY }, michael@0: { '4', UCOL_QUATERNARY }, michael@0: { 'D', UCOL_DEFAULT }, michael@0: { 'I', UCOL_IDENTICAL }, michael@0: { 'L', UCOL_LOWER_FIRST }, michael@0: { 'N', UCOL_NON_IGNORABLE }, michael@0: { 'O', UCOL_ON }, michael@0: { 'S', UCOL_SHIFTED }, michael@0: { 'U', UCOL_UPPER_FIRST }, michael@0: { 'X', UCOL_OFF } michael@0: }; michael@0: michael@0: michael@0: static char michael@0: ucol_sit_attributeValueToLetter(UColAttributeValue value, UErrorCode *status) { michael@0: uint32_t i = 0; michael@0: for(i = 0; i < sizeof(conversions)/sizeof(conversions[0]); i++) { michael@0: if(conversions[i].value == value) { michael@0: return conversions[i].letter; michael@0: } michael@0: } michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: #ifdef UCOL_TRACE_SIT michael@0: fprintf(stderr, "%s:%d: unknown UColAttributeValue %d: %s\n", __FILE__, __LINE__, value, u_errorName(*status)); michael@0: #endif michael@0: return 0; michael@0: } michael@0: michael@0: static UColAttributeValue michael@0: ucol_sit_letterToAttributeValue(char letter, UErrorCode *status) { michael@0: uint32_t i = 0; michael@0: for(i = 0; i < sizeof(conversions)/sizeof(conversions[0]); i++) { michael@0: if(conversions[i].letter == letter) { michael@0: return conversions[i].value; michael@0: } michael@0: } michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: #ifdef UCOL_TRACE_SIT michael@0: fprintf(stderr, "%s:%d: unknown letter %c: %s\n", __FILE__, __LINE__, letter, u_errorName(*status)); michael@0: #endif michael@0: return UCOL_DEFAULT; michael@0: } michael@0: michael@0: /* function prototype for functions used to parse a short string */ michael@0: U_CDECL_BEGIN michael@0: typedef const char* U_CALLCONV michael@0: ActionFunction(CollatorSpec *spec, uint32_t value1, const char* string, michael@0: UErrorCode *status); michael@0: U_CDECL_END michael@0: michael@0: U_CDECL_BEGIN michael@0: static const char* U_CALLCONV michael@0: _processLocaleElement(CollatorSpec *spec, uint32_t value, const char* string, michael@0: UErrorCode *status) michael@0: { michael@0: int32_t len = 0; michael@0: do { michael@0: if(value == UCOL_SIT_LANGUAGE || value == UCOL_SIT_KEYWORD || value == UCOL_SIT_PROVIDER) { michael@0: spec->locElements[value][len++] = uprv_tolower(*string); michael@0: } else { michael@0: spec->locElements[value][len++] = *string; michael@0: } michael@0: } while(*(++string) != '_' && *string && len < locElementCapacity); michael@0: if(len >= locElementCapacity) { michael@0: *status = U_BUFFER_OVERFLOW_ERROR; michael@0: return string; michael@0: } michael@0: // don't skip the underscore at the end michael@0: return string; michael@0: } michael@0: U_CDECL_END michael@0: michael@0: U_CDECL_BEGIN michael@0: static const char* U_CALLCONV michael@0: _processRFC3066Locale(CollatorSpec *spec, uint32_t, const char* string, michael@0: UErrorCode *status) michael@0: { michael@0: char terminator = *string; michael@0: string++; michael@0: const char *end = uprv_strchr(string+1, terminator); michael@0: if(end == NULL || end - string >= loc3066Capacity) { michael@0: *status = U_BUFFER_OVERFLOW_ERROR; michael@0: return string; michael@0: } else { michael@0: uprv_strncpy(spec->locale, string, end-string); michael@0: return end+1; michael@0: } michael@0: } michael@0: michael@0: U_CDECL_END michael@0: michael@0: U_CDECL_BEGIN michael@0: static const char* U_CALLCONV michael@0: _processCollatorOption(CollatorSpec *spec, uint32_t option, const char* string, michael@0: UErrorCode *status) michael@0: { michael@0: spec->options[option] = ucol_sit_letterToAttributeValue(*string, status); michael@0: if((*(++string) != '_' && *string) || U_FAILURE(*status)) { michael@0: #ifdef UCOL_TRACE_SIT michael@0: fprintf(stderr, "%s:%d: unknown collator option at '%s': %s\n", __FILE__, __LINE__, string, u_errorName(*status)); michael@0: #endif michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: } michael@0: return string; michael@0: } michael@0: U_CDECL_END michael@0: michael@0: michael@0: static UChar michael@0: readHexCodeUnit(const char **string, UErrorCode *status) michael@0: { michael@0: UChar result = 0; michael@0: int32_t value = 0; michael@0: char c; michael@0: int32_t noDigits = 0; michael@0: while((c = **string) != 0 && noDigits < 4) { michael@0: if( c >= '0' && c <= '9') { michael@0: value = c - '0'; michael@0: } else if ( c >= 'a' && c <= 'f') { michael@0: value = c - 'a' + 10; michael@0: } else if ( c >= 'A' && c <= 'F') { michael@0: value = c - 'A' + 10; michael@0: } else { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: #ifdef UCOL_TRACE_SIT michael@0: fprintf(stderr, "%s:%d: Bad hex char at '%s': %s\n", __FILE__, __LINE__, *string, u_errorName(*status)); michael@0: #endif michael@0: return 0; michael@0: } michael@0: result = (result << 4) | (UChar)value; michael@0: noDigits++; michael@0: (*string)++; michael@0: } michael@0: // if the string was terminated before we read 4 digits, set an error michael@0: if(noDigits < 4) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: #ifdef UCOL_TRACE_SIT michael@0: fprintf(stderr, "%s:%d: Short (only %d digits, wanted 4) at '%s': %s\n", __FILE__, __LINE__, noDigits,*string, u_errorName(*status)); michael@0: #endif michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: U_CDECL_BEGIN michael@0: static const char* U_CALLCONV michael@0: _processVariableTop(CollatorSpec *spec, uint32_t value1, const char* string, UErrorCode *status) michael@0: { michael@0: // get four digits michael@0: int32_t i = 0; michael@0: if(!value1) { michael@0: while(U_SUCCESS(*status) && i < locElementCapacity && *string != 0 && *string != '_') { michael@0: spec->variableTopString[i++] = readHexCodeUnit(&string, status); michael@0: } michael@0: spec->variableTopStringLen = i; michael@0: if(i == locElementCapacity && *string != 0 && *string != '_') { michael@0: *status = U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: } else { michael@0: spec->variableTopValue = readHexCodeUnit(&string, status); michael@0: } michael@0: if(U_SUCCESS(*status)) { michael@0: spec->variableTopSet = TRUE; michael@0: } michael@0: return string; michael@0: } michael@0: U_CDECL_END michael@0: michael@0: michael@0: /* Table for parsing short strings */ michael@0: struct ShortStringOptions { michael@0: char optionStart; michael@0: ActionFunction *action; michael@0: uint32_t attr; michael@0: }; michael@0: michael@0: static const ShortStringOptions options[UCOL_SIT_ITEMS_COUNT] = michael@0: { michael@0: /* 10 ALTERNATE_HANDLING */ {alternateHArg, _processCollatorOption, UCOL_ALTERNATE_HANDLING }, // alternate N, S, D michael@0: /* 15 VARIABLE_TOP_VALUE */ {variableTopValArg, _processVariableTop, 1 }, michael@0: /* 08 CASE_FIRST */ {caseFirstArg, _processCollatorOption, UCOL_CASE_FIRST }, // case first L, U, X, D michael@0: /* 09 NUMERIC_COLLATION */ {numericCollArg, _processCollatorOption, UCOL_NUMERIC_COLLATION }, // codan O, X, D michael@0: /* 07 CASE_LEVEL */ {caseLevelArg, _processCollatorOption, UCOL_CASE_LEVEL }, // case level O, X, D michael@0: /* 12 FRENCH_COLLATION */ {frenchCollArg, _processCollatorOption, UCOL_FRENCH_COLLATION }, // french O, X, D michael@0: /* 13 HIRAGANA_QUATERNARY] */ {hiraganaQArg, _processCollatorOption, UCOL_HIRAGANA_QUATERNARY_MODE }, // hiragana O, X, D michael@0: /* 04 KEYWORD */ {keywordArg, _processLocaleElement, UCOL_SIT_KEYWORD }, // keyword michael@0: /* 00 LANGUAGE */ {languageArg, _processLocaleElement, UCOL_SIT_LANGUAGE }, // language michael@0: /* 11 NORMALIZATION_MODE */ {normArg, _processCollatorOption, UCOL_NORMALIZATION_MODE }, // norm O, X, D michael@0: /* 02 REGION */ {regionArg, _processLocaleElement, UCOL_SIT_REGION }, // region michael@0: /* 06 STRENGTH */ {strengthArg, _processCollatorOption, UCOL_STRENGTH }, // strength 1, 2, 3, 4, I, D michael@0: /* 14 VARIABLE_TOP */ {variableTopArg, _processVariableTop, 0 }, michael@0: /* 03 VARIANT */ {variantArg, _processLocaleElement, UCOL_SIT_VARIANT }, // variant michael@0: /* 05 RFC3066BIS */ {RFC3066Arg, _processRFC3066Locale, 0 }, // rfc3066bis locale name michael@0: /* 01 SCRIPT */ {scriptArg, _processLocaleElement, UCOL_SIT_SCRIPT }, // script michael@0: /* PROVIDER */ {providerArg, _processLocaleElement, UCOL_SIT_PROVIDER } michael@0: }; michael@0: michael@0: michael@0: static michael@0: const char* ucol_sit_readOption(const char *start, CollatorSpec *spec, michael@0: UErrorCode *status) michael@0: { michael@0: int32_t i = 0; michael@0: michael@0: for(i = 0; i < UCOL_SIT_ITEMS_COUNT; i++) { michael@0: if(*start == options[i].optionStart) { michael@0: spec->entries[i].start = start; michael@0: const char* end = options[i].action(spec, options[i].attr, start+1, status); michael@0: spec->entries[i].len = (int32_t)(end - start); michael@0: return end; michael@0: } michael@0: } michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: #ifdef UCOL_TRACE_SIT michael@0: fprintf(stderr, "%s:%d: Unknown option at '%s': %s\n", __FILE__, __LINE__, start, u_errorName(*status)); michael@0: #endif michael@0: return start; michael@0: } michael@0: michael@0: static michael@0: void ucol_sit_initCollatorSpecs(CollatorSpec *spec) michael@0: { michael@0: // reset everything michael@0: uprv_memset(spec, 0, sizeof(CollatorSpec)); michael@0: // set collation options to default michael@0: int32_t i = 0; michael@0: for(i = 0; i < UCOL_ATTRIBUTE_COUNT; i++) { michael@0: spec->options[i] = UCOL_DEFAULT; michael@0: } michael@0: } michael@0: michael@0: static const char* michael@0: ucol_sit_readSpecs(CollatorSpec *s, const char *string, michael@0: UParseError *parseError, UErrorCode *status) michael@0: { michael@0: const char *definition = string; michael@0: while(U_SUCCESS(*status) && *string) { michael@0: string = ucol_sit_readOption(string, s, status); michael@0: // advance over '_' michael@0: while(*string && *string == '_') { michael@0: string++; michael@0: } michael@0: } michael@0: if(U_FAILURE(*status)) { michael@0: parseError->offset = (int32_t)(string - definition); michael@0: } michael@0: return string; michael@0: } michael@0: michael@0: static michael@0: int32_t ucol_sit_dumpSpecs(CollatorSpec *s, char *destination, int32_t capacity, UErrorCode *status) michael@0: { michael@0: int32_t i = 0, j = 0; michael@0: int32_t len = 0; michael@0: char optName; michael@0: if(U_SUCCESS(*status)) { michael@0: for(i = 0; i < UCOL_SIT_ITEMS_COUNT; i++) { michael@0: if(s->entries[i].start) { michael@0: if(len) { michael@0: if(len < capacity) { michael@0: uprv_strcat(destination, "_"); michael@0: } michael@0: len++; michael@0: } michael@0: optName = *(s->entries[i].start); michael@0: if(optName == languageArg || optName == regionArg || optName == variantArg || optName == keywordArg) { michael@0: for(j = 0; j < s->entries[i].len; j++) { michael@0: if(len + j < capacity) { michael@0: destination[len+j] = uprv_toupper(*(s->entries[i].start+j)); michael@0: } michael@0: } michael@0: len += s->entries[i].len; michael@0: } else { michael@0: len += s->entries[i].len; michael@0: if(len < capacity) { michael@0: uprv_strncat(destination,s->entries[i].start, s->entries[i].len); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: return len; michael@0: } else { michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: ucol_sit_calculateWholeLocale(CollatorSpec *s) { michael@0: // put the locale together, unless we have a done michael@0: // locale michael@0: if(s->locale[0] == 0) { michael@0: // first the language michael@0: uprv_strcat(s->locale, s->locElements[UCOL_SIT_LANGUAGE]); michael@0: // then the script, if present michael@0: if(*(s->locElements[UCOL_SIT_SCRIPT])) { michael@0: uprv_strcat(s->locale, "_"); michael@0: uprv_strcat(s->locale, s->locElements[UCOL_SIT_SCRIPT]); michael@0: } michael@0: // then the region, if present michael@0: if(*(s->locElements[UCOL_SIT_REGION])) { michael@0: uprv_strcat(s->locale, "_"); michael@0: uprv_strcat(s->locale, s->locElements[UCOL_SIT_REGION]); michael@0: } else if(*(s->locElements[UCOL_SIT_VARIANT])) { // if there is a variant, we need an underscore michael@0: uprv_strcat(s->locale, "_"); michael@0: } michael@0: // add variant, if there michael@0: if(*(s->locElements[UCOL_SIT_VARIANT])) { michael@0: uprv_strcat(s->locale, "_"); michael@0: uprv_strcat(s->locale, s->locElements[UCOL_SIT_VARIANT]); michael@0: } michael@0: michael@0: // if there is a collation keyword, add that too michael@0: if(*(s->locElements[UCOL_SIT_KEYWORD])) { michael@0: uprv_strcat(s->locale, collationKeyword); michael@0: uprv_strcat(s->locale, s->locElements[UCOL_SIT_KEYWORD]); michael@0: } michael@0: michael@0: // if there is a provider keyword, add that too michael@0: if(*(s->locElements[UCOL_SIT_PROVIDER])) { michael@0: uprv_strcat(s->locale, providerKeyword); michael@0: uprv_strcat(s->locale, s->locElements[UCOL_SIT_PROVIDER]); michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: ucol_prepareShortStringOpen( const char *definition, michael@0: UBool, michael@0: UParseError *parseError, michael@0: UErrorCode *status) michael@0: { michael@0: if(U_FAILURE(*status)) return; michael@0: michael@0: UParseError internalParseError; michael@0: michael@0: if(!parseError) { michael@0: parseError = &internalParseError; michael@0: } michael@0: parseError->line = 0; michael@0: parseError->offset = 0; michael@0: parseError->preContext[0] = 0; michael@0: parseError->postContext[0] = 0; michael@0: michael@0: michael@0: // first we want to pick stuff out of short string. michael@0: // we'll end up with an UCA version, locale and a bunch of michael@0: // settings michael@0: michael@0: // analyse the string in order to get everything we need. michael@0: CollatorSpec s; michael@0: ucol_sit_initCollatorSpecs(&s); michael@0: ucol_sit_readSpecs(&s, definition, parseError, status); michael@0: ucol_sit_calculateWholeLocale(&s); michael@0: michael@0: char buffer[internalBufferSize]; michael@0: uprv_memset(buffer, 0, internalBufferSize); michael@0: uloc_canonicalize(s.locale, buffer, internalBufferSize, status); michael@0: michael@0: UResourceBundle *b = ures_open(U_ICUDATA_COLL, buffer, status); michael@0: /* we try to find stuff from keyword */ michael@0: UResourceBundle *collations = ures_getByKey(b, "collations", NULL, status); michael@0: UResourceBundle *collElem = NULL; michael@0: char keyBuffer[256]; michael@0: // if there is a keyword, we pick it up and try to get elements michael@0: if(!uloc_getKeywordValue(buffer, "collation", keyBuffer, 256, status)) { michael@0: // no keyword. we try to find the default setting, which will give us the keyword value michael@0: UResourceBundle *defaultColl = ures_getByKeyWithFallback(collations, "default", NULL, status); michael@0: if(U_SUCCESS(*status)) { michael@0: int32_t defaultKeyLen = 0; michael@0: const UChar *defaultKey = ures_getString(defaultColl, &defaultKeyLen, status); michael@0: u_UCharsToChars(defaultKey, keyBuffer, defaultKeyLen); michael@0: keyBuffer[defaultKeyLen] = 0; michael@0: } else { michael@0: *status = U_INTERNAL_PROGRAM_ERROR; michael@0: return; michael@0: } michael@0: ures_close(defaultColl); michael@0: } michael@0: collElem = ures_getByKeyWithFallback(collations, keyBuffer, collElem, status); michael@0: ures_close(collElem); michael@0: ures_close(collations); michael@0: ures_close(b); michael@0: } michael@0: michael@0: michael@0: U_CAPI UCollator* U_EXPORT2 michael@0: ucol_openFromShortString( const char *definition, michael@0: UBool forceDefaults, michael@0: UParseError *parseError, michael@0: UErrorCode *status) michael@0: { michael@0: UTRACE_ENTRY_OC(UTRACE_UCOL_OPEN_FROM_SHORT_STRING); michael@0: UTRACE_DATA1(UTRACE_INFO, "short string = \"%s\"", definition); michael@0: michael@0: if(U_FAILURE(*status)) return 0; michael@0: michael@0: UParseError internalParseError; michael@0: michael@0: if(!parseError) { michael@0: parseError = &internalParseError; michael@0: } michael@0: parseError->line = 0; michael@0: parseError->offset = 0; michael@0: parseError->preContext[0] = 0; michael@0: parseError->postContext[0] = 0; michael@0: michael@0: michael@0: // first we want to pick stuff out of short string. michael@0: // we'll end up with an UCA version, locale and a bunch of michael@0: // settings michael@0: michael@0: // analyse the string in order to get everything we need. michael@0: const char *string = definition; michael@0: CollatorSpec s; michael@0: ucol_sit_initCollatorSpecs(&s); michael@0: string = ucol_sit_readSpecs(&s, definition, parseError, status); michael@0: ucol_sit_calculateWholeLocale(&s); michael@0: michael@0: char buffer[internalBufferSize]; michael@0: uprv_memset(buffer, 0, internalBufferSize); michael@0: uloc_canonicalize(s.locale, buffer, internalBufferSize, status); michael@0: michael@0: UCollator *result = ucol_open(buffer, status); michael@0: int32_t i = 0; michael@0: michael@0: for(i = 0; i < UCOL_ATTRIBUTE_COUNT; i++) { michael@0: if(s.options[i] != UCOL_DEFAULT) { michael@0: if(forceDefaults || ucol_getAttribute(result, (UColAttribute)i, status) != s.options[i]) { michael@0: ucol_setAttribute(result, (UColAttribute)i, s.options[i], status); michael@0: } michael@0: michael@0: if(U_FAILURE(*status)) { michael@0: parseError->offset = (int32_t)(string - definition); michael@0: ucol_close(result); michael@0: return NULL; michael@0: } michael@0: michael@0: } michael@0: } michael@0: if(s.variableTopSet) { michael@0: if(s.variableTopString[0]) { michael@0: ucol_setVariableTop(result, s.variableTopString, s.variableTopStringLen, status); michael@0: } else { // we set by value, using 'B' michael@0: ucol_restoreVariableTop(result, s.variableTopValue, status); michael@0: } michael@0: } michael@0: michael@0: michael@0: if(U_FAILURE(*status)) { // here it can only be a bogus value michael@0: ucol_close(result); michael@0: result = NULL; michael@0: } michael@0: michael@0: UTRACE_EXIT_PTR_STATUS(result, *status); michael@0: return result; michael@0: } michael@0: michael@0: michael@0: static void appendShortStringElement(const char *src, int32_t len, char *result, int32_t *resultSize, int32_t capacity, char arg) michael@0: { michael@0: if(len) { michael@0: if(*resultSize) { michael@0: if(*resultSize < capacity) { michael@0: uprv_strcat(result, "_"); michael@0: } michael@0: (*resultSize)++; michael@0: } michael@0: *resultSize += len + 1; michael@0: if(*resultSize < capacity) { michael@0: uprv_strncat(result, &arg, 1); michael@0: uprv_strncat(result, src, len); michael@0: } michael@0: } michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucol_getShortDefinitionString(const UCollator *coll, michael@0: const char *locale, michael@0: char *dst, michael@0: int32_t capacity, michael@0: UErrorCode *status) michael@0: { michael@0: if(U_FAILURE(*status)) return 0; michael@0: if(coll->delegate != NULL) { michael@0: return ((icu::Collator*)coll->delegate)->internalGetShortDefinitionString(locale,dst,capacity,*status); michael@0: } michael@0: char buffer[internalBufferSize]; michael@0: uprv_memset(buffer, 0, internalBufferSize*sizeof(char)); michael@0: int32_t resultSize = 0; michael@0: char tempbuff[internalBufferSize]; michael@0: char locBuff[internalBufferSize]; michael@0: uprv_memset(buffer, 0, internalBufferSize*sizeof(char)); michael@0: int32_t elementSize = 0; michael@0: UBool isAvailable = 0; michael@0: CollatorSpec s; michael@0: ucol_sit_initCollatorSpecs(&s); michael@0: michael@0: if(!locale) { michael@0: locale = ucol_getLocaleByType(coll, ULOC_VALID_LOCALE, status); michael@0: } michael@0: elementSize = ucol_getFunctionalEquivalent(locBuff, internalBufferSize, "collation", locale, &isAvailable, status); michael@0: michael@0: if(elementSize) { michael@0: // we should probably canonicalize here... michael@0: elementSize = uloc_getLanguage(locBuff, tempbuff, internalBufferSize, status); michael@0: appendShortStringElement(tempbuff, elementSize, buffer, &resultSize, /*capacity*/internalBufferSize, languageArg); michael@0: elementSize = uloc_getCountry(locBuff, tempbuff, internalBufferSize, status); michael@0: appendShortStringElement(tempbuff, elementSize, buffer, &resultSize, /*capacity*/internalBufferSize, regionArg); michael@0: elementSize = uloc_getScript(locBuff, tempbuff, internalBufferSize, status); michael@0: appendShortStringElement(tempbuff, elementSize, buffer, &resultSize, /*capacity*/internalBufferSize, scriptArg); michael@0: elementSize = uloc_getVariant(locBuff, tempbuff, internalBufferSize, status); michael@0: appendShortStringElement(tempbuff, elementSize, buffer, &resultSize, /*capacity*/internalBufferSize, variantArg); michael@0: elementSize = uloc_getKeywordValue(locBuff, "collation", tempbuff, internalBufferSize, status); michael@0: appendShortStringElement(tempbuff, elementSize, buffer, &resultSize, /*capacity*/internalBufferSize, keywordArg); michael@0: } michael@0: michael@0: int32_t i = 0; michael@0: UColAttributeValue attribute = UCOL_DEFAULT; michael@0: for(i = 0; i < UCOL_SIT_ITEMS_COUNT; i++) { michael@0: if(options[i].action == _processCollatorOption) { michael@0: attribute = ucol_getAttributeOrDefault(coll, (UColAttribute)options[i].attr, status); michael@0: if(attribute != UCOL_DEFAULT) { michael@0: char letter = ucol_sit_attributeValueToLetter(attribute, status); michael@0: appendShortStringElement(&letter, 1, michael@0: buffer, &resultSize, /*capacity*/internalBufferSize, options[i].optionStart); michael@0: } michael@0: } michael@0: } michael@0: if(coll->variableTopValueisDefault == FALSE) { michael@0: //s.variableTopValue = ucol_getVariableTop(coll, status); michael@0: elementSize = T_CString_integerToString(tempbuff, coll->variableTopValue, 16); michael@0: appendShortStringElement(tempbuff, elementSize, buffer, &resultSize, capacity, variableTopValArg); michael@0: } michael@0: michael@0: UParseError parseError; michael@0: return ucol_normalizeShortDefinitionString(buffer, dst, capacity, &parseError, status); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucol_normalizeShortDefinitionString(const char *definition, michael@0: char *destination, michael@0: int32_t capacity, michael@0: UParseError *parseError, michael@0: UErrorCode *status) michael@0: { michael@0: michael@0: if(U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: michael@0: if(destination) { michael@0: uprv_memset(destination, 0, capacity*sizeof(char)); michael@0: } michael@0: michael@0: UParseError pe; michael@0: if(!parseError) { michael@0: parseError = &pe; michael@0: } michael@0: michael@0: // validate michael@0: CollatorSpec s; michael@0: ucol_sit_initCollatorSpecs(&s); michael@0: ucol_sit_readSpecs(&s, definition, parseError, status); michael@0: return ucol_sit_dumpSpecs(&s, destination, capacity, status); michael@0: } michael@0: michael@0: U_CAPI UColAttributeValue U_EXPORT2 michael@0: ucol_getAttributeOrDefault(const UCollator *coll, UColAttribute attr, UErrorCode *status) michael@0: { michael@0: if(U_FAILURE(*status) || coll == NULL) { michael@0: return UCOL_DEFAULT; michael@0: } michael@0: switch(attr) { michael@0: case UCOL_NUMERIC_COLLATION: michael@0: return coll->numericCollationisDefault?UCOL_DEFAULT:coll->numericCollation; michael@0: case UCOL_HIRAGANA_QUATERNARY_MODE: michael@0: return coll->hiraganaQisDefault?UCOL_DEFAULT:coll->hiraganaQ; michael@0: case UCOL_FRENCH_COLLATION: /* attribute for direction of secondary weights*/ michael@0: return coll->frenchCollationisDefault?UCOL_DEFAULT:coll->frenchCollation; michael@0: case UCOL_ALTERNATE_HANDLING: /* attribute for handling variable elements*/ michael@0: return coll->alternateHandlingisDefault?UCOL_DEFAULT:coll->alternateHandling; michael@0: case UCOL_CASE_FIRST: /* who goes first, lower case or uppercase */ michael@0: return coll->caseFirstisDefault?UCOL_DEFAULT:coll->caseFirst; michael@0: case UCOL_CASE_LEVEL: /* do we have an extra case level */ michael@0: return coll->caseLevelisDefault?UCOL_DEFAULT:coll->caseLevel; michael@0: case UCOL_NORMALIZATION_MODE: /* attribute for normalization */ michael@0: return coll->normalizationModeisDefault?UCOL_DEFAULT:coll->normalizationMode; michael@0: case UCOL_STRENGTH: /* attribute for strength */ michael@0: return coll->strengthisDefault?UCOL_DEFAULT:coll->strength; michael@0: case UCOL_ATTRIBUTE_COUNT: michael@0: default: michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: #ifdef UCOL_TRACE_SIT michael@0: fprintf(stderr, "%s:%d: Unknown attr value '%d': %s\n", __FILE__, __LINE__, (int)attr, u_errorName(*status)); michael@0: #endif michael@0: break; michael@0: } michael@0: return UCOL_DEFAULT; michael@0: } michael@0: michael@0: michael@0: struct contContext { michael@0: const UCollator *coll; michael@0: USet *conts; michael@0: USet *expansions; michael@0: USet *removedContractions; michael@0: UBool addPrefixes; michael@0: UErrorCode *status; michael@0: }; michael@0: michael@0: michael@0: michael@0: static void michael@0: addSpecial(contContext *context, UChar *buffer, int32_t bufLen, michael@0: uint32_t CE, int32_t leftIndex, int32_t rightIndex, UErrorCode *status) michael@0: { michael@0: const UCollator *coll = context->coll; michael@0: USet *contractions = context->conts; michael@0: USet *expansions = context->expansions; michael@0: UBool addPrefixes = context->addPrefixes; michael@0: michael@0: const UChar *UCharOffset = (UChar *)coll->image+getContractOffset(CE); michael@0: uint32_t newCE = *(coll->contractionCEs + (UCharOffset - coll->contractionIndex)); michael@0: // we might have a contraction that ends from previous level michael@0: if(newCE != UCOL_NOT_FOUND) { michael@0: if(isSpecial(CE) && getCETag(CE) == CONTRACTION_TAG && isSpecial(newCE) && getCETag(newCE) == SPEC_PROC_TAG && addPrefixes) { michael@0: addSpecial(context, buffer, bufLen, newCE, leftIndex, rightIndex, status); michael@0: } michael@0: if(contractions && rightIndex-leftIndex > 1) { michael@0: uset_addString(contractions, buffer+leftIndex, rightIndex-leftIndex); michael@0: if(expansions && isSpecial(CE) && getCETag(CE) == EXPANSION_TAG) { michael@0: uset_addString(expansions, buffer+leftIndex, rightIndex-leftIndex); michael@0: } michael@0: } michael@0: } michael@0: michael@0: UCharOffset++; michael@0: // check whether we're doing contraction or prefix michael@0: if(getCETag(CE) == SPEC_PROC_TAG && addPrefixes) { michael@0: if(leftIndex == 0) { michael@0: *status = U_INTERNAL_PROGRAM_ERROR; michael@0: return; michael@0: } michael@0: --leftIndex; michael@0: while(*UCharOffset != 0xFFFF) { michael@0: newCE = *(coll->contractionCEs + (UCharOffset - coll->contractionIndex)); michael@0: buffer[leftIndex] = *UCharOffset; michael@0: if(isSpecial(newCE) && (getCETag(newCE) == CONTRACTION_TAG || getCETag(newCE) == SPEC_PROC_TAG)) { michael@0: addSpecial(context, buffer, bufLen, newCE, leftIndex, rightIndex, status); michael@0: } else { michael@0: if(contractions) { michael@0: uset_addString(contractions, buffer+leftIndex, rightIndex-leftIndex); michael@0: } michael@0: if(expansions && isSpecial(newCE) && getCETag(newCE) == EXPANSION_TAG) { michael@0: uset_addString(expansions, buffer+leftIndex, rightIndex-leftIndex); michael@0: } michael@0: } michael@0: UCharOffset++; michael@0: } michael@0: } else if(getCETag(CE) == CONTRACTION_TAG) { michael@0: if(rightIndex == bufLen-1) { michael@0: *status = U_INTERNAL_PROGRAM_ERROR; michael@0: return; michael@0: } michael@0: while(*UCharOffset != 0xFFFF) { michael@0: newCE = *(coll->contractionCEs + (UCharOffset - coll->contractionIndex)); michael@0: buffer[rightIndex] = *UCharOffset; michael@0: if(isSpecial(newCE) && (getCETag(newCE) == CONTRACTION_TAG || getCETag(newCE) == SPEC_PROC_TAG)) { michael@0: addSpecial(context, buffer, bufLen, newCE, leftIndex, rightIndex+1, status); michael@0: } else { michael@0: if(contractions) { michael@0: uset_addString(contractions, buffer+leftIndex, rightIndex+1-leftIndex); michael@0: } michael@0: if(expansions && isSpecial(newCE) && getCETag(newCE) == EXPANSION_TAG) { michael@0: uset_addString(expansions, buffer+leftIndex, rightIndex+1-leftIndex); michael@0: } michael@0: } michael@0: UCharOffset++; michael@0: } michael@0: } michael@0: michael@0: } michael@0: michael@0: U_CDECL_BEGIN michael@0: static UBool U_CALLCONV michael@0: _processSpecials(const void *context, UChar32 start, UChar32 limit, uint32_t CE) michael@0: { michael@0: UErrorCode *status = ((contContext *)context)->status; michael@0: USet *expansions = ((contContext *)context)->expansions; michael@0: USet *removed = ((contContext *)context)->removedContractions; michael@0: UBool addPrefixes = ((contContext *)context)->addPrefixes; michael@0: UChar contraction[internalBufferSize]; michael@0: if(isSpecial(CE)) { michael@0: if(((getCETag(CE) == SPEC_PROC_TAG && addPrefixes) || getCETag(CE) == CONTRACTION_TAG)) { michael@0: while(start < limit && U_SUCCESS(*status)) { michael@0: // if there are suppressed contractions, we don't michael@0: // want to add them. michael@0: if(removed && uset_contains(removed, start)) { michael@0: start++; michael@0: continue; michael@0: } michael@0: // we start our contraction from middle, since we don't know if it michael@0: // will grow toward right or left michael@0: contraction[internalBufferSize/2] = (UChar)start; michael@0: addSpecial(((contContext *)context), contraction, internalBufferSize, CE, internalBufferSize/2, internalBufferSize/2+1, status); michael@0: start++; michael@0: } michael@0: } else if(expansions && getCETag(CE) == EXPANSION_TAG) { michael@0: while(start < limit && U_SUCCESS(*status)) { michael@0: uset_add(expansions, start++); michael@0: } michael@0: } michael@0: } michael@0: if(U_FAILURE(*status)) { michael@0: return FALSE; michael@0: } else { michael@0: return TRUE; michael@0: } michael@0: } michael@0: michael@0: U_CDECL_END michael@0: michael@0: michael@0: michael@0: /** michael@0: * Get a set containing the contractions defined by the collator. The set includes michael@0: * both the UCA contractions and the contractions defined by the collator michael@0: * @param coll collator michael@0: * @param conts the set to hold the result michael@0: * @param status to hold the error code michael@0: * @return the size of the contraction set michael@0: */ michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucol_getContractions( const UCollator *coll, michael@0: USet *contractions, michael@0: UErrorCode *status) michael@0: { michael@0: ucol_getContractionsAndExpansions(coll, contractions, NULL, FALSE, status); michael@0: return uset_getItemCount(contractions); michael@0: } michael@0: michael@0: /** michael@0: * Get a set containing the expansions defined by the collator. The set includes michael@0: * both the UCA expansions and the expansions defined by the tailoring michael@0: * @param coll collator michael@0: * @param conts the set to hold the result michael@0: * @param addPrefixes add the prefix contextual elements to contractions michael@0: * @param status to hold the error code michael@0: * michael@0: * @draft ICU 3.4 michael@0: */ michael@0: U_CAPI void U_EXPORT2 michael@0: ucol_getContractionsAndExpansions( const UCollator *coll, michael@0: USet *contractions, michael@0: USet *expansions, michael@0: UBool addPrefixes, michael@0: UErrorCode *status) michael@0: { michael@0: if(U_FAILURE(*status)) { michael@0: return; michael@0: } michael@0: if(coll == NULL) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return; michael@0: } michael@0: michael@0: if(contractions) { michael@0: uset_clear(contractions); michael@0: } michael@0: if(expansions) { michael@0: uset_clear(expansions); michael@0: } michael@0: int32_t rulesLen = 0; michael@0: const UChar* rules = ucol_getRules(coll, &rulesLen); michael@0: UColTokenParser src; michael@0: ucol_tok_initTokenList(&src, rules, rulesLen, coll->UCA, michael@0: ucol_tok_getRulesFromBundle, NULL, status); michael@0: michael@0: contContext c = { NULL, contractions, expansions, src.removeSet, addPrefixes, status }; michael@0: michael@0: // Add the UCA contractions michael@0: c.coll = coll->UCA; michael@0: utrie_enum(&coll->UCA->mapping, NULL, _processSpecials, &c); michael@0: michael@0: // This is collator specific. Add contractions from a collator michael@0: c.coll = coll; michael@0: c.removedContractions = NULL; michael@0: utrie_enum(&coll->mapping, NULL, _processSpecials, &c); michael@0: ucol_tok_closeTokenList(&src); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucol_getUnsafeSet( const UCollator *coll, michael@0: USet *unsafe, michael@0: UErrorCode *status) michael@0: { michael@0: UChar buffer[internalBufferSize]; michael@0: int32_t len = 0; michael@0: michael@0: uset_clear(unsafe); michael@0: michael@0: // cccpattern = "[[:^tccc=0:][:^lccc=0:]]", unfortunately variant michael@0: static const UChar cccpattern[25] = { 0x5b, 0x5b, 0x3a, 0x5e, 0x74, 0x63, 0x63, 0x63, 0x3d, 0x30, 0x3a, 0x5d, michael@0: 0x5b, 0x3a, 0x5e, 0x6c, 0x63, 0x63, 0x63, 0x3d, 0x30, 0x3a, 0x5d, 0x5d, 0x00 }; michael@0: michael@0: // add chars that fail the fcd check michael@0: uset_applyPattern(unsafe, cccpattern, 24, USET_IGNORE_SPACE, status); michael@0: michael@0: // add Thai/Lao prevowels michael@0: uset_addRange(unsafe, 0xe40, 0xe44); michael@0: uset_addRange(unsafe, 0xec0, 0xec4); michael@0: // add lead/trail surrogates michael@0: uset_addRange(unsafe, 0xd800, 0xdfff); michael@0: michael@0: USet *contractions = uset_open(0,0); michael@0: michael@0: int32_t i = 0, j = 0; michael@0: int32_t contsSize = ucol_getContractions(coll, contractions, status); michael@0: UChar32 c = 0; michael@0: // Contraction set consists only of strings michael@0: // to get unsafe code points, we need to michael@0: // break the strings apart and add them to the unsafe set michael@0: for(i = 0; i < contsSize; i++) { michael@0: len = uset_getItem(contractions, i, NULL, NULL, buffer, internalBufferSize, status); michael@0: if(len > 0) { michael@0: j = 0; michael@0: while(j < len) { michael@0: U16_NEXT(buffer, j, len, c); michael@0: if(j < len) { michael@0: uset_add(unsafe, c); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: uset_close(contractions); michael@0: michael@0: return uset_size(unsafe); michael@0: } michael@0: #endif