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