uchriter.h 13 KB

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