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