| 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 |
{ |
| 56 |
fRB = rb; |
| 57 |
fStatus = rb->fStatus; |
| 58 |
fRangeList = nullptr; |
| 59 |
fMutableTrie = nullptr; |
| 60 |
fTrie = nullptr; |
| 61 |
fTrieSize = 0; |
| 62 |
fGroupCount = 0; |
| 63 |
fSawBOF = false; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
//------------------------------------------------------------------------ |
| 68 |
// |
| 69 |
// Destructor |
| 70 |
// |
| 71 |
//------------------------------------------------------------------------ |
| 72 |
RBBISetBuilder::~RBBISetBuilder() |
| 73 |
{ |
| 74 |
RangeDescriptor *nextRangeDesc; |
| 75 |
|
| 76 |
// Walk through & delete the linked list of RangeDescriptors |
| 77 |
for (nextRangeDesc = fRangeList; nextRangeDesc!=nullptr;) { |
| 78 |
RangeDescriptor *r = nextRangeDesc; |
| 79 |
nextRangeDesc = r->fNext; |
| 80 |
delete r; |
| 81 |
} |
| 82 |
|
| 83 |
ucptrie_close(fTrie); |
| 84 |
umutablecptrie_close(fMutableTrie); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
//------------------------------------------------------------------------ |
| 91 |
// |
| 92 |
// build Build the list of non-overlapping character ranges |
| 93 |
// from the Unicode Sets. |
| 94 |
// |
| 95 |
//------------------------------------------------------------------------ |
| 96 |
void RBBISetBuilder::buildRanges() { |
| 97 |
RBBINode *usetNode; |
| 98 |
RangeDescriptor *rlRange; |
| 99 |
|
| 100 |
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 |
fRangeList = new RangeDescriptor(*fStatus); // will check for status here |
| 107 |
if (fRangeList == nullptr) { |
| 108 |
*fStatus = U_MEMORY_ALLOCATION_ERROR; |
| 109 |
return; |
| 110 |
} |
| 111 |
fRangeList->fStartChar = 0; |
| 112 |
fRangeList->fEndChar = 0x10ffff; |
| 113 |
|
| 114 |
if (U_FAILURE(*fStatus)) { |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
// |
| 119 |
// Find the set of non-overlapping ranges of characters |
| 120 |
// |
| 121 |
int ni; |
| 122 |
for (ni=0; ; ni++) { // Loop over each of the UnicodeSets encountered in the input rules |
| 123 |
usetNode = static_cast<RBBINode*>(this->fRB->fUSetNodes->elementAt(ni)); |
| 124 |
if (usetNode==nullptr) { |
| 125 |
break; |
| 126 |
} |
| 127 |
|
| 128 |
UnicodeSet *inputSet = usetNode->fInputSet; |
| 129 |
int32_t inputSetRangeCount = inputSet->getRangeCount(); |
| 130 |
int inputSetRangeIndex = 0; |
| 131 |
rlRange = fRangeList; |
| 132 |
|
| 133 |
for (;;) { |
| 134 |
if (inputSetRangeIndex >= inputSetRangeCount) { |
| 135 |
break; |
| 136 |
} |
| 137 |
UChar32 inputSetRangeBegin = inputSet->getRangeStart(inputSetRangeIndex); |
| 138 |
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 |
while (rlRange->fEndChar < inputSetRangeBegin) { |
| 143 |
rlRange = rlRange->fNext; |
| 144 |
} |
| 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 |
if (rlRange->fStartChar < inputSetRangeBegin) { |
| 153 |
rlRange->split(inputSetRangeBegin, *fStatus); |
| 154 |
if (U_FAILURE(*fStatus)) { |
| 155 |
return; |
| 156 |
} |
| 157 |
continue; |
| 158 |
} |
| 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 |
if (rlRange->fEndChar > inputSetRangeEnd) { |
| 166 |
rlRange->split(inputSetRangeEnd+1, *fStatus); |
| 167 |
if (U_FAILURE(*fStatus)) { |
| 168 |
return; |
| 169 |
} |
| 170 |
} |
| 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 |
if (rlRange->fIncludesSets->indexOf(usetNode) == -1) { |
| 175 |
rlRange->fIncludesSets->addElement(usetNode, *fStatus); |
| 176 |
if (U_FAILURE(*fStatus)) { |
| 177 |
return; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
// Advance over ranges that we are finished with. |
| 182 |
if (inputSetRangeEnd == rlRange->fEndChar) { |
| 183 |
inputSetRangeIndex++; |
| 184 |
} |
| 185 |
rlRange = rlRange->fNext; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
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 |
RangeDescriptor *rlSearchRange; |
| 203 |
int32_t dictGroupCount = 0; |
| 204 |
|
| 205 |
for (rlRange = fRangeList; rlRange!=nullptr; rlRange=rlRange->fNext) { |
| 206 |
for (rlSearchRange=fRangeList; rlSearchRange != rlRange; rlSearchRange=rlSearchRange->fNext) { |
| 207 |
if (rlRange->fIncludesSets->equals(*rlSearchRange->fIncludesSets)) { |
| 208 |
rlRange->fNum = rlSearchRange->fNum; |
| 209 |
rlRange->fIncludesDict = rlSearchRange->fIncludesDict; |
| 210 |
break; |
| 211 |
} |
| 212 |
} |
| 213 |
if (rlRange->fNum == 0) { |
| 214 |
rlRange->fFirstInGroup = true; |
| 215 |
if (rlRange->isDictionaryRange()) { |
| 216 |
rlRange->fNum = ++dictGroupCount; |
| 217 |
rlRange->fIncludesDict = true; |
| 218 |
} else { |
| 219 |
fGroupCount++; |
| 220 |
rlRange->fNum = fGroupCount+2; |
| 221 |
addValToSets(rlRange->fIncludesSets, rlRange->fNum); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
// Move the character category numbers for any dictionary ranges up, so that they |
| 227 |
// immediately follow the non-dictionary ranges. |
| 228 |
|
| 229 |
fDictCategoriesStart = fGroupCount + 3; |
| 230 |
for (rlRange = fRangeList; rlRange!=nullptr; rlRange=rlRange->fNext) { |
| 231 |
if (rlRange->fIncludesDict) { |
| 232 |
rlRange->fNum += fDictCategoriesStart - 1; |
| 233 |
if (rlRange->fFirstInGroup) { |
| 234 |
addValToSets(rlRange->fIncludesSets, rlRange->fNum); |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
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 |
UnicodeString eofString(u"eof"); |
| 252 |
UnicodeString bofString(u"bof"); |
| 253 |
for (ni=0; ; ni++) { // Loop over each of the UnicodeSets encountered in the input rules |
| 254 |
usetNode = static_cast<RBBINode*>(this->fRB->fUSetNodes->elementAt(ni)); |
| 255 |
if (usetNode==nullptr) { |
| 256 |
break; |
| 257 |
} |
| 258 |
UnicodeSet *inputSet = usetNode->fInputSet; |
| 259 |
if (inputSet->contains(eofString)) { |
| 260 |
addValToSet(usetNode, 1); |
| 261 |
} |
| 262 |
if (inputSet->contains(bofString)) { |
| 263 |
addValToSet(usetNode, 2); |
| 264 |
fSawBOF = true; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
if (fRB->fDebugEnv && uprv_strstr(fRB->fDebugEnv, "rgroup")) {printRangeGroups();} |
| 270 |
if (fRB->fDebugEnv && uprv_strstr(fRB->fDebugEnv, "esets")) {printSets();} |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
// |
| 275 |
// Build the Trie table for mapping UChar32 values to the corresponding |
| 276 |
// range group number. |
| 277 |
// |
| 278 |
void RBBISetBuilder::buildTrie() { |
| 279 |
fMutableTrie = umutablecptrie_open( |
| 280 |
0, // Initial value for all code points. |
| 281 |
0, // Error value for out-of-range input. |
| 282 |
fStatus); |
| 283 |
|
| 284 |
for (RangeDescriptor *range = fRangeList; range!=nullptr && U_SUCCESS(*fStatus); range=range->fNext) { |
| 285 |
umutablecptrie_setRange(fMutableTrie, |
| 286 |
range->fStartChar, // Range start |
| 287 |
range->fEndChar, // Range end (inclusive) |
| 288 |
range->fNum, // value for range |
| 289 |
fStatus); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
void RBBISetBuilder::mergeCategories(IntPair categories) { |
| 295 |
U_ASSERT(categories.first >= 1); |
| 296 |
U_ASSERT(categories.second > categories.first); |
| 297 |
U_ASSERT((categories.first < fDictCategoriesStart && categories.second < fDictCategoriesStart) || |
| 298 |
(categories.first >= fDictCategoriesStart && categories.second >= fDictCategoriesStart)); |
| 299 |
|
| 300 |
for (RangeDescriptor *rd = fRangeList; rd != nullptr; rd = rd->fNext) { |
| 301 |
int32_t rangeNum = rd->fNum; |
| 302 |
if (rangeNum == categories.second) { |
| 303 |
rd->fNum = categories.first; |
| 304 |
} else if (rangeNum > categories.second) { |
| 305 |
rd->fNum--; |
| 306 |
} |
| 307 |
} |
| 308 |
--fGroupCount; |
| 309 |
if (categories.second <= fDictCategoriesStart) { |
| 310 |
--fDictCategoriesStart; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
//----------------------------------------------------------------------------------- |
| 316 |
// |
| 317 |
// getTrieSize() Return the size that will be required to serialize the Trie. |
| 318 |
// |
| 319 |
//----------------------------------------------------------------------------------- |
| 320 |
int32_t RBBISetBuilder::getTrieSize() { |
| 321 |
if (U_FAILURE(*fStatus)) { |
| 322 |
return 0; |
| 323 |
} |
| 324 |
if (fTrie == nullptr) { |
| 325 |
bool use8Bits = getNumCharCategories() <= kMaxCharCategoriesFor8BitsTrie; |
| 326 |
fTrie = umutablecptrie_buildImmutable( |
| 327 |
fMutableTrie, |
| 328 |
UCPTRIE_TYPE_FAST, |
| 329 |
use8Bits ? UCPTRIE_VALUE_BITS_8 : UCPTRIE_VALUE_BITS_16, |
| 330 |
fStatus); |
| 331 |
fTrieSize = ucptrie_toBinary(fTrie, nullptr, 0, fStatus); |
| 332 |
if (*fStatus == U_BUFFER_OVERFLOW_ERROR) { |
| 333 |
*fStatus = U_ZERO_ERROR; |
| 334 |
} |
| 335 |
} |
| 336 |
return fTrieSize; |
| 337 |
} |
| 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 |
void RBBISetBuilder::serializeTrie(uint8_t *where) { |
| 348 |
ucptrie_toBinary(fTrie, |
| 349 |
where, // Buffer |
| 350 |
fTrieSize, // Capacity |
| 351 |
fStatus); |
| 352 |
} |
| 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 |
void RBBISetBuilder::addValToSets(UVector *sets, uint32_t val) { |
| 369 |
int32_t ix; |
| 370 |
|
| 371 |
for (ix=0; ix<sets->size(); ix++) { |
| 372 |
RBBINode* usetNode = static_cast<RBBINode*>(sets->elementAt(ix)); |
| 373 |
addValToSet(usetNode, val); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
void RBBISetBuilder::addValToSet(RBBINode *usetNode, uint32_t val) { |
| 378 |
RBBINode *leafNode = new RBBINode(RBBINode::leafChar, *fStatus); |
| 379 |
if (U_FAILURE(*fStatus)) { |
| 380 |
delete leafNode; |
| 381 |
return; |
| 382 |
} |
| 383 |
if (leafNode == nullptr) { |
| 384 |
*fStatus = U_MEMORY_ALLOCATION_ERROR; |
| 385 |
return; |
| 386 |
} |
| 387 |
leafNode->fVal = static_cast<unsigned short>(val); |
| 388 |
if (usetNode->fLeftChild == nullptr) { |
| 389 |
usetNode->fLeftChild = leafNode; |
| 390 |
leafNode->fParent = usetNode; |
| 391 |
} else { |
| 392 |
// There are already input symbols present for this set. |
| 393 |
// Set up an OR node, with the previous stuff as the left child |
| 394 |
// and the new value as the right child. |
| 395 |
RBBINode *orNode = new RBBINode(RBBINode::opOr, *fStatus); |
| 396 |
if (orNode == nullptr) { |
| 397 |
*fStatus = U_MEMORY_ALLOCATION_ERROR; |
| 398 |
} |
| 399 |
if (U_FAILURE(*fStatus)) { |
| 400 |
delete orNode; |
| 401 |
delete leafNode; |
| 402 |
return; |
| 403 |
} |
| 404 |
orNode->fLeftChild = usetNode->fLeftChild; |
| 405 |
orNode->fRightChild = leafNode; |
| 406 |
orNode->fLeftChild->fParent = orNode; |
| 407 |
orNode->fRightChild->fParent = orNode; |
| 408 |
usetNode->fLeftChild = orNode; |
| 409 |
orNode->fParent = usetNode; |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
//------------------------------------------------------------------------ |
| 415 |
// |
| 416 |
// getNumCharCategories |
| 417 |
// |
| 418 |
//------------------------------------------------------------------------ |
| 419 |
int32_t RBBISetBuilder::getNumCharCategories() const { |
| 420 |
return fGroupCount + 3; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
//------------------------------------------------------------------------ |
| 425 |
// |
| 426 |
// getDictCategoriesStart |
| 427 |
// |
| 428 |
//------------------------------------------------------------------------ |
| 429 |
int32_t RBBISetBuilder::getDictCategoriesStart() const { |
| 430 |
return fDictCategoriesStart; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
//------------------------------------------------------------------------ |
| 435 |
// |
| 436 |
// sawBOF |
| 437 |
// |
| 438 |
//------------------------------------------------------------------------ |
| 439 |
UBool RBBISetBuilder::sawBOF() const { |
| 440 |
return fSawBOF; |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
//------------------------------------------------------------------------ |
| 445 |
// |
| 446 |
// getFirstChar Given a runtime RBBI character category, find |
| 447 |
// the first UChar32 that is in the set of chars |
| 448 |
// in the category. |
| 449 |
//------------------------------------------------------------------------ |
| 450 |
UChar32 RBBISetBuilder::getFirstChar(int32_t category) const { |
| 451 |
RangeDescriptor *rlRange; |
| 452 |
UChar32 retVal = static_cast<UChar32>(-1); |
| 453 |
for (rlRange = fRangeList; rlRange!=nullptr; rlRange=rlRange->fNext) { |
| 454 |
if (rlRange->fNum == category) { |
| 455 |
retVal = rlRange->fStartChar; |
| 456 |
break; |
| 457 |
} |
| 458 |
} |
| 459 |
return retVal; |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
//------------------------------------------------------------------------ |
| 464 |
// |
| 465 |
// printRanges A debugging function. |
| 466 |
// dump out all of the range definitions. |
| 467 |
// |
| 468 |
//------------------------------------------------------------------------ |
| 469 |
#ifdef RBBI_DEBUG |
| 470 |
void RBBISetBuilder::printRanges() { |
| 471 |
RangeDescriptor *rlRange; |
| 472 |
int i; |
| 473 |
|
| 474 |
RBBIDebugPrintf("\n\n Nonoverlapping Ranges ...\n"); |
| 475 |
for (rlRange = fRangeList; rlRange!=nullptr; rlRange=rlRange->fNext) { |
| 476 |
RBBIDebugPrintf("%4x-%4x ", rlRange->fStartChar, rlRange->fEndChar); |
| 477 |
|
| 478 |
for (i=0; i<rlRange->fIncludesSets->size(); i++) { |
| 479 |
RBBINode *usetNode = (RBBINode *)rlRange->fIncludesSets->elementAt(i); |
| 480 |
UnicodeString setName {u"anon"}; |
| 481 |
RBBINode *setRef = usetNode->fParent; |
| 482 |
if (setRef != nullptr) { |
| 483 |
RBBINode *varRef = setRef->fParent; |
| 484 |
if (varRef != nullptr && varRef->fType == RBBINode::varRef) { |
| 485 |
setName = varRef->fText; |
| 486 |
} |
| 487 |
} |
| 488 |
RBBI_DEBUG_printUnicodeString(setName); RBBIDebugPrintf(" "); |
| 489 |
} |
| 490 |
RBBIDebugPrintf("\n"); |
| 491 |
} |
| 492 |
} |
| 493 |
#endif |
| 494 |
|
| 495 |
|
| 496 |
//------------------------------------------------------------------------ |
| 497 |
// |
| 498 |
// printRangeGroups A debugging function. |
| 499 |
// dump out all of the range groups. |
| 500 |
// |
| 501 |
//------------------------------------------------------------------------ |
| 502 |
#ifdef RBBI_DEBUG |
| 503 |
void RBBISetBuilder::printRangeGroups() { |
| 504 |
int i; |
| 505 |
|
| 506 |
RBBIDebugPrintf("\nRanges grouped by Unicode Set Membership...\n"); |
| 507 |
for (RangeDescriptor *rlRange = fRangeList; rlRange!=nullptr; rlRange=rlRange->fNext) { |
| 508 |
if (rlRange->fFirstInGroup) { |
| 509 |
int groupNum = rlRange->fNum; |
| 510 |
RBBIDebugPrintf("%2i ", groupNum); |
| 511 |
|
| 512 |
if (groupNum >= fDictCategoriesStart) { RBBIDebugPrintf(" <DICT> ");} |
| 513 |
|
| 514 |
for (i=0; i<rlRange->fIncludesSets->size(); i++) { |
| 515 |
RBBINode *usetNode = (RBBINode *)rlRange->fIncludesSets->elementAt(i); |
| 516 |
UnicodeString setName = UNICODE_STRING("anon", 4); |
| 517 |
RBBINode *setRef = usetNode->fParent; |
| 518 |
if (setRef != nullptr) { |
| 519 |
RBBINode *varRef = setRef->fParent; |
| 520 |
if (varRef != nullptr && varRef->fType == RBBINode::varRef) { |
| 521 |
setName = varRef->fText; |
| 522 |
} |
| 523 |
} |
| 524 |
RBBI_DEBUG_printUnicodeString(setName); RBBIDebugPrintf(" "); |
| 525 |
} |
| 526 |
|
| 527 |
i = 0; |
| 528 |
for (RangeDescriptor *tRange = rlRange; tRange != nullptr; tRange = tRange->fNext) { |
| 529 |
if (tRange->fNum == rlRange->fNum) { |
| 530 |
if (i++ % 5 == 0) { |
| 531 |
RBBIDebugPrintf("\n "); |
| 532 |
} |
| 533 |
RBBIDebugPrintf(" %05x-%05x", tRange->fStartChar, tRange->fEndChar); |
| 534 |
} |
| 535 |
} |
| 536 |
RBBIDebugPrintf("\n"); |
| 537 |
} |
| 538 |
} |
| 539 |
RBBIDebugPrintf("\n"); |
| 540 |
} |
| 541 |
#endif |
| 542 |
|
| 543 |
|
| 544 |
//------------------------------------------------------------------------ |
| 545 |
// |
| 546 |
// printSets A debugging function. |
| 547 |
// dump out all of the set definitions. |
| 548 |
// |
| 549 |
//------------------------------------------------------------------------ |
| 550 |
#ifdef RBBI_DEBUG |
| 551 |
void RBBISetBuilder::printSets() { |
| 552 |
int i; |
| 553 |
|
| 554 |
RBBIDebugPrintf("\n\nUnicode Sets List\n------------------\n"); |
| 555 |
for (i=0; ; i++) { |
| 556 |
RBBINode *usetNode; |
| 557 |
RBBINode *setRef; |
| 558 |
RBBINode *varRef; |
| 559 |
UnicodeString setName; |
| 560 |
|
| 561 |
usetNode = (RBBINode *)fRB->fUSetNodes->elementAt(i); |
| 562 |
if (usetNode == nullptr) { |
| 563 |
break; |
| 564 |
} |
| 565 |
|
| 566 |
RBBIDebugPrintf("%3d ", i); |
| 567 |
setName = UNICODE_STRING("anonymous", 9); |
| 568 |
setRef = usetNode->fParent; |
| 569 |
if (setRef != nullptr) { |
| 570 |
varRef = setRef->fParent; |
| 571 |
if (varRef != nullptr && varRef->fType == RBBINode::varRef) { |
| 572 |
setName = varRef->fText; |
| 573 |
} |
| 574 |
} |
| 575 |
RBBI_DEBUG_printUnicodeString(setName); |
| 576 |
RBBIDebugPrintf(" "); |
| 577 |
RBBI_DEBUG_printUnicodeString(usetNode->fText); |
| 578 |
RBBIDebugPrintf("\n"); |
| 579 |
if (usetNode->fLeftChild != nullptr) { |
| 580 |
RBBINode::printTree(usetNode->fLeftChild, true); |
| 581 |
} |
| 582 |
} |
| 583 |
RBBIDebugPrintf("\n"); |
| 584 |
} |
| 585 |
#endif |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
//------------------------------------------------------------------------------------- |
| 590 |
// |
| 591 |
// RangeDescriptor copy constructor |
| 592 |
// |
| 593 |
//------------------------------------------------------------------------------------- |
| 594 |
|
| 595 |
RangeDescriptor::RangeDescriptor(const RangeDescriptor &other, UErrorCode &status) : |
| 596 |
fStartChar(other.fStartChar), fEndChar {other.fEndChar}, fNum {other.fNum}, |
| 597 |
fIncludesDict{other.fIncludesDict}, fFirstInGroup{other.fFirstInGroup} { |
| 598 |
|
| 599 |
if (U_FAILURE(status)) { |
| 600 |
return; |
| 601 |
} |
| 602 |
fIncludesSets = new UVector(status); |
| 603 |
if (this->fIncludesSets == nullptr) { |
| 604 |
status = U_MEMORY_ALLOCATION_ERROR; |
| 605 |
} |
| 606 |
if (U_FAILURE(status)) { |
| 607 |
return; |
| 608 |
} |
| 609 |
|
| 610 |
for (int32_t i=0; i<other.fIncludesSets->size(); i++) { |
| 611 |
this->fIncludesSets->addElement(other.fIncludesSets->elementAt(i), status); |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
//------------------------------------------------------------------------------------- |
| 617 |
// |
| 618 |
// RangeDesriptor default constructor |
| 619 |
// |
| 620 |
//------------------------------------------------------------------------------------- |
| 621 |
RangeDescriptor::RangeDescriptor(UErrorCode &status) { |
| 622 |
if (U_FAILURE(status)) { |
| 623 |
return; |
| 624 |
} |
| 625 |
fIncludesSets = new UVector(status); |
| 626 |
if (fIncludesSets == nullptr) { |
| 627 |
status = U_MEMORY_ALLOCATION_ERROR; |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
//------------------------------------------------------------------------------------- |
| 633 |
// |
| 634 |
// RangeDesriptor Destructor |
| 635 |
// |
| 636 |
//------------------------------------------------------------------------------------- |
| 637 |
RangeDescriptor::~RangeDescriptor() { |
| 638 |
delete fIncludesSets; |
| 639 |
fIncludesSets = nullptr; |
| 640 |
} |
| 641 |
|
| 642 |
//------------------------------------------------------------------------------------- |
| 643 |
// |
| 644 |
// RangeDesriptor::split() |
| 645 |
// |
| 646 |
//------------------------------------------------------------------------------------- |
| 647 |
void RangeDescriptor::split(UChar32 where, UErrorCode &status) { |
| 648 |
U_ASSERT(where>fStartChar && where<=fEndChar); |
| 649 |
RangeDescriptor *nr = new RangeDescriptor(*this, status); |
| 650 |
if(nr == nullptr) { |
| 651 |
status = U_MEMORY_ALLOCATION_ERROR; |
| 652 |
return; |
| 653 |
} |
| 654 |
if (U_FAILURE(status)) { |
| 655 |
delete nr; |
| 656 |
return; |
| 657 |
} |
| 658 |
// RangeDescriptor copy constructor copies all fields. |
| 659 |
// Only need to update those that are different after the split. |
| 660 |
nr->fStartChar = where; |
| 661 |
this->fEndChar = where-1; |
| 662 |
nr->fNext = this->fNext; |
| 663 |
this->fNext = nr; |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
//------------------------------------------------------------------------------------- |
| 668 |
// |
| 669 |
// RangeDescriptor::isDictionaryRange |
| 670 |
// |
| 671 |
// Test whether this range includes characters from |
| 672 |
// the original Unicode Set named "dictionary". |
| 673 |
// |
| 674 |
// This function looks through the Unicode Sets that |
| 675 |
// the range includes, checking for one named "dictionary" |
| 676 |
// |
| 677 |
// TODO: a faster way would be to find the set node for |
| 678 |
// "dictionary" just once, rather than looking it |
| 679 |
// up by name every time. |
| 680 |
// |
| 681 |
//------------------------------------------------------------------------------------- |
| 682 |
bool RangeDescriptor::isDictionaryRange() { |
| 683 |
static const char16_t *dictionary = u"dictionary"; |
| 684 |
for (int32_t i=0; i<fIncludesSets->size(); i++) { |
| 685 |
RBBINode* usetNode = static_cast<RBBINode*>(fIncludesSets->elementAt(i)); |
| 686 |
RBBINode *setRef = usetNode->fParent; |
| 687 |
if (setRef != nullptr) { |
| 688 |
RBBINode *varRef = setRef->fParent; |
| 689 |
if (varRef && varRef->fType == RBBINode::varRef) { |
| 690 |
const UnicodeString *setName = &varRef->fText; |
| 691 |
if (setName->compare(dictionary, -1) == 0) { |
| 692 |
return true; |
| 693 |
} |
| 694 |
} |
| 695 |
} |
| 696 |
} |
| 697 |
return false; |
| 698 |
} |
| 699 |
|
| 700 |
U_NAMESPACE_END |
| 701 |
|
| 702 |
#endif /* #if !UCONFIG_NO_BREAK_ITERATION */ |