1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
5 * Copyright (C) 1998-2012, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ******************************************************************************
10 #include "utypeinfo.h" // for 'typeid' to work
12 #include "unicode/uchriter.h"
13 #include "unicode/ustring.h"
14 #include "unicode/utf16.h"
19 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UCharCharacterIterator)
21 UCharCharacterIterator::UCharCharacterIterator()
22 : CharacterIterator(),
25 // never default construct!
28 UCharCharacterIterator::UCharCharacterIterator(const UChar* textPtr,
30 : CharacterIterator(textPtr != 0 ? (length>=0 ? length : u_strlen(textPtr)) : 0),
35 UCharCharacterIterator::UCharCharacterIterator(const UChar* textPtr,
38 : CharacterIterator(textPtr != 0 ? (length>=0 ? length : u_strlen(textPtr)) : 0, position),
43 UCharCharacterIterator::UCharCharacterIterator(const UChar* textPtr,
48 : CharacterIterator(textPtr != 0 ? (length>=0 ? length : u_strlen(textPtr)) : 0, textBegin, textEnd, position),
53 UCharCharacterIterator::UCharCharacterIterator(const UCharCharacterIterator& that)
54 : CharacterIterator(that),
59 UCharCharacterIterator&
60 UCharCharacterIterator::operator=(const UCharCharacterIterator& that) {
61 CharacterIterator::operator=(that);
66 UCharCharacterIterator::~UCharCharacterIterator() {
70 UCharCharacterIterator::operator==(const ForwardCharacterIterator& that) const {
74 if (typeid(*this) != typeid(that)) {
78 UCharCharacterIterator& realThat = (UCharCharacterIterator&)that;
80 return text == realThat.text
81 && textLength == realThat.textLength
82 && pos == realThat.pos
83 && begin == realThat.begin
84 && end == realThat.end;
88 UCharCharacterIterator::hashCode() const {
89 return ustr_hashUCharsN(text, textLength) ^ pos ^ begin ^ end;
93 UCharCharacterIterator::clone() const {
94 return new UCharCharacterIterator(*this);
98 UCharCharacterIterator::first() {
108 UCharCharacterIterator::firstPostInc() {
118 UCharCharacterIterator::last() {
128 UCharCharacterIterator::setIndex(int32_t position) {
129 if(position < begin) {
131 } else if(position > end) {
144 UCharCharacterIterator::current() const {
145 if (pos >= begin && pos < end) {
153 UCharCharacterIterator::next() {
157 /* make current() return DONE */
164 UCharCharacterIterator::nextPostInc() {
173 UCharCharacterIterator::hasNext() {
174 return (UBool)(pos < end ? TRUE : FALSE);
178 UCharCharacterIterator::previous() {
187 UCharCharacterIterator::hasPrevious() {
188 return (UBool)(pos > begin ? TRUE : FALSE);
192 UCharCharacterIterator::first32() {
197 U16_NEXT(text, i, end, c);
205 UCharCharacterIterator::first32PostInc() {
209 U16_NEXT(text, pos, end, c);
217 UCharCharacterIterator::last32() {
221 U16_PREV(text, begin, pos, c);
229 UCharCharacterIterator::setIndex32(int32_t position) {
230 if(position < begin) {
232 } else if(position > end) {
236 U16_SET_CP_START(text, begin, position);
237 int32_t i = this->pos = position;
239 U16_NEXT(text, i, end, c);
242 this->pos = position;
248 UCharCharacterIterator::current32() const {
249 if (pos >= begin && pos < end) {
251 U16_GET(text, begin, pos, end, c);
259 UCharCharacterIterator::next32() {
261 U16_FWD_1(text, pos, end);
265 U16_NEXT(text, i, end, c);
269 /* make current() return DONE */
275 UCharCharacterIterator::next32PostInc() {
278 U16_NEXT(text, pos, end, c);
286 UCharCharacterIterator::previous32() {
289 U16_PREV(text, begin, pos, c);
297 UCharCharacterIterator::move(int32_t delta, CharacterIterator::EOrigin origin) {
314 } else if(pos > end) {
322 UCharCharacterIterator::move32(int32_t delta, CharacterIterator::EOrigin origin) {
323 // this implementation relies on the "safe" version of the UTF macros
324 // (or the trustworthiness of the caller)
329 U16_FWD_N(text, pos, end, delta);
334 U16_FWD_N(text, pos, end, delta);
336 U16_BACK_N(text, begin, pos, -delta);
342 U16_BACK_N(text, begin, pos, -delta);
352 void UCharCharacterIterator::setText(const UChar* newText,
353 int32_t newTextLength) {
355 if(newText == 0 || newTextLength < 0) {
358 end = textLength = newTextLength;
363 UCharCharacterIterator::getText(UnicodeString& result) {
364 result = UnicodeString(text, textLength);