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