/src/icu/source/common/rbbisetb.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 | | // rbbisetb.cpp |
5 | | // |
6 | | /* |
7 | | *************************************************************************** |
8 | | * Copyright (C) 2002-2008 International Business Machines Corporation * |
9 | | * and others. All rights reserved. * |
10 | | *************************************************************************** |
11 | | */ |
12 | | // |
13 | | // RBBISetBuilder Handles processing of Unicode Sets from RBBI rules |
14 | | // (part of the rule building process.) |
15 | | // |
16 | | // Starting with the rules parse tree from the scanner, |
17 | | // |
18 | | // - Enumerate the set of UnicodeSets that are referenced |
19 | | // by the RBBI rules. |
20 | | // - compute a set of non-overlapping character ranges |
21 | | // with all characters within a range belonging to the same |
22 | | // set of input unicode sets. |
23 | | // - Derive a set of non-overlapping UnicodeSet (like things) |
24 | | // that will correspond to columns in the state table for |
25 | | // the RBBI execution engine. All characters within one |
26 | | // of these sets belong to the same set of the original |
27 | | // UnicodeSets from the user's rules. |
28 | | // - construct the trie table that maps input characters |
29 | | // to the index of the matching non-overlapping set of set from |
30 | | // the previous step. |
31 | | // |
32 | | |
33 | | #include "unicode/utypes.h" |
34 | | |
35 | | #if !UCONFIG_NO_BREAK_ITERATION |
36 | | |
37 | | #include "unicode/uniset.h" |
38 | | #include "uvector.h" |
39 | | #include "uassert.h" |
40 | | #include "cmemory.h" |
41 | | #include "cstring.h" |
42 | | |
43 | | #include "rbbisetb.h" |
44 | | #include "rbbinode.h" |
45 | | |
46 | | U_NAMESPACE_BEGIN |
47 | | |
48 | | const int32_t kMaxCharCategoriesFor8BitsTrie = 255; |
49 | | //------------------------------------------------------------------------ |
50 | | // |
51 | | // Constructor |
52 | | // |
53 | | //------------------------------------------------------------------------ |
54 | | RBBISetBuilder::RBBISetBuilder(RBBIRuleBuilder *rb) |
55 | 0 | { |
56 | 0 | fRB = rb; |
57 | 0 | fStatus = rb->fStatus; |
58 | 0 | fRangeList = nullptr; |
59 | 0 | fMutableTrie = nullptr; |
60 | 0 | fTrie = nullptr; |
61 | 0 | fTrieSize = 0; |
62 | 0 | fGroupCount = 0; |
63 | 0 | fSawBOF = false; |
64 | 0 | } |
65 | | |
66 | | |
67 | | //------------------------------------------------------------------------ |
68 | | // |
69 | | // Destructor |
70 | | // |
71 | | //------------------------------------------------------------------------ |
72 | | RBBISetBuilder::~RBBISetBuilder() |
73 | 0 | { |
74 | 0 | RangeDescriptor *nextRangeDesc; |
75 | | |
76 | | // Walk through & delete the linked list of RangeDescriptors |
77 | 0 | for (nextRangeDesc = fRangeList; nextRangeDesc!=NULL;) { |
78 | 0 | RangeDescriptor *r = nextRangeDesc; |
79 | 0 | nextRangeDesc = r->fNext; |
80 | 0 | delete r; |
81 | 0 | } |
82 | |
|
83 | 0 | ucptrie_close(fTrie); |
84 | 0 | umutablecptrie_close(fMutableTrie); |
85 | 0 | } |
86 | | |
87 | | |
88 | | |
89 | | |
90 | | //------------------------------------------------------------------------ |
91 | | // |
92 | | // build Build the list of non-overlapping character ranges |
93 | | // from the Unicode Sets. |
94 | | // |
95 | | //------------------------------------------------------------------------ |
96 | 0 | void RBBISetBuilder::buildRanges() { |
97 | 0 | RBBINode *usetNode; |
98 | 0 | RangeDescriptor *rlRange; |
99 | |
|
100 | 0 | if (fRB->fDebugEnv && uprv_strstr(fRB->fDebugEnv, "usets")) {printSets();} |
101 | | |
102 | | // |
103 | | // Initialize the process by creating a single range encompassing all characters |
104 | | // that is in no sets. |
105 | | // |
106 | 0 | fRangeList = new RangeDescriptor(*fStatus); // will check for status here |
107 | 0 | if (fRangeList == NULL) { |
108 | 0 | *fStatus = U_MEMORY_ALLOCATION_ERROR; |
109 | 0 | return; |
110 | 0 | } |
111 | 0 | fRangeList->fStartChar = 0; |
112 | 0 | fRangeList->fEndChar = 0x10ffff; |
113 | |
|
114 | 0 | if (U_FAILURE(*fStatus)) { |
115 | 0 | return; |
116 | 0 | } |
117 | | |
118 | | // |
119 | | // Find the set of non-overlapping ranges of characters |
120 | | // |
121 | 0 | int ni; |
122 | 0 | for (ni=0; ; ni++) { // Loop over each of the UnicodeSets encountered in the input rules |
123 | 0 | usetNode = (RBBINode *)this->fRB->fUSetNodes->elementAt(ni); |
124 | 0 | if (usetNode==NULL) { |
125 | 0 | break; |
126 | 0 | } |
127 | | |
128 | 0 | UnicodeSet *inputSet = usetNode->fInputSet; |
129 | 0 | int32_t inputSetRangeCount = inputSet->getRangeCount(); |
130 | 0 | int inputSetRangeIndex = 0; |
131 | 0 | rlRange = fRangeList; |
132 | |
|
133 | 0 | for (;;) { |
134 | 0 | if (inputSetRangeIndex >= inputSetRangeCount) { |
135 | 0 | break; |
136 | 0 | } |
137 | 0 | UChar32 inputSetRangeBegin = inputSet->getRangeStart(inputSetRangeIndex); |
138 | 0 | UChar32 inputSetRangeEnd = inputSet->getRangeEnd(inputSetRangeIndex); |
139 | | |
140 | | // skip over ranges from the range list that are completely |
141 | | // below the current range from the input unicode set. |
142 | 0 | while (rlRange->fEndChar < inputSetRangeBegin) { |
143 | 0 | rlRange = rlRange->fNext; |
144 | 0 | } |
145 | | |
146 | | // If the start of the range from the range list is before with |
147 | | // the start of the range from the unicode set, split the range list range |
148 | | // in two, with one part being before (wholly outside of) the unicode set |
149 | | // and the other containing the rest. |
150 | | // Then continue the loop; the post-split current range will then be skipped |
151 | | // over |
152 | 0 | if (rlRange->fStartChar < inputSetRangeBegin) { |
153 | 0 | rlRange->split(inputSetRangeBegin, *fStatus); |
154 | 0 | if (U_FAILURE(*fStatus)) { |
155 | 0 | return; |
156 | 0 | } |
157 | 0 | continue; |
158 | 0 | } |
159 | | |
160 | | // Same thing at the end of the ranges... |
161 | | // If the end of the range from the range list doesn't coincide with |
162 | | // the end of the range from the unicode set, split the range list |
163 | | // range in two. The first part of the split range will be |
164 | | // wholly inside the Unicode set. |
165 | 0 | if (rlRange->fEndChar > inputSetRangeEnd) { |
166 | 0 | rlRange->split(inputSetRangeEnd+1, *fStatus); |
167 | 0 | if (U_FAILURE(*fStatus)) { |
168 | 0 | return; |
169 | 0 | } |
170 | 0 | } |
171 | | |
172 | | // The current rlRange is now entirely within the UnicodeSet range. |
173 | | // Add this unicode set to the list of sets for this rlRange |
174 | 0 | if (rlRange->fIncludesSets->indexOf(usetNode) == -1) { |
175 | 0 | rlRange->fIncludesSets->addElementX(usetNode, *fStatus); |
176 | 0 | if (U_FAILURE(*fStatus)) { |
177 | 0 | return; |
178 | 0 | } |
179 | 0 | } |
180 | | |
181 | | // Advance over ranges that we are finished with. |
182 | 0 | if (inputSetRangeEnd == rlRange->fEndChar) { |
183 | 0 | inputSetRangeIndex++; |
184 | 0 | } |
185 | 0 | rlRange = rlRange->fNext; |
186 | 0 | } |
187 | 0 | } |
188 | | |
189 | 0 | if (fRB->fDebugEnv && uprv_strstr(fRB->fDebugEnv, "range")) { printRanges();} |
190 | | |
191 | | // |
192 | | // Group the above ranges, with each group consisting of one or more |
193 | | // ranges that are in exactly the same set of original UnicodeSets. |
194 | | // The groups are numbered, and these group numbers are the set of |
195 | | // input symbols recognized by the run-time state machine. |
196 | | // |
197 | | // Numbering: # 0 (state table column 0) is unused. |
198 | | // # 1 is reserved - table column 1 is for end-of-input |
199 | | // # 2 is reserved - table column 2 is for beginning-of-input |
200 | | // # 3 is the first range list. |
201 | | // |
202 | 0 | RangeDescriptor *rlSearchRange; |
203 | 0 | int32_t dictGroupCount = 0; |
204 | |
|
205 | 0 | for (rlRange = fRangeList; rlRange!=nullptr; rlRange=rlRange->fNext) { |
206 | 0 | for (rlSearchRange=fRangeList; rlSearchRange != rlRange; rlSearchRange=rlSearchRange->fNext) { |
207 | 0 | if (rlRange->fIncludesSets->equals(*rlSearchRange->fIncludesSets)) { |
208 | 0 | rlRange->fNum = rlSearchRange->fNum; |
209 | 0 | rlRange->fIncludesDict = rlSearchRange->fIncludesDict; |
210 | 0 | break; |
211 | 0 | } |
212 | 0 | } |
213 | 0 | if (rlRange->fNum == 0) { |
214 | 0 | rlRange->fFirstInGroup = true; |
215 | 0 | if (rlRange->isDictionaryRange()) { |
216 | 0 | rlRange->fNum = ++dictGroupCount; |
217 | 0 | rlRange->fIncludesDict = true; |
218 | 0 | } else { |
219 | 0 | fGroupCount++; |
220 | 0 | rlRange->fNum = fGroupCount+2; |
221 | 0 | addValToSets(rlRange->fIncludesSets, rlRange->fNum); |
222 | 0 | } |
223 | 0 | } |
224 | 0 | } |
225 | | |
226 | | // Move the character category numbers for any dictionary ranges up, so that they |
227 | | // immediately follow the non-dictionary ranges. |
228 | |
|
229 | 0 | fDictCategoriesStart = fGroupCount + 3; |
230 | 0 | for (rlRange = fRangeList; rlRange!=nullptr; rlRange=rlRange->fNext) { |
231 | 0 | if (rlRange->fIncludesDict) { |
232 | 0 | rlRange->fNum += fDictCategoriesStart - 1; |
233 | 0 | if (rlRange->fFirstInGroup) { |
234 | 0 | addValToSets(rlRange->fIncludesSets, rlRange->fNum); |
235 | 0 | } |
236 | 0 | } |
237 | 0 | } |
238 | 0 | fGroupCount += dictGroupCount; |
239 | | |
240 | | |
241 | | // Handle input sets that contain the special string {eof}. |
242 | | // Column 1 of the state table is reserved for EOF on input. |
243 | | // Column 2 is reserved for before-the-start-input. |
244 | | // (This column can be optimized away later if there are no rule |
245 | | // references to {bof}.) |
246 | | // Add this column value (1 or 2) to the equivalent expression |
247 | | // subtree for each UnicodeSet that contains the string {eof} |
248 | | // Because {bof} and {eof} are not characters in the normal sense, |
249 | | // they don't affect the computation of the ranges or TRIE. |
250 | |
|
251 | 0 | UnicodeString eofString(u"eof"); |
252 | 0 | UnicodeString bofString(u"bof"); |
253 | 0 | for (ni=0; ; ni++) { // Loop over each of the UnicodeSets encountered in the input rules |
254 | 0 | usetNode = (RBBINode *)this->fRB->fUSetNodes->elementAt(ni); |
255 | 0 | if (usetNode==NULL) { |
256 | 0 | break; |
257 | 0 | } |
258 | 0 | UnicodeSet *inputSet = usetNode->fInputSet; |
259 | 0 | if (inputSet->contains(eofString)) { |
260 | 0 | addValToSet(usetNode, 1); |
261 | 0 | } |
262 | 0 | if (inputSet->contains(bofString)) { |
263 | 0 | addValToSet(usetNode, 2); |
264 | 0 | fSawBOF = TRUE; |
265 | 0 | } |
266 | 0 | } |
267 | | |
268 | |
|
269 | 0 | if (fRB->fDebugEnv && uprv_strstr(fRB->fDebugEnv, "rgroup")) {printRangeGroups();} |
270 | 0 | if (fRB->fDebugEnv && uprv_strstr(fRB->fDebugEnv, "esets")) {printSets();} |
271 | 0 | } |
272 | | |
273 | | |
274 | | // |
275 | | // Build the Trie table for mapping UChar32 values to the corresponding |
276 | | // range group number. |
277 | | // |
278 | 0 | void RBBISetBuilder::buildTrie() { |
279 | 0 | fMutableTrie = umutablecptrie_open( |
280 | 0 | 0, // Initial value for all code points. |
281 | 0 | 0, // Error value for out-of-range input. |
282 | 0 | fStatus); |
283 | |
|
284 | 0 | for (RangeDescriptor *range = fRangeList; range!=nullptr && U_SUCCESS(*fStatus); range=range->fNext) { |
285 | 0 | umutablecptrie_setRange(fMutableTrie, |
286 | 0 | range->fStartChar, // Range start |
287 | 0 | range->fEndChar, // Range end (inclusive) |
288 | 0 | range->fNum, // value for range |
289 | 0 | fStatus); |
290 | 0 | } |
291 | 0 | } |
292 | | |
293 | | |
294 | 0 | void RBBISetBuilder::mergeCategories(IntPair categories) { |
295 | 0 | U_ASSERT(categories.first >= 1); |
296 | 0 | U_ASSERT(categories.second > categories.first); |
297 | 0 | U_ASSERT((categories.first < fDictCategoriesStart && categories.second < fDictCategoriesStart) || |
298 | 0 | (categories.first >= fDictCategoriesStart && categories.second >= fDictCategoriesStart)); |
299 | |
|
300 | 0 | for (RangeDescriptor *rd = fRangeList; rd != nullptr; rd = rd->fNext) { |
301 | 0 | int32_t rangeNum = rd->fNum; |
302 | 0 | if (rangeNum == categories.second) { |
303 | 0 | rd->fNum = categories.first; |
304 | 0 | } else if (rangeNum > categories.second) { |
305 | 0 | rd->fNum--; |
306 | 0 | } |
307 | 0 | } |
308 | 0 | --fGroupCount; |
309 | 0 | if (categories.second <= fDictCategoriesStart) { |
310 | 0 | --fDictCategoriesStart; |
311 | 0 | } |
312 | 0 | } |
313 | | |
314 | | |
315 | | //----------------------------------------------------------------------------------- |
316 | | // |
317 | | // getTrieSize() Return the size that will be required to serialize the Trie. |
318 | | // |
319 | | //----------------------------------------------------------------------------------- |
320 | 0 | int32_t RBBISetBuilder::getTrieSize() { |
321 | 0 | if (U_FAILURE(*fStatus)) { |
322 | 0 | return 0; |
323 | 0 | } |
324 | 0 | if (fTrie == nullptr) { |
325 | 0 | bool use8Bits = getNumCharCategories() <= kMaxCharCategoriesFor8BitsTrie; |
326 | 0 | fTrie = umutablecptrie_buildImmutable( |
327 | 0 | fMutableTrie, |
328 | 0 | UCPTRIE_TYPE_FAST, |
329 | 0 | use8Bits ? UCPTRIE_VALUE_BITS_8 : UCPTRIE_VALUE_BITS_16, |
330 | 0 | fStatus); |
331 | 0 | fTrieSize = ucptrie_toBinary(fTrie, nullptr, 0, fStatus); |
332 | 0 | if (*fStatus == U_BUFFER_OVERFLOW_ERROR) { |
333 | 0 | *fStatus = U_ZERO_ERROR; |
334 | 0 | } |
335 | 0 | } |
336 | 0 | return fTrieSize; |
337 | 0 | } |
338 | | |
339 | | |
340 | | //----------------------------------------------------------------------------------- |
341 | | // |
342 | | // serializeTrie() Put the serialized trie at the specified address. |
343 | | // Trust the caller to have given us enough memory. |
344 | | // getTrieSize() MUST be called first. |
345 | | // |
346 | | //----------------------------------------------------------------------------------- |
347 | 0 | void RBBISetBuilder::serializeTrie(uint8_t *where) { |
348 | 0 | ucptrie_toBinary(fTrie, |
349 | 0 | where, // Buffer |
350 | 0 | fTrieSize, // Capacity |
351 | 0 | fStatus); |
352 | 0 | } |
353 | | |
354 | | //------------------------------------------------------------------------ |
355 | | // |
356 | | // addValToSets Add a runtime-mapped input value to each uset from a |
357 | | // list of uset nodes. (val corresponds to a state table column.) |
358 | | // For each of the original Unicode sets - which correspond |
359 | | // directly to uset nodes - a logically equivalent expression |
360 | | // is constructed in terms of the remapped runtime input |
361 | | // symbol set. This function adds one runtime input symbol to |
362 | | // a list of sets. |
363 | | // |
364 | | // The "logically equivalent expression" is the tree for an |
365 | | // or-ing together of all of the symbols that go into the set. |
366 | | // |
367 | | //------------------------------------------------------------------------ |
368 | 0 | void RBBISetBuilder::addValToSets(UVector *sets, uint32_t val) { |
369 | 0 | int32_t ix; |
370 | |
|
371 | 0 | for (ix=0; ix<sets->size(); ix++) { |
372 | 0 | RBBINode *usetNode = (RBBINode *)sets->elementAt(ix); |
373 | 0 | addValToSet(usetNode, val); |
374 | 0 | } |
375 | 0 | } |
376 | | |
377 | 0 | void RBBISetBuilder::addValToSet(RBBINode *usetNode, uint32_t val) { |
378 | 0 | RBBINode *leafNode = new RBBINode(RBBINode::leafChar); |
379 | 0 | if (leafNode == NULL) { |
380 | 0 | *fStatus = U_MEMORY_ALLOCATION_ERROR; |
381 | 0 | return; |
382 | 0 | } |
383 | 0 | leafNode->fVal = (unsigned short)val; |
384 | 0 | if (usetNode->fLeftChild == NULL) { |
385 | 0 | usetNode->fLeftChild = leafNode; |
386 | 0 | leafNode->fParent = usetNode; |
387 | 0 | } else { |
388 | | // There are already input symbols present for this set. |
389 | | // Set up an OR node, with the previous stuff as the left child |
390 | | // and the new value as the right child. |
391 | 0 | RBBINode *orNode = new RBBINode(RBBINode::opOr); |
392 | 0 | if (orNode == NULL) { |
393 | 0 | *fStatus = U_MEMORY_ALLOCATION_ERROR; |
394 | 0 | return; |
395 | 0 | } |
396 | 0 | orNode->fLeftChild = usetNode->fLeftChild; |
397 | 0 | orNode->fRightChild = leafNode; |
398 | 0 | orNode->fLeftChild->fParent = orNode; |
399 | 0 | orNode->fRightChild->fParent = orNode; |
400 | 0 | usetNode->fLeftChild = orNode; |
401 | 0 | orNode->fParent = usetNode; |
402 | 0 | } |
403 | 0 | } |
404 | | |
405 | | |
406 | | //------------------------------------------------------------------------ |
407 | | // |
408 | | // getNumCharCategories |
409 | | // |
410 | | //------------------------------------------------------------------------ |
411 | 0 | int32_t RBBISetBuilder::getNumCharCategories() const { |
412 | 0 | return fGroupCount + 3; |
413 | 0 | } |
414 | | |
415 | | |
416 | | //------------------------------------------------------------------------ |
417 | | // |
418 | | // getDictCategoriesStart |
419 | | // |
420 | | //------------------------------------------------------------------------ |
421 | 0 | int32_t RBBISetBuilder::getDictCategoriesStart() const { |
422 | 0 | return fDictCategoriesStart; |
423 | 0 | } |
424 | | |
425 | | |
426 | | //------------------------------------------------------------------------ |
427 | | // |
428 | | // sawBOF |
429 | | // |
430 | | //------------------------------------------------------------------------ |
431 | 0 | UBool RBBISetBuilder::sawBOF() const { |
432 | 0 | return fSawBOF; |
433 | 0 | } |
434 | | |
435 | | |
436 | | //------------------------------------------------------------------------ |
437 | | // |
438 | | // getFirstChar Given a runtime RBBI character category, find |
439 | | // the first UChar32 that is in the set of chars |
440 | | // in the category. |
441 | | //------------------------------------------------------------------------ |
442 | 0 | UChar32 RBBISetBuilder::getFirstChar(int32_t category) const { |
443 | 0 | RangeDescriptor *rlRange; |
444 | 0 | UChar32 retVal = (UChar32)-1; |
445 | 0 | for (rlRange = fRangeList; rlRange!=nullptr; rlRange=rlRange->fNext) { |
446 | 0 | if (rlRange->fNum == category) { |
447 | 0 | retVal = rlRange->fStartChar; |
448 | 0 | break; |
449 | 0 | } |
450 | 0 | } |
451 | 0 | return retVal; |
452 | 0 | } |
453 | | |
454 | | |
455 | | //------------------------------------------------------------------------ |
456 | | // |
457 | | // printRanges A debugging function. |
458 | | // dump out all of the range definitions. |
459 | | // |
460 | | //------------------------------------------------------------------------ |
461 | | #ifdef RBBI_DEBUG |
462 | | void RBBISetBuilder::printRanges() { |
463 | | RangeDescriptor *rlRange; |
464 | | int i; |
465 | | |
466 | | RBBIDebugPrintf("\n\n Nonoverlapping Ranges ...\n"); |
467 | | for (rlRange = fRangeList; rlRange!=nullptr; rlRange=rlRange->fNext) { |
468 | | RBBIDebugPrintf("%4x-%4x ", rlRange->fStartChar, rlRange->fEndChar); |
469 | | |
470 | | for (i=0; i<rlRange->fIncludesSets->size(); i++) { |
471 | | RBBINode *usetNode = (RBBINode *)rlRange->fIncludesSets->elementAt(i); |
472 | | UnicodeString setName {u"anon"}; |
473 | | RBBINode *setRef = usetNode->fParent; |
474 | | if (setRef != nullptr) { |
475 | | RBBINode *varRef = setRef->fParent; |
476 | | if (varRef != nullptr && varRef->fType == RBBINode::varRef) { |
477 | | setName = varRef->fText; |
478 | | } |
479 | | } |
480 | | RBBI_DEBUG_printUnicodeString(setName); RBBIDebugPrintf(" "); |
481 | | } |
482 | | RBBIDebugPrintf("\n"); |
483 | | } |
484 | | } |
485 | | #endif |
486 | | |
487 | | |
488 | | //------------------------------------------------------------------------ |
489 | | // |
490 | | // printRangeGroups A debugging function. |
491 | | // dump out all of the range groups. |
492 | | // |
493 | | //------------------------------------------------------------------------ |
494 | | #ifdef RBBI_DEBUG |
495 | | void RBBISetBuilder::printRangeGroups() { |
496 | | int i; |
497 | | |
498 | | RBBIDebugPrintf("\nRanges grouped by Unicode Set Membership...\n"); |
499 | | for (RangeDescriptor *rlRange = fRangeList; rlRange!=nullptr; rlRange=rlRange->fNext) { |
500 | | if (rlRange->fFirstInGroup) { |
501 | | int groupNum = rlRange->fNum; |
502 | | RBBIDebugPrintf("%2i ", groupNum); |
503 | | |
504 | | if (groupNum >= fDictCategoriesStart) { RBBIDebugPrintf(" <DICT> ");} |
505 | | |
506 | | for (i=0; i<rlRange->fIncludesSets->size(); i++) { |
507 | | RBBINode *usetNode = (RBBINode *)rlRange->fIncludesSets->elementAt(i); |
508 | | UnicodeString setName = UNICODE_STRING("anon", 4); |
509 | | RBBINode *setRef = usetNode->fParent; |
510 | | if (setRef != NULL) { |
511 | | RBBINode *varRef = setRef->fParent; |
512 | | if (varRef != NULL && varRef->fType == RBBINode::varRef) { |
513 | | setName = varRef->fText; |
514 | | } |
515 | | } |
516 | | RBBI_DEBUG_printUnicodeString(setName); RBBIDebugPrintf(" "); |
517 | | } |
518 | | |
519 | | i = 0; |
520 | | for (RangeDescriptor *tRange = rlRange; tRange != nullptr; tRange = tRange->fNext) { |
521 | | if (tRange->fNum == rlRange->fNum) { |
522 | | if (i++ % 5 == 0) { |
523 | | RBBIDebugPrintf("\n "); |
524 | | } |
525 | | RBBIDebugPrintf(" %05x-%05x", tRange->fStartChar, tRange->fEndChar); |
526 | | } |
527 | | } |
528 | | RBBIDebugPrintf("\n"); |
529 | | } |
530 | | } |
531 | | RBBIDebugPrintf("\n"); |
532 | | } |
533 | | #endif |
534 | | |
535 | | |
536 | | //------------------------------------------------------------------------ |
537 | | // |
538 | | // printSets A debugging function. |
539 | | // dump out all of the set definitions. |
540 | | // |
541 | | //------------------------------------------------------------------------ |
542 | | #ifdef RBBI_DEBUG |
543 | | void RBBISetBuilder::printSets() { |
544 | | int i; |
545 | | |
546 | | RBBIDebugPrintf("\n\nUnicode Sets List\n------------------\n"); |
547 | | for (i=0; ; i++) { |
548 | | RBBINode *usetNode; |
549 | | RBBINode *setRef; |
550 | | RBBINode *varRef; |
551 | | UnicodeString setName; |
552 | | |
553 | | usetNode = (RBBINode *)fRB->fUSetNodes->elementAt(i); |
554 | | if (usetNode == NULL) { |
555 | | break; |
556 | | } |
557 | | |
558 | | RBBIDebugPrintf("%3d ", i); |
559 | | setName = UNICODE_STRING("anonymous", 9); |
560 | | setRef = usetNode->fParent; |
561 | | if (setRef != NULL) { |
562 | | varRef = setRef->fParent; |
563 | | if (varRef != NULL && varRef->fType == RBBINode::varRef) { |
564 | | setName = varRef->fText; |
565 | | } |
566 | | } |
567 | | RBBI_DEBUG_printUnicodeString(setName); |
568 | | RBBIDebugPrintf(" "); |
569 | | RBBI_DEBUG_printUnicodeString(usetNode->fText); |
570 | | RBBIDebugPrintf("\n"); |
571 | | if (usetNode->fLeftChild != NULL) { |
572 | | RBBINode::printTree(usetNode->fLeftChild, TRUE); |
573 | | } |
574 | | } |
575 | | RBBIDebugPrintf("\n"); |
576 | | } |
577 | | #endif |
578 | | |
579 | | |
580 | | |
581 | | //------------------------------------------------------------------------------------- |
582 | | // |
583 | | // RangeDescriptor copy constructor |
584 | | // |
585 | | //------------------------------------------------------------------------------------- |
586 | | |
587 | | RangeDescriptor::RangeDescriptor(const RangeDescriptor &other, UErrorCode &status) : |
588 | 0 | fStartChar(other.fStartChar), fEndChar {other.fEndChar}, fNum {other.fNum}, |
589 | 0 | fIncludesDict{other.fIncludesDict}, fFirstInGroup{other.fFirstInGroup} { |
590 | |
|
591 | 0 | if (U_FAILURE(status)) { |
592 | 0 | return; |
593 | 0 | } |
594 | 0 | fIncludesSets = new UVector(status); |
595 | 0 | if (this->fIncludesSets == nullptr) { |
596 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
597 | 0 | } |
598 | 0 | if (U_FAILURE(status)) { |
599 | 0 | return; |
600 | 0 | } |
601 | | |
602 | 0 | for (int32_t i=0; i<other.fIncludesSets->size(); i++) { |
603 | 0 | this->fIncludesSets->addElementX(other.fIncludesSets->elementAt(i), status); |
604 | 0 | } |
605 | 0 | } |
606 | | |
607 | | |
608 | | //------------------------------------------------------------------------------------- |
609 | | // |
610 | | // RangeDesriptor default constructor |
611 | | // |
612 | | //------------------------------------------------------------------------------------- |
613 | 0 | RangeDescriptor::RangeDescriptor(UErrorCode &status) { |
614 | 0 | if (U_FAILURE(status)) { |
615 | 0 | return; |
616 | 0 | } |
617 | 0 | fIncludesSets = new UVector(status); |
618 | 0 | if (fIncludesSets == nullptr) { |
619 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
620 | 0 | } |
621 | 0 | } |
622 | | |
623 | | |
624 | | //------------------------------------------------------------------------------------- |
625 | | // |
626 | | // RangeDesriptor Destructor |
627 | | // |
628 | | //------------------------------------------------------------------------------------- |
629 | 0 | RangeDescriptor::~RangeDescriptor() { |
630 | 0 | delete fIncludesSets; |
631 | 0 | fIncludesSets = nullptr; |
632 | 0 | } |
633 | | |
634 | | //------------------------------------------------------------------------------------- |
635 | | // |
636 | | // RangeDesriptor::split() |
637 | | // |
638 | | //------------------------------------------------------------------------------------- |
639 | 0 | void RangeDescriptor::split(UChar32 where, UErrorCode &status) { |
640 | 0 | U_ASSERT(where>fStartChar && where<=fEndChar); |
641 | 0 | RangeDescriptor *nr = new RangeDescriptor(*this, status); |
642 | 0 | if(nr == nullptr) { |
643 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
644 | 0 | return; |
645 | 0 | } |
646 | 0 | if (U_FAILURE(status)) { |
647 | 0 | delete nr; |
648 | 0 | return; |
649 | 0 | } |
650 | | // RangeDescriptor copy constructor copies all fields. |
651 | | // Only need to update those that are different after the split. |
652 | 0 | nr->fStartChar = where; |
653 | 0 | this->fEndChar = where-1; |
654 | 0 | nr->fNext = this->fNext; |
655 | 0 | this->fNext = nr; |
656 | 0 | } |
657 | | |
658 | | |
659 | | //------------------------------------------------------------------------------------- |
660 | | // |
661 | | // RangeDescriptor::isDictionaryRange |
662 | | // |
663 | | // Test whether this range includes characters from |
664 | | // the original Unicode Set named "dictionary". |
665 | | // |
666 | | // This function looks through the Unicode Sets that |
667 | | // the range includes, checking for one named "dictionary" |
668 | | // |
669 | | // TODO: a faster way would be to find the set node for |
670 | | // "dictionary" just once, rather than looking it |
671 | | // up by name every time. |
672 | | // |
673 | | //------------------------------------------------------------------------------------- |
674 | 0 | bool RangeDescriptor::isDictionaryRange() { |
675 | 0 | static const char16_t *dictionary = u"dictionary"; |
676 | 0 | for (int32_t i=0; i<fIncludesSets->size(); i++) { |
677 | 0 | RBBINode *usetNode = (RBBINode *)fIncludesSets->elementAt(i); |
678 | 0 | RBBINode *setRef = usetNode->fParent; |
679 | 0 | if (setRef != nullptr) { |
680 | 0 | RBBINode *varRef = setRef->fParent; |
681 | 0 | if (varRef && varRef->fType == RBBINode::varRef) { |
682 | 0 | const UnicodeString *setName = &varRef->fText; |
683 | 0 | if (setName->compare(dictionary, -1) == 0) { |
684 | 0 | return true; |
685 | 0 | } |
686 | 0 | } |
687 | 0 | } |
688 | 0 | } |
689 | 0 | return false; |
690 | 0 | } |
691 | | |
692 | | U_NAMESPACE_END |
693 | | |
694 | | #endif /* #if !UCONFIG_NO_BREAK_ITERATION */ |