intl/icu/source/i18n/coll.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 ******************************************************************************
michael@0 3 * Copyright (C) 1996-2013, International Business Machines Corporation and
michael@0 4 * others. All Rights Reserved.
michael@0 5 ******************************************************************************
michael@0 6 */
michael@0 7
michael@0 8 /**
michael@0 9 * File coll.cpp
michael@0 10 *
michael@0 11 * Created by: Helena Shih
michael@0 12 *
michael@0 13 * Modification History:
michael@0 14 *
michael@0 15 * Date Name Description
michael@0 16 * 2/5/97 aliu Modified createDefault to load collation data from
michael@0 17 * binary files when possible. Added related methods
michael@0 18 * createCollationFromFile, chopLocale, createPathName.
michael@0 19 * 2/11/97 aliu Added methods addToCache, findInCache, which implement
michael@0 20 * a Collation cache. Modified createDefault to look in
michael@0 21 * cache first, and also to store newly created Collation
michael@0 22 * objects in the cache. Modified to not use gLocPath.
michael@0 23 * 2/12/97 aliu Modified to create objects from RuleBasedCollator cache.
michael@0 24 * Moved cache out of Collation class.
michael@0 25 * 2/13/97 aliu Moved several methods out of this class and into
michael@0 26 * RuleBasedCollator, with modifications. Modified
michael@0 27 * createDefault() to call new RuleBasedCollator(Locale&)
michael@0 28 * constructor. General clean up and documentation.
michael@0 29 * 2/20/97 helena Added clone, operator==, operator!=, operator=, and copy
michael@0 30 * constructor.
michael@0 31 * 05/06/97 helena Added memory allocation error detection.
michael@0 32 * 05/08/97 helena Added createInstance().
michael@0 33 * 6/20/97 helena Java class name change.
michael@0 34 * 04/23/99 stephen Removed EDecompositionMode, merged with
michael@0 35 * Normalizer::EMode
michael@0 36 * 11/23/9 srl Inlining of some critical functions
michael@0 37 * 01/29/01 synwee Modified into a C++ wrapper calling C APIs (ucol.h)
michael@0 38 */
michael@0 39
michael@0 40 #include "utypeinfo.h" // for 'typeid' to work
michael@0 41
michael@0 42 #include "unicode/utypes.h"
michael@0 43
michael@0 44 #if !UCONFIG_NO_COLLATION
michael@0 45
michael@0 46 #include "unicode/coll.h"
michael@0 47 #include "unicode/tblcoll.h"
michael@0 48 #include "ucol_imp.h"
michael@0 49 #include "cstring.h"
michael@0 50 #include "cmemory.h"
michael@0 51 #include "umutex.h"
michael@0 52 #include "servloc.h"
michael@0 53 #include "uassert.h"
michael@0 54 #include "ustrenum.h"
michael@0 55 #include "uresimp.h"
michael@0 56 #include "ucln_in.h"
michael@0 57
michael@0 58 static icu::Locale* availableLocaleList = NULL;
michael@0 59 static int32_t availableLocaleListCount;
michael@0 60 static icu::ICULocaleService* gService = NULL;
michael@0 61 static icu::UInitOnce gServiceInitOnce = U_INITONCE_INITIALIZER;
michael@0 62 static icu::UInitOnce gAvailableLocaleListInitOnce;
michael@0 63
michael@0 64 /**
michael@0 65 * Release all static memory held by collator.
michael@0 66 */
michael@0 67 U_CDECL_BEGIN
michael@0 68 static UBool U_CALLCONV collator_cleanup(void) {
michael@0 69 #if !UCONFIG_NO_SERVICE
michael@0 70 if (gService) {
michael@0 71 delete gService;
michael@0 72 gService = NULL;
michael@0 73 }
michael@0 74 gServiceInitOnce.reset();
michael@0 75 #endif
michael@0 76 if (availableLocaleList) {
michael@0 77 delete []availableLocaleList;
michael@0 78 availableLocaleList = NULL;
michael@0 79 }
michael@0 80 availableLocaleListCount = 0;
michael@0 81 gAvailableLocaleListInitOnce.reset();
michael@0 82 return TRUE;
michael@0 83 }
michael@0 84
michael@0 85 U_CDECL_END
michael@0 86
michael@0 87 U_NAMESPACE_BEGIN
michael@0 88
michael@0 89 #if !UCONFIG_NO_SERVICE
michael@0 90
michael@0 91 // ------------------------------------------
michael@0 92 //
michael@0 93 // Registration
michael@0 94 //
michael@0 95
michael@0 96 //-------------------------------------------
michael@0 97
michael@0 98 CollatorFactory::~CollatorFactory() {}
michael@0 99
michael@0 100 //-------------------------------------------
michael@0 101
michael@0 102 UBool
michael@0 103 CollatorFactory::visible(void) const {
michael@0 104 return TRUE;
michael@0 105 }
michael@0 106
michael@0 107 //-------------------------------------------
michael@0 108
michael@0 109 UnicodeString&
michael@0 110 CollatorFactory::getDisplayName(const Locale& objectLocale,
michael@0 111 const Locale& displayLocale,
michael@0 112 UnicodeString& result)
michael@0 113 {
michael@0 114 return objectLocale.getDisplayName(displayLocale, result);
michael@0 115 }
michael@0 116
michael@0 117 // -------------------------------------
michael@0 118
michael@0 119 class ICUCollatorFactory : public ICUResourceBundleFactory {
michael@0 120 public:
michael@0 121 ICUCollatorFactory() : ICUResourceBundleFactory(UnicodeString(U_ICUDATA_COLL, -1, US_INV)) { }
michael@0 122 virtual ~ICUCollatorFactory();
michael@0 123 protected:
michael@0 124 virtual UObject* create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const;
michael@0 125 };
michael@0 126
michael@0 127 ICUCollatorFactory::~ICUCollatorFactory() {}
michael@0 128
michael@0 129 UObject*
michael@0 130 ICUCollatorFactory::create(const ICUServiceKey& key, const ICUService* /* service */, UErrorCode& status) const {
michael@0 131 if (handlesKey(key, status)) {
michael@0 132 const LocaleKey& lkey = (const LocaleKey&)key;
michael@0 133 Locale loc;
michael@0 134 // make sure the requested locale is correct
michael@0 135 // default LocaleFactory uses currentLocale since that's the one vetted by handlesKey
michael@0 136 // but for ICU rb resources we use the actual one since it will fallback again
michael@0 137 lkey.canonicalLocale(loc);
michael@0 138
michael@0 139 return Collator::makeInstance(loc, status);
michael@0 140 }
michael@0 141 return NULL;
michael@0 142 }
michael@0 143
michael@0 144 // -------------------------------------
michael@0 145
michael@0 146 class ICUCollatorService : public ICULocaleService {
michael@0 147 public:
michael@0 148 ICUCollatorService()
michael@0 149 : ICULocaleService(UNICODE_STRING_SIMPLE("Collator"))
michael@0 150 {
michael@0 151 UErrorCode status = U_ZERO_ERROR;
michael@0 152 registerFactory(new ICUCollatorFactory(), status);
michael@0 153 }
michael@0 154
michael@0 155 virtual ~ICUCollatorService();
michael@0 156
michael@0 157 virtual UObject* cloneInstance(UObject* instance) const {
michael@0 158 return ((Collator*)instance)->clone();
michael@0 159 }
michael@0 160
michael@0 161 virtual UObject* handleDefault(const ICUServiceKey& key, UnicodeString* actualID, UErrorCode& status) const {
michael@0 162 LocaleKey& lkey = (LocaleKey&)key;
michael@0 163 if (actualID) {
michael@0 164 // Ugly Hack Alert! We return an empty actualID to signal
michael@0 165 // to callers that this is a default object, not a "real"
michael@0 166 // service-created object. (TODO remove in 3.0) [aliu]
michael@0 167 actualID->truncate(0);
michael@0 168 }
michael@0 169 Locale loc("");
michael@0 170 lkey.canonicalLocale(loc);
michael@0 171 return Collator::makeInstance(loc, status);
michael@0 172 }
michael@0 173
michael@0 174 virtual UObject* getKey(ICUServiceKey& key, UnicodeString* actualReturn, UErrorCode& status) const {
michael@0 175 UnicodeString ar;
michael@0 176 if (actualReturn == NULL) {
michael@0 177 actualReturn = &ar;
michael@0 178 }
michael@0 179 Collator* result = (Collator*)ICULocaleService::getKey(key, actualReturn, status);
michael@0 180 // Ugly Hack Alert! If the actualReturn length is zero, this
michael@0 181 // means we got a default object, not a "real" service-created
michael@0 182 // object. We don't call setLocales() on a default object,
michael@0 183 // because that will overwrite its correct built-in locale
michael@0 184 // metadata (valid & actual) with our incorrect data (all we
michael@0 185 // have is the requested locale). (TODO remove in 3.0) [aliu]
michael@0 186 if (result && actualReturn->length() > 0) {
michael@0 187 const LocaleKey& lkey = (const LocaleKey&)key;
michael@0 188 Locale canonicalLocale("");
michael@0 189 Locale currentLocale("");
michael@0 190
michael@0 191 LocaleUtility::initLocaleFromName(*actualReturn, currentLocale);
michael@0 192 result->setLocales(lkey.canonicalLocale(canonicalLocale), currentLocale, currentLocale);
michael@0 193 }
michael@0 194 return result;
michael@0 195 }
michael@0 196
michael@0 197 virtual UBool isDefault() const {
michael@0 198 return countFactories() == 1;
michael@0 199 }
michael@0 200 };
michael@0 201
michael@0 202 ICUCollatorService::~ICUCollatorService() {}
michael@0 203
michael@0 204 // -------------------------------------
michael@0 205
michael@0 206 static void U_CALLCONV initService() {
michael@0 207 gService = new ICUCollatorService();
michael@0 208 ucln_i18n_registerCleanup(UCLN_I18N_COLLATOR, collator_cleanup);
michael@0 209 }
michael@0 210
michael@0 211
michael@0 212 static ICULocaleService*
michael@0 213 getService(void)
michael@0 214 {
michael@0 215 umtx_initOnce(gServiceInitOnce, &initService);
michael@0 216 return gService;
michael@0 217 }
michael@0 218
michael@0 219 // -------------------------------------
michael@0 220
michael@0 221 static inline UBool
michael@0 222 hasService(void)
michael@0 223 {
michael@0 224 UBool retVal = !gServiceInitOnce.isReset() && (getService() != NULL);
michael@0 225 return retVal;
michael@0 226 }
michael@0 227
michael@0 228 // -------------------------------------
michael@0 229
michael@0 230 UCollator*
michael@0 231 Collator::createUCollator(const char *loc,
michael@0 232 UErrorCode *status)
michael@0 233 {
michael@0 234 UCollator *result = 0;
michael@0 235 if (status && U_SUCCESS(*status) && hasService()) {
michael@0 236 Locale desiredLocale(loc);
michael@0 237 Collator *col = (Collator*)gService->get(desiredLocale, *status);
michael@0 238 RuleBasedCollator *rbc;
michael@0 239 if (col && (rbc = dynamic_cast<RuleBasedCollator *>(col))) {
michael@0 240 if (!rbc->dataIsOwned) {
michael@0 241 result = ucol_safeClone(rbc->ucollator, NULL, NULL, status);
michael@0 242 } else {
michael@0 243 result = rbc->ucollator;
michael@0 244 rbc->ucollator = NULL; // to prevent free on delete
michael@0 245 }
michael@0 246 } else {
michael@0 247 // should go in a function- ucol_initDelegate(delegate)
michael@0 248 result = (UCollator *)uprv_malloc(sizeof(UCollator));
michael@0 249 if(result == NULL) {
michael@0 250 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 251 } else {
michael@0 252 uprv_memset(result, 0, sizeof(UCollator));
michael@0 253 result->delegate = col;
michael@0 254 result->freeOnClose = TRUE; // do free on close.
michael@0 255 col = NULL; // to prevent free on delete.
michael@0 256 }
michael@0 257 }
michael@0 258 delete col;
michael@0 259 }
michael@0 260 return result;
michael@0 261 }
michael@0 262 #endif /* UCONFIG_NO_SERVICE */
michael@0 263
michael@0 264 static void U_CALLCONV
michael@0 265 initAvailableLocaleList(UErrorCode &status) {
michael@0 266 U_ASSERT(availableLocaleListCount == 0);
michael@0 267 U_ASSERT(availableLocaleList == NULL);
michael@0 268 // for now, there is a hardcoded list, so just walk through that list and set it up.
michael@0 269 UResourceBundle *index = NULL;
michael@0 270 UResourceBundle installed;
michael@0 271 int32_t i = 0;
michael@0 272
michael@0 273 ures_initStackObject(&installed);
michael@0 274 index = ures_openDirect(U_ICUDATA_COLL, "res_index", &status);
michael@0 275 ures_getByKey(index, "InstalledLocales", &installed, &status);
michael@0 276
michael@0 277 if(U_SUCCESS(status)) {
michael@0 278 availableLocaleListCount = ures_getSize(&installed);
michael@0 279 availableLocaleList = new Locale[availableLocaleListCount];
michael@0 280
michael@0 281 if (availableLocaleList != NULL) {
michael@0 282 ures_resetIterator(&installed);
michael@0 283 while(ures_hasNext(&installed)) {
michael@0 284 const char *tempKey = NULL;
michael@0 285 ures_getNextString(&installed, NULL, &tempKey, &status);
michael@0 286 availableLocaleList[i++] = Locale(tempKey);
michael@0 287 }
michael@0 288 }
michael@0 289 U_ASSERT(availableLocaleListCount == i);
michael@0 290 ures_close(&installed);
michael@0 291 }
michael@0 292 ures_close(index);
michael@0 293 ucln_i18n_registerCleanup(UCLN_I18N_COLLATOR, collator_cleanup);
michael@0 294 }
michael@0 295
michael@0 296 static UBool isAvailableLocaleListInitialized(UErrorCode &status) {
michael@0 297 umtx_initOnce(gAvailableLocaleListInitOnce, &initAvailableLocaleList, status);
michael@0 298 return U_SUCCESS(status);
michael@0 299 }
michael@0 300
michael@0 301
michael@0 302 // Collator public methods -----------------------------------------------
michael@0 303
michael@0 304 Collator* U_EXPORT2 Collator::createInstance(UErrorCode& success)
michael@0 305 {
michael@0 306 return createInstance(Locale::getDefault(), success);
michael@0 307 }
michael@0 308
michael@0 309 Collator* U_EXPORT2 Collator::createInstance(const Locale& desiredLocale,
michael@0 310 UErrorCode& status)
michael@0 311 {
michael@0 312 if (U_FAILURE(status))
michael@0 313 return 0;
michael@0 314
michael@0 315 #if !UCONFIG_NO_SERVICE
michael@0 316 if (hasService()) {
michael@0 317 Locale actualLoc;
michael@0 318 Collator *result =
michael@0 319 (Collator*)gService->get(desiredLocale, &actualLoc, status);
michael@0 320
michael@0 321 // Ugly Hack Alert! If the returned locale is empty (not root,
michael@0 322 // but empty -- getName() == "") then that means the service
michael@0 323 // returned a default object, not a "real" service object. In
michael@0 324 // that case, the locale metadata (valid & actual) is setup
michael@0 325 // correctly already, and we don't want to overwrite it. (TODO
michael@0 326 // remove in 3.0) [aliu]
michael@0 327 if (*actualLoc.getName() != 0) {
michael@0 328 result->setLocales(desiredLocale, actualLoc, actualLoc);
michael@0 329 }
michael@0 330 return result;
michael@0 331 }
michael@0 332 #endif
michael@0 333 return makeInstance(desiredLocale, status);
michael@0 334 }
michael@0 335
michael@0 336
michael@0 337 Collator* Collator::makeInstance(const Locale& desiredLocale,
michael@0 338 UErrorCode& status)
michael@0 339 {
michael@0 340 // A bit of explanation is required here. Although in the current
michael@0 341 // implementation
michael@0 342 // Collator::createInstance() is just turning around and calling
michael@0 343 // RuleBasedCollator(Locale&), this will not necessarily always be the
michael@0 344 // case. For example, suppose we modify this code to handle a
michael@0 345 // non-table-based Collator, such as that for Thai. In this case,
michael@0 346 // createInstance() will have to be modified to somehow determine this fact
michael@0 347 // (perhaps a field in the resource bundle). Then it can construct the
michael@0 348 // non-table-based Collator in some other way, when it sees that it needs
michael@0 349 // to.
michael@0 350 // The specific caution is this: RuleBasedCollator(Locale&) will ALWAYS
michael@0 351 // return a valid collation object, if the system is functioning properly.
michael@0 352 // The reason is that it will fall back, use the default locale, and even
michael@0 353 // use the built-in default collation rules. THEREFORE, createInstance()
michael@0 354 // should in general ONLY CALL RuleBasedCollator(Locale&) IF IT KNOWS IN
michael@0 355 // ADVANCE that the given locale's collation is properly implemented as a
michael@0 356 // RuleBasedCollator.
michael@0 357 // Currently, we don't do this...we always return a RuleBasedCollator,
michael@0 358 // whether it is strictly correct to do so or not, without checking, because
michael@0 359 // we currently have no way of checking.
michael@0 360
michael@0 361 RuleBasedCollator* collation = new RuleBasedCollator(desiredLocale,
michael@0 362 status);
michael@0 363 /* test for NULL */
michael@0 364 if (collation == 0) {
michael@0 365 status = U_MEMORY_ALLOCATION_ERROR;
michael@0 366 return 0;
michael@0 367 }
michael@0 368 if (U_FAILURE(status))
michael@0 369 {
michael@0 370 delete collation;
michael@0 371 collation = 0;
michael@0 372 }
michael@0 373 return collation;
michael@0 374 }
michael@0 375
michael@0 376 #ifdef U_USE_COLLATION_OBSOLETE_2_6
michael@0 377 // !!! dlf the following is obsolete, ignore registration for this
michael@0 378
michael@0 379 Collator *
michael@0 380 Collator::createInstance(const Locale &loc,
michael@0 381 UVersionInfo version,
michael@0 382 UErrorCode &status)
michael@0 383 {
michael@0 384 Collator *collator;
michael@0 385 UVersionInfo info;
michael@0 386
michael@0 387 collator=new RuleBasedCollator(loc, status);
michael@0 388 /* test for NULL */
michael@0 389 if (collator == 0) {
michael@0 390 status = U_MEMORY_ALLOCATION_ERROR;
michael@0 391 return 0;
michael@0 392 }
michael@0 393
michael@0 394 if(U_SUCCESS(status)) {
michael@0 395 collator->getVersion(info);
michael@0 396 if(0!=uprv_memcmp(version, info, sizeof(UVersionInfo))) {
michael@0 397 delete collator;
michael@0 398 status=U_MISSING_RESOURCE_ERROR;
michael@0 399 return 0;
michael@0 400 }
michael@0 401 }
michael@0 402 return collator;
michael@0 403 }
michael@0 404 #endif
michael@0 405
michael@0 406 Collator *
michael@0 407 Collator::safeClone() const {
michael@0 408 return clone();
michael@0 409 }
michael@0 410
michael@0 411 // implement deprecated, previously abstract method
michael@0 412 Collator::EComparisonResult Collator::compare(const UnicodeString& source,
michael@0 413 const UnicodeString& target) const
michael@0 414 {
michael@0 415 UErrorCode ec = U_ZERO_ERROR;
michael@0 416 return (EComparisonResult)compare(source, target, ec);
michael@0 417 }
michael@0 418
michael@0 419 // implement deprecated, previously abstract method
michael@0 420 Collator::EComparisonResult Collator::compare(const UnicodeString& source,
michael@0 421 const UnicodeString& target,
michael@0 422 int32_t length) const
michael@0 423 {
michael@0 424 UErrorCode ec = U_ZERO_ERROR;
michael@0 425 return (EComparisonResult)compare(source, target, length, ec);
michael@0 426 }
michael@0 427
michael@0 428 // implement deprecated, previously abstract method
michael@0 429 Collator::EComparisonResult Collator::compare(const UChar* source, int32_t sourceLength,
michael@0 430 const UChar* target, int32_t targetLength)
michael@0 431 const
michael@0 432 {
michael@0 433 UErrorCode ec = U_ZERO_ERROR;
michael@0 434 return (EComparisonResult)compare(source, sourceLength, target, targetLength, ec);
michael@0 435 }
michael@0 436
michael@0 437 UCollationResult Collator::compare(UCharIterator &/*sIter*/,
michael@0 438 UCharIterator &/*tIter*/,
michael@0 439 UErrorCode &status) const {
michael@0 440 if(U_SUCCESS(status)) {
michael@0 441 // Not implemented in the base class.
michael@0 442 status = U_UNSUPPORTED_ERROR;
michael@0 443 }
michael@0 444 return UCOL_EQUAL;
michael@0 445 }
michael@0 446
michael@0 447 UCollationResult Collator::compareUTF8(const StringPiece &source,
michael@0 448 const StringPiece &target,
michael@0 449 UErrorCode &status) const {
michael@0 450 if(U_FAILURE(status)) {
michael@0 451 return UCOL_EQUAL;
michael@0 452 }
michael@0 453 UCharIterator sIter, tIter;
michael@0 454 uiter_setUTF8(&sIter, source.data(), source.length());
michael@0 455 uiter_setUTF8(&tIter, target.data(), target.length());
michael@0 456 return compare(sIter, tIter, status);
michael@0 457 }
michael@0 458
michael@0 459 UBool Collator::equals(const UnicodeString& source,
michael@0 460 const UnicodeString& target) const
michael@0 461 {
michael@0 462 UErrorCode ec = U_ZERO_ERROR;
michael@0 463 return (compare(source, target, ec) == UCOL_EQUAL);
michael@0 464 }
michael@0 465
michael@0 466 UBool Collator::greaterOrEqual(const UnicodeString& source,
michael@0 467 const UnicodeString& target) const
michael@0 468 {
michael@0 469 UErrorCode ec = U_ZERO_ERROR;
michael@0 470 return (compare(source, target, ec) != UCOL_LESS);
michael@0 471 }
michael@0 472
michael@0 473 UBool Collator::greater(const UnicodeString& source,
michael@0 474 const UnicodeString& target) const
michael@0 475 {
michael@0 476 UErrorCode ec = U_ZERO_ERROR;
michael@0 477 return (compare(source, target, ec) == UCOL_GREATER);
michael@0 478 }
michael@0 479
michael@0 480 // this API ignores registered collators, since it returns an
michael@0 481 // array of indefinite lifetime
michael@0 482 const Locale* U_EXPORT2 Collator::getAvailableLocales(int32_t& count)
michael@0 483 {
michael@0 484 UErrorCode status = U_ZERO_ERROR;
michael@0 485 Locale *result = NULL;
michael@0 486 count = 0;
michael@0 487 if (isAvailableLocaleListInitialized(status))
michael@0 488 {
michael@0 489 result = availableLocaleList;
michael@0 490 count = availableLocaleListCount;
michael@0 491 }
michael@0 492 return result;
michael@0 493 }
michael@0 494
michael@0 495 UnicodeString& U_EXPORT2 Collator::getDisplayName(const Locale& objectLocale,
michael@0 496 const Locale& displayLocale,
michael@0 497 UnicodeString& name)
michael@0 498 {
michael@0 499 #if !UCONFIG_NO_SERVICE
michael@0 500 if (hasService()) {
michael@0 501 UnicodeString locNameStr;
michael@0 502 LocaleUtility::initNameFromLocale(objectLocale, locNameStr);
michael@0 503 return gService->getDisplayName(locNameStr, name, displayLocale);
michael@0 504 }
michael@0 505 #endif
michael@0 506 return objectLocale.getDisplayName(displayLocale, name);
michael@0 507 }
michael@0 508
michael@0 509 UnicodeString& U_EXPORT2 Collator::getDisplayName(const Locale& objectLocale,
michael@0 510 UnicodeString& name)
michael@0 511 {
michael@0 512 return getDisplayName(objectLocale, Locale::getDefault(), name);
michael@0 513 }
michael@0 514
michael@0 515 /* This is useless information */
michael@0 516 /*void Collator::getVersion(UVersionInfo versionInfo) const
michael@0 517 {
michael@0 518 if (versionInfo!=NULL)
michael@0 519 uprv_memcpy(versionInfo, fVersion, U_MAX_VERSION_LENGTH);
michael@0 520 }
michael@0 521 */
michael@0 522
michael@0 523 // UCollator protected constructor destructor ----------------------------
michael@0 524
michael@0 525 /**
michael@0 526 * Default constructor.
michael@0 527 * Constructor is different from the old default Collator constructor.
michael@0 528 * The task for determing the default collation strength and normalization mode
michael@0 529 * is left to the child class.
michael@0 530 */
michael@0 531 Collator::Collator()
michael@0 532 : UObject()
michael@0 533 {
michael@0 534 }
michael@0 535
michael@0 536 /**
michael@0 537 * Constructor.
michael@0 538 * Empty constructor, does not handle the arguments.
michael@0 539 * This constructor is done for backward compatibility with 1.7 and 1.8.
michael@0 540 * The task for handling the argument collation strength and normalization
michael@0 541 * mode is left to the child class.
michael@0 542 * @param collationStrength collation strength
michael@0 543 * @param decompositionMode
michael@0 544 * @deprecated 2.4 use the default constructor instead
michael@0 545 */
michael@0 546 Collator::Collator(UCollationStrength, UNormalizationMode )
michael@0 547 : UObject()
michael@0 548 {
michael@0 549 }
michael@0 550
michael@0 551 Collator::~Collator()
michael@0 552 {
michael@0 553 }
michael@0 554
michael@0 555 Collator::Collator(const Collator &other)
michael@0 556 : UObject(other)
michael@0 557 {
michael@0 558 }
michael@0 559
michael@0 560 UBool Collator::operator==(const Collator& other) const
michael@0 561 {
michael@0 562 // Subclasses: Call this method and then add more specific checks.
michael@0 563 return typeid(*this) == typeid(other);
michael@0 564 }
michael@0 565
michael@0 566 UBool Collator::operator!=(const Collator& other) const
michael@0 567 {
michael@0 568 return (UBool)!(*this == other);
michael@0 569 }
michael@0 570
michael@0 571 int32_t U_EXPORT2 Collator::getBound(const uint8_t *source,
michael@0 572 int32_t sourceLength,
michael@0 573 UColBoundMode boundType,
michael@0 574 uint32_t noOfLevels,
michael@0 575 uint8_t *result,
michael@0 576 int32_t resultLength,
michael@0 577 UErrorCode &status)
michael@0 578 {
michael@0 579 return ucol_getBound(source, sourceLength, boundType, noOfLevels, result, resultLength, &status);
michael@0 580 }
michael@0 581
michael@0 582 void
michael@0 583 Collator::setLocales(const Locale& /* requestedLocale */, const Locale& /* validLocale */, const Locale& /*actualLocale*/) {
michael@0 584 }
michael@0 585
michael@0 586 UnicodeSet *Collator::getTailoredSet(UErrorCode &status) const
michael@0 587 {
michael@0 588 if(U_FAILURE(status)) {
michael@0 589 return NULL;
michael@0 590 }
michael@0 591 // everything can be changed
michael@0 592 return new UnicodeSet(0, 0x10FFFF);
michael@0 593 }
michael@0 594
michael@0 595 // -------------------------------------
michael@0 596
michael@0 597 #if !UCONFIG_NO_SERVICE
michael@0 598 URegistryKey U_EXPORT2
michael@0 599 Collator::registerInstance(Collator* toAdopt, const Locale& locale, UErrorCode& status)
michael@0 600 {
michael@0 601 if (U_SUCCESS(status)) {
michael@0 602 return getService()->registerInstance(toAdopt, locale, status);
michael@0 603 }
michael@0 604 return NULL;
michael@0 605 }
michael@0 606
michael@0 607 // -------------------------------------
michael@0 608
michael@0 609 class CFactory : public LocaleKeyFactory {
michael@0 610 private:
michael@0 611 CollatorFactory* _delegate;
michael@0 612 Hashtable* _ids;
michael@0 613
michael@0 614 public:
michael@0 615 CFactory(CollatorFactory* delegate, UErrorCode& status)
michael@0 616 : LocaleKeyFactory(delegate->visible() ? VISIBLE : INVISIBLE)
michael@0 617 , _delegate(delegate)
michael@0 618 , _ids(NULL)
michael@0 619 {
michael@0 620 if (U_SUCCESS(status)) {
michael@0 621 int32_t count = 0;
michael@0 622 _ids = new Hashtable(status);
michael@0 623 if (_ids) {
michael@0 624 const UnicodeString * idlist = _delegate->getSupportedIDs(count, status);
michael@0 625 for (int i = 0; i < count; ++i) {
michael@0 626 _ids->put(idlist[i], (void*)this, status);
michael@0 627 if (U_FAILURE(status)) {
michael@0 628 delete _ids;
michael@0 629 _ids = NULL;
michael@0 630 return;
michael@0 631 }
michael@0 632 }
michael@0 633 } else {
michael@0 634 status = U_MEMORY_ALLOCATION_ERROR;
michael@0 635 }
michael@0 636 }
michael@0 637 }
michael@0 638
michael@0 639 virtual ~CFactory();
michael@0 640
michael@0 641 virtual UObject* create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const;
michael@0 642
michael@0 643 protected:
michael@0 644 virtual const Hashtable* getSupportedIDs(UErrorCode& status) const
michael@0 645 {
michael@0 646 if (U_SUCCESS(status)) {
michael@0 647 return _ids;
michael@0 648 }
michael@0 649 return NULL;
michael@0 650 }
michael@0 651
michael@0 652 virtual UnicodeString&
michael@0 653 getDisplayName(const UnicodeString& id, const Locale& locale, UnicodeString& result) const;
michael@0 654 };
michael@0 655
michael@0 656 CFactory::~CFactory()
michael@0 657 {
michael@0 658 delete _delegate;
michael@0 659 delete _ids;
michael@0 660 }
michael@0 661
michael@0 662 UObject*
michael@0 663 CFactory::create(const ICUServiceKey& key, const ICUService* /* service */, UErrorCode& status) const
michael@0 664 {
michael@0 665 if (handlesKey(key, status)) {
michael@0 666 const LocaleKey& lkey = (const LocaleKey&)key;
michael@0 667 Locale validLoc;
michael@0 668 lkey.currentLocale(validLoc);
michael@0 669 return _delegate->createCollator(validLoc);
michael@0 670 }
michael@0 671 return NULL;
michael@0 672 }
michael@0 673
michael@0 674 UnicodeString&
michael@0 675 CFactory::getDisplayName(const UnicodeString& id, const Locale& locale, UnicodeString& result) const
michael@0 676 {
michael@0 677 if ((_coverage & 0x1) == 0) {
michael@0 678 UErrorCode status = U_ZERO_ERROR;
michael@0 679 const Hashtable* ids = getSupportedIDs(status);
michael@0 680 if (ids && (ids->get(id) != NULL)) {
michael@0 681 Locale loc;
michael@0 682 LocaleUtility::initLocaleFromName(id, loc);
michael@0 683 return _delegate->getDisplayName(loc, locale, result);
michael@0 684 }
michael@0 685 }
michael@0 686 result.setToBogus();
michael@0 687 return result;
michael@0 688 }
michael@0 689
michael@0 690 URegistryKey U_EXPORT2
michael@0 691 Collator::registerFactory(CollatorFactory* toAdopt, UErrorCode& status)
michael@0 692 {
michael@0 693 if (U_SUCCESS(status)) {
michael@0 694 CFactory* f = new CFactory(toAdopt, status);
michael@0 695 if (f) {
michael@0 696 return getService()->registerFactory(f, status);
michael@0 697 }
michael@0 698 status = U_MEMORY_ALLOCATION_ERROR;
michael@0 699 }
michael@0 700 return NULL;
michael@0 701 }
michael@0 702
michael@0 703 // -------------------------------------
michael@0 704
michael@0 705 UBool U_EXPORT2
michael@0 706 Collator::unregister(URegistryKey key, UErrorCode& status)
michael@0 707 {
michael@0 708 if (U_SUCCESS(status)) {
michael@0 709 if (hasService()) {
michael@0 710 return gService->unregister(key, status);
michael@0 711 }
michael@0 712 status = U_ILLEGAL_ARGUMENT_ERROR;
michael@0 713 }
michael@0 714 return FALSE;
michael@0 715 }
michael@0 716 #endif /* UCONFIG_NO_SERVICE */
michael@0 717
michael@0 718 class CollationLocaleListEnumeration : public StringEnumeration {
michael@0 719 private:
michael@0 720 int32_t index;
michael@0 721 public:
michael@0 722 static UClassID U_EXPORT2 getStaticClassID(void);
michael@0 723 virtual UClassID getDynamicClassID(void) const;
michael@0 724 public:
michael@0 725 CollationLocaleListEnumeration()
michael@0 726 : index(0)
michael@0 727 {
michael@0 728 // The global variables should already be initialized.
michael@0 729 //isAvailableLocaleListInitialized(status);
michael@0 730 }
michael@0 731
michael@0 732 virtual ~CollationLocaleListEnumeration();
michael@0 733
michael@0 734 virtual StringEnumeration * clone() const
michael@0 735 {
michael@0 736 CollationLocaleListEnumeration *result = new CollationLocaleListEnumeration();
michael@0 737 if (result) {
michael@0 738 result->index = index;
michael@0 739 }
michael@0 740 return result;
michael@0 741 }
michael@0 742
michael@0 743 virtual int32_t count(UErrorCode &/*status*/) const {
michael@0 744 return availableLocaleListCount;
michael@0 745 }
michael@0 746
michael@0 747 virtual const char* next(int32_t* resultLength, UErrorCode& /*status*/) {
michael@0 748 const char* result;
michael@0 749 if(index < availableLocaleListCount) {
michael@0 750 result = availableLocaleList[index++].getName();
michael@0 751 if(resultLength != NULL) {
michael@0 752 *resultLength = (int32_t)uprv_strlen(result);
michael@0 753 }
michael@0 754 } else {
michael@0 755 if(resultLength != NULL) {
michael@0 756 *resultLength = 0;
michael@0 757 }
michael@0 758 result = NULL;
michael@0 759 }
michael@0 760 return result;
michael@0 761 }
michael@0 762
michael@0 763 virtual const UnicodeString* snext(UErrorCode& status) {
michael@0 764 int32_t resultLength = 0;
michael@0 765 const char *s = next(&resultLength, status);
michael@0 766 return setChars(s, resultLength, status);
michael@0 767 }
michael@0 768
michael@0 769 virtual void reset(UErrorCode& /*status*/) {
michael@0 770 index = 0;
michael@0 771 }
michael@0 772 };
michael@0 773
michael@0 774 CollationLocaleListEnumeration::~CollationLocaleListEnumeration() {}
michael@0 775
michael@0 776 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CollationLocaleListEnumeration)
michael@0 777
michael@0 778
michael@0 779 // -------------------------------------
michael@0 780
michael@0 781 StringEnumeration* U_EXPORT2
michael@0 782 Collator::getAvailableLocales(void)
michael@0 783 {
michael@0 784 #if !UCONFIG_NO_SERVICE
michael@0 785 if (hasService()) {
michael@0 786 return getService()->getAvailableLocales();
michael@0 787 }
michael@0 788 #endif /* UCONFIG_NO_SERVICE */
michael@0 789 UErrorCode status = U_ZERO_ERROR;
michael@0 790 if (isAvailableLocaleListInitialized(status)) {
michael@0 791 return new CollationLocaleListEnumeration();
michael@0 792 }
michael@0 793 return NULL;
michael@0 794 }
michael@0 795
michael@0 796 StringEnumeration* U_EXPORT2
michael@0 797 Collator::getKeywords(UErrorCode& status) {
michael@0 798 // This is a wrapper over ucol_getKeywords
michael@0 799 UEnumeration* uenum = ucol_getKeywords(&status);
michael@0 800 if (U_FAILURE(status)) {
michael@0 801 uenum_close(uenum);
michael@0 802 return NULL;
michael@0 803 }
michael@0 804 return new UStringEnumeration(uenum);
michael@0 805 }
michael@0 806
michael@0 807 StringEnumeration* U_EXPORT2
michael@0 808 Collator::getKeywordValues(const char *keyword, UErrorCode& status) {
michael@0 809 // This is a wrapper over ucol_getKeywordValues
michael@0 810 UEnumeration* uenum = ucol_getKeywordValues(keyword, &status);
michael@0 811 if (U_FAILURE(status)) {
michael@0 812 uenum_close(uenum);
michael@0 813 return NULL;
michael@0 814 }
michael@0 815 return new UStringEnumeration(uenum);
michael@0 816 }
michael@0 817
michael@0 818 StringEnumeration* U_EXPORT2
michael@0 819 Collator::getKeywordValuesForLocale(const char* key, const Locale& locale,
michael@0 820 UBool commonlyUsed, UErrorCode& status) {
michael@0 821 // This is a wrapper over ucol_getKeywordValuesForLocale
michael@0 822 UEnumeration *uenum = ucol_getKeywordValuesForLocale(key, locale.getName(),
michael@0 823 commonlyUsed, &status);
michael@0 824 if (U_FAILURE(status)) {
michael@0 825 uenum_close(uenum);
michael@0 826 return NULL;
michael@0 827 }
michael@0 828 return new UStringEnumeration(uenum);
michael@0 829 }
michael@0 830
michael@0 831 Locale U_EXPORT2
michael@0 832 Collator::getFunctionalEquivalent(const char* keyword, const Locale& locale,
michael@0 833 UBool& isAvailable, UErrorCode& status) {
michael@0 834 // This is a wrapper over ucol_getFunctionalEquivalent
michael@0 835 char loc[ULOC_FULLNAME_CAPACITY];
michael@0 836 /*int32_t len =*/ ucol_getFunctionalEquivalent(loc, sizeof(loc),
michael@0 837 keyword, locale.getName(), &isAvailable, &status);
michael@0 838 if (U_FAILURE(status)) {
michael@0 839 *loc = 0; // root
michael@0 840 }
michael@0 841 return Locale::createFromName(loc);
michael@0 842 }
michael@0 843
michael@0 844 Collator::ECollationStrength
michael@0 845 Collator::getStrength(void) const {
michael@0 846 UErrorCode intStatus = U_ZERO_ERROR;
michael@0 847 return (ECollationStrength)getAttribute(UCOL_STRENGTH, intStatus);
michael@0 848 }
michael@0 849
michael@0 850 void
michael@0 851 Collator::setStrength(ECollationStrength newStrength) {
michael@0 852 UErrorCode intStatus = U_ZERO_ERROR;
michael@0 853 setAttribute(UCOL_STRENGTH, (UColAttributeValue)newStrength, intStatus);
michael@0 854 }
michael@0 855
michael@0 856 int32_t
michael@0 857 Collator::getReorderCodes(int32_t* /* dest*/,
michael@0 858 int32_t /* destCapacity*/,
michael@0 859 UErrorCode& status) const
michael@0 860 {
michael@0 861 if (U_SUCCESS(status)) {
michael@0 862 status = U_UNSUPPORTED_ERROR;
michael@0 863 }
michael@0 864 return 0;
michael@0 865 }
michael@0 866
michael@0 867 void
michael@0 868 Collator::setReorderCodes(const int32_t* /* reorderCodes */,
michael@0 869 int32_t /* reorderCodesLength */,
michael@0 870 UErrorCode& status)
michael@0 871 {
michael@0 872 if (U_SUCCESS(status)) {
michael@0 873 status = U_UNSUPPORTED_ERROR;
michael@0 874 }
michael@0 875 }
michael@0 876
michael@0 877 int32_t U_EXPORT2
michael@0 878 Collator::getEquivalentReorderCodes(int32_t /* reorderCode */,
michael@0 879 int32_t* /* dest */,
michael@0 880 int32_t /* destCapacity */,
michael@0 881 UErrorCode& status)
michael@0 882 {
michael@0 883 if (U_SUCCESS(status)) {
michael@0 884 status = U_UNSUPPORTED_ERROR;
michael@0 885 }
michael@0 886 return 0;
michael@0 887 }
michael@0 888
michael@0 889 int32_t
michael@0 890 Collator::internalGetShortDefinitionString(const char * /*locale*/,
michael@0 891 char * /*buffer*/,
michael@0 892 int32_t /*capacity*/,
michael@0 893 UErrorCode &status) const {
michael@0 894 if(U_SUCCESS(status)) {
michael@0 895 status = U_UNSUPPORTED_ERROR; /* Shouldn't happen, internal function */
michael@0 896 }
michael@0 897 return 0;
michael@0 898 }
michael@0 899
michael@0 900 // UCollator private data members ----------------------------------------
michael@0 901
michael@0 902 /* This is useless information */
michael@0 903 /*const UVersionInfo Collator::fVersion = {1, 1, 0, 0};*/
michael@0 904
michael@0 905 // -------------------------------------
michael@0 906
michael@0 907 U_NAMESPACE_END
michael@0 908
michael@0 909 #endif /* #if !UCONFIG_NO_COLLATION */
michael@0 910
michael@0 911 /* eof */

mercurial