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