michael@0: /* michael@0: ********************************************************************** michael@0: * Copyright (C) 1998-2005, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ********************************************************************** michael@0: */ michael@0: michael@0: #ifndef UCHRITER_H michael@0: #define UCHRITER_H michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/chariter.h" michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C++ API: UChar Character Iterator michael@0: */ michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: /** michael@0: * A concrete subclass of CharacterIterator that iterates over the michael@0: * characters (code units or code points) in a UChar array. michael@0: * It's possible not only to create an michael@0: * iterator that iterates over an entire UChar array, but also to michael@0: * create one that iterates over only a subrange of a UChar array michael@0: * (iterators over different subranges of the same UChar array don't michael@0: * compare equal). michael@0: * @see CharacterIterator michael@0: * @see ForwardCharacterIterator michael@0: * @stable ICU 2.0 michael@0: */ michael@0: class U_COMMON_API UCharCharacterIterator : public CharacterIterator { michael@0: public: michael@0: /** michael@0: * Create an iterator over the UChar array referred to by "textPtr". michael@0: * The iteration range is 0 to length-1. michael@0: * text is only aliased, not adopted (the michael@0: * destructor will not delete it). michael@0: * @param textPtr The UChar array to be iterated over michael@0: * @param length The length of the UChar array michael@0: * @stable ICU 2.0 michael@0: */ michael@0: UCharCharacterIterator(const UChar* textPtr, int32_t length); michael@0: michael@0: /** michael@0: * Create an iterator over the UChar array referred to by "textPtr". michael@0: * The iteration range is 0 to length-1. michael@0: * text is only aliased, not adopted (the michael@0: * destructor will not delete it). michael@0: * The starting michael@0: * position is specified by "position". If "position" is outside the valid michael@0: * iteration range, the behavior of this object is undefined. michael@0: * @param textPtr The UChar array to be iteratd over michael@0: * @param length The length of the UChar array michael@0: * @param position The starting position of the iteration michael@0: * @stable ICU 2.0 michael@0: */ michael@0: UCharCharacterIterator(const UChar* textPtr, int32_t length, michael@0: int32_t position); michael@0: michael@0: /** michael@0: * Create an iterator over the UChar array referred to by "textPtr". michael@0: * The iteration range is 0 to end-1. michael@0: * text is only aliased, not adopted (the michael@0: * destructor will not delete it). michael@0: * The starting michael@0: * position is specified by "position". If begin and end do not michael@0: * form a valid iteration range or "position" is outside the valid michael@0: * iteration range, the behavior of this object is undefined. michael@0: * @param textPtr The UChar array to be iterated over michael@0: * @param length The length of the UChar array michael@0: * @param textBegin The begin position of the iteration range michael@0: * @param textEnd The end position of the iteration range michael@0: * @param position The starting position of the iteration michael@0: * @stable ICU 2.0 michael@0: */ michael@0: UCharCharacterIterator(const UChar* textPtr, int32_t length, michael@0: int32_t textBegin, michael@0: int32_t textEnd, michael@0: int32_t position); michael@0: michael@0: /** michael@0: * Copy constructor. The new iterator iterates over the same range michael@0: * of the same string as "that", and its initial position is the michael@0: * same as "that"'s current position. michael@0: * @param that The UCharCharacterIterator to be copied michael@0: * @stable ICU 2.0 michael@0: */ michael@0: UCharCharacterIterator(const UCharCharacterIterator& that); michael@0: michael@0: /** michael@0: * Destructor. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual ~UCharCharacterIterator(); michael@0: michael@0: /** michael@0: * Assignment operator. *this is altered to iterate over the sane michael@0: * range of the same string as "that", and refers to the same michael@0: * character within that string as "that" does. michael@0: * @param that The object to be copied michael@0: * @return the newly created object michael@0: * @stable ICU 2.0 michael@0: */ michael@0: UCharCharacterIterator& michael@0: operator=(const UCharCharacterIterator& that); michael@0: michael@0: /** michael@0: * Returns true if the iterators iterate over the same range of the michael@0: * same string and are pointing at the same character. michael@0: * @param that The ForwardCharacterIterator used to be compared for equality michael@0: * @return true if the iterators iterate over the same range of the michael@0: * same string and are pointing at the same character. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual UBool operator==(const ForwardCharacterIterator& that) const; michael@0: michael@0: /** michael@0: * Generates a hash code for this iterator. michael@0: * @return the hash code. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual int32_t hashCode(void) const; michael@0: michael@0: /** michael@0: * Returns a new UCharCharacterIterator referring to the same michael@0: * character in the same range of the same string as this one. The michael@0: * caller must delete the new iterator. michael@0: * @return the CharacterIterator newly created michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual CharacterIterator* clone(void) const; michael@0: michael@0: /** michael@0: * Sets the iterator to refer to the first code unit in its michael@0: * iteration range, and returns that code unit. michael@0: * This can be used to begin an iteration with next(). michael@0: * @return the first code unit in its iteration range. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual UChar first(void); michael@0: michael@0: /** michael@0: * Sets the iterator to refer to the first code unit in its michael@0: * iteration range, returns that code unit, and moves the position michael@0: * to the second code unit. This is an alternative to setToStart() michael@0: * for forward iteration with nextPostInc(). michael@0: * @return the first code unit in its iteration range michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual UChar firstPostInc(void); michael@0: michael@0: /** michael@0: * Sets the iterator to refer to the first code point in its michael@0: * iteration range, and returns that code unit, michael@0: * This can be used to begin an iteration with next32(). michael@0: * Note that an iteration with next32PostInc(), beginning with, michael@0: * e.g., setToStart() or firstPostInc(), is more efficient. michael@0: * @return the first code point in its iteration range michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual UChar32 first32(void); michael@0: michael@0: /** michael@0: * Sets the iterator to refer to the first code point in its michael@0: * iteration range, returns that code point, and moves the position michael@0: * to the second code point. This is an alternative to setToStart() michael@0: * for forward iteration with next32PostInc(). michael@0: * @return the first code point in its iteration range. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual UChar32 first32PostInc(void); michael@0: michael@0: /** michael@0: * Sets the iterator to refer to the last code unit in its michael@0: * iteration range, and returns that code unit. michael@0: * This can be used to begin an iteration with previous(). michael@0: * @return the last code unit in its iteration range. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual UChar last(void); michael@0: michael@0: /** michael@0: * Sets the iterator to refer to the last code point in its michael@0: * iteration range, and returns that code unit. michael@0: * This can be used to begin an iteration with previous32(). michael@0: * @return the last code point in its iteration range. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual UChar32 last32(void); michael@0: michael@0: /** michael@0: * Sets the iterator to refer to the "position"-th code unit michael@0: * in the text-storage object the iterator refers to, and michael@0: * returns that code unit. michael@0: * @param position the position within the text-storage object michael@0: * @return the code unit michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual UChar setIndex(int32_t position); michael@0: michael@0: /** michael@0: * Sets the iterator to refer to the beginning of the code point michael@0: * that contains the "position"-th code unit michael@0: * in the text-storage object the iterator refers to, and michael@0: * returns that code point. michael@0: * The current position is adjusted to the beginning of the code point michael@0: * (its first code unit). michael@0: * @param position the position within the text-storage object michael@0: * @return the code unit michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual UChar32 setIndex32(int32_t position); michael@0: michael@0: /** michael@0: * Returns the code unit the iterator currently refers to. michael@0: * @return the code unit the iterator currently refers to. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual UChar current(void) const; michael@0: michael@0: /** michael@0: * Returns the code point the iterator currently refers to. michael@0: * @return the code point the iterator currently refers to. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual UChar32 current32(void) const; michael@0: michael@0: /** michael@0: * Advances to the next code unit in the iteration range (toward michael@0: * endIndex()), and returns that code unit. If there are no more michael@0: * code units to return, returns DONE. michael@0: * @return the next code unit in the iteration range. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual UChar next(void); michael@0: michael@0: /** michael@0: * Gets the current code unit for returning and advances to the next code unit michael@0: * in the iteration range michael@0: * (toward endIndex()). If there are michael@0: * no more code units to return, returns DONE. michael@0: * @return the current code unit. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual UChar nextPostInc(void); michael@0: michael@0: /** michael@0: * Advances to the next code point in the iteration range (toward michael@0: * endIndex()), and returns that code point. If there are no more michael@0: * code points to return, returns DONE. michael@0: * Note that iteration with "pre-increment" semantics is less michael@0: * efficient than iteration with "post-increment" semantics michael@0: * that is provided by next32PostInc(). michael@0: * @return the next code point in the iteration range. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual UChar32 next32(void); michael@0: michael@0: /** michael@0: * Gets the current code point for returning and advances to the next code point michael@0: * in the iteration range michael@0: * (toward endIndex()). If there are michael@0: * no more code points to return, returns DONE. michael@0: * @return the current point. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual UChar32 next32PostInc(void); michael@0: michael@0: /** michael@0: * Returns FALSE if there are no more code units or code points michael@0: * at or after the current position in the iteration range. michael@0: * This is used with nextPostInc() or next32PostInc() in forward michael@0: * iteration. michael@0: * @return FALSE if there are no more code units or code points michael@0: * at or after the current position in the iteration range. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual UBool hasNext(); michael@0: michael@0: /** michael@0: * Advances to the previous code unit in the iteration range (toward michael@0: * startIndex()), and returns that code unit. If there are no more michael@0: * code units to return, returns DONE. michael@0: * @return the previous code unit in the iteration range. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual UChar previous(void); michael@0: michael@0: /** michael@0: * Advances to the previous code point in the iteration range (toward michael@0: * startIndex()), and returns that code point. If there are no more michael@0: * code points to return, returns DONE. michael@0: * @return the previous code point in the iteration range. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual UChar32 previous32(void); michael@0: michael@0: /** michael@0: * Returns FALSE if there are no more code units or code points michael@0: * before the current position in the iteration range. michael@0: * This is used with previous() or previous32() in backward michael@0: * iteration. michael@0: * @return FALSE if there are no more code units or code points michael@0: * before the current position in the iteration range. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual UBool hasPrevious(); michael@0: michael@0: /** michael@0: * Moves the current position relative to the start or end of the michael@0: * iteration range, or relative to the current position itself. michael@0: * The movement is expressed in numbers of code units forward michael@0: * or backward by specifying a positive or negative delta. michael@0: * @param delta the position relative to origin. A positive delta means forward; michael@0: * a negative delta means backward. michael@0: * @param origin Origin enumeration {kStart, kCurrent, kEnd} michael@0: * @return the new position michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual int32_t move(int32_t delta, EOrigin origin); michael@0: michael@0: /** michael@0: * Moves the current position relative to the start or end of the michael@0: * iteration range, or relative to the current position itself. michael@0: * The movement is expressed in numbers of code points forward michael@0: * or backward by specifying a positive or negative delta. michael@0: * @param delta the position relative to origin. A positive delta means forward; michael@0: * a negative delta means backward. michael@0: * @param origin Origin enumeration {kStart, kCurrent, kEnd} michael@0: * @return the new position michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual int32_t move32(int32_t delta, EOrigin origin); michael@0: michael@0: /** michael@0: * Sets the iterator to iterate over a new range of text michael@0: * @stable ICU 2.0 michael@0: */ michael@0: void setText(const UChar* newText, int32_t newTextLength); michael@0: michael@0: /** michael@0: * Copies the UChar array under iteration into the UnicodeString michael@0: * referred to by "result". Even if this iterator iterates across michael@0: * only a part of this string, the whole string is copied. michael@0: * @param result Receives a copy of the text under iteration. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual void getText(UnicodeString& result); michael@0: michael@0: /** michael@0: * Return a class ID for this class (not really public) michael@0: * @return a class ID for this class michael@0: * @stable ICU 2.0 michael@0: */ michael@0: static UClassID U_EXPORT2 getStaticClassID(void); michael@0: michael@0: /** michael@0: * Return a class ID for this object (not really public) michael@0: * @return a class ID for this object. michael@0: * @stable ICU 2.0 michael@0: */ michael@0: virtual UClassID getDynamicClassID(void) const; michael@0: michael@0: protected: michael@0: /** michael@0: * Protected constructor michael@0: * @stable ICU 2.0 michael@0: */ michael@0: UCharCharacterIterator(); michael@0: /** michael@0: * Protected member text michael@0: * @stable ICU 2.0 michael@0: */ michael@0: const UChar* text; michael@0: michael@0: }; michael@0: michael@0: U_NAMESPACE_END michael@0: #endif