uchriter.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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-2012, International Business Machines Corporation and
  6. * others. All Rights Reserved.
  7. ******************************************************************************
  8. */
  9. #include "utypeinfo.h" // for 'typeid' to work
  10. #include "unicode/uchriter.h"
  11. #include "unicode/ustring.h"
  12. #include "unicode/utf16.h"
  13. #include "ustr_imp.h"
  14. U_NAMESPACE_BEGIN
  15. UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UCharCharacterIterator)
  16. UCharCharacterIterator::UCharCharacterIterator()
  17. : CharacterIterator(),
  18. text(nullptr)
  19. {
  20. // never default construct!
  21. }
  22. UCharCharacterIterator::UCharCharacterIterator(ConstChar16Ptr textPtr,
  23. int32_t length)
  24. : CharacterIterator(textPtr != nullptr ? (length >= 0 ? length : u_strlen(textPtr)) : 0),
  25. text(textPtr)
  26. {
  27. }
  28. UCharCharacterIterator::UCharCharacterIterator(ConstChar16Ptr textPtr,
  29. int32_t length,
  30. int32_t position)
  31. : CharacterIterator(textPtr != nullptr ? (length >= 0 ? length : u_strlen(textPtr)) : 0, position),
  32. text(textPtr)
  33. {
  34. }
  35. UCharCharacterIterator::UCharCharacterIterator(ConstChar16Ptr textPtr,
  36. int32_t length,
  37. int32_t textBegin,
  38. int32_t textEnd,
  39. int32_t position)
  40. : CharacterIterator(textPtr != nullptr ? (length >= 0 ? length : u_strlen(textPtr)) : 0,
  41. textBegin, textEnd, position),
  42. text(textPtr)
  43. {
  44. }
  45. UCharCharacterIterator::UCharCharacterIterator(const UCharCharacterIterator& that)
  46. : CharacterIterator(that),
  47. text(that.text)
  48. {
  49. }
  50. UCharCharacterIterator&
  51. UCharCharacterIterator::operator=(const UCharCharacterIterator& that) {
  52. CharacterIterator::operator=(that);
  53. text = that.text;
  54. return *this;
  55. }
  56. UCharCharacterIterator::~UCharCharacterIterator() {
  57. }
  58. bool
  59. UCharCharacterIterator::operator==(const ForwardCharacterIterator& that) const {
  60. if (this == &that) {
  61. return true;
  62. }
  63. if (typeid(*this) != typeid(that)) {
  64. return false;
  65. }
  66. const UCharCharacterIterator& realThat = static_cast<const UCharCharacterIterator&>(that);
  67. return text == realThat.text
  68. && textLength == realThat.textLength
  69. && pos == realThat.pos
  70. && begin == realThat.begin
  71. && end == realThat.end;
  72. }
  73. int32_t
  74. UCharCharacterIterator::hashCode() const {
  75. return ustr_hashUCharsN(text, textLength) ^ pos ^ begin ^ end;
  76. }
  77. UCharCharacterIterator*
  78. UCharCharacterIterator::clone() const {
  79. return new UCharCharacterIterator(*this);
  80. }
  81. char16_t
  82. UCharCharacterIterator::first() {
  83. pos = begin;
  84. if(pos < end) {
  85. return text[pos];
  86. } else {
  87. return DONE;
  88. }
  89. }
  90. char16_t
  91. UCharCharacterIterator::firstPostInc() {
  92. pos = begin;
  93. if(pos < end) {
  94. return text[pos++];
  95. } else {
  96. return DONE;
  97. }
  98. }
  99. char16_t
  100. UCharCharacterIterator::last() {
  101. pos = end;
  102. if(pos > begin) {
  103. return text[--pos];
  104. } else {
  105. return DONE;
  106. }
  107. }
  108. char16_t
  109. UCharCharacterIterator::setIndex(int32_t position) {
  110. if(position < begin) {
  111. pos = begin;
  112. } else if(position > end) {
  113. pos = end;
  114. } else {
  115. pos = position;
  116. }
  117. if(pos < end) {
  118. return text[pos];
  119. } else {
  120. return DONE;
  121. }
  122. }
  123. char16_t
  124. UCharCharacterIterator::current() const {
  125. if (pos >= begin && pos < end) {
  126. return text[pos];
  127. } else {
  128. return DONE;
  129. }
  130. }
  131. char16_t
  132. UCharCharacterIterator::next() {
  133. if (pos + 1 < end) {
  134. return text[++pos];
  135. } else {
  136. /* make current() return DONE */
  137. pos = end;
  138. return DONE;
  139. }
  140. }
  141. char16_t
  142. UCharCharacterIterator::nextPostInc() {
  143. if (pos < end) {
  144. return text[pos++];
  145. } else {
  146. return DONE;
  147. }
  148. }
  149. UBool
  150. UCharCharacterIterator::hasNext() {
  151. return pos < end;
  152. }
  153. char16_t
  154. UCharCharacterIterator::previous() {
  155. if (pos > begin) {
  156. return text[--pos];
  157. } else {
  158. return DONE;
  159. }
  160. }
  161. UBool
  162. UCharCharacterIterator::hasPrevious() {
  163. return pos > begin;
  164. }
  165. UChar32
  166. UCharCharacterIterator::first32() {
  167. pos = begin;
  168. if(pos < end) {
  169. int32_t i = pos;
  170. UChar32 c;
  171. U16_NEXT(text, i, end, c);
  172. return c;
  173. } else {
  174. return DONE;
  175. }
  176. }
  177. UChar32
  178. UCharCharacterIterator::first32PostInc() {
  179. pos = begin;
  180. if(pos < end) {
  181. UChar32 c;
  182. U16_NEXT(text, pos, end, c);
  183. return c;
  184. } else {
  185. return DONE;
  186. }
  187. }
  188. UChar32
  189. UCharCharacterIterator::last32() {
  190. pos = end;
  191. if(pos > begin) {
  192. UChar32 c;
  193. U16_PREV(text, begin, pos, c);
  194. return c;
  195. } else {
  196. return DONE;
  197. }
  198. }
  199. UChar32
  200. UCharCharacterIterator::setIndex32(int32_t position) {
  201. if(position < begin) {
  202. position = begin;
  203. } else if(position > end) {
  204. position = end;
  205. }
  206. if(position < end) {
  207. U16_SET_CP_START(text, begin, position);
  208. int32_t i = this->pos = position;
  209. UChar32 c;
  210. U16_NEXT(text, i, end, c);
  211. return c;
  212. } else {
  213. this->pos = position;
  214. return DONE;
  215. }
  216. }
  217. UChar32
  218. UCharCharacterIterator::current32() const {
  219. if (pos >= begin && pos < end) {
  220. UChar32 c;
  221. U16_GET(text, begin, pos, end, c);
  222. return c;
  223. } else {
  224. return DONE;
  225. }
  226. }
  227. UChar32
  228. UCharCharacterIterator::next32() {
  229. if (pos < end) {
  230. U16_FWD_1(text, pos, end);
  231. if(pos < end) {
  232. int32_t i = pos;
  233. UChar32 c;
  234. U16_NEXT(text, i, end, c);
  235. return c;
  236. }
  237. }
  238. /* make current() return DONE */
  239. pos = end;
  240. return DONE;
  241. }
  242. UChar32
  243. UCharCharacterIterator::next32PostInc() {
  244. if (pos < end) {
  245. UChar32 c;
  246. U16_NEXT(text, pos, end, c);
  247. return c;
  248. } else {
  249. return DONE;
  250. }
  251. }
  252. UChar32
  253. UCharCharacterIterator::previous32() {
  254. if (pos > begin) {
  255. UChar32 c;
  256. U16_PREV(text, begin, pos, c);
  257. return c;
  258. } else {
  259. return DONE;
  260. }
  261. }
  262. int32_t
  263. UCharCharacterIterator::move(int32_t delta, CharacterIterator::EOrigin origin) {
  264. switch(origin) {
  265. case kStart:
  266. pos = begin + delta;
  267. break;
  268. case kCurrent:
  269. pos += delta;
  270. break;
  271. case kEnd:
  272. pos = end + delta;
  273. break;
  274. default:
  275. break;
  276. }
  277. if(pos < begin) {
  278. pos = begin;
  279. } else if(pos > end) {
  280. pos = end;
  281. }
  282. return pos;
  283. }
  284. int32_t
  285. UCharCharacterIterator::move32(int32_t delta, CharacterIterator::EOrigin origin) {
  286. // this implementation relies on the "safe" version of the UTF macros
  287. // (or the trustworthiness of the caller)
  288. switch(origin) {
  289. case kStart:
  290. pos = begin;
  291. if(delta > 0) {
  292. U16_FWD_N(text, pos, end, delta);
  293. }
  294. break;
  295. case kCurrent:
  296. if(delta > 0) {
  297. U16_FWD_N(text, pos, end, delta);
  298. } else {
  299. U16_BACK_N(text, begin, pos, -delta);
  300. }
  301. break;
  302. case kEnd:
  303. pos = end;
  304. if(delta < 0) {
  305. U16_BACK_N(text, begin, pos, -delta);
  306. }
  307. break;
  308. default:
  309. break;
  310. }
  311. return pos;
  312. }
  313. void UCharCharacterIterator::setText(ConstChar16Ptr newText,
  314. int32_t newTextLength) {
  315. text = newText;
  316. if (newText == nullptr || newTextLength < 0) {
  317. newTextLength = 0;
  318. }
  319. end = textLength = newTextLength;
  320. pos = begin = 0;
  321. }
  322. void
  323. UCharCharacterIterator::getText(UnicodeString& result) {
  324. result = UnicodeString(text, textLength);
  325. }
  326. U_NAMESPACE_END