sl@0: /* sl@0: ******************************************************************** sl@0: * sl@0: * Copyright (C) 1997-2005, International Business Machines sl@0: * Corporation and others. All Rights Reserved. sl@0: * sl@0: ******************************************************************** sl@0: */ sl@0: sl@0: #ifndef CHARITER_H sl@0: #define CHARITER_H sl@0: sl@0: #include "unicode/utypes.h" sl@0: #include "unicode/uobject.h" sl@0: #include "unicode/unistr.h" sl@0: /** sl@0: * \file sl@0: * \brief C++ API: Character Iterator sl@0: */ sl@0: sl@0: U_NAMESPACE_BEGIN sl@0: /** sl@0: * Abstract class that defines an API for forward-only iteration sl@0: * on text objects. sl@0: * This is a minimal interface for iteration without random access sl@0: * or backwards iteration. It is especially useful for wrapping sl@0: * streams with converters into an object for collation or sl@0: * normalization. sl@0: * sl@0: *
Characters can be accessed in two ways: as code units or as sl@0: * code points. sl@0: * Unicode code points are 21-bit integers and are the scalar values sl@0: * of Unicode characters. ICU uses the type UChar32 for them. sl@0: * Unicode code units are the storage units of a given sl@0: * Unicode/UCS Transformation Format (a character encoding scheme). sl@0: * With UTF-16, all code points can be represented with either one sl@0: * or two code units ("surrogates"). sl@0: * String storage is typically based on code units, while properties sl@0: * of characters are typically determined using code point values. sl@0: * Some processes may be designed to work with sequences of code units, sl@0: * or it may be known that all characters that are important to an sl@0: * algorithm can be represented with single code units. sl@0: * Other processes will need to use the code point access functions.
sl@0: * sl@0: *ForwardCharacterIterator provides nextPostInc() to access
sl@0: * a code unit and advance an internal position into the text object,
sl@0: * similar to a return text[position++]
.
sl@0: * It provides next32PostInc() to access a code point and advance an internal
sl@0: * position.
next32PostInc() assumes that the current position is that of sl@0: * the beginning of a code point, i.e., of its first code unit. sl@0: * After next32PostInc(), this will be true again. sl@0: * In general, access to code units and code points in the same sl@0: * iteration loop should not be mixed. In UTF-16, if the current position sl@0: * is on a second code unit (Low Surrogate), then only that code unit sl@0: * is returned even by next32PostInc().
sl@0: * sl@0: *For iteration with either function, there are two ways to sl@0: * check for the end of the iteration. When there are no more sl@0: * characters in the text object: sl@0: *
Despite the fact that this function is public, sl@0: * DO NOT CONSIDER IT PART OF CHARACTERITERATOR'S API! sl@0: * @return a UClassID for this ForwardCharacterIterator sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UClassID getDynamicClassID(void) const = 0; sl@0: sl@0: /** sl@0: * Gets the current code unit for returning and advances to the next code unit sl@0: * in the iteration range sl@0: * (toward endIndex()). If there are sl@0: * no more code units to return, returns DONE. sl@0: * @return the current code unit. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UChar nextPostInc(void) = 0; sl@0: sl@0: /** sl@0: * Gets the current code point for returning and advances to the next code point sl@0: * in the iteration range sl@0: * (toward endIndex()). If there are sl@0: * no more code points to return, returns DONE. sl@0: * @return the current code point. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UChar32 next32PostInc(void) = 0; sl@0: sl@0: /** sl@0: * Returns FALSE if there are no more code units or code points sl@0: * at or after the current position in the iteration range. sl@0: * This is used with nextPostInc() or next32PostInc() in forward sl@0: * iteration. sl@0: * @returns FALSE if there are no more code units or code points sl@0: * at or after the current position in the iteration range. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UBool hasNext() = 0; sl@0: sl@0: protected: sl@0: /** Default constructor to be overridden in the implementing class. @stable ICU 2.0*/ sl@0: ForwardCharacterIterator(); sl@0: sl@0: /** Copy constructor to be overridden in the implementing class. @stable ICU 2.0*/ sl@0: ForwardCharacterIterator(const ForwardCharacterIterator &other); sl@0: sl@0: /** sl@0: * Assignment operator to be overridden in the implementing class. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: ForwardCharacterIterator &operator=(const ForwardCharacterIterator&) { return *this; } sl@0: }; sl@0: sl@0: /** sl@0: * Abstract class that defines an API for iteration sl@0: * on text objects. sl@0: * This is an interface for forward and backward iteration sl@0: * and random access into a text object. sl@0: * sl@0: *
The API provides backward compatibility to the Java and older ICU sl@0: * CharacterIterator classes but extends them significantly: sl@0: *
Examples for some of the new functions:
sl@0: * sl@0: * Forward iteration with hasNext(): sl@0: * \code sl@0: * void forward1(CharacterIterator &it) { sl@0: * UChar32 c; sl@0: * for(it.setToStart(); it.hasNext();) { sl@0: * c=it.next32PostInc(); sl@0: * // use c sl@0: * } sl@0: * } sl@0: * \endcode sl@0: * Forward iteration more similar to loops with the old forward iteration, sl@0: * showing a way to convert simple for() loops: sl@0: * \code sl@0: * void forward2(CharacterIterator &it) { sl@0: * UChar c; sl@0: * for(c=it.firstPostInc(); c!=CharacterIterator::DONE; c=it.nextPostInc()) { sl@0: * // use c sl@0: * } sl@0: * } sl@0: * \endcode sl@0: * Backward iteration with setToEnd() and hasPrevious(): sl@0: * \code sl@0: * void backward1(CharacterIterator &it) { sl@0: * UChar32 c; sl@0: * for(it.setToEnd(); it.hasPrevious();) { sl@0: * c=it.previous32(); sl@0: * // use c sl@0: * } sl@0: * } sl@0: * \endcode sl@0: * Backward iteration with a more traditional for() loop: sl@0: * \code sl@0: * void backward2(CharacterIterator &it) { sl@0: * UChar c; sl@0: * for(c=it.last(); c!=CharacterIterator::DONE; c=it.previous()) { sl@0: * // use c sl@0: * } sl@0: * } sl@0: * \endcode sl@0: * sl@0: * Example for random access: sl@0: * \code sl@0: * void random(CharacterIterator &it) { sl@0: * // set to the third code point from the beginning sl@0: * it.move32(3, CharacterIterator::kStart); sl@0: * // get a code point from here without moving the position sl@0: * UChar32 c=it.current32(); sl@0: * // get the position sl@0: * int32_t pos=it.getIndex(); sl@0: * // get the previous code unit sl@0: * UChar u=it.previous(); sl@0: * // move back one more code unit sl@0: * it.move(-1, CharacterIterator::kCurrent); sl@0: * // set the position back to where it was sl@0: * // and read the same code point c and move beyond it sl@0: * it.setIndex(pos); sl@0: * if(c!=it.next32PostInc()) { sl@0: * exit(1); // CharacterIterator inconsistent sl@0: * } sl@0: * } sl@0: * \endcode sl@0: * sl@0: *Examples, especially for the old API:
sl@0: * sl@0: * Function processing characters, in this example simple output sl@0: *sl@0: * \code sl@0: * void processChar( UChar c ) sl@0: * { sl@0: * cout << " " << c; sl@0: * } sl@0: * \endcode sl@0: *sl@0: * Traverse the text from start to finish sl@0: *
sl@0: * \code sl@0: * void traverseForward(CharacterIterator& iter) sl@0: * { sl@0: * for(UChar c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) { sl@0: * processChar(c); sl@0: * } sl@0: * } sl@0: * \endcode sl@0: *sl@0: * Traverse the text backwards, from end to start sl@0: *
sl@0: * \code sl@0: * void traverseBackward(CharacterIterator& iter) sl@0: * { sl@0: * for(UChar c = iter.last(); c != CharacterIterator.DONE; c = iter.previous()) { sl@0: * processChar(c); sl@0: * } sl@0: * } sl@0: * \endcode sl@0: *sl@0: * Traverse both forward and backward from a given position in the text. sl@0: * Calls to notBoundary() in this example represents some additional stopping criteria. sl@0: *
sl@0: * \code sl@0: * void traverseOut(CharacterIterator& iter, int32_t pos) sl@0: * { sl@0: * UChar c; sl@0: * for (c = iter.setIndex(pos); sl@0: * c != CharacterIterator.DONE && (Unicode::isLetter(c) || Unicode::isDigit(c)); sl@0: * c = iter.next()) {} sl@0: * int32_t end = iter.getIndex(); sl@0: * for (c = iter.setIndex(pos); sl@0: * c != CharacterIterator.DONE && (Unicode::isLetter(c) || Unicode::isDigit(c)); sl@0: * c = iter.previous()) {} sl@0: * int32_t start = iter.getIndex() + 1; sl@0: * sl@0: * cout << "start: " << start << " end: " << end << endl; sl@0: * for (c = iter.setIndex(start); iter.getIndex() < end; c = iter.next() ) { sl@0: * processChar(c); sl@0: * } sl@0: * } sl@0: * \endcode sl@0: *sl@0: * Creating a StringCharacterIterator and calling the test functions sl@0: *
sl@0: * \code sl@0: * void CharacterIterator_Example( void ) sl@0: * { sl@0: * cout << endl << "===== CharacterIterator_Example: =====" << endl; sl@0: * UnicodeString text("Ein kleiner Satz."); sl@0: * StringCharacterIterator iterator(text); sl@0: * cout << "----- traverseForward: -----------" << endl; sl@0: * traverseForward( iterator ); sl@0: * cout << endl << endl << "----- traverseBackward: ----------" << endl; sl@0: * traverseBackward( iterator ); sl@0: * cout << endl << endl << "----- traverseOut: ---------------" << endl; sl@0: * traverseOut( iterator, 7 ); sl@0: * cout << endl << endl << "-----" << endl; sl@0: * } sl@0: * \endcode sl@0: *sl@0: * sl@0: * @stable ICU 2.0 sl@0: */ sl@0: class U_COMMON_API CharacterIterator : public ForwardCharacterIterator { sl@0: public: sl@0: /** sl@0: * Origin enumeration for the move() and move32() functions. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: enum EOrigin { kStart, kCurrent, kEnd }; sl@0: sl@0: /** sl@0: * Returns a pointer to a new CharacterIterator of the same sl@0: * concrete class as this one, and referring to the same sl@0: * character in the same text-storage object as this one. The sl@0: * caller is responsible for deleting the new clone. sl@0: * @return a pointer to a new CharacterIterator sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual CharacterIterator* clone(void) const = 0; sl@0: sl@0: /** sl@0: * Sets the iterator to refer to the first code unit in its sl@0: * iteration range, and returns that code unit. sl@0: * This can be used to begin an iteration with next(). sl@0: * @return the first code unit in its iteration range. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UChar first(void) = 0; sl@0: sl@0: /** sl@0: * Sets the iterator to refer to the first code unit in its sl@0: * iteration range, returns that code unit, and moves the position sl@0: * to the second code unit. This is an alternative to setToStart() sl@0: * for forward iteration with nextPostInc(). sl@0: * @return the first code unit in its iteration range. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UChar firstPostInc(void); sl@0: sl@0: /** sl@0: * Sets the iterator to refer to the first code point in its sl@0: * iteration range, and returns that code unit, sl@0: * This can be used to begin an iteration with next32(). sl@0: * Note that an iteration with next32PostInc(), beginning with, sl@0: * e.g., setToStart() or firstPostInc(), is more efficient. sl@0: * @return the first code point in its iteration range. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UChar32 first32(void) = 0; sl@0: sl@0: /** sl@0: * Sets the iterator to refer to the first code point in its sl@0: * iteration range, returns that code point, and moves the position sl@0: * to the second code point. This is an alternative to setToStart() sl@0: * for forward iteration with next32PostInc(). sl@0: * @return the first code point in its iteration range. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UChar32 first32PostInc(void); sl@0: sl@0: /** sl@0: * Sets the iterator to refer to the first code unit or code point in its sl@0: * iteration range. This can be used to begin a forward sl@0: * iteration with nextPostInc() or next32PostInc(). sl@0: * @return the start position of the iteration range sl@0: * @stable ICU 2.0 sl@0: */ sl@0: inline int32_t setToStart(); sl@0: sl@0: /** sl@0: * Sets the iterator to refer to the last code unit in its sl@0: * iteration range, and returns that code unit. sl@0: * This can be used to begin an iteration with previous(). sl@0: * @return the last code unit. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UChar last(void) = 0; sl@0: sl@0: /** sl@0: * Sets the iterator to refer to the last code point in its sl@0: * iteration range, and returns that code unit. sl@0: * This can be used to begin an iteration with previous32(). sl@0: * @return the last code point. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UChar32 last32(void) = 0; sl@0: sl@0: /** sl@0: * Sets the iterator to the end of its iteration range, just behind sl@0: * the last code unit or code point. This can be used to begin a backward sl@0: * iteration with previous() or previous32(). sl@0: * @return the end position of the iteration range sl@0: * @stable ICU 2.0 sl@0: */ sl@0: inline int32_t setToEnd(); sl@0: sl@0: /** sl@0: * Sets the iterator to refer to the "position"-th code unit sl@0: * in the text-storage object the iterator refers to, and sl@0: * returns that code unit. sl@0: * @param position the "position"-th code unit in the text-storage object sl@0: * @return the "position"-th code unit. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UChar setIndex(int32_t position) = 0; sl@0: sl@0: /** sl@0: * Sets the iterator to refer to the beginning of the code point sl@0: * that contains the "position"-th code unit sl@0: * in the text-storage object the iterator refers to, and sl@0: * returns that code point. sl@0: * The current position is adjusted to the beginning of the code point sl@0: * (its first code unit). sl@0: * @param position the "position"-th code unit in the text-storage object sl@0: * @return the "position"-th code point. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UChar32 setIndex32(int32_t position) = 0; sl@0: sl@0: /** sl@0: * Returns the code unit the iterator currently refers to. sl@0: * @return the current code unit. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UChar current(void) const = 0; sl@0: sl@0: /** sl@0: * Returns the code point the iterator currently refers to. sl@0: * @return the current code point. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UChar32 current32(void) const = 0; sl@0: sl@0: /** sl@0: * Advances to the next code unit in the iteration range sl@0: * (toward endIndex()), and returns that code unit. If there are sl@0: * no more code units to return, returns DONE. sl@0: * @return the next code unit. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UChar next(void) = 0; sl@0: sl@0: /** sl@0: * Advances to the next code point in the iteration range sl@0: * (toward endIndex()), and returns that code point. If there are sl@0: * no more code points to return, returns DONE. sl@0: * Note that iteration with "pre-increment" semantics is less sl@0: * efficient than iteration with "post-increment" semantics sl@0: * that is provided by next32PostInc(). sl@0: * @return the next code point. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UChar32 next32(void) = 0; sl@0: sl@0: /** sl@0: * Advances to the previous code unit in the iteration range sl@0: * (toward startIndex()), and returns that code unit. If there are sl@0: * no more code units to return, returns DONE. sl@0: * @return the previous code unit. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UChar previous(void) = 0; sl@0: sl@0: /** sl@0: * Advances to the previous code point in the iteration range sl@0: * (toward startIndex()), and returns that code point. If there are sl@0: * no more code points to return, returns DONE. sl@0: * @return the previous code point. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UChar32 previous32(void) = 0; sl@0: sl@0: /** sl@0: * Returns FALSE if there are no more code units or code points sl@0: * before the current position in the iteration range. sl@0: * This is used with previous() or previous32() in backward sl@0: * iteration. sl@0: * @return FALSE if there are no more code units or code points sl@0: * before the current position in the iteration range, return TRUE otherwise. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual UBool hasPrevious() = 0; sl@0: sl@0: /** sl@0: * Returns the numeric index in the underlying text-storage sl@0: * object of the character returned by first(). Since it's sl@0: * possible to create an iterator that iterates across only sl@0: * part of a text-storage object, this number isn't sl@0: * necessarily 0. sl@0: * @returns the numeric index in the underlying text-storage sl@0: * object of the character returned by first(). sl@0: * @stable ICU 2.0 sl@0: */ sl@0: inline int32_t startIndex(void) const; sl@0: sl@0: /** sl@0: * Returns the numeric index in the underlying text-storage sl@0: * object of the position immediately BEYOND the character sl@0: * returned by last(). sl@0: * @return the numeric index in the underlying text-storage sl@0: * object of the position immediately BEYOND the character sl@0: * returned by last(). sl@0: * @stable ICU 2.0 sl@0: */ sl@0: inline int32_t endIndex(void) const; sl@0: sl@0: /** sl@0: * Returns the numeric index in the underlying text-storage sl@0: * object of the character the iterator currently refers to sl@0: * (i.e., the character returned by current()). sl@0: * @return the numberic index in the text-storage object of sl@0: * the character the iterator currently refers to sl@0: * @stable ICU 2.0 sl@0: */ sl@0: inline int32_t getIndex(void) const; sl@0: sl@0: /** sl@0: * Returns the length of the entire text in the underlying sl@0: * text-storage object. sl@0: * @return the length of the entire text in the text-storage object sl@0: * @stable ICU 2.0 sl@0: */ sl@0: inline int32_t getLength() const; sl@0: sl@0: /** sl@0: * Moves the current position relative to the start or end of the sl@0: * iteration range, or relative to the current position itself. sl@0: * The movement is expressed in numbers of code units forward sl@0: * or backward by specifying a positive or negative delta. sl@0: * @param delta the position relative to origin. A positive delta means forward; sl@0: * a negative delta means backward. sl@0: * @param origin Origin enumeration {kStart, kCurrent, kEnd} sl@0: * @return the new position sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual int32_t move(int32_t delta, EOrigin origin) = 0; sl@0: sl@0: /** sl@0: * Moves the current position relative to the start or end of the sl@0: * iteration range, or relative to the current position itself. sl@0: * The movement is expressed in numbers of code points forward sl@0: * or backward by specifying a positive or negative delta. sl@0: * @param delta the position relative to origin. A positive delta means forward; sl@0: * a negative delta means backward. sl@0: * @param origin Origin enumeration {kStart, kCurrent, kEnd} sl@0: * @return the new position sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual int32_t move32(int32_t delta, EOrigin origin) = 0; sl@0: sl@0: /** sl@0: * Copies the text under iteration into the UnicodeString sl@0: * referred to by "result". sl@0: * @param result Receives a copy of the text under iteration. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual void getText(UnicodeString& result) = 0; sl@0: sl@0: protected: sl@0: /** sl@0: * Empty constructor. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: CharacterIterator(); sl@0: sl@0: /** sl@0: * Constructor, just setting the length field in this base class. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: CharacterIterator(int32_t length); sl@0: sl@0: /** sl@0: * Constructor, just setting the length and position fields in this base class. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: CharacterIterator(int32_t length, int32_t position); sl@0: sl@0: /** sl@0: * Constructor, just setting the length, start, end, and position fields in this base class. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: CharacterIterator(int32_t length, int32_t textBegin, int32_t textEnd, int32_t position); sl@0: sl@0: /** sl@0: * Copy constructor. sl@0: * sl@0: * @param that The CharacterIterator to be copied sl@0: * @stable ICU 2.0 sl@0: */ sl@0: CharacterIterator(const CharacterIterator &that); sl@0: sl@0: /** sl@0: * Assignment operator. Sets this CharacterIterator to have the same behavior, sl@0: * as the one passed in. sl@0: * @param that The CharacterIterator passed in. sl@0: * @return the newly set CharacterIterator. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: CharacterIterator &operator=(const CharacterIterator &that); sl@0: sl@0: /** sl@0: * Base class text length field. sl@0: * Necessary this for correct getText() and hashCode(). sl@0: * @stable ICU 2.0 sl@0: */ sl@0: int32_t textLength; sl@0: sl@0: /** sl@0: * Base class field for the current position. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: int32_t pos; sl@0: sl@0: /** sl@0: * Base class field for the start of the iteration range. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: int32_t begin; sl@0: sl@0: /** sl@0: * Base class field for the end of the iteration range. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: int32_t end; sl@0: }; sl@0: sl@0: inline UBool sl@0: ForwardCharacterIterator::operator!=(const ForwardCharacterIterator& that) const { sl@0: return !operator==(that); sl@0: } sl@0: sl@0: inline int32_t sl@0: CharacterIterator::setToStart() { sl@0: return move(0, kStart); sl@0: } sl@0: sl@0: inline int32_t sl@0: CharacterIterator::setToEnd() { sl@0: return move(0, kEnd); sl@0: } sl@0: sl@0: inline int32_t sl@0: CharacterIterator::startIndex(void) const { sl@0: return begin; sl@0: } sl@0: sl@0: inline int32_t sl@0: CharacterIterator::endIndex(void) const { sl@0: return end; sl@0: } sl@0: sl@0: inline int32_t sl@0: CharacterIterator::getIndex(void) const { sl@0: return pos; sl@0: } sl@0: sl@0: inline int32_t sl@0: CharacterIterator::getLength(void) const { sl@0: return textLength; sl@0: } sl@0: sl@0: U_NAMESPACE_END sl@0: #endif