uchriter.h 13 KB

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