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 //  file:  rbbi.cpp  Contains the implementation of the rule based break iterator
11 //                   runtime engine and the API implementation for
12 //                   class RuleBasedBreakIterator
13 //
14 
15 #include "utypeinfo.h"  // for 'typeid' to work
16 
17 #include "unicode/utypes.h"
18 
19 #if !UCONFIG_NO_BREAK_ITERATION
20 
21 #include <cinttypes>
22 
23 #include "unicode/rbbi.h"
24 #include "unicode/schriter.h"
25 #include "unicode/uchriter.h"
26 #include "unicode/uclean.h"
27 #include "unicode/udata.h"
28 
29 #include "brkeng.h"
30 #include "ucln_cmn.h"
31 #include "cmemory.h"
32 #include "cstring.h"
33 #include "localsvc.h"
34 #include "rbbidata.h"
35 #include "rbbi_cache.h"
36 #include "rbbirb.h"
37 #include "uassert.h"
38 #include "umutex.h"
39 #include "uvectr32.h"
40 
41 #ifdef RBBI_DEBUG
42 static UBool gTrace = FALSE;
43 #endif
44 
45 U_NAMESPACE_BEGIN
46 
47 // The state number of the starting state
48 constexpr int32_t START_STATE = 1;
49 
50 // The state-transition value indicating "stop"
51 constexpr int32_t STOP_STATE = 0;
52 
53 
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RuleBasedBreakIterator)54 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RuleBasedBreakIterator)
55 
56 
57 //=======================================================================
58 // constructors
59 //=======================================================================
60 
61 /**
62  * Constructs a RuleBasedBreakIterator that uses the already-created
63  * tables object that is passed in as a parameter.
64  */
65 RuleBasedBreakIterator::RuleBasedBreakIterator(RBBIDataHeader* data, UErrorCode &status)
66  : fSCharIter(UnicodeString())
67 {
68     init(status);
69     fData = new RBBIDataWrapper(data, status); // status checked in constructor
70     if (U_FAILURE(status)) {return;}
71     if(fData == nullptr) {
72         status = U_MEMORY_ALLOCATION_ERROR;
73         return;
74     }
75     if (fData->fForwardTable->fLookAheadResultsSize > 0) {
76         fLookAheadMatches = static_cast<int32_t *>(
77             uprv_malloc(fData->fForwardTable->fLookAheadResultsSize * sizeof(int32_t)));
78         if (fLookAheadMatches == nullptr) {
79             status = U_MEMORY_ALLOCATION_ERROR;
80             return;
81         }
82     }
83 }
84 
85 //
86 //  Construct from precompiled binary rules (tables).  This constructor is public API,
87 //  taking the rules as a (const uint8_t *) to match the type produced by getBinaryRules().
88 //
RuleBasedBreakIterator(const uint8_t * compiledRules,uint32_t ruleLength,UErrorCode & status)89 RuleBasedBreakIterator::RuleBasedBreakIterator(const uint8_t *compiledRules,
90                        uint32_t       ruleLength,
91                        UErrorCode     &status)
92  : fSCharIter(UnicodeString())
93 {
94     init(status);
95     if (U_FAILURE(status)) {
96         return;
97     }
98     if (compiledRules == NULL || ruleLength < sizeof(RBBIDataHeader)) {
99         status = U_ILLEGAL_ARGUMENT_ERROR;
100         return;
101     }
102     const RBBIDataHeader *data = (const RBBIDataHeader *)compiledRules;
103     if (data->fLength > ruleLength) {
104         status = U_ILLEGAL_ARGUMENT_ERROR;
105         return;
106     }
107     fData = new RBBIDataWrapper(data, RBBIDataWrapper::kDontAdopt, status);
108     if (U_FAILURE(status)) {return;}
109     if(fData == nullptr) {
110         status = U_MEMORY_ALLOCATION_ERROR;
111         return;
112     }
113     if (fData->fForwardTable->fLookAheadResultsSize > 0) {
114         fLookAheadMatches = static_cast<int32_t *>(
115             uprv_malloc(fData->fForwardTable->fLookAheadResultsSize * sizeof(int32_t)));
116         if (fLookAheadMatches == nullptr) {
117             status = U_MEMORY_ALLOCATION_ERROR;
118             return;
119         }
120     }
121 }
122 
123 
124 //-------------------------------------------------------------------------------
125 //
126 //   Constructor   from a UDataMemory handle to precompiled break rules
127 //                 stored in an ICU data file.
128 //
129 //-------------------------------------------------------------------------------
RuleBasedBreakIterator(UDataMemory * udm,UErrorCode & status)130 RuleBasedBreakIterator::RuleBasedBreakIterator(UDataMemory* udm, UErrorCode &status)
131  : fSCharIter(UnicodeString())
132 {
133     init(status);
134     fData = new RBBIDataWrapper(udm, status); // status checked in constructor
135     if (U_FAILURE(status)) {return;}
136     if(fData == nullptr) {
137         status = U_MEMORY_ALLOCATION_ERROR;
138         return;
139     }
140     if (fData->fForwardTable->fLookAheadResultsSize > 0) {
141         fLookAheadMatches = static_cast<int32_t *>(
142             uprv_malloc(fData->fForwardTable->fLookAheadResultsSize * sizeof(int32_t)));
143         if (fLookAheadMatches == nullptr) {
144             status = U_MEMORY_ALLOCATION_ERROR;
145             return;
146         }
147     }
148 }
149 
150 
151 
152 //-------------------------------------------------------------------------------
153 //
154 //   Constructor       from a set of rules supplied as a string.
155 //
156 //-------------------------------------------------------------------------------
RuleBasedBreakIterator(const UnicodeString & rules,UParseError & parseError,UErrorCode & status)157 RuleBasedBreakIterator::RuleBasedBreakIterator( const UnicodeString  &rules,
158                                                 UParseError          &parseError,
159                                                 UErrorCode           &status)
160  : fSCharIter(UnicodeString())
161 {
162     init(status);
163     if (U_FAILURE(status)) {return;}
164     RuleBasedBreakIterator *bi = (RuleBasedBreakIterator *)
165         RBBIRuleBuilder::createRuleBasedBreakIterator(rules, &parseError, status);
166     // Note:  This is a bit awkward.  The RBBI ruleBuilder has a factory method that
167     //        creates and returns a complete RBBI.  From here, in a constructor, we
168     //        can't just return the object created by the builder factory, hence
169     //        the assignment of the factory created object to "this".
170     if (U_SUCCESS(status)) {
171         *this = *bi;
172         delete bi;
173     }
174 }
175 
176 
177 //-------------------------------------------------------------------------------
178 //
179 // Default Constructor.      Create an empty shell that can be set up later.
180 //                           Used when creating a RuleBasedBreakIterator from a set
181 //                           of rules.
182 //-------------------------------------------------------------------------------
RuleBasedBreakIterator()183 RuleBasedBreakIterator::RuleBasedBreakIterator()
184  : fSCharIter(UnicodeString())
185 {
186     UErrorCode status = U_ZERO_ERROR;
187     init(status);
188 }
189 
190 
191 //-------------------------------------------------------------------------------
192 //
193 //   Copy constructor.  Will produce a break iterator with the same behavior,
194 //                      and which iterates over the same text, as the one passed in.
195 //
196 //-------------------------------------------------------------------------------
RuleBasedBreakIterator(const RuleBasedBreakIterator & other)197 RuleBasedBreakIterator::RuleBasedBreakIterator(const RuleBasedBreakIterator& other)
198 : BreakIterator(other),
199   fSCharIter(UnicodeString())
200 {
201     UErrorCode status = U_ZERO_ERROR;
202     this->init(status);
203     *this = other;
204 }
205 
206 
207 /**
208  * Destructor
209  */
~RuleBasedBreakIterator()210 RuleBasedBreakIterator::~RuleBasedBreakIterator() {
211     if (fCharIter != &fSCharIter) {
212         // fCharIter was adopted from the outside.
213         delete fCharIter;
214     }
215     fCharIter = nullptr;
216 
217     utext_close(&fText);
218 
219     if (fData != nullptr) {
220         fData->removeReference();
221         fData = nullptr;
222     }
223     delete fBreakCache;
224     fBreakCache = nullptr;
225 
226     delete fDictionaryCache;
227     fDictionaryCache = nullptr;
228 
229     delete fLanguageBreakEngines;
230     fLanguageBreakEngines = nullptr;
231 
232     delete fUnhandledBreakEngine;
233     fUnhandledBreakEngine = nullptr;
234 
235     uprv_free(fLookAheadMatches);
236     fLookAheadMatches = nullptr;
237 }
238 
239 /**
240  * Assignment operator.  Sets this iterator to have the same behavior,
241  * and iterate over the same text, as the one passed in.
242  * TODO: needs better handling of memory allocation errors.
243  */
244 RuleBasedBreakIterator&
operator =(const RuleBasedBreakIterator & that)245 RuleBasedBreakIterator::operator=(const RuleBasedBreakIterator& that) {
246     if (this == &that) {
247         return *this;
248     }
249     BreakIterator::operator=(that);
250 
251     if (fLanguageBreakEngines != NULL) {
252         delete fLanguageBreakEngines;
253         fLanguageBreakEngines = NULL;   // Just rebuild for now
254     }
255     // TODO: clone fLanguageBreakEngines from "that"
256     UErrorCode status = U_ZERO_ERROR;
257     utext_clone(&fText, &that.fText, FALSE, TRUE, &status);
258 
259     if (fCharIter != &fSCharIter) {
260         delete fCharIter;
261     }
262     fCharIter = &fSCharIter;
263 
264     if (that.fCharIter != NULL && that.fCharIter != &that.fSCharIter) {
265         // This is a little bit tricky - it will intially appear that
266         //  this->fCharIter is adopted, even if that->fCharIter was
267         //  not adopted.  That's ok.
268         fCharIter = that.fCharIter->clone();
269     }
270     fSCharIter = that.fSCharIter;
271     if (fCharIter == NULL) {
272         fCharIter = &fSCharIter;
273     }
274 
275     if (fData != NULL) {
276         fData->removeReference();
277         fData = NULL;
278     }
279     if (that.fData != NULL) {
280         fData = that.fData->addReference();
281     }
282 
283     uprv_free(fLookAheadMatches);
284     fLookAheadMatches = nullptr;
285     if (fData && fData->fForwardTable->fLookAheadResultsSize > 0) {
286         fLookAheadMatches = static_cast<int32_t *>(
287             uprv_malloc(fData->fForwardTable->fLookAheadResultsSize * sizeof(int32_t)));
288     }
289 
290 
291     fPosition = that.fPosition;
292     fRuleStatusIndex = that.fRuleStatusIndex;
293     fDone = that.fDone;
294 
295     // TODO: both the dictionary and the main cache need to be copied.
296     //       Current position could be within a dictionary range. Trying to continue
297     //       the iteration without the caches present would go to the rules, with
298     //       the assumption that the current position is on a rule boundary.
299     fBreakCache->reset(fPosition, fRuleStatusIndex);
300     fDictionaryCache->reset();
301 
302     return *this;
303 }
304 
305 
306 
307 //-----------------------------------------------------------------------------
308 //
309 //    init()      Shared initialization routine.   Used by all the constructors.
310 //                Initializes all fields, leaving the object in a consistent state.
311 //
312 //-----------------------------------------------------------------------------
init(UErrorCode & status)313 void RuleBasedBreakIterator::init(UErrorCode &status) {
314     fCharIter             = nullptr;
315     fData                 = nullptr;
316     fPosition             = 0;
317     fRuleStatusIndex      = 0;
318     fDone                 = false;
319     fDictionaryCharCount  = 0;
320     fLanguageBreakEngines = nullptr;
321     fUnhandledBreakEngine = nullptr;
322     fBreakCache           = nullptr;
323     fDictionaryCache      = nullptr;
324     fLookAheadMatches     = nullptr;
325 
326     // Note: IBM xlC is unable to assign or initialize member fText from UTEXT_INITIALIZER.
327     // fText                 = UTEXT_INITIALIZER;
328     static const UText initializedUText = UTEXT_INITIALIZER;
329     uprv_memcpy(&fText, &initializedUText, sizeof(UText));
330 
331    if (U_FAILURE(status)) {
332         return;
333     }
334 
335     utext_openUChars(&fText, NULL, 0, &status);
336     fDictionaryCache = new DictionaryCache(this, status);
337     fBreakCache      = new BreakCache(this, status);
338     if (U_SUCCESS(status) && (fDictionaryCache == NULL || fBreakCache == NULL)) {
339         status = U_MEMORY_ALLOCATION_ERROR;
340     }
341 
342 #ifdef RBBI_DEBUG
343     static UBool debugInitDone = FALSE;
344     if (debugInitDone == FALSE) {
345         char *debugEnv = getenv("U_RBBIDEBUG");
346         if (debugEnv && uprv_strstr(debugEnv, "trace")) {
347             gTrace = TRUE;
348         }
349         debugInitDone = TRUE;
350     }
351 #endif
352 }
353 
354 
355 
356 //-----------------------------------------------------------------------------
357 //
358 //    clone - Returns a newly-constructed RuleBasedBreakIterator with the same
359 //            behavior, and iterating over the same text, as this one.
360 //            Virtual function: does the right thing with subclasses.
361 //
362 //-----------------------------------------------------------------------------
363 RuleBasedBreakIterator*
clone() const364 RuleBasedBreakIterator::clone() const {
365     return new RuleBasedBreakIterator(*this);
366 }
367 
368 /**
369  * Equality operator.  Returns TRUE if both BreakIterators are of the
370  * same class, have the same behavior, and iterate over the same text.
371  */
372 UBool
operator ==(const BreakIterator & that) const373 RuleBasedBreakIterator::operator==(const BreakIterator& that) const {
374     if (typeid(*this) != typeid(that)) {
375         return FALSE;
376     }
377     if (this == &that) {
378         return TRUE;
379     }
380 
381     // The base class BreakIterator carries no state that participates in equality,
382     // and does not implement an equality function that would otherwise be
383     // checked at this point.
384 
385     const RuleBasedBreakIterator& that2 = (const RuleBasedBreakIterator&) that;
386 
387     if (!utext_equals(&fText, &that2.fText)) {
388         // The two break iterators are operating on different text,
389         //   or have a different iteration position.
390         //   Note that fText's position is always the same as the break iterator's position.
391         return FALSE;
392     }
393 
394     if (!(fPosition == that2.fPosition &&
395             fRuleStatusIndex == that2.fRuleStatusIndex &&
396             fDone == that2.fDone)) {
397         return FALSE;
398     }
399 
400     if (that2.fData == fData ||
401         (fData != NULL && that2.fData != NULL && *that2.fData == *fData)) {
402             // The two break iterators are using the same rules.
403             return TRUE;
404         }
405     return FALSE;
406 }
407 
408 /**
409  * Compute a hash code for this BreakIterator
410  * @return A hash code
411  */
412 int32_t
hashCode(void) const413 RuleBasedBreakIterator::hashCode(void) const {
414     int32_t   hash = 0;
415     if (fData != NULL) {
416         hash = fData->hashCode();
417     }
418     return hash;
419 }
420 
421 
setText(UText * ut,UErrorCode & status)422 void RuleBasedBreakIterator::setText(UText *ut, UErrorCode &status) {
423     if (U_FAILURE(status)) {
424         return;
425     }
426     fBreakCache->reset();
427     fDictionaryCache->reset();
428     utext_clone(&fText, ut, FALSE, TRUE, &status);
429 
430     // Set up a dummy CharacterIterator to be returned if anyone
431     //   calls getText().  With input from UText, there is no reasonable
432     //   way to return a characterIterator over the actual input text.
433     //   Return one over an empty string instead - this is the closest
434     //   we can come to signaling a failure.
435     //   (GetText() is obsolete, this failure is sort of OK)
436     fSCharIter.setText(UnicodeString());
437 
438     if (fCharIter != &fSCharIter) {
439         // existing fCharIter was adopted from the outside.  Delete it now.
440         delete fCharIter;
441     }
442     fCharIter = &fSCharIter;
443 
444     this->first();
445 }
446 
447 
getUText(UText * fillIn,UErrorCode & status) const448 UText *RuleBasedBreakIterator::getUText(UText *fillIn, UErrorCode &status) const {
449     UText *result = utext_clone(fillIn, &fText, FALSE, TRUE, &status);
450     return result;
451 }
452 
453 
454 //=======================================================================
455 // BreakIterator overrides
456 //=======================================================================
457 
458 /**
459  * Return a CharacterIterator over the text being analyzed.
460  */
461 CharacterIterator&
getText() const462 RuleBasedBreakIterator::getText() const {
463     return *fCharIter;
464 }
465 
466 /**
467  * Set the iterator to analyze a new piece of text.  This function resets
468  * the current iteration position to the beginning of the text.
469  * @param newText An iterator over the text to analyze.
470  */
471 void
adoptText(CharacterIterator * newText)472 RuleBasedBreakIterator::adoptText(CharacterIterator* newText) {
473     // If we are holding a CharacterIterator adopted from a
474     //   previous call to this function, delete it now.
475     if (fCharIter != &fSCharIter) {
476         delete fCharIter;
477     }
478 
479     fCharIter = newText;
480     UErrorCode status = U_ZERO_ERROR;
481     fBreakCache->reset();
482     fDictionaryCache->reset();
483     if (newText==NULL || newText->startIndex() != 0) {
484         // startIndex !=0 wants to be an error, but there's no way to report it.
485         // Make the iterator text be an empty string.
486         utext_openUChars(&fText, NULL, 0, &status);
487     } else {
488         utext_openCharacterIterator(&fText, newText, &status);
489     }
490     this->first();
491 }
492 
493 /**
494  * Set the iterator to analyze a new piece of text.  This function resets
495  * the current iteration position to the beginning of the text.
496  * @param newText An iterator over the text to analyze.
497  */
498 void
setText(const UnicodeString & newText)499 RuleBasedBreakIterator::setText(const UnicodeString& newText) {
500     UErrorCode status = U_ZERO_ERROR;
501     fBreakCache->reset();
502     fDictionaryCache->reset();
503     utext_openConstUnicodeString(&fText, &newText, &status);
504 
505     // Set up a character iterator on the string.
506     //   Needed in case someone calls getText().
507     //  Can not, unfortunately, do this lazily on the (probably never)
508     //  call to getText(), because getText is const.
509     fSCharIter.setText(newText);
510 
511     if (fCharIter != &fSCharIter) {
512         // old fCharIter was adopted from the outside.  Delete it.
513         delete fCharIter;
514     }
515     fCharIter = &fSCharIter;
516 
517     this->first();
518 }
519 
520 
521 /**
522  *  Provide a new UText for the input text.  Must reference text with contents identical
523  *  to the original.
524  *  Intended for use with text data originating in Java (garbage collected) environments
525  *  where the data may be moved in memory at arbitrary times.
526  */
refreshInputText(UText * input,UErrorCode & status)527 RuleBasedBreakIterator &RuleBasedBreakIterator::refreshInputText(UText *input, UErrorCode &status) {
528     if (U_FAILURE(status)) {
529         return *this;
530     }
531     if (input == NULL) {
532         status = U_ILLEGAL_ARGUMENT_ERROR;
533         return *this;
534     }
535     int64_t pos = utext_getNativeIndex(&fText);
536     //  Shallow read-only clone of the new UText into the existing input UText
537     utext_clone(&fText, input, FALSE, TRUE, &status);
538     if (U_FAILURE(status)) {
539         return *this;
540     }
541     utext_setNativeIndex(&fText, pos);
542     if (utext_getNativeIndex(&fText) != pos) {
543         // Sanity check.  The new input utext is supposed to have the exact same
544         // contents as the old.  If we can't set to the same position, it doesn't.
545         // The contents underlying the old utext might be invalid at this point,
546         // so it's not safe to check directly.
547         status = U_ILLEGAL_ARGUMENT_ERROR;
548     }
549     return *this;
550 }
551 
552 
553 /**
554  * Sets the current iteration position to the beginning of the text, position zero.
555  * @return The new iterator position, which is zero.
556  */
first(void)557 int32_t RuleBasedBreakIterator::first(void) {
558     UErrorCode status = U_ZERO_ERROR;
559     if (!fBreakCache->seek(0)) {
560         fBreakCache->populateNear(0, status);
561     }
562     fBreakCache->current();
563     U_ASSERT(fPosition == 0);
564     return 0;
565 }
566 
567 /**
568  * Sets the current iteration position to the end of the text.
569  * @return The text's past-the-end offset.
570  */
last(void)571 int32_t RuleBasedBreakIterator::last(void) {
572     int32_t endPos = (int32_t)utext_nativeLength(&fText);
573     UBool endShouldBeBoundary = isBoundary(endPos);      // Has side effect of setting iterator position.
574     (void)endShouldBeBoundary;
575     U_ASSERT(endShouldBeBoundary);
576     U_ASSERT(fPosition == endPos);
577     return endPos;
578 }
579 
580 /**
581  * Advances the iterator either forward or backward the specified number of steps.
582  * Negative values move backward, and positive values move forward.  This is
583  * equivalent to repeatedly calling next() or previous().
584  * @param n The number of steps to move.  The sign indicates the direction
585  * (negative is backwards, and positive is forwards).
586  * @return The character offset of the boundary position n boundaries away from
587  * the current one.
588  */
next(int32_t n)589 int32_t RuleBasedBreakIterator::next(int32_t n) {
590     int32_t result = 0;
591     if (n > 0) {
592         for (; n > 0 && result != UBRK_DONE; --n) {
593             result = next();
594         }
595     } else if (n < 0) {
596         for (; n < 0 && result != UBRK_DONE; ++n) {
597             result = previous();
598         }
599     } else {
600         result = current();
601     }
602     return result;
603 }
604 
605 /**
606  * Advances the iterator to the next boundary position.
607  * @return The position of the first boundary after this one.
608  */
next(void)609 int32_t RuleBasedBreakIterator::next(void) {
610     fBreakCache->next();
611     return fDone ? UBRK_DONE : fPosition;
612 }
613 
614 /**
615  * Move the iterator backwards, to the boundary preceding the current one.
616  *
617  *         Starts from the current position within fText.
618  *         Starting position need not be on a boundary.
619  *
620  * @return The position of the boundary position immediately preceding the starting position.
621  */
previous(void)622 int32_t RuleBasedBreakIterator::previous(void) {
623     UErrorCode status = U_ZERO_ERROR;
624     fBreakCache->previous(status);
625     return fDone ? UBRK_DONE : fPosition;
626 }
627 
628 /**
629  * Sets the iterator to refer to the first boundary position following
630  * the specified position.
631  * @param startPos The position from which to begin searching for a break position.
632  * @return The position of the first break after the current position.
633  */
following(int32_t startPos)634 int32_t RuleBasedBreakIterator::following(int32_t startPos) {
635     // if the supplied position is before the beginning, return the
636     // text's starting offset
637     if (startPos < 0) {
638         return first();
639     }
640 
641     // Move requested offset to a code point start. It might be on a trail surrogate,
642     // or on a trail byte if the input is UTF-8. Or it may be beyond the end of the text.
643     utext_setNativeIndex(&fText, startPos);
644     startPos = (int32_t)utext_getNativeIndex(&fText);
645 
646     UErrorCode status = U_ZERO_ERROR;
647     fBreakCache->following(startPos, status);
648     return fDone ? UBRK_DONE : fPosition;
649 }
650 
651 /**
652  * Sets the iterator to refer to the last boundary position before the
653  * specified position.
654  * @param offset The position to begin searching for a break from.
655  * @return The position of the last boundary before the starting position.
656  */
preceding(int32_t offset)657 int32_t RuleBasedBreakIterator::preceding(int32_t offset) {
658     if (offset > utext_nativeLength(&fText)) {
659         return last();
660     }
661 
662     // Move requested offset to a code point start. It might be on a trail surrogate,
663     // or on a trail byte if the input is UTF-8.
664 
665     utext_setNativeIndex(&fText, offset);
666     int32_t adjustedOffset = static_cast<int32_t>(utext_getNativeIndex(&fText));
667 
668     UErrorCode status = U_ZERO_ERROR;
669     fBreakCache->preceding(adjustedOffset, status);
670     return fDone ? UBRK_DONE : fPosition;
671 }
672 
673 /**
674  * Returns true if the specfied position is a boundary position.  As a side
675  * effect, leaves the iterator pointing to the first boundary position at
676  * or after "offset".
677  *
678  * @param offset the offset to check.
679  * @return True if "offset" is a boundary position.
680  */
isBoundary(int32_t offset)681 UBool RuleBasedBreakIterator::isBoundary(int32_t offset) {
682     // out-of-range indexes are never boundary positions
683     if (offset < 0) {
684         first();       // For side effects on current position, tag values.
685         return FALSE;
686     }
687 
688     // Adjust offset to be on a code point boundary and not beyond the end of the text.
689     // Note that isBoundary() is always false for offsets that are not on code point boundaries.
690     // But we still need the side effect of leaving iteration at the following boundary.
691 
692     utext_setNativeIndex(&fText, offset);
693     int32_t adjustedOffset = static_cast<int32_t>(utext_getNativeIndex(&fText));
694 
695     bool result = false;
696     UErrorCode status = U_ZERO_ERROR;
697     if (fBreakCache->seek(adjustedOffset) || fBreakCache->populateNear(adjustedOffset, status)) {
698         result = (fBreakCache->current() == offset);
699     }
700 
701     if (result && adjustedOffset < offset && utext_char32At(&fText, offset) == U_SENTINEL) {
702         // Original offset is beyond the end of the text. Return FALSE, it's not a boundary,
703         // but the iteration position remains set to the end of the text, which is a boundary.
704         return FALSE;
705     }
706     if (!result) {
707         // Not on a boundary. isBoundary() must leave iterator on the following boundary.
708         // Cache->seek(), above, left us on the preceding boundary, so advance one.
709         next();
710     }
711     return result;
712 }
713 
714 
715 /**
716  * Returns the current iteration position.
717  * @return The current iteration position.
718  */
current(void) const719 int32_t RuleBasedBreakIterator::current(void) const {
720     return fPosition;
721 }
722 
723 
724 //=======================================================================
725 // implementation
726 //=======================================================================
727 
728 //
729 // RBBIRunMode  -  the state machine runs an extra iteration at the beginning and end
730 //                 of user text.  A variable with this enum type keeps track of where we
731 //                 are.  The state machine only fetches user input while in the RUN mode.
732 //
733 enum RBBIRunMode {
734     RBBI_START,     // state machine processing is before first char of input
735     RBBI_RUN,       // state machine processing is in the user text
736     RBBI_END        // state machine processing is after end of user text.
737 };
738 
739 
740 // Wrapper functions to select the appropriate handleNext() or handleSafePrevious()
741 // instantiation, based on whether an 8 or 16 bit table is required.
742 //
743 // These Trie access functions will be inlined within the handleNext()/Previous() instantions.
TrieFunc8(const UCPTrie * trie,UChar32 c)744 static inline uint16_t TrieFunc8(const UCPTrie *trie, UChar32 c) {
745     return UCPTRIE_FAST_GET(trie, UCPTRIE_8, c);
746 }
747 
TrieFunc16(const UCPTrie * trie,UChar32 c)748 static inline uint16_t TrieFunc16(const UCPTrie *trie, UChar32 c) {
749     return UCPTRIE_FAST_GET(trie, UCPTRIE_16, c);
750 }
751 
handleNext()752 int32_t RuleBasedBreakIterator::handleNext() {
753     const RBBIStateTable *statetable = fData->fForwardTable;
754     bool use8BitsTrie = ucptrie_getValueWidth(fData->fTrie) == UCPTRIE_VALUE_BITS_8;
755     if (statetable->fFlags & RBBI_8BITS_ROWS) {
756         if (use8BitsTrie) {
757             return handleNext<RBBIStateTableRow8, TrieFunc8>();
758         } else {
759             return handleNext<RBBIStateTableRow8, TrieFunc16>();
760         }
761     } else {
762         if (use8BitsTrie) {
763             return handleNext<RBBIStateTableRow16, TrieFunc8>();
764         } else {
765             return handleNext<RBBIStateTableRow16, TrieFunc16>();
766         }
767     }
768 }
769 
handleSafePrevious(int32_t fromPosition)770 int32_t RuleBasedBreakIterator::handleSafePrevious(int32_t fromPosition) {
771     const RBBIStateTable *statetable = fData->fReverseTable;
772     bool use8BitsTrie = ucptrie_getValueWidth(fData->fTrie) == UCPTRIE_VALUE_BITS_8;
773     if (statetable->fFlags & RBBI_8BITS_ROWS) {
774         if (use8BitsTrie) {
775             return handleSafePrevious<RBBIStateTableRow8, TrieFunc8>(fromPosition);
776         } else {
777             return handleSafePrevious<RBBIStateTableRow8, TrieFunc16>(fromPosition);
778         }
779     } else {
780         if (use8BitsTrie) {
781             return handleSafePrevious<RBBIStateTableRow16, TrieFunc8>(fromPosition);
782         } else {
783             return handleSafePrevious<RBBIStateTableRow16, TrieFunc16>(fromPosition);
784         }
785     }
786 }
787 
788 
789 //-----------------------------------------------------------------------------------
790 //
791 //  handleNext()
792 //     Run the state machine to find a boundary
793 //
794 //-----------------------------------------------------------------------------------
795 template <typename RowType, RuleBasedBreakIterator::PTrieFunc trieFunc>
handleNext()796 int32_t RuleBasedBreakIterator::handleNext() {
797     int32_t             state;
798     uint16_t            category        = 0;
799     RBBIRunMode         mode;
800 
801     RowType             *row;
802     UChar32             c;
803     int32_t             result             = 0;
804     int32_t             initialPosition    = 0;
805     const RBBIStateTable *statetable       = fData->fForwardTable;
806     const char         *tableData          = statetable->fTableData;
807     uint32_t            tableRowLen        = statetable->fRowLen;
808     uint32_t            dictStart          = statetable->fDictCategoriesStart;
809     #ifdef RBBI_DEBUG
810         if (gTrace) {
811             RBBIDebugPuts("Handle Next   pos   char  state category");
812         }
813     #endif
814 
815     // handleNext always sets the break tag value.
816     // Set the default for it.
817     fRuleStatusIndex = 0;
818 
819     fDictionaryCharCount = 0;
820 
821     // if we're already at the end of the text, return DONE.
822     initialPosition = fPosition;
823     UTEXT_SETNATIVEINDEX(&fText, initialPosition);
824     result          = initialPosition;
825     c               = UTEXT_NEXT32(&fText);
826     if (c==U_SENTINEL) {
827         fDone = TRUE;
828         return UBRK_DONE;
829     }
830 
831     //  Set the initial state for the state machine
832     state = START_STATE;
833     row = (RowType *)
834             //(statetable->fTableData + (statetable->fRowLen * state));
835             (tableData + tableRowLen * state);
836 
837 
838     mode     = RBBI_RUN;
839     if (statetable->fFlags & RBBI_BOF_REQUIRED) {
840         category = 2;
841         mode     = RBBI_START;
842     }
843 
844 
845     // loop until we reach the end of the text or transition to state 0
846     //
847     for (;;) {
848         if (c == U_SENTINEL) {
849             // Reached end of input string.
850             if (mode == RBBI_END) {
851                 // We have already run the loop one last time with the
852                 //   character set to the psueudo {eof} value.  Now it is time
853                 //   to unconditionally bail out.
854                 break;
855             }
856             // Run the loop one last time with the fake end-of-input character category.
857             mode = RBBI_END;
858             category = 1;
859         }
860 
861         //
862         // Get the char category.  An incoming category of 1 or 2 means that
863         //      we are preset for doing the beginning or end of input, and
864         //      that we shouldn't get a category from an actual text input character.
865         //
866         if (mode == RBBI_RUN) {
867             // look up the current character's character category, which tells us
868             // which column in the state table to look at.
869             category = trieFunc(fData->fTrie, c);
870             fDictionaryCharCount += (category >= dictStart);
871         }
872 
873        #ifdef RBBI_DEBUG
874             if (gTrace) {
875                 RBBIDebugPrintf("             %4" PRId64 "   ", utext_getNativeIndex(&fText));
876                 if (0x20<=c && c<0x7f) {
877                     RBBIDebugPrintf("\"%c\"  ", c);
878                 } else {
879                     RBBIDebugPrintf("%5x  ", c);
880                 }
881                 RBBIDebugPrintf("%3d  %3d\n", state, category);
882             }
883         #endif
884 
885         // State Transition - move machine to its next state
886         //
887 
888         // fNextState is a variable-length array.
889         U_ASSERT(category<fData->fHeader->fCatCount);
890         state = row->fNextState[category];  /*Not accessing beyond memory*/
891         row = (RowType *)
892             // (statetable->fTableData + (statetable->fRowLen * state));
893             (tableData + tableRowLen * state);
894 
895 
896         uint16_t accepting = row->fAccepting;
897         if (accepting == ACCEPTING_UNCONDITIONAL) {
898             // Match found, common case.
899             if (mode != RBBI_START) {
900                 result = (int32_t)UTEXT_GETNATIVEINDEX(&fText);
901             }
902             fRuleStatusIndex = row->fTagsIdx;   // Remember the break status (tag) values.
903         } else if (accepting > ACCEPTING_UNCONDITIONAL) {
904             // Lookahead match is completed.
905             U_ASSERT(accepting < fData->fForwardTable->fLookAheadResultsSize);
906             int32_t lookaheadResult = fLookAheadMatches[accepting];
907             if (lookaheadResult >= 0) {
908                 fRuleStatusIndex = row->fTagsIdx;
909                 fPosition = lookaheadResult;
910                 return lookaheadResult;
911             }
912         }
913 
914         // If we are at the position of the '/' in a look-ahead (hard break) rule;
915         // record the current position, to be returned later, if the full rule matches.
916         // TODO: Move this check before the previous check of fAccepting.
917         //       This would enable hard-break rules with no following context.
918         //       But there are line break test failures when trying this. Investigate.
919         //       Issue ICU-20837
920         uint16_t rule = row->fLookAhead;
921         U_ASSERT(rule == 0 || rule > ACCEPTING_UNCONDITIONAL);
922         U_ASSERT(rule == 0 || rule < fData->fForwardTable->fLookAheadResultsSize);
923         if (rule > ACCEPTING_UNCONDITIONAL) {
924             int32_t  pos = (int32_t)UTEXT_GETNATIVEINDEX(&fText);
925             fLookAheadMatches[rule] = pos;
926         }
927 
928         if (state == STOP_STATE) {
929             // This is the normal exit from the lookup state machine.
930             // We have advanced through the string until it is certain that no
931             //   longer match is possible, no matter what characters follow.
932             break;
933         }
934 
935         // Advance to the next character.
936         // If this is a beginning-of-input loop iteration, don't advance
937         //    the input position.  The next iteration will be processing the
938         //    first real input character.
939         if (mode == RBBI_RUN) {
940             c = UTEXT_NEXT32(&fText);
941         } else {
942             if (mode == RBBI_START) {
943                 mode = RBBI_RUN;
944             }
945         }
946     }
947 
948     // The state machine is done.  Check whether it found a match...
949 
950     // If the iterator failed to advance in the match engine, force it ahead by one.
951     //   (This really indicates a defect in the break rules.  They should always match
952     //    at least one character.)
953     if (result == initialPosition) {
954         utext_setNativeIndex(&fText, initialPosition);
955         utext_next32(&fText);
956         result = (int32_t)utext_getNativeIndex(&fText);
957         fRuleStatusIndex = 0;
958     }
959 
960     // Leave the iterator at our result position.
961     fPosition = result;
962     #ifdef RBBI_DEBUG
963         if (gTrace) {
964             RBBIDebugPrintf("result = %d\n\n", result);
965         }
966     #endif
967     return result;
968 }
969 
970 
971 //-----------------------------------------------------------------------------------
972 //
973 //  handleSafePrevious()
974 //
975 //      Iterate backwards using the safe reverse rules.
976 //      The logic of this function is similar to handleNext(), but simpler
977 //      because the safe table does not require as many options.
978 //
979 //-----------------------------------------------------------------------------------
980 template <typename RowType, RuleBasedBreakIterator::PTrieFunc trieFunc>
handleSafePrevious(int32_t fromPosition)981 int32_t RuleBasedBreakIterator::handleSafePrevious(int32_t fromPosition) {
982 
983     int32_t             state;
984     uint16_t            category        = 0;
985     RowType            *row;
986     UChar32             c;
987     int32_t             result          = 0;
988 
989     const RBBIStateTable *stateTable = fData->fReverseTable;
990     UTEXT_SETNATIVEINDEX(&fText, fromPosition);
991     #ifdef RBBI_DEBUG
992         if (gTrace) {
993             RBBIDebugPuts("Handle Previous   pos   char  state category");
994         }
995     #endif
996 
997     // if we're already at the start of the text, return DONE.
998     if (fData == NULL || UTEXT_GETNATIVEINDEX(&fText)==0) {
999         return BreakIterator::DONE;
1000     }
1001 
1002     //  Set the initial state for the state machine
1003     c = UTEXT_PREVIOUS32(&fText);
1004     state = START_STATE;
1005     row = (RowType *)
1006             (stateTable->fTableData + (stateTable->fRowLen * state));
1007 
1008     // loop until we reach the start of the text or transition to state 0
1009     //
1010     for (; c != U_SENTINEL; c = UTEXT_PREVIOUS32(&fText)) {
1011 
1012         // look up the current character's character category, which tells us
1013         // which column in the state table to look at.
1014         //
1015         //  Off the dictionary flag bit. For reverse iteration it is not used.
1016         category = trieFunc(fData->fTrie, c);
1017 
1018         #ifdef RBBI_DEBUG
1019             if (gTrace) {
1020                 RBBIDebugPrintf("             %4d   ", (int32_t)utext_getNativeIndex(&fText));
1021                 if (0x20<=c && c<0x7f) {
1022                     RBBIDebugPrintf("\"%c\"  ", c);
1023                 } else {
1024                     RBBIDebugPrintf("%5x  ", c);
1025                 }
1026                 RBBIDebugPrintf("%3d  %3d\n", state, category);
1027             }
1028         #endif
1029 
1030         // State Transition - move machine to its next state
1031         //
1032         // fNextState is a variable-length array.
1033         U_ASSERT(category<fData->fHeader->fCatCount);
1034         state = row->fNextState[category];  /*Not accessing beyond memory*/
1035         row = (RowType *)
1036             (stateTable->fTableData + (stateTable->fRowLen * state));
1037 
1038         if (state == STOP_STATE) {
1039             // This is the normal exit from the lookup state machine.
1040             // Transistion to state zero means we have found a safe point.
1041             break;
1042         }
1043     }
1044 
1045     // The state machine is done.  Check whether it found a match...
1046     result = (int32_t)UTEXT_GETNATIVEINDEX(&fText);
1047     #ifdef RBBI_DEBUG
1048         if (gTrace) {
1049             RBBIDebugPrintf("result = %d\n\n", result);
1050         }
1051     #endif
1052     return result;
1053 }
1054 
1055 
1056 //-------------------------------------------------------------------------------
1057 //
1058 //   getRuleStatus()   Return the break rule tag associated with the current
1059 //                     iterator position.  If the iterator arrived at its current
1060 //                     position by iterating forwards, the value will have been
1061 //                     cached by the handleNext() function.
1062 //
1063 //-------------------------------------------------------------------------------
1064 
getRuleStatus() const1065 int32_t  RuleBasedBreakIterator::getRuleStatus() const {
1066 
1067     // fLastRuleStatusIndex indexes to the start of the appropriate status record
1068     //                                                 (the number of status values.)
1069     //   This function returns the last (largest) of the array of status values.
1070     int32_t  idx = fRuleStatusIndex + fData->fRuleStatusTable[fRuleStatusIndex];
1071     int32_t  tagVal = fData->fRuleStatusTable[idx];
1072 
1073     return tagVal;
1074 }
1075 
1076 
getRuleStatusVec(int32_t * fillInVec,int32_t capacity,UErrorCode & status)1077 int32_t RuleBasedBreakIterator::getRuleStatusVec(
1078              int32_t *fillInVec, int32_t capacity, UErrorCode &status) {
1079     if (U_FAILURE(status)) {
1080         return 0;
1081     }
1082 
1083     int32_t  numVals = fData->fRuleStatusTable[fRuleStatusIndex];
1084     int32_t  numValsToCopy = numVals;
1085     if (numVals > capacity) {
1086         status = U_BUFFER_OVERFLOW_ERROR;
1087         numValsToCopy = capacity;
1088     }
1089     int i;
1090     for (i=0; i<numValsToCopy; i++) {
1091         fillInVec[i] = fData->fRuleStatusTable[fRuleStatusIndex + i + 1];
1092     }
1093     return numVals;
1094 }
1095 
1096 
1097 
1098 //-------------------------------------------------------------------------------
1099 //
1100 //   getBinaryRules        Access to the compiled form of the rules,
1101 //                         for use by build system tools that save the data
1102 //                         for standard iterator types.
1103 //
1104 //-------------------------------------------------------------------------------
getBinaryRules(uint32_t & length)1105 const uint8_t  *RuleBasedBreakIterator::getBinaryRules(uint32_t &length) {
1106     const uint8_t  *retPtr = NULL;
1107     length = 0;
1108 
1109     if (fData != NULL) {
1110         retPtr = (const uint8_t *)fData->fHeader;
1111         length = fData->fHeader->fLength;
1112     }
1113     return retPtr;
1114 }
1115 
1116 
createBufferClone(void *,int32_t & bufferSize,UErrorCode & status)1117 RuleBasedBreakIterator *RuleBasedBreakIterator::createBufferClone(
1118         void * /*stackBuffer*/, int32_t &bufferSize, UErrorCode &status) {
1119     if (U_FAILURE(status)){
1120         return NULL;
1121     }
1122 
1123     if (bufferSize == 0) {
1124         bufferSize = 1;  // preflighting for deprecated functionality
1125         return NULL;
1126     }
1127 
1128     BreakIterator *clonedBI = clone();
1129     if (clonedBI == NULL) {
1130         status = U_MEMORY_ALLOCATION_ERROR;
1131     } else {
1132         status = U_SAFECLONE_ALLOCATED_WARNING;
1133     }
1134     return (RuleBasedBreakIterator *)clonedBI;
1135 }
1136 
1137 U_NAMESPACE_END
1138 
1139 
1140 static icu::UStack *gLanguageBreakFactories = nullptr;
1141 static const icu::UnicodeString *gEmptyString = nullptr;
1142 static icu::UInitOnce gLanguageBreakFactoriesInitOnce = U_INITONCE_INITIALIZER;
1143 static icu::UInitOnce gRBBIInitOnce = U_INITONCE_INITIALIZER;
1144 
1145 /**
1146  * Release all static memory held by breakiterator.
1147  */
1148 U_CDECL_BEGIN
rbbi_cleanup(void)1149 UBool U_CALLCONV rbbi_cleanup(void) {
1150     delete gLanguageBreakFactories;
1151     gLanguageBreakFactories = nullptr;
1152     delete gEmptyString;
1153     gEmptyString = nullptr;
1154     gLanguageBreakFactoriesInitOnce.reset();
1155     gRBBIInitOnce.reset();
1156     return TRUE;
1157 }
1158 U_CDECL_END
1159 
1160 U_CDECL_BEGIN
_deleteFactory(void * obj)1161 static void U_CALLCONV _deleteFactory(void *obj) {
1162     delete (icu::LanguageBreakFactory *) obj;
1163 }
1164 U_CDECL_END
1165 U_NAMESPACE_BEGIN
1166 
rbbiInit()1167 static void U_CALLCONV rbbiInit() {
1168     gEmptyString = new UnicodeString();
1169     ucln_common_registerCleanup(UCLN_COMMON_RBBI, rbbi_cleanup);
1170 }
1171 
initLanguageFactories()1172 static void U_CALLCONV initLanguageFactories() {
1173     UErrorCode status = U_ZERO_ERROR;
1174     U_ASSERT(gLanguageBreakFactories == NULL);
1175     gLanguageBreakFactories = new UStack(_deleteFactory, NULL, status);
1176     if (gLanguageBreakFactories != NULL && U_SUCCESS(status)) {
1177         ICULanguageBreakFactory *builtIn = new ICULanguageBreakFactory(status);
1178         gLanguageBreakFactories->push(builtIn, status);
1179 #ifdef U_LOCAL_SERVICE_HOOK
1180         LanguageBreakFactory *extra = (LanguageBreakFactory *)uprv_svc_hook("languageBreakFactory", &status);
1181         if (extra != NULL) {
1182             gLanguageBreakFactories->push(extra, status);
1183         }
1184 #endif
1185     }
1186     ucln_common_registerCleanup(UCLN_COMMON_RBBI, rbbi_cleanup);
1187 }
1188 
1189 
1190 static const LanguageBreakEngine*
getLanguageBreakEngineFromFactory(UChar32 c)1191 getLanguageBreakEngineFromFactory(UChar32 c)
1192 {
1193     umtx_initOnce(gLanguageBreakFactoriesInitOnce, &initLanguageFactories);
1194     if (gLanguageBreakFactories == NULL) {
1195         return NULL;
1196     }
1197 
1198     int32_t i = gLanguageBreakFactories->size();
1199     const LanguageBreakEngine *lbe = NULL;
1200     while (--i >= 0) {
1201         LanguageBreakFactory *factory = (LanguageBreakFactory *)(gLanguageBreakFactories->elementAt(i));
1202         lbe = factory->getEngineFor(c);
1203         if (lbe != NULL) {
1204             break;
1205         }
1206     }
1207     return lbe;
1208 }
1209 
1210 
1211 //-------------------------------------------------------------------------------
1212 //
1213 //  getLanguageBreakEngine  Find an appropriate LanguageBreakEngine for the
1214 //                          the character c.
1215 //
1216 //-------------------------------------------------------------------------------
1217 const LanguageBreakEngine *
getLanguageBreakEngine(UChar32 c)1218 RuleBasedBreakIterator::getLanguageBreakEngine(UChar32 c) {
1219     const LanguageBreakEngine *lbe = NULL;
1220     UErrorCode status = U_ZERO_ERROR;
1221 
1222     if (fLanguageBreakEngines == NULL) {
1223         fLanguageBreakEngines = new UStack(status);
1224         if (fLanguageBreakEngines == NULL || U_FAILURE(status)) {
1225             delete fLanguageBreakEngines;
1226             fLanguageBreakEngines = 0;
1227             return NULL;
1228         }
1229     }
1230 
1231     int32_t i = fLanguageBreakEngines->size();
1232     while (--i >= 0) {
1233         lbe = (const LanguageBreakEngine *)(fLanguageBreakEngines->elementAt(i));
1234         if (lbe->handles(c)) {
1235             return lbe;
1236         }
1237     }
1238 
1239     // No existing dictionary took the character. See if a factory wants to
1240     // give us a new LanguageBreakEngine for this character.
1241     lbe = getLanguageBreakEngineFromFactory(c);
1242 
1243     // If we got one, use it and push it on our stack.
1244     if (lbe != NULL) {
1245         fLanguageBreakEngines->push((void *)lbe, status);
1246         // Even if we can't remember it, we can keep looking it up, so
1247         // return it even if the push fails.
1248         return lbe;
1249     }
1250 
1251     // No engine is forthcoming for this character. Add it to the
1252     // reject set. Create the reject break engine if needed.
1253     if (fUnhandledBreakEngine == NULL) {
1254         fUnhandledBreakEngine = new UnhandledEngine(status);
1255         if (U_SUCCESS(status) && fUnhandledBreakEngine == NULL) {
1256             status = U_MEMORY_ALLOCATION_ERROR;
1257             return nullptr;
1258         }
1259         // Put it last so that scripts for which we have an engine get tried
1260         // first.
1261         fLanguageBreakEngines->insertElementAt(fUnhandledBreakEngine, 0, status);
1262         // If we can't insert it, or creation failed, get rid of it
1263         if (U_FAILURE(status)) {
1264             delete fUnhandledBreakEngine;
1265             fUnhandledBreakEngine = 0;
1266             return NULL;
1267         }
1268     }
1269 
1270     // Tell the reject engine about the character; at its discretion, it may
1271     // add more than just the one character.
1272     fUnhandledBreakEngine->handleCharacter(c);
1273 
1274     return fUnhandledBreakEngine;
1275 }
1276 
dumpCache()1277 void RuleBasedBreakIterator::dumpCache() {
1278     fBreakCache->dumpCache();
1279 }
1280 
dumpTables()1281 void RuleBasedBreakIterator::dumpTables() {
1282     fData->printData();
1283 }
1284 
1285 /**
1286  * Returns the description used to create this iterator
1287  */
1288 
1289 const UnicodeString&
getRules() const1290 RuleBasedBreakIterator::getRules() const {
1291     if (fData != NULL) {
1292         return fData->getRuleSourceString();
1293     } else {
1294         umtx_initOnce(gRBBIInitOnce, &rbbiInit);
1295         return *gEmptyString;
1296     }
1297 }
1298 
1299 U_NAMESPACE_END
1300 
1301 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */
1302