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-2016 International Business Machines Corporation * 6 * and others. All rights reserved. * 7 *************************************************************************** 8 9 ********************************************************************** 10 * Date Name Description 11 * 10/22/99 alan Creation. 12 * 11/11/99 rgillam Complete port from Java. 13 ********************************************************************** 14 */ 15 16 #ifndef RBBI_H 17 #define RBBI_H 18 19 #include "unicode/utypes.h" 20 21 /** 22 * \file 23 * \brief C++ API: Rule Based Break Iterator 24 */ 25 26 #if !UCONFIG_NO_BREAK_ITERATION 27 28 #include "unicode/brkiter.h" 29 #include "unicode/udata.h" 30 #include "unicode/parseerr.h" 31 #include "unicode/schriter.h" 32 #include "unicode/uchriter.h" 33 34 U_NAMESPACE_BEGIN 35 36 /** @internal */ 37 class LanguageBreakEngine; 38 struct RBBIDataHeader; 39 class RBBIDataWrapper; 40 class UnhandledEngine; 41 class UStack; 42 43 /** 44 * 45 * A subclass of BreakIterator whose behavior is specified using a list of rules. 46 * <p>Instances of this class are most commonly created by the factory methods of 47 * BreakIterator::createWordInstance(), BreakIterator::createLineInstance(), etc., 48 * and then used via the abstract API in class BreakIterator</p> 49 * 50 * <p>See the ICU User Guide for information on Break Iterator Rules.</p> 51 * 52 * <p>This class is not intended to be subclassed.</p> 53 */ 54 class U_COMMON_API RuleBasedBreakIterator /*U_FINAL*/ : public BreakIterator { 55 56 private: 57 /** 58 * The UText through which this BreakIterator accesses the text 59 * @internal 60 */ 61 UText *fText; 62 63 /** 64 * A character iterator that refers to the same text as the UText, above. 65 * Only included for compatibility with old API, which was based on CharacterIterators. 66 * Value may be adopted from outside, or one of fSCharIter or fDCharIter, below. 67 */ 68 CharacterIterator *fCharIter; 69 70 /** 71 * When the input text is provided by a UnicodeString, this will point to 72 * a characterIterator that wraps that data. Needed only for the 73 * implementation of getText(), a backwards compatibility issue. 74 */ 75 StringCharacterIterator *fSCharIter; 76 77 /** 78 * When the input text is provided by a UText, this 79 * dummy CharacterIterator over an empty string will 80 * be returned from getText() 81 */ 82 UCharCharacterIterator *fDCharIter; 83 84 /** 85 * The rule data for this BreakIterator instance 86 * @internal 87 */ 88 RBBIDataWrapper *fData; 89 90 /** 91 * The iteration state - current position, rule status for the current position, 92 * and whether the iterator ran off the end, yielding UBRK_DONE. 93 * Current position is pinned to be 0 < position <= text.length. 94 * Current position is always set to a boundary. 95 * @internal 96 */ 97 /** 98 * The current position of the iterator. Pinned, 0 < fPosition <= text.length. 99 * Never has the value UBRK_DONE (-1). 100 */ 101 int32_t fPosition; 102 103 /** 104 * TODO: 105 */ 106 int32_t fRuleStatusIndex; 107 108 /** 109 * True when iteration has run off the end, and iterator functions should return UBRK_DONE. 110 */ 111 UBool fDone; 112 113 /** 114 * Cache of previously determined boundary positions. 115 */ 116 public: // TODO: debug, return to private. 117 class BreakCache; 118 BreakCache *fBreakCache; 119 private: 120 /** 121 * Counter for the number of characters encountered with the "dictionary" 122 * flag set. 123 * @internal 124 */ 125 uint32_t fDictionaryCharCount; 126 127 /** 128 * Cache of boundary positions within a region of text that has been 129 * sub-divided by dictionary based breaking. 130 */ 131 class DictionaryCache; 132 DictionaryCache *fDictionaryCache; 133 134 /** 135 * 136 * If present, UStack of LanguageBreakEngine objects that might handle 137 * dictionary characters. Searched from top to bottom to find an object to 138 * handle a given character. 139 * @internal 140 */ 141 UStack *fLanguageBreakEngines; 142 143 /** 144 * 145 * If present, the special LanguageBreakEngine used for handling 146 * characters that are in the dictionary set, but not handled by any 147 * LangugageBreakEngine. 148 * @internal 149 */ 150 UnhandledEngine *fUnhandledBreakEngine; 151 152 /** 153 * 154 * The type of the break iterator, or -1 if it has not been set. 155 * @internal 156 */ 157 int32_t fBreakType; 158 159 //======================================================================= 160 // constructors 161 //======================================================================= 162 163 /** 164 * Constructor from a flattened set of RBBI data in malloced memory. 165 * RulesBasedBreakIterators built from a custom set of rules 166 * are created via this constructor; the rules are compiled 167 * into memory, then the break iterator is constructed here. 168 * 169 * The break iterator adopts the memory, and will 170 * free it when done. 171 * @internal 172 */ 173 RuleBasedBreakIterator(RBBIDataHeader* data, UErrorCode &status); 174 175 /** @internal */ 176 friend class RBBIRuleBuilder; 177 /** @internal */ 178 friend class BreakIterator; 179 180 public: 181 182 /** Default constructor. Creates an empty shell of an iterator, with no 183 * rules or text to iterate over. Object can subsequently be assigned to. 184 * @stable ICU 2.2 185 */ 186 RuleBasedBreakIterator(); 187 188 /** 189 * Copy constructor. Will produce a break iterator with the same behavior, 190 * and which iterates over the same text, as the one passed in. 191 * @param that The RuleBasedBreakIterator passed to be copied 192 * @stable ICU 2.0 193 */ 194 RuleBasedBreakIterator(const RuleBasedBreakIterator& that); 195 196 /** 197 * Construct a RuleBasedBreakIterator from a set of rules supplied as a string. 198 * @param rules The break rules to be used. 199 * @param parseError In the event of a syntax error in the rules, provides the location 200 * within the rules of the problem. 201 * @param status Information on any errors encountered. 202 * @stable ICU 2.2 203 */ 204 RuleBasedBreakIterator( const UnicodeString &rules, 205 UParseError &parseError, 206 UErrorCode &status); 207 208 /** 209 * Contruct a RuleBasedBreakIterator from a set of precompiled binary rules. 210 * Binary rules are obtained from RulesBasedBreakIterator::getBinaryRules(). 211 * Construction of a break iterator in this way is substantially faster than 212 * constuction from source rules. 213 * 214 * Ownership of the storage containing the compiled rules remains with the 215 * caller of this function. The compiled rules must not be modified or 216 * deleted during the life of the break iterator. 217 * 218 * The compiled rules are not compatible across different major versions of ICU. 219 * The compiled rules are comaptible only between machines with the same 220 * byte ordering (little or big endian) and the same base character set family 221 * (ASCII or EBCDIC). 222 * 223 * @see #getBinaryRules 224 * @param compiledRules A pointer to the compiled break rules to be used. 225 * @param ruleLength The length of the compiled break rules, in bytes. This 226 * corresponds to the length value produced by getBinaryRules(). 227 * @param status Information on any errors encountered, including invalid 228 * binary rules. 229 * @stable ICU 4.8 230 */ 231 RuleBasedBreakIterator(const uint8_t *compiledRules, 232 uint32_t ruleLength, 233 UErrorCode &status); 234 235 /** 236 * This constructor uses the udata interface to create a BreakIterator 237 * whose internal tables live in a memory-mapped file. "image" is an 238 * ICU UDataMemory handle for the pre-compiled break iterator tables. 239 * @param image handle to the memory image for the break iterator data. 240 * Ownership of the UDataMemory handle passes to the Break Iterator, 241 * which will be responsible for closing it when it is no longer needed. 242 * @param status Information on any errors encountered. 243 * @see udata_open 244 * @see #getBinaryRules 245 * @stable ICU 2.8 246 */ 247 RuleBasedBreakIterator(UDataMemory* image, UErrorCode &status); 248 249 /** 250 * Destructor 251 * @stable ICU 2.0 252 */ 253 virtual ~RuleBasedBreakIterator(); 254 255 /** 256 * Assignment operator. Sets this iterator to have the same behavior, 257 * and iterate over the same text, as the one passed in. 258 * @param that The RuleBasedBreakItertor passed in 259 * @return the newly created RuleBasedBreakIterator 260 * @stable ICU 2.0 261 */ 262 RuleBasedBreakIterator& operator=(const RuleBasedBreakIterator& that); 263 264 /** 265 * Equality operator. Returns TRUE if both BreakIterators are of the 266 * same class, have the same behavior, and iterate over the same text. 267 * @param that The BreakIterator to be compared for equality 268 * @return TRUE if both BreakIterators are of the 269 * same class, have the same behavior, and iterate over the same text. 270 * @stable ICU 2.0 271 */ 272 virtual UBool operator==(const BreakIterator& that) const; 273 274 /** 275 * Not-equal operator. If operator== returns TRUE, this returns FALSE, 276 * and vice versa. 277 * @param that The BreakIterator to be compared for inequality 278 * @return TRUE if both BreakIterators are not same. 279 * @stable ICU 2.0 280 */ 281 UBool operator!=(const BreakIterator& that) const; 282 283 /** 284 * Returns a newly-constructed RuleBasedBreakIterator with the same 285 * behavior, and iterating over the same text, as this one. 286 * Differs from the copy constructor in that it is polymorphic, and 287 * will correctly clone (copy) a derived class. 288 * clone() is thread safe. Multiple threads may simultaeneously 289 * clone the same source break iterator. 290 * @return a newly-constructed RuleBasedBreakIterator 291 * @stable ICU 2.0 292 */ 293 virtual BreakIterator* clone() const; 294 295 /** 296 * Compute a hash code for this BreakIterator 297 * @return A hash code 298 * @stable ICU 2.0 299 */ 300 virtual int32_t hashCode(void) const; 301 302 /** 303 * Returns the description used to create this iterator 304 * @return the description used to create this iterator 305 * @stable ICU 2.0 306 */ 307 virtual const UnicodeString& getRules(void) const; 308 309 //======================================================================= 310 // BreakIterator overrides 311 //======================================================================= 312 313 /** 314 * <p> 315 * Return a CharacterIterator over the text being analyzed. 316 * The returned character iterator is owned by the break iterator, and must 317 * not be deleted by the caller. Repeated calls to this function may 318 * return the same CharacterIterator. 319 * </p> 320 * <p> 321 * The returned character iterator must not be used concurrently with 322 * the break iterator. If concurrent operation is needed, clone the 323 * returned character iterator first and operate on the clone. 324 * </p> 325 * <p> 326 * When the break iterator is operating on text supplied via a UText, 327 * this function will fail. Lacking any way to signal failures, it 328 * returns an CharacterIterator containing no text. 329 * The function getUText() provides similar functionality, 330 * is reliable, and is more efficient. 331 * </p> 332 * 333 * TODO: deprecate this function? 334 * 335 * @return An iterator over the text being analyzed. 336 * @stable ICU 2.0 337 */ 338 virtual CharacterIterator& getText(void) const; 339 340 341 /** 342 * Get a UText for the text being analyzed. 343 * The returned UText is a shallow clone of the UText used internally 344 * by the break iterator implementation. It can safely be used to 345 * access the text without impacting any break iterator operations, 346 * but the underlying text itself must not be altered. 347 * 348 * @param fillIn A UText to be filled in. If NULL, a new UText will be 349 * allocated to hold the result. 350 * @param status receives any error codes. 351 * @return The current UText for this break iterator. If an input 352 * UText was provided, it will always be returned. 353 * @stable ICU 3.4 354 */ 355 virtual UText *getUText(UText *fillIn, UErrorCode &status) const; 356 357 /** 358 * Set the iterator to analyze a new piece of text. This function resets 359 * the current iteration position to the beginning of the text. 360 * @param newText An iterator over the text to analyze. The BreakIterator 361 * takes ownership of the character iterator. The caller MUST NOT delete it! 362 * @stable ICU 2.0 363 */ 364 virtual void adoptText(CharacterIterator* newText); 365 366 /** 367 * Set the iterator to analyze a new piece of text. This function resets 368 * the current iteration position to the beginning of the text. 369 * 370 * The BreakIterator will retain a reference to the supplied string. 371 * The caller must not modify or delete the text while the BreakIterator 372 * retains the reference. 373 * 374 * @param newText The text to analyze. 375 * @stable ICU 2.0 376 */ 377 virtual void setText(const UnicodeString& newText); 378 379 /** 380 * Reset the break iterator to operate over the text represented by 381 * the UText. The iterator position is reset to the start. 382 * 383 * This function makes a shallow clone of the supplied UText. This means 384 * that the caller is free to immediately close or otherwise reuse the 385 * Utext that was passed as a parameter, but that the underlying text itself 386 * must not be altered while being referenced by the break iterator. 387 * 388 * @param text The UText used to change the text. 389 * @param status Receives any error codes. 390 * @stable ICU 3.4 391 */ 392 virtual void setText(UText *text, UErrorCode &status); 393 394 /** 395 * Sets the current iteration position to the beginning of the text, position zero. 396 * @return The offset of the beginning of the text, zero. 397 * @stable ICU 2.0 398 */ 399 virtual int32_t first(void); 400 401 /** 402 * Sets the current iteration position to the end of the text. 403 * @return The text's past-the-end offset. 404 * @stable ICU 2.0 405 */ 406 virtual int32_t last(void); 407 408 /** 409 * Advances the iterator either forward or backward the specified number of steps. 410 * Negative values move backward, and positive values move forward. This is 411 * equivalent to repeatedly calling next() or previous(). 412 * @param n The number of steps to move. The sign indicates the direction 413 * (negative is backwards, and positive is forwards). 414 * @return The character offset of the boundary position n boundaries away from 415 * the current one. 416 * @stable ICU 2.0 417 */ 418 virtual int32_t next(int32_t n); 419 420 /** 421 * Advances the iterator to the next boundary position. 422 * @return The position of the first boundary after this one. 423 * @stable ICU 2.0 424 */ 425 virtual int32_t next(void); 426 427 /** 428 * Moves the iterator backwards, to the last boundary preceding this one. 429 * @return The position of the last boundary position preceding this one. 430 * @stable ICU 2.0 431 */ 432 virtual int32_t previous(void); 433 434 /** 435 * Sets the iterator to refer to the first boundary position following 436 * the specified position. 437 * @param offset The position from which to begin searching for a break position. 438 * @return The position of the first break after the current position. 439 * @stable ICU 2.0 440 */ 441 virtual int32_t following(int32_t offset); 442 443 /** 444 * Sets the iterator to refer to the last boundary position before the 445 * specified position. 446 * @param offset The position to begin searching for a break from. 447 * @return The position of the last boundary before the starting position. 448 * @stable ICU 2.0 449 */ 450 virtual int32_t preceding(int32_t offset); 451 452 /** 453 * Returns true if the specfied position is a boundary position. As a side 454 * effect, leaves the iterator pointing to the first boundary position at 455 * or after "offset". 456 * @param offset the offset to check. 457 * @return True if "offset" is a boundary position. 458 * @stable ICU 2.0 459 */ 460 virtual UBool isBoundary(int32_t offset); 461 462 /** 463 * Returns the current iteration position. Note that UBRK_DONE is never 464 * returned from this function; if iteration has run to the end of a 465 * string, current() will return the length of the string while 466 * next() will return UBRK_DONE). 467 * @return The current iteration position. 468 * @stable ICU 2.0 469 */ 470 virtual int32_t current(void) const; 471 472 473 /** 474 * Return the status tag from the break rule that determined the most recently 475 * returned break position. For break rules that do not specify a 476 * status, a default value of 0 is returned. If more than one break rule 477 * would cause a boundary to be located at some position in the text, 478 * the numerically largest of the applicable status values is returned. 479 * <p> 480 * Of the standard types of ICU break iterators, only word break and 481 * line break provide status values. The values are defined in 482 * the header file ubrk.h. For Word breaks, the status allows distinguishing between words 483 * that contain alphabetic letters, "words" that appear to be numbers, 484 * punctuation and spaces, words containing ideographic characters, and 485 * more. For Line Break, the status distinguishes between hard (mandatory) breaks 486 * and soft (potential) break positions. 487 * <p> 488 * <code>getRuleStatus()</code> can be called after obtaining a boundary 489 * position from <code>next()</code>, <code>previous()</code>, or 490 * any other break iterator functions that returns a boundary position. 491 * <p> 492 * When creating custom break rules, one is free to define whatever 493 * status values may be convenient for the application. 494 * <p> 495 * Note: this function is not thread safe. It should not have been 496 * declared const, and the const remains only for compatibility 497 * reasons. (The function is logically const, but not bit-wise const). 498 * TODO: check this. Probably thread safe now. 499 * <p> 500 * @return the status from the break rule that determined the most recently 501 * returned break position. 502 * 503 * @see UWordBreak 504 * @stable ICU 2.2 505 */ 506 virtual int32_t getRuleStatus() const; 507 508 /** 509 * Get the status (tag) values from the break rule(s) that determined the most 510 * recently returned break position. 511 * <p> 512 * The returned status value(s) are stored into an array provided by the caller. 513 * The values are stored in sorted (ascending) order. 514 * If the capacity of the output array is insufficient to hold the data, 515 * the output will be truncated to the available length, and a 516 * U_BUFFER_OVERFLOW_ERROR will be signaled. 517 * 518 * @param fillInVec an array to be filled in with the status values. 519 * @param capacity the length of the supplied vector. A length of zero causes 520 * the function to return the number of status values, in the 521 * normal way, without attemtping to store any values. 522 * @param status receives error codes. 523 * @return The number of rule status values from rules that determined 524 * the most recent boundary returned by the break iterator. 525 * In the event of a U_BUFFER_OVERFLOW_ERROR, the return value 526 * is the total number of status values that were available, 527 * not the reduced number that were actually returned. 528 * @see getRuleStatus 529 * @stable ICU 3.0 530 */ 531 virtual int32_t getRuleStatusVec(int32_t *fillInVec, int32_t capacity, UErrorCode &status); 532 533 /** 534 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. 535 * This method is to implement a simple version of RTTI, since not all 536 * C++ compilers support genuine RTTI. Polymorphic operator==() and 537 * clone() methods call this method. 538 * 539 * @return The class ID for this object. All objects of a 540 * given class have the same class ID. Objects of 541 * other classes have different class IDs. 542 * @stable ICU 2.0 543 */ 544 virtual UClassID getDynamicClassID(void) const; 545 546 /** 547 * Returns the class ID for this class. This is useful only for 548 * comparing to a return value from getDynamicClassID(). For example: 549 * 550 * Base* polymorphic_pointer = createPolymorphicObject(); 551 * if (polymorphic_pointer->getDynamicClassID() == 552 * Derived::getStaticClassID()) ... 553 * 554 * @return The class ID for all objects of this class. 555 * @stable ICU 2.0 556 */ 557 static UClassID U_EXPORT2 getStaticClassID(void); 558 559 /** 560 * Deprecated functionality. Use clone() instead. 561 * 562 * Create a clone (copy) of this break iterator in memory provided 563 * by the caller. The idea is to increase performance by avoiding 564 * a storage allocation. Use of this functoin is NOT RECOMMENDED. 565 * Performance gains are minimal, and correct buffer management is 566 * tricky. Use clone() instead. 567 * 568 * @param stackBuffer The pointer to the memory into which the cloned object 569 * should be placed. If NULL, allocate heap memory 570 * for the cloned object. 571 * @param BufferSize The size of the buffer. If zero, return the required 572 * buffer size, but do not clone the object. If the 573 * size was too small (but not zero), allocate heap 574 * storage for the cloned object. 575 * 576 * @param status Error status. U_SAFECLONE_ALLOCATED_WARNING will be 577 * returned if the the provided buffer was too small, and 578 * the clone was therefore put on the heap. 579 * 580 * @return Pointer to the clone object. This may differ from the stackBuffer 581 * address if the byte alignment of the stack buffer was not suitable 582 * or if the stackBuffer was too small to hold the clone. 583 * @deprecated ICU 52. Use clone() instead. 584 */ 585 virtual BreakIterator * createBufferClone(void *stackBuffer, 586 int32_t &BufferSize, 587 UErrorCode &status); 588 589 590 /** 591 * Return the binary form of compiled break rules, 592 * which can then be used to create a new break iterator at some 593 * time in the future. Creating a break iterator from pre-compiled rules 594 * is much faster than building one from the source form of the 595 * break rules. 596 * 597 * The binary data can only be used with the same version of ICU 598 * and on the same platform type (processor endian-ness) 599 * 600 * @param length Returns the length of the binary data. (Out paramter.) 601 * 602 * @return A pointer to the binary (compiled) rule data. The storage 603 * belongs to the RulesBasedBreakIterator object, not the 604 * caller, and must not be modified or deleted. 605 * @stable ICU 4.8 606 */ 607 virtual const uint8_t *getBinaryRules(uint32_t &length); 608 609 /** 610 * Set the subject text string upon which the break iterator is operating 611 * without changing any other aspect of the matching state. 612 * The new and previous text strings must have the same content. 613 * 614 * This function is intended for use in environments where ICU is operating on 615 * strings that may move around in memory. It provides a mechanism for notifying 616 * ICU that the string has been relocated, and providing a new UText to access the 617 * string in its new position. 618 * 619 * Note that the break iterator implementation never copies the underlying text 620 * of a string being processed, but always operates directly on the original text 621 * provided by the user. Refreshing simply drops the references to the old text 622 * and replaces them with references to the new. 623 * 624 * Caution: this function is normally used only by very specialized, 625 * system-level code. One example use case is with garbage collection that moves 626 * the text in memory. 627 * 628 * @param input The new (moved) text string. 629 * @param status Receives errors detected by this function. 630 * @return *this 631 * 632 * @stable ICU 49 633 */ 634 virtual RuleBasedBreakIterator &refreshInputText(UText *input, UErrorCode &status); 635 636 637 private: 638 //======================================================================= 639 // implementation 640 //======================================================================= 641 /** 642 * Dumps caches and performs other actions associated with a complete change 643 * in text or iteration position. 644 * @internal 645 */ 646 void reset(void); 647 648 /** 649 * Set the type of the break iterator. 650 * @internal 651 */ 652 void setBreakType(int32_t type); 653 654 /** 655 * Common initialization function, used by constructors and bufferClone. 656 * @internal 657 */ 658 void init(UErrorCode &status); 659 660 /** 661 * Iterate backwards from an arbitrary position in the input text using the Safe Reverse rules. 662 * This locates a "Safe Position" from which the forward break rules 663 * will operate correctly. A Safe Position is not necessarily a boundary itself. 664 * 665 * @param fromPosition the position in the input text to begin the iteration. 666 * @internal 667 */ 668 int32_t handlePrevious(int32_t fromPosition); 669 670 /** 671 * Find a rule-based boundary by running the state machine. 672 * Input 673 * fPosition, the position in the text to begin from. 674 * Output 675 * fPosition: the boundary following the starting position. 676 * fDictionaryCharCount the number of dictionary characters encountered. 677 * If > 0, the segment will be further subdivided 678 * fRuleStatusIndex Info from the state table indicating which rules caused the boundary. 679 * 680 * @internal 681 */ 682 int32_t handleNext(); 683 684 685 /** 686 * This function returns the appropriate LanguageBreakEngine for a 687 * given character c. 688 * @param c A character in the dictionary set 689 * @internal 690 */ 691 const LanguageBreakEngine *getLanguageBreakEngine(UChar32 c); 692 693 public: 694 #ifndef U_HIDE_INTERNAL_API 695 /** 696 * Debugging function only. 697 * @internal 698 */ 699 void dumpCache(); 700 #endif /* U_HIDE_INTERNAL_API */ 701 }; 702 703 //------------------------------------------------------------------------------ 704 // 705 // Inline Functions Definitions ... 706 // 707 //------------------------------------------------------------------------------ 708 709 inline UBool RuleBasedBreakIterator::operator!=(const BreakIterator& that) const { 710 return !operator==(that); 711 } 712 713 U_NAMESPACE_END 714 715 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */ 716 717 #endif 718