From f4130d74f3f5ad7650022bac7f46e6f37613b650 Mon Sep 17 00:00:00 2001 From: George Rhoten Date: Mon, 28 Mar 2005 05:12:13 +0000 Subject: [PATCH] ICU-4189 Fix for UCONFIG_NO_CONVERSION with UCONFIG_NO_SERVICE. Separate LocaleUtility into its own API. X-SVN-Rev: 17404 --- icu4c/source/common/Makefile.in | 2 +- icu4c/source/common/common.vcproj | 9 +- icu4c/source/common/locutil.cpp | 265 ++++++++++++++++++++++++++++++ icu4c/source/common/locutil.h | 32 ++++ icu4c/source/common/servloc.h | 13 +- icu4c/source/i18n/transreg.cpp | 2 +- 6 files changed, 306 insertions(+), 17 deletions(-) create mode 100644 icu4c/source/common/locutil.cpp create mode 100644 icu4c/source/common/locutil.h diff --git a/icu4c/source/common/Makefile.in b/icu4c/source/common/Makefile.in index 0203652b5e..36e3293eb4 100644 --- a/icu4c/source/common/Makefile.in +++ b/icu4c/source/common/Makefile.in @@ -74,7 +74,7 @@ uscript.o usc_impl.o uvector.o ustack.o uvectr32.o ucmp8.o \ uarrsort.o utrie.o uset.o uset_props.o uniset.o uniset_props.o ruleiter.o caniter.o unifilt.o unifunct.o usetiter.o \ brkiter.o brkdict.o ubrk.o dbbi.o dbbi_tbl.o \ rbbi.o rbbidata.o rbbinode.o rbbirb.o rbbiscan.o rbbisetb.o rbbistbl.o rbbitblb.o \ -serv.o servnotf.o servls.o servlu.o servlk.o servlkf.o servrbf.o servslkf.o \ +serv.o servnotf.o servls.o servlk.o servlkf.o servrbf.o servslkf.o locutil.o \ uenum.o ustrenum.o uidna.o usprep.o punycode.o \ cwchar.o filestrm.o util.o parsepos.o utrace.o locbased.o diff --git a/icu4c/source/common/common.vcproj b/icu4c/source/common/common.vcproj index bd41e4eb89..9db336a432 100644 --- a/icu4c/source/common/common.vcproj +++ b/icu4c/source/common/common.vcproj @@ -1174,6 +1174,12 @@ Outputs="..\..\include\unicode\$(InputFileName)"/> + + + + @@ -1622,9 +1628,6 @@ - - diff --git a/icu4c/source/common/locutil.cpp b/icu4c/source/common/locutil.cpp new file mode 100644 index 0000000000..dd3615b919 --- /dev/null +++ b/icu4c/source/common/locutil.cpp @@ -0,0 +1,265 @@ +/** + ******************************************************************************* + * Copyright (C) 2002-2005, International Business Machines Corporation and * + * others. All Rights Reserved. * + ******************************************************************************* + * + ******************************************************************************* + */ +#include "unicode/utypes.h" + +#if !UCONFIG_NO_SERVICE + +#include "unicode/resbund.h" +#include "cmemory.h" +#include "ustrfmt.h" +#include "locutil.h" +#include "charstr.h" +#include "ucln_cmn.h" +#include "uassert.h" +#include "umutex.h" + +// see LocaleUtility::getAvailableLocaleNames +static Hashtable * LocaleUtility_cache = NULL; + +#define UNDERSCORE_CHAR ((UChar)0x005f) +#define AT_SIGN_CHAR ((UChar)64) +#define PERIOD_CHAR ((UChar)46) + +/* + ****************************************************************** + */ + +/** + * Release all static memory held by Locale Utility. + */ +U_CDECL_BEGIN +static UBool U_CALLCONV service_cleanup(void) { + if (LocaleUtility_cache) { + delete LocaleUtility_cache; + LocaleUtility_cache = NULL; + } + return TRUE; +} +U_CDECL_END + +U_NAMESPACE_BEGIN + +UnicodeString& +LocaleUtility::canonicalLocaleString(const UnicodeString* id, UnicodeString& result) +{ + if (id == NULL) { + result.setToBogus(); + } else { + // Fix case only (no other changes) up to the first '@' or '.' or + // end of string, whichever comes first. In 3.0 I changed this to + // stop at first '@' or '.'. It used to run out to the end of + // string. My fix makes the tests pass but is probably + // structurally incorrect. See below. [alan 3.0] + + // TODO: Doug, you might want to revise this... + result = *id; + int32_t i = 0; + int32_t end = result.indexOf(AT_SIGN_CHAR); + int32_t n = result.indexOf(PERIOD_CHAR); + if (n >= 0 && n < end) { + end = n; + } + if (end < 0) { + end = result.length(); + } + n = result.indexOf(UNDERSCORE_CHAR); + if (n < 0) { + n = end; + } + for (; i < n; ++i) { + UChar c = result.charAt(i); + if (c >= 0x0041 && c <= 0x005a) { + c += 0x20; + result.setCharAt(i, c); + } + } + for (n = end; i < n; ++i) { + UChar c = result.charAt(i); + if (c >= 0x0061 && c <= 0x007a) { + c -= 0x20; + result.setCharAt(i, c); + } + } + } + return result; + +#if 0 + // This code does a proper full level 2 canonicalization of id. + // It's nasty to go from UChar to char to char to UChar -- but + // that's what you have to do to use the uloc_canonicalize + // function on UnicodeStrings. + + // I ended up doing the alternate fix (see above) not for + // performance reasons, although performance will certainly be + // better, but because doing a full level 2 canonicalization + // causes some tests to fail. [alan 3.0] + + // TODO: Doug, you might want to revisit this... + result.setToBogus(); + if (id != 0) { + int32_t buflen = id->length() + 8; // space for NUL + char* buf = (char*) uprv_malloc(buflen); + char* canon = (buf == 0) ? 0 : (char*) uprv_malloc(buflen); + if (buf != 0 && canon != 0) { + U_ASSERT(id->extract(0, INT32_MAX, buf, buflen) < buflen); + UErrorCode ec = U_ZERO_ERROR; + uloc_canonicalize(buf, canon, buflen, &ec); + if (U_SUCCESS(ec)) { + result = UnicodeString(canon); + } + } + uprv_free(buf); + uprv_free(canon); + } + return result; +#endif +} + +Locale& +LocaleUtility::initLocaleFromName(const UnicodeString& id, Locale& result) +{ + enum { BUFLEN = 128 }; // larger than ever needed + + if (id.isBogus() || id.length() >= BUFLEN) { + result.setToBogus(); + } else { + /* + * We need to convert from a UnicodeString to char * in order to + * create a Locale. + * + * Problem: Locale ID strings may contain '@' which is a variant + * character and cannot be handled by invariant-character conversion. + * + * Hack: Since ICU code can handle locale IDs with multiple encodings + * of '@' (at least for EBCDIC; it's not known to be a problem for + * ASCII-based systems), + * we use regular invariant-character conversion for everything else + * and manually convert U+0040 into a compiler-char-constant '@'. + * While this compilation-time constant may not match the runtime + * encoding of '@', it should be one of the encodings which ICU + * recognizes. + * + * There should be only at most one '@' in a locale ID. + */ + char buffer[BUFLEN]; + int32_t prev, i; + prev = 0; + for(;;) { + i = id.indexOf((UChar)0x40, prev); + if(i < 0) { + // no @ between prev and the rest of the string + id.extract(prev, INT32_MAX, buffer + prev, BUFLEN - prev, US_INV); + break; // done + } else { + // normal invariant-character conversion for text between @s + id.extract(prev, i - prev, buffer + prev, BUFLEN - prev, US_INV); + // manually "convert" U+0040 at id[i] into '@' at buffer[i] + buffer[i] = '@'; + prev = i + 1; + } + } + result = Locale::createFromName(buffer); + } + return result; +} + +UnicodeString& +LocaleUtility::initNameFromLocale(const Locale& locale, UnicodeString& result) +{ + if (locale.isBogus()) { + result.setToBogus(); + } else { + result.append(UnicodeString(locale.getName(), -1, US_INV)); + } + return result; +} + +const Hashtable* +LocaleUtility::getAvailableLocaleNames(const UnicodeString& bundleID) +{ + // LocaleUtility_cache is a hash-of-hashes. The top-level keys + // are path strings ('bundleID') passed to + // ures_openAvailableLocales. The top-level values are + // second-level hashes. The second-level keys are result strings + // from ures_openAvailableLocales. The second-level values are + // garbage ((void*)1 or other random pointer). + + UErrorCode status = U_ZERO_ERROR; + Hashtable* cache; + umtx_lock(NULL); + cache = LocaleUtility_cache; + umtx_unlock(NULL); + + if (cache == NULL) { + cache = new Hashtable(status); + if (cache == NULL || U_FAILURE(status)) { + return NULL; // catastrophic failure; e.g. out of memory + } + cache->setValueDeleter(uhash_deleteHashtable); + Hashtable* h; // set this to final LocaleUtility_cache value + umtx_lock(NULL); + h = LocaleUtility_cache; + if (h == NULL) { + LocaleUtility_cache = h = cache; + cache = NULL; + ucln_common_registerCleanup(UCLN_COMMON_SERVICE, service_cleanup); + } + umtx_unlock(NULL); + delete cache; + cache = h; + } + + U_ASSERT(cache != NULL); + + Hashtable* htp; + umtx_lock(NULL); + htp = (Hashtable*) cache->get(bundleID); + umtx_unlock(NULL); + + if (htp == NULL) { + htp = new Hashtable(status); + if (htp && U_SUCCESS(status)) { + CharString cbundleID(bundleID); + const char* path = (const char*) cbundleID; + if (*path == 0) path = NULL; // empty string => NULL + UEnumeration *uenum = ures_openAvailableLocales(path, &status); + for (;;) { + const UChar* id = uenum_unext(uenum, NULL, &status); + if (id == NULL) { + break; + } + htp->put(UnicodeString(id), (void*)htp, status); + } + uenum_close(uenum); + if (U_FAILURE(status)) { + delete htp; + return NULL; + } + umtx_lock(NULL); + cache->put(bundleID, (void*)htp, status); + umtx_unlock(NULL); + } + } + return htp; +} + +UBool +LocaleUtility::isFallbackOf(const UnicodeString& root, const UnicodeString& child) +{ + return child.indexOf(root) == 0 && + (child.length() == root.length() || + child.charAt(root.length()) == UNDERSCORE_CHAR); +} + +U_NAMESPACE_END + +/* !UCONFIG_NO_SERVICE */ +#endif + + diff --git a/icu4c/source/common/locutil.h b/icu4c/source/common/locutil.h new file mode 100644 index 0000000000..1a8871b752 --- /dev/null +++ b/icu4c/source/common/locutil.h @@ -0,0 +1,32 @@ +/** + ******************************************************************************* + * Copyright (C) 2002-2005, International Business Machines Corporation and * + * others. All Rights Reserved. * + ******************************************************************************* + * + ******************************************************************************* + */ +#ifndef LOCUTIL_H +#define LOCUTIL_H + +#include "unicode/utypes.h" +#include "hash.h" + +U_NAMESPACE_BEGIN + +// temporary utility functions, till I know where to find them +// in header so tests can also access them + +class U_COMMON_API LocaleUtility { +public: + static UnicodeString& canonicalLocaleString(const UnicodeString* id, UnicodeString& result); + static Locale& initLocaleFromName(const UnicodeString& id, Locale& result); + static UnicodeString& initNameFromLocale(const Locale& locale, UnicodeString& result); + static const Hashtable* getAvailableLocaleNames(const UnicodeString& bundleID); + static UBool isFallbackOf(const UnicodeString& root, const UnicodeString& child); +}; + +U_NAMESPACE_END + + +#endif diff --git a/icu4c/source/common/servloc.h b/icu4c/source/common/servloc.h index de24048e5b..d08b09e36e 100644 --- a/icu4c/source/common/servloc.h +++ b/icu4c/source/common/servloc.h @@ -33,6 +33,7 @@ U_NAMESPACE_END #include "uvector.h" #include "serv.h" +#include "locutil.h" U_NAMESPACE_BEGIN @@ -539,18 +540,6 @@ class U_COMMON_API ICULocaleService : public ICUService friend class ServiceEnumeration; }; -// temporary utility functions, till I know where to find them -// in header so tests can also access them - -class U_COMMON_API LocaleUtility { -public: - static UnicodeString& canonicalLocaleString(const UnicodeString* id, UnicodeString& result); - static Locale& initLocaleFromName(const UnicodeString& id, Locale& result); - static UnicodeString& initNameFromLocale(const Locale& locale, UnicodeString& result); - static const Hashtable* getAvailableLocaleNames(const UnicodeString& bundleID); - static UBool isFallbackOf(const UnicodeString& root, const UnicodeString& child); -}; - U_NAMESPACE_END /* UCONFIG_NO_SERVICE */ diff --git a/icu4c/source/i18n/transreg.cpp b/icu4c/source/i18n/transreg.cpp index b232d53138..fa747abe58 100644 --- a/icu4c/source/i18n/transreg.cpp +++ b/icu4c/source/i18n/transreg.cpp @@ -28,7 +28,7 @@ #include "tridpars.h" #include "charstr.h" #include "uassert.h" -#include "servloc.h" +#include "locutil.h" // Enable the following symbol to add debugging code that tracks the // allocation, deletion, and use of Entry objects. BoundsChecker has