jpayne@69: // © 2016 and later: Unicode, Inc. and others. jpayne@69: // License & terms of use: http://www.unicode.org/copyright.html jpayne@69: /* jpayne@69: ******************************************************************** jpayne@69: * jpayne@69: * Copyright (C) 1997-2011, International Business Machines jpayne@69: * Corporation and others. All Rights Reserved. jpayne@69: * jpayne@69: ******************************************************************** jpayne@69: */ jpayne@69: jpayne@69: #ifndef CHARITER_H jpayne@69: #define CHARITER_H jpayne@69: jpayne@69: #include "unicode/utypes.h" jpayne@69: jpayne@69: #if U_SHOW_CPLUSPLUS_API jpayne@69: jpayne@69: #include "unicode/uobject.h" jpayne@69: #include "unicode/unistr.h" jpayne@69: /** jpayne@69: * \file jpayne@69: * \brief C++ API: Character Iterator jpayne@69: */ jpayne@69: jpayne@69: U_NAMESPACE_BEGIN jpayne@69: /** jpayne@69: * Abstract class that defines an API for forward-only iteration jpayne@69: * on text objects. jpayne@69: * This is a minimal interface for iteration without random access jpayne@69: * or backwards iteration. It is especially useful for wrapping jpayne@69: * streams with converters into an object for collation or jpayne@69: * normalization. jpayne@69: * jpayne@69: *

Characters can be accessed in two ways: as code units or as jpayne@69: * code points. jpayne@69: * Unicode code points are 21-bit integers and are the scalar values jpayne@69: * of Unicode characters. ICU uses the type UChar32 for them. jpayne@69: * Unicode code units are the storage units of a given jpayne@69: * Unicode/UCS Transformation Format (a character encoding scheme). jpayne@69: * With UTF-16, all code points can be represented with either one jpayne@69: * or two code units ("surrogates"). jpayne@69: * String storage is typically based on code units, while properties jpayne@69: * of characters are typically determined using code point values. jpayne@69: * Some processes may be designed to work with sequences of code units, jpayne@69: * or it may be known that all characters that are important to an jpayne@69: * algorithm can be represented with single code units. jpayne@69: * Other processes will need to use the code point access functions.

jpayne@69: * jpayne@69: *

ForwardCharacterIterator provides nextPostInc() to access jpayne@69: * a code unit and advance an internal position into the text object, jpayne@69: * similar to a return text[position++].
jpayne@69: * It provides next32PostInc() to access a code point and advance an internal jpayne@69: * position.

jpayne@69: * jpayne@69: *

next32PostInc() assumes that the current position is that of jpayne@69: * the beginning of a code point, i.e., of its first code unit. jpayne@69: * After next32PostInc(), this will be true again. jpayne@69: * In general, access to code units and code points in the same jpayne@69: * iteration loop should not be mixed. In UTF-16, if the current position jpayne@69: * is on a second code unit (Low Surrogate), then only that code unit jpayne@69: * is returned even by next32PostInc().

jpayne@69: * jpayne@69: *

For iteration with either function, there are two ways to jpayne@69: * check for the end of the iteration. When there are no more jpayne@69: * characters in the text object: jpayne@69: *

jpayne@69: * jpayne@69: * Example: jpayne@69: * \code jpayne@69: * void function1(ForwardCharacterIterator &it) { jpayne@69: * UChar32 c; jpayne@69: * while(it.hasNext()) { jpayne@69: * c=it.next32PostInc(); jpayne@69: * // use c jpayne@69: * } jpayne@69: * } jpayne@69: * jpayne@69: * void function1(ForwardCharacterIterator &it) { jpayne@69: * char16_t c; jpayne@69: * while((c=it.nextPostInc())!=ForwardCharacterIterator::DONE) { jpayne@69: * // use c jpayne@69: * } jpayne@69: * } jpayne@69: * \endcode jpayne@69: *

jpayne@69: * jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: class U_COMMON_API ForwardCharacterIterator : public UObject { jpayne@69: public: jpayne@69: /** jpayne@69: * Value returned by most of ForwardCharacterIterator's functions jpayne@69: * when the iterator has reached the limits of its iteration. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: enum { DONE = 0xffff }; jpayne@69: jpayne@69: /** jpayne@69: * Destructor. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual ~ForwardCharacterIterator(); jpayne@69: jpayne@69: /** jpayne@69: * Returns true when both iterators refer to the same jpayne@69: * character in the same character-storage object. jpayne@69: * @param that The ForwardCharacterIterator to be compared for equality jpayne@69: * @return true when both iterators refer to the same jpayne@69: * character in the same character-storage object jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual UBool operator==(const ForwardCharacterIterator& that) const = 0; jpayne@69: jpayne@69: /** jpayne@69: * Returns true when the iterators refer to different jpayne@69: * text-storage objects, or to different characters in the jpayne@69: * same text-storage object. jpayne@69: * @param that The ForwardCharacterIterator to be compared for inequality jpayne@69: * @return true when the iterators refer to different jpayne@69: * text-storage objects, or to different characters in the jpayne@69: * same text-storage object jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: inline UBool operator!=(const ForwardCharacterIterator& that) const; jpayne@69: jpayne@69: /** jpayne@69: * Generates a hash code for this iterator. jpayne@69: * @return the hash code. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual int32_t hashCode(void) const = 0; jpayne@69: jpayne@69: /** jpayne@69: * Returns a UClassID for this ForwardCharacterIterator ("poor man's jpayne@69: * RTTI").

Despite the fact that this function is public, jpayne@69: * DO NOT CONSIDER IT PART OF CHARACTERITERATOR'S API! jpayne@69: * @return a UClassID for this ForwardCharacterIterator jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual UClassID getDynamicClassID(void) const = 0; jpayne@69: jpayne@69: /** jpayne@69: * Gets the current code unit for returning and advances to the next code unit jpayne@69: * in the iteration range jpayne@69: * (toward endIndex()). If there are jpayne@69: * no more code units to return, returns DONE. jpayne@69: * @return the current code unit. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual char16_t nextPostInc(void) = 0; jpayne@69: jpayne@69: /** jpayne@69: * Gets the current code point for returning and advances to the next code point jpayne@69: * in the iteration range jpayne@69: * (toward endIndex()). If there are jpayne@69: * no more code points to return, returns DONE. jpayne@69: * @return the current code point. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual UChar32 next32PostInc(void) = 0; jpayne@69: jpayne@69: /** jpayne@69: * Returns FALSE if there are no more code units or code points jpayne@69: * at or after the current position in the iteration range. jpayne@69: * This is used with nextPostInc() or next32PostInc() in forward jpayne@69: * iteration. jpayne@69: * @returns FALSE if there are no more code units or code points jpayne@69: * at or after the current position in the iteration range. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual UBool hasNext() = 0; jpayne@69: jpayne@69: protected: jpayne@69: /** Default constructor to be overridden in the implementing class. @stable ICU 2.0*/ jpayne@69: ForwardCharacterIterator(); jpayne@69: jpayne@69: /** Copy constructor to be overridden in the implementing class. @stable ICU 2.0*/ jpayne@69: ForwardCharacterIterator(const ForwardCharacterIterator &other); jpayne@69: jpayne@69: /** jpayne@69: * Assignment operator to be overridden in the implementing class. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: ForwardCharacterIterator &operator=(const ForwardCharacterIterator&) { return *this; } jpayne@69: }; jpayne@69: jpayne@69: /** jpayne@69: * Abstract class that defines an API for iteration jpayne@69: * on text objects. jpayne@69: * This is an interface for forward and backward iteration jpayne@69: * and random access into a text object. jpayne@69: * jpayne@69: *

The API provides backward compatibility to the Java and older ICU jpayne@69: * CharacterIterator classes but extends them significantly: jpayne@69: *

    jpayne@69: *
  1. CharacterIterator is now a subclass of ForwardCharacterIterator.
  2. jpayne@69: *
  3. While the old API functions provided forward iteration with jpayne@69: * "pre-increment" semantics, the new one also provides functions jpayne@69: * with "post-increment" semantics. They are more efficient and should jpayne@69: * be the preferred iterator functions for new implementations. jpayne@69: * The backward iteration always had "pre-decrement" semantics, which jpayne@69: * are efficient.
  4. jpayne@69: *
  5. Just like ForwardCharacterIterator, it provides access to jpayne@69: * both code units and code points. Code point access versions are available jpayne@69: * for the old and the new iteration semantics.
  6. jpayne@69: *
  7. There are new functions for setting and moving the current position jpayne@69: * without returning a character, for efficiency.
  8. jpayne@69: *
jpayne@69: * jpayne@69: * See ForwardCharacterIterator for examples for using the new forward iteration jpayne@69: * functions. For backward iteration, there is also a hasPrevious() function jpayne@69: * that can be used analogously to hasNext(). jpayne@69: * The old functions work as before and are shown below.

jpayne@69: * jpayne@69: *

Examples for some of the new functions:

jpayne@69: * jpayne@69: * Forward iteration with hasNext(): jpayne@69: * \code jpayne@69: * void forward1(CharacterIterator &it) { jpayne@69: * UChar32 c; jpayne@69: * for(it.setToStart(); it.hasNext();) { jpayne@69: * c=it.next32PostInc(); jpayne@69: * // use c jpayne@69: * } jpayne@69: * } jpayne@69: * \endcode jpayne@69: * Forward iteration more similar to loops with the old forward iteration, jpayne@69: * showing a way to convert simple for() loops: jpayne@69: * \code jpayne@69: * void forward2(CharacterIterator &it) { jpayne@69: * char16_t c; jpayne@69: * for(c=it.firstPostInc(); c!=CharacterIterator::DONE; c=it.nextPostInc()) { jpayne@69: * // use c jpayne@69: * } jpayne@69: * } jpayne@69: * \endcode jpayne@69: * Backward iteration with setToEnd() and hasPrevious(): jpayne@69: * \code jpayne@69: * void backward1(CharacterIterator &it) { jpayne@69: * UChar32 c; jpayne@69: * for(it.setToEnd(); it.hasPrevious();) { jpayne@69: * c=it.previous32(); jpayne@69: * // use c jpayne@69: * } jpayne@69: * } jpayne@69: * \endcode jpayne@69: * Backward iteration with a more traditional for() loop: jpayne@69: * \code jpayne@69: * void backward2(CharacterIterator &it) { jpayne@69: * char16_t c; jpayne@69: * for(c=it.last(); c!=CharacterIterator::DONE; c=it.previous()) { jpayne@69: * // use c jpayne@69: * } jpayne@69: * } jpayne@69: * \endcode jpayne@69: * jpayne@69: * Example for random access: jpayne@69: * \code jpayne@69: * void random(CharacterIterator &it) { jpayne@69: * // set to the third code point from the beginning jpayne@69: * it.move32(3, CharacterIterator::kStart); jpayne@69: * // get a code point from here without moving the position jpayne@69: * UChar32 c=it.current32(); jpayne@69: * // get the position jpayne@69: * int32_t pos=it.getIndex(); jpayne@69: * // get the previous code unit jpayne@69: * char16_t u=it.previous(); jpayne@69: * // move back one more code unit jpayne@69: * it.move(-1, CharacterIterator::kCurrent); jpayne@69: * // set the position back to where it was jpayne@69: * // and read the same code point c and move beyond it jpayne@69: * it.setIndex(pos); jpayne@69: * if(c!=it.next32PostInc()) { jpayne@69: * exit(1); // CharacterIterator inconsistent jpayne@69: * } jpayne@69: * } jpayne@69: * \endcode jpayne@69: * jpayne@69: *

Examples, especially for the old API:

jpayne@69: * jpayne@69: * Function processing characters, in this example simple output jpayne@69: *
jpayne@69:  * \code
jpayne@69:  *  void processChar( char16_t c )
jpayne@69:  *  {
jpayne@69:  *      cout << " " << c;
jpayne@69:  *  }
jpayne@69:  * \endcode
jpayne@69:  * 
jpayne@69: * Traverse the text from start to finish jpayne@69: *
 
jpayne@69:  * \code
jpayne@69:  *  void traverseForward(CharacterIterator& iter)
jpayne@69:  *  {
jpayne@69:  *      for(char16_t c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
jpayne@69:  *          processChar(c);
jpayne@69:  *      }
jpayne@69:  *  }
jpayne@69:  * \endcode
jpayne@69:  * 
jpayne@69: * Traverse the text backwards, from end to start jpayne@69: *
jpayne@69:  * \code
jpayne@69:  *  void traverseBackward(CharacterIterator& iter)
jpayne@69:  *  {
jpayne@69:  *      for(char16_t c = iter.last(); c != CharacterIterator.DONE; c = iter.previous()) {
jpayne@69:  *          processChar(c);
jpayne@69:  *      }
jpayne@69:  *  }
jpayne@69:  * \endcode
jpayne@69:  * 
jpayne@69: * Traverse both forward and backward from a given position in the text. jpayne@69: * Calls to notBoundary() in this example represents some additional stopping criteria. jpayne@69: *
jpayne@69:  * \code
jpayne@69:  * void traverseOut(CharacterIterator& iter, int32_t pos)
jpayne@69:  * {
jpayne@69:  *      char16_t c;
jpayne@69:  *      for (c = iter.setIndex(pos);
jpayne@69:  *      c != CharacterIterator.DONE && (Unicode::isLetter(c) || Unicode::isDigit(c));
jpayne@69:  *          c = iter.next()) {}
jpayne@69:  *      int32_t end = iter.getIndex();
jpayne@69:  *      for (c = iter.setIndex(pos);
jpayne@69:  *          c != CharacterIterator.DONE && (Unicode::isLetter(c) || Unicode::isDigit(c));
jpayne@69:  *          c = iter.previous()) {}
jpayne@69:  *      int32_t start = iter.getIndex() + 1;
jpayne@69:  *  
jpayne@69:  *      cout << "start: " << start << " end: " << end << endl;
jpayne@69:  *      for (c = iter.setIndex(start); iter.getIndex() < end; c = iter.next() ) {
jpayne@69:  *          processChar(c);
jpayne@69:  *     }
jpayne@69:  *  }
jpayne@69:  * \endcode
jpayne@69:  * 
jpayne@69: * Creating a StringCharacterIterator and calling the test functions jpayne@69: *
jpayne@69:  * \code
jpayne@69:  *  void CharacterIterator_Example( void )
jpayne@69:  *   {
jpayne@69:  *       cout << endl << "===== CharacterIterator_Example: =====" << endl;
jpayne@69:  *       UnicodeString text("Ein kleiner Satz.");
jpayne@69:  *       StringCharacterIterator iterator(text);
jpayne@69:  *       cout << "----- traverseForward: -----------" << endl;
jpayne@69:  *       traverseForward( iterator );
jpayne@69:  *       cout << endl << endl << "----- traverseBackward: ----------" << endl;
jpayne@69:  *       traverseBackward( iterator );
jpayne@69:  *       cout << endl << endl << "----- traverseOut: ---------------" << endl;
jpayne@69:  *       traverseOut( iterator, 7 );
jpayne@69:  *       cout << endl << endl << "-----" << endl;
jpayne@69:  *   }
jpayne@69:  * \endcode
jpayne@69:  * 
jpayne@69: * jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: class U_COMMON_API CharacterIterator : public ForwardCharacterIterator { jpayne@69: public: jpayne@69: /** jpayne@69: * Origin enumeration for the move() and move32() functions. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: enum EOrigin { kStart, kCurrent, kEnd }; jpayne@69: jpayne@69: /** jpayne@69: * Destructor. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual ~CharacterIterator(); jpayne@69: jpayne@69: /** jpayne@69: * Returns a pointer to a new CharacterIterator of the same jpayne@69: * concrete class as this one, and referring to the same jpayne@69: * character in the same text-storage object as this one. The jpayne@69: * caller is responsible for deleting the new clone. jpayne@69: * @return a pointer to a new CharacterIterator jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual CharacterIterator* clone() const = 0; jpayne@69: jpayne@69: /** jpayne@69: * Sets the iterator to refer to the first code unit in its jpayne@69: * iteration range, and returns that code unit. jpayne@69: * This can be used to begin an iteration with next(). jpayne@69: * @return the first code unit in its iteration range. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual char16_t first(void) = 0; jpayne@69: jpayne@69: /** jpayne@69: * Sets the iterator to refer to the first code unit in its jpayne@69: * iteration range, returns that code unit, and moves the position jpayne@69: * to the second code unit. This is an alternative to setToStart() jpayne@69: * for forward iteration with nextPostInc(). jpayne@69: * @return the first code unit in its iteration range. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual char16_t firstPostInc(void); jpayne@69: jpayne@69: /** jpayne@69: * Sets the iterator to refer to the first code point in its jpayne@69: * iteration range, and returns that code unit, jpayne@69: * This can be used to begin an iteration with next32(). jpayne@69: * Note that an iteration with next32PostInc(), beginning with, jpayne@69: * e.g., setToStart() or firstPostInc(), is more efficient. jpayne@69: * @return the first code point in its iteration range. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual UChar32 first32(void) = 0; jpayne@69: jpayne@69: /** jpayne@69: * Sets the iterator to refer to the first code point in its jpayne@69: * iteration range, returns that code point, and moves the position jpayne@69: * to the second code point. This is an alternative to setToStart() jpayne@69: * for forward iteration with next32PostInc(). jpayne@69: * @return the first code point in its iteration range. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual UChar32 first32PostInc(void); jpayne@69: jpayne@69: /** jpayne@69: * Sets the iterator to refer to the first code unit or code point in its jpayne@69: * iteration range. This can be used to begin a forward jpayne@69: * iteration with nextPostInc() or next32PostInc(). jpayne@69: * @return the start position of the iteration range jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: inline int32_t setToStart(); jpayne@69: jpayne@69: /** jpayne@69: * Sets the iterator to refer to the last code unit in its jpayne@69: * iteration range, and returns that code unit. jpayne@69: * This can be used to begin an iteration with previous(). jpayne@69: * @return the last code unit. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual char16_t last(void) = 0; jpayne@69: jpayne@69: /** jpayne@69: * Sets the iterator to refer to the last code point in its jpayne@69: * iteration range, and returns that code unit. jpayne@69: * This can be used to begin an iteration with previous32(). jpayne@69: * @return the last code point. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual UChar32 last32(void) = 0; jpayne@69: jpayne@69: /** jpayne@69: * Sets the iterator to the end of its iteration range, just behind jpayne@69: * the last code unit or code point. This can be used to begin a backward jpayne@69: * iteration with previous() or previous32(). jpayne@69: * @return the end position of the iteration range jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: inline int32_t setToEnd(); jpayne@69: jpayne@69: /** jpayne@69: * Sets the iterator to refer to the "position"-th code unit jpayne@69: * in the text-storage object the iterator refers to, and jpayne@69: * returns that code unit. jpayne@69: * @param position the "position"-th code unit in the text-storage object jpayne@69: * @return the "position"-th code unit. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual char16_t setIndex(int32_t position) = 0; jpayne@69: jpayne@69: /** jpayne@69: * Sets the iterator to refer to the beginning of the code point jpayne@69: * that contains the "position"-th code unit jpayne@69: * in the text-storage object the iterator refers to, and jpayne@69: * returns that code point. jpayne@69: * The current position is adjusted to the beginning of the code point jpayne@69: * (its first code unit). jpayne@69: * @param position the "position"-th code unit in the text-storage object jpayne@69: * @return the "position"-th code point. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual UChar32 setIndex32(int32_t position) = 0; jpayne@69: jpayne@69: /** jpayne@69: * Returns the code unit the iterator currently refers to. jpayne@69: * @return the current code unit. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual char16_t current(void) const = 0; jpayne@69: jpayne@69: /** jpayne@69: * Returns the code point the iterator currently refers to. jpayne@69: * @return the current code point. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual UChar32 current32(void) const = 0; jpayne@69: jpayne@69: /** jpayne@69: * Advances to the next code unit in the iteration range jpayne@69: * (toward endIndex()), and returns that code unit. If there are jpayne@69: * no more code units to return, returns DONE. jpayne@69: * @return the next code unit. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual char16_t next(void) = 0; jpayne@69: jpayne@69: /** jpayne@69: * Advances to the next code point in the iteration range jpayne@69: * (toward endIndex()), and returns that code point. If there are jpayne@69: * no more code points to return, returns DONE. jpayne@69: * Note that iteration with "pre-increment" semantics is less jpayne@69: * efficient than iteration with "post-increment" semantics jpayne@69: * that is provided by next32PostInc(). jpayne@69: * @return the next code point. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual UChar32 next32(void) = 0; jpayne@69: jpayne@69: /** jpayne@69: * Advances to the previous code unit in the iteration range jpayne@69: * (toward startIndex()), and returns that code unit. If there are jpayne@69: * no more code units to return, returns DONE. jpayne@69: * @return the previous code unit. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual char16_t previous(void) = 0; jpayne@69: jpayne@69: /** jpayne@69: * Advances to the previous code point in the iteration range jpayne@69: * (toward startIndex()), and returns that code point. If there are jpayne@69: * no more code points to return, returns DONE. jpayne@69: * @return the previous code point. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual UChar32 previous32(void) = 0; jpayne@69: jpayne@69: /** jpayne@69: * Returns FALSE if there are no more code units or code points jpayne@69: * before the current position in the iteration range. jpayne@69: * This is used with previous() or previous32() in backward jpayne@69: * iteration. jpayne@69: * @return FALSE if there are no more code units or code points jpayne@69: * before the current position in the iteration range, return TRUE otherwise. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual UBool hasPrevious() = 0; jpayne@69: jpayne@69: /** jpayne@69: * Returns the numeric index in the underlying text-storage jpayne@69: * object of the character returned by first(). Since it's jpayne@69: * possible to create an iterator that iterates across only jpayne@69: * part of a text-storage object, this number isn't jpayne@69: * necessarily 0. jpayne@69: * @returns the numeric index in the underlying text-storage jpayne@69: * object of the character returned by first(). jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: inline int32_t startIndex(void) const; jpayne@69: jpayne@69: /** jpayne@69: * Returns the numeric index in the underlying text-storage jpayne@69: * object of the position immediately BEYOND the character jpayne@69: * returned by last(). jpayne@69: * @return the numeric index in the underlying text-storage jpayne@69: * object of the position immediately BEYOND the character jpayne@69: * returned by last(). jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: inline int32_t endIndex(void) const; jpayne@69: jpayne@69: /** jpayne@69: * Returns the numeric index in the underlying text-storage jpayne@69: * object of the character the iterator currently refers to jpayne@69: * (i.e., the character returned by current()). jpayne@69: * @return the numeric index in the text-storage object of jpayne@69: * the character the iterator currently refers to jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: inline int32_t getIndex(void) const; jpayne@69: jpayne@69: /** jpayne@69: * Returns the length of the entire text in the underlying jpayne@69: * text-storage object. jpayne@69: * @return the length of the entire text in the text-storage object jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: inline int32_t getLength() const; jpayne@69: jpayne@69: /** jpayne@69: * Moves the current position relative to the start or end of the jpayne@69: * iteration range, or relative to the current position itself. jpayne@69: * The movement is expressed in numbers of code units forward jpayne@69: * or backward by specifying a positive or negative delta. jpayne@69: * @param delta the position relative to origin. A positive delta means forward; jpayne@69: * a negative delta means backward. jpayne@69: * @param origin Origin enumeration {kStart, kCurrent, kEnd} jpayne@69: * @return the new position jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual int32_t move(int32_t delta, EOrigin origin) = 0; jpayne@69: jpayne@69: /** jpayne@69: * Moves the current position relative to the start or end of the jpayne@69: * iteration range, or relative to the current position itself. jpayne@69: * The movement is expressed in numbers of code points forward jpayne@69: * or backward by specifying a positive or negative delta. jpayne@69: * @param delta the position relative to origin. A positive delta means forward; jpayne@69: * a negative delta means backward. jpayne@69: * @param origin Origin enumeration {kStart, kCurrent, kEnd} jpayne@69: * @return the new position jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: #ifdef move32 jpayne@69: // One of the system headers right now is sometimes defining a conflicting macro we don't use jpayne@69: #undef move32 jpayne@69: #endif jpayne@69: virtual int32_t move32(int32_t delta, EOrigin origin) = 0; jpayne@69: jpayne@69: /** jpayne@69: * Copies the text under iteration into the UnicodeString jpayne@69: * referred to by "result". jpayne@69: * @param result Receives a copy of the text under iteration. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: virtual void getText(UnicodeString& result) = 0; jpayne@69: jpayne@69: protected: jpayne@69: /** jpayne@69: * Empty constructor. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: CharacterIterator(); jpayne@69: jpayne@69: /** jpayne@69: * Constructor, just setting the length field in this base class. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: CharacterIterator(int32_t length); jpayne@69: jpayne@69: /** jpayne@69: * Constructor, just setting the length and position fields in this base class. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: CharacterIterator(int32_t length, int32_t position); jpayne@69: jpayne@69: /** jpayne@69: * Constructor, just setting the length, start, end, and position fields in this base class. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: CharacterIterator(int32_t length, int32_t textBegin, int32_t textEnd, int32_t position); jpayne@69: jpayne@69: /** jpayne@69: * Copy constructor. jpayne@69: * jpayne@69: * @param that The CharacterIterator to be copied jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: CharacterIterator(const CharacterIterator &that); jpayne@69: jpayne@69: /** jpayne@69: * Assignment operator. Sets this CharacterIterator to have the same behavior, jpayne@69: * as the one passed in. jpayne@69: * @param that The CharacterIterator passed in. jpayne@69: * @return the newly set CharacterIterator. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: CharacterIterator &operator=(const CharacterIterator &that); jpayne@69: jpayne@69: /** jpayne@69: * Base class text length field. jpayne@69: * Necessary this for correct getText() and hashCode(). jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: int32_t textLength; jpayne@69: jpayne@69: /** jpayne@69: * Base class field for the current position. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: int32_t pos; jpayne@69: jpayne@69: /** jpayne@69: * Base class field for the start of the iteration range. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: int32_t begin; jpayne@69: jpayne@69: /** jpayne@69: * Base class field for the end of the iteration range. jpayne@69: * @stable ICU 2.0 jpayne@69: */ jpayne@69: int32_t end; jpayne@69: }; jpayne@69: jpayne@69: inline UBool jpayne@69: ForwardCharacterIterator::operator!=(const ForwardCharacterIterator& that) const { jpayne@69: return !operator==(that); jpayne@69: } jpayne@69: jpayne@69: inline int32_t jpayne@69: CharacterIterator::setToStart() { jpayne@69: return move(0, kStart); jpayne@69: } jpayne@69: jpayne@69: inline int32_t jpayne@69: CharacterIterator::setToEnd() { jpayne@69: return move(0, kEnd); jpayne@69: } jpayne@69: jpayne@69: inline int32_t jpayne@69: CharacterIterator::startIndex(void) const { jpayne@69: return begin; jpayne@69: } jpayne@69: jpayne@69: inline int32_t jpayne@69: CharacterIterator::endIndex(void) const { jpayne@69: return end; jpayne@69: } jpayne@69: jpayne@69: inline int32_t jpayne@69: CharacterIterator::getIndex(void) const { jpayne@69: return pos; jpayne@69: } jpayne@69: jpayne@69: inline int32_t jpayne@69: CharacterIterator::getLength(void) const { jpayne@69: return textLength; jpayne@69: } jpayne@69: jpayne@69: U_NAMESPACE_END jpayne@69: jpayne@69: #endif /* U_SHOW_CPLUSPLUS_API */ jpayne@69: jpayne@69: #endif