]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ******************************************************************************* | |
3 | * | |
73c04bcf | 4 | * Copyright (C) 2002-2006, International Business Machines |
b75a7d8f A |
5 | * Corporation and others. All Rights Reserved. |
6 | * | |
7 | ******************************************************************************* | |
8 | * file name: uiter.cpp | |
9 | * encoding: US-ASCII | |
10 | * tab size: 8 (not used) | |
11 | * indentation:4 | |
12 | * | |
13 | * created on: 2002jan18 | |
14 | * created by: Markus W. Scherer | |
15 | */ | |
16 | ||
17 | #include "unicode/utypes.h" | |
18 | #include "unicode/ustring.h" | |
19 | #include "unicode/chariter.h" | |
20 | #include "unicode/rep.h" | |
21 | #include "unicode/uiter.h" | |
22 | #include "cstring.h" | |
23 | ||
24 | #define IS_EVEN(n) (((n)&1)==0) | |
25 | #define IS_POINTER_EVEN(p) IS_EVEN((size_t)p) | |
26 | ||
27 | U_CDECL_BEGIN | |
28 | ||
29 | /* No-Op UCharIterator implementation for illegal input --------------------- */ | |
30 | ||
31 | static int32_t U_CALLCONV | |
32 | noopGetIndex(UCharIterator * /*iter*/, UCharIteratorOrigin /*origin*/) { | |
33 | return 0; | |
34 | } | |
35 | ||
36 | static int32_t U_CALLCONV | |
37 | noopMove(UCharIterator * /*iter*/, int32_t /*delta*/, UCharIteratorOrigin /*origin*/) { | |
38 | return 0; | |
39 | } | |
40 | ||
41 | static UBool U_CALLCONV | |
42 | noopHasNext(UCharIterator * /*iter*/) { | |
43 | return FALSE; | |
44 | } | |
45 | ||
46 | static UChar32 U_CALLCONV | |
47 | noopCurrent(UCharIterator * /*iter*/) { | |
48 | return U_SENTINEL; | |
49 | } | |
50 | ||
51 | static uint32_t U_CALLCONV | |
52 | noopGetState(const UCharIterator * /*iter*/) { | |
73c04bcf | 53 | return UITER_NO_STATE; |
b75a7d8f A |
54 | } |
55 | ||
56 | static void U_CALLCONV | |
73c04bcf A |
57 | noopSetState(UCharIterator * /*iter*/, uint32_t /*state*/, UErrorCode *pErrorCode) { |
58 | *pErrorCode=U_UNSUPPORTED_ERROR; | |
b75a7d8f A |
59 | } |
60 | ||
61 | static const UCharIterator noopIterator={ | |
62 | 0, 0, 0, 0, 0, 0, | |
63 | noopGetIndex, | |
64 | noopMove, | |
65 | noopHasNext, | |
66 | noopHasNext, | |
67 | noopCurrent, | |
68 | noopCurrent, | |
69 | noopCurrent, | |
70 | NULL, | |
71 | noopGetState, | |
72 | noopSetState | |
73 | }; | |
74 | ||
75 | /* UCharIterator implementation for simple strings -------------------------- */ | |
76 | ||
77 | /* | |
78 | * This is an implementation of a code unit (UChar) iterator | |
79 | * for UChar * strings. | |
80 | * | |
81 | * The UCharIterator.context field holds a pointer to the string. | |
82 | */ | |
83 | ||
84 | static int32_t U_CALLCONV | |
85 | stringIteratorGetIndex(UCharIterator *iter, UCharIteratorOrigin origin) { | |
86 | switch(origin) { | |
87 | case UITER_ZERO: | |
88 | return 0; | |
89 | case UITER_START: | |
90 | return iter->start; | |
91 | case UITER_CURRENT: | |
92 | return iter->index; | |
93 | case UITER_LIMIT: | |
94 | return iter->limit; | |
95 | case UITER_LENGTH: | |
96 | return iter->length; | |
97 | default: | |
98 | /* not a valid origin */ | |
99 | /* Should never get here! */ | |
100 | return -1; | |
101 | } | |
102 | } | |
103 | ||
104 | static int32_t U_CALLCONV | |
105 | stringIteratorMove(UCharIterator *iter, int32_t delta, UCharIteratorOrigin origin) { | |
106 | int32_t pos; | |
107 | ||
108 | switch(origin) { | |
109 | case UITER_ZERO: | |
110 | pos=delta; | |
111 | break; | |
112 | case UITER_START: | |
113 | pos=iter->start+delta; | |
114 | break; | |
115 | case UITER_CURRENT: | |
116 | pos=iter->index+delta; | |
117 | break; | |
118 | case UITER_LIMIT: | |
119 | pos=iter->limit+delta; | |
120 | break; | |
121 | case UITER_LENGTH: | |
122 | pos=iter->length+delta; | |
123 | break; | |
124 | default: | |
125 | return -1; /* Error */ | |
126 | } | |
127 | ||
128 | if(pos<iter->start) { | |
129 | pos=iter->start; | |
130 | } else if(pos>iter->limit) { | |
131 | pos=iter->limit; | |
132 | } | |
133 | ||
134 | return iter->index=pos; | |
135 | } | |
136 | ||
137 | static UBool U_CALLCONV | |
138 | stringIteratorHasNext(UCharIterator *iter) { | |
139 | return iter->index<iter->limit; | |
140 | } | |
141 | ||
142 | static UBool U_CALLCONV | |
143 | stringIteratorHasPrevious(UCharIterator *iter) { | |
144 | return iter->index>iter->start; | |
145 | } | |
146 | ||
147 | static UChar32 U_CALLCONV | |
148 | stringIteratorCurrent(UCharIterator *iter) { | |
149 | if(iter->index<iter->limit) { | |
150 | return ((const UChar *)(iter->context))[iter->index]; | |
151 | } else { | |
152 | return U_SENTINEL; | |
153 | } | |
154 | } | |
155 | ||
156 | static UChar32 U_CALLCONV | |
157 | stringIteratorNext(UCharIterator *iter) { | |
158 | if(iter->index<iter->limit) { | |
159 | return ((const UChar *)(iter->context))[iter->index++]; | |
160 | } else { | |
161 | return U_SENTINEL; | |
162 | } | |
163 | } | |
164 | ||
165 | static UChar32 U_CALLCONV | |
166 | stringIteratorPrevious(UCharIterator *iter) { | |
167 | if(iter->index>iter->start) { | |
168 | return ((const UChar *)(iter->context))[--iter->index]; | |
169 | } else { | |
170 | return U_SENTINEL; | |
171 | } | |
172 | } | |
173 | ||
174 | static uint32_t U_CALLCONV | |
175 | stringIteratorGetState(const UCharIterator *iter) { | |
176 | return (uint32_t)iter->index; | |
177 | } | |
178 | ||
179 | static void U_CALLCONV | |
180 | stringIteratorSetState(UCharIterator *iter, uint32_t state, UErrorCode *pErrorCode) { | |
181 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { | |
182 | /* do nothing */ | |
183 | } else if(iter==NULL) { | |
184 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; | |
185 | } else if((int32_t)state<iter->start || iter->limit<(int32_t)state) { | |
186 | *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; | |
187 | } else { | |
188 | iter->index=(int32_t)state; | |
189 | } | |
190 | } | |
191 | ||
192 | static const UCharIterator stringIterator={ | |
193 | 0, 0, 0, 0, 0, 0, | |
194 | stringIteratorGetIndex, | |
195 | stringIteratorMove, | |
196 | stringIteratorHasNext, | |
197 | stringIteratorHasPrevious, | |
198 | stringIteratorCurrent, | |
199 | stringIteratorNext, | |
200 | stringIteratorPrevious, | |
201 | NULL, | |
202 | stringIteratorGetState, | |
203 | stringIteratorSetState | |
204 | }; | |
205 | ||
206 | U_CAPI void U_EXPORT2 | |
207 | uiter_setString(UCharIterator *iter, const UChar *s, int32_t length) { | |
208 | if(iter!=0) { | |
209 | if(s!=0 && length>=-1) { | |
210 | *iter=stringIterator; | |
211 | iter->context=s; | |
212 | if(length>=0) { | |
213 | iter->length=length; | |
214 | } else { | |
215 | iter->length=u_strlen(s); | |
216 | } | |
217 | iter->limit=iter->length; | |
218 | } else { | |
219 | *iter=noopIterator; | |
220 | } | |
221 | } | |
222 | } | |
223 | ||
224 | /* UCharIterator implementation for UTF-16BE strings ------------------------ */ | |
225 | ||
226 | /* | |
227 | * This is an implementation of a code unit (UChar) iterator | |
228 | * for UTF-16BE strings, i.e., strings in byte-vectors where | |
229 | * each UChar is stored as a big-endian pair of bytes. | |
230 | * | |
231 | * The UCharIterator.context field holds a pointer to the string. | |
232 | * Everything works just like with a normal UChar iterator (uiter_setString), | |
233 | * except that UChars are assembled from byte pairs. | |
234 | */ | |
235 | ||
374ca955 A |
236 | /* internal helper function */ |
237 | static inline UChar32 | |
238 | utf16BEIteratorGet(UCharIterator *iter, int32_t index) { | |
239 | const uint8_t *p=(const uint8_t *)iter->context; | |
240 | return ((UChar)p[2*index]<<8)|(UChar)p[2*index+1]; | |
241 | } | |
242 | ||
b75a7d8f A |
243 | static UChar32 U_CALLCONV |
244 | utf16BEIteratorCurrent(UCharIterator *iter) { | |
245 | int32_t index; | |
246 | ||
247 | if((index=iter->index)<iter->limit) { | |
374ca955 | 248 | return utf16BEIteratorGet(iter, index); |
b75a7d8f A |
249 | } else { |
250 | return U_SENTINEL; | |
251 | } | |
252 | } | |
253 | ||
254 | static UChar32 U_CALLCONV | |
255 | utf16BEIteratorNext(UCharIterator *iter) { | |
256 | int32_t index; | |
257 | ||
258 | if((index=iter->index)<iter->limit) { | |
b75a7d8f | 259 | iter->index=index+1; |
374ca955 | 260 | return utf16BEIteratorGet(iter, index); |
b75a7d8f A |
261 | } else { |
262 | return U_SENTINEL; | |
263 | } | |
264 | } | |
265 | ||
266 | static UChar32 U_CALLCONV | |
267 | utf16BEIteratorPrevious(UCharIterator *iter) { | |
268 | int32_t index; | |
269 | ||
270 | if((index=iter->index)>iter->start) { | |
b75a7d8f | 271 | iter->index=--index; |
374ca955 | 272 | return utf16BEIteratorGet(iter, index); |
b75a7d8f A |
273 | } else { |
274 | return U_SENTINEL; | |
275 | } | |
276 | } | |
277 | ||
278 | static const UCharIterator utf16BEIterator={ | |
279 | 0, 0, 0, 0, 0, 0, | |
280 | stringIteratorGetIndex, | |
281 | stringIteratorMove, | |
282 | stringIteratorHasNext, | |
283 | stringIteratorHasPrevious, | |
284 | utf16BEIteratorCurrent, | |
285 | utf16BEIteratorNext, | |
286 | utf16BEIteratorPrevious, | |
287 | NULL, | |
288 | stringIteratorGetState, | |
289 | stringIteratorSetState | |
290 | }; | |
291 | ||
292 | /* | |
293 | * Count the number of UChars in a UTF-16BE string before a terminating UChar NUL, | |
294 | * i.e., before a pair of 0 bytes where the first 0 byte is at an even | |
295 | * offset from s. | |
296 | */ | |
297 | static int32_t | |
298 | utf16BE_strlen(const char *s) { | |
299 | if(IS_POINTER_EVEN(s)) { | |
300 | /* | |
301 | * even-aligned, call u_strlen(s) | |
302 | * we are probably on a little-endian machine, but searching for UChar NUL | |
303 | * does not care about endianness | |
304 | */ | |
305 | return u_strlen((const UChar *)s); | |
306 | } else { | |
307 | /* odd-aligned, search for pair of 0 bytes */ | |
308 | const char *p=s; | |
309 | ||
310 | while(!(*p==0 && p[1]==0)) { | |
311 | p+=2; | |
312 | } | |
313 | return (int32_t)((p-s)/2); | |
314 | } | |
315 | } | |
316 | ||
317 | U_CAPI void U_EXPORT2 | |
318 | uiter_setUTF16BE(UCharIterator *iter, const char *s, int32_t length) { | |
319 | if(iter!=NULL) { | |
320 | /* allow only even-length strings (the input length counts bytes) */ | |
321 | if(s!=NULL && (length==-1 || (length>=0 && IS_EVEN(length)))) { | |
322 | /* length/=2, except that >>=1 also works for -1 (-1/2==0, -1>>1==-1) */ | |
323 | length>>=1; | |
324 | ||
325 | if(U_IS_BIG_ENDIAN && IS_POINTER_EVEN(s)) { | |
326 | /* big-endian machine and 2-aligned UTF-16BE string: use normal UChar iterator */ | |
327 | uiter_setString(iter, (const UChar *)s, length); | |
328 | return; | |
329 | } | |
330 | ||
331 | *iter=utf16BEIterator; | |
332 | iter->context=s; | |
333 | if(length>=0) { | |
334 | iter->length=length; | |
335 | } else { | |
336 | iter->length=utf16BE_strlen(s); | |
337 | } | |
338 | iter->limit=iter->length; | |
339 | } else { | |
340 | *iter=noopIterator; | |
341 | } | |
342 | } | |
343 | } | |
344 | ||
345 | /* UCharIterator wrapper around CharacterIterator --------------------------- */ | |
346 | ||
347 | /* | |
348 | * This is wrapper code around a C++ CharacterIterator to | |
349 | * look like a C UCharIterator. | |
350 | * | |
351 | * The UCharIterator.context field holds a pointer to the CharacterIterator. | |
352 | */ | |
353 | ||
354 | static int32_t U_CALLCONV | |
355 | characterIteratorGetIndex(UCharIterator *iter, UCharIteratorOrigin origin) { | |
356 | switch(origin) { | |
357 | case UITER_ZERO: | |
358 | return 0; | |
359 | case UITER_START: | |
360 | return ((CharacterIterator *)(iter->context))->startIndex(); | |
361 | case UITER_CURRENT: | |
362 | return ((CharacterIterator *)(iter->context))->getIndex(); | |
363 | case UITER_LIMIT: | |
364 | return ((CharacterIterator *)(iter->context))->endIndex(); | |
365 | case UITER_LENGTH: | |
366 | return ((CharacterIterator *)(iter->context))->getLength(); | |
367 | default: | |
368 | /* not a valid origin */ | |
369 | /* Should never get here! */ | |
370 | return -1; | |
371 | } | |
372 | } | |
373 | ||
374 | static int32_t U_CALLCONV | |
375 | characterIteratorMove(UCharIterator *iter, int32_t delta, UCharIteratorOrigin origin) { | |
376 | switch(origin) { | |
377 | case UITER_ZERO: | |
378 | ((CharacterIterator *)(iter->context))->setIndex(delta); | |
379 | return ((CharacterIterator *)(iter->context))->getIndex(); | |
380 | case UITER_START: | |
381 | case UITER_CURRENT: | |
382 | case UITER_LIMIT: | |
383 | return ((CharacterIterator *)(iter->context))->move(delta, (CharacterIterator::EOrigin)origin); | |
384 | case UITER_LENGTH: | |
385 | ((CharacterIterator *)(iter->context))->setIndex(((CharacterIterator *)(iter->context))->getLength()+delta); | |
386 | return ((CharacterIterator *)(iter->context))->getIndex(); | |
387 | default: | |
388 | /* not a valid origin */ | |
389 | /* Should never get here! */ | |
390 | return -1; | |
391 | } | |
392 | } | |
393 | ||
394 | static UBool U_CALLCONV | |
395 | characterIteratorHasNext(UCharIterator *iter) { | |
396 | return ((CharacterIterator *)(iter->context))->hasNext(); | |
397 | } | |
398 | ||
399 | static UBool U_CALLCONV | |
400 | characterIteratorHasPrevious(UCharIterator *iter) { | |
401 | return ((CharacterIterator *)(iter->context))->hasPrevious(); | |
402 | } | |
403 | ||
404 | static UChar32 U_CALLCONV | |
405 | characterIteratorCurrent(UCharIterator *iter) { | |
406 | UChar32 c; | |
407 | ||
408 | c=((CharacterIterator *)(iter->context))->current(); | |
409 | if(c!=0xffff || ((CharacterIterator *)(iter->context))->hasNext()) { | |
410 | return c; | |
411 | } else { | |
412 | return U_SENTINEL; | |
413 | } | |
414 | } | |
415 | ||
416 | static UChar32 U_CALLCONV | |
417 | characterIteratorNext(UCharIterator *iter) { | |
418 | if(((CharacterIterator *)(iter->context))->hasNext()) { | |
419 | return ((CharacterIterator *)(iter->context))->nextPostInc(); | |
420 | } else { | |
421 | return U_SENTINEL; | |
422 | } | |
423 | } | |
424 | ||
425 | static UChar32 U_CALLCONV | |
426 | characterIteratorPrevious(UCharIterator *iter) { | |
427 | if(((CharacterIterator *)(iter->context))->hasPrevious()) { | |
428 | return ((CharacterIterator *)(iter->context))->previous(); | |
429 | } else { | |
430 | return U_SENTINEL; | |
431 | } | |
432 | } | |
433 | ||
434 | static uint32_t U_CALLCONV | |
435 | characterIteratorGetState(const UCharIterator *iter) { | |
436 | return ((CharacterIterator *)(iter->context))->getIndex(); | |
437 | } | |
438 | ||
439 | static void U_CALLCONV | |
440 | characterIteratorSetState(UCharIterator *iter, uint32_t state, UErrorCode *pErrorCode) { | |
441 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { | |
442 | /* do nothing */ | |
443 | } else if(iter==NULL || iter->context==NULL) { | |
444 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; | |
445 | } else if((int32_t)state<((CharacterIterator *)(iter->context))->startIndex() || ((CharacterIterator *)(iter->context))->endIndex()<(int32_t)state) { | |
446 | *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; | |
447 | } else { | |
448 | ((CharacterIterator *)(iter->context))->setIndex((int32_t)state); | |
449 | } | |
450 | } | |
451 | ||
452 | static const UCharIterator characterIteratorWrapper={ | |
453 | 0, 0, 0, 0, 0, 0, | |
454 | characterIteratorGetIndex, | |
455 | characterIteratorMove, | |
456 | characterIteratorHasNext, | |
457 | characterIteratorHasPrevious, | |
458 | characterIteratorCurrent, | |
459 | characterIteratorNext, | |
460 | characterIteratorPrevious, | |
461 | NULL, | |
462 | characterIteratorGetState, | |
463 | characterIteratorSetState | |
464 | }; | |
465 | ||
466 | U_CAPI void U_EXPORT2 | |
467 | uiter_setCharacterIterator(UCharIterator *iter, CharacterIterator *charIter) { | |
468 | if(iter!=0) { | |
469 | if(charIter!=0) { | |
470 | *iter=characterIteratorWrapper; | |
471 | iter->context=charIter; | |
472 | } else { | |
473 | *iter=noopIterator; | |
474 | } | |
475 | } | |
476 | } | |
477 | ||
478 | /* UCharIterator wrapper around Replaceable --------------------------------- */ | |
479 | ||
480 | /* | |
481 | * This is an implementation of a code unit (UChar) iterator | |
482 | * based on a Replaceable object. | |
483 | * | |
484 | * The UCharIterator.context field holds a pointer to the Replaceable. | |
485 | * UCharIterator.length and UCharIterator.index hold Replaceable.length() | |
486 | * and the iteration index. | |
487 | */ | |
488 | ||
489 | static UChar32 U_CALLCONV | |
490 | replaceableIteratorCurrent(UCharIterator *iter) { | |
491 | if(iter->index<iter->limit) { | |
492 | return ((Replaceable *)(iter->context))->charAt(iter->index); | |
493 | } else { | |
494 | return U_SENTINEL; | |
495 | } | |
496 | } | |
497 | ||
498 | static UChar32 U_CALLCONV | |
499 | replaceableIteratorNext(UCharIterator *iter) { | |
500 | if(iter->index<iter->limit) { | |
501 | return ((Replaceable *)(iter->context))->charAt(iter->index++); | |
502 | } else { | |
503 | return U_SENTINEL; | |
504 | } | |
505 | } | |
506 | ||
507 | static UChar32 U_CALLCONV | |
508 | replaceableIteratorPrevious(UCharIterator *iter) { | |
509 | if(iter->index>iter->start) { | |
510 | return ((Replaceable *)(iter->context))->charAt(--iter->index); | |
511 | } else { | |
512 | return U_SENTINEL; | |
513 | } | |
514 | } | |
515 | ||
516 | static const UCharIterator replaceableIterator={ | |
517 | 0, 0, 0, 0, 0, 0, | |
518 | stringIteratorGetIndex, | |
519 | stringIteratorMove, | |
520 | stringIteratorHasNext, | |
521 | stringIteratorHasPrevious, | |
522 | replaceableIteratorCurrent, | |
523 | replaceableIteratorNext, | |
524 | replaceableIteratorPrevious, | |
525 | NULL, | |
526 | stringIteratorGetState, | |
527 | stringIteratorSetState | |
528 | }; | |
529 | ||
530 | U_CAPI void U_EXPORT2 | |
531 | uiter_setReplaceable(UCharIterator *iter, const Replaceable *rep) { | |
532 | if(iter!=0) { | |
533 | if(rep!=0) { | |
534 | *iter=replaceableIterator; | |
535 | iter->context=rep; | |
536 | iter->limit=iter->length=rep->length(); | |
537 | } else { | |
538 | *iter=noopIterator; | |
539 | } | |
540 | } | |
541 | } | |
542 | ||
543 | /* UCharIterator implementation for UTF-8 strings --------------------------- */ | |
544 | ||
545 | /* | |
546 | * Possible, probably necessary only for an implementation for arbitrary | |
547 | * converters: | |
548 | * Maintain a buffer (ring buffer?) for a piece of converted 16-bit text. | |
549 | * This would require to turn reservedFn into a close function and | |
550 | * to introduce a uiter_close(iter). | |
551 | */ | |
552 | ||
553 | #define UITER_CNV_CAPACITY 16 | |
554 | ||
555 | /* | |
556 | * Minimal implementation: | |
557 | * Maintain a single-UChar buffer for an additional surrogate. | |
558 | * The caller must not modify start and limit because they are used internally. | |
559 | * | |
560 | * Use UCharIterator fields as follows: | |
561 | * context pointer to UTF-8 string | |
562 | * length UTF-16 length of the string; -1 until lazy evaluation | |
563 | * start current UTF-8 index | |
564 | * index current UTF-16 index; may be -1="unknown" after setState() | |
565 | * limit UTF-8 length of the string | |
566 | * reservedField supplementary code point | |
567 | * | |
568 | * Since UCharIterator delivers 16-bit code units, the iteration can be | |
569 | * currently in the middle of the byte sequence for a supplementary code point. | |
570 | * In this case, reservedField will contain that code point and start will | |
571 | * point to after the corresponding byte sequence. The UTF-16 index will be | |
572 | * one less than what it would otherwise be corresponding to the UTF-8 index. | |
573 | * Otherwise, reservedField will be 0. | |
574 | */ | |
575 | ||
576 | /* | |
577 | * Possible optimization for NUL-terminated UTF-8 and UTF-16 strings: | |
578 | * Add implementations that do not call strlen() for iteration but check for NUL. | |
579 | */ | |
580 | ||
581 | static int32_t U_CALLCONV | |
582 | utf8IteratorGetIndex(UCharIterator *iter, UCharIteratorOrigin origin) { | |
583 | switch(origin) { | |
584 | case UITER_ZERO: | |
585 | case UITER_START: | |
586 | return 0; | |
587 | case UITER_CURRENT: | |
588 | if(iter->index<0) { | |
589 | /* the current UTF-16 index is unknown after setState(), count from the beginning */ | |
590 | const uint8_t *s; | |
591 | UChar32 c; | |
592 | int32_t i, limit, index; | |
593 | ||
594 | s=(const uint8_t *)iter->context; | |
595 | i=index=0; | |
596 | limit=iter->start; /* count up to the UTF-8 index */ | |
597 | while(i<limit) { | |
598 | U8_NEXT(s, i, limit, c); | |
599 | if(c<=0xffff) { | |
600 | ++index; | |
601 | } else { | |
602 | index+=2; | |
603 | } | |
604 | } | |
605 | ||
606 | iter->start=i; /* just in case setState() did not get us to a code point boundary */ | |
607 | if(i==iter->limit) { | |
608 | iter->length=index; /* in case it was <0 or wrong */ | |
609 | } | |
610 | if(iter->reservedField!=0) { | |
611 | --index; /* we are in the middle of a supplementary code point */ | |
612 | } | |
613 | iter->index=index; | |
614 | } | |
615 | return iter->index; | |
616 | case UITER_LIMIT: | |
617 | case UITER_LENGTH: | |
618 | if(iter->length<0) { | |
619 | const uint8_t *s; | |
620 | UChar32 c; | |
621 | int32_t i, limit, length; | |
622 | ||
623 | s=(const uint8_t *)iter->context; | |
624 | if(iter->index<0) { | |
625 | /* | |
626 | * the current UTF-16 index is unknown after setState(), | |
627 | * we must first count from the beginning to here | |
628 | */ | |
629 | i=length=0; | |
630 | limit=iter->start; | |
631 | ||
632 | /* count from the beginning to the current index */ | |
633 | while(i<limit) { | |
634 | U8_NEXT(s, i, limit, c); | |
635 | if(c<=0xffff) { | |
636 | ++length; | |
637 | } else { | |
638 | length+=2; | |
639 | } | |
640 | } | |
641 | ||
642 | /* assume i==limit==iter->start, set the UTF-16 index */ | |
643 | iter->start=i; /* just in case setState() did not get us to a code point boundary */ | |
644 | iter->index= iter->reservedField!=0 ? length-1 : length; | |
645 | } else { | |
646 | i=iter->start; | |
647 | length=iter->index; | |
648 | if(iter->reservedField!=0) { | |
649 | ++length; | |
650 | } | |
651 | } | |
652 | ||
653 | /* count from the current index to the end */ | |
654 | limit=iter->limit; | |
655 | while(i<limit) { | |
656 | U8_NEXT(s, i, limit, c); | |
657 | if(c<=0xffff) { | |
658 | ++length; | |
659 | } else { | |
660 | length+=2; | |
661 | } | |
662 | } | |
663 | iter->length=length; | |
664 | } | |
665 | return iter->length; | |
666 | default: | |
667 | /* not a valid origin */ | |
668 | /* Should never get here! */ | |
669 | return -1; | |
670 | } | |
671 | } | |
672 | ||
673 | static int32_t U_CALLCONV | |
674 | utf8IteratorMove(UCharIterator *iter, int32_t delta, UCharIteratorOrigin origin) { | |
675 | const uint8_t *s; | |
676 | UChar32 c; | |
677 | int32_t pos; /* requested UTF-16 index */ | |
678 | int32_t i; /* UTF-8 index */ | |
679 | UBool havePos; | |
680 | ||
681 | /* calculate the requested UTF-16 index */ | |
682 | switch(origin) { | |
683 | case UITER_ZERO: | |
684 | case UITER_START: | |
685 | pos=delta; | |
686 | havePos=TRUE; | |
687 | /* iter->index<0 (unknown) is possible */ | |
688 | break; | |
689 | case UITER_CURRENT: | |
690 | if(iter->index>=0) { | |
691 | pos=iter->index+delta; | |
692 | havePos=TRUE; | |
693 | } else { | |
694 | /* the current UTF-16 index is unknown after setState(), use only delta */ | |
695 | pos=0; | |
696 | havePos=FALSE; | |
697 | } | |
698 | break; | |
699 | case UITER_LIMIT: | |
700 | case UITER_LENGTH: | |
701 | if(iter->length>=0) { | |
702 | pos=iter->length+delta; | |
703 | havePos=TRUE; | |
704 | } else { | |
705 | /* pin to the end, avoid counting the length */ | |
706 | iter->index=-1; | |
707 | iter->start=iter->limit; | |
708 | iter->reservedField=0; | |
709 | if(delta>=0) { | |
710 | return UITER_UNKNOWN_INDEX; | |
711 | } else { | |
712 | /* the current UTF-16 index is unknown, use only delta */ | |
713 | pos=0; | |
714 | havePos=FALSE; | |
715 | } | |
716 | } | |
717 | break; | |
718 | default: | |
719 | return -1; /* Error */ | |
720 | } | |
721 | ||
722 | if(havePos) { | |
723 | /* shortcuts: pinning to the edges of the string */ | |
724 | if(pos<=0) { | |
725 | iter->index=iter->start=iter->reservedField=0; | |
726 | return 0; | |
727 | } else if(iter->length>=0 && pos>=iter->length) { | |
728 | iter->index=iter->length; | |
729 | iter->start=iter->limit; | |
730 | iter->reservedField=0; | |
731 | return iter->index; | |
732 | } | |
733 | ||
734 | /* minimize the number of U8_NEXT/PREV operations */ | |
735 | if(iter->index<0 || pos<iter->index/2) { | |
736 | /* go forward from the start instead of backward from the current index */ | |
737 | iter->index=iter->start=iter->reservedField=0; | |
738 | } else if(iter->length>=0 && (iter->length-pos)<(pos-iter->index)) { | |
739 | /* | |
740 | * if we have the UTF-16 index and length and the new position is | |
741 | * closer to the end than the current index, | |
742 | * then go backward from the end instead of forward from the current index | |
743 | */ | |
744 | iter->index=iter->length; | |
745 | iter->start=iter->limit; | |
746 | iter->reservedField=0; | |
747 | } | |
748 | ||
749 | delta=pos-iter->index; | |
750 | if(delta==0) { | |
751 | return iter->index; /* nothing to do */ | |
752 | } | |
753 | } else { | |
754 | /* move relative to unknown UTF-16 index */ | |
755 | if(delta==0) { | |
756 | return UITER_UNKNOWN_INDEX; /* nothing to do */ | |
757 | } else if(-delta>=iter->start) { | |
758 | /* moving backwards by more UChars than there are UTF-8 bytes, pin to 0 */ | |
759 | iter->index=iter->start=iter->reservedField=0; | |
760 | return 0; | |
761 | } else if(delta>=(iter->limit-iter->start)) { | |
762 | /* moving forward by more UChars than the remaining UTF-8 bytes, pin to the end */ | |
763 | iter->index=iter->length; /* may or may not be <0 (unknown) */ | |
764 | iter->start=iter->limit; | |
765 | iter->reservedField=0; | |
374ca955 | 766 | return iter->index>=0 ? iter->index : (int32_t)UITER_UNKNOWN_INDEX; |
b75a7d8f A |
767 | } |
768 | } | |
769 | ||
770 | /* delta!=0 */ | |
771 | ||
772 | /* move towards the requested position, pin to the edges of the string */ | |
773 | s=(const uint8_t *)iter->context; | |
774 | pos=iter->index; /* could be <0 (unknown) */ | |
775 | i=iter->start; | |
776 | if(delta>0) { | |
777 | /* go forward */ | |
778 | int32_t limit=iter->limit; | |
779 | if(iter->reservedField!=0) { | |
780 | iter->reservedField=0; | |
781 | ++pos; | |
782 | --delta; | |
783 | } | |
784 | while(delta>0 && i<limit) { | |
785 | U8_NEXT(s, i, limit, c); | |
786 | if(c<0xffff) { | |
787 | ++pos; | |
788 | --delta; | |
789 | } else if(delta>=2) { | |
790 | pos+=2; | |
791 | delta-=2; | |
792 | } else /* delta==1 */ { | |
793 | /* stop in the middle of a supplementary code point */ | |
794 | iter->reservedField=c; | |
795 | ++pos; | |
796 | break; /* delta=0; */ | |
797 | } | |
798 | } | |
799 | if(i==limit) { | |
800 | if(iter->length<0 && iter->index>=0) { | |
801 | iter->length= iter->reservedField==0 ? pos : pos+1; | |
802 | } else if(iter->index<0 && iter->length>=0) { | |
803 | iter->index= iter->reservedField==0 ? iter->length : iter->length-1; | |
804 | } | |
805 | } | |
806 | } else /* delta<0 */ { | |
807 | /* go backward */ | |
808 | if(iter->reservedField!=0) { | |
809 | iter->reservedField=0; | |
810 | i-=4; /* we stayed behind the supplementary code point; go before it now */ | |
811 | --pos; | |
812 | ++delta; | |
813 | } | |
814 | while(delta<0 && i>0) { | |
815 | U8_PREV(s, 0, i, c); | |
816 | if(c<0xffff) { | |
817 | --pos; | |
818 | ++delta; | |
819 | } else if(delta<=-2) { | |
820 | pos-=2; | |
821 | delta+=2; | |
822 | } else /* delta==-1 */ { | |
823 | /* stop in the middle of a supplementary code point */ | |
824 | i+=4; /* back to behind this supplementary code point for consistent state */ | |
825 | iter->reservedField=c; | |
826 | --pos; | |
827 | break; /* delta=0; */ | |
828 | } | |
829 | } | |
830 | } | |
831 | ||
832 | iter->start=i; | |
833 | if(iter->index>=0) { | |
834 | return iter->index=pos; | |
835 | } else { | |
836 | /* we started with index<0 (unknown) so pos is bogus */ | |
837 | if(i<=1) { | |
838 | return iter->index=i; /* reached the beginning */ | |
839 | } else { | |
840 | /* we still don't know the UTF-16 index */ | |
841 | return UITER_UNKNOWN_INDEX; | |
842 | } | |
843 | } | |
844 | } | |
845 | ||
846 | static UBool U_CALLCONV | |
847 | utf8IteratorHasNext(UCharIterator *iter) { | |
374ca955 | 848 | return iter->start<iter->limit || iter->reservedField!=0; |
b75a7d8f A |
849 | } |
850 | ||
851 | static UBool U_CALLCONV | |
852 | utf8IteratorHasPrevious(UCharIterator *iter) { | |
853 | return iter->start>0; | |
854 | } | |
855 | ||
856 | static UChar32 U_CALLCONV | |
857 | utf8IteratorCurrent(UCharIterator *iter) { | |
858 | if(iter->reservedField!=0) { | |
859 | return U16_TRAIL(iter->reservedField); | |
860 | } else if(iter->start<iter->limit) { | |
861 | const uint8_t *s=(const uint8_t *)iter->context; | |
862 | UChar32 c; | |
863 | int32_t i=iter->start; | |
864 | ||
865 | U8_NEXT(s, i, iter->limit, c); | |
866 | if(c<0) { | |
867 | return 0xfffd; | |
868 | } else if(c<=0xffff) { | |
869 | return c; | |
870 | } else { | |
871 | return U16_LEAD(c); | |
872 | } | |
873 | } else { | |
874 | return U_SENTINEL; | |
875 | } | |
876 | } | |
877 | ||
878 | static UChar32 U_CALLCONV | |
879 | utf8IteratorNext(UCharIterator *iter) { | |
880 | int32_t index; | |
881 | ||
882 | if(iter->reservedField!=0) { | |
883 | UChar trail=U16_TRAIL(iter->reservedField); | |
884 | iter->reservedField=0; | |
885 | if((index=iter->index)>=0) { | |
886 | iter->index=index+1; | |
887 | } | |
888 | return trail; | |
889 | } else if(iter->start<iter->limit) { | |
890 | const uint8_t *s=(const uint8_t *)iter->context; | |
891 | UChar32 c; | |
892 | ||
893 | U8_NEXT(s, iter->start, iter->limit, c); | |
894 | if((index=iter->index)>=0) { | |
895 | iter->index=++index; | |
896 | if(iter->length<0 && iter->start==iter->limit) { | |
897 | iter->length= c<=0xffff ? index : index+1; | |
898 | } | |
899 | } else if(iter->start==iter->limit && iter->length>=0) { | |
900 | iter->index= c<=0xffff ? iter->length : iter->length-1; | |
901 | } | |
902 | if(c<0) { | |
903 | return 0xfffd; | |
904 | } else if(c<=0xffff) { | |
905 | return c; | |
906 | } else { | |
907 | iter->reservedField=c; | |
908 | return U16_LEAD(c); | |
909 | } | |
910 | } else { | |
911 | return U_SENTINEL; | |
912 | } | |
913 | } | |
914 | ||
915 | static UChar32 U_CALLCONV | |
916 | utf8IteratorPrevious(UCharIterator *iter) { | |
917 | int32_t index; | |
918 | ||
919 | if(iter->reservedField!=0) { | |
920 | UChar lead=U16_LEAD(iter->reservedField); | |
921 | iter->reservedField=0; | |
922 | iter->start-=4; /* we stayed behind the supplementary code point; go before it now */ | |
923 | if((index=iter->index)>0) { | |
924 | iter->index=index-1; | |
925 | } | |
926 | return lead; | |
927 | } else if(iter->start>0) { | |
928 | const uint8_t *s=(const uint8_t *)iter->context; | |
929 | UChar32 c; | |
930 | ||
931 | U8_PREV(s, 0, iter->start, c); | |
932 | if((index=iter->index)>0) { | |
933 | iter->index=index-1; | |
934 | } else if(iter->start<=1) { | |
935 | iter->index= c<=0xffff ? iter->start : iter->start+1; | |
936 | } | |
937 | if(c<0) { | |
938 | return 0xfffd; | |
939 | } else if(c<=0xffff) { | |
940 | return c; | |
941 | } else { | |
942 | iter->start+=4; /* back to behind this supplementary code point for consistent state */ | |
943 | iter->reservedField=c; | |
944 | return U16_TRAIL(c); | |
945 | } | |
946 | } else { | |
947 | return U_SENTINEL; | |
948 | } | |
949 | } | |
950 | ||
951 | static uint32_t U_CALLCONV | |
952 | utf8IteratorGetState(const UCharIterator *iter) { | |
953 | uint32_t state=(uint32_t)(iter->start<<1); | |
954 | if(iter->reservedField!=0) { | |
955 | state|=1; | |
956 | } | |
957 | return state; | |
958 | } | |
959 | ||
960 | static void U_CALLCONV | |
961 | utf8IteratorSetState(UCharIterator *iter, | |
962 | uint32_t state, | |
963 | UErrorCode *pErrorCode) | |
964 | { | |
965 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { | |
966 | /* do nothing */ | |
967 | } else if(iter==NULL) { | |
968 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; | |
969 | } else if(state==utf8IteratorGetState(iter)) { | |
970 | /* setting to the current state: no-op */ | |
971 | } else { | |
972 | int32_t index=(int32_t)(state>>1); /* UTF-8 index */ | |
973 | state&=1; /* 1 if in surrogate pair, must be index>=4 */ | |
974 | ||
975 | if((state==0 ? index<0 : index<4) || iter->limit<index) { | |
976 | *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; | |
977 | } else { | |
978 | iter->start=index; /* restore UTF-8 byte index */ | |
979 | if(index<=1) { | |
980 | iter->index=index; | |
981 | } else { | |
982 | iter->index=-1; /* unknown UTF-16 index */ | |
983 | } | |
984 | if(state==0) { | |
985 | iter->reservedField=0; | |
986 | } else { | |
987 | /* verified index>=4 above */ | |
988 | UChar32 c; | |
989 | U8_PREV((const uint8_t *)iter->context, 0, index, c); | |
990 | if(c<=0xffff) { | |
991 | *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; | |
992 | } else { | |
993 | iter->reservedField=c; | |
994 | } | |
995 | } | |
996 | } | |
997 | } | |
998 | } | |
999 | ||
1000 | static const UCharIterator utf8Iterator={ | |
1001 | 0, 0, 0, 0, 0, 0, | |
1002 | utf8IteratorGetIndex, | |
1003 | utf8IteratorMove, | |
1004 | utf8IteratorHasNext, | |
1005 | utf8IteratorHasPrevious, | |
1006 | utf8IteratorCurrent, | |
1007 | utf8IteratorNext, | |
1008 | utf8IteratorPrevious, | |
1009 | NULL, | |
1010 | utf8IteratorGetState, | |
1011 | utf8IteratorSetState | |
1012 | }; | |
1013 | ||
1014 | U_CAPI void U_EXPORT2 | |
1015 | uiter_setUTF8(UCharIterator *iter, const char *s, int32_t length) { | |
1016 | if(iter!=0) { | |
1017 | if(s!=0 && length>=-1) { | |
1018 | *iter=utf8Iterator; | |
1019 | iter->context=s; | |
1020 | if(length>=0) { | |
1021 | iter->limit=length; | |
1022 | } else { | |
374ca955 | 1023 | iter->limit=(int32_t)uprv_strlen(s); |
b75a7d8f A |
1024 | } |
1025 | iter->length= iter->limit<=1 ? iter->limit : -1; | |
1026 | } else { | |
1027 | *iter=noopIterator; | |
1028 | } | |
1029 | } | |
1030 | } | |
1031 | ||
1032 | /* Helper functions --------------------------------------------------------- */ | |
1033 | ||
1034 | U_CAPI UChar32 U_EXPORT2 | |
1035 | uiter_current32(UCharIterator *iter) { | |
1036 | UChar32 c, c2; | |
1037 | ||
1038 | c=iter->current(iter); | |
1039 | if(UTF_IS_SURROGATE(c)) { | |
1040 | if(UTF_IS_SURROGATE_FIRST(c)) { | |
1041 | /* | |
1042 | * go to the next code unit | |
1043 | * we know that we are not at the limit because c!=U_SENTINEL | |
1044 | */ | |
1045 | iter->move(iter, 1, UITER_CURRENT); | |
1046 | if(UTF_IS_SECOND_SURROGATE(c2=iter->current(iter))) { | |
1047 | c=UTF16_GET_PAIR_VALUE(c, c2); | |
1048 | } | |
1049 | ||
1050 | /* undo index movement */ | |
1051 | iter->move(iter, -1, UITER_CURRENT); | |
1052 | } else { | |
1053 | if(UTF_IS_FIRST_SURROGATE(c2=iter->previous(iter))) { | |
1054 | c=UTF16_GET_PAIR_VALUE(c2, c); | |
1055 | } | |
1056 | if(c2>=0) { | |
1057 | /* undo index movement */ | |
1058 | iter->move(iter, 1, UITER_CURRENT); | |
1059 | } | |
1060 | } | |
1061 | } | |
1062 | return c; | |
1063 | } | |
1064 | ||
1065 | U_CAPI UChar32 U_EXPORT2 | |
1066 | uiter_next32(UCharIterator *iter) { | |
1067 | UChar32 c, c2; | |
1068 | ||
1069 | c=iter->next(iter); | |
1070 | if(UTF_IS_FIRST_SURROGATE(c)) { | |
1071 | if(UTF_IS_SECOND_SURROGATE(c2=iter->next(iter))) { | |
1072 | c=UTF16_GET_PAIR_VALUE(c, c2); | |
1073 | } else if(c2>=0) { | |
1074 | /* unmatched first surrogate, undo index movement */ | |
1075 | iter->move(iter, -1, UITER_CURRENT); | |
1076 | } | |
1077 | } | |
1078 | return c; | |
1079 | } | |
1080 | ||
1081 | U_CAPI UChar32 U_EXPORT2 | |
1082 | uiter_previous32(UCharIterator *iter) { | |
1083 | UChar32 c, c2; | |
1084 | ||
1085 | c=iter->previous(iter); | |
1086 | if(UTF_IS_SECOND_SURROGATE(c)) { | |
1087 | if(UTF_IS_FIRST_SURROGATE(c2=iter->previous(iter))) { | |
1088 | c=UTF16_GET_PAIR_VALUE(c2, c); | |
1089 | } else if(c2>=0) { | |
1090 | /* unmatched second surrogate, undo index movement */ | |
1091 | iter->move(iter, 1, UITER_CURRENT); | |
1092 | } | |
1093 | } | |
1094 | return c; | |
1095 | } | |
1096 | ||
1097 | U_CAPI uint32_t U_EXPORT2 | |
1098 | uiter_getState(const UCharIterator *iter) { | |
1099 | if(iter==NULL || iter->getState==NULL) { | |
1100 | return UITER_NO_STATE; | |
1101 | } else { | |
1102 | return iter->getState(iter); | |
1103 | } | |
1104 | } | |
1105 | ||
1106 | U_CAPI void U_EXPORT2 | |
1107 | uiter_setState(UCharIterator *iter, uint32_t state, UErrorCode *pErrorCode) { | |
1108 | if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { | |
1109 | /* do nothing */ | |
1110 | } else if(iter==NULL) { | |
1111 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; | |
1112 | } else if(iter->setState==NULL) { | |
1113 | *pErrorCode=U_UNSUPPORTED_ERROR; | |
1114 | } else { | |
1115 | iter->setState(iter, state, pErrorCode); | |
1116 | } | |
1117 | } | |
1118 | ||
1119 | U_CDECL_END |