]>
Commit | Line | Data |
---|---|---|
0f5d89e8 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
3 | /* | |
4 | *************************************************************************** | |
5 | * Copyright (C) 1999-2014 International Business Machines Corporation * | |
6 | * and others. All rights reserved. * | |
7 | *************************************************************************** | |
8 | */ | |
9 | ||
10 | #include "unicode/utypes.h" | |
11 | ||
12 | #if !UCONFIG_NO_BREAK_ITERATION | |
13 | ||
14 | #include "unicode/utypes.h" | |
15 | #include "rbbidata57.h" | |
16 | #include "rbbirb57.h" | |
17 | #include "utrie.h" | |
18 | #include "udatamem.h" | |
19 | #include "cmemory.h" | |
20 | #include "cstring.h" | |
21 | #include "umutex.h" | |
22 | ||
23 | #include "uassert.h" | |
24 | ||
25 | ||
26 | //----------------------------------------------------------------------------------- | |
27 | // | |
28 | // Trie access folding function. Copied as-is from properties code in uchar.c | |
29 | // | |
30 | //----------------------------------------------------------------------------------- | |
31 | U_CDECL_BEGIN | |
32 | static int32_t U_CALLCONV | |
33 | getFoldingOffset(uint32_t data) { | |
34 | /* if bit 15 is set, then the folding offset is in bits 14..0 of the 16-bit trie result */ | |
35 | if(data&0x8000) { | |
36 | return (int32_t)(data&0x7fff); | |
37 | } else { | |
38 | return 0; | |
39 | } | |
40 | } | |
41 | U_CDECL_END | |
42 | ||
43 | U_NAMESPACE_BEGIN | |
44 | ||
45 | //----------------------------------------------------------------------------- | |
46 | // | |
47 | // Constructors. | |
48 | // | |
49 | //----------------------------------------------------------------------------- | |
50 | RBBIDataWrapper57::RBBIDataWrapper57(const RBBIDataHeader57 *data, UErrorCode &status) { | |
51 | init0(); | |
52 | init(data, status); | |
53 | } | |
54 | ||
55 | RBBIDataWrapper57::RBBIDataWrapper57(const RBBIDataHeader57 *data, enum EDontAdopt, UErrorCode &status) { | |
56 | init0(); | |
57 | init(data, status); | |
58 | fDontFreeData = TRUE; | |
59 | } | |
60 | ||
61 | RBBIDataWrapper57::RBBIDataWrapper57(UDataMemory* udm, UErrorCode &status) { | |
62 | init0(); | |
63 | if (U_FAILURE(status)) { | |
64 | return; | |
65 | } | |
66 | const DataHeader *dh = udm->pHeader; | |
67 | int32_t headerSize = dh->dataHeader.headerSize; | |
68 | if ( !(headerSize >= 20 && | |
69 | dh->info.isBigEndian == U_IS_BIG_ENDIAN && | |
70 | dh->info.charsetFamily == U_CHARSET_FAMILY && | |
71 | dh->info.dataFormat[0] == 0x42 && // dataFormat="Brk " | |
72 | dh->info.dataFormat[1] == 0x72 && | |
73 | dh->info.dataFormat[2] == 0x6b && | |
74 | dh->info.dataFormat[3] == 0x20) | |
75 | // Note: info.fFormatVersion is duplicated in the RBBIDataHeader57, and is | |
76 | // validated when checking that. | |
77 | ) { | |
78 | status = U_INVALID_FORMAT_ERROR; | |
79 | return; | |
80 | } | |
81 | const char *dataAsBytes = reinterpret_cast<const char *>(dh); | |
82 | const RBBIDataHeader57 *rbbidh = reinterpret_cast<const RBBIDataHeader57 *>(dataAsBytes + headerSize); | |
83 | init(rbbidh, status); | |
84 | fUDataMem = udm; | |
85 | } | |
86 | ||
87 | //----------------------------------------------------------------------------- | |
88 | // | |
89 | // init(). Does most of the work of construction, shared between the | |
90 | // constructors. | |
91 | // | |
92 | //----------------------------------------------------------------------------- | |
93 | void RBBIDataWrapper57::init0() { | |
94 | fHeader = NULL; | |
95 | fForwardTable = NULL; | |
96 | fReverseTable = NULL; | |
97 | fSafeFwdTable = NULL; | |
98 | fSafeRevTable = NULL; | |
99 | fRuleSource = NULL; | |
100 | fRuleStatusTable = NULL; | |
101 | fUDataMem = NULL; | |
102 | fRefCount = 0; | |
103 | fDontFreeData = TRUE; | |
104 | } | |
105 | ||
106 | void RBBIDataWrapper57::init(const RBBIDataHeader57 *data, UErrorCode &status) { | |
107 | if (U_FAILURE(status)) { | |
108 | return; | |
109 | } | |
110 | fHeader = data; | |
111 | if (fHeader->fMagic != 0xb1a0 || fHeader->fFormatVersion[0] != 3) | |
112 | { | |
113 | status = U_INVALID_FORMAT_ERROR; | |
114 | return; | |
115 | } | |
116 | // Note: in ICU version 3.2 and earlier, there was a formatVersion 1 | |
117 | // that is no longer supported. At that time fFormatVersion was | |
118 | // an int32_t field, rather than an array of 4 bytes. | |
119 | ||
120 | fDontFreeData = FALSE; | |
121 | if (data->fFTableLen != 0) { | |
122 | fForwardTable = (RBBIStateTable *)((char *)data + fHeader->fFTable); | |
123 | } | |
124 | if (data->fRTableLen != 0) { | |
125 | fReverseTable = (RBBIStateTable *)((char *)data + fHeader->fRTable); | |
126 | } | |
127 | if (data->fSFTableLen != 0) { | |
128 | fSafeFwdTable = (RBBIStateTable *)((char *)data + fHeader->fSFTable); | |
129 | } | |
130 | if (data->fSRTableLen != 0) { | |
131 | fSafeRevTable = (RBBIStateTable *)((char *)data + fHeader->fSRTable); | |
132 | } | |
133 | ||
134 | ||
135 | utrie_unserialize(&fTrie, | |
136 | (uint8_t *)data + fHeader->fTrie, | |
137 | fHeader->fTrieLen, | |
138 | &status); | |
139 | if (U_FAILURE(status)) { | |
140 | return; | |
141 | } | |
142 | fTrie.getFoldingOffset=getFoldingOffset; | |
143 | ||
144 | ||
145 | fRuleSource = (UChar *)((char *)data + fHeader->fRuleSource); | |
146 | fRuleString.setTo(TRUE, fRuleSource, -1); | |
147 | U_ASSERT(data->fRuleSourceLen > 0); | |
148 | ||
149 | fRuleStatusTable = (int32_t *)((char *)data + fHeader->fStatusTable); | |
150 | fStatusMaxIdx = data->fStatusTableLen / sizeof(int32_t); | |
151 | ||
152 | fRefCount = 1; | |
153 | ||
154 | #ifdef RBBI_DEBUG | |
155 | char *debugEnv = getenv("U_RBBIDEBUG"); | |
156 | if (debugEnv && uprv_strstr(debugEnv, "data")) {this->printData();} | |
157 | #endif | |
158 | } | |
159 | ||
160 | ||
161 | //----------------------------------------------------------------------------- | |
162 | // | |
163 | // Destructor. Don't call this - use removeReference() instead. | |
164 | // | |
165 | //----------------------------------------------------------------------------- | |
166 | RBBIDataWrapper57::~RBBIDataWrapper57() { | |
167 | U_ASSERT(fRefCount == 0); | |
168 | if (fUDataMem) { | |
169 | udata_close(fUDataMem); | |
170 | } else if (!fDontFreeData) { | |
171 | uprv_free((void *)fHeader); | |
172 | } | |
173 | } | |
174 | ||
175 | ||
176 | ||
177 | //----------------------------------------------------------------------------- | |
178 | // | |
179 | // Operator == Consider two RBBIDataWrappers to be equal if they | |
180 | // refer to the same underlying data. Although | |
181 | // the data wrappers are normally shared between | |
182 | // iterator instances, it's possible to independently | |
183 | // open the same data twice, and get two instances, which | |
184 | // should still be ==. | |
185 | // | |
186 | //----------------------------------------------------------------------------- | |
187 | UBool RBBIDataWrapper57::operator ==(const RBBIDataWrapper57 &other) const { | |
188 | if (fHeader == other.fHeader) { | |
189 | return TRUE; | |
190 | } | |
191 | if (fHeader->fLength != other.fHeader->fLength) { | |
192 | return FALSE; | |
193 | } | |
194 | if (uprv_memcmp(fHeader, other.fHeader, fHeader->fLength) == 0) { | |
195 | return TRUE; | |
196 | } | |
197 | return FALSE; | |
198 | } | |
199 | ||
200 | int32_t RBBIDataWrapper57::hashCode() { | |
201 | return fHeader->fFTableLen; | |
202 | } | |
203 | ||
204 | ||
205 | ||
206 | //----------------------------------------------------------------------------- | |
207 | // | |
208 | // Reference Counting. A single RBBIDataWrapper57 object is shared among | |
209 | // however many RulesBasedBreakIterator instances are | |
210 | // referencing the same data. | |
211 | // | |
212 | //----------------------------------------------------------------------------- | |
213 | void RBBIDataWrapper57::removeReference() { | |
214 | if (umtx_atomic_dec(&fRefCount) == 0) { | |
215 | delete this; | |
216 | } | |
217 | } | |
218 | ||
219 | ||
220 | RBBIDataWrapper57 *RBBIDataWrapper57::addReference() { | |
221 | umtx_atomic_inc(&fRefCount); | |
222 | return this; | |
223 | } | |
224 | ||
225 | ||
226 | ||
227 | //----------------------------------------------------------------------------- | |
228 | // | |
229 | // getRuleSourceString | |
230 | // | |
231 | //----------------------------------------------------------------------------- | |
232 | const UnicodeString &RBBIDataWrapper57::getRuleSourceString() const { | |
233 | return fRuleString; | |
234 | } | |
235 | ||
236 | ||
237 | //----------------------------------------------------------------------------- | |
238 | // | |
239 | // print - debugging function to dump the runtime data tables. | |
240 | // | |
241 | //----------------------------------------------------------------------------- | |
242 | #ifdef RBBI_DEBUG | |
243 | void RBBIDataWrapper57::printTable(const char *heading, const RBBIStateTable *table) { | |
244 | uint32_t c; | |
245 | uint32_t s; | |
246 | ||
247 | RBBIDebugPrintf(" %s\n", heading); | |
248 | ||
249 | RBBIDebugPrintf("State | Acc LA TagIx"); | |
250 | for (c=0; c<fHeader->fCatCount; c++) {RBBIDebugPrintf("%3d ", c);} | |
251 | RBBIDebugPrintf("\n------|---------------"); for (c=0;c<fHeader->fCatCount; c++) { | |
252 | RBBIDebugPrintf("----"); | |
253 | } | |
254 | RBBIDebugPrintf("\n"); | |
255 | ||
256 | if (table == NULL) { | |
257 | RBBIDebugPrintf(" N U L L T A B L E\n\n"); | |
258 | return; | |
259 | } | |
260 | for (s=0; s<table->fNumStates; s++) { | |
261 | RBBIStateTableRow *row = (RBBIStateTableRow *) | |
262 | (table->fTableData + (table->fRowLen * s)); | |
263 | RBBIDebugPrintf("%4d | %3d %3d %3d ", s, row->fAccepting, row->fLookAhead, row->fTagIdx); | |
264 | for (c=0; c<fHeader->fCatCount; c++) { | |
265 | RBBIDebugPrintf("%3d ", row->fNextState[c]); | |
266 | } | |
267 | RBBIDebugPrintf("\n"); | |
268 | } | |
269 | RBBIDebugPrintf("\n"); | |
270 | } | |
271 | #endif | |
272 | ||
273 | ||
274 | #ifdef RBBI_DEBUG | |
275 | void RBBIDataWrapper57::printData() { | |
276 | RBBIDebugPrintf("RBBI Data at %p\n", (void *)fHeader); | |
277 | RBBIDebugPrintf(" Version = {%d %d %d %d}\n", fHeader->fFormatVersion[0], fHeader->fFormatVersion[1], | |
278 | fHeader->fFormatVersion[2], fHeader->fFormatVersion[3]); | |
279 | RBBIDebugPrintf(" total length of data = %d\n", fHeader->fLength); | |
280 | RBBIDebugPrintf(" number of character categories = %d\n\n", fHeader->fCatCount); | |
281 | ||
282 | printTable("Forward State Transition Table", fForwardTable); | |
283 | printTable("Reverse State Transition Table", fReverseTable); | |
284 | printTable("Safe Forward State Transition Table", fSafeFwdTable); | |
285 | printTable("Safe Reverse State Transition Table", fSafeRevTable); | |
286 | ||
287 | RBBIDebugPrintf("\nOrignal Rules source:\n"); | |
288 | for (int32_t c=0; fRuleSource[c] != 0; c++) { | |
289 | RBBIDebugPrintf("%c", fRuleSource[c]); | |
290 | } | |
291 | RBBIDebugPrintf("\n\n"); | |
292 | } | |
293 | #endif | |
294 | ||
295 | ||
296 | U_NAMESPACE_END | |
297 | U_NAMESPACE_USE | |
298 | ||
299 | //----------------------------------------------------------------------------- | |
300 | // | |
301 | // ubrk_swap - in standard rbbidata.cpp | |
302 | // | |
303 | //----------------------------------------------------------------------------- | |
304 | ||
305 | #endif /* #if !UCONFIG_NO_BREAK_ITERATION */ |