Coverage Report

Created: 2023-02-22 06:51

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