uchriter.cpp 7.8 KB

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