Coverage Report

Created: 2026-10-31 06:17

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