uchriter.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. **********************************************************************
  3. * Copyright (C) 1998-2005, International Business Machines
  4. * Corporation and others. All Rights Reserved.
  5. **********************************************************************
  6. */
  7. #ifndef UCHRITER_H
  8. #define UCHRITER_H
  9. #include "unicode/utypes.h"
  10. #include "unicode/chariter.h"
  11. /**
  12. * \file
  13. * \brief C++ API: UChar Character Iterator
  14. */
  15. U_NAMESPACE_BEGIN
  16. /**
  17. * A concrete subclass of CharacterIterator that iterates over the
  18. * characters (code units or code points) in a UChar array.
  19. * It's possible not only to create an
  20. * iterator that iterates over an entire UChar array, but also to
  21. * create one that iterates over only a subrange of a UChar array
  22. * (iterators over different subranges of the same UChar array don't
  23. * compare equal).
  24. * @see CharacterIterator
  25. * @see ForwardCharacterIterator
  26. * @stable ICU 2.0
  27. */
  28. class U_COMMON_API UCharCharacterIterator : public CharacterIterator {
  29. public:
  30. /**
  31. * Create an iterator over the UChar array referred to by "textPtr".
  32. * The iteration range is 0 to <code>length-1</code>.
  33. * text is only aliased, not adopted (the
  34. * destructor will not delete it).
  35. * @param textPtr The UChar array to be iterated over
  36. * @param length The length of the UChar array
  37. * @stable ICU 2.0
  38. */
  39. UCharCharacterIterator(const UChar* textPtr, int32_t length);
  40. /**
  41. * Create an iterator over the UChar array referred to by "textPtr".
  42. * The iteration range is 0 to <code>length-1</code>.
  43. * text is only aliased, not adopted (the
  44. * destructor will not delete it).
  45. * The starting
  46. * position is specified by "position". If "position" is outside the valid
  47. * iteration range, the behavior of this object is undefined.
  48. * @param textPtr The UChar array to be iteratd over
  49. * @param length The length of the UChar array
  50. * @param position The starting position of the iteration
  51. * @stable ICU 2.0
  52. */
  53. UCharCharacterIterator(const UChar* textPtr, int32_t length,
  54. int32_t position);
  55. /**
  56. * Create an iterator over the UChar array referred to by "textPtr".
  57. * The iteration range is 0 to <code>end-1</code>.
  58. * text is only aliased, not adopted (the
  59. * destructor will not delete it).
  60. * The starting
  61. * position is specified by "position". If begin and end do not
  62. * form a valid iteration range or "position" is outside the valid
  63. * iteration range, the behavior of this object is undefined.
  64. * @param textPtr The UChar array to be iterated over
  65. * @param length The length of the UChar array
  66. * @param textBegin The begin position of the iteration range
  67. * @param textEnd The end position of the iteration range
  68. * @param position The starting position of the iteration
  69. * @stable ICU 2.0
  70. */
  71. UCharCharacterIterator(const UChar* textPtr, int32_t length,
  72. int32_t textBegin,
  73. int32_t textEnd,
  74. int32_t position);
  75. /**
  76. * Copy constructor. The new iterator iterates over the same range
  77. * of the same string as "that", and its initial position is the
  78. * same as "that"'s current position.
  79. * @param that The UCharCharacterIterator to be copied
  80. * @stable ICU 2.0
  81. */
  82. UCharCharacterIterator(const UCharCharacterIterator& that);
  83. /**
  84. * Destructor.
  85. * @stable ICU 2.0
  86. */
  87. virtual ~UCharCharacterIterator();
  88. /**
  89. * Assignment operator. *this is altered to iterate over the sane
  90. * range of the same string as "that", and refers to the same
  91. * character within that string as "that" does.
  92. * @param that The object to be copied
  93. * @return the newly created object
  94. * @stable ICU 2.0
  95. */
  96. UCharCharacterIterator&
  97. operator=(const UCharCharacterIterator& that);
  98. /**
  99. * Returns true if the iterators iterate over the same range of the
  100. * same string and are pointing at the same character.
  101. * @param that The ForwardCharacterIterator used to be compared for equality
  102. * @return true if the iterators iterate over the same range of the
  103. * same string and are pointing at the same character.
  104. * @stable ICU 2.0
  105. */
  106. virtual UBool operator==(const ForwardCharacterIterator& that) const;
  107. /**
  108. * Generates a hash code for this iterator.
  109. * @return the hash code.
  110. * @stable ICU 2.0
  111. */
  112. virtual int32_t hashCode(void) const;
  113. /**
  114. * Returns a new UCharCharacterIterator referring to the same
  115. * character in the same range of the same string as this one. The
  116. * caller must delete the new iterator.
  117. * @return the CharacterIterator newly created
  118. * @stable ICU 2.0
  119. */
  120. virtual CharacterIterator* clone(void) const;
  121. /**
  122. * Sets the iterator to refer to the first code unit in its
  123. * iteration range, and returns that code unit.
  124. * This can be used to begin an iteration with next().
  125. * @return the first code unit in its iteration range.
  126. * @stable ICU 2.0
  127. */
  128. virtual UChar first(void);
  129. /**
  130. * Sets the iterator to refer to the first code unit in its
  131. * iteration range, returns that code unit, and moves the position
  132. * to the second code unit. This is an alternative to setToStart()
  133. * for forward iteration with nextPostInc().
  134. * @return the first code unit in its iteration range
  135. * @stable ICU 2.0
  136. */
  137. virtual UChar firstPostInc(void);
  138. /**
  139. * Sets the iterator to refer to the first code point in its
  140. * iteration range, and returns that code unit,
  141. * This can be used to begin an iteration with next32().
  142. * Note that an iteration with next32PostInc(), beginning with,
  143. * e.g., setToStart() or firstPostInc(), is more efficient.
  144. * @return the first code point in its iteration range
  145. * @stable ICU 2.0
  146. */
  147. virtual UChar32 first32(void);
  148. /**
  149. * Sets the iterator to refer to the first code point in its
  150. * iteration range, returns that code point, and moves the position
  151. * to the second code point. This is an alternative to setToStart()
  152. * for forward iteration with next32PostInc().
  153. * @return the first code point in its iteration range.
  154. * @stable ICU 2.0
  155. */
  156. virtual UChar32 first32PostInc(void);
  157. /**
  158. * Sets the iterator to refer to the last code unit in its
  159. * iteration range, and returns that code unit.
  160. * This can be used to begin an iteration with previous().
  161. * @return the last code unit in its iteration range.
  162. * @stable ICU 2.0
  163. */
  164. virtual UChar last(void);
  165. /**
  166. * Sets the iterator to refer to the last code point in its
  167. * iteration range, and returns that code unit.
  168. * This can be used to begin an iteration with previous32().
  169. * @return the last code point in its iteration range.
  170. * @stable ICU 2.0
  171. */
  172. virtual UChar32 last32(void);
  173. /**
  174. * Sets the iterator to refer to the "position"-th code unit
  175. * in the text-storage object the iterator refers to, and
  176. * returns that code unit.
  177. * @param position the position within the text-storage object
  178. * @return the code unit
  179. * @stable ICU 2.0
  180. */
  181. virtual UChar setIndex(int32_t position);
  182. /**
  183. * Sets the iterator to refer to the beginning of the code point
  184. * that contains the "position"-th code unit
  185. * in the text-storage object the iterator refers to, and
  186. * returns that code point.
  187. * The current position is adjusted to the beginning of the code point
  188. * (its first code unit).
  189. * @param position the position within the text-storage object
  190. * @return the code unit
  191. * @stable ICU 2.0
  192. */
  193. virtual UChar32 setIndex32(int32_t position);
  194. /**
  195. * Returns the code unit the iterator currently refers to.
  196. * @return the code unit the iterator currently refers to.
  197. * @stable ICU 2.0
  198. */
  199. virtual UChar current(void) const;
  200. /**
  201. * Returns the code point the iterator currently refers to.
  202. * @return the code point the iterator currently refers to.
  203. * @stable ICU 2.0
  204. */
  205. virtual UChar32 current32(void) const;
  206. /**
  207. * Advances to the next code unit in the iteration range (toward
  208. * endIndex()), and returns that code unit. If there are no more
  209. * code units to return, returns DONE.
  210. * @return the next code unit in the iteration range.
  211. * @stable ICU 2.0
  212. */
  213. virtual UChar next(void);
  214. /**
  215. * Gets the current code unit for returning and advances to the next code unit
  216. * in the iteration range
  217. * (toward endIndex()). If there are
  218. * no more code units to return, returns DONE.
  219. * @return the current code unit.
  220. * @stable ICU 2.0
  221. */
  222. virtual UChar nextPostInc(void);
  223. /**
  224. * Advances to the next code point in the iteration range (toward
  225. * endIndex()), and returns that code point. If there are no more
  226. * code points to return, returns DONE.
  227. * Note that iteration with "pre-increment" semantics is less
  228. * efficient than iteration with "post-increment" semantics
  229. * that is provided by next32PostInc().
  230. * @return the next code point in the iteration range.
  231. * @stable ICU 2.0
  232. */
  233. virtual UChar32 next32(void);
  234. /**
  235. * Gets the current code point for returning and advances to the next code point
  236. * in the iteration range
  237. * (toward endIndex()). If there are
  238. * no more code points to return, returns DONE.
  239. * @return the current point.
  240. * @stable ICU 2.0
  241. */
  242. virtual UChar32 next32PostInc(void);
  243. /**
  244. * Returns FALSE if there are no more code units or code points
  245. * at or after the current position in the iteration range.
  246. * This is used with nextPostInc() or next32PostInc() in forward
  247. * iteration.
  248. * @return FALSE if there are no more code units or code points
  249. * at or after the current position in the iteration range.
  250. * @stable ICU 2.0
  251. */
  252. virtual UBool hasNext();
  253. /**
  254. * Advances to the previous code unit in the iteration range (toward
  255. * startIndex()), and returns that code unit. If there are no more
  256. * code units to return, returns DONE.
  257. * @return the previous code unit in the iteration range.
  258. * @stable ICU 2.0
  259. */
  260. virtual UChar previous(void);
  261. /**
  262. * Advances to the previous code point in the iteration range (toward
  263. * startIndex()), and returns that code point. If there are no more
  264. * code points to return, returns DONE.
  265. * @return the previous code point in the iteration range.
  266. * @stable ICU 2.0
  267. */
  268. virtual UChar32 previous32(void);
  269. /**
  270. * Returns FALSE if there are no more code units or code points
  271. * before the current position in the iteration range.
  272. * This is used with previous() or previous32() in backward
  273. * iteration.
  274. * @return FALSE if there are no more code units or code points
  275. * before the current position in the iteration range.
  276. * @stable ICU 2.0
  277. */
  278. virtual UBool hasPrevious();
  279. /**
  280. * Moves the current position relative to the start or end of the
  281. * iteration range, or relative to the current position itself.
  282. * The movement is expressed in numbers of code units forward
  283. * or backward by specifying a positive or negative delta.
  284. * @param delta the position relative to origin. A positive delta means forward;
  285. * a negative delta means backward.
  286. * @param origin Origin enumeration {kStart, kCurrent, kEnd}
  287. * @return the new position
  288. * @stable ICU 2.0
  289. */
  290. virtual int32_t move(int32_t delta, EOrigin origin);
  291. /**
  292. * Moves the current position relative to the start or end of the
  293. * iteration range, or relative to the current position itself.
  294. * The movement is expressed in numbers of code points forward
  295. * or backward by specifying a positive or negative delta.
  296. * @param delta the position relative to origin. A positive delta means forward;
  297. * a negative delta means backward.
  298. * @param origin Origin enumeration {kStart, kCurrent, kEnd}
  299. * @return the new position
  300. * @stable ICU 2.0
  301. */
  302. virtual int32_t move32(int32_t delta, EOrigin origin);
  303. /**
  304. * Sets the iterator to iterate over a new range of text
  305. * @stable ICU 2.0
  306. */
  307. void setText(const UChar* newText, int32_t newTextLength);
  308. /**
  309. * Copies the UChar array under iteration into the UnicodeString
  310. * referred to by "result". Even if this iterator iterates across
  311. * only a part of this string, the whole string is copied.
  312. * @param result Receives a copy of the text under iteration.
  313. * @stable ICU 2.0
  314. */
  315. virtual void getText(UnicodeString& result);
  316. /**
  317. * Return a class ID for this class (not really public)
  318. * @return a class ID for this class
  319. * @stable ICU 2.0
  320. */
  321. static UClassID U_EXPORT2 getStaticClassID(void);
  322. /**
  323. * Return a class ID for this object (not really public)
  324. * @return a class ID for this object.
  325. * @stable ICU 2.0
  326. */
  327. virtual UClassID getDynamicClassID(void) const;
  328. protected:
  329. /**
  330. * Protected constructor
  331. * @stable ICU 2.0
  332. */
  333. UCharCharacterIterator();
  334. /**
  335. * Protected member text
  336. * @stable ICU 2.0
  337. */
  338. const UChar* text;
  339. };
  340. U_NAMESPACE_END
  341. #endif