From bb7e154629caabf6753657c048d8743a7e2a2a5f Mon Sep 17 00:00:00 2001 From: Peter Edberg Date: Wed, 18 Sep 2013 08:33:47 +0000 Subject: [PATCH] ICU-10260 Port J LocaleDisplayNamesImpl change for (->[ to C; make similar changes in uloc_getDisplayName X-SVN-Rev: 34369 --- icu4c/source/common/locdispnames.cpp | 21 +++++++- icu4c/source/i18n/locdspnm.cpp | 21 ++++++++ icu4c/source/test/cintltst/cloctst.c | 78 ++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 1 deletion(-) diff --git a/icu4c/source/common/locdispnames.cpp b/icu4c/source/common/locdispnames.cpp index cca0730be9b..3ea9602d055 100644 --- a/icu4c/source/common/locdispnames.cpp +++ b/icu4c/source/common/locdispnames.cpp @@ -483,6 +483,11 @@ uloc_getDisplayName(const char *locale, const UChar *pattern; int32_t patLen = 0; int32_t sub0Pos, sub1Pos; + + UChar formatOpenParen = 0x0028; // ( + UChar formatReplaceOpenParen = 0x005B; // [ + UChar formatCloseParen = 0x0029; // ) + UChar formatReplaceCloseParen = 0x005D; // ] UBool haveLang = TRUE; /* assume true, set false if we find we don't have a lang component in the locale */ @@ -543,6 +548,7 @@ uloc_getDisplayName(const char *locale, patLen=defaultPatLen; sub0Pos=defaultSub0Pos; sub1Pos=defaultSub1Pos; + // use default formatOpenParen etc. set above } else { /* non-default pattern */ UChar *p0=u_strstr(pattern, sub0); UChar *p1=u_strstr(pattern, sub1); @@ -556,6 +562,12 @@ uloc_getDisplayName(const char *locale, int32_t t=sub0Pos; sub0Pos=sub1Pos; sub1Pos=t; langi=1; } + if (u_strchr(pattern, 0xFF08) != NULL) { + formatOpenParen = 0xFF08; // fullwidth ( + formatReplaceOpenParen = 0xFF3B; // fullwidth [ + formatCloseParen = 0xFF09; // fullwidth ) + formatReplaceCloseParen = 0xFF3D; // fullwidth ] + } } /* We loop here because there is one case in which after the first pass we could need to @@ -677,7 +689,14 @@ uloc_getDisplayName(const char *locale, if (len>0) { /* we addeed a component, so add separator and write it if there's room. */ if(len+sepLen<=cap) { - p+=len; + const UChar * plimit = p + len; + for (; p < plimit; p++) { + if (*p == formatOpenParen) { + *p = formatReplaceOpenParen; + } else if (*p == formatCloseParen) { + *p = formatReplaceCloseParen; + } + } for(int32_t i=0;i= 0) { + formatOpenParen.setTo(0xFF08); // fullwidth ( + formatReplaceOpenParen.setTo(0xFF3B); // fullwidth [ + formatCloseParen.setTo(0xFF09); // fullwidth ) + formatReplaceCloseParen.setTo(0xFF3D); // fullwidth ] + } else { + formatOpenParen.setTo(0x0028); // ( + formatReplaceOpenParen.setTo(0x005B); // [ + formatCloseParen.setTo(0x0029); // ) + formatReplaceCloseParen.setTo(0x005D); // ] + } UnicodeString ktPattern; langData.get("localeDisplayPattern", "keyTypePattern", ktPattern); @@ -596,6 +611,8 @@ LocaleDisplayNamesImpl::localeDisplayName(const Locale& locale, if (hasVariant) { appendWithSep(resultRemainder, variantDisplayName(variant, temp)); } + resultRemainder.findAndReplace(formatOpenParen, formatReplaceOpenParen); + resultRemainder.findAndReplace(formatCloseParen, formatReplaceCloseParen); e = locale.createKeywords(status); if (e && U_SUCCESS(status)) { @@ -605,7 +622,11 @@ LocaleDisplayNamesImpl::localeDisplayName(const Locale& locale, while ((key = e->next((int32_t *)0, status)) != NULL) { locale.getKeywordValue(key, value, ULOC_KEYWORD_AND_VALUES_CAPACITY, status); keyDisplayName(key, temp); + temp.findAndReplace(formatOpenParen, formatReplaceOpenParen); + temp.findAndReplace(formatCloseParen, formatReplaceCloseParen); keyValueDisplayName(key, value, temp2); + temp2.findAndReplace(formatOpenParen, formatReplaceOpenParen); + temp2.findAndReplace(formatCloseParen, formatReplaceCloseParen); if (temp2 != UnicodeString(value, -1, US_INV)) { appendWithSep(resultRemainder, temp2); } else if (temp != UnicodeString(key, -1, US_INV)) { diff --git a/icu4c/source/test/cintltst/cloctst.c b/icu4c/source/test/cintltst/cloctst.c index 525d1463b87..16e25d53b67 100644 --- a/icu4c/source/test/cintltst/cloctst.c +++ b/icu4c/source/test/cintltst/cloctst.c @@ -33,6 +33,7 @@ #include "unicode/ustring.h" #include "unicode/utypes.h" #include "unicode/ulocdata.h" +#include "unicode/uldnames.h" #include "unicode/parseerr.h" /* may not be included with some uconfig switches */ #include "udbgutil.h" #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) @@ -41,6 +42,7 @@ static void TestNullDefault(void); static void TestNonexistentLanguageExemplars(void); static void TestLocDataErrorCodeChaining(void); static void TestLanguageExemplarsFallbacks(void); +static void TestDisplayNameBrackets(void); static void TestUnicodeDefines(void); @@ -245,6 +247,7 @@ void addLocaleTest(TestNode** root) TESTCASE(TestTrailingNull); TESTCASE(TestUnicodeDefines); TESTCASE(TestEnglishExemplarCharacters); + TESTCASE(TestDisplayNameBrackets); } @@ -1021,6 +1024,81 @@ setUpDataTable(); cleanUpDataTable(); } +/*------------------------------ + * TestDisplayNameBrackets + */ + +typedef struct { + const char * displayLocale; + const char * namedRegion; + const char * namedLocale; + const char * regionName; + const char * localeName; +} DisplayNameBracketsItem; + +static const DisplayNameBracketsItem displayNameBracketsItems[] = { + { "en", "CC", "en_CC", "Cocos (Keeling) Islands", "English (Cocos [Keeling] Islands)" }, + { "en", "MM", "my_MM", "Myanmar (Burma)", "Burmese (Myanmar [Burma])" }, + { "en", "MM", "my_Mymr_MM", "Myanmar (Burma)", "Burmese (Myanmar, Myanmar [Burma])" }, + { "zh", "CC", "en_CC", "\\u79D1\\u79D1\\u65AF\\uFF08\\u57FA\\u6797\\uFF09\\u7FA4\\u5C9B", "\\u82F1\\u6587\\uFF08\\u79D1\\u79D1\\u65AF\\uFF3B\\u57FA\\u6797\\uFF3D\\u7FA4\\u5C9B\\uFF09" }, + { "zh", "CG", "fr_CG", "\\u521A\\u679C\\uFF08\\u5E03\\uFF09", "\\u6CD5\\u6587\\uFF08\\u521A\\u679C\\uFF3B\\u5E03\\uFF3D\\uFF09" }, + { NULL, NULL, NULL, NULL, NULL } +}; + +enum { kDisplayNameBracketsMax = 128 }; + +static void TestDisplayNameBrackets() +{ + const DisplayNameBracketsItem * itemPtr = displayNameBracketsItems; + for (; itemPtr->displayLocale != NULL; itemPtr++) { + ULocaleDisplayNames * uldn; + UErrorCode status; + UChar expectRegionName[kDisplayNameBracketsMax]; + UChar expectLocaleName[kDisplayNameBracketsMax]; + UChar getName[kDisplayNameBracketsMax]; + int32_t ulen; + + (void) u_unescape(itemPtr->regionName, expectRegionName, kDisplayNameBracketsMax); + (void) u_unescape(itemPtr->localeName, expectLocaleName, kDisplayNameBracketsMax); + + status = U_ZERO_ERROR; + ulen = uloc_getDisplayCountry(itemPtr->namedLocale, itemPtr->displayLocale, getName, kDisplayNameBracketsMax, &status); + if ( U_FAILURE(status) || u_strcmp(getName, expectRegionName) != 0 ) { + log_err("uloc_getDisplayCountry for displayLocale %s and namedLocale %s returns unexpected name or status %s\n", itemPtr->displayLocale, itemPtr->namedLocale, myErrorName(status)); + } + + status = U_ZERO_ERROR; + ulen = uloc_getDisplayName(itemPtr->namedLocale, itemPtr->displayLocale, getName, kDisplayNameBracketsMax, &status); + if ( U_FAILURE(status) || u_strcmp(getName, expectLocaleName) != 0 ) { + log_err("uloc_getDisplayName for displayLocale %s and namedLocale %s returns unexpected name or status %s\n", itemPtr->displayLocale, itemPtr->namedLocale, myErrorName(status)); + } + + status = U_ZERO_ERROR; + uldn = uldn_open(itemPtr->displayLocale, ULDN_STANDARD_NAMES, &status); + if (U_SUCCESS(status)) { + status = U_ZERO_ERROR; + ulen = uldn_regionDisplayName(uldn, itemPtr->namedRegion, getName, kDisplayNameBracketsMax, &status); + if ( U_FAILURE(status) || u_strcmp(getName, expectRegionName) != 0 ) { + log_err("uldn_regionDisplayName for displayLocale %s and namedRegion %s returns unexpected name or status %s\n", itemPtr->displayLocale, itemPtr->namedRegion, myErrorName(status)); + } + + status = U_ZERO_ERROR; + ulen = uldn_localeDisplayName(uldn, itemPtr->namedLocale, getName, kDisplayNameBracketsMax, &status); + if ( U_FAILURE(status) || u_strcmp(getName, expectLocaleName) != 0 ) { + log_err("uldn_localeDisplayName for displayLocale %s and namedLocale %s returns unexpected name or status %s\n", itemPtr->displayLocale, itemPtr->namedLocale, myErrorName(status)); + } + + uldn_close(uldn); + } else { + log_data_err("uldn_open fails for displayLocale %s, status=%s\n", itemPtr->displayLocale, u_errorName(status)); + } + } +} + +/*------------------------------ + * TestISOFunctions + */ + #if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION /* test for uloc_getISOLanguages, uloc_getISOCountries */ static void TestISOFunctions()