/src/mozilla-central/intl/icu/source/i18n/coll.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // © 2016 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | /* |
4 | | ****************************************************************************** |
5 | | * Copyright (C) 1996-2014, International Business Machines Corporation and |
6 | | * others. All Rights Reserved. |
7 | | ****************************************************************************** |
8 | | */ |
9 | | |
10 | | /** |
11 | | * File coll.cpp |
12 | | * |
13 | | * Created by: Helena Shih |
14 | | * |
15 | | * Modification History: |
16 | | * |
17 | | * Date Name Description |
18 | | * 2/5/97 aliu Modified createDefault to load collation data from |
19 | | * binary files when possible. Added related methods |
20 | | * createCollationFromFile, chopLocale, createPathName. |
21 | | * 2/11/97 aliu Added methods addToCache, findInCache, which implement |
22 | | * a Collation cache. Modified createDefault to look in |
23 | | * cache first, and also to store newly created Collation |
24 | | * objects in the cache. Modified to not use gLocPath. |
25 | | * 2/12/97 aliu Modified to create objects from RuleBasedCollator cache. |
26 | | * Moved cache out of Collation class. |
27 | | * 2/13/97 aliu Moved several methods out of this class and into |
28 | | * RuleBasedCollator, with modifications. Modified |
29 | | * createDefault() to call new RuleBasedCollator(Locale&) |
30 | | * constructor. General clean up and documentation. |
31 | | * 2/20/97 helena Added clone, operator==, operator!=, operator=, and copy |
32 | | * constructor. |
33 | | * 05/06/97 helena Added memory allocation error detection. |
34 | | * 05/08/97 helena Added createInstance(). |
35 | | * 6/20/97 helena Java class name change. |
36 | | * 04/23/99 stephen Removed EDecompositionMode, merged with |
37 | | * Normalizer::EMode |
38 | | * 11/23/9 srl Inlining of some critical functions |
39 | | * 01/29/01 synwee Modified into a C++ wrapper calling C APIs (ucol.h) |
40 | | * 2012-2014 markus Rewritten in C++ again. |
41 | | */ |
42 | | |
43 | | #include "utypeinfo.h" // for 'typeid' to work |
44 | | |
45 | | #include "unicode/utypes.h" |
46 | | |
47 | | #if !UCONFIG_NO_COLLATION |
48 | | |
49 | | #include "unicode/coll.h" |
50 | | #include "unicode/tblcoll.h" |
51 | | #include "collationdata.h" |
52 | | #include "collationroot.h" |
53 | | #include "collationtailoring.h" |
54 | | #include "ucol_imp.h" |
55 | | #include "cstring.h" |
56 | | #include "cmemory.h" |
57 | | #include "umutex.h" |
58 | | #include "servloc.h" |
59 | | #include "uassert.h" |
60 | | #include "ustrenum.h" |
61 | | #include "uresimp.h" |
62 | | #include "ucln_in.h" |
63 | | |
64 | | static icu::Locale* availableLocaleList = NULL; |
65 | | static int32_t availableLocaleListCount; |
66 | | #if !UCONFIG_NO_SERVICE |
67 | | static icu::ICULocaleService* gService = NULL; |
68 | | static icu::UInitOnce gServiceInitOnce = U_INITONCE_INITIALIZER; |
69 | | #endif |
70 | | static icu::UInitOnce gAvailableLocaleListInitOnce; |
71 | | |
72 | | /** |
73 | | * Release all static memory held by collator. |
74 | | */ |
75 | | U_CDECL_BEGIN |
76 | 0 | static UBool U_CALLCONV collator_cleanup(void) { |
77 | 0 | #if !UCONFIG_NO_SERVICE |
78 | 0 | if (gService) { |
79 | 0 | delete gService; |
80 | 0 | gService = NULL; |
81 | 0 | } |
82 | 0 | gServiceInitOnce.reset(); |
83 | 0 | #endif |
84 | 0 | if (availableLocaleList) { |
85 | 0 | delete []availableLocaleList; |
86 | 0 | availableLocaleList = NULL; |
87 | 0 | } |
88 | 0 | availableLocaleListCount = 0; |
89 | 0 | gAvailableLocaleListInitOnce.reset(); |
90 | 0 | return TRUE; |
91 | 0 | } |
92 | | |
93 | | U_CDECL_END |
94 | | |
95 | | U_NAMESPACE_BEGIN |
96 | | |
97 | | #if !UCONFIG_NO_SERVICE |
98 | | |
99 | | // ------------------------------------------ |
100 | | // |
101 | | // Registration |
102 | | // |
103 | | |
104 | | //------------------------------------------- |
105 | | |
106 | 0 | CollatorFactory::~CollatorFactory() {} |
107 | | |
108 | | //------------------------------------------- |
109 | | |
110 | | UBool |
111 | 0 | CollatorFactory::visible(void) const { |
112 | 0 | return TRUE; |
113 | 0 | } |
114 | | |
115 | | //------------------------------------------- |
116 | | |
117 | | UnicodeString& |
118 | | CollatorFactory::getDisplayName(const Locale& objectLocale, |
119 | | const Locale& displayLocale, |
120 | | UnicodeString& result) |
121 | 0 | { |
122 | 0 | return objectLocale.getDisplayName(displayLocale, result); |
123 | 0 | } |
124 | | |
125 | | // ------------------------------------- |
126 | | |
127 | | class ICUCollatorFactory : public ICUResourceBundleFactory { |
128 | | public: |
129 | 0 | ICUCollatorFactory() : ICUResourceBundleFactory(UnicodeString(U_ICUDATA_COLL, -1, US_INV)) { } |
130 | | virtual ~ICUCollatorFactory(); |
131 | | protected: |
132 | | virtual UObject* create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const; |
133 | | }; |
134 | | |
135 | 0 | ICUCollatorFactory::~ICUCollatorFactory() {} |
136 | | |
137 | | UObject* |
138 | 0 | ICUCollatorFactory::create(const ICUServiceKey& key, const ICUService* /* service */, UErrorCode& status) const { |
139 | 0 | if (handlesKey(key, status)) { |
140 | 0 | const LocaleKey& lkey = (const LocaleKey&)key; |
141 | 0 | Locale loc; |
142 | 0 | // make sure the requested locale is correct |
143 | 0 | // default LocaleFactory uses currentLocale since that's the one vetted by handlesKey |
144 | 0 | // but for ICU rb resources we use the actual one since it will fallback again |
145 | 0 | lkey.canonicalLocale(loc); |
146 | 0 | |
147 | 0 | return Collator::makeInstance(loc, status); |
148 | 0 | } |
149 | 0 | return NULL; |
150 | 0 | } |
151 | | |
152 | | // ------------------------------------- |
153 | | |
154 | | class ICUCollatorService : public ICULocaleService { |
155 | | public: |
156 | | ICUCollatorService() |
157 | | : ICULocaleService(UNICODE_STRING_SIMPLE("Collator")) |
158 | 0 | { |
159 | 0 | UErrorCode status = U_ZERO_ERROR; |
160 | 0 | registerFactory(new ICUCollatorFactory(), status); |
161 | 0 | } |
162 | | |
163 | | virtual ~ICUCollatorService(); |
164 | | |
165 | 0 | virtual UObject* cloneInstance(UObject* instance) const { |
166 | 0 | return ((Collator*)instance)->clone(); |
167 | 0 | } |
168 | | |
169 | 0 | virtual UObject* handleDefault(const ICUServiceKey& key, UnicodeString* actualID, UErrorCode& status) const { |
170 | 0 | LocaleKey& lkey = (LocaleKey&)key; |
171 | 0 | if (actualID) { |
172 | 0 | // Ugly Hack Alert! We return an empty actualID to signal |
173 | 0 | // to callers that this is a default object, not a "real" |
174 | 0 | // service-created object. (TODO remove in 3.0) [aliu] |
175 | 0 | actualID->truncate(0); |
176 | 0 | } |
177 | 0 | Locale loc(""); |
178 | 0 | lkey.canonicalLocale(loc); |
179 | 0 | return Collator::makeInstance(loc, status); |
180 | 0 | } |
181 | | |
182 | 0 | virtual UObject* getKey(ICUServiceKey& key, UnicodeString* actualReturn, UErrorCode& status) const { |
183 | 0 | UnicodeString ar; |
184 | 0 | if (actualReturn == NULL) { |
185 | 0 | actualReturn = &ar; |
186 | 0 | } |
187 | 0 | return (Collator*)ICULocaleService::getKey(key, actualReturn, status); |
188 | 0 | } |
189 | | |
190 | 0 | virtual UBool isDefault() const { |
191 | 0 | return countFactories() == 1; |
192 | 0 | } |
193 | | }; |
194 | | |
195 | 0 | ICUCollatorService::~ICUCollatorService() {} |
196 | | |
197 | | // ------------------------------------- |
198 | | |
199 | 0 | static void U_CALLCONV initService() { |
200 | 0 | gService = new ICUCollatorService(); |
201 | 0 | ucln_i18n_registerCleanup(UCLN_I18N_COLLATOR, collator_cleanup); |
202 | 0 | } |
203 | | |
204 | | |
205 | | static ICULocaleService* |
206 | | getService(void) |
207 | 0 | { |
208 | 0 | umtx_initOnce(gServiceInitOnce, &initService); |
209 | 0 | return gService; |
210 | 0 | } |
211 | | |
212 | | // ------------------------------------- |
213 | | |
214 | | static inline UBool |
215 | | hasService(void) |
216 | 0 | { |
217 | 0 | UBool retVal = !gServiceInitOnce.isReset() && (getService() != NULL); |
218 | 0 | return retVal; |
219 | 0 | } |
220 | | |
221 | | #endif /* UCONFIG_NO_SERVICE */ |
222 | | |
223 | | static void U_CALLCONV |
224 | 0 | initAvailableLocaleList(UErrorCode &status) { |
225 | 0 | U_ASSERT(availableLocaleListCount == 0); |
226 | 0 | U_ASSERT(availableLocaleList == NULL); |
227 | 0 | // for now, there is a hardcoded list, so just walk through that list and set it up. |
228 | 0 | UResourceBundle *index = NULL; |
229 | 0 | UResourceBundle installed; |
230 | 0 | int32_t i = 0; |
231 | 0 | |
232 | 0 | ures_initStackObject(&installed); |
233 | 0 | index = ures_openDirect(U_ICUDATA_COLL, "res_index", &status); |
234 | 0 | ures_getByKey(index, "InstalledLocales", &installed, &status); |
235 | 0 | |
236 | 0 | if(U_SUCCESS(status)) { |
237 | 0 | availableLocaleListCount = ures_getSize(&installed); |
238 | 0 | availableLocaleList = new Locale[availableLocaleListCount]; |
239 | 0 | |
240 | 0 | if (availableLocaleList != NULL) { |
241 | 0 | ures_resetIterator(&installed); |
242 | 0 | while(ures_hasNext(&installed)) { |
243 | 0 | const char *tempKey = NULL; |
244 | 0 | ures_getNextString(&installed, NULL, &tempKey, &status); |
245 | 0 | availableLocaleList[i++] = Locale(tempKey); |
246 | 0 | } |
247 | 0 | } |
248 | 0 | U_ASSERT(availableLocaleListCount == i); |
249 | 0 | ures_close(&installed); |
250 | 0 | } |
251 | 0 | ures_close(index); |
252 | 0 | ucln_i18n_registerCleanup(UCLN_I18N_COLLATOR, collator_cleanup); |
253 | 0 | } |
254 | | |
255 | 0 | static UBool isAvailableLocaleListInitialized(UErrorCode &status) { |
256 | 0 | umtx_initOnce(gAvailableLocaleListInitOnce, &initAvailableLocaleList, status); |
257 | 0 | return U_SUCCESS(status); |
258 | 0 | } |
259 | | |
260 | | |
261 | | // Collator public methods ----------------------------------------------- |
262 | | |
263 | | namespace { |
264 | | |
265 | | static const struct { |
266 | | const char *name; |
267 | | UColAttribute attr; |
268 | | } collAttributes[] = { |
269 | | { "colStrength", UCOL_STRENGTH }, |
270 | | { "colBackwards", UCOL_FRENCH_COLLATION }, |
271 | | { "colCaseLevel", UCOL_CASE_LEVEL }, |
272 | | { "colCaseFirst", UCOL_CASE_FIRST }, |
273 | | { "colAlternate", UCOL_ALTERNATE_HANDLING }, |
274 | | { "colNormalization", UCOL_NORMALIZATION_MODE }, |
275 | | { "colNumeric", UCOL_NUMERIC_COLLATION } |
276 | | }; |
277 | | |
278 | | static const struct { |
279 | | const char *name; |
280 | | UColAttributeValue value; |
281 | | } collAttributeValues[] = { |
282 | | { "primary", UCOL_PRIMARY }, |
283 | | { "secondary", UCOL_SECONDARY }, |
284 | | { "tertiary", UCOL_TERTIARY }, |
285 | | { "quaternary", UCOL_QUATERNARY }, |
286 | | // Note: Not supporting typo "quarternary" because it was never supported in locale IDs. |
287 | | { "identical", UCOL_IDENTICAL }, |
288 | | { "no", UCOL_OFF }, |
289 | | { "yes", UCOL_ON }, |
290 | | { "shifted", UCOL_SHIFTED }, |
291 | | { "non-ignorable", UCOL_NON_IGNORABLE }, |
292 | | { "lower", UCOL_LOWER_FIRST }, |
293 | | { "upper", UCOL_UPPER_FIRST } |
294 | | }; |
295 | | |
296 | | static const char *collReorderCodes[UCOL_REORDER_CODE_LIMIT - UCOL_REORDER_CODE_FIRST] = { |
297 | | "space", "punct", "symbol", "currency", "digit" |
298 | | }; |
299 | | |
300 | 0 | int32_t getReorderCode(const char *s) { |
301 | 0 | for (int32_t i = 0; i < UPRV_LENGTHOF(collReorderCodes); ++i) { |
302 | 0 | if (uprv_stricmp(s, collReorderCodes[i]) == 0) { |
303 | 0 | return UCOL_REORDER_CODE_FIRST + i; |
304 | 0 | } |
305 | 0 | } |
306 | 0 | // Not supporting "others" = UCOL_REORDER_CODE_OTHERS |
307 | 0 | // as a synonym for Zzzz = USCRIPT_UNKNOWN for now: |
308 | 0 | // Avoid introducing synonyms/aliases. |
309 | 0 | return -1; |
310 | 0 | } |
311 | | |
312 | | /** |
313 | | * Sets collation attributes according to locale keywords. See |
314 | | * http://www.unicode.org/reports/tr35/tr35-collation.html#Collation_Settings |
315 | | * |
316 | | * Using "alias" keywords and values where defined: |
317 | | * http://www.unicode.org/reports/tr35/tr35.html#Old_Locale_Extension_Syntax |
318 | | * http://unicode.org/repos/cldr/trunk/common/bcp47/collation.xml |
319 | | */ |
320 | 0 | void setAttributesFromKeywords(const Locale &loc, Collator &coll, UErrorCode &errorCode) { |
321 | 0 | if (U_FAILURE(errorCode)) { |
322 | 0 | return; |
323 | 0 | } |
324 | 0 | if (uprv_strcmp(loc.getName(), loc.getBaseName()) == 0) { |
325 | 0 | // No keywords. |
326 | 0 | return; |
327 | 0 | } |
328 | 0 | char value[1024]; // The reordering value could be long. |
329 | 0 | // Check for collation keywords that were already deprecated |
330 | 0 | // before any were supported in createInstance() (except for "collation"). |
331 | 0 | int32_t length = loc.getKeywordValue("colHiraganaQuaternary", value, UPRV_LENGTHOF(value), errorCode); |
332 | 0 | if (U_FAILURE(errorCode)) { |
333 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
334 | 0 | return; |
335 | 0 | } |
336 | 0 | if (length != 0) { |
337 | 0 | errorCode = U_UNSUPPORTED_ERROR; |
338 | 0 | return; |
339 | 0 | } |
340 | 0 | length = loc.getKeywordValue("variableTop", value, UPRV_LENGTHOF(value), errorCode); |
341 | 0 | if (U_FAILURE(errorCode)) { |
342 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
343 | 0 | return; |
344 | 0 | } |
345 | 0 | if (length != 0) { |
346 | 0 | errorCode = U_UNSUPPORTED_ERROR; |
347 | 0 | return; |
348 | 0 | } |
349 | 0 | // Parse known collation keywords, ignore others. |
350 | 0 | if (errorCode == U_STRING_NOT_TERMINATED_WARNING) { |
351 | 0 | errorCode = U_ZERO_ERROR; |
352 | 0 | } |
353 | 0 | for (int32_t i = 0; i < UPRV_LENGTHOF(collAttributes); ++i) { |
354 | 0 | length = loc.getKeywordValue(collAttributes[i].name, value, UPRV_LENGTHOF(value), errorCode); |
355 | 0 | if (U_FAILURE(errorCode) || errorCode == U_STRING_NOT_TERMINATED_WARNING) { |
356 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
357 | 0 | return; |
358 | 0 | } |
359 | 0 | if (length == 0) { continue; } |
360 | 0 | for (int32_t j = 0;; ++j) { |
361 | 0 | if (j == UPRV_LENGTHOF(collAttributeValues)) { |
362 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
363 | 0 | return; |
364 | 0 | } |
365 | 0 | if (uprv_stricmp(value, collAttributeValues[j].name) == 0) { |
366 | 0 | coll.setAttribute(collAttributes[i].attr, collAttributeValues[j].value, errorCode); |
367 | 0 | break; |
368 | 0 | } |
369 | 0 | } |
370 | 0 | } |
371 | 0 | length = loc.getKeywordValue("colReorder", value, UPRV_LENGTHOF(value), errorCode); |
372 | 0 | if (U_FAILURE(errorCode) || errorCode == U_STRING_NOT_TERMINATED_WARNING) { |
373 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
374 | 0 | return; |
375 | 0 | } |
376 | 0 | if (length != 0) { |
377 | 0 | int32_t codes[USCRIPT_CODE_LIMIT + UCOL_REORDER_CODE_LIMIT - UCOL_REORDER_CODE_FIRST]; |
378 | 0 | int32_t codesLength = 0; |
379 | 0 | char *scriptName = value; |
380 | 0 | for (;;) { |
381 | 0 | if (codesLength == UPRV_LENGTHOF(codes)) { |
382 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
383 | 0 | return; |
384 | 0 | } |
385 | 0 | char *limit = scriptName; |
386 | 0 | char c; |
387 | 0 | while ((c = *limit) != 0 && c != '-') { ++limit; } |
388 | 0 | *limit = 0; |
389 | 0 | int32_t code; |
390 | 0 | if ((limit - scriptName) == 4) { |
391 | 0 | // Strict parsing, accept only 4-letter script codes, not long names. |
392 | 0 | code = u_getPropertyValueEnum(UCHAR_SCRIPT, scriptName); |
393 | 0 | } else { |
394 | 0 | code = getReorderCode(scriptName); |
395 | 0 | } |
396 | 0 | if (code < 0) { |
397 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
398 | 0 | return; |
399 | 0 | } |
400 | 0 | codes[codesLength++] = code; |
401 | 0 | if (c == 0) { break; } |
402 | 0 | scriptName = limit + 1; |
403 | 0 | } |
404 | 0 | coll.setReorderCodes(codes, codesLength, errorCode); |
405 | 0 | } |
406 | 0 | length = loc.getKeywordValue("kv", value, UPRV_LENGTHOF(value), errorCode); |
407 | 0 | if (U_FAILURE(errorCode) || errorCode == U_STRING_NOT_TERMINATED_WARNING) { |
408 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
409 | 0 | return; |
410 | 0 | } |
411 | 0 | if (length != 0) { |
412 | 0 | int32_t code = getReorderCode(value); |
413 | 0 | if (code < 0) { |
414 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
415 | 0 | return; |
416 | 0 | } |
417 | 0 | coll.setMaxVariable((UColReorderCode)code, errorCode); |
418 | 0 | } |
419 | 0 | if (U_FAILURE(errorCode)) { |
420 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
421 | 0 | } |
422 | 0 | } |
423 | | |
424 | | } // namespace |
425 | | |
426 | | Collator* U_EXPORT2 Collator::createInstance(UErrorCode& success) |
427 | 0 | { |
428 | 0 | return createInstance(Locale::getDefault(), success); |
429 | 0 | } |
430 | | |
431 | | Collator* U_EXPORT2 Collator::createInstance(const Locale& desiredLocale, |
432 | | UErrorCode& status) |
433 | 0 | { |
434 | 0 | if (U_FAILURE(status)) |
435 | 0 | return 0; |
436 | 0 | if (desiredLocale.isBogus()) { |
437 | 0 | // Locale constructed from malformed locale ID or language tag. |
438 | 0 | status = U_ILLEGAL_ARGUMENT_ERROR; |
439 | 0 | return NULL; |
440 | 0 | } |
441 | 0 | |
442 | 0 | Collator* coll; |
443 | 0 | #if !UCONFIG_NO_SERVICE |
444 | 0 | if (hasService()) { |
445 | 0 | Locale actualLoc; |
446 | 0 | coll = (Collator*)gService->get(desiredLocale, &actualLoc, status); |
447 | 0 | } else |
448 | 0 | #endif |
449 | 0 | { |
450 | 0 | coll = makeInstance(desiredLocale, status); |
451 | 0 | } |
452 | 0 | setAttributesFromKeywords(desiredLocale, *coll, status); |
453 | 0 | if (U_FAILURE(status)) { |
454 | 0 | delete coll; |
455 | 0 | return NULL; |
456 | 0 | } |
457 | 0 | return coll; |
458 | 0 | } |
459 | | |
460 | | |
461 | 0 | Collator* Collator::makeInstance(const Locale& desiredLocale, UErrorCode& status) { |
462 | 0 | const CollationCacheEntry *entry = CollationLoader::loadTailoring(desiredLocale, status); |
463 | 0 | if (U_SUCCESS(status)) { |
464 | 0 | Collator *result = new RuleBasedCollator(entry); |
465 | 0 | if (result != NULL) { |
466 | 0 | // Both the unified cache's get() and the RBC constructor |
467 | 0 | // did addRef(). Undo one of them. |
468 | 0 | entry->removeRef(); |
469 | 0 | return result; |
470 | 0 | } |
471 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
472 | 0 | } |
473 | 0 | if (entry != NULL) { |
474 | 0 | // Undo the addRef() from the cache.get(). |
475 | 0 | entry->removeRef(); |
476 | 0 | } |
477 | 0 | return NULL; |
478 | 0 | } |
479 | | |
480 | | Collator * |
481 | 0 | Collator::safeClone() const { |
482 | 0 | return clone(); |
483 | 0 | } |
484 | | |
485 | | // implement deprecated, previously abstract method |
486 | | Collator::EComparisonResult Collator::compare(const UnicodeString& source, |
487 | | const UnicodeString& target) const |
488 | 0 | { |
489 | 0 | UErrorCode ec = U_ZERO_ERROR; |
490 | 0 | return (EComparisonResult)compare(source, target, ec); |
491 | 0 | } |
492 | | |
493 | | // implement deprecated, previously abstract method |
494 | | Collator::EComparisonResult Collator::compare(const UnicodeString& source, |
495 | | const UnicodeString& target, |
496 | | int32_t length) const |
497 | 0 | { |
498 | 0 | UErrorCode ec = U_ZERO_ERROR; |
499 | 0 | return (EComparisonResult)compare(source, target, length, ec); |
500 | 0 | } |
501 | | |
502 | | // implement deprecated, previously abstract method |
503 | | Collator::EComparisonResult Collator::compare(const UChar* source, int32_t sourceLength, |
504 | | const UChar* target, int32_t targetLength) |
505 | | const |
506 | 0 | { |
507 | 0 | UErrorCode ec = U_ZERO_ERROR; |
508 | 0 | return (EComparisonResult)compare(source, sourceLength, target, targetLength, ec); |
509 | 0 | } |
510 | | |
511 | | UCollationResult Collator::compare(UCharIterator &/*sIter*/, |
512 | | UCharIterator &/*tIter*/, |
513 | 0 | UErrorCode &status) const { |
514 | 0 | if(U_SUCCESS(status)) { |
515 | 0 | // Not implemented in the base class. |
516 | 0 | status = U_UNSUPPORTED_ERROR; |
517 | 0 | } |
518 | 0 | return UCOL_EQUAL; |
519 | 0 | } |
520 | | |
521 | | UCollationResult Collator::compareUTF8(const StringPiece &source, |
522 | | const StringPiece &target, |
523 | 0 | UErrorCode &status) const { |
524 | 0 | if(U_FAILURE(status)) { |
525 | 0 | return UCOL_EQUAL; |
526 | 0 | } |
527 | 0 | UCharIterator sIter, tIter; |
528 | 0 | uiter_setUTF8(&sIter, source.data(), source.length()); |
529 | 0 | uiter_setUTF8(&tIter, target.data(), target.length()); |
530 | 0 | return compare(sIter, tIter, status); |
531 | 0 | } |
532 | | |
533 | | UBool Collator::equals(const UnicodeString& source, |
534 | | const UnicodeString& target) const |
535 | 0 | { |
536 | 0 | UErrorCode ec = U_ZERO_ERROR; |
537 | 0 | return (compare(source, target, ec) == UCOL_EQUAL); |
538 | 0 | } |
539 | | |
540 | | UBool Collator::greaterOrEqual(const UnicodeString& source, |
541 | | const UnicodeString& target) const |
542 | 0 | { |
543 | 0 | UErrorCode ec = U_ZERO_ERROR; |
544 | 0 | return (compare(source, target, ec) != UCOL_LESS); |
545 | 0 | } |
546 | | |
547 | | UBool Collator::greater(const UnicodeString& source, |
548 | | const UnicodeString& target) const |
549 | 0 | { |
550 | 0 | UErrorCode ec = U_ZERO_ERROR; |
551 | 0 | return (compare(source, target, ec) == UCOL_GREATER); |
552 | 0 | } |
553 | | |
554 | | // this API ignores registered collators, since it returns an |
555 | | // array of indefinite lifetime |
556 | | const Locale* U_EXPORT2 Collator::getAvailableLocales(int32_t& count) |
557 | 0 | { |
558 | 0 | UErrorCode status = U_ZERO_ERROR; |
559 | 0 | Locale *result = NULL; |
560 | 0 | count = 0; |
561 | 0 | if (isAvailableLocaleListInitialized(status)) |
562 | 0 | { |
563 | 0 | result = availableLocaleList; |
564 | 0 | count = availableLocaleListCount; |
565 | 0 | } |
566 | 0 | return result; |
567 | 0 | } |
568 | | |
569 | | UnicodeString& U_EXPORT2 Collator::getDisplayName(const Locale& objectLocale, |
570 | | const Locale& displayLocale, |
571 | | UnicodeString& name) |
572 | 0 | { |
573 | 0 | #if !UCONFIG_NO_SERVICE |
574 | 0 | if (hasService()) { |
575 | 0 | UnicodeString locNameStr; |
576 | 0 | LocaleUtility::initNameFromLocale(objectLocale, locNameStr); |
577 | 0 | return gService->getDisplayName(locNameStr, name, displayLocale); |
578 | 0 | } |
579 | 0 | #endif |
580 | 0 | return objectLocale.getDisplayName(displayLocale, name); |
581 | 0 | } |
582 | | |
583 | | UnicodeString& U_EXPORT2 Collator::getDisplayName(const Locale& objectLocale, |
584 | | UnicodeString& name) |
585 | 0 | { |
586 | 0 | return getDisplayName(objectLocale, Locale::getDefault(), name); |
587 | 0 | } |
588 | | |
589 | | /* This is useless information */ |
590 | | /*void Collator::getVersion(UVersionInfo versionInfo) const |
591 | | { |
592 | | if (versionInfo!=NULL) |
593 | | uprv_memcpy(versionInfo, fVersion, U_MAX_VERSION_LENGTH); |
594 | | } |
595 | | */ |
596 | | |
597 | | // UCollator protected constructor destructor ---------------------------- |
598 | | |
599 | | /** |
600 | | * Default constructor. |
601 | | * Constructor is different from the old default Collator constructor. |
602 | | * The task for determing the default collation strength and normalization mode |
603 | | * is left to the child class. |
604 | | */ |
605 | | Collator::Collator() |
606 | | : UObject() |
607 | 0 | { |
608 | 0 | } |
609 | | |
610 | | /** |
611 | | * Constructor. |
612 | | * Empty constructor, does not handle the arguments. |
613 | | * This constructor is done for backward compatibility with 1.7 and 1.8. |
614 | | * The task for handling the argument collation strength and normalization |
615 | | * mode is left to the child class. |
616 | | * @param collationStrength collation strength |
617 | | * @param decompositionMode |
618 | | * @deprecated 2.4 use the default constructor instead |
619 | | */ |
620 | | Collator::Collator(UCollationStrength, UNormalizationMode ) |
621 | | : UObject() |
622 | 0 | { |
623 | 0 | } |
624 | | |
625 | | Collator::~Collator() |
626 | 0 | { |
627 | 0 | } |
628 | | |
629 | | Collator::Collator(const Collator &other) |
630 | | : UObject(other) |
631 | 0 | { |
632 | 0 | } |
633 | | |
634 | | UBool Collator::operator==(const Collator& other) const |
635 | 0 | { |
636 | 0 | // Subclasses: Call this method and then add more specific checks. |
637 | 0 | return typeid(*this) == typeid(other); |
638 | 0 | } |
639 | | |
640 | | UBool Collator::operator!=(const Collator& other) const |
641 | 0 | { |
642 | 0 | return (UBool)!(*this == other); |
643 | 0 | } |
644 | | |
645 | | int32_t U_EXPORT2 Collator::getBound(const uint8_t *source, |
646 | | int32_t sourceLength, |
647 | | UColBoundMode boundType, |
648 | | uint32_t noOfLevels, |
649 | | uint8_t *result, |
650 | | int32_t resultLength, |
651 | | UErrorCode &status) |
652 | 0 | { |
653 | 0 | return ucol_getBound(source, sourceLength, boundType, noOfLevels, result, resultLength, &status); |
654 | 0 | } |
655 | | |
656 | | void |
657 | 0 | Collator::setLocales(const Locale& /* requestedLocale */, const Locale& /* validLocale */, const Locale& /*actualLocale*/) { |
658 | 0 | } |
659 | | |
660 | | UnicodeSet *Collator::getTailoredSet(UErrorCode &status) const |
661 | 0 | { |
662 | 0 | if(U_FAILURE(status)) { |
663 | 0 | return NULL; |
664 | 0 | } |
665 | 0 | // everything can be changed |
666 | 0 | return new UnicodeSet(0, 0x10FFFF); |
667 | 0 | } |
668 | | |
669 | | // ------------------------------------- |
670 | | |
671 | | #if !UCONFIG_NO_SERVICE |
672 | | URegistryKey U_EXPORT2 |
673 | | Collator::registerInstance(Collator* toAdopt, const Locale& locale, UErrorCode& status) |
674 | 0 | { |
675 | 0 | if (U_SUCCESS(status)) { |
676 | 0 | // Set the collator locales while registering so that createInstance() |
677 | 0 | // need not guess whether the collator's locales are already set properly |
678 | 0 | // (as they are by the data loader). |
679 | 0 | toAdopt->setLocales(locale, locale, locale); |
680 | 0 | return getService()->registerInstance(toAdopt, locale, status); |
681 | 0 | } |
682 | 0 | return NULL; |
683 | 0 | } |
684 | | |
685 | | // ------------------------------------- |
686 | | |
687 | | class CFactory : public LocaleKeyFactory { |
688 | | private: |
689 | | CollatorFactory* _delegate; |
690 | | Hashtable* _ids; |
691 | | |
692 | | public: |
693 | | CFactory(CollatorFactory* delegate, UErrorCode& status) |
694 | | : LocaleKeyFactory(delegate->visible() ? VISIBLE : INVISIBLE) |
695 | | , _delegate(delegate) |
696 | | , _ids(NULL) |
697 | 0 | { |
698 | 0 | if (U_SUCCESS(status)) { |
699 | 0 | int32_t count = 0; |
700 | 0 | _ids = new Hashtable(status); |
701 | 0 | if (_ids) { |
702 | 0 | const UnicodeString * idlist = _delegate->getSupportedIDs(count, status); |
703 | 0 | for (int i = 0; i < count; ++i) { |
704 | 0 | _ids->put(idlist[i], (void*)this, status); |
705 | 0 | if (U_FAILURE(status)) { |
706 | 0 | delete _ids; |
707 | 0 | _ids = NULL; |
708 | 0 | return; |
709 | 0 | } |
710 | 0 | } |
711 | 0 | } else { |
712 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
713 | 0 | } |
714 | 0 | } |
715 | 0 | } |
716 | | |
717 | | virtual ~CFactory(); |
718 | | |
719 | | virtual UObject* create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const; |
720 | | |
721 | | protected: |
722 | | virtual const Hashtable* getSupportedIDs(UErrorCode& status) const |
723 | 0 | { |
724 | 0 | if (U_SUCCESS(status)) { |
725 | 0 | return _ids; |
726 | 0 | } |
727 | 0 | return NULL; |
728 | 0 | } |
729 | | |
730 | | virtual UnicodeString& |
731 | | getDisplayName(const UnicodeString& id, const Locale& locale, UnicodeString& result) const; |
732 | | }; |
733 | | |
734 | | CFactory::~CFactory() |
735 | 0 | { |
736 | 0 | delete _delegate; |
737 | 0 | delete _ids; |
738 | 0 | } |
739 | | |
740 | | UObject* |
741 | | CFactory::create(const ICUServiceKey& key, const ICUService* /* service */, UErrorCode& status) const |
742 | 0 | { |
743 | 0 | if (handlesKey(key, status)) { |
744 | 0 | const LocaleKey& lkey = (const LocaleKey&)key; |
745 | 0 | Locale validLoc; |
746 | 0 | lkey.currentLocale(validLoc); |
747 | 0 | return _delegate->createCollator(validLoc); |
748 | 0 | } |
749 | 0 | return NULL; |
750 | 0 | } |
751 | | |
752 | | UnicodeString& |
753 | | CFactory::getDisplayName(const UnicodeString& id, const Locale& locale, UnicodeString& result) const |
754 | 0 | { |
755 | 0 | if ((_coverage & 0x1) == 0) { |
756 | 0 | UErrorCode status = U_ZERO_ERROR; |
757 | 0 | const Hashtable* ids = getSupportedIDs(status); |
758 | 0 | if (ids && (ids->get(id) != NULL)) { |
759 | 0 | Locale loc; |
760 | 0 | LocaleUtility::initLocaleFromName(id, loc); |
761 | 0 | return _delegate->getDisplayName(loc, locale, result); |
762 | 0 | } |
763 | 0 | } |
764 | 0 | result.setToBogus(); |
765 | 0 | return result; |
766 | 0 | } |
767 | | |
768 | | URegistryKey U_EXPORT2 |
769 | | Collator::registerFactory(CollatorFactory* toAdopt, UErrorCode& status) |
770 | 0 | { |
771 | 0 | if (U_SUCCESS(status)) { |
772 | 0 | CFactory* f = new CFactory(toAdopt, status); |
773 | 0 | if (f) { |
774 | 0 | return getService()->registerFactory(f, status); |
775 | 0 | } |
776 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
777 | 0 | } |
778 | 0 | return NULL; |
779 | 0 | } |
780 | | |
781 | | // ------------------------------------- |
782 | | |
783 | | UBool U_EXPORT2 |
784 | | Collator::unregister(URegistryKey key, UErrorCode& status) |
785 | 0 | { |
786 | 0 | if (U_SUCCESS(status)) { |
787 | 0 | if (hasService()) { |
788 | 0 | return gService->unregister(key, status); |
789 | 0 | } |
790 | 0 | status = U_ILLEGAL_ARGUMENT_ERROR; |
791 | 0 | } |
792 | 0 | return FALSE; |
793 | 0 | } |
794 | | #endif /* UCONFIG_NO_SERVICE */ |
795 | | |
796 | | class CollationLocaleListEnumeration : public StringEnumeration { |
797 | | private: |
798 | | int32_t index; |
799 | | public: |
800 | | static UClassID U_EXPORT2 getStaticClassID(void); |
801 | | virtual UClassID getDynamicClassID(void) const; |
802 | | public: |
803 | | CollationLocaleListEnumeration() |
804 | | : index(0) |
805 | 0 | { |
806 | 0 | // The global variables should already be initialized. |
807 | 0 | //isAvailableLocaleListInitialized(status); |
808 | 0 | } |
809 | | |
810 | | virtual ~CollationLocaleListEnumeration(); |
811 | | |
812 | | virtual StringEnumeration * clone() const |
813 | 0 | { |
814 | 0 | CollationLocaleListEnumeration *result = new CollationLocaleListEnumeration(); |
815 | 0 | if (result) { |
816 | 0 | result->index = index; |
817 | 0 | } |
818 | 0 | return result; |
819 | 0 | } |
820 | | |
821 | 0 | virtual int32_t count(UErrorCode &/*status*/) const { |
822 | 0 | return availableLocaleListCount; |
823 | 0 | } |
824 | | |
825 | 0 | virtual const char* next(int32_t* resultLength, UErrorCode& /*status*/) { |
826 | 0 | const char* result; |
827 | 0 | if(index < availableLocaleListCount) { |
828 | 0 | result = availableLocaleList[index++].getName(); |
829 | 0 | if(resultLength != NULL) { |
830 | 0 | *resultLength = (int32_t)uprv_strlen(result); |
831 | 0 | } |
832 | 0 | } else { |
833 | 0 | if(resultLength != NULL) { |
834 | 0 | *resultLength = 0; |
835 | 0 | } |
836 | 0 | result = NULL; |
837 | 0 | } |
838 | 0 | return result; |
839 | 0 | } |
840 | | |
841 | 0 | virtual const UnicodeString* snext(UErrorCode& status) { |
842 | 0 | int32_t resultLength = 0; |
843 | 0 | const char *s = next(&resultLength, status); |
844 | 0 | return setChars(s, resultLength, status); |
845 | 0 | } |
846 | | |
847 | 0 | virtual void reset(UErrorCode& /*status*/) { |
848 | 0 | index = 0; |
849 | 0 | } |
850 | | }; |
851 | | |
852 | 0 | CollationLocaleListEnumeration::~CollationLocaleListEnumeration() {} |
853 | | |
854 | | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CollationLocaleListEnumeration) |
855 | | |
856 | | |
857 | | // ------------------------------------- |
858 | | |
859 | | StringEnumeration* U_EXPORT2 |
860 | | Collator::getAvailableLocales(void) |
861 | 0 | { |
862 | 0 | #if !UCONFIG_NO_SERVICE |
863 | 0 | if (hasService()) { |
864 | 0 | return getService()->getAvailableLocales(); |
865 | 0 | } |
866 | 0 | #endif /* UCONFIG_NO_SERVICE */ |
867 | 0 | UErrorCode status = U_ZERO_ERROR; |
868 | 0 | if (isAvailableLocaleListInitialized(status)) { |
869 | 0 | return new CollationLocaleListEnumeration(); |
870 | 0 | } |
871 | 0 | return NULL; |
872 | 0 | } |
873 | | |
874 | | StringEnumeration* U_EXPORT2 |
875 | 0 | Collator::getKeywords(UErrorCode& status) { |
876 | 0 | return UStringEnumeration::fromUEnumeration( |
877 | 0 | ucol_getKeywords(&status), status); |
878 | 0 | } |
879 | | |
880 | | StringEnumeration* U_EXPORT2 |
881 | 0 | Collator::getKeywordValues(const char *keyword, UErrorCode& status) { |
882 | 0 | return UStringEnumeration::fromUEnumeration( |
883 | 0 | ucol_getKeywordValues(keyword, &status), status); |
884 | 0 | } |
885 | | |
886 | | StringEnumeration* U_EXPORT2 |
887 | | Collator::getKeywordValuesForLocale(const char* key, const Locale& locale, |
888 | 0 | UBool commonlyUsed, UErrorCode& status) { |
889 | 0 | return UStringEnumeration::fromUEnumeration( |
890 | 0 | ucol_getKeywordValuesForLocale( |
891 | 0 | key, locale.getName(), commonlyUsed, &status), |
892 | 0 | status); |
893 | 0 | } |
894 | | |
895 | | Locale U_EXPORT2 |
896 | | Collator::getFunctionalEquivalent(const char* keyword, const Locale& locale, |
897 | 0 | UBool& isAvailable, UErrorCode& status) { |
898 | 0 | // This is a wrapper over ucol_getFunctionalEquivalent |
899 | 0 | char loc[ULOC_FULLNAME_CAPACITY]; |
900 | 0 | /*int32_t len =*/ ucol_getFunctionalEquivalent(loc, sizeof(loc), |
901 | 0 | keyword, locale.getName(), &isAvailable, &status); |
902 | 0 | if (U_FAILURE(status)) { |
903 | 0 | *loc = 0; // root |
904 | 0 | } |
905 | 0 | return Locale::createFromName(loc); |
906 | 0 | } |
907 | | |
908 | | Collator::ECollationStrength |
909 | 0 | Collator::getStrength(void) const { |
910 | 0 | UErrorCode intStatus = U_ZERO_ERROR; |
911 | 0 | return (ECollationStrength)getAttribute(UCOL_STRENGTH, intStatus); |
912 | 0 | } |
913 | | |
914 | | void |
915 | 0 | Collator::setStrength(ECollationStrength newStrength) { |
916 | 0 | UErrorCode intStatus = U_ZERO_ERROR; |
917 | 0 | setAttribute(UCOL_STRENGTH, (UColAttributeValue)newStrength, intStatus); |
918 | 0 | } |
919 | | |
920 | | Collator & |
921 | 0 | Collator::setMaxVariable(UColReorderCode /*group*/, UErrorCode &errorCode) { |
922 | 0 | if (U_SUCCESS(errorCode)) { |
923 | 0 | errorCode = U_UNSUPPORTED_ERROR; |
924 | 0 | } |
925 | 0 | return *this; |
926 | 0 | } |
927 | | |
928 | | UColReorderCode |
929 | 0 | Collator::getMaxVariable() const { |
930 | 0 | return UCOL_REORDER_CODE_PUNCTUATION; |
931 | 0 | } |
932 | | |
933 | | int32_t |
934 | | Collator::getReorderCodes(int32_t* /* dest*/, |
935 | | int32_t /* destCapacity*/, |
936 | | UErrorCode& status) const |
937 | 0 | { |
938 | 0 | if (U_SUCCESS(status)) { |
939 | 0 | status = U_UNSUPPORTED_ERROR; |
940 | 0 | } |
941 | 0 | return 0; |
942 | 0 | } |
943 | | |
944 | | void |
945 | | Collator::setReorderCodes(const int32_t* /* reorderCodes */, |
946 | | int32_t /* reorderCodesLength */, |
947 | | UErrorCode& status) |
948 | 0 | { |
949 | 0 | if (U_SUCCESS(status)) { |
950 | 0 | status = U_UNSUPPORTED_ERROR; |
951 | 0 | } |
952 | 0 | } |
953 | | |
954 | | int32_t |
955 | | Collator::getEquivalentReorderCodes(int32_t reorderCode, |
956 | | int32_t *dest, int32_t capacity, |
957 | 0 | UErrorCode &errorCode) { |
958 | 0 | if(U_FAILURE(errorCode)) { return 0; } |
959 | 0 | if(capacity < 0 || (dest == NULL && capacity > 0)) { |
960 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
961 | 0 | return 0; |
962 | 0 | } |
963 | 0 | const CollationData *baseData = CollationRoot::getData(errorCode); |
964 | 0 | if(U_FAILURE(errorCode)) { return 0; } |
965 | 0 | return baseData->getEquivalentScripts(reorderCode, dest, capacity, errorCode); |
966 | 0 | } |
967 | | |
968 | | int32_t |
969 | | Collator::internalGetShortDefinitionString(const char * /*locale*/, |
970 | | char * /*buffer*/, |
971 | | int32_t /*capacity*/, |
972 | 0 | UErrorCode &status) const { |
973 | 0 | if(U_SUCCESS(status)) { |
974 | 0 | status = U_UNSUPPORTED_ERROR; /* Shouldn't happen, internal function */ |
975 | 0 | } |
976 | 0 | return 0; |
977 | 0 | } |
978 | | |
979 | | UCollationResult |
980 | | Collator::internalCompareUTF8(const char *left, int32_t leftLength, |
981 | | const char *right, int32_t rightLength, |
982 | 0 | UErrorCode &errorCode) const { |
983 | 0 | if(U_FAILURE(errorCode)) { return UCOL_EQUAL; } |
984 | 0 | if((left == NULL && leftLength != 0) || (right == NULL && rightLength != 0)) { |
985 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
986 | 0 | return UCOL_EQUAL; |
987 | 0 | } |
988 | 0 | return compareUTF8( |
989 | 0 | StringPiece(left, (leftLength < 0) ? uprv_strlen(left) : leftLength), |
990 | 0 | StringPiece(right, (rightLength < 0) ? uprv_strlen(right) : rightLength), |
991 | 0 | errorCode); |
992 | 0 | } |
993 | | |
994 | | int32_t |
995 | | Collator::internalNextSortKeyPart(UCharIterator * /*iter*/, uint32_t /*state*/[2], |
996 | 0 | uint8_t * /*dest*/, int32_t /*count*/, UErrorCode &errorCode) const { |
997 | 0 | if (U_SUCCESS(errorCode)) { |
998 | 0 | errorCode = U_UNSUPPORTED_ERROR; |
999 | 0 | } |
1000 | 0 | return 0; |
1001 | 0 | } |
1002 | | |
1003 | | // UCollator private data members ---------------------------------------- |
1004 | | |
1005 | | /* This is useless information */ |
1006 | | /*const UVersionInfo Collator::fVersion = {1, 1, 0, 0};*/ |
1007 | | |
1008 | | // ------------------------------------- |
1009 | | |
1010 | | U_NAMESPACE_END |
1011 | | |
1012 | | #endif /* #if !UCONFIG_NO_COLLATION */ |
1013 | | |
1014 | | /* eof */ |