-// structure for packing the bits of the attributes in the
-// identifier number.
-// locale is packed separately
-struct bitPacking {
- char letter;
- uint32_t offset;
- uint32_t width;
- UColAttribute attribute;
- UColAttributeValue values[6];
-};
-
-static const bitPacking attributesToBits[UCOL_ATTRIBUTE_COUNT] = {
- /* french */ { frenchCollArg, 29, 2, UCOL_FRENCH_COLLATION, { UCOL_DEFAULT, UCOL_OFF, UCOL_ON }},
- /* alternate */ { alternateHArg, 27, 2, UCOL_ALTERNATE_HANDLING, { UCOL_DEFAULT, UCOL_NON_IGNORABLE, UCOL_SHIFTED }},
- /* case first */ { caseFirstArg, 25, 2, UCOL_CASE_FIRST, { UCOL_DEFAULT, UCOL_OFF, UCOL_LOWER_FIRST, UCOL_UPPER_FIRST }},
- /* case level */ { caseLevelArg, 23, 2, UCOL_CASE_LEVEL, { UCOL_DEFAULT, UCOL_OFF, UCOL_ON }},
- /* normalization */ { normArg, 21, 2, UCOL_NORMALIZATION_MODE, { UCOL_DEFAULT, UCOL_OFF, UCOL_ON }},
- /* strength */ { strengthArg, 18, 3, UCOL_STRENGTH, { UCOL_DEFAULT, UCOL_PRIMARY, UCOL_SECONDARY, UCOL_TERTIARY, UCOL_QUATERNARY, UCOL_IDENTICAL }},
- /* hiragana */ { hiraganaQArg, 16, 2, UCOL_HIRAGANA_QUATERNARY_MODE, { UCOL_DEFAULT, UCOL_OFF, UCOL_ON }},
- /* numeric coll */ { numericCollArg, 14, 2, UCOL_NUMERIC_COLLATION, { UCOL_DEFAULT, UCOL_OFF, UCOL_ON }}
-};
-
-static const uint32_t keywordShift = 9;
-static const uint32_t keywordWidth = 5;
-static const uint32_t localeShift = 0;
-static const uint32_t localeWidth = 7;
-
-
-static uint32_t ucol_sit_putLocaleInIdentifier(uint32_t result, const char* locale, UErrorCode* status) {
- char buffer[internalBufferSize], keywordBuffer[internalBufferSize],
- baseName[internalBufferSize], localeBuffer[internalBufferSize];
- int32_t len = 0, keywordLen = 0,
- baseNameLen = 0, localeLen = 0;
- uint32_t i = 0;
- UBool isAvailable = FALSE;
- if(locale) {
- len = uloc_canonicalize(locale, buffer, internalBufferSize, status);
- localeLen = ucol_getFunctionalEquivalent(localeBuffer, internalBufferSize, "collation", buffer, &isAvailable, status);
- keywordLen = uloc_getKeywordValue(buffer, "collation", keywordBuffer, internalBufferSize, status);
- baseNameLen = uloc_getBaseName(buffer, baseName, internalBufferSize, status);
-
- /*Binary search for the map entry for normal cases */
-
- uint32_t low = 0;
- uint32_t high = sizeof(locales)/sizeof(locales[0]);
- uint32_t mid = high;
- uint32_t oldmid = 0;
- int32_t compVal = 0;
-
-
- while (high > low) /*binary search*/{
-
- mid = (high+low) >> 1; /*Finds median*/
-
- if (mid == oldmid)
- return UCOL_SIT_COLLATOR_NOT_ENCODABLE; // we didn't find it
-
- compVal = uprv_strcmp(baseName, locales[mid]);
- if (compVal < 0){
- high = mid;
- }
- else if (compVal > 0){
- low = mid;
- }
- else /*we found it*/{
- break;
- }
- oldmid = mid;
- }
-
- result |= (mid & ((1 << localeWidth) - 1)) << localeShift;
- }
-
- if(keywordLen) {
- for(i = 1; i < sizeof(keywords)/sizeof(keywords[0]); i++) {
- if(uprv_strcmp(keywords[i], keywordBuffer) == 0) {
- result |= (i & ((1 << keywordWidth) - 1)) << keywordShift;
- break;
- }
- }
- }
- return result;
-}
-
-U_CAPI uint32_t U_EXPORT2
-ucol_collatorToIdentifier(const UCollator *coll,
- const char *locale,
- UErrorCode *status)
-{
- uint32_t result = 0;
- uint32_t i = 0, j = 0;
- UColAttributeValue attrValue = UCOL_DEFAULT;
-
- // if variable top is not default, we need to use strings
- if(coll->variableTopValueisDefault != TRUE) {
- return UCOL_SIT_COLLATOR_NOT_ENCODABLE;
- }
-
- if(locale == NULL) {
- locale = ucol_getLocale(coll, ULOC_VALID_LOCALE, status);
- }
-
- result = ucol_sit_putLocaleInIdentifier(result, locale, status);
-
- for(i = 0; i < sizeof(attributesToBits)/sizeof(attributesToBits[0]); i++) {
- attrValue = ucol_getAttributeOrDefault(coll, attributesToBits[i].attribute, status);
- j = 0;
- while(attributesToBits[i].values[j] != attrValue) {
- j++;
- }
- result |= (j & ((1 << attributesToBits[i].width) - 1)) << attributesToBits[i].offset;
- }
-
- return result;
-}
-
-U_CAPI UCollator* U_EXPORT2
-ucol_openFromIdentifier(uint32_t identifier,
- UBool forceDefaults,
- UErrorCode *status)
-{
- uint32_t i = 0;
- int32_t value = 0, keyword = 0;
- char locale[internalBufferSize];
-
- value = (identifier >> localeShift) & ((1 << localeWidth) - 1);
- keyword = (identifier >> keywordShift) & ((1 << keywordWidth) - 1);
-
- uprv_strcpy(locale, locales[value]);
-
- if(keyword) {
- uprv_strcat(locale, collationKeyword);
- uprv_strcat(locale, keywords[keyword]);
- }
-
- UColAttributeValue attrValue = UCOL_DEFAULT;
-
- UCollator *result = ucol_open(locale, status);
-
- // variable top is not set in the identifier, so we can easily skip that on
-
- for(i = 0; i < sizeof(attributesToBits)/sizeof(attributesToBits[0]); i++) {
- value = (identifier >> attributesToBits[i].offset) & ((1 << attributesToBits[i].width) - 1);
- attrValue = attributesToBits[i].values[value];
- // the collator is all default, so we will set only the values that will differ from
- // the default values.
- if(attrValue != UCOL_DEFAULT) {
- if(forceDefaults ||
- ucol_getAttribute(result, attributesToBits[i].attribute, status) != attrValue) {
- ucol_setAttribute(result, attributesToBits[i].attribute, attrValue, status);
- }
- }
- }
-
- return result;
-}
-
-U_CAPI int32_t U_EXPORT2
-ucol_identifierToShortString(uint32_t identifier,
- char *buffer,
- int32_t capacity,
- UBool forceDefaults,
- UErrorCode *status)
-{
- int32_t locIndex = (identifier >> localeShift) & ((1 << localeWidth) - 1);
- int32_t keywordIndex = (identifier >> keywordShift) & ((1 << keywordWidth) - 1);
- CollatorSpec s;
- ucol_sit_initCollatorSpecs(&s);
- uprv_strcpy(s.locale, locales[locIndex]);
- if(keywordIndex) {
- uprv_strcat(s.locale, collationKeyword);
- uprv_strcat(s.locale, keywords[keywordIndex]);
- }
- UCollator *coll = ucol_openFromIdentifier(identifier, forceDefaults, status);
- int32_t resultLen = ucol_getShortDefinitionString(coll, s.locale, buffer, capacity, status);
- ucol_close(coll);
- return resultLen;
-
-#if 0
- // TODO: Crumy, crumy, crumy... Very hard to currently go algorithmically from
- // identifier to short string. Do rethink
- if(forceDefaults == FALSE) {
- UCollator *coll = ucol_openFromIdentifier(identifier, FALSE, status);
- int32_t resultLen = ucol_getShortDefinitionString(coll, s.locale, buffer, capacity, status);
- ucol_close(coll);
- return resultLen;
- } else { // forceDefaults == TRUE
- char letter;
- UColAttributeValue value;
- int32_t i = 0;
- for(i = 0; i < sizeof(attributesToBits)/sizeof(attributesToBits[0]); i++) {
- value = attributesToBits[i].values[(identifier >> attributesToBits[i].offset) & ((1 << attributesToBits[i].width) - 1)];
- if(value != UCOL_DEFAULT) {
- uprv_strcat(buffer, "_");
- uprv_strncat(buffer, &attributesToBits[i].letter, 1);
- letter = ucol_sit_attributeValueToLetter(value, status);
- uprv_strncat(buffer, &letter, 1);
- }
- }
- return ucol_sit_dumpSpecs(&s, buffer, capacity, status);
- }
-#endif
-}
-
-U_CAPI uint32_t U_EXPORT2
-ucol_shortStringToIdentifier(const char *definition,
- UBool forceDefaults,
- UErrorCode *status)
-{
- UParseError parseError;
- CollatorSpec s;
- uint32_t result = 0;
- uint32_t i = 0, j = 0;
- ucol_sit_initCollatorSpecs(&s);
-
- ucol_sit_readSpecs(&s, definition, &parseError, status);
- ucol_sit_calculateWholeLocale(&s);
-
- char locBuffer[internalBufferSize];
- UBool isAvailable = FALSE;
- UColAttributeValue attrValue = UCOL_DEFAULT;
-
- ucol_getFunctionalEquivalent(locBuffer, internalBufferSize, "collation", s.locale, &isAvailable, status);
-
- if(forceDefaults == FALSE) {
- UCollator *coll = ucol_openFromShortString(definition, FALSE, &parseError, status);
- result = ucol_collatorToIdentifier(coll, locBuffer, status);
- ucol_close(coll);
- } else { // forceDefaults == TRUE
- result = ucol_sit_putLocaleInIdentifier(result, locBuffer, status);
-
- for(i = 0; i < sizeof(attributesToBits)/sizeof(attributesToBits[0]); i++) {
- attrValue = s.options[i];
- j = 0;
- while(attributesToBits[i].values[j] != attrValue) {
- j++;
- }
- result |= (j & ((1 << attributesToBits[i].width) - 1)) << attributesToBits[i].offset;
- }
-
- }
- return result;
-
-}
-
-U_CAPI UColAttributeValue U_EXPORT2
-ucol_getAttributeOrDefault(const UCollator *coll, UColAttribute attr, UErrorCode *status)
-{
- if(U_FAILURE(*status) || coll == NULL) {
- return UCOL_DEFAULT;
- }
- switch(attr) {
- case UCOL_NUMERIC_COLLATION:
- return coll->numericCollationisDefault?UCOL_DEFAULT:coll->numericCollation;
- case UCOL_HIRAGANA_QUATERNARY_MODE:
- return coll->hiraganaQisDefault?UCOL_DEFAULT:coll->hiraganaQ;
- case UCOL_FRENCH_COLLATION: /* attribute for direction of secondary weights*/
- return coll->frenchCollationisDefault?UCOL_DEFAULT:coll->frenchCollation;
- case UCOL_ALTERNATE_HANDLING: /* attribute for handling variable elements*/
- return coll->alternateHandlingisDefault?UCOL_DEFAULT:coll->alternateHandling;
- case UCOL_CASE_FIRST: /* who goes first, lower case or uppercase */
- return coll->caseFirstisDefault?UCOL_DEFAULT:coll->caseFirst;
- case UCOL_CASE_LEVEL: /* do we have an extra case level */
- return coll->caseLevelisDefault?UCOL_DEFAULT:coll->caseLevel;
- case UCOL_NORMALIZATION_MODE: /* attribute for normalization */
- return coll->normalizationModeisDefault?UCOL_DEFAULT:coll->normalizationMode;
- case UCOL_STRENGTH: /* attribute for strength */
- return coll->strengthisDefault?UCOL_DEFAULT:coll->strength;
- case UCOL_ATTRIBUTE_COUNT:
- default:
- *status = U_ILLEGAL_ARGUMENT_ERROR;
- break;
- }
- return UCOL_DEFAULT;
-}
-
-
-struct contContext {
- const UCollator *coll;
- USet *conts;
- USet *expansions;
- USet *removedContractions;
- UBool addPrefixes;
- UErrorCode *status;
-};
-
-
-
-static void
-addSpecial(contContext *context, UChar *buffer, int32_t bufLen,
- uint32_t CE, int32_t leftIndex, int32_t rightIndex, UErrorCode *status)
-{
- const UCollator *coll = context->coll;
- USet *contractions = context->conts;
- USet *expansions = context->expansions;
- UBool addPrefixes = context->addPrefixes;
-
- const UChar *UCharOffset = (UChar *)coll->image+getContractOffset(CE);
- uint32_t newCE = *(coll->contractionCEs + (UCharOffset - coll->contractionIndex));
- // we might have a contraction that ends from previous level
- if(newCE != UCOL_NOT_FOUND) {
- if(isSpecial(CE) && getCETag(CE) == CONTRACTION_TAG && isSpecial(newCE) && getCETag(newCE) == SPEC_PROC_TAG && addPrefixes) {
- addSpecial(context, buffer, bufLen, newCE, leftIndex, rightIndex, status);
- }
- if(contractions && rightIndex-leftIndex > 1) {
- uset_addString(contractions, buffer+leftIndex, rightIndex-leftIndex);
- if(expansions && isSpecial(CE) && getCETag(CE) == EXPANSION_TAG) {
- uset_addString(expansions, buffer+leftIndex, rightIndex-leftIndex);
- }
- }
- }
-
- UCharOffset++;
- // check whether we're doing contraction or prefix
- if(getCETag(CE) == SPEC_PROC_TAG && addPrefixes) {
- if(leftIndex == 0) {
- *status = U_INTERNAL_PROGRAM_ERROR;
- return;
- }
- --leftIndex;
- while(*UCharOffset != 0xFFFF) {
- newCE = *(coll->contractionCEs + (UCharOffset - coll->contractionIndex));
- buffer[leftIndex] = *UCharOffset;
- if(isSpecial(newCE) && (getCETag(newCE) == CONTRACTION_TAG || getCETag(newCE) == SPEC_PROC_TAG)) {
- addSpecial(context, buffer, bufLen, newCE, leftIndex, rightIndex, status);
- } else {
- if(contractions) {
- uset_addString(contractions, buffer+leftIndex, rightIndex-leftIndex);
- }
- if(expansions && isSpecial(newCE) && getCETag(newCE) == EXPANSION_TAG) {
- uset_addString(expansions, buffer+leftIndex, rightIndex-leftIndex);
- }
- }
- UCharOffset++;
- }
- } else if(getCETag(CE) == CONTRACTION_TAG) {
- if(rightIndex == bufLen-1) {
- *status = U_INTERNAL_PROGRAM_ERROR;
- return;
- }
- while(*UCharOffset != 0xFFFF) {
- newCE = *(coll->contractionCEs + (UCharOffset - coll->contractionIndex));
- buffer[rightIndex] = *UCharOffset;
- if(isSpecial(newCE) && (getCETag(newCE) == CONTRACTION_TAG || getCETag(newCE) == SPEC_PROC_TAG)) {
- addSpecial(context, buffer, bufLen, newCE, leftIndex, rightIndex+1, status);
- } else {
- if(contractions) {
- uset_addString(contractions, buffer+leftIndex, rightIndex+1-leftIndex);
- }
- if(expansions && isSpecial(newCE) && getCETag(newCE) == EXPANSION_TAG) {
- uset_addString(expansions, buffer+leftIndex, rightIndex+1-leftIndex);
- }
- }
- UCharOffset++;
- }
- }
-
-}
-
-U_CDECL_BEGIN
-static UBool U_CALLCONV
-_processSpecials(const void *context, UChar32 start, UChar32 limit, uint32_t CE)
-{
- UErrorCode *status = ((contContext *)context)->status;
- USet *expansions = ((contContext *)context)->expansions;
- USet *removed = ((contContext *)context)->removedContractions;
- UBool addPrefixes = ((contContext *)context)->addPrefixes;
- UChar contraction[internalBufferSize];
- if(isSpecial(CE)) {
- if(((getCETag(CE) == SPEC_PROC_TAG && addPrefixes) || getCETag(CE) == CONTRACTION_TAG)) {
- while(start < limit && U_SUCCESS(*status)) {
- // if there are suppressed contractions, we don't
- // want to add them.
- if(removed && uset_contains(removed, start)) {
- start++;
- continue;
- }
- // we start our contraction from middle, since we don't know if it
- // will grow toward right or left
- contraction[internalBufferSize/2] = (UChar)start;
- addSpecial(((contContext *)context), contraction, internalBufferSize, CE, internalBufferSize/2, internalBufferSize/2+1, status);
- start++;
- }
- } else if(expansions && getCETag(CE) == EXPANSION_TAG) {
- while(start < limit && U_SUCCESS(*status)) {
- uset_add(expansions, start++);
- }
- }
- }
- if(U_FAILURE(*status)) {
- return FALSE;
- } else {
- return TRUE;
- }
-}
-
-U_CDECL_END
-
-
-