Coverage Report

Created: 2025-06-24 06:43

/src/icu/source/common/uchriter.cpp
Line
Count
Source (jump to first uncovered line)
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(0)
24
0
{
25
    // never default construct!
26
0
}
27
28
UCharCharacterIterator::UCharCharacterIterator(ConstChar16Ptr textPtr,
29
                                               int32_t length)
30
0
  : CharacterIterator(textPtr != 0 ? (length>=0 ? length : u_strlen(textPtr)) : 0),
31
0
  text(textPtr)
32
0
{
33
0
}
34
35
UCharCharacterIterator::UCharCharacterIterator(ConstChar16Ptr textPtr,
36
                                               int32_t length,
37
                                               int32_t position)
38
0
  : CharacterIterator(textPtr != 0 ? (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 != 0 ? (length>=0 ? length : u_strlen(textPtr)) : 0, textBegin, textEnd, position),
49
0
  text(textPtr)
50
0
{
51
0
}
52
53
UCharCharacterIterator::UCharCharacterIterator(const UCharCharacterIterator& that)
54
0
: CharacterIterator(that),
55
0
  text(that.text)
56
0
{
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
0
}
65
66
0
UCharCharacterIterator::~UCharCharacterIterator() {
67
0
}
68
69
bool
70
0
UCharCharacterIterator::operator==(const ForwardCharacterIterator& that) const {
71
0
    if (this == &that) {
72
0
        return TRUE;
73
0
    }
74
0
    if (typeid(*this) != typeid(that)) {
75
0
        return FALSE;
76
0
    }
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
0
}
86
87
int32_t
88
0
UCharCharacterIterator::hashCode() const {
89
0
    return ustr_hashUCharsN(text, textLength) ^ pos ^ begin ^ end;
90
0
}
91
92
UCharCharacterIterator*
93
0
UCharCharacterIterator::clone() const {
94
0
    return new UCharCharacterIterator(*this);
95
0
}
96
97
UChar
98
0
UCharCharacterIterator::first() {
99
0
    pos = begin;
100
0
    if(pos < end) {
101
0
        return text[pos];
102
0
    } else {
103
0
        return DONE;
104
0
    }
105
0
}
106
107
UChar
108
0
UCharCharacterIterator::firstPostInc() {
109
0
    pos = begin;
110
0
    if(pos < end) {
111
0
        return text[pos++];
112
0
    } else {
113
0
        return DONE;
114
0
    }
115
0
}
116
117
UChar
118
0
UCharCharacterIterator::last() {
119
0
    pos = end;
120
0
    if(pos > begin) {
121
0
        return text[--pos];
122
0
    } else {
123
0
        return DONE;
124
0
    }
125
0
}
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
0
    } else {
134
0
        pos = position;
135
0
    }
136
0
    if(pos < end) {
137
0
        return text[pos];
138
0
    } else {
139
0
        return DONE;
140
0
    }
141
0
}
142
143
UChar
144
0
UCharCharacterIterator::current() const {
145
0
    if (pos >= begin && pos < end) {
146
0
        return text[pos];
147
0
    } else {
148
0
        return DONE;
149
0
    }
150
0
}
151
152
UChar
153
0
UCharCharacterIterator::next() {
154
0
    if (pos + 1 < end) {
155
0
        return text[++pos];
156
0
    } else {
157
        /* make current() return DONE */
158
0
        pos = end;
159
0
        return DONE;
160
0
    }
161
0
}
162
163
UChar
164
0
UCharCharacterIterator::nextPostInc() {
165
0
    if (pos < end) {
166
0
        return text[pos++];
167
0
    } else {
168
0
        return DONE;
169
0
    }
170
0
}
171
172
UBool
173
0
UCharCharacterIterator::hasNext() {
174
0
    return (UBool)(pos < end ? TRUE : FALSE);
175
0
}
176
177
UChar
178
0
UCharCharacterIterator::previous() {
179
0
    if (pos > begin) {
180
0
        return text[--pos];
181
0
    } else {
182
0
        return DONE;
183
0
    }
184
0
}
185
186
UBool
187
0
UCharCharacterIterator::hasPrevious() {
188
0
    return (UBool)(pos > begin ? TRUE : FALSE);
189
0
}
190
191
UChar32
192
0
UCharCharacterIterator::first32() {
193
0
    pos = begin;
194
0
    if(pos < end) {
195
0
        int32_t i = pos;
196
0
        UChar32 c;
197
0
        U16_NEXT(text, i, end, c);
198
0
        return c;
199
0
    } else {
200
0
        return DONE;
201
0
    }
202
0
}
203
204
UChar32
205
0
UCharCharacterIterator::first32PostInc() {
206
0
    pos = begin;
207
0
    if(pos < end) {
208
0
        UChar32 c;
209
0
        U16_NEXT(text, pos, end, c);
210
0
        return c;
211
0
    } else {
212
0
        return DONE;
213
0
    }
214
0
}
215
216
UChar32
217
0
UCharCharacterIterator::last32() {
218
0
    pos = end;
219
0
    if(pos > begin) {
220
0
        UChar32 c;
221
0
        U16_PREV(text, begin, pos, c);
222
0
        return c;
223
0
    } else {
224
0
        return DONE;
225
0
    }
226
0
}
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
0
    }
235
0
    if(position < end) {
236
0
        U16_SET_CP_START(text, begin, position);
237
0
        int32_t i = this->pos = position;
238
0
        UChar32 c;
239
0
        U16_NEXT(text, i, end, c);
240
0
        return c;
241
0
    } else {
242
0
        this->pos = position;
243
0
        return DONE;
244
0
    }
245
0
}
246
247
UChar32
248
0
UCharCharacterIterator::current32() const {
249
0
    if (pos >= begin && pos < end) {
250
0
        UChar32 c;
251
0
        U16_GET(text, begin, pos, end, c);
252
0
        return c;
253
0
    } else {
254
0
        return DONE;
255
0
    }
256
0
}
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
0
            UChar32 c;
265
0
            U16_NEXT(text, i, end, c);
266
0
            return c;
267
0
        }
268
0
    }
269
    /* make current() return DONE */
270
0
    pos = end;
271
0
    return DONE;
272
0
}
273
274
UChar32
275
0
UCharCharacterIterator::next32PostInc() {
276
0
    if (pos < end) {
277
0
        UChar32 c;
278
0
        U16_NEXT(text, pos, end, c);
279
0
        return c;
280
0
    } else {
281
0
        return DONE;
282
0
    }
283
0
}
284
285
UChar32
286
0
UCharCharacterIterator::previous32() {
287
0
    if (pos > begin) {
288
0
        UChar32 c;
289
0
        U16_PREV(text, begin, pos, c);
290
0
        return c;
291
0
    } else {
292
0
        return DONE;
293
0
    }
294
0
}
295
296
int32_t
297
0
UCharCharacterIterator::move(int32_t delta, CharacterIterator::EOrigin origin) {
298
0
    switch(origin) {
299
0
    case kStart:
300
0
        pos = begin + delta;
301
0
        break;
302
0
    case kCurrent:
303
0
        pos += delta;
304
0
        break;
305
0
    case kEnd:
306
0
        pos = end + delta;
307
0
        break;
308
0
    default:
309
0
        break;
310
0
    }
311
312
0
    if(pos < begin) {
313
0
        pos = begin;
314
0
    } else if(pos > end) {
315
0
        pos = end;
316
0
    }
317
318
0
    return pos;
319
0
}
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
0
    case kStart:
327
0
        pos = begin;
328
0
        if(delta > 0) {
329
0
            U16_FWD_N(text, pos, end, delta);
330
0
        }
331
0
        break;
332
0
    case kCurrent:
333
0
        if(delta > 0) {
334
0
            U16_FWD_N(text, pos, end, delta);
335
0
        } else {
336
0
            U16_BACK_N(text, begin, pos, -delta);
337
0
        }
338
0
        break;
339
0
    case kEnd:
340
0
        pos = end;
341
0
        if(delta < 0) {
342
0
            U16_BACK_N(text, begin, pos, -delta);
343
0
        }
344
0
        break;
345
0
    default:
346
0
        break;
347
0
    }
348
349
0
    return pos;
350
0
}
351
352
void UCharCharacterIterator::setText(ConstChar16Ptr newText,
353
0
                                     int32_t      newTextLength) {
354
0
    text = newText;
355
0
    if(newText == 0 || newTextLength < 0) {
356
0
        newTextLength = 0;
357
0
    }
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