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