From 89e381736c7453b93369d3a4aae998de8f3346bc Mon Sep 17 00:00:00 2001 From: Alan Liu Date: Wed, 19 May 2004 22:57:40 +0000 Subject: [PATCH] ICU-3748 don't remap "" with keywords; cleanup; fix buffer handling X-SVN-Rev: 15415 --- icu4c/source/common/uloc.c | 125 ++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 63 deletions(-) diff --git a/icu4c/source/common/uloc.c b/icu4c/source/common/uloc.c index 66b8f789268..54ee6d62498 100644 --- a/icu4c/source/common/uloc.c +++ b/icu4c/source/common/uloc.c @@ -832,13 +832,20 @@ _deleteVariant(char* variants, int32_t variantsLen, #define OPTION_SET(options, mask) ((options & mask) != 0) +/** + * Canonicalize the given localeID, to level 1 or to level 2, + * depending on the options. To specify level 1, pass in options=0. + * To specify level 2, pass in options=_ULOC_CANONICALIZE. + * + * This is the code underlying uloc_getName and uloc_canonicalize. + */ static int32_t _canonicalize(const char* localeID, char* result, int32_t resultCapacity, uint32_t options, UErrorCode* err) { - int32_t i, j, len, fieldCount=0, scriptSize=0, variantSize=0, nameCapacity; + int32_t j, len, fieldCount=0, scriptSize=0, variantSize=0, nameCapacity; char localeBuffer[ULOC_FULLNAME_CAPACITY]; const char* keywordAssign = NULL; const char* separatorIndicator = NULL; @@ -857,8 +864,9 @@ _canonicalize(const char* localeID, } /* if we are doing a full canonicalization, then put results in - localeBuffer; otherwise send them to result. */ - if (OPTION_SET(options, _ULOC_CANONICALIZE)) { + localeBuffer, if necessary; otherwise send them to result. */ + if (OPTION_SET(options, _ULOC_CANONICALIZE) && + (result == NULL || resultCapacity < sizeof(localeBuffer))) { name = localeBuffer; nameCapacity = sizeof(localeBuffer); } else { @@ -867,43 +875,43 @@ _canonicalize(const char* localeID, } /* get all pieces, one after another, and separate with '_' */ - i=_getLanguage(localeID, name, nameCapacity, &localeID); + len=_getLanguage(localeID, name, nameCapacity, &localeID); if(_isIDSeparator(*localeID)) { const char *scriptID; ++fieldCount; - if(i 0) { /* Found optional script */ localeID = scriptID; ++fieldCount; - i+=scriptSize; + len+=scriptSize; if (_isIDSeparator(*localeID)) { /* If there is something else, then we add the _ */ - if(i 0) { - variant = name+i; - i += variantSize; + variant = name+len; + len += variantSize; localeID += variantSize + 1; /* skip '_' and variant */ } } @@ -921,17 +929,18 @@ _canonicalize(const char* localeID, done = TRUE; break; default: - if (i 0)) { do { - if(i 0)); if (posixVariantSize > 0) { if (variant == NULL) { - variant = name+i; + variant = name+len; } - i += posixVariantSize; + len += posixVariantSize; variantSize += posixVariantSize; } } /* Check for EURO variants. */ sawEuro = _deleteVariant(variant, variantSize, "EURO", 4); - i -= sawEuro; - if (sawEuro > 0 && name[i-1] == '_') { /* delete trailing '_' */ - --i; + len -= sawEuro; + if (sawEuro > 0 && name[len-1] == '_') { /* delete trailing '_' */ + --len; } /* Look up the ID in the canonicalization map */ for (j=0; j<(int32_t)(sizeof(CANONICALIZE_MAP)/sizeof(CANONICALIZE_MAP[0])); j++) { const char* id = CANONICALIZE_MAP[j].id; int32_t n = uprv_strlen(id); - if (i == n && uprv_strncmp(name, id, n) == 0) { - i = _copyCount(name, nameCapacity, CANONICALIZE_MAP[j].canonicalID); + if (len == n && uprv_strncmp(name, id, n) == 0) { + if (n == 0 && localeID != NULL) { + break; /* Don't remap "" if keywords present */ + } + len = _copyCount(name, nameCapacity, CANONICALIZE_MAP[j].canonicalID); addKeyword = CANONICALIZE_MAP[j].keyword; addValue = CANONICALIZE_MAP[j].value; break; @@ -1007,41 +1019,28 @@ _canonicalize(const char* localeID, if (!OPTION_SET(options, _ULOC_STRIP_KEYWORDS)) { if (localeID!=NULL && keywordAssign!=NULL && (!separatorIndicator || separatorIndicator > keywordAssign)) { - if(i