1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ***************************************************************************
5 * Copyright (C) 1999-2016 International Business Machines Corporation
6 * and others. All rights reserved.
7 ***************************************************************************
10 // file: rbbi.cpp Contains the implementation of the rule based break iterator
11 // runtime engine and the API implementation for
12 // class RuleBasedBreakIterator
15 #include "utypeinfo.h" // for 'typeid' to work
17 #include "unicode/utypes.h"
19 #if !UCONFIG_NO_BREAK_ITERATION
21 #include "unicode/rbbi.h"
22 #include "unicode/schriter.h"
23 #include "unicode/uchriter.h"
24 #include "unicode/uclean.h"
25 #include "unicode/udata.h"
32 #include "rbbi_cache.h"
38 // if U_LOCAL_SERVICE_HOOK is defined, then localsvc.cpp is expected to be included.
39 #if U_LOCAL_SERVICE_HOOK
47 static UBool gTrace
= FALSE
;
52 // The state number of the starting state
53 constexpr int32_t START_STATE
= 1;
55 // The state-transition value indicating "stop"
56 constexpr int32_t STOP_STATE
= 0;
59 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RuleBasedBreakIterator
)
62 //=======================================================================
64 //=======================================================================
67 * Constructs a RuleBasedBreakIterator that uses the already-created
68 * tables object that is passed in as a parameter.
70 RuleBasedBreakIterator::RuleBasedBreakIterator(RBBIDataHeader
* data
, UErrorCode
&status
)
71 : fSCharIter(UnicodeString())
74 fData
= new RBBIDataWrapper(data
, status
); // status checked in constructor
75 if (U_FAILURE(status
)) {return;}
77 status
= U_MEMORY_ALLOCATION_ERROR
;
83 // Construct from precompiled binary rules (tables). This constructor is public API,
84 // taking the rules as a (const uint8_t *) to match the type produced by getBinaryRules().
86 RuleBasedBreakIterator::RuleBasedBreakIterator(const uint8_t *compiledRules
,
89 : fSCharIter(UnicodeString())
92 if (U_FAILURE(status
)) {
95 if (compiledRules
== NULL
|| ruleLength
< sizeof(RBBIDataHeader
)) {
96 status
= U_ILLEGAL_ARGUMENT_ERROR
;
99 const RBBIDataHeader
*data
= (const RBBIDataHeader
*)compiledRules
;
100 if (data
->fLength
> ruleLength
) {
101 status
= U_ILLEGAL_ARGUMENT_ERROR
;
104 fData
= new RBBIDataWrapper(data
, RBBIDataWrapper::kDontAdopt
, status
);
105 if (U_FAILURE(status
)) {return;}
107 status
= U_MEMORY_ALLOCATION_ERROR
;
113 //-------------------------------------------------------------------------------
115 // Constructor from a UDataMemory handle to precompiled break rules
116 // stored in an ICU data file.
118 //-------------------------------------------------------------------------------
119 RuleBasedBreakIterator::RuleBasedBreakIterator(UDataMemory
* udm
, UErrorCode
&status
)
120 : fSCharIter(UnicodeString())
123 fData
= new RBBIDataWrapper(udm
, status
); // status checked in constructor
124 if (U_FAILURE(status
)) {return;}
126 status
= U_MEMORY_ALLOCATION_ERROR
;
133 //-------------------------------------------------------------------------------
135 // Constructor from a set of rules supplied as a string.
137 //-------------------------------------------------------------------------------
138 RuleBasedBreakIterator::RuleBasedBreakIterator( const UnicodeString
&rules
,
139 UParseError
&parseError
,
141 : fSCharIter(UnicodeString())
144 if (U_FAILURE(status
)) {return;}
145 RuleBasedBreakIterator
*bi
= (RuleBasedBreakIterator
*)
146 RBBIRuleBuilder::createRuleBasedBreakIterator(rules
, &parseError
, status
);
147 // Note: This is a bit awkward. The RBBI ruleBuilder has a factory method that
148 // creates and returns a complete RBBI. From here, in a constructor, we
149 // can't just return the object created by the builder factory, hence
150 // the assignment of the factory created object to "this".
151 if (U_SUCCESS(status
)) {
158 //-------------------------------------------------------------------------------
160 // Default Constructor. Create an empty shell that can be set up later.
161 // Used when creating a RuleBasedBreakIterator from a set
163 //-------------------------------------------------------------------------------
164 RuleBasedBreakIterator::RuleBasedBreakIterator()
165 : fSCharIter(UnicodeString())
167 UErrorCode status
= U_ZERO_ERROR
;
172 //-------------------------------------------------------------------------------
174 // Copy constructor. Will produce a break iterator with the same behavior,
175 // and which iterates over the same text, as the one passed in.
177 //-------------------------------------------------------------------------------
178 RuleBasedBreakIterator::RuleBasedBreakIterator(const RuleBasedBreakIterator
& other
)
179 : BreakIterator(other
),
180 fSCharIter(UnicodeString())
182 UErrorCode status
= U_ZERO_ERROR
;
191 RuleBasedBreakIterator::~RuleBasedBreakIterator() {
192 if (fCharIter
!= &fSCharIter
) {
193 // fCharIter was adopted from the outside.
201 fData
->removeReference();
207 delete fDictionaryCache
;
208 fDictionaryCache
= NULL
;
210 delete fLanguageBreakEngines
;
211 fLanguageBreakEngines
= NULL
;
213 delete fUnhandledBreakEngine
;
214 fUnhandledBreakEngine
= NULL
;
216 delete [] fLatin1Cat
;
221 * Assignment operator. Sets this iterator to have the same behavior,
222 * and iterate over the same text, as the one passed in.
224 RuleBasedBreakIterator
&
225 RuleBasedBreakIterator::operator=(const RuleBasedBreakIterator
& that
) {
229 BreakIterator::operator=(that
);
230 fLineWordOpts
= that
.fLineWordOpts
;
232 if (fLanguageBreakEngines
!= NULL
) {
233 delete fLanguageBreakEngines
;
234 fLanguageBreakEngines
= NULL
; // Just rebuild for now
236 // TODO: clone fLanguageBreakEngines from "that"
237 UErrorCode status
= U_ZERO_ERROR
;
238 utext_clone(&fText
, &that
.fText
, FALSE
, TRUE
, &status
);
240 if (fCharIter
!= &fSCharIter
) {
243 fCharIter
= &fSCharIter
;
245 if (that
.fCharIter
!= NULL
&& that
.fCharIter
!= &that
.fSCharIter
) {
246 // This is a little bit tricky - it will intially appear that
247 // this->fCharIter is adopted, even if that->fCharIter was
248 // not adopted. That's ok.
249 fCharIter
= that
.fCharIter
->clone();
251 fSCharIter
= that
.fSCharIter
;
252 if (fCharIter
== NULL
) {
253 fCharIter
= &fSCharIter
;
257 fData
->removeReference();
260 if (that
.fData
!= NULL
) {
261 fData
= that
.fData
->addReference();
264 delete [] fLatin1Cat
;
267 fPosition
= that
.fPosition
;
268 fRuleStatusIndex
= that
.fRuleStatusIndex
;
271 // TODO: both the dictionary and the main cache need to be copied.
272 // Current position could be within a dictionary range. Trying to continue
273 // the iteration without the caches present would go to the rules, with
274 // the assumption that the current position is on a rule boundary.
275 fBreakCache
->reset(fPosition
, fRuleStatusIndex
);
276 fDictionaryCache
->reset();
283 //-----------------------------------------------------------------------------
285 // init() Shared initialization routine. Used by all the constructors.
286 // Initializes all fields, leaving the object in a consistent state.
288 //-----------------------------------------------------------------------------
289 void RuleBasedBreakIterator::init(UErrorCode
&status
) {
294 fRuleStatusIndex
= 0;
296 fDictionaryCharCount
= 0;
297 fLanguageBreakEngines
= NULL
;
298 fUnhandledBreakEngine
= NULL
;
300 fDictionaryCache
= NULL
;
302 // Note: IBM xlC is unable to assign or initialize member fText from UTEXT_INITIALIZER.
303 // fText = UTEXT_INITIALIZER;
304 static const UText initializedUText
= UTEXT_INITIALIZER
;
305 uprv_memcpy(&fText
, &initializedUText
, sizeof(UText
));
307 if (U_FAILURE(status
)) {
311 utext_openUChars(&fText
, NULL
, 0, &status
);
312 fDictionaryCache
= new DictionaryCache(this, status
);
313 fBreakCache
= new BreakCache(this, status
);
314 if (U_SUCCESS(status
) && (fDictionaryCache
== NULL
|| fBreakCache
== NULL
)) {
315 status
= U_MEMORY_ALLOCATION_ERROR
;
319 static UBool debugInitDone
= FALSE
;
320 if (debugInitDone
== FALSE
) {
321 char *debugEnv
= getenv("U_RBBIDEBUG");
322 if (debugEnv
&& uprv_strstr(debugEnv
, "trace")) {
325 debugInitDone
= TRUE
;
331 void RuleBasedBreakIterator::initLatin1Cat(void) {
332 fLatin1Cat
= new uint16_t[256];
333 for (UChar32 c
= 0; c
< 256; ++c
) {
334 fLatin1Cat
[c
] = UTRIE2_GET16(fData
->fTrie
, c
);
338 //-----------------------------------------------------------------------------
340 // clone - Returns a newly-constructed RuleBasedBreakIterator with the same
341 // behavior, and iterating over the same text, as this one.
342 // Virtual function: does the right thing with subclasses.
344 //-----------------------------------------------------------------------------
346 RuleBasedBreakIterator::clone(void) const {
347 return new RuleBasedBreakIterator(*this);
351 * Equality operator. Returns TRUE if both BreakIterators are of the
352 * same class, have the same behavior, and iterate over the same text.
355 RuleBasedBreakIterator::operator==(const BreakIterator
& that
) const {
356 if (typeid(*this) != typeid(that
)) {
363 // The base class BreakIterator carries no state that participates in equality,
364 // and does not implement an equality function that would otherwise be
365 // checked at this point.
367 const RuleBasedBreakIterator
& that2
= (const RuleBasedBreakIterator
&) that
;
368 if (that2
.fLineWordOpts
!= fLineWordOpts
) {
372 if (!utext_equals(&fText
, &that2
.fText
)) {
373 // The two break iterators are operating on different text,
374 // or have a different iteration position.
375 // Note that fText's position is always the same as the break iterator's position.
379 if (!(fPosition
== that2
.fPosition
&&
380 fRuleStatusIndex
== that2
.fRuleStatusIndex
&&
381 fDone
== that2
.fDone
)) {
385 if (that2
.fData
== fData
||
386 (fData
!= NULL
&& that2
.fData
!= NULL
&& *that2
.fData
== *fData
)) {
387 // The two break iterators are using the same rules.
394 * Compute a hash code for this BreakIterator
395 * @return A hash code
398 RuleBasedBreakIterator::hashCode(void) const {
401 hash
= fData
->hashCode();
407 void RuleBasedBreakIterator::setText(UText
*ut
, UErrorCode
&status
) {
408 if (U_FAILURE(status
)) {
411 fBreakCache
->reset();
412 fDictionaryCache
->reset();
413 utext_clone(&fText
, ut
, FALSE
, TRUE
, &status
);
415 // Set up a dummy CharacterIterator to be returned if anyone
416 // calls getText(). With input from UText, there is no reasonable
417 // way to return a characterIterator over the actual input text.
418 // Return one over an empty string instead - this is the closest
419 // we can come to signaling a failure.
420 // (GetText() is obsolete, this failure is sort of OK)
421 fSCharIter
.setText(UnicodeString());
423 if (fCharIter
!= &fSCharIter
) {
424 // existing fCharIter was adopted from the outside. Delete it now.
427 fCharIter
= &fSCharIter
;
433 UText
*RuleBasedBreakIterator::getUText(UText
*fillIn
, UErrorCode
&status
) const {
434 UText
*result
= utext_clone(fillIn
, &fText
, FALSE
, TRUE
, &status
);
439 //=======================================================================
440 // BreakIterator overrides
441 //=======================================================================
444 * Return a CharacterIterator over the text being analyzed.
447 RuleBasedBreakIterator::getText() const {
452 * Set the iterator to analyze a new piece of text. This function resets
453 * the current iteration position to the beginning of the text.
454 * @param newText An iterator over the text to analyze.
457 RuleBasedBreakIterator::adoptText(CharacterIterator
* newText
) {
458 // If we are holding a CharacterIterator adopted from a
459 // previous call to this function, delete it now.
460 if (fCharIter
!= &fSCharIter
) {
465 UErrorCode status
= U_ZERO_ERROR
;
466 fBreakCache
->reset();
467 fDictionaryCache
->reset();
468 if (newText
==NULL
|| newText
->startIndex() != 0) {
469 // startIndex !=0 wants to be an error, but there's no way to report it.
470 // Make the iterator text be an empty string.
471 utext_openUChars(&fText
, NULL
, 0, &status
);
473 utext_openCharacterIterator(&fText
, newText
, &status
);
479 * Set the iterator to analyze a new piece of text. This function resets
480 * the current iteration position to the beginning of the text.
481 * @param newText An iterator over the text to analyze.
484 RuleBasedBreakIterator::setText(const UnicodeString
& newText
) {
485 UErrorCode status
= U_ZERO_ERROR
;
486 fBreakCache
->reset();
487 fDictionaryCache
->reset();
488 utext_openConstUnicodeString(&fText
, &newText
, &status
);
490 // Set up a character iterator on the string.
491 // Needed in case someone calls getText().
492 // Can not, unfortunately, do this lazily on the (probably never)
493 // call to getText(), because getText is const.
494 fSCharIter
.setText(newText
);
496 if (fCharIter
!= &fSCharIter
) {
497 // old fCharIter was adopted from the outside. Delete it.
500 fCharIter
= &fSCharIter
;
507 * Provide a new UText for the input text. Must reference text with contents identical
509 * Intended for use with text data originating in Java (garbage collected) environments
510 * where the data may be moved in memory at arbitrary times.
512 RuleBasedBreakIterator
&RuleBasedBreakIterator::refreshInputText(UText
*input
, UErrorCode
&status
) {
513 if (U_FAILURE(status
)) {
517 status
= U_ILLEGAL_ARGUMENT_ERROR
;
520 int64_t pos
= utext_getNativeIndex(&fText
);
521 // Shallow read-only clone of the new UText into the existing input UText
522 utext_clone(&fText
, input
, FALSE
, TRUE
, &status
);
523 if (U_FAILURE(status
)) {
526 utext_setNativeIndex(&fText
, pos
);
527 if (utext_getNativeIndex(&fText
) != pos
) {
528 // Sanity check. The new input utext is supposed to have the exact same
529 // contents as the old. If we can't set to the same position, it doesn't.
530 // The contents underlying the old utext might be invalid at this point,
531 // so it's not safe to check directly.
532 status
= U_ILLEGAL_ARGUMENT_ERROR
;
539 * Sets the current iteration position to the beginning of the text, position zero.
540 * @return The new iterator position, which is zero.
542 int32_t RuleBasedBreakIterator::first(void) {
543 UErrorCode status
= U_ZERO_ERROR
;
544 if (!fBreakCache
->seek(0)) {
545 fBreakCache
->populateNear(0, status
);
547 fBreakCache
->current();
548 U_ASSERT(fPosition
== 0);
553 * Sets the current iteration position to the end of the text.
554 * @return The text's past-the-end offset.
556 int32_t RuleBasedBreakIterator::last(void) {
557 int32_t endPos
= (int32_t)utext_nativeLength(&fText
);
558 UBool endShouldBeBoundary
= isBoundary(endPos
); // Has side effect of setting iterator position.
559 (void)endShouldBeBoundary
;
560 U_ASSERT(endShouldBeBoundary
);
561 U_ASSERT(fPosition
== endPos
);
566 * Advances the iterator either forward or backward the specified number of steps.
567 * Negative values move backward, and positive values move forward. This is
568 * equivalent to repeatedly calling next() or previous().
569 * @param n The number of steps to move. The sign indicates the direction
570 * (negative is backwards, and positive is forwards).
571 * @return The character offset of the boundary position n boundaries away from
574 int32_t RuleBasedBreakIterator::next(int32_t n
) {
577 for (; n
> 0 && result
!= UBRK_DONE
; --n
) {
581 for (; n
< 0 && result
!= UBRK_DONE
; ++n
) {
591 * Advances the iterator to the next boundary position.
592 * @return The position of the first boundary after this one.
594 int32_t RuleBasedBreakIterator::next(void) {
596 return fDone
? UBRK_DONE
: fPosition
;
600 * Move the iterator backwards, to the boundary preceding the current one.
602 * Starts from the current position within fText.
603 * Starting position need not be on a boundary.
605 * @return The position of the boundary position immediately preceding the starting position.
607 int32_t RuleBasedBreakIterator::previous(void) {
608 UErrorCode status
= U_ZERO_ERROR
;
609 fBreakCache
->previous(status
);
610 return fDone
? UBRK_DONE
: fPosition
;
614 * Sets the iterator to refer to the first boundary position following
615 * the specified position.
616 * @param startPos The position from which to begin searching for a break position.
617 * @return The position of the first break after the current position.
619 int32_t RuleBasedBreakIterator::following(int32_t startPos
) {
620 // if the supplied position is before the beginning, return the
621 // text's starting offset
626 // Move requested offset to a code point start. It might be on a trail surrogate,
627 // or on a trail byte if the input is UTF-8. Or it may be beyond the end of the text.
628 utext_setNativeIndex(&fText
, startPos
);
629 startPos
= (int32_t)utext_getNativeIndex(&fText
);
631 UErrorCode status
= U_ZERO_ERROR
;
632 fBreakCache
->following(startPos
, status
);
633 return fDone
? UBRK_DONE
: fPosition
;
637 * Sets the iterator to refer to the last boundary position before the
638 * specified position.
639 * @param offset The position to begin searching for a break from.
640 * @return The position of the last boundary before the starting position.
642 int32_t RuleBasedBreakIterator::preceding(int32_t offset
) {
643 if (offset
> utext_nativeLength(&fText
)) {
647 // Move requested offset to a code point start. It might be on a trail surrogate,
648 // or on a trail byte if the input is UTF-8.
650 utext_setNativeIndex(&fText
, offset
);
651 int32_t adjustedOffset
= utext_getNativeIndex(&fText
);
653 UErrorCode status
= U_ZERO_ERROR
;
654 fBreakCache
->preceding(adjustedOffset
, status
);
655 return fDone
? UBRK_DONE
: fPosition
;
659 * Returns true if the specfied position is a boundary position. As a side
660 * effect, leaves the iterator pointing to the first boundary position at
663 * @param offset the offset to check.
664 * @return True if "offset" is a boundary position.
666 UBool
RuleBasedBreakIterator::isBoundary(int32_t offset
) {
667 // out-of-range indexes are never boundary positions
669 first(); // For side effects on current position, tag values.
673 // Adjust offset to be on a code point boundary and not beyond the end of the text.
674 // Note that isBoundary() is always false for offsets that are not on code point boundaries.
675 // But we still need the side effect of leaving iteration at the following boundary.
677 utext_setNativeIndex(&fText
, offset
);
678 int32_t adjustedOffset
= utext_getNativeIndex(&fText
);
681 UErrorCode status
= U_ZERO_ERROR
;
682 if (fBreakCache
->seek(adjustedOffset
) || fBreakCache
->populateNear(adjustedOffset
, status
)) {
683 result
= (fBreakCache
->current() == offset
);
686 if (result
&& adjustedOffset
< offset
&& utext_char32At(&fText
, offset
) == U_SENTINEL
) {
687 // Original offset is beyond the end of the text. Return FALSE, it's not a boundary,
688 // but the iteration position remains set to the end of the text, which is a boundary.
692 // Not on a boundary. isBoundary() must leave iterator on the following boundary.
693 // Cache->seek(), above, left us on the preceding boundary, so advance one.
701 * Returns the current iteration position.
702 * @return The current iteration position.
704 int32_t RuleBasedBreakIterator::current(void) const {
709 //=======================================================================
711 //=======================================================================
714 // RBBIRunMode - the state machine runs an extra iteration at the beginning and end
715 // of user text. A variable with this enum type keeps track of where we
716 // are. The state machine only fetches user input while in the RUN mode.
719 RBBI_START
, // state machine processing is before first char of input
720 RBBI_RUN
, // state machine processing is in the user text
721 RBBI_END
// state machine processing is after end of user text.
725 // Map from look-ahead break states (corresponds to rules) to boundary positions.
726 // Allows multiple lookahead break rules to be in flight at the same time.
728 // This is a temporary approach for ICU 57. A better fix is to make the look-ahead numbers
729 // in the state table be sequential, then we can just index an array. And the
730 // table could also tell us in advance how big that array needs to be.
732 // Before ICU 57 there was just a single simple variable for a look-ahead match that
733 // was in progress. Two rules at once did not work.
735 static const int32_t kMaxLookaheads
= 8;
736 struct LookAheadResults
{
737 int32_t fUsedSlotLimit
;
738 int32_t fPositions
[8];
741 LookAheadResults() : fUsedSlotLimit(0), fPositions(), fKeys() {};
743 int32_t getPosition(int16_t key
) {
744 for (int32_t i
=0; i
<fUsedSlotLimit
; ++i
) {
745 if (fKeys
[i
] == key
) {
746 return fPositions
[i
];
749 // with NLLT source rules, Latn sample and ubrk_next, we see a request for key 79 here
750 // near the end of text, when setPosition has only ever set positions for key 80 or 82.
755 void setPosition(int16_t key
, int32_t position
) {
757 for (i
=0; i
<fUsedSlotLimit
; ++i
) {
758 if (fKeys
[i
] == key
) {
759 fPositions
[i
] = position
;
763 if (i
>= kMaxLookaheads
) {
765 i
= kMaxLookaheads
- 1;
768 fPositions
[i
] = position
;
769 U_ASSERT(fUsedSlotLimit
== i
);
770 fUsedSlotLimit
= i
+ 1;
775 //-----------------------------------------------------------------------------------
778 // Run the state machine to find a boundary
780 //-----------------------------------------------------------------------------------
781 // Route handleNext calls through the following to handleNextInternal,
782 // in order to handle fLineWordOpts.
783 int32_t RuleBasedBreakIterator::handleNext() {
784 int32_t result
= handleNextInternal();
785 while (fLineWordOpts
!= UBRK_LINEWORD_NORMAL
) {
786 UChar32 prevChr
= utext_char32At(&fText
, result
-1);
787 UChar32 currChr
= utext_char32At(&fText
, result
);
788 if (currChr
== U_SENTINEL
|| prevChr
== U_SENTINEL
) {
791 if (fLineWordOpts
== UBRK_LINEWORD_KEEP_HANGUL
) {
792 UErrorCode status
= U_ZERO_ERROR
;
793 if (uscript_getScript(currChr
, &status
) != USCRIPT_HANGUL
|| uscript_getScript(prevChr
, &status
) != USCRIPT_HANGUL
) {
797 if (!u_isalpha(currChr
) || !u_isalpha(prevChr
)) {
801 int32_t nextResult
= handleNextInternal();
802 if (nextResult
<= result
) {
810 int32_t RuleBasedBreakIterator::handleNextInternal() {
812 uint16_t category
= 0;
815 RBBIStateTableRow
*row
;
817 LookAheadResults lookAheadMatches
;
819 int32_t initialPosition
= 0;
820 const RBBIStateTable
*statetable
= fData
->fForwardTable
;
821 const char *tableData
= statetable
->fTableData
;
822 uint32_t tableRowLen
= statetable
->fRowLen
;
825 RBBIDebugPuts("Handle Next pos char state category");
829 // handleNext alway sets the break tag value.
830 // Set the default for it.
831 fRuleStatusIndex
= 0;
833 fDictionaryCharCount
= 0;
835 // if we're already at the end of the text, return DONE.
836 initialPosition
= fPosition
;
837 UTEXT_SETNATIVEINDEX(&fText
, initialPosition
);
838 result
= initialPosition
;
839 c
= UTEXT_NEXT32(&fText
);
845 // Set the initial state for the state machine
847 row
= (RBBIStateTableRow
*)
848 //(statetable->fTableData + (statetable->fRowLen * state));
849 (tableData
+ tableRowLen
* state
);
853 if (statetable
->fFlags
& RBBI_BOF_REQUIRED
) {
859 // loop until we reach the end of the text or transition to state 0
862 if (c
== U_SENTINEL
) {
863 // Reached end of input string.
864 if (mode
== RBBI_END
) {
865 // We have already run the loop one last time with the
866 // character set to the psueudo {eof} value. Now it is time
867 // to unconditionally bail out.
870 // Run the loop one last time with the fake end-of-input character category.
876 // Get the char category. An incoming category of 1 or 2 means that
877 // we are preset for doing the beginning or end of input, and
878 // that we shouldn't get a category from an actual text input character.
880 if (mode
== RBBI_RUN
) {
881 // look up the current character's character category, which tells us
882 // which column in the state table to look at.
883 // Note: the 16 in UTRIE_GET16 refers to the size of the data being returned,
884 // not the size of the character going in, which is a UChar32.
886 category
= (fLatin1Cat
!=NULL
&& c
<0x100)? fLatin1Cat
[c
]: UTRIE2_GET16(fData
->fTrie
, c
);
888 // Check the dictionary bit in the character's category.
889 // Counter is only used by dictionary based iteration.
890 // Chars that need to be handled by a dictionary have a flag bit set
891 // in their category values.
893 if ((category
& 0x4000) != 0) {
894 fDictionaryCharCount
++;
895 // And off the dictionary flag bit.
902 RBBIDebugPrintf(" %4ld ", utext_getNativeIndex(&fText
));
903 if (0x20<=c
&& c
<0x7f) {
904 RBBIDebugPrintf("\"%c\" ", c
);
906 RBBIDebugPrintf("%5x ", c
);
908 RBBIDebugPrintf("%3d %3d\n", state
, category
);
912 // State Transition - move machine to its next state
915 // fNextState is a variable-length array.
916 U_ASSERT(category
<fData
->fHeader
->fCatCount
);
917 state
= row
->fNextState
[category
]; /*Not accessing beyond memory*/
918 row
= (RBBIStateTableRow
*)
919 // (statetable->fTableData + (statetable->fRowLen * state));
920 (tableData
+ tableRowLen
* state
);
923 if (row
->fAccepting
== -1) {
924 // Match found, common case.
925 if (mode
!= RBBI_START
) {
926 result
= (int32_t)UTEXT_GETNATIVEINDEX(&fText
);
928 fRuleStatusIndex
= row
->fTagIdx
; // Remember the break status (tag) values.
931 int16_t completedRule
= row
->fAccepting
;
932 if (completedRule
> 0) {
933 // Lookahead match is completed.
934 int32_t lookaheadResult
= lookAheadMatches
.getPosition(completedRule
);
935 if (lookaheadResult
>= 0) {
936 fRuleStatusIndex
= row
->fTagIdx
;
937 fPosition
= lookaheadResult
;
938 return lookaheadResult
;
941 int16_t rule
= row
->fLookAhead
;
943 // At the position of a '/' in a look-ahead match. Record it.
944 int32_t pos
= (int32_t)UTEXT_GETNATIVEINDEX(&fText
);
945 lookAheadMatches
.setPosition(rule
, pos
);
948 if (state
== STOP_STATE
) {
949 // This is the normal exit from the lookup state machine.
950 // We have advanced through the string until it is certain that no
951 // longer match is possible, no matter what characters follow.
955 // Advance to the next character.
956 // If this is a beginning-of-input loop iteration, don't advance
957 // the input position. The next iteration will be processing the
958 // first real input character.
959 if (mode
== RBBI_RUN
) {
960 c
= UTEXT_NEXT32(&fText
);
962 if (mode
== RBBI_START
) {
968 // The state machine is done. Check whether it found a match...
970 // If the iterator failed to advance in the match engine, force it ahead by one.
971 // (This really indicates a defect in the break rules. They should always match
972 // at least one character.)
973 if (result
== initialPosition
) {
974 utext_setNativeIndex(&fText
, initialPosition
);
975 utext_next32(&fText
);
976 result
= (int32_t)utext_getNativeIndex(&fText
);
977 fRuleStatusIndex
= 0;
980 // Leave the iterator at our result position.
984 RBBIDebugPrintf("result = %d\n\n", result
);
991 //-----------------------------------------------------------------------------------
993 // handleSafePrevious()
995 // Iterate backwards using the safe reverse rules.
996 // The logic of this function is similar to handleNext(), but simpler
997 // because the safe table does not require as many options.
999 //-----------------------------------------------------------------------------------
1000 int32_t RuleBasedBreakIterator::handleSafePrevious(int32_t fromPosition
) {
1002 uint16_t category
= 0;
1003 RBBIStateTableRow
*row
;
1007 const RBBIStateTable
*stateTable
= fData
->fReverseTable
;
1008 UTEXT_SETNATIVEINDEX(&fText
, fromPosition
);
1011 RBBIDebugPuts("Handle Previous pos char state category");
1015 // if we're already at the start of the text, return DONE.
1016 if (fData
== NULL
|| UTEXT_GETNATIVEINDEX(&fText
)==0) {
1017 return BreakIterator::DONE
;
1020 // Set the initial state for the state machine
1021 c
= UTEXT_PREVIOUS32(&fText
);
1022 state
= START_STATE
;
1023 row
= (RBBIStateTableRow
*)
1024 (stateTable
->fTableData
+ (stateTable
->fRowLen
* state
));
1026 // loop until we reach the start of the text or transition to state 0
1028 for (; c
!= U_SENTINEL
; c
= UTEXT_PREVIOUS32(&fText
)) {
1030 // look up the current character's character category, which tells us
1031 // which column in the state table to look at.
1032 // Note: the 16 in UTRIE_GET16 refers to the size of the data being returned,
1033 // not the size of the character going in, which is a UChar32.
1035 // And off the dictionary flag bit. For reverse iteration it is not used.
1036 category
= UTRIE2_GET16(fData
->fTrie
, c
);
1037 category
&= ~0x4000;
1041 RBBIDebugPrintf(" %4d ", (int32_t)utext_getNativeIndex(&fText
));
1042 if (0x20<=c
&& c
<0x7f) {
1043 RBBIDebugPrintf("\"%c\" ", c
);
1045 RBBIDebugPrintf("%5x ", c
);
1047 RBBIDebugPrintf("%3d %3d\n", state
, category
);
1051 // State Transition - move machine to its next state
1053 // fNextState is a variable-length array.
1054 U_ASSERT(category
<fData
->fHeader
->fCatCount
);
1055 state
= row
->fNextState
[category
]; /*Not accessing beyond memory*/
1056 row
= (RBBIStateTableRow
*)
1057 (stateTable
->fTableData
+ (stateTable
->fRowLen
* state
));
1059 if (state
== STOP_STATE
) {
1060 // This is the normal exit from the lookup state machine.
1061 // Transistion to state zero means we have found a safe point.
1066 // The state machine is done. Check whether it found a match...
1067 result
= (int32_t)UTEXT_GETNATIVEINDEX(&fText
);
1070 RBBIDebugPrintf("result = %d\n\n", result
);
1076 //-------------------------------------------------------------------------------
1078 // getRuleStatus() Return the break rule tag associated with the current
1079 // iterator position. If the iterator arrived at its current
1080 // position by iterating forwards, the value will have been
1081 // cached by the handleNext() function.
1083 //-------------------------------------------------------------------------------
1085 int32_t RuleBasedBreakIterator::getRuleStatus() const {
1087 // fLastRuleStatusIndex indexes to the start of the appropriate status record
1088 // (the number of status values.)
1089 // This function returns the last (largest) of the array of status values.
1090 int32_t idx
= fRuleStatusIndex
+ fData
->fRuleStatusTable
[fRuleStatusIndex
];
1091 int32_t tagVal
= fData
->fRuleStatusTable
[idx
];
1097 int32_t RuleBasedBreakIterator::getRuleStatusVec(
1098 int32_t *fillInVec
, int32_t capacity
, UErrorCode
&status
) {
1099 if (U_FAILURE(status
)) {
1103 int32_t numVals
= fData
->fRuleStatusTable
[fRuleStatusIndex
];
1104 int32_t numValsToCopy
= numVals
;
1105 if (numVals
> capacity
) {
1106 status
= U_BUFFER_OVERFLOW_ERROR
;
1107 numValsToCopy
= capacity
;
1110 for (i
=0; i
<numValsToCopy
; i
++) {
1111 fillInVec
[i
] = fData
->fRuleStatusTable
[fRuleStatusIndex
+ i
+ 1];
1116 // Apple custom addition
1117 int32_t RuleBasedBreakIterator::tokenize(int32_t maxTokens
, RuleBasedTokenRange
*outTokenRanges
, unsigned long *outTokenFlags
)
1119 //os_log(OS_LOG_DEFAULT, "# tokenize 0: maxT %d; txt idx %lld, len %lld", maxTokens, utext_getNativeIndex(fText), utext_nativeLength(fText));
1123 RuleBasedTokenRange
*outTokenLimit
= outTokenRanges
+ maxTokens
;
1124 RuleBasedTokenRange
*outTokenP
= outTokenRanges
;
1125 int32_t lastOffset
= fPosition
;
1126 //os_log(OS_LOG_DEFAULT, "# tokenize 1");
1127 while (outTokenP
< outTokenLimit
) {
1128 // start portion from inlining populateFollowing()
1130 int32_t ruleStatusIdx
= 0;
1131 int32_t startPos
= fPosition
;
1133 if (fDictionaryCache
->following(startPos
, &pos
, &ruleStatusIdx
)) {
1135 fRuleStatusIndex
= ruleStatusIdx
;
1137 pos
= handleNextInternal(); // sets fRuleStatusIndex for the pos it returns, updates fPosition
1138 if (pos
== UBRK_DONE
) {
1139 // fDone = TRUE; already set by handleNextInternal
1142 // Use current result from handleNextInternal(), including fRuleStatusIndex,
1143 // unless overridden by dictionary subdivisions
1145 if (fDictionaryCharCount
> 0) {
1146 // The text segment obtained from the rules includes dictionary characters.
1147 // Subdivide it, with subdivided results going into the dictionary cache.
1148 fDictionaryCache
->populateDictionary(startPos
, pos
, fRuleStatusIndex
, fRuleStatusIndex
);
1149 if (fDictionaryCache
->following(startPos
, &pos
, &ruleStatusIdx
)) {
1151 fRuleStatusIndex
= ruleStatusIdx
;
1155 // end portion from inlining populateFollowing()
1156 int32_t flagCount
= fData
->fRuleStatusTable
[fRuleStatusIndex
];
1157 const int32_t* flagPtr
= fData
->fRuleStatusTable
+ fRuleStatusIndex
+ flagCount
;
1158 int32_t flagSet
= *flagPtr
; // if -1 then skip token
1159 if (flagSet
!= -1) {
1160 outTokenP
->location
= lastOffset
;
1161 outTokenP
++->length
= fPosition
- lastOffset
;
1162 if (outTokenFlags
) {
1163 // flagSet should be the OR of all flags returned by getRuleStatusVec;
1164 // here we collect from high-order to low-order.
1165 while (--flagCount
> 0) {
1166 flagSet
|= *--flagPtr
;
1168 *outTokenFlags
++ = (unsigned long)flagSet
;
1171 lastOffset
= fPosition
;
1173 return (outTokenP
- outTokenRanges
);
1176 //-------------------------------------------------------------------------------
1178 // getBinaryRules Access to the compiled form of the rules,
1179 // for use by build system tools that save the data
1180 // for standard iterator types.
1182 //-------------------------------------------------------------------------------
1183 const uint8_t *RuleBasedBreakIterator::getBinaryRules(uint32_t &length
) {
1184 const uint8_t *retPtr
= NULL
;
1187 if (fData
!= NULL
) {
1188 retPtr
= (const uint8_t *)fData
->fHeader
;
1189 length
= fData
->fHeader
->fLength
;
1195 BreakIterator
* RuleBasedBreakIterator::createBufferClone(void * /*stackBuffer*/,
1196 int32_t &bufferSize
,
1199 if (U_FAILURE(status
)){
1203 if (bufferSize
== 0) {
1204 bufferSize
= 1; // preflighting for deprecated functionality
1208 BreakIterator
*clonedBI
= clone();
1209 if (clonedBI
== NULL
) {
1210 status
= U_MEMORY_ALLOCATION_ERROR
;
1212 status
= U_SAFECLONE_ALLOCATED_WARNING
;
1214 return (RuleBasedBreakIterator
*)clonedBI
;
1220 static icu::UStack
*gLanguageBreakFactories
= nullptr;
1221 static const icu::UnicodeString
*gEmptyString
= nullptr;
1222 static icu::UInitOnce gLanguageBreakFactoriesInitOnce
= U_INITONCE_INITIALIZER
;
1223 static icu::UInitOnce gRBBIInitOnce
= U_INITONCE_INITIALIZER
;
1226 * Release all static memory held by breakiterator.
1229 static UBool U_CALLCONV
rbbi_cleanup(void) {
1230 delete gLanguageBreakFactories
;
1231 gLanguageBreakFactories
= nullptr;
1232 delete gEmptyString
;
1233 gEmptyString
= nullptr;
1234 gLanguageBreakFactoriesInitOnce
.reset();
1235 gRBBIInitOnce
.reset();
1241 static void U_CALLCONV
_deleteFactory(void *obj
) {
1242 delete (icu::LanguageBreakFactory
*) obj
;
1247 static void U_CALLCONV
rbbiInit() {
1248 gEmptyString
= new UnicodeString();
1249 ucln_common_registerCleanup(UCLN_COMMON_RBBI
, rbbi_cleanup
);
1252 static void U_CALLCONV
initLanguageFactories() {
1253 UErrorCode status
= U_ZERO_ERROR
;
1254 U_ASSERT(gLanguageBreakFactories
== NULL
);
1255 gLanguageBreakFactories
= new UStack(_deleteFactory
, NULL
, status
);
1256 if (gLanguageBreakFactories
!= NULL
&& U_SUCCESS(status
)) {
1257 ICULanguageBreakFactory
*builtIn
= new ICULanguageBreakFactory(status
);
1258 gLanguageBreakFactories
->push(builtIn
, status
);
1259 #ifdef U_LOCAL_SERVICE_HOOK
1260 LanguageBreakFactory
*extra
= (LanguageBreakFactory
*)uprv_svc_hook("languageBreakFactory", &status
);
1261 if (extra
!= NULL
) {
1262 gLanguageBreakFactories
->push(extra
, status
);
1266 ucln_common_registerCleanup(UCLN_COMMON_RBBI
, rbbi_cleanup
);
1270 static const LanguageBreakEngine
*
1271 getLanguageBreakEngineFromFactory(UChar32 c
)
1273 umtx_initOnce(gLanguageBreakFactoriesInitOnce
, &initLanguageFactories
);
1274 if (gLanguageBreakFactories
== NULL
) {
1278 int32_t i
= gLanguageBreakFactories
->size();
1279 const LanguageBreakEngine
*lbe
= NULL
;
1281 LanguageBreakFactory
*factory
= (LanguageBreakFactory
*)(gLanguageBreakFactories
->elementAt(i
));
1282 lbe
= factory
->getEngineFor(c
);
1291 //-------------------------------------------------------------------------------
1293 // getLanguageBreakEngine Find an appropriate LanguageBreakEngine for the
1296 //-------------------------------------------------------------------------------
1297 const LanguageBreakEngine
*
1298 RuleBasedBreakIterator::getLanguageBreakEngine(UChar32 c
) {
1299 const LanguageBreakEngine
*lbe
= NULL
;
1300 UErrorCode status
= U_ZERO_ERROR
;
1302 if (fLanguageBreakEngines
== NULL
) {
1303 fLanguageBreakEngines
= new UStack(status
);
1304 if (fLanguageBreakEngines
== NULL
|| U_FAILURE(status
)) {
1305 delete fLanguageBreakEngines
;
1306 fLanguageBreakEngines
= 0;
1311 int32_t i
= fLanguageBreakEngines
->size();
1313 lbe
= (const LanguageBreakEngine
*)(fLanguageBreakEngines
->elementAt(i
));
1314 if (lbe
->handles(c
)) {
1319 // No existing dictionary took the character. See if a factory wants to
1320 // give us a new LanguageBreakEngine for this character.
1321 lbe
= getLanguageBreakEngineFromFactory(c
);
1323 // If we got one, use it and push it on our stack.
1325 fLanguageBreakEngines
->push((void *)lbe
, status
);
1326 // Even if we can't remember it, we can keep looking it up, so
1327 // return it even if the push fails.
1331 // No engine is forthcoming for this character. Add it to the
1332 // reject set. Create the reject break engine if needed.
1333 if (fUnhandledBreakEngine
== NULL
) {
1334 fUnhandledBreakEngine
= new UnhandledEngine(status
);
1335 if (U_SUCCESS(status
) && fUnhandledBreakEngine
== NULL
) {
1336 status
= U_MEMORY_ALLOCATION_ERROR
;
1339 // Put it last so that scripts for which we have an engine get tried
1341 fLanguageBreakEngines
->insertElementAt(fUnhandledBreakEngine
, 0, status
);
1342 // If we can't insert it, or creation failed, get rid of it
1343 if (U_FAILURE(status
)) {
1344 delete fUnhandledBreakEngine
;
1345 fUnhandledBreakEngine
= 0;
1350 // Tell the reject engine about the character; at its discretion, it may
1351 // add more than just the one character.
1352 fUnhandledBreakEngine
->handleCharacter(c
);
1354 return fUnhandledBreakEngine
;
1357 void RuleBasedBreakIterator::dumpCache() {
1358 fBreakCache
->dumpCache();
1361 void RuleBasedBreakIterator::dumpTables() {
1366 * Returns the description used to create this iterator
1369 const UnicodeString
&
1370 RuleBasedBreakIterator::getRules() const {
1371 if (fData
!= NULL
) {
1372 return fData
->getRuleSourceString();
1374 umtx_initOnce(gRBBIInitOnce
, &rbbiInit
);
1375 return *gEmptyString
;
1381 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */