sl@0: /* sl@0: *************************************************************************** sl@0: * Copyright (C) 1999-2005 International Business Machines Corporation * sl@0: * and others. All rights reserved. * sl@0: *************************************************************************** sl@0: sl@0: ********************************************************************** sl@0: * Date Name Description sl@0: * 10/22/99 alan Creation. sl@0: * 11/11/99 rgillam Complete port from Java. sl@0: ********************************************************************** sl@0: */ sl@0: sl@0: #ifndef RBBI_H sl@0: #define RBBI_H sl@0: sl@0: #include "unicode/utypes.h" sl@0: sl@0: /** sl@0: * \file sl@0: * \brief C++ API: Rule Based Break Iterator sl@0: */ sl@0: sl@0: #if !UCONFIG_NO_BREAK_ITERATION sl@0: sl@0: #include "unicode/brkiter.h" sl@0: #include "unicode/udata.h" sl@0: #include "unicode/parseerr.h" sl@0: sl@0: sl@0: struct UTrie; sl@0: sl@0: U_NAMESPACE_BEGIN sl@0: sl@0: /** @internal */ sl@0: struct RBBIDataHeader; sl@0: class RuleBasedBreakIteratorTables; sl@0: class BreakIterator; sl@0: class RBBIDataWrapper; sl@0: struct RBBIStateTable; sl@0: sl@0: sl@0: sl@0: /** sl@0: * sl@0: * A subclass of BreakIterator whose behavior is specified using a list of rules. sl@0: *
Instances of this class are most commonly created by the factory methods of sl@0: * BreakIterator::createWordInstance(), BreakIterator::createLineInstance(), etc., sl@0: * and then used via the abstract API in class BreakIterator
sl@0: * sl@0: *See the ICU User Guide for information on Break Iterator Rules.
sl@0: * sl@0: *This class is not intended to be subclassed. (Class DictionaryBasedBreakIterator sl@0: * is a subclass, but that relationship is effectively internal to the ICU sl@0: * implementation. The subclassing interface to RulesBasedBreakIterator is sl@0: * not part of the ICU API, and may not remain stable.
sl@0: * sl@0: */ sl@0: class U_COMMON_API RuleBasedBreakIterator : public BreakIterator { sl@0: sl@0: protected: sl@0: /** sl@0: * The character iterator through which this BreakIterator accesses the text sl@0: * @internal sl@0: */ sl@0: CharacterIterator* fText; sl@0: sl@0: /** sl@0: * The rule data for this BreakIterator instance sl@0: * @internal sl@0: */ sl@0: RBBIDataWrapper *fData; sl@0: sl@0: /** Index of the Rule {tag} values for the most recent match. sl@0: * @internal sl@0: */ sl@0: int32_t fLastRuleStatusIndex; sl@0: sl@0: /** sl@0: * Rule tag value valid flag. sl@0: * Some iterator operations don't intrinsically set the correct tag value. sl@0: * This flag lets us lazily compute the value if we are ever asked for it. sl@0: * @internal sl@0: */ sl@0: UBool fLastStatusIndexValid; sl@0: sl@0: /** sl@0: * Counter for the number of characters encountered with the "dictionary" sl@0: * flag set. Normal RBBI iterators don't use it, although the code sl@0: * for updating it is live. Dictionary Based break iterators (a subclass sl@0: * of us) access this field directly. sl@0: * @internal sl@0: */ sl@0: uint32_t fDictionaryCharCount; sl@0: sl@0: /** sl@0: * Debugging flag. Trace operation of state machine when true. sl@0: * @internal sl@0: */ sl@0: static UBool fTrace; sl@0: sl@0: sl@0: protected: sl@0: //======================================================================= sl@0: // constructors sl@0: //======================================================================= sl@0: sl@0: /** sl@0: * Constructor from a flattened set of RBBI data in malloced memory. sl@0: * RulesBasedBreakIterators built from a custom set of rules sl@0: * are created via this constructor; the rules are compiled sl@0: * into memory, then the break iterator is constructed here. sl@0: * sl@0: * The break iterator adopts the memory, and will sl@0: * free it when done. sl@0: * @internal sl@0: */ sl@0: RuleBasedBreakIterator(RBBIDataHeader* data, UErrorCode &status); sl@0: sl@0: /** @internal */ sl@0: friend class RBBIRuleBuilder; sl@0: /** @internal */ sl@0: friend class BreakIterator; sl@0: sl@0: sl@0: sl@0: public: sl@0: sl@0: /** Default constructor. Creates an empty shell of an iterator, with no sl@0: * rules or text to iterate over. Object can subsequently be assigned to. sl@0: * @stable ICU 2.2 sl@0: */ sl@0: RuleBasedBreakIterator(); sl@0: sl@0: /** sl@0: * Copy constructor. Will produce a break iterator with the same behavior, sl@0: * and which iterates over the same text, as the one passed in. sl@0: * @param that The RuleBasedBreakIterator passed to be copied sl@0: * @stable ICU 2.0 sl@0: */ sl@0: RuleBasedBreakIterator(const RuleBasedBreakIterator& that); sl@0: sl@0: /** sl@0: * Construct a RuleBasedBreakIterator from a set of rules supplied as a string. sl@0: * @param rules The break rules to be used. sl@0: * @param parseError In the event of a syntax error in the rules, provides the location sl@0: * within the rules of the problem. sl@0: * @param status Information on any errors encountered. sl@0: * @stable ICU 2.2 sl@0: */ sl@0: RuleBasedBreakIterator( const UnicodeString &rules, sl@0: UParseError &parseError, sl@0: UErrorCode &status); sl@0: sl@0: sl@0: /** sl@0: * This constructor uses the udata interface to create a BreakIterator sl@0: * whose internal tables live in a memory-mapped file. "image" is an sl@0: * ICU UDataMemory handle for the pre-compiled break iterator tables. sl@0: * @param image handle to the memory image for the break iterator data. sl@0: * Ownership of the UDataMemory handle passes to the Break Iterator, sl@0: * which will be responsible for closing it when it is no longer needed. sl@0: * @param status Information on any errors encountered. sl@0: * @see udata_open sl@0: * @see #getBinaryRules sl@0: * @stable ICU 2.8 sl@0: */ sl@0: RuleBasedBreakIterator(UDataMemory* image, UErrorCode &status); sl@0: sl@0: /** sl@0: * Destructor sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual ~RuleBasedBreakIterator(); sl@0: sl@0: /** sl@0: * Assignment operator. Sets this iterator to have the same behavior, sl@0: * and iterate over the same text, as the one passed in. sl@0: * @param that The RuleBasedBreakItertor passed in sl@0: * @return the newly created RuleBasedBreakIterator sl@0: * @stable ICU 2.0 sl@0: */ sl@0: RuleBasedBreakIterator& operator=(const RuleBasedBreakIterator& that); sl@0: sl@0: /** sl@0: * Equality operator. Returns TRUE if both BreakIterators are of the sl@0: * same class, have the same behavior, and iterate over the same text. sl@0: * @param that The BreakIterator to be compared for equality sl@0: * @return TRUE if both BreakIterators are of the sl@0: * same class, have the same behavior, and iterate over the same text. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UBool operator==(const BreakIterator& that) const; sl@0: sl@0: /** sl@0: * Not-equal operator. If operator== returns TRUE, this returns FALSE, sl@0: * and vice versa. sl@0: * @param that The BreakIterator to be compared for inequality sl@0: * @return TRUE if both BreakIterators are not same. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: UBool operator!=(const BreakIterator& that) const; sl@0: sl@0: /** sl@0: * Returns a newly-constructed RuleBasedBreakIterator with the same sl@0: * behavior, and iterating over the same text, as this one. sl@0: * Differs from the copy constructor in that it is polymorphic, and sl@0: * will correctly clone (copy) a derived class. sl@0: * clone() is thread safe. Multiple threads may simultaeneously sl@0: * clone the same source break iterator. sl@0: * @return a newly-constructed RuleBasedBreakIterator sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual BreakIterator* clone() const; sl@0: sl@0: /** sl@0: * Compute a hash code for this BreakIterator sl@0: * @return A hash code sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual int32_t hashCode(void) const; sl@0: sl@0: /** sl@0: * Returns the description used to create this iterator sl@0: * @return the description used to create this iterator sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual const UnicodeString& getRules(void) const; sl@0: sl@0: //======================================================================= sl@0: // BreakIterator overrides sl@0: //======================================================================= sl@0: sl@0: /** sl@0: * Return a CharacterIterator over the text being analyzed. This version sl@0: * of this method returns the actual CharacterIterator we're using internally. sl@0: * Changing the state of this iterator can have undefined consequences. If sl@0: * you need to change it, clone it first. sl@0: * @return An iterator over the text being analyzed. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual const CharacterIterator& getText(void) const; sl@0: sl@0: sl@0: /** sl@0: * Get a UText for the text being analyzed. sl@0: * The returned UText is a shallow clone of the UText used internally sl@0: * by the break iterator implementation. It can safely be used to sl@0: * access the text without impacting any break iterator operations, sl@0: * but the underlying text itself must not be altered. sl@0: * sl@0: * @param fillIn A UText to be filled in. If NULL, a new UText will be sl@0: * allocated to hold the result. sl@0: * @param status receives any error codes. sl@0: * @return The current UText for this break iterator. If an input sl@0: * UText was provided, it will always be returned. sl@0: * @draft ICU 3.4 sl@0: */ sl@0: virtual UText *getUText(UText *fillIn, UErrorCode &status) const; sl@0: sl@0: /** sl@0: * Set the iterator to analyze a new piece of text. This function resets sl@0: * the current iteration position to the beginning of the text. sl@0: * @param newText An iterator over the text to analyze. The BreakIterator sl@0: * takes ownership of the character iterator. The caller MUST NOT delete it! sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual void adoptText(CharacterIterator* newText); sl@0: sl@0: /** sl@0: * Set the iterator to analyze a new piece of text. This function resets sl@0: * the current iteration position to the beginning of the text. sl@0: * @param newText The text to analyze. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual void setText(const UnicodeString& newText); sl@0: sl@0: /** sl@0: * Reset the break iterator to operate over the text represented by sl@0: * the UText. The iterator position is reset to the start. sl@0: * sl@0: * This function makes a shallow clone of the supplied UText. This means sl@0: * that the caller is free to immediately close or otherwise reuse the sl@0: * Utext that was passed as a parameter, but that the underlying text itself sl@0: * must not be altered while being referenced by the break iterator. sl@0: * sl@0: * @param text The UText used to change the text. sl@0: * @param status Receives any error codes. sl@0: * @draft ICU 3.4 sl@0: */ sl@0: virtual void setText(UText *text, UErrorCode &status); sl@0: sl@0: /** sl@0: * Sets the current iteration position to the beginning of the text. sl@0: * (i.e., the CharacterIterator's starting offset). sl@0: * @return The offset of the beginning of the text. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual int32_t first(void); sl@0: sl@0: /** sl@0: * Sets the current iteration position to the end of the text. sl@0: * (i.e., the CharacterIterator's ending offset). sl@0: * @return The text's past-the-end offset. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual int32_t last(void); sl@0: sl@0: /** sl@0: * Advances the iterator either forward or backward the specified number of steps. sl@0: * Negative values move backward, and positive values move forward. This is sl@0: * equivalent to repeatedly calling next() or previous(). sl@0: * @param n The number of steps to move. The sign indicates the direction sl@0: * (negative is backwards, and positive is forwards). sl@0: * @return The character offset of the boundary position n boundaries away from sl@0: * the current one. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual int32_t next(int32_t n); sl@0: sl@0: /** sl@0: * Advances the iterator to the next boundary position. sl@0: * @return The position of the first boundary after this one. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual int32_t next(void); sl@0: sl@0: /** sl@0: * Moves the iterator backwards, to the last boundary preceding this one. sl@0: * @return The position of the last boundary position preceding this one. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual int32_t previous(void); sl@0: sl@0: /** sl@0: * Sets the iterator to refer to the first boundary position following sl@0: * the specified position. sl@0: * @param offset The position from which to begin searching for a break position. sl@0: * @return The position of the first break after the current position. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual int32_t following(int32_t offset); sl@0: sl@0: /** sl@0: * Sets the iterator to refer to the last boundary position before the sl@0: * specified position. sl@0: * @param offset The position to begin searching for a break from. sl@0: * @return The position of the last boundary before the starting position. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual int32_t preceding(int32_t offset); sl@0: sl@0: /** sl@0: * Returns true if the specfied position is a boundary position. As a side sl@0: * effect, leaves the iterator pointing to the first boundary position at sl@0: * or after "offset". sl@0: * @param offset the offset to check. sl@0: * @return True if "offset" is a boundary position. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UBool isBoundary(int32_t offset); sl@0: sl@0: /** sl@0: * Returns the current iteration position. sl@0: * @return The current iteration position. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual int32_t current(void) const; sl@0: sl@0: sl@0: /** sl@0: * Return the status tag from the break rule that determined the most recently sl@0: * returned break position. For break rules that do not specify a sl@0: * status, a default value of 0 is returned. If more than one break rule sl@0: * would cause a boundary to be located at some position in the text, sl@0: * the numerically largest of the applicable status values is returned. sl@0: *sl@0: * Of the standard types of ICU break iterators, only word break and sl@0: * line break provide status values. The values are defined in sl@0: * the header file ubrk.h. For Word breaks, the status allows distinguishing between words sl@0: * that contain alphabetic letters, "words" that appear to be numbers, sl@0: * punctuation and spaces, words containing ideographic characters, and sl@0: * more. For Line Break, the status distinguishes between hard (mandatory) breaks sl@0: * and soft (potential) break positions. sl@0: *
sl@0: * getRuleStatus()
can be called after obtaining a boundary
sl@0: * position from next()
, previous()
, or
sl@0: * any other break iterator functions that returns a boundary position.
sl@0: *
sl@0: * When creating custom break rules, one is free to define whatever sl@0: * status values may be convenient for the application. sl@0: *
sl@0: * Note: this function is not thread safe. It should not have been sl@0: * declared const, and the const remains only for compatibility sl@0: * reasons. (The function is logically const, but not bit-wise const). sl@0: *
sl@0: * @return the status from the break rule that determined the most recently sl@0: * returned break position. sl@0: * sl@0: * @see UWordBreak sl@0: * @stable ICU 2.2 sl@0: */ sl@0: virtual int32_t getRuleStatus() const; sl@0: sl@0: /** sl@0: * Get the status (tag) values from the break rule(s) that determined the most sl@0: * recently returned break position. sl@0: *
sl@0: * The returned status value(s) are stored into an array provided by the caller. sl@0: * The values are stored in sorted (ascending) order. sl@0: * If the capacity of the output array is insufficient to hold the data, sl@0: * the output will be truncated to the available length, and a sl@0: * U_BUFFER_OVERFLOW_ERROR will be signaled. sl@0: * sl@0: * @param fillInVec an array to be filled in with the status values. sl@0: * @param capacity the length of the supplied vector. A length of zero causes sl@0: * the function to return the number of status values, in the sl@0: * normal way, without attemtping to store any values. sl@0: * @param status receives error codes. sl@0: * @return The number of rule status values from rules that determined sl@0: * the most recent boundary returned by the break iterator. sl@0: * In the event of a U_BUFFER_OVERFLOW_ERROR, the return value sl@0: * is the total number of status values that were available, sl@0: * not the reduced number that were actually returned. sl@0: * @see getRuleStatus sl@0: * @draft ICU 3.0 sl@0: */ sl@0: virtual int32_t getRuleStatusVec(int32_t *fillInVec, int32_t capacity, UErrorCode &status); sl@0: sl@0: /** sl@0: * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. sl@0: * This method is to implement a simple version of RTTI, since not all sl@0: * C++ compilers support genuine RTTI. Polymorphic operator==() and sl@0: * clone() methods call this method. sl@0: * sl@0: * @return The class ID for this object. All objects of a sl@0: * given class have the same class ID. Objects of sl@0: * other classes have different class IDs. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UClassID getDynamicClassID(void) const; sl@0: sl@0: /** sl@0: * Returns the class ID for this class. This is useful only for sl@0: * comparing to a return value from getDynamicClassID(). For example: sl@0: * sl@0: * Base* polymorphic_pointer = createPolymorphicObject(); sl@0: * if (polymorphic_pointer->getDynamicClassID() == sl@0: * Derived::getStaticClassID()) ... sl@0: * sl@0: * @return The class ID for all objects of this class. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: static UClassID U_EXPORT2 getStaticClassID(void); sl@0: sl@0: /* sl@0: * Create a clone (copy) of this break iterator in memory provided sl@0: * by the caller. The idea is to increase performance by avoiding sl@0: * a storage allocation. Use of this functoin is NOT RECOMMENDED. sl@0: * Performance gains are minimal, and correct buffer management is sl@0: * tricky. Use clone() instead. sl@0: * sl@0: * @param stackBuffer The pointer to the memory into which the cloned object sl@0: * should be placed. If NULL, allocate heap memory sl@0: * for the cloned object. sl@0: * @param BufferSize The size of the buffer. If zero, return the required sl@0: * buffer size, but do not clone the object. If the sl@0: * size was too small (but not zero), allocate heap sl@0: * storage for the cloned object. sl@0: * sl@0: * @param status Error status. U_SAFECLONE_ALLOCATED_WARNING will be sl@0: * returned if the the provided buffer was too small, and sl@0: * the clone was therefore put on the heap. sl@0: * sl@0: * @return Pointer to the clone object. This may differ from the stackBuffer sl@0: * address if the byte alignment of the stack buffer was not suitable sl@0: * or if the stackBuffer was too small to hold the clone. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual BreakIterator * createBufferClone(void *stackBuffer, sl@0: int32_t &BufferSize, sl@0: UErrorCode &status); sl@0: sl@0: sl@0: /** sl@0: * Return the binary form of compiled break rules, sl@0: * which can then be used to create a new break iterator at some sl@0: * time in the future. Creating a break iterator from pre-compiled rules sl@0: * is much faster than building one from the source form of the sl@0: * break rules. sl@0: * sl@0: * The binary data can only be used with the same version of ICU sl@0: * and on the same platform type (processor endian-ness) sl@0: * sl@0: * @param length Returns the length of the binary data. (Out paramter.) sl@0: * sl@0: * @return A pointer to the binary (compiled) rule data. The storage sl@0: * belongs to the RulesBasedBreakIterator object, not the sl@0: * caller, and must not be modified or deleted. sl@0: * @internal sl@0: */ sl@0: virtual const uint8_t *getBinaryRules(uint32_t &length); sl@0: sl@0: sl@0: protected: sl@0: //======================================================================= sl@0: // implementation sl@0: //======================================================================= sl@0: /** sl@0: * This method is the actual implementation of the next() method. All iteration sl@0: * vectors through here. This method initializes the state machine to state 1 sl@0: * and advances through the text character by character until we reach the end sl@0: * of the text or the state machine transitions to state 0. We update our return sl@0: * value every time the state machine passes through a possible end state. sl@0: * @internal sl@0: */ sl@0: virtual int32_t handleNext(void); sl@0: sl@0: /** sl@0: * This method backs the iterator back up to a "safe position" in the text. sl@0: * This is a position that we know, without any context, must be a break position. sl@0: * The various calling methods then iterate forward from this safe position to sl@0: * the appropriate position to return. (For more information, see the description sl@0: * of buildBackwardsStateTable() in RuleBasedBreakIterator.Builder.) sl@0: * @internal sl@0: */ sl@0: virtual int32_t handlePrevious(void); sl@0: sl@0: /** sl@0: * Dumps caches and performs other actions associated with a complete change sl@0: * in text or iteration position. This function is a no-op in RuleBasedBreakIterator, sl@0: * but subclasses can and do override it. sl@0: * @internal sl@0: */ sl@0: virtual void reset(void); sl@0: sl@0: /** sl@0: * Return true if the category lookup for this char sl@0: * indicates that it is in the set of dictionary lookup chars. sl@0: * This function is intended for use by dictionary based break iterators. sl@0: * @return true if the category lookup for this char sl@0: * indicates that it is in the set of dictionary lookup chars. sl@0: * @internal sl@0: */ sl@0: virtual UBool isDictionaryChar(UChar32); sl@0: sl@0: /** sl@0: * Common initialization function, used by constructors and bufferClone. sl@0: * (Also used by DictionaryBasedBreakIterator::createBufferClone().) sl@0: * @internal sl@0: */ sl@0: void init(); sl@0: sl@0: private: sl@0: sl@0: /** sl@0: * This method backs the iterator back up to a "safe position" in the text. sl@0: * This is a position that we know, without any context, must be a break position. sl@0: * The various calling methods then iterate forward from this safe position to sl@0: * the appropriate position to return. (For more information, see the description sl@0: * of buildBackwardsStateTable() in RuleBasedBreakIterator.Builder.) sl@0: * @param statetable state table used of moving backwards sl@0: * @internal sl@0: */ sl@0: int32_t handlePrevious(const RBBIStateTable *statetable); sl@0: sl@0: /** sl@0: * This method is the actual implementation of the next() method. All iteration sl@0: * vectors through here. This method initializes the state machine to state 1 sl@0: * and advances through the text character by character until we reach the end sl@0: * of the text or the state machine transitions to state 0. We update our return sl@0: * value every time the state machine passes through a possible end state. sl@0: * @param statetable state table used of moving forwards sl@0: * @internal sl@0: */ sl@0: int32_t handleNext(const RBBIStateTable *statetable); sl@0: sl@0: /** sl@0: * @internal sl@0: */ sl@0: void makeRuleStatusValid(); sl@0: sl@0: }; sl@0: sl@0: //------------------------------------------------------------------------------ sl@0: // sl@0: // Inline Functions Definitions ... sl@0: // sl@0: //------------------------------------------------------------------------------ sl@0: sl@0: inline UBool RuleBasedBreakIterator::operator!=(const BreakIterator& that) const { sl@0: return !operator==(that); sl@0: } sl@0: sl@0: U_NAMESPACE_END sl@0: sl@0: #endif /* #if !UCONFIG_NO_BREAK_ITERATION */ sl@0: sl@0: #endif