/src/icu/source/i18n/collationtailoring.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) 2013-2015, International Business Machines  | 
6  |  | * Corporation and others.  All Rights Reserved.  | 
7  |  | *******************************************************************************  | 
8  |  | * collationtailoring.cpp  | 
9  |  | *  | 
10  |  | * created on: 2013mar12  | 
11  |  | * created by: Markus W. Scherer  | 
12  |  | */  | 
13  |  |  | 
14  |  | #include "unicode/utypes.h"  | 
15  |  |  | 
16  |  | #if !UCONFIG_NO_COLLATION  | 
17  |  |  | 
18  |  | #include "unicode/udata.h"  | 
19  |  | #include "unicode/unistr.h"  | 
20  |  | #include "unicode/ures.h"  | 
21  |  | #include "unicode/uversion.h"  | 
22  |  | #include "unicode/uvernum.h"  | 
23  |  | #include "cmemory.h"  | 
24  |  | #include "collationdata.h"  | 
25  |  | #include "collationsettings.h"  | 
26  |  | #include "collationtailoring.h"  | 
27  |  | #include "normalizer2impl.h"  | 
28  |  | #include "uassert.h"  | 
29  |  | #include "uhash.h"  | 
30  |  | #include "umutex.h"  | 
31  |  | #include "utrie2.h"  | 
32  |  |  | 
33  |  | U_NAMESPACE_BEGIN  | 
34  |  |  | 
35  |  | CollationTailoring::CollationTailoring(const CollationSettings *baseSettings)  | 
36  | 0  |         : data(NULL), settings(baseSettings),  | 
37  | 0  |           actualLocale(""), | 
38  |  |           ownedData(NULL),  | 
39  |  |           builder(NULL), memory(NULL), bundle(NULL),  | 
40  |  |           trie(NULL), unsafeBackwardSet(NULL),  | 
41  | 0  |           maxExpansions(NULL) { | 
42  | 0  |     if(baseSettings != NULL) { | 
43  | 0  |         U_ASSERT(baseSettings->reorderCodesLength == 0);  | 
44  | 0  |         U_ASSERT(baseSettings->reorderTable == NULL);  | 
45  | 0  |         U_ASSERT(baseSettings->minHighNoReorder == 0);  | 
46  | 0  |     } else { | 
47  | 0  |         settings = new CollationSettings();  | 
48  | 0  |     }  | 
49  | 0  |     if(settings != NULL) { | 
50  | 0  |         settings->addRef();  | 
51  | 0  |     }  | 
52  | 0  |     rules.getTerminatedBuffer();  // ensure NUL-termination  | 
53  | 0  |     version[0] = version[1] = version[2] = version[3] = 0;  | 
54  | 0  |     maxExpansionsInitOnce.reset();  | 
55  | 0  | }  | 
56  |  |  | 
57  | 0  | CollationTailoring::~CollationTailoring() { | 
58  | 0  |     SharedObject::clearPtr(settings);  | 
59  | 0  |     delete ownedData;  | 
60  | 0  |     delete builder;  | 
61  | 0  |     udata_close(memory);  | 
62  | 0  |     ures_close(bundle);  | 
63  | 0  |     utrie2_close(trie);  | 
64  | 0  |     delete unsafeBackwardSet;  | 
65  | 0  |     uhash_close(maxExpansions);  | 
66  | 0  |     maxExpansionsInitOnce.reset();  | 
67  | 0  | }  | 
68  |  |  | 
69  |  | UBool  | 
70  | 0  | CollationTailoring::ensureOwnedData(UErrorCode &errorCode) { | 
71  | 0  |     if(U_FAILURE(errorCode)) { return FALSE; } | 
72  | 0  |     if(ownedData == NULL) { | 
73  | 0  |         const Normalizer2Impl *nfcImpl = Normalizer2Factory::getNFCImpl(errorCode);  | 
74  | 0  |         if(U_FAILURE(errorCode)) { return FALSE; } | 
75  | 0  |         ownedData = new CollationData(*nfcImpl);  | 
76  | 0  |         if(ownedData == NULL) { | 
77  | 0  |             errorCode = U_MEMORY_ALLOCATION_ERROR;  | 
78  | 0  |             return FALSE;  | 
79  | 0  |         }  | 
80  | 0  |     }  | 
81  | 0  |     data = ownedData;  | 
82  | 0  |     return TRUE;  | 
83  | 0  | }  | 
84  |  |  | 
85  |  | void  | 
86  | 0  | CollationTailoring::makeBaseVersion(const UVersionInfo ucaVersion, UVersionInfo version) { | 
87  | 0  |     version[0] = UCOL_BUILDER_VERSION;  | 
88  | 0  |     version[1] = (ucaVersion[0] << 3) + ucaVersion[1];  | 
89  | 0  |     version[2] = ucaVersion[2] << 6;  | 
90  | 0  |     version[3] = 0;  | 
91  | 0  | }  | 
92  |  |  | 
93  |  | void  | 
94  | 0  | CollationTailoring::setVersion(const UVersionInfo baseVersion, const UVersionInfo rulesVersion) { | 
95  | 0  |     version[0] = UCOL_BUILDER_VERSION;  | 
96  | 0  |     version[1] = baseVersion[1];  | 
97  | 0  |     version[2] = (baseVersion[2] & 0xc0) + ((rulesVersion[0] + (rulesVersion[0] >> 6)) & 0x3f);  | 
98  | 0  |     version[3] = (rulesVersion[1] << 3) + (rulesVersion[1] >> 5) + rulesVersion[2] +  | 
99  | 0  |             (rulesVersion[3] << 4) + (rulesVersion[3] >> 4);  | 
100  | 0  | }  | 
101  |  |  | 
102  |  | int32_t  | 
103  | 0  | CollationTailoring::getUCAVersion() const { | 
104  | 0  |     return ((int32_t)version[1] << 4) | (version[2] >> 6);  | 
105  | 0  | }  | 
106  |  |  | 
107  | 0  | CollationCacheEntry::~CollationCacheEntry() { | 
108  | 0  |     SharedObject::clearPtr(tailoring);  | 
109  | 0  | }  | 
110  |  |  | 
111  |  | U_NAMESPACE_END  | 
112  |  |  | 
113  |  | #endif  // !UCONFIG_NO_COLLATION  |