1999-08-16 21:50:52 +00:00
|
|
|
/*
|
2007-08-10 23:31:48 +00:00
|
|
|
******************************************************************************
|
2014-02-25 21:21:49 +00:00
|
|
|
* Copyright (C) 1996-2014, International Business Machines Corporation and
|
2010-05-19 17:29:33 +00:00
|
|
|
* others. All Rights Reserved.
|
2007-08-10 23:31:48 +00:00
|
|
|
******************************************************************************
|
|
|
|
*/
|
2001-01-18 00:23:29 +00:00
|
|
|
|
|
|
|
/**
|
2007-08-10 23:31:48 +00:00
|
|
|
* File coll.cpp
|
|
|
|
*
|
|
|
|
* Created by: Helena Shih
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 2/5/97 aliu Modified createDefault to load collation data from
|
|
|
|
* binary files when possible. Added related methods
|
|
|
|
* createCollationFromFile, chopLocale, createPathName.
|
|
|
|
* 2/11/97 aliu Added methods addToCache, findInCache, which implement
|
|
|
|
* a Collation cache. Modified createDefault to look in
|
|
|
|
* cache first, and also to store newly created Collation
|
|
|
|
* objects in the cache. Modified to not use gLocPath.
|
|
|
|
* 2/12/97 aliu Modified to create objects from RuleBasedCollator cache.
|
|
|
|
* Moved cache out of Collation class.
|
|
|
|
* 2/13/97 aliu Moved several methods out of this class and into
|
|
|
|
* RuleBasedCollator, with modifications. Modified
|
|
|
|
* createDefault() to call new RuleBasedCollator(Locale&)
|
|
|
|
* constructor. General clean up and documentation.
|
|
|
|
* 2/20/97 helena Added clone, operator==, operator!=, operator=, and copy
|
|
|
|
* constructor.
|
|
|
|
* 05/06/97 helena Added memory allocation error detection.
|
|
|
|
* 05/08/97 helena Added createInstance().
|
|
|
|
* 6/20/97 helena Java class name change.
|
|
|
|
* 04/23/99 stephen Removed EDecompositionMode, merged with
|
|
|
|
* Normalizer::EMode
|
|
|
|
* 11/23/9 srl Inlining of some critical functions
|
|
|
|
* 01/29/01 synwee Modified into a C++ wrapper calling C APIs (ucol.h)
|
2014-02-25 21:21:49 +00:00
|
|
|
* 2012-2014 markus Rewritten in C++ again.
|
2007-08-10 23:31:48 +00:00
|
|
|
*/
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2014-02-25 21:21:49 +00:00
|
|
|
#include "utypeinfo.h" // for 'typeid' to work
|
2012-08-03 21:49:28 +00:00
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_COLLATION
|
|
|
|
|
2001-01-18 00:23:29 +00:00
|
|
|
#include "unicode/coll.h"
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/tblcoll.h"
|
2014-02-25 21:21:49 +00:00
|
|
|
#include "collationdata.h"
|
|
|
|
#include "collationroot.h"
|
|
|
|
#include "collationtailoring.h"
|
2004-04-24 02:57:18 +00:00
|
|
|
#include "ucol_imp.h"
|
2006-10-04 06:05:30 +00:00
|
|
|
#include "cstring.h"
|
2000-06-03 04:37:12 +00:00
|
|
|
#include "cmemory.h"
|
2006-09-08 04:10:40 +00:00
|
|
|
#include "umutex.h"
|
2004-12-21 06:55:20 +00:00
|
|
|
#include "servloc.h"
|
2013-05-31 23:50:15 +00:00
|
|
|
#include "uassert.h"
|
2004-04-24 02:57:18 +00:00
|
|
|
#include "ustrenum.h"
|
2008-01-05 01:27:56 +00:00
|
|
|
#include "uresimp.h"
|
2003-05-13 23:12:37 +00:00
|
|
|
#include "ucln_in.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2011-07-06 04:03:35 +00:00
|
|
|
static icu::Locale* availableLocaleList = NULL;
|
2006-10-04 06:05:30 +00:00
|
|
|
static int32_t availableLocaleListCount;
|
2011-07-06 04:03:35 +00:00
|
|
|
static icu::ICULocaleService* gService = NULL;
|
2013-09-10 00:13:41 +00:00
|
|
|
static icu::UInitOnce gServiceInitOnce = U_INITONCE_INITIALIZER;
|
|
|
|
static icu::UInitOnce gAvailableLocaleListInitOnce;
|
2006-10-04 06:05:30 +00:00
|
|
|
|
2004-09-12 23:07:29 +00:00
|
|
|
/**
|
|
|
|
* Release all static memory held by collator.
|
|
|
|
*/
|
2004-10-07 17:26:53 +00:00
|
|
|
U_CDECL_BEGIN
|
2004-10-07 17:29:15 +00:00
|
|
|
static UBool U_CALLCONV collator_cleanup(void) {
|
2006-10-04 06:05:30 +00:00
|
|
|
#if !UCONFIG_NO_SERVICE
|
2004-09-12 23:07:29 +00:00
|
|
|
if (gService) {
|
|
|
|
delete gService;
|
|
|
|
gService = NULL;
|
|
|
|
}
|
2013-05-31 23:50:15 +00:00
|
|
|
gServiceInitOnce.reset();
|
2006-10-04 06:05:30 +00:00
|
|
|
#endif
|
|
|
|
if (availableLocaleList) {
|
|
|
|
delete []availableLocaleList;
|
|
|
|
availableLocaleList = NULL;
|
|
|
|
}
|
|
|
|
availableLocaleListCount = 0;
|
2013-05-31 23:50:15 +00:00
|
|
|
gAvailableLocaleListInitOnce.reset();
|
2004-09-12 23:07:29 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2006-10-04 06:05:30 +00:00
|
|
|
|
2004-10-07 17:26:53 +00:00
|
|
|
U_CDECL_END
|
2004-09-12 23:07:29 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2007-08-10 23:31:48 +00:00
|
|
|
#if !UCONFIG_NO_SERVICE
|
|
|
|
|
2003-04-28 21:13:14 +00:00
|
|
|
// ------------------------------------------
|
|
|
|
//
|
|
|
|
// Registration
|
|
|
|
//
|
|
|
|
|
|
|
|
//-------------------------------------------
|
|
|
|
|
2004-04-07 05:20:22 +00:00
|
|
|
CollatorFactory::~CollatorFactory() {}
|
|
|
|
|
|
|
|
//-------------------------------------------
|
|
|
|
|
2003-04-28 21:13:14 +00:00
|
|
|
UBool
|
|
|
|
CollatorFactory::visible(void) const {
|
2003-08-29 18:03:00 +00:00
|
|
|
return TRUE;
|
2003-04-28 21:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
CollatorFactory::getDisplayName(const Locale& objectLocale,
|
|
|
|
const Locale& displayLocale,
|
|
|
|
UnicodeString& result)
|
|
|
|
{
|
|
|
|
return objectLocale.getDisplayName(displayLocale, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
class ICUCollatorFactory : public ICUResourceBundleFactory {
|
2004-04-24 02:57:18 +00:00
|
|
|
public:
|
2011-07-26 05:32:25 +00:00
|
|
|
ICUCollatorFactory() : ICUResourceBundleFactory(UnicodeString(U_ICUDATA_COLL, -1, US_INV)) { }
|
|
|
|
virtual ~ICUCollatorFactory();
|
2004-04-24 02:57:18 +00:00
|
|
|
protected:
|
2003-08-29 18:03:00 +00:00
|
|
|
virtual UObject* create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const;
|
2003-04-28 21:13:14 +00:00
|
|
|
};
|
|
|
|
|
2011-07-26 05:32:25 +00:00
|
|
|
ICUCollatorFactory::~ICUCollatorFactory() {}
|
|
|
|
|
2003-04-28 21:13:14 +00:00
|
|
|
UObject*
|
2003-05-19 17:04:05 +00:00
|
|
|
ICUCollatorFactory::create(const ICUServiceKey& key, const ICUService* /* service */, UErrorCode& status) const {
|
2003-04-28 21:13:14 +00:00
|
|
|
if (handlesKey(key, status)) {
|
|
|
|
const LocaleKey& lkey = (const LocaleKey&)key;
|
|
|
|
Locale loc;
|
|
|
|
// make sure the requested locale is correct
|
|
|
|
// default LocaleFactory uses currentLocale since that's the one vetted by handlesKey
|
|
|
|
// but for ICU rb resources we use the actual one since it will fallback again
|
|
|
|
lkey.canonicalLocale(loc);
|
2003-08-29 18:03:00 +00:00
|
|
|
|
2003-04-28 21:13:14 +00:00
|
|
|
return Collator::makeInstance(loc, status);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
class ICUCollatorService : public ICULocaleService {
|
2003-08-29 18:03:00 +00:00
|
|
|
public:
|
2003-04-28 21:13:14 +00:00
|
|
|
ICUCollatorService()
|
2004-12-03 06:16:10 +00:00
|
|
|
: ICULocaleService(UNICODE_STRING_SIMPLE("Collator"))
|
2003-04-28 21:13:14 +00:00
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
registerFactory(new ICUCollatorFactory(), status);
|
|
|
|
}
|
2011-07-26 05:32:25 +00:00
|
|
|
|
|
|
|
virtual ~ICUCollatorService();
|
|
|
|
|
2003-04-28 21:13:14 +00:00
|
|
|
virtual UObject* cloneInstance(UObject* instance) const {
|
|
|
|
return ((Collator*)instance)->clone();
|
|
|
|
}
|
2003-08-29 18:03:00 +00:00
|
|
|
|
2003-04-28 21:13:14 +00:00
|
|
|
virtual UObject* handleDefault(const ICUServiceKey& key, UnicodeString* actualID, UErrorCode& status) const {
|
|
|
|
LocaleKey& lkey = (LocaleKey&)key;
|
2003-08-29 18:03:00 +00:00
|
|
|
if (actualID) {
|
2004-01-23 18:06:22 +00:00
|
|
|
// Ugly Hack Alert! We return an empty actualID to signal
|
|
|
|
// to callers that this is a default object, not a "real"
|
|
|
|
// service-created object. (TODO remove in 3.0) [aliu]
|
|
|
|
actualID->truncate(0);
|
2003-08-29 18:03:00 +00:00
|
|
|
}
|
2003-10-06 22:41:57 +00:00
|
|
|
Locale loc("");
|
2003-04-28 21:13:14 +00:00
|
|
|
lkey.canonicalLocale(loc);
|
|
|
|
return Collator::makeInstance(loc, status);
|
|
|
|
}
|
2003-08-29 18:03:00 +00:00
|
|
|
|
|
|
|
virtual UObject* getKey(ICUServiceKey& key, UnicodeString* actualReturn, UErrorCode& status) const {
|
|
|
|
UnicodeString ar;
|
|
|
|
if (actualReturn == NULL) {
|
|
|
|
actualReturn = &ar;
|
|
|
|
}
|
2014-02-25 21:21:49 +00:00
|
|
|
return (Collator*)ICULocaleService::getKey(key, actualReturn, status);
|
2003-08-29 18:03:00 +00:00
|
|
|
}
|
2003-05-02 22:17:14 +00:00
|
|
|
|
2003-04-28 21:13:14 +00:00
|
|
|
virtual UBool isDefault() const {
|
|
|
|
return countFactories() == 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-07-26 05:32:25 +00:00
|
|
|
ICUCollatorService::~ICUCollatorService() {}
|
|
|
|
|
2003-04-28 21:13:14 +00:00
|
|
|
// -------------------------------------
|
|
|
|
|
2013-05-31 23:50:15 +00:00
|
|
|
static void U_CALLCONV initService() {
|
|
|
|
gService = new ICUCollatorService();
|
|
|
|
ucln_i18n_registerCleanup(UCLN_I18N_COLLATOR, collator_cleanup);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-28 21:13:14 +00:00
|
|
|
static ICULocaleService*
|
|
|
|
getService(void)
|
|
|
|
{
|
2013-05-31 23:50:15 +00:00
|
|
|
umtx_initOnce(gServiceInitOnce, &initService);
|
2003-08-29 18:03:00 +00:00
|
|
|
return gService;
|
2003-04-28 21:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
2006-09-08 04:10:40 +00:00
|
|
|
static inline UBool
|
2003-04-28 21:13:14 +00:00
|
|
|
hasService(void)
|
|
|
|
{
|
2013-05-31 23:50:15 +00:00
|
|
|
UBool retVal = !gServiceInitOnce.isReset() && (getService() != NULL);
|
2006-09-08 04:10:40 +00:00
|
|
|
return retVal;
|
2003-04-28 21:13:14 +00:00
|
|
|
}
|
|
|
|
|
2004-07-18 01:37:13 +00:00
|
|
|
#endif /* UCONFIG_NO_SERVICE */
|
2003-04-28 21:13:14 +00:00
|
|
|
|
2013-05-31 23:50:15 +00:00
|
|
|
static void U_CALLCONV
|
|
|
|
initAvailableLocaleList(UErrorCode &status) {
|
|
|
|
U_ASSERT(availableLocaleListCount == 0);
|
|
|
|
U_ASSERT(availableLocaleList == NULL);
|
2006-10-04 06:05:30 +00:00
|
|
|
// for now, there is a hardcoded list, so just walk through that list and set it up.
|
2013-05-31 23:50:15 +00:00
|
|
|
UResourceBundle *index = NULL;
|
|
|
|
UResourceBundle installed;
|
|
|
|
int32_t i = 0;
|
|
|
|
|
|
|
|
ures_initStackObject(&installed);
|
|
|
|
index = ures_openDirect(U_ICUDATA_COLL, "res_index", &status);
|
|
|
|
ures_getByKey(index, "InstalledLocales", &installed, &status);
|
|
|
|
|
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
availableLocaleListCount = ures_getSize(&installed);
|
|
|
|
availableLocaleList = new Locale[availableLocaleListCount];
|
2006-10-04 06:05:30 +00:00
|
|
|
|
2013-05-31 23:50:15 +00:00
|
|
|
if (availableLocaleList != NULL) {
|
|
|
|
ures_resetIterator(&installed);
|
|
|
|
while(ures_hasNext(&installed)) {
|
|
|
|
const char *tempKey = NULL;
|
|
|
|
ures_getNextString(&installed, NULL, &tempKey, &status);
|
|
|
|
availableLocaleList[i++] = Locale(tempKey);
|
2006-10-04 06:05:30 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-31 23:50:15 +00:00
|
|
|
U_ASSERT(availableLocaleListCount == i);
|
|
|
|
ures_close(&installed);
|
2006-10-04 06:05:30 +00:00
|
|
|
}
|
2013-05-31 23:50:15 +00:00
|
|
|
ures_close(index);
|
|
|
|
ucln_i18n_registerCleanup(UCLN_I18N_COLLATOR, collator_cleanup);
|
2006-10-04 06:05:30 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 23:50:15 +00:00
|
|
|
static UBool isAvailableLocaleListInitialized(UErrorCode &status) {
|
|
|
|
umtx_initOnce(gAvailableLocaleListInitOnce, &initAvailableLocaleList, status);
|
|
|
|
return U_SUCCESS(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-18 00:23:29 +00:00
|
|
|
// Collator public methods -----------------------------------------------
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2014-05-27 18:50:10 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const char *name;
|
|
|
|
UColAttribute attr;
|
|
|
|
} collAttributes[] = {
|
|
|
|
{ "colStrength", UCOL_STRENGTH },
|
|
|
|
{ "colBackwards", UCOL_FRENCH_COLLATION },
|
|
|
|
{ "colCaseLevel", UCOL_CASE_LEVEL },
|
|
|
|
{ "colCaseFirst", UCOL_CASE_FIRST },
|
|
|
|
{ "colAlternate", UCOL_ALTERNATE_HANDLING },
|
|
|
|
{ "colNormalization", UCOL_NORMALIZATION_MODE },
|
|
|
|
{ "colNumeric", UCOL_NUMERIC_COLLATION }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const char *name;
|
|
|
|
UColAttributeValue value;
|
|
|
|
} collAttributeValues[] = {
|
|
|
|
{ "primary", UCOL_PRIMARY },
|
|
|
|
{ "secondary", UCOL_SECONDARY },
|
|
|
|
{ "tertiary", UCOL_TERTIARY },
|
|
|
|
{ "quaternary", UCOL_QUATERNARY },
|
|
|
|
// Note: Not supporting typo "quarternary" because it was never supported in locale IDs.
|
|
|
|
{ "identical", UCOL_IDENTICAL },
|
|
|
|
{ "no", UCOL_OFF },
|
|
|
|
{ "yes", UCOL_ON },
|
|
|
|
{ "shifted", UCOL_SHIFTED },
|
|
|
|
{ "non-ignorable", UCOL_NON_IGNORABLE },
|
|
|
|
{ "lower", UCOL_LOWER_FIRST },
|
|
|
|
{ "upper", UCOL_UPPER_FIRST }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *collReorderCodes[UCOL_REORDER_CODE_LIMIT - UCOL_REORDER_CODE_FIRST] = {
|
|
|
|
"space", "punct", "symbol", "currency", "digit"
|
|
|
|
};
|
|
|
|
|
|
|
|
int32_t getReorderCode(const char *s) {
|
2014-08-28 22:13:45 +00:00
|
|
|
for (int32_t i = 0; i < UPRV_LENGTHOF(collReorderCodes); ++i) {
|
2014-05-27 18:50:10 +00:00
|
|
|
if (uprv_stricmp(s, collReorderCodes[i]) == 0) {
|
|
|
|
return UCOL_REORDER_CODE_FIRST + i;
|
|
|
|
}
|
|
|
|
}
|
2014-07-09 22:53:09 +00:00
|
|
|
// Not supporting "others" = UCOL_REORDER_CODE_OTHERS
|
|
|
|
// as a synonym for Zzzz = USCRIPT_UNKNOWN for now:
|
|
|
|
// Avoid introducing synonyms/aliases.
|
2014-05-27 18:50:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets collation attributes according to locale keywords. See
|
|
|
|
* http://www.unicode.org/reports/tr35/tr35-collation.html#Collation_Settings
|
|
|
|
*
|
|
|
|
* Using "alias" keywords and values where defined:
|
|
|
|
* http://www.unicode.org/reports/tr35/tr35.html#Old_Locale_Extension_Syntax
|
|
|
|
* http://unicode.org/repos/cldr/trunk/common/bcp47/collation.xml
|
|
|
|
*/
|
|
|
|
void setAttributesFromKeywords(const Locale &loc, Collator &coll, UErrorCode &errorCode) {
|
|
|
|
if (U_FAILURE(errorCode)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (uprv_strcmp(loc.getName(), loc.getBaseName()) == 0) {
|
|
|
|
// No keywords.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char value[1024]; // The reordering value could be long.
|
|
|
|
// Check for collation keywords that were already deprecated
|
|
|
|
// before any were supported in createInstance() (except for "collation").
|
2014-08-28 22:13:45 +00:00
|
|
|
int32_t length = loc.getKeywordValue("colHiraganaQuaternary", value, UPRV_LENGTHOF(value), errorCode);
|
2014-05-27 18:50:10 +00:00
|
|
|
if (U_FAILURE(errorCode)) {
|
|
|
|
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (length != 0) {
|
|
|
|
errorCode = U_UNSUPPORTED_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2014-08-28 22:13:45 +00:00
|
|
|
length = loc.getKeywordValue("variableTop", value, UPRV_LENGTHOF(value), errorCode);
|
2014-05-27 18:50:10 +00:00
|
|
|
if (U_FAILURE(errorCode)) {
|
|
|
|
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (length != 0) {
|
|
|
|
errorCode = U_UNSUPPORTED_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Parse known collation keywords, ignore others.
|
|
|
|
if (errorCode == U_STRING_NOT_TERMINATED_WARNING) {
|
|
|
|
errorCode = U_ZERO_ERROR;
|
|
|
|
}
|
2014-08-28 22:13:45 +00:00
|
|
|
for (int32_t i = 0; i < UPRV_LENGTHOF(collAttributes); ++i) {
|
|
|
|
length = loc.getKeywordValue(collAttributes[i].name, value, UPRV_LENGTHOF(value), errorCode);
|
2014-05-27 18:50:10 +00:00
|
|
|
if (U_FAILURE(errorCode) || errorCode == U_STRING_NOT_TERMINATED_WARNING) {
|
|
|
|
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (length == 0) { continue; }
|
|
|
|
for (int32_t j = 0;; ++j) {
|
2014-08-28 22:13:45 +00:00
|
|
|
if (j == UPRV_LENGTHOF(collAttributeValues)) {
|
2014-05-27 18:50:10 +00:00
|
|
|
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (uprv_stricmp(value, collAttributeValues[j].name) == 0) {
|
|
|
|
coll.setAttribute(collAttributes[i].attr, collAttributeValues[j].value, errorCode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-28 22:13:45 +00:00
|
|
|
length = loc.getKeywordValue("colReorder", value, UPRV_LENGTHOF(value), errorCode);
|
2014-05-27 18:50:10 +00:00
|
|
|
if (U_FAILURE(errorCode) || errorCode == U_STRING_NOT_TERMINATED_WARNING) {
|
|
|
|
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (length != 0) {
|
2014-05-28 23:03:52 +00:00
|
|
|
int32_t codes[USCRIPT_CODE_LIMIT + UCOL_REORDER_CODE_LIMIT - UCOL_REORDER_CODE_FIRST];
|
2014-05-27 18:50:10 +00:00
|
|
|
int32_t codesLength = 0;
|
|
|
|
char *scriptName = value;
|
|
|
|
for (;;) {
|
2014-08-28 22:13:45 +00:00
|
|
|
if (codesLength == UPRV_LENGTHOF(codes)) {
|
2014-05-27 18:50:10 +00:00
|
|
|
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char *limit = scriptName;
|
|
|
|
char c;
|
|
|
|
while ((c = *limit) != 0 && c != '-') { ++limit; }
|
|
|
|
*limit = 0;
|
|
|
|
int32_t code;
|
|
|
|
if ((limit - scriptName) == 4) {
|
|
|
|
// Strict parsing, accept only 4-letter script codes, not long names.
|
|
|
|
code = u_getPropertyValueEnum(UCHAR_SCRIPT, scriptName);
|
|
|
|
} else {
|
|
|
|
code = getReorderCode(scriptName);
|
|
|
|
}
|
|
|
|
if (code < 0) {
|
|
|
|
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
codes[codesLength++] = code;
|
|
|
|
if (c == 0) { break; }
|
|
|
|
scriptName = limit + 1;
|
|
|
|
}
|
|
|
|
coll.setReorderCodes(codes, codesLength, errorCode);
|
|
|
|
}
|
2014-08-28 22:13:45 +00:00
|
|
|
length = loc.getKeywordValue("kv", value, UPRV_LENGTHOF(value), errorCode);
|
2014-05-27 18:50:10 +00:00
|
|
|
if (U_FAILURE(errorCode) || errorCode == U_STRING_NOT_TERMINATED_WARNING) {
|
|
|
|
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (length != 0) {
|
|
|
|
int32_t code = getReorderCode(value);
|
|
|
|
if (code < 0) {
|
|
|
|
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
coll.setMaxVariable((UColReorderCode)code, errorCode);
|
|
|
|
}
|
|
|
|
if (U_FAILURE(errorCode)) {
|
|
|
|
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
Collator* U_EXPORT2 Collator::createInstance(UErrorCode& success)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-08-29 18:03:00 +00:00
|
|
|
return createInstance(Locale::getDefault(), success);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
Collator* U_EXPORT2 Collator::createInstance(const Locale& desiredLocale,
|
2003-04-28 21:13:14 +00:00
|
|
|
UErrorCode& status)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-08-29 18:03:00 +00:00
|
|
|
if (U_FAILURE(status))
|
|
|
|
return 0;
|
2014-05-27 18:50:10 +00:00
|
|
|
if (desiredLocale.isBogus()) {
|
|
|
|
// Locale constructed from malformed locale ID or language tag.
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Collator* coll;
|
2004-07-18 01:37:13 +00:00
|
|
|
#if !UCONFIG_NO_SERVICE
|
2003-08-29 18:03:00 +00:00
|
|
|
if (hasService()) {
|
2004-01-23 18:06:22 +00:00
|
|
|
Locale actualLoc;
|
2014-05-27 18:50:10 +00:00
|
|
|
coll = (Collator*)gService->get(desiredLocale, &actualLoc, status);
|
|
|
|
} else
|
2004-07-18 01:37:13 +00:00
|
|
|
#endif
|
2014-05-27 18:50:10 +00:00
|
|
|
{
|
|
|
|
coll = makeInstance(desiredLocale, status);
|
|
|
|
}
|
|
|
|
setAttributesFromKeywords(desiredLocale, *coll, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete coll;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return coll;
|
2003-04-28 21:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-20 21:46:02 +00:00
|
|
|
Collator* Collator::makeInstance(const Locale& desiredLocale, UErrorCode& status) {
|
|
|
|
const CollationCacheEntry *entry = CollationLoader::loadTailoring(desiredLocale, status);
|
2014-02-25 21:21:49 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
2014-08-20 21:46:02 +00:00
|
|
|
Collator *result = new RuleBasedCollator(entry);
|
2014-02-25 21:21:49 +00:00
|
|
|
if (result != NULL) {
|
2014-08-20 21:46:02 +00:00
|
|
|
// Both the unified cache's get() and the RBC constructor
|
|
|
|
// did addRef(). Undo one of them.
|
|
|
|
entry->removeRef();
|
2014-02-25 21:21:49 +00:00
|
|
|
return result;
|
|
|
|
}
|
2003-08-29 18:03:00 +00:00
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
2014-08-20 21:46:02 +00:00
|
|
|
if (entry != NULL) {
|
|
|
|
// Undo the addRef() from the cache.get().
|
|
|
|
entry->removeRef();
|
2003-08-29 18:03:00 +00:00
|
|
|
}
|
2014-02-25 21:21:49 +00:00
|
|
|
return NULL;
|
2001-03-14 00:23:47 +00:00
|
|
|
}
|
|
|
|
|
2012-08-08 21:03:52 +00:00
|
|
|
Collator *
|
|
|
|
Collator::safeClone() const {
|
|
|
|
return clone();
|
|
|
|
}
|
|
|
|
|
2003-05-01 23:23:52 +00:00
|
|
|
// implement deprecated, previously abstract method
|
|
|
|
Collator::EComparisonResult Collator::compare(const UnicodeString& source,
|
|
|
|
const UnicodeString& target) const
|
|
|
|
{
|
2003-08-29 18:03:00 +00:00
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
2012-08-03 21:49:28 +00:00
|
|
|
return (EComparisonResult)compare(source, target, ec);
|
2003-05-01 23:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// implement deprecated, previously abstract method
|
|
|
|
Collator::EComparisonResult Collator::compare(const UnicodeString& source,
|
|
|
|
const UnicodeString& target,
|
|
|
|
int32_t length) const
|
|
|
|
{
|
2003-08-29 18:03:00 +00:00
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
2012-08-03 21:49:28 +00:00
|
|
|
return (EComparisonResult)compare(source, target, length, ec);
|
2003-05-01 23:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// implement deprecated, previously abstract method
|
|
|
|
Collator::EComparisonResult Collator::compare(const UChar* source, int32_t sourceLength,
|
|
|
|
const UChar* target, int32_t targetLength)
|
|
|
|
const
|
|
|
|
{
|
2003-08-29 18:03:00 +00:00
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
2012-08-03 21:49:28 +00:00
|
|
|
return (EComparisonResult)compare(source, sourceLength, target, targetLength, ec);
|
2003-05-01 23:23:52 +00:00
|
|
|
}
|
|
|
|
|
2009-03-27 00:37:55 +00:00
|
|
|
UCollationResult Collator::compare(UCharIterator &/*sIter*/,
|
|
|
|
UCharIterator &/*tIter*/,
|
2009-03-13 23:38:43 +00:00
|
|
|
UErrorCode &status) const {
|
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
// Not implemented in the base class.
|
|
|
|
status = U_UNSUPPORTED_ERROR;
|
|
|
|
}
|
|
|
|
return UCOL_EQUAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
UCollationResult Collator::compareUTF8(const StringPiece &source,
|
|
|
|
const StringPiece &target,
|
|
|
|
UErrorCode &status) const {
|
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
return UCOL_EQUAL;
|
|
|
|
}
|
|
|
|
UCharIterator sIter, tIter;
|
|
|
|
uiter_setUTF8(&sIter, source.data(), source.length());
|
|
|
|
uiter_setUTF8(&tIter, target.data(), target.length());
|
|
|
|
return compare(sIter, tIter, status);
|
|
|
|
}
|
|
|
|
|
2001-01-18 00:23:29 +00:00
|
|
|
UBool Collator::equals(const UnicodeString& source,
|
2003-08-29 18:03:00 +00:00
|
|
|
const UnicodeString& target) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-08-29 18:03:00 +00:00
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
return (compare(source, target, ec) == UCOL_EQUAL);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-01-18 00:23:29 +00:00
|
|
|
UBool Collator::greaterOrEqual(const UnicodeString& source,
|
2003-08-29 18:03:00 +00:00
|
|
|
const UnicodeString& target) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-08-29 18:03:00 +00:00
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
return (compare(source, target, ec) != UCOL_LESS);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-01-18 00:23:29 +00:00
|
|
|
UBool Collator::greater(const UnicodeString& source,
|
2003-08-29 18:03:00 +00:00
|
|
|
const UnicodeString& target) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-08-29 18:03:00 +00:00
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
return (compare(source, target, ec) == UCOL_GREATER);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2003-04-28 21:13:14 +00:00
|
|
|
// this API ignores registered collators, since it returns an
|
|
|
|
// array of indefinite lifetime
|
2004-08-24 17:38:33 +00:00
|
|
|
const Locale* U_EXPORT2 Collator::getAvailableLocales(int32_t& count)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2006-10-04 06:05:30 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
Locale *result = NULL;
|
|
|
|
count = 0;
|
|
|
|
if (isAvailableLocaleListInitialized(status))
|
|
|
|
{
|
|
|
|
result = availableLocaleList;
|
|
|
|
count = availableLocaleListCount;
|
|
|
|
}
|
|
|
|
return result;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-01-18 00:23:29 +00:00
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
UnicodeString& U_EXPORT2 Collator::getDisplayName(const Locale& objectLocale,
|
2003-08-29 18:03:00 +00:00
|
|
|
const Locale& displayLocale,
|
|
|
|
UnicodeString& name)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2004-07-18 01:37:13 +00:00
|
|
|
#if !UCONFIG_NO_SERVICE
|
2003-08-29 18:03:00 +00:00
|
|
|
if (hasService()) {
|
2005-03-09 23:50:26 +00:00
|
|
|
UnicodeString locNameStr;
|
|
|
|
LocaleUtility::initNameFromLocale(objectLocale, locNameStr);
|
|
|
|
return gService->getDisplayName(locNameStr, name, displayLocale);
|
2003-08-29 18:03:00 +00:00
|
|
|
}
|
2004-07-18 01:37:13 +00:00
|
|
|
#endif
|
2003-08-29 18:03:00 +00:00
|
|
|
return objectLocale.getDisplayName(displayLocale, name);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
UnicodeString& U_EXPORT2 Collator::getDisplayName(const Locale& objectLocale,
|
2003-08-29 18:03:00 +00:00
|
|
|
UnicodeString& name)
|
2001-01-18 00:23:29 +00:00
|
|
|
{
|
2003-08-29 18:03:00 +00:00
|
|
|
return getDisplayName(objectLocale, Locale::getDefault(), name);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2002-02-28 20:01:48 +00:00
|
|
|
/* This is useless information */
|
|
|
|
/*void Collator::getVersion(UVersionInfo versionInfo) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-01-18 00:23:29 +00:00
|
|
|
if (versionInfo!=NULL)
|
|
|
|
uprv_memcpy(versionInfo, fVersion, U_MAX_VERSION_LENGTH);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2002-02-28 20:01:48 +00:00
|
|
|
*/
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-01-18 00:23:29 +00:00
|
|
|
// UCollator protected constructor destructor ----------------------------
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-01-18 00:23:29 +00:00
|
|
|
/**
|
|
|
|
* Default constructor.
|
|
|
|
* Constructor is different from the old default Collator constructor.
|
|
|
|
* The task for determing the default collation strength and normalization mode
|
|
|
|
* is left to the child class.
|
|
|
|
*/
|
|
|
|
Collator::Collator()
|
2003-08-29 18:03:00 +00:00
|
|
|
: UObject()
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2001-01-18 00:23:29 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
* Empty constructor, does not handle the arguments.
|
|
|
|
* This constructor is done for backward compatibility with 1.7 and 1.8.
|
|
|
|
* The task for handling the argument collation strength and normalization
|
|
|
|
* mode is left to the child class.
|
|
|
|
* @param collationStrength collation strength
|
2002-11-21 22:52:46 +00:00
|
|
|
* @param decompositionMode
|
|
|
|
* @deprecated 2.4 use the default constructor instead
|
2001-01-18 00:23:29 +00:00
|
|
|
*/
|
2002-11-21 22:52:46 +00:00
|
|
|
Collator::Collator(UCollationStrength, UNormalizationMode )
|
2003-08-29 18:03:00 +00:00
|
|
|
: UObject()
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2001-01-18 00:23:29 +00:00
|
|
|
Collator::~Collator()
|
|
|
|
{
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2002-07-02 23:58:34 +00:00
|
|
|
Collator::Collator(const Collator &other)
|
|
|
|
: UObject(other)
|
2000-06-03 04:18:06 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-08-29 18:03:00 +00:00
|
|
|
UBool Collator::operator==(const Collator& other) const
|
|
|
|
{
|
2012-08-03 21:49:28 +00:00
|
|
|
// Subclasses: Call this method and then add more specific checks.
|
|
|
|
return typeid(*this) == typeid(other);
|
2003-08-29 18:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UBool Collator::operator!=(const Collator& other) const
|
|
|
|
{
|
|
|
|
return (UBool)!(*this == other);
|
|
|
|
}
|
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
int32_t U_EXPORT2 Collator::getBound(const uint8_t *source,
|
2003-08-29 18:03:00 +00:00
|
|
|
int32_t sourceLength,
|
|
|
|
UColBoundMode boundType,
|
|
|
|
uint32_t noOfLevels,
|
|
|
|
uint8_t *result,
|
|
|
|
int32_t resultLength,
|
|
|
|
UErrorCode &status)
|
|
|
|
{
|
|
|
|
return ucol_getBound(source, sourceLength, boundType, noOfLevels, result, resultLength, &status);
|
2002-03-21 21:21:08 +00:00
|
|
|
}
|
|
|
|
|
2003-04-28 21:13:14 +00:00
|
|
|
void
|
2007-12-18 01:19:16 +00:00
|
|
|
Collator::setLocales(const Locale& /* requestedLocale */, const Locale& /* validLocale */, const Locale& /*actualLocale*/) {
|
2003-04-28 21:13:14 +00:00
|
|
|
}
|
|
|
|
|
2003-08-29 18:03:00 +00:00
|
|
|
UnicodeSet *Collator::getTailoredSet(UErrorCode &status) const
|
|
|
|
{
|
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// everything can be changed
|
|
|
|
return new UnicodeSet(0, 0x10FFFF);
|
|
|
|
}
|
|
|
|
|
2003-04-28 21:13:14 +00:00
|
|
|
// -------------------------------------
|
|
|
|
|
2004-07-18 01:37:13 +00:00
|
|
|
#if !UCONFIG_NO_SERVICE
|
2004-08-24 17:38:33 +00:00
|
|
|
URegistryKey U_EXPORT2
|
2003-04-28 21:13:14 +00:00
|
|
|
Collator::registerInstance(Collator* toAdopt, const Locale& locale, UErrorCode& status)
|
|
|
|
{
|
|
|
|
if (U_SUCCESS(status)) {
|
2014-02-25 21:21:49 +00:00
|
|
|
// Set the collator locales while registering so that createInstance()
|
|
|
|
// need not guess whether the collator's locales are already set properly
|
|
|
|
// (as they are by the data loader).
|
|
|
|
toAdopt->setLocales(locale, locale, locale);
|
2003-04-28 21:13:14 +00:00
|
|
|
return getService()->registerInstance(toAdopt, locale, status);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
class CFactory : public LocaleKeyFactory {
|
|
|
|
private:
|
|
|
|
CollatorFactory* _delegate;
|
|
|
|
Hashtable* _ids;
|
2003-08-29 18:03:00 +00:00
|
|
|
|
2003-04-28 21:13:14 +00:00
|
|
|
public:
|
2003-05-08 22:21:05 +00:00
|
|
|
CFactory(CollatorFactory* delegate, UErrorCode& status)
|
2003-04-28 21:13:14 +00:00
|
|
|
: LocaleKeyFactory(delegate->visible() ? VISIBLE : INVISIBLE)
|
|
|
|
, _delegate(delegate)
|
|
|
|
, _ids(NULL)
|
|
|
|
{
|
2003-08-29 18:03:00 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
int32_t count = 0;
|
|
|
|
_ids = new Hashtable(status);
|
|
|
|
if (_ids) {
|
|
|
|
const UnicodeString * idlist = _delegate->getSupportedIDs(count, status);
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
_ids->put(idlist[i], (void*)this, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
delete _ids;
|
|
|
|
_ids = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
2003-04-28 21:13:14 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-26 05:32:25 +00:00
|
|
|
|
|
|
|
virtual ~CFactory();
|
|
|
|
|
2003-04-28 21:13:14 +00:00
|
|
|
virtual UObject* create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const;
|
2003-08-29 18:03:00 +00:00
|
|
|
|
|
|
|
protected:
|
2003-04-28 21:13:14 +00:00
|
|
|
virtual const Hashtable* getSupportedIDs(UErrorCode& status) const
|
|
|
|
{
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
return _ids;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-08-29 18:03:00 +00:00
|
|
|
|
2003-04-28 21:13:14 +00:00
|
|
|
virtual UnicodeString&
|
|
|
|
getDisplayName(const UnicodeString& id, const Locale& locale, UnicodeString& result) const;
|
|
|
|
};
|
|
|
|
|
2011-07-26 05:32:25 +00:00
|
|
|
CFactory::~CFactory()
|
|
|
|
{
|
|
|
|
delete _delegate;
|
|
|
|
delete _ids;
|
|
|
|
}
|
|
|
|
|
2003-04-28 21:13:14 +00:00
|
|
|
UObject*
|
2003-05-19 17:04:05 +00:00
|
|
|
CFactory::create(const ICUServiceKey& key, const ICUService* /* service */, UErrorCode& status) const
|
2003-04-28 21:13:14 +00:00
|
|
|
{
|
|
|
|
if (handlesKey(key, status)) {
|
|
|
|
const LocaleKey& lkey = (const LocaleKey&)key;
|
|
|
|
Locale validLoc;
|
|
|
|
lkey.currentLocale(validLoc);
|
2003-08-29 18:03:00 +00:00
|
|
|
return _delegate->createCollator(validLoc);
|
2003-04-28 21:13:14 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
CFactory::getDisplayName(const UnicodeString& id, const Locale& locale, UnicodeString& result) const
|
|
|
|
{
|
|
|
|
if ((_coverage & 0x1) == 0) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
const Hashtable* ids = getSupportedIDs(status);
|
|
|
|
if (ids && (ids->get(id) != NULL)) {
|
|
|
|
Locale loc;
|
|
|
|
LocaleUtility::initLocaleFromName(id, loc);
|
|
|
|
return _delegate->getDisplayName(loc, locale, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.setToBogus();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
URegistryKey U_EXPORT2
|
2003-04-28 21:13:14 +00:00
|
|
|
Collator::registerFactory(CollatorFactory* toAdopt, UErrorCode& status)
|
|
|
|
{
|
|
|
|
if (U_SUCCESS(status)) {
|
2003-05-08 22:21:05 +00:00
|
|
|
CFactory* f = new CFactory(toAdopt, status);
|
2003-04-28 21:13:14 +00:00
|
|
|
if (f) {
|
|
|
|
return getService()->registerFactory(f, status);
|
|
|
|
}
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
UBool U_EXPORT2
|
2003-04-28 21:13:14 +00:00
|
|
|
Collator::unregister(URegistryKey key, UErrorCode& status)
|
|
|
|
{
|
2003-08-29 18:03:00 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
if (hasService()) {
|
|
|
|
return gService->unregister(key, status);
|
|
|
|
}
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
2003-04-28 21:13:14 +00:00
|
|
|
}
|
2003-08-29 18:03:00 +00:00
|
|
|
return FALSE;
|
2003-04-28 21:13:14 +00:00
|
|
|
}
|
2006-10-04 06:05:30 +00:00
|
|
|
#endif /* UCONFIG_NO_SERVICE */
|
|
|
|
|
|
|
|
class CollationLocaleListEnumeration : public StringEnumeration {
|
|
|
|
private:
|
|
|
|
int32_t index;
|
|
|
|
public:
|
|
|
|
static UClassID U_EXPORT2 getStaticClassID(void);
|
|
|
|
virtual UClassID getDynamicClassID(void) const;
|
|
|
|
public:
|
|
|
|
CollationLocaleListEnumeration()
|
|
|
|
: index(0)
|
|
|
|
{
|
|
|
|
// The global variables should already be initialized.
|
|
|
|
//isAvailableLocaleListInitialized(status);
|
|
|
|
}
|
|
|
|
|
2011-07-26 05:32:25 +00:00
|
|
|
virtual ~CollationLocaleListEnumeration();
|
2006-10-04 06:05:30 +00:00
|
|
|
|
|
|
|
virtual StringEnumeration * clone() const
|
|
|
|
{
|
|
|
|
CollationLocaleListEnumeration *result = new CollationLocaleListEnumeration();
|
2008-01-09 17:30:59 +00:00
|
|
|
if (result) {
|
2008-02-23 19:15:18 +00:00
|
|
|
result->index = index;
|
2008-01-09 17:30:59 +00:00
|
|
|
}
|
2006-10-04 06:05:30 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int32_t count(UErrorCode &/*status*/) const {
|
|
|
|
return availableLocaleListCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char* next(int32_t* resultLength, UErrorCode& /*status*/) {
|
|
|
|
const char* result;
|
|
|
|
if(index < availableLocaleListCount) {
|
|
|
|
result = availableLocaleList[index++].getName();
|
|
|
|
if(resultLength != NULL) {
|
2009-11-11 15:47:22 +00:00
|
|
|
*resultLength = (int32_t)uprv_strlen(result);
|
2006-10-04 06:05:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(resultLength != NULL) {
|
|
|
|
*resultLength = 0;
|
|
|
|
}
|
|
|
|
result = NULL;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const UnicodeString* snext(UErrorCode& status) {
|
|
|
|
int32_t resultLength = 0;
|
|
|
|
const char *s = next(&resultLength, status);
|
|
|
|
return setChars(s, resultLength, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void reset(UErrorCode& /*status*/) {
|
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-07-26 05:32:25 +00:00
|
|
|
CollationLocaleListEnumeration::~CollationLocaleListEnumeration() {}
|
|
|
|
|
2006-10-04 06:05:30 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CollationLocaleListEnumeration)
|
|
|
|
|
2003-04-28 21:13:14 +00:00
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
StringEnumeration* U_EXPORT2
|
2003-04-28 21:13:14 +00:00
|
|
|
Collator::getAvailableLocales(void)
|
|
|
|
{
|
2006-10-04 06:05:30 +00:00
|
|
|
#if !UCONFIG_NO_SERVICE
|
|
|
|
if (hasService()) {
|
|
|
|
return getService()->getAvailableLocales();
|
|
|
|
}
|
2004-07-18 01:37:13 +00:00
|
|
|
#endif /* UCONFIG_NO_SERVICE */
|
2006-10-04 06:05:30 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
if (isAvailableLocaleListInitialized(status)) {
|
|
|
|
return new CollationLocaleListEnumeration();
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-03-21 21:21:08 +00:00
|
|
|
|
2014-12-09 23:08:31 +00:00
|
|
|
static StringEnumeration *
|
|
|
|
newUStringEnumeration(UEnumeration *uenumToAdopt, UErrorCode &status) {
|
2004-04-24 02:57:18 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2014-12-09 23:08:31 +00:00
|
|
|
uenum_close(uenumToAdopt);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
StringEnumeration *result = new UStringEnumeration(uenumToAdopt);
|
|
|
|
if (result == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
uenum_close(uenumToAdopt);
|
2004-04-24 02:57:18 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-12-09 23:08:31 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringEnumeration* U_EXPORT2
|
|
|
|
Collator::getKeywords(UErrorCode& status) {
|
|
|
|
return newUStringEnumeration(ucol_getKeywords(&status), status);
|
2004-04-24 02:57:18 +00:00
|
|
|
}
|
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
StringEnumeration* U_EXPORT2
|
2004-04-24 02:57:18 +00:00
|
|
|
Collator::getKeywordValues(const char *keyword, UErrorCode& status) {
|
2014-12-09 23:08:31 +00:00
|
|
|
return newUStringEnumeration(
|
|
|
|
ucol_getKeywordValues(keyword, &status), status);
|
2004-04-24 02:57:18 +00:00
|
|
|
}
|
|
|
|
|
2009-02-14 02:32:41 +00:00
|
|
|
StringEnumeration* U_EXPORT2
|
|
|
|
Collator::getKeywordValuesForLocale(const char* key, const Locale& locale,
|
|
|
|
UBool commonlyUsed, UErrorCode& status) {
|
2014-12-09 23:08:31 +00:00
|
|
|
return newUStringEnumeration(
|
|
|
|
ucol_getKeywordValuesForLocale(
|
|
|
|
key, locale.getName(), commonlyUsed, &status),
|
|
|
|
status);
|
2009-02-14 02:32:41 +00:00
|
|
|
}
|
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
Locale U_EXPORT2
|
2004-05-03 18:15:13 +00:00
|
|
|
Collator::getFunctionalEquivalent(const char* keyword, const Locale& locale,
|
|
|
|
UBool& isAvailable, UErrorCode& status) {
|
2004-04-24 02:57:18 +00:00
|
|
|
// This is a wrapper over ucol_getFunctionalEquivalent
|
|
|
|
char loc[ULOC_FULLNAME_CAPACITY];
|
2004-04-29 18:54:22 +00:00
|
|
|
/*int32_t len =*/ ucol_getFunctionalEquivalent(loc, sizeof(loc),
|
2004-05-18 02:04:31 +00:00
|
|
|
keyword, locale.getName(), &isAvailable, &status);
|
2004-04-24 02:57:18 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
*loc = 0; // root
|
|
|
|
}
|
|
|
|
return Locale::createFromName(loc);
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:49:28 +00:00
|
|
|
Collator::ECollationStrength
|
|
|
|
Collator::getStrength(void) const {
|
|
|
|
UErrorCode intStatus = U_ZERO_ERROR;
|
|
|
|
return (ECollationStrength)getAttribute(UCOL_STRENGTH, intStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Collator::setStrength(ECollationStrength newStrength) {
|
|
|
|
UErrorCode intStatus = U_ZERO_ERROR;
|
|
|
|
setAttribute(UCOL_STRENGTH, (UColAttributeValue)newStrength, intStatus);
|
|
|
|
}
|
|
|
|
|
2014-02-25 21:21:49 +00:00
|
|
|
Collator &
|
|
|
|
Collator::setMaxVariable(UColReorderCode /*group*/, UErrorCode &errorCode) {
|
|
|
|
if (U_SUCCESS(errorCode)) {
|
|
|
|
errorCode = U_UNSUPPORTED_ERROR;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
UColReorderCode
|
|
|
|
Collator::getMaxVariable() const {
|
|
|
|
return UCOL_REORDER_CODE_PUNCTUATION;
|
|
|
|
}
|
|
|
|
|
2012-08-03 23:12:28 +00:00
|
|
|
int32_t
|
2011-05-04 00:06:12 +00:00
|
|
|
Collator::getReorderCodes(int32_t* /* dest*/,
|
|
|
|
int32_t /* destCapacity*/,
|
|
|
|
UErrorCode& status) const
|
2010-10-27 18:02:52 +00:00
|
|
|
{
|
2010-11-04 20:12:39 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
status = U_UNSUPPORTED_ERROR;
|
|
|
|
}
|
2010-10-30 00:42:12 +00:00
|
|
|
return 0;
|
2010-10-27 18:02:52 +00:00
|
|
|
}
|
|
|
|
|
2012-08-03 23:12:28 +00:00
|
|
|
void
|
2011-05-04 00:06:12 +00:00
|
|
|
Collator::setReorderCodes(const int32_t* /* reorderCodes */,
|
|
|
|
int32_t /* reorderCodesLength */,
|
|
|
|
UErrorCode& status)
|
2010-10-27 18:02:52 +00:00
|
|
|
{
|
2010-11-04 20:12:39 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
status = U_UNSUPPORTED_ERROR;
|
|
|
|
}
|
2010-10-27 18:02:52 +00:00
|
|
|
}
|
|
|
|
|
2014-02-25 21:21:49 +00:00
|
|
|
int32_t
|
|
|
|
Collator::getEquivalentReorderCodes(int32_t reorderCode,
|
|
|
|
int32_t *dest, int32_t capacity,
|
|
|
|
UErrorCode &errorCode) {
|
|
|
|
if(U_FAILURE(errorCode)) { return 0; }
|
|
|
|
if(capacity < 0 || (dest == NULL && capacity > 0)) {
|
|
|
|
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
2011-03-18 22:52:30 +00:00
|
|
|
}
|
2014-02-25 21:21:49 +00:00
|
|
|
const CollationData *baseData = CollationRoot::getData(errorCode);
|
|
|
|
if(U_FAILURE(errorCode)) { return 0; }
|
|
|
|
return baseData->getEquivalentScripts(reorderCode, dest, capacity, errorCode);
|
2011-03-18 22:52:30 +00:00
|
|
|
}
|
|
|
|
|
2012-02-19 09:14:56 +00:00
|
|
|
int32_t
|
|
|
|
Collator::internalGetShortDefinitionString(const char * /*locale*/,
|
|
|
|
char * /*buffer*/,
|
2012-02-18 23:10:51 +00:00
|
|
|
int32_t /*capacity*/,
|
2012-02-19 06:39:16 +00:00
|
|
|
UErrorCode &status) const {
|
2012-02-18 23:10:51 +00:00
|
|
|
if(U_SUCCESS(status)) {
|
|
|
|
status = U_UNSUPPORTED_ERROR; /* Shouldn't happen, internal function */
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-25 21:21:49 +00:00
|
|
|
UCollationResult
|
|
|
|
Collator::internalCompareUTF8(const char *left, int32_t leftLength,
|
|
|
|
const char *right, int32_t rightLength,
|
|
|
|
UErrorCode &errorCode) const {
|
|
|
|
if(U_FAILURE(errorCode)) { return UCOL_EQUAL; }
|
|
|
|
if((left == NULL && leftLength != 0) || (right == NULL && rightLength != 0)) {
|
|
|
|
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return UCOL_EQUAL;
|
|
|
|
}
|
|
|
|
return compareUTF8(
|
|
|
|
StringPiece(left, (leftLength < 0) ? uprv_strlen(left) : leftLength),
|
|
|
|
StringPiece(right, (rightLength < 0) ? uprv_strlen(right) : rightLength),
|
|
|
|
errorCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
Collator::internalNextSortKeyPart(UCharIterator * /*iter*/, uint32_t /*state*/[2],
|
|
|
|
uint8_t * /*dest*/, int32_t /*count*/, UErrorCode &errorCode) const {
|
|
|
|
if (U_SUCCESS(errorCode)) {
|
|
|
|
errorCode = U_UNSUPPORTED_ERROR;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-01-18 00:23:29 +00:00
|
|
|
// UCollator private data members ----------------------------------------
|
|
|
|
|
2002-02-28 20:01:48 +00:00
|
|
|
/* This is useless information */
|
|
|
|
/*const UVersionInfo Collator::fVersion = {1, 1, 0, 0};*/
|
2001-10-08 23:26:58 +00:00
|
|
|
|
2003-04-28 21:13:14 +00:00
|
|
|
// -------------------------------------
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_COLLATION */
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
/* eof */
|