• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *******************************************************************************
3 * Copyright (C) 2013-2014, 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 
CollationTailoring(const CollationSettings * baseSettings)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     } else {
44         settings = new CollationSettings();
45     }
46     if(settings != NULL) {
47         settings->addRef();
48     }
49     rules.getTerminatedBuffer();  // ensure NUL-termination
50     version[0] = version[1] = version[2] = version[3] = 0;
51     maxExpansionsInitOnce.reset();
52 }
53 
~CollationTailoring()54 CollationTailoring::~CollationTailoring() {
55     SharedObject::clearPtr(settings);
56     delete ownedData;
57     delete builder;
58     udata_close(memory);
59     ures_close(bundle);
60     utrie2_close(trie);
61     delete unsafeBackwardSet;
62     uhash_close(maxExpansions);
63     maxExpansionsInitOnce.reset();
64 }
65 
66 UBool
ensureOwnedData(UErrorCode & errorCode)67 CollationTailoring::ensureOwnedData(UErrorCode &errorCode) {
68     if(U_FAILURE(errorCode)) { return FALSE; }
69     if(ownedData == NULL) {
70         const Normalizer2Impl *nfcImpl = Normalizer2Factory::getNFCImpl(errorCode);
71         if(U_FAILURE(errorCode)) { return FALSE; }
72         ownedData = new CollationData(*nfcImpl);
73         if(ownedData == NULL) {
74             errorCode = U_MEMORY_ALLOCATION_ERROR;
75             return FALSE;
76         }
77     }
78     data = ownedData;
79     return TRUE;
80 }
81 
82 void
makeBaseVersion(const UVersionInfo ucaVersion,UVersionInfo version)83 CollationTailoring::makeBaseVersion(const UVersionInfo ucaVersion, UVersionInfo version) {
84     version[0] = UCOL_BUILDER_VERSION;
85     version[1] = (ucaVersion[0] << 3) + ucaVersion[1];
86     version[2] = ucaVersion[2] << 6;
87     version[3] = 0;
88 }
89 
90 void
setVersion(const UVersionInfo baseVersion,const UVersionInfo rulesVersion)91 CollationTailoring::setVersion(const UVersionInfo baseVersion, const UVersionInfo rulesVersion) {
92     version[0] = UCOL_BUILDER_VERSION;
93     version[1] = baseVersion[1];
94     version[2] = (baseVersion[2] & 0xc0) + ((rulesVersion[0] + (rulesVersion[0] >> 6)) & 0x3f);
95     version[3] = (rulesVersion[1] << 3) + (rulesVersion[1] >> 5) + rulesVersion[2] +
96             (rulesVersion[3] << 4) + (rulesVersion[3] >> 4);
97 }
98 
99 int32_t
getUCAVersion() const100 CollationTailoring::getUCAVersion() const {
101     return ((int32_t)version[1] << 4) | (version[2] >> 6);
102 }
103 
104 U_NAMESPACE_END
105 
106 #endif  // !UCONFIG_NO_COLLATION
107