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