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