2 ***************************************************************************
3 * Copyright (C) 1999-2004 International Business Machines Corporation *
4 * and others. All rights reserved. *
5 ***************************************************************************
7 **********************************************************************
8 * Date Name Description
9 * 10/22/99 alan Creation.
10 * 11/11/99 rgillam Complete port from Java.
11 **********************************************************************
17 #include "unicode/utypes.h"
19 #if !UCONFIG_NO_BREAK_ITERATION
21 #include "unicode/brkiter.h"
22 #include "unicode/udata.h"
23 #include "unicode/parseerr.h"
30 struct RBBIDataHeader
;
31 class RuleBasedBreakIteratorTables
;
33 class RBBIDataWrapper
;
34 struct RBBIStateTable
;
39 * A subclass of BreakIterator whose behavior is specified using a list of rules.
40 * <p>Instances of this class are most commonly created by the factory methods of
41 * BreakIterator::createWordInstance(), BreakIterator::createLineInstance(), etc.,
42 * and then used via the abstract API in class BreakIterator</p>
44 * <p>See the ICU User Guide for information on Break Iterator Rules.</p>
46 * <p>This class is not intended to be subclassed. (Class DictionaryBasedBreakIterator
47 * is a subclass, but that relationship is effectively internal to the ICU
48 * implementation. The subclassing interface to RulesBasedBreakIterator is
49 * not part of the ICU API, and may not remain stable.</p>
52 class U_COMMON_API RuleBasedBreakIterator
: public BreakIterator
{
56 * The character iterator through which this BreakIterator accesses the text
59 CharacterIterator
* fText
;
62 * The rule data for this BreakIterator instance
65 RBBIDataWrapper
*fData
;
67 /** Index of the Rule {tag} values for the most recent match.
70 int32_t fLastRuleStatusIndex
;
73 * Rule tag value valid flag.
74 * Some iterator operations don't intrinsically set the correct tag value.
75 * This flag lets us lazily compute the value if we are ever asked for it.
78 UBool fLastStatusIndexValid
;
81 * Counter for the number of characters encountered with the "dictionary"
82 * flag set. Normal RBBI iterators don't use it, although the code
83 * for updating it is live. Dictionary Based break iterators (a subclass
84 * of us) access this field directly.
87 uint32_t fDictionaryCharCount
;
90 * Debugging flag. Trace operation of state machine when true.
97 //=======================================================================
99 //=======================================================================
102 * Constructor from a flattened set of RBBI data in malloced memory.
103 * RulesBasedBreakIterators built from a custom set of rules
104 * are created via this constructor; the rules are compiled
105 * into memory, then the break iterator is constructed here.
107 * The break iterator adopts the memory, and will
111 RuleBasedBreakIterator(RBBIDataHeader
* data
, UErrorCode
&status
);
113 friend class RBBIRuleBuilder
; /** @internal */
114 friend class BreakIterator
;
120 /** Default constructor. Creates an empty shell of an iterator, with no
121 * rules or text to iterate over. Object can subsequently be assigned to.
124 RuleBasedBreakIterator();
127 * Copy constructor. Will produce a break iterator with the same behavior,
128 * and which iterates over the same text, as the one passed in.
129 * @param that The RuleBasedBreakIterator passed to be copied
132 RuleBasedBreakIterator(const RuleBasedBreakIterator
& that
);
135 * Construct a RuleBasedBreakIterator from a set of rules supplied as a string.
136 * @param rules The break rules to be used.
137 * @param parseError In the event of a syntax error in the rules, provides the location
138 * within the rules of the problem.
139 * @param status Information on any errors encountered.
142 RuleBasedBreakIterator( const UnicodeString
&rules
,
143 UParseError
&parseError
,
148 * This constructor uses the udata interface to create a BreakIterator
149 * whose internal tables live in a memory-mapped file. "image" is an
150 * ICU UDataMemory handle for the pre-compiled break iterator tables.
151 * @param image handle to the memory image for the break iterator data.
152 * Ownership of the UDataMemory handle passes to the Break Iterator,
153 * which will be responsible for closing it when it is no longer needed.
154 * @param status Information on any errors encountered.
156 * @see #getBinaryRules
159 RuleBasedBreakIterator(UDataMemory
* image
, UErrorCode
&status
);
165 virtual ~RuleBasedBreakIterator();
168 * Assignment operator. Sets this iterator to have the same behavior,
169 * and iterate over the same text, as the one passed in.
170 * @param that The RuleBasedBreakItertor passed in
171 * @return the newly created RuleBasedBreakIterator
174 RuleBasedBreakIterator
& operator=(const RuleBasedBreakIterator
& that
);
177 * Equality operator. Returns TRUE if both BreakIterators are of the
178 * same class, have the same behavior, and iterate over the same text.
179 * @param that The BreakIterator to be compared for equality
180 * @return TRUE if both BreakIterators are of the
181 * same class, have the same behavior, and iterate over the same text.
184 virtual UBool
operator==(const BreakIterator
& that
) const;
187 * Not-equal operator. If operator== returns TRUE, this returns FALSE,
189 * @param that The BreakIterator to be compared for inequality
190 * @return TRUE if both BreakIterators are not same.
193 UBool
operator!=(const BreakIterator
& that
) const;
196 * Returns a newly-constructed RuleBasedBreakIterator with the same
197 * behavior, and iterating over the same text, as this one.
198 * Differs from the copy constructor in that it is polymorphic, and
199 * will correctly clone (copy) a derived class.
200 * clone() is thread safe. Multiple threads may simultaeneously
201 * clone the same source break iterator.
202 * @return a newly-constructed RuleBasedBreakIterator
205 virtual BreakIterator
* clone() const;
208 * Compute a hash code for this BreakIterator
209 * @return A hash code
212 virtual int32_t hashCode(void) const;
215 * Returns the description used to create this iterator
216 * @return the description used to create this iterator
219 virtual const UnicodeString
& getRules(void) const;
221 //=======================================================================
222 // BreakIterator overrides
223 //=======================================================================
226 * Return a CharacterIterator over the text being analyzed. This version
227 * of this method returns the actual CharacterIterator we're using internally.
228 * Changing the state of this iterator can have undefined consequences. If
229 * you need to change it, clone it first.
230 * @return An iterator over the text being analyzed.
233 virtual const CharacterIterator
& getText(void) const;
237 * Set the iterator to analyze a new piece of text. This function resets
238 * the current iteration position to the beginning of the text.
239 * @param newText An iterator over the text to analyze. The BreakIterator
240 * takes ownership of the character iterator. The caller MUST NOT delete it!
243 virtual void adoptText(CharacterIterator
* newText
);
246 * Set the iterator to analyze a new piece of text. This function resets
247 * the current iteration position to the beginning of the text.
248 * @param newText The text to analyze.
251 virtual void setText(const UnicodeString
& newText
);
254 * Sets the current iteration position to the beginning of the text.
255 * (i.e., the CharacterIterator's starting offset).
256 * @return The offset of the beginning of the text.
259 virtual int32_t first(void);
262 * Sets the current iteration position to the end of the text.
263 * (i.e., the CharacterIterator's ending offset).
264 * @return The text's past-the-end offset.
267 virtual int32_t last(void);
270 * Advances the iterator either forward or backward the specified number of steps.
271 * Negative values move backward, and positive values move forward. This is
272 * equivalent to repeatedly calling next() or previous().
273 * @param n The number of steps to move. The sign indicates the direction
274 * (negative is backwards, and positive is forwards).
275 * @return The character offset of the boundary position n boundaries away from
279 virtual int32_t next(int32_t n
);
282 * Advances the iterator to the next boundary position.
283 * @return The position of the first boundary after this one.
286 virtual int32_t next(void);
289 * Moves the iterator backwards, to the last boundary preceding this one.
290 * @return The position of the last boundary position preceding this one.
293 virtual int32_t previous(void);
296 * Sets the iterator to refer to the first boundary position following
297 * the specified position.
298 * @param offset The position from which to begin searching for a break position.
299 * @return The position of the first break after the current position.
302 virtual int32_t following(int32_t offset
);
305 * Sets the iterator to refer to the last boundary position before the
306 * specified position.
307 * @param offset The position to begin searching for a break from.
308 * @return The position of the last boundary before the starting position.
311 virtual int32_t preceding(int32_t offset
);
314 * Returns true if the specfied position is a boundary position. As a side
315 * effect, leaves the iterator pointing to the first boundary position at
317 * @param offset the offset to check.
318 * @return True if "offset" is a boundary position.
321 virtual UBool
isBoundary(int32_t offset
);
324 * Returns the current iteration position.
325 * @return The current iteration position.
328 virtual int32_t current(void) const;
332 * Return the status tag from the break rule that determined the most recently
333 * returned break position. For break rules that do not specify a
334 * status, a default value of 0 is returned. If more than one break rule
335 * would cause a boundary to be located at some position in the text,
336 * the numerically largest of the applicable status values is returned.
338 * Of the standard types of ICU break iterators, only word break and
339 * line break provide status values. The values are defined in
340 * the header file ubrk.h. For Word breaks, the status allows distinguishing between words
341 * that contain alphabetic letters, "words" that appear to be numbers,
342 * punctuation and spaces, words containing ideographic characters, and
343 * more. For Line Break, the status distinguishes between hard (mandatory) breaks
344 * and soft (potential) break positions.
346 * <code>getRuleStatus()</code> can be called after obtaining a boundary
347 * position from <code>next()</code>, <code>previous()</code>, or
348 * any other break iterator functions that returns a boundary position.
350 * When creating custom break rules, one is free to define whatever
351 * status values may be convenient for the application.
353 * Note: this function is not thread safe. It should not have been
354 * declared const, and the const remains only for compatibility
355 * reasons. (The function is logically const, but not bit-wise const).
357 * @return the status from the break rule that determined the most recently
358 * returned break position.
363 virtual int32_t getRuleStatus() const;
366 * Get the status (tag) values from the break rule(s) that determined the most
367 * recently returned break position.
369 * The returned status value(s) are stored into an array provided by the caller.
370 * The values are stored in sorted (ascending) order.
371 * If the capacity of the output array is insufficient to hold the data,
372 * the output will be truncated to the available length, and a
373 * U_BUFFER_OVERFLOW_ERROR will be signaled.
375 * @param fillInVec an array to be filled in with the status values.
376 * @param capacity the length of the supplied vector. A length of zero causes
377 * the function to return the number of status values, in the
378 * normal way, without attemtping to store any values.
379 * @param status receives error codes.
380 * @return The number of rule status values from rules that determined
381 * the most recent boundary returned by the break iterator.
382 * In the event of a U_BUFFER_OVERFLOW_ERROR, the return value
383 * is the total number of status values that were available,
384 * not the reduced number that were actually returned.
388 virtual int32_t getRuleStatusVec(int32_t *fillInVec
, int32_t capacity
, UErrorCode
&status
);
391 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override.
392 * This method is to implement a simple version of RTTI, since not all
393 * C++ compilers support genuine RTTI. Polymorphic operator==() and
394 * clone() methods call this method.
396 * @return The class ID for this object. All objects of a
397 * given class have the same class ID. Objects of
398 * other classes have different class IDs.
401 virtual UClassID
getDynamicClassID(void) const;
404 * Returns the class ID for this class. This is useful only for
405 * comparing to a return value from getDynamicClassID(). For example:
407 * Base* polymorphic_pointer = createPolymorphicObject();
408 * if (polymorphic_pointer->getDynamicClassID() ==
409 * Derived::getStaticClassID()) ...
411 * @return The class ID for all objects of this class.
414 static UClassID U_EXPORT2
getStaticClassID(void);
417 * Create a clone (copy) of this break iterator in memory provided
418 * by the caller. The idea is to increase performance by avoiding
419 * a storage allocation. Use of this functoin is NOT RECOMMENDED.
420 * Performance gains are minimal, and correct buffer management is
421 * tricky. Use clone() instead.
423 * @param stackBuffer The pointer to the memory into which the cloned object
424 * should be placed. If NULL, allocate heap memory
425 * for the cloned object.
426 * @param BufferSize The size of the buffer. If zero, return the required
427 * buffer size, but do not clone the object. If the
428 * size was too small (but not zero), allocate heap
429 * storage for the cloned object.
431 * @param status Error status. U_SAFECLONE_ALLOCATED_WARNING will be
432 * returned if the the provided buffer was too small, and
433 * the clone was therefore put on the heap.
435 * @return Pointer to the clone object. This may differ from the stackBuffer
436 * address if the byte alignment of the stack buffer was not suitable
437 * or if the stackBuffer was too small to hold the clone.
440 virtual BreakIterator
* createBufferClone(void *stackBuffer
,
446 * Return the binary form of compiled break rules,
447 * which can then be used to create a new break iterator at some
448 * time in the future. Creating a break iterator from pre-compiled rules
449 * is much faster than building one from the source form of the
452 * The binary data can only be used with the same version of ICU
453 * and on the same platform type (processor endian-ness)
455 * @param length Returns the length of the binary data. (Out paramter.)
457 * @return A pointer to the binary (compiled) rule data. The storage
458 * belongs to the RulesBasedBreakIterator object, not the
459 * caller, and must not be modified or deleted.
462 virtual const uint8_t *getBinaryRules(uint32_t &length
);
466 //=======================================================================
468 //=======================================================================
470 * This method is the actual implementation of the next() method. All iteration
471 * vectors through here. This method initializes the state machine to state 1
472 * and advances through the text character by character until we reach the end
473 * of the text or the state machine transitions to state 0. We update our return
474 * value every time the state machine passes through a possible end state.
477 virtual int32_t handleNext(void);
480 * This method backs the iterator back up to a "safe position" in the text.
481 * This is a position that we know, without any context, must be a break position.
482 * The various calling methods then iterate forward from this safe position to
483 * the appropriate position to return. (For more information, see the description
484 * of buildBackwardsStateTable() in RuleBasedBreakIterator.Builder.)
487 virtual int32_t handlePrevious(void);
490 * Dumps caches and performs other actions associated with a complete change
491 * in text or iteration position. This function is a no-op in RuleBasedBreakIterator,
492 * but subclasses can and do override it.
495 virtual void reset(void);
498 * Return true if the category lookup for this char
499 * indicates that it is in the set of dictionary lookup chars.
500 * This function is intended for use by dictionary based break iterators.
501 * @return true if the category lookup for this char
502 * indicates that it is in the set of dictionary lookup chars.
505 virtual UBool
isDictionaryChar(UChar32
);
508 * Common initialization function, used by constructors and bufferClone.
509 * (Also used by DictionaryBasedBreakIterator::createBufferClone().)
517 * This method backs the iterator back up to a "safe position" in the text.
518 * This is a position that we know, without any context, must be a break position.
519 * The various calling methods then iterate forward from this safe position to
520 * the appropriate position to return. (For more information, see the description
521 * of buildBackwardsStateTable() in RuleBasedBreakIterator.Builder.)
522 * @param statetable state table used of moving backwards
525 int32_t handlePrevious(const RBBIStateTable
*statetable
);
528 * This method is the actual implementation of the next() method. All iteration
529 * vectors through here. This method initializes the state machine to state 1
530 * and advances through the text character by character until we reach the end
531 * of the text or the state machine transitions to state 0. We update our return
532 * value every time the state machine passes through a possible end state.
533 * @param statetable state table used of moving forwards
536 int32_t handleNext(const RBBIStateTable
*statetable
);
541 void makeRuleStatusValid();
545 //------------------------------------------------------------------------------
547 // Inline Functions Definitions ...
549 //------------------------------------------------------------------------------
551 inline UBool
RuleBasedBreakIterator::operator!=(const BreakIterator
& that
) const {
552 return !operator==(that
);
557 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */