Coverage Report

Created: 2023-03-29 06:15

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