From f4fd91b94f812d049e281f901fe55a3b1593ee44 Mon Sep 17 00:00:00 2001 From: Peter Edberg Date: Thu, 11 Jul 2013 07:00:49 +0000 Subject: [PATCH] ICU-10244 (C) ulocdata_getLocaleSeparator now returns piece between {0} and {1} in new localeSeparator pattern X-SVN-Rev: 33906 --- icu4c/source/i18n/ulocdata.c | 18 ++++++++ icu4c/source/test/cintltst/cldrtest.c | 60 ++++++++++++++++++++------- 2 files changed, 64 insertions(+), 14 deletions(-) diff --git a/icu4c/source/i18n/ulocdata.c b/icu4c/source/i18n/ulocdata.c index 5034bb7a2c2..04d062fd812 100644 --- a/icu4c/source/i18n/ulocdata.c +++ b/icu4c/source/i18n/ulocdata.c @@ -305,6 +305,10 @@ ulocdata_getLocaleSeparator(ULocaleData *uld, int32_t len = 0; const UChar *separator = NULL; UErrorCode localStatus = U_ZERO_ERROR; + UChar *p0, *p1; + static const UChar sub0[4] = { 0x007b, 0x0030, 0x007d , 0x0000 }; /* {0} */ + static const UChar sub1[4] = { 0x007b, 0x0031, 0x007d , 0x0000 }; /* {1} */ + static const int32_t subLen = 3; if (U_FAILURE(*status)) return 0; @@ -339,6 +343,20 @@ ulocdata_getLocaleSeparator(ULocaleData *uld, return 0; } + /* For backwards compatibility, if we have a pattern, return the portion between {0} and {1} */ + p0=u_strstr(separator, sub0); + p1=u_strstr(separator, sub1); + if (p0!=NULL && p1!=NULL && p0<=p1) { + separator = (const UChar *)p0 + subLen; + len = p1 - separator; + /* Desired separator is no longer zero-terminated; handle that if necessary */ + if (len < resultCapacity) { + u_strncpy(result, separator, len); + result[len] = 0; + return len; + } + } + u_strncpy(result, separator, resultCapacity); return len; } diff --git a/icu4c/source/test/cintltst/cldrtest.c b/icu4c/source/test/cintltst/cldrtest.c index f992ba1dfea..0b05bacbc32 100644 --- a/icu4c/source/test/cintltst/cldrtest.c +++ b/icu4c/source/test/cintltst/cldrtest.c @@ -1223,26 +1223,58 @@ static void TestExemplarSet(void){ } } +enum { kUBufMax = 32 }; static void TestLocaleDisplayPattern(void){ - UErrorCode status = U_ZERO_ERROR; - UChar pattern[32] = {0,}; - UChar separator[32] = {0,}; - ULocaleData *uld = ulocdata_open(uloc_getDefault(), &status); + UErrorCode status; + UChar pattern[kUBufMax] = {0,}; + UChar separator[kUBufMax] = {0,}; + ULocaleData *uld; + static const UChar enExpectPat[] = { 0x007B,0x0030,0x007D,0x0020,0x0028,0x007B,0x0031,0x007D,0x0029,0 }; /* "{0} ({1})" */ + static const UChar enExpectSep[] = { 0x002C,0x0020,0 }; /* ", " */ + static const UChar zhExpectPat[] = { 0x007B,0x0030,0x007D,0xFF08,0x007B,0x0031,0x007D,0xFF09,0 }; + static const UChar zhExpectSep[] = { 0x3001,0 }; + status = U_ZERO_ERROR; + uld = ulocdata_open("en", &status); if(U_FAILURE(status)){ - log_data_err("ulocdata_open error"); - return; - } - ulocdata_getLocaleDisplayPattern(uld, pattern, 32, &status); - if (U_FAILURE(status)){ - log_err("ulocdata_getLocaleDisplayPattern error!"); + log_data_err("ulocdata_open en error %s", u_errorName(status)); + } else { + ulocdata_getLocaleDisplayPattern(uld, pattern, kUBufMax, &status); + if (U_FAILURE(status)){ + log_err("ulocdata_getLocaleDisplayPattern en error %s", u_errorName(status)); + } else if (u_strcmp(pattern, enExpectPat) != 0) { + log_err("ulocdata_getLocaleDisplayPattern en returns unexpected pattern"); + } + status = U_ZERO_ERROR; + ulocdata_getLocaleSeparator(uld, separator, kUBufMax, &status); + if (U_FAILURE(status)){ + log_err("ulocdata_getLocaleSeparator en error %s", u_errorName(status)); + } else if (u_strcmp(separator, enExpectSep) != 0) { + log_err("ulocdata_getLocaleSeparator en returns unexpected string "); + } + ulocdata_close(uld); } + status = U_ZERO_ERROR; - ulocdata_getLocaleSeparator(uld, separator, 32, &status); - if (U_FAILURE(status)){ - log_err("ulocdata_getLocaleSeparator error!"); + uld = ulocdata_open("zh", &status); + if(U_FAILURE(status)){ + log_data_err("ulocdata_open zh error %s", u_errorName(status)); + } else { + ulocdata_getLocaleDisplayPattern(uld, pattern, kUBufMax, &status); + if (U_FAILURE(status)){ + log_err("ulocdata_getLocaleDisplayPattern zh error %s", u_errorName(status)); + } else if (u_strcmp(pattern, zhExpectPat) != 0) { + log_err("ulocdata_getLocaleDisplayPattern zh returns unexpected pattern"); + } + status = U_ZERO_ERROR; + ulocdata_getLocaleSeparator(uld, separator, kUBufMax, &status); + if (U_FAILURE(status)){ + log_err("ulocdata_getLocaleSeparator zh error %s", u_errorName(status)); + } else if (u_strcmp(separator, zhExpectSep) != 0) { + log_err("ulocdata_getLocaleSeparator zh returns unexpected string "); + } + ulocdata_close(uld); } - ulocdata_close(uld); } static void TestCoverage(void){ -- 2.40.0