michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 2002-2012, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: uiter.cpp michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2002jan18 michael@0: * created by: Markus W. Scherer michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/ustring.h" michael@0: #include "unicode/chariter.h" michael@0: #include "unicode/rep.h" michael@0: #include "unicode/uiter.h" michael@0: #include "unicode/utf.h" michael@0: #include "unicode/utf8.h" michael@0: #include "unicode/utf16.h" michael@0: #include "cstring.h" michael@0: michael@0: U_NAMESPACE_USE michael@0: michael@0: #define IS_EVEN(n) (((n)&1)==0) michael@0: #define IS_POINTER_EVEN(p) IS_EVEN((size_t)p) michael@0: michael@0: U_CDECL_BEGIN michael@0: michael@0: /* No-Op UCharIterator implementation for illegal input --------------------- */ michael@0: michael@0: static int32_t U_CALLCONV michael@0: noopGetIndex(UCharIterator * /*iter*/, UCharIteratorOrigin /*origin*/) { michael@0: return 0; michael@0: } michael@0: michael@0: static int32_t U_CALLCONV michael@0: noopMove(UCharIterator * /*iter*/, int32_t /*delta*/, UCharIteratorOrigin /*origin*/) { michael@0: return 0; michael@0: } michael@0: michael@0: static UBool U_CALLCONV michael@0: noopHasNext(UCharIterator * /*iter*/) { michael@0: return FALSE; michael@0: } michael@0: michael@0: static UChar32 U_CALLCONV michael@0: noopCurrent(UCharIterator * /*iter*/) { michael@0: return U_SENTINEL; michael@0: } michael@0: michael@0: static uint32_t U_CALLCONV michael@0: noopGetState(const UCharIterator * /*iter*/) { michael@0: return UITER_NO_STATE; michael@0: } michael@0: michael@0: static void U_CALLCONV michael@0: noopSetState(UCharIterator * /*iter*/, uint32_t /*state*/, UErrorCode *pErrorCode) { michael@0: *pErrorCode=U_UNSUPPORTED_ERROR; michael@0: } michael@0: michael@0: static const UCharIterator noopIterator={ michael@0: 0, 0, 0, 0, 0, 0, michael@0: noopGetIndex, michael@0: noopMove, michael@0: noopHasNext, michael@0: noopHasNext, michael@0: noopCurrent, michael@0: noopCurrent, michael@0: noopCurrent, michael@0: NULL, michael@0: noopGetState, michael@0: noopSetState michael@0: }; michael@0: michael@0: /* UCharIterator implementation for simple strings -------------------------- */ michael@0: michael@0: /* michael@0: * This is an implementation of a code unit (UChar) iterator michael@0: * for UChar * strings. michael@0: * michael@0: * The UCharIterator.context field holds a pointer to the string. michael@0: */ michael@0: michael@0: static int32_t U_CALLCONV michael@0: stringIteratorGetIndex(UCharIterator *iter, UCharIteratorOrigin origin) { michael@0: switch(origin) { michael@0: case UITER_ZERO: michael@0: return 0; michael@0: case UITER_START: michael@0: return iter->start; michael@0: case UITER_CURRENT: michael@0: return iter->index; michael@0: case UITER_LIMIT: michael@0: return iter->limit; michael@0: case UITER_LENGTH: michael@0: return iter->length; michael@0: default: michael@0: /* not a valid origin */ michael@0: /* Should never get here! */ michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: static int32_t U_CALLCONV michael@0: stringIteratorMove(UCharIterator *iter, int32_t delta, UCharIteratorOrigin origin) { michael@0: int32_t pos; michael@0: michael@0: switch(origin) { michael@0: case UITER_ZERO: michael@0: pos=delta; michael@0: break; michael@0: case UITER_START: michael@0: pos=iter->start+delta; michael@0: break; michael@0: case UITER_CURRENT: michael@0: pos=iter->index+delta; michael@0: break; michael@0: case UITER_LIMIT: michael@0: pos=iter->limit+delta; michael@0: break; michael@0: case UITER_LENGTH: michael@0: pos=iter->length+delta; michael@0: break; michael@0: default: michael@0: return -1; /* Error */ michael@0: } michael@0: michael@0: if(posstart) { michael@0: pos=iter->start; michael@0: } else if(pos>iter->limit) { michael@0: pos=iter->limit; michael@0: } michael@0: michael@0: return iter->index=pos; michael@0: } michael@0: michael@0: static UBool U_CALLCONV michael@0: stringIteratorHasNext(UCharIterator *iter) { michael@0: return iter->indexlimit; michael@0: } michael@0: michael@0: static UBool U_CALLCONV michael@0: stringIteratorHasPrevious(UCharIterator *iter) { michael@0: return iter->index>iter->start; michael@0: } michael@0: michael@0: static UChar32 U_CALLCONV michael@0: stringIteratorCurrent(UCharIterator *iter) { michael@0: if(iter->indexlimit) { michael@0: return ((const UChar *)(iter->context))[iter->index]; michael@0: } else { michael@0: return U_SENTINEL; michael@0: } michael@0: } michael@0: michael@0: static UChar32 U_CALLCONV michael@0: stringIteratorNext(UCharIterator *iter) { michael@0: if(iter->indexlimit) { michael@0: return ((const UChar *)(iter->context))[iter->index++]; michael@0: } else { michael@0: return U_SENTINEL; michael@0: } michael@0: } michael@0: michael@0: static UChar32 U_CALLCONV michael@0: stringIteratorPrevious(UCharIterator *iter) { michael@0: if(iter->index>iter->start) { michael@0: return ((const UChar *)(iter->context))[--iter->index]; michael@0: } else { michael@0: return U_SENTINEL; michael@0: } michael@0: } michael@0: michael@0: static uint32_t U_CALLCONV michael@0: stringIteratorGetState(const UCharIterator *iter) { michael@0: return (uint32_t)iter->index; michael@0: } michael@0: michael@0: static void U_CALLCONV michael@0: stringIteratorSetState(UCharIterator *iter, uint32_t state, UErrorCode *pErrorCode) { michael@0: if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { michael@0: /* do nothing */ michael@0: } else if(iter==NULL) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: } else if((int32_t)statestart || iter->limit<(int32_t)state) { michael@0: *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; michael@0: } else { michael@0: iter->index=(int32_t)state; michael@0: } michael@0: } michael@0: michael@0: static const UCharIterator stringIterator={ michael@0: 0, 0, 0, 0, 0, 0, michael@0: stringIteratorGetIndex, michael@0: stringIteratorMove, michael@0: stringIteratorHasNext, michael@0: stringIteratorHasPrevious, michael@0: stringIteratorCurrent, michael@0: stringIteratorNext, michael@0: stringIteratorPrevious, michael@0: NULL, michael@0: stringIteratorGetState, michael@0: stringIteratorSetState michael@0: }; michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uiter_setString(UCharIterator *iter, const UChar *s, int32_t length) { michael@0: if(iter!=0) { michael@0: if(s!=0 && length>=-1) { michael@0: *iter=stringIterator; michael@0: iter->context=s; michael@0: if(length>=0) { michael@0: iter->length=length; michael@0: } else { michael@0: iter->length=u_strlen(s); michael@0: } michael@0: iter->limit=iter->length; michael@0: } else { michael@0: *iter=noopIterator; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* UCharIterator implementation for UTF-16BE strings ------------------------ */ michael@0: michael@0: /* michael@0: * This is an implementation of a code unit (UChar) iterator michael@0: * for UTF-16BE strings, i.e., strings in byte-vectors where michael@0: * each UChar is stored as a big-endian pair of bytes. michael@0: * michael@0: * The UCharIterator.context field holds a pointer to the string. michael@0: * Everything works just like with a normal UChar iterator (uiter_setString), michael@0: * except that UChars are assembled from byte pairs. michael@0: */ michael@0: michael@0: /* internal helper function */ michael@0: static inline UChar32 michael@0: utf16BEIteratorGet(UCharIterator *iter, int32_t index) { michael@0: const uint8_t *p=(const uint8_t *)iter->context; michael@0: return ((UChar)p[2*index]<<8)|(UChar)p[2*index+1]; michael@0: } michael@0: michael@0: static UChar32 U_CALLCONV michael@0: utf16BEIteratorCurrent(UCharIterator *iter) { michael@0: int32_t index; michael@0: michael@0: if((index=iter->index)limit) { michael@0: return utf16BEIteratorGet(iter, index); michael@0: } else { michael@0: return U_SENTINEL; michael@0: } michael@0: } michael@0: michael@0: static UChar32 U_CALLCONV michael@0: utf16BEIteratorNext(UCharIterator *iter) { michael@0: int32_t index; michael@0: michael@0: if((index=iter->index)limit) { michael@0: iter->index=index+1; michael@0: return utf16BEIteratorGet(iter, index); michael@0: } else { michael@0: return U_SENTINEL; michael@0: } michael@0: } michael@0: michael@0: static UChar32 U_CALLCONV michael@0: utf16BEIteratorPrevious(UCharIterator *iter) { michael@0: int32_t index; michael@0: michael@0: if((index=iter->index)>iter->start) { michael@0: iter->index=--index; michael@0: return utf16BEIteratorGet(iter, index); michael@0: } else { michael@0: return U_SENTINEL; michael@0: } michael@0: } michael@0: michael@0: static const UCharIterator utf16BEIterator={ michael@0: 0, 0, 0, 0, 0, 0, michael@0: stringIteratorGetIndex, michael@0: stringIteratorMove, michael@0: stringIteratorHasNext, michael@0: stringIteratorHasPrevious, michael@0: utf16BEIteratorCurrent, michael@0: utf16BEIteratorNext, michael@0: utf16BEIteratorPrevious, michael@0: NULL, michael@0: stringIteratorGetState, michael@0: stringIteratorSetState michael@0: }; michael@0: michael@0: /* michael@0: * Count the number of UChars in a UTF-16BE string before a terminating UChar NUL, michael@0: * i.e., before a pair of 0 bytes where the first 0 byte is at an even michael@0: * offset from s. michael@0: */ michael@0: static int32_t michael@0: utf16BE_strlen(const char *s) { michael@0: if(IS_POINTER_EVEN(s)) { michael@0: /* michael@0: * even-aligned, call u_strlen(s) michael@0: * we are probably on a little-endian machine, but searching for UChar NUL michael@0: * does not care about endianness michael@0: */ michael@0: return u_strlen((const UChar *)s); michael@0: } else { michael@0: /* odd-aligned, search for pair of 0 bytes */ michael@0: const char *p=s; michael@0: michael@0: while(!(*p==0 && p[1]==0)) { michael@0: p+=2; michael@0: } michael@0: return (int32_t)((p-s)/2); michael@0: } michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uiter_setUTF16BE(UCharIterator *iter, const char *s, int32_t length) { michael@0: if(iter!=NULL) { michael@0: /* allow only even-length strings (the input length counts bytes) */ michael@0: if(s!=NULL && (length==-1 || (length>=0 && IS_EVEN(length)))) { michael@0: /* length/=2, except that >>=1 also works for -1 (-1/2==0, -1>>1==-1) */ michael@0: length>>=1; michael@0: michael@0: if(U_IS_BIG_ENDIAN && IS_POINTER_EVEN(s)) { michael@0: /* big-endian machine and 2-aligned UTF-16BE string: use normal UChar iterator */ michael@0: uiter_setString(iter, (const UChar *)s, length); michael@0: return; michael@0: } michael@0: michael@0: *iter=utf16BEIterator; michael@0: iter->context=s; michael@0: if(length>=0) { michael@0: iter->length=length; michael@0: } else { michael@0: iter->length=utf16BE_strlen(s); michael@0: } michael@0: iter->limit=iter->length; michael@0: } else { michael@0: *iter=noopIterator; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* UCharIterator wrapper around CharacterIterator --------------------------- */ michael@0: michael@0: /* michael@0: * This is wrapper code around a C++ CharacterIterator to michael@0: * look like a C UCharIterator. michael@0: * michael@0: * The UCharIterator.context field holds a pointer to the CharacterIterator. michael@0: */ michael@0: michael@0: static int32_t U_CALLCONV michael@0: characterIteratorGetIndex(UCharIterator *iter, UCharIteratorOrigin origin) { michael@0: switch(origin) { michael@0: case UITER_ZERO: michael@0: return 0; michael@0: case UITER_START: michael@0: return ((CharacterIterator *)(iter->context))->startIndex(); michael@0: case UITER_CURRENT: michael@0: return ((CharacterIterator *)(iter->context))->getIndex(); michael@0: case UITER_LIMIT: michael@0: return ((CharacterIterator *)(iter->context))->endIndex(); michael@0: case UITER_LENGTH: michael@0: return ((CharacterIterator *)(iter->context))->getLength(); michael@0: default: michael@0: /* not a valid origin */ michael@0: /* Should never get here! */ michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: static int32_t U_CALLCONV michael@0: characterIteratorMove(UCharIterator *iter, int32_t delta, UCharIteratorOrigin origin) { michael@0: switch(origin) { michael@0: case UITER_ZERO: michael@0: ((CharacterIterator *)(iter->context))->setIndex(delta); michael@0: return ((CharacterIterator *)(iter->context))->getIndex(); michael@0: case UITER_START: michael@0: case UITER_CURRENT: michael@0: case UITER_LIMIT: michael@0: return ((CharacterIterator *)(iter->context))->move(delta, (CharacterIterator::EOrigin)origin); michael@0: case UITER_LENGTH: michael@0: ((CharacterIterator *)(iter->context))->setIndex(((CharacterIterator *)(iter->context))->getLength()+delta); michael@0: return ((CharacterIterator *)(iter->context))->getIndex(); michael@0: default: michael@0: /* not a valid origin */ michael@0: /* Should never get here! */ michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: static UBool U_CALLCONV michael@0: characterIteratorHasNext(UCharIterator *iter) { michael@0: return ((CharacterIterator *)(iter->context))->hasNext(); michael@0: } michael@0: michael@0: static UBool U_CALLCONV michael@0: characterIteratorHasPrevious(UCharIterator *iter) { michael@0: return ((CharacterIterator *)(iter->context))->hasPrevious(); michael@0: } michael@0: michael@0: static UChar32 U_CALLCONV michael@0: characterIteratorCurrent(UCharIterator *iter) { michael@0: UChar32 c; michael@0: michael@0: c=((CharacterIterator *)(iter->context))->current(); michael@0: if(c!=0xffff || ((CharacterIterator *)(iter->context))->hasNext()) { michael@0: return c; michael@0: } else { michael@0: return U_SENTINEL; michael@0: } michael@0: } michael@0: michael@0: static UChar32 U_CALLCONV michael@0: characterIteratorNext(UCharIterator *iter) { michael@0: if(((CharacterIterator *)(iter->context))->hasNext()) { michael@0: return ((CharacterIterator *)(iter->context))->nextPostInc(); michael@0: } else { michael@0: return U_SENTINEL; michael@0: } michael@0: } michael@0: michael@0: static UChar32 U_CALLCONV michael@0: characterIteratorPrevious(UCharIterator *iter) { michael@0: if(((CharacterIterator *)(iter->context))->hasPrevious()) { michael@0: return ((CharacterIterator *)(iter->context))->previous(); michael@0: } else { michael@0: return U_SENTINEL; michael@0: } michael@0: } michael@0: michael@0: static uint32_t U_CALLCONV michael@0: characterIteratorGetState(const UCharIterator *iter) { michael@0: return ((CharacterIterator *)(iter->context))->getIndex(); michael@0: } michael@0: michael@0: static void U_CALLCONV michael@0: characterIteratorSetState(UCharIterator *iter, uint32_t state, UErrorCode *pErrorCode) { michael@0: if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { michael@0: /* do nothing */ michael@0: } else if(iter==NULL || iter->context==NULL) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: } else if((int32_t)state<((CharacterIterator *)(iter->context))->startIndex() || ((CharacterIterator *)(iter->context))->endIndex()<(int32_t)state) { michael@0: *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; michael@0: } else { michael@0: ((CharacterIterator *)(iter->context))->setIndex((int32_t)state); michael@0: } michael@0: } michael@0: michael@0: static const UCharIterator characterIteratorWrapper={ michael@0: 0, 0, 0, 0, 0, 0, michael@0: characterIteratorGetIndex, michael@0: characterIteratorMove, michael@0: characterIteratorHasNext, michael@0: characterIteratorHasPrevious, michael@0: characterIteratorCurrent, michael@0: characterIteratorNext, michael@0: characterIteratorPrevious, michael@0: NULL, michael@0: characterIteratorGetState, michael@0: characterIteratorSetState michael@0: }; michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uiter_setCharacterIterator(UCharIterator *iter, CharacterIterator *charIter) { michael@0: if(iter!=0) { michael@0: if(charIter!=0) { michael@0: *iter=characterIteratorWrapper; michael@0: iter->context=charIter; michael@0: } else { michael@0: *iter=noopIterator; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* UCharIterator wrapper around Replaceable --------------------------------- */ michael@0: michael@0: /* michael@0: * This is an implementation of a code unit (UChar) iterator michael@0: * based on a Replaceable object. michael@0: * michael@0: * The UCharIterator.context field holds a pointer to the Replaceable. michael@0: * UCharIterator.length and UCharIterator.index hold Replaceable.length() michael@0: * and the iteration index. michael@0: */ michael@0: michael@0: static UChar32 U_CALLCONV michael@0: replaceableIteratorCurrent(UCharIterator *iter) { michael@0: if(iter->indexlimit) { michael@0: return ((Replaceable *)(iter->context))->charAt(iter->index); michael@0: } else { michael@0: return U_SENTINEL; michael@0: } michael@0: } michael@0: michael@0: static UChar32 U_CALLCONV michael@0: replaceableIteratorNext(UCharIterator *iter) { michael@0: if(iter->indexlimit) { michael@0: return ((Replaceable *)(iter->context))->charAt(iter->index++); michael@0: } else { michael@0: return U_SENTINEL; michael@0: } michael@0: } michael@0: michael@0: static UChar32 U_CALLCONV michael@0: replaceableIteratorPrevious(UCharIterator *iter) { michael@0: if(iter->index>iter->start) { michael@0: return ((Replaceable *)(iter->context))->charAt(--iter->index); michael@0: } else { michael@0: return U_SENTINEL; michael@0: } michael@0: } michael@0: michael@0: static const UCharIterator replaceableIterator={ michael@0: 0, 0, 0, 0, 0, 0, michael@0: stringIteratorGetIndex, michael@0: stringIteratorMove, michael@0: stringIteratorHasNext, michael@0: stringIteratorHasPrevious, michael@0: replaceableIteratorCurrent, michael@0: replaceableIteratorNext, michael@0: replaceableIteratorPrevious, michael@0: NULL, michael@0: stringIteratorGetState, michael@0: stringIteratorSetState michael@0: }; michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uiter_setReplaceable(UCharIterator *iter, const Replaceable *rep) { michael@0: if(iter!=0) { michael@0: if(rep!=0) { michael@0: *iter=replaceableIterator; michael@0: iter->context=rep; michael@0: iter->limit=iter->length=rep->length(); michael@0: } else { michael@0: *iter=noopIterator; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* UCharIterator implementation for UTF-8 strings --------------------------- */ michael@0: michael@0: /* michael@0: * Possible, probably necessary only for an implementation for arbitrary michael@0: * converters: michael@0: * Maintain a buffer (ring buffer?) for a piece of converted 16-bit text. michael@0: * This would require to turn reservedFn into a close function and michael@0: * to introduce a uiter_close(iter). michael@0: */ michael@0: michael@0: #define UITER_CNV_CAPACITY 16 michael@0: michael@0: /* michael@0: * Minimal implementation: michael@0: * Maintain a single-UChar buffer for an additional surrogate. michael@0: * The caller must not modify start and limit because they are used internally. michael@0: * michael@0: * Use UCharIterator fields as follows: michael@0: * context pointer to UTF-8 string michael@0: * length UTF-16 length of the string; -1 until lazy evaluation michael@0: * start current UTF-8 index michael@0: * index current UTF-16 index; may be -1="unknown" after setState() michael@0: * limit UTF-8 length of the string michael@0: * reservedField supplementary code point michael@0: * michael@0: * Since UCharIterator delivers 16-bit code units, the iteration can be michael@0: * currently in the middle of the byte sequence for a supplementary code point. michael@0: * In this case, reservedField will contain that code point and start will michael@0: * point to after the corresponding byte sequence. The UTF-16 index will be michael@0: * one less than what it would otherwise be corresponding to the UTF-8 index. michael@0: * Otherwise, reservedField will be 0. michael@0: */ michael@0: michael@0: /* michael@0: * Possible optimization for NUL-terminated UTF-8 and UTF-16 strings: michael@0: * Add implementations that do not call strlen() for iteration but check for NUL. michael@0: */ michael@0: michael@0: static int32_t U_CALLCONV michael@0: utf8IteratorGetIndex(UCharIterator *iter, UCharIteratorOrigin origin) { michael@0: switch(origin) { michael@0: case UITER_ZERO: michael@0: case UITER_START: michael@0: return 0; michael@0: case UITER_CURRENT: michael@0: if(iter->index<0) { michael@0: /* the current UTF-16 index is unknown after setState(), count from the beginning */ michael@0: const uint8_t *s; michael@0: UChar32 c; michael@0: int32_t i, limit, index; michael@0: michael@0: s=(const uint8_t *)iter->context; michael@0: i=index=0; michael@0: limit=iter->start; /* count up to the UTF-8 index */ michael@0: while(istart=i; /* just in case setState() did not get us to a code point boundary */ michael@0: if(i==iter->limit) { michael@0: iter->length=index; /* in case it was <0 or wrong */ michael@0: } michael@0: if(iter->reservedField!=0) { michael@0: --index; /* we are in the middle of a supplementary code point */ michael@0: } michael@0: iter->index=index; michael@0: } michael@0: return iter->index; michael@0: case UITER_LIMIT: michael@0: case UITER_LENGTH: michael@0: if(iter->length<0) { michael@0: const uint8_t *s; michael@0: UChar32 c; michael@0: int32_t i, limit, length; michael@0: michael@0: s=(const uint8_t *)iter->context; michael@0: if(iter->index<0) { michael@0: /* michael@0: * the current UTF-16 index is unknown after setState(), michael@0: * we must first count from the beginning to here michael@0: */ michael@0: i=length=0; michael@0: limit=iter->start; michael@0: michael@0: /* count from the beginning to the current index */ michael@0: while(istart, set the UTF-16 index */ michael@0: iter->start=i; /* just in case setState() did not get us to a code point boundary */ michael@0: iter->index= iter->reservedField!=0 ? length-1 : length; michael@0: } else { michael@0: i=iter->start; michael@0: length=iter->index; michael@0: if(iter->reservedField!=0) { michael@0: ++length; michael@0: } michael@0: } michael@0: michael@0: /* count from the current index to the end */ michael@0: limit=iter->limit; michael@0: while(ilength=length; michael@0: } michael@0: return iter->length; michael@0: default: michael@0: /* not a valid origin */ michael@0: /* Should never get here! */ michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: static int32_t U_CALLCONV michael@0: utf8IteratorMove(UCharIterator *iter, int32_t delta, UCharIteratorOrigin origin) { michael@0: const uint8_t *s; michael@0: UChar32 c; michael@0: int32_t pos; /* requested UTF-16 index */ michael@0: int32_t i; /* UTF-8 index */ michael@0: UBool havePos; michael@0: michael@0: /* calculate the requested UTF-16 index */ michael@0: switch(origin) { michael@0: case UITER_ZERO: michael@0: case UITER_START: michael@0: pos=delta; michael@0: havePos=TRUE; michael@0: /* iter->index<0 (unknown) is possible */ michael@0: break; michael@0: case UITER_CURRENT: michael@0: if(iter->index>=0) { michael@0: pos=iter->index+delta; michael@0: havePos=TRUE; michael@0: } else { michael@0: /* the current UTF-16 index is unknown after setState(), use only delta */ michael@0: pos=0; michael@0: havePos=FALSE; michael@0: } michael@0: break; michael@0: case UITER_LIMIT: michael@0: case UITER_LENGTH: michael@0: if(iter->length>=0) { michael@0: pos=iter->length+delta; michael@0: havePos=TRUE; michael@0: } else { michael@0: /* pin to the end, avoid counting the length */ michael@0: iter->index=-1; michael@0: iter->start=iter->limit; michael@0: iter->reservedField=0; michael@0: if(delta>=0) { michael@0: return UITER_UNKNOWN_INDEX; michael@0: } else { michael@0: /* the current UTF-16 index is unknown, use only delta */ michael@0: pos=0; michael@0: havePos=FALSE; michael@0: } michael@0: } michael@0: break; michael@0: default: michael@0: return -1; /* Error */ michael@0: } michael@0: michael@0: if(havePos) { michael@0: /* shortcuts: pinning to the edges of the string */ michael@0: if(pos<=0) { michael@0: iter->index=iter->start=iter->reservedField=0; michael@0: return 0; michael@0: } else if(iter->length>=0 && pos>=iter->length) { michael@0: iter->index=iter->length; michael@0: iter->start=iter->limit; michael@0: iter->reservedField=0; michael@0: return iter->index; michael@0: } michael@0: michael@0: /* minimize the number of U8_NEXT/PREV operations */ michael@0: if(iter->index<0 || posindex/2) { michael@0: /* go forward from the start instead of backward from the current index */ michael@0: iter->index=iter->start=iter->reservedField=0; michael@0: } else if(iter->length>=0 && (iter->length-pos)<(pos-iter->index)) { michael@0: /* michael@0: * if we have the UTF-16 index and length and the new position is michael@0: * closer to the end than the current index, michael@0: * then go backward from the end instead of forward from the current index michael@0: */ michael@0: iter->index=iter->length; michael@0: iter->start=iter->limit; michael@0: iter->reservedField=0; michael@0: } michael@0: michael@0: delta=pos-iter->index; michael@0: if(delta==0) { michael@0: return iter->index; /* nothing to do */ michael@0: } michael@0: } else { michael@0: /* move relative to unknown UTF-16 index */ michael@0: if(delta==0) { michael@0: return UITER_UNKNOWN_INDEX; /* nothing to do */ michael@0: } else if(-delta>=iter->start) { michael@0: /* moving backwards by more UChars than there are UTF-8 bytes, pin to 0 */ michael@0: iter->index=iter->start=iter->reservedField=0; michael@0: return 0; michael@0: } else if(delta>=(iter->limit-iter->start)) { michael@0: /* moving forward by more UChars than the remaining UTF-8 bytes, pin to the end */ michael@0: iter->index=iter->length; /* may or may not be <0 (unknown) */ michael@0: iter->start=iter->limit; michael@0: iter->reservedField=0; michael@0: return iter->index>=0 ? iter->index : (int32_t)UITER_UNKNOWN_INDEX; michael@0: } michael@0: } michael@0: michael@0: /* delta!=0 */ michael@0: michael@0: /* move towards the requested position, pin to the edges of the string */ michael@0: s=(const uint8_t *)iter->context; michael@0: pos=iter->index; /* could be <0 (unknown) */ michael@0: i=iter->start; michael@0: if(delta>0) { michael@0: /* go forward */ michael@0: int32_t limit=iter->limit; michael@0: if(iter->reservedField!=0) { michael@0: iter->reservedField=0; michael@0: ++pos; michael@0: --delta; michael@0: } michael@0: while(delta>0 && i=2) { michael@0: pos+=2; michael@0: delta-=2; michael@0: } else /* delta==1 */ { michael@0: /* stop in the middle of a supplementary code point */ michael@0: iter->reservedField=c; michael@0: ++pos; michael@0: break; /* delta=0; */ michael@0: } michael@0: } michael@0: if(i==limit) { michael@0: if(iter->length<0 && iter->index>=0) { michael@0: iter->length= iter->reservedField==0 ? pos : pos+1; michael@0: } else if(iter->index<0 && iter->length>=0) { michael@0: iter->index= iter->reservedField==0 ? iter->length : iter->length-1; michael@0: } michael@0: } michael@0: } else /* delta<0 */ { michael@0: /* go backward */ michael@0: if(iter->reservedField!=0) { michael@0: iter->reservedField=0; michael@0: i-=4; /* we stayed behind the supplementary code point; go before it now */ michael@0: --pos; michael@0: ++delta; michael@0: } michael@0: while(delta<0 && i>0) { michael@0: U8_PREV_OR_FFFD(s, 0, i, c); michael@0: if(c<=0xffff) { michael@0: --pos; michael@0: ++delta; michael@0: } else if(delta<=-2) { michael@0: pos-=2; michael@0: delta+=2; michael@0: } else /* delta==-1 */ { michael@0: /* stop in the middle of a supplementary code point */ michael@0: i+=4; /* back to behind this supplementary code point for consistent state */ michael@0: iter->reservedField=c; michael@0: --pos; michael@0: break; /* delta=0; */ michael@0: } michael@0: } michael@0: } michael@0: michael@0: iter->start=i; michael@0: if(iter->index>=0) { michael@0: return iter->index=pos; michael@0: } else { michael@0: /* we started with index<0 (unknown) so pos is bogus */ michael@0: if(i<=1) { michael@0: return iter->index=i; /* reached the beginning */ michael@0: } else { michael@0: /* we still don't know the UTF-16 index */ michael@0: return UITER_UNKNOWN_INDEX; michael@0: } michael@0: } michael@0: } michael@0: michael@0: static UBool U_CALLCONV michael@0: utf8IteratorHasNext(UCharIterator *iter) { michael@0: return iter->startlimit || iter->reservedField!=0; michael@0: } michael@0: michael@0: static UBool U_CALLCONV michael@0: utf8IteratorHasPrevious(UCharIterator *iter) { michael@0: return iter->start>0; michael@0: } michael@0: michael@0: static UChar32 U_CALLCONV michael@0: utf8IteratorCurrent(UCharIterator *iter) { michael@0: if(iter->reservedField!=0) { michael@0: return U16_TRAIL(iter->reservedField); michael@0: } else if(iter->startlimit) { michael@0: const uint8_t *s=(const uint8_t *)iter->context; michael@0: UChar32 c; michael@0: int32_t i=iter->start; michael@0: michael@0: U8_NEXT_OR_FFFD(s, i, iter->limit, c); michael@0: if(c<=0xffff) { michael@0: return c; michael@0: } else { michael@0: return U16_LEAD(c); michael@0: } michael@0: } else { michael@0: return U_SENTINEL; michael@0: } michael@0: } michael@0: michael@0: static UChar32 U_CALLCONV michael@0: utf8IteratorNext(UCharIterator *iter) { michael@0: int32_t index; michael@0: michael@0: if(iter->reservedField!=0) { michael@0: UChar trail=U16_TRAIL(iter->reservedField); michael@0: iter->reservedField=0; michael@0: if((index=iter->index)>=0) { michael@0: iter->index=index+1; michael@0: } michael@0: return trail; michael@0: } else if(iter->startlimit) { michael@0: const uint8_t *s=(const uint8_t *)iter->context; michael@0: UChar32 c; michael@0: michael@0: U8_NEXT_OR_FFFD(s, iter->start, iter->limit, c); michael@0: if((index=iter->index)>=0) { michael@0: iter->index=++index; michael@0: if(iter->length<0 && iter->start==iter->limit) { michael@0: iter->length= c<=0xffff ? index : index+1; michael@0: } michael@0: } else if(iter->start==iter->limit && iter->length>=0) { michael@0: iter->index= c<=0xffff ? iter->length : iter->length-1; michael@0: } michael@0: if(c<=0xffff) { michael@0: return c; michael@0: } else { michael@0: iter->reservedField=c; michael@0: return U16_LEAD(c); michael@0: } michael@0: } else { michael@0: return U_SENTINEL; michael@0: } michael@0: } michael@0: michael@0: static UChar32 U_CALLCONV michael@0: utf8IteratorPrevious(UCharIterator *iter) { michael@0: int32_t index; michael@0: michael@0: if(iter->reservedField!=0) { michael@0: UChar lead=U16_LEAD(iter->reservedField); michael@0: iter->reservedField=0; michael@0: iter->start-=4; /* we stayed behind the supplementary code point; go before it now */ michael@0: if((index=iter->index)>0) { michael@0: iter->index=index-1; michael@0: } michael@0: return lead; michael@0: } else if(iter->start>0) { michael@0: const uint8_t *s=(const uint8_t *)iter->context; michael@0: UChar32 c; michael@0: michael@0: U8_PREV_OR_FFFD(s, 0, iter->start, c); michael@0: if((index=iter->index)>0) { michael@0: iter->index=index-1; michael@0: } else if(iter->start<=1) { michael@0: iter->index= c<=0xffff ? iter->start : iter->start+1; michael@0: } michael@0: if(c<=0xffff) { michael@0: return c; michael@0: } else { michael@0: iter->start+=4; /* back to behind this supplementary code point for consistent state */ michael@0: iter->reservedField=c; michael@0: return U16_TRAIL(c); michael@0: } michael@0: } else { michael@0: return U_SENTINEL; michael@0: } michael@0: } michael@0: michael@0: static uint32_t U_CALLCONV michael@0: utf8IteratorGetState(const UCharIterator *iter) { michael@0: uint32_t state=(uint32_t)(iter->start<<1); michael@0: if(iter->reservedField!=0) { michael@0: state|=1; michael@0: } michael@0: return state; michael@0: } michael@0: michael@0: static void U_CALLCONV michael@0: utf8IteratorSetState(UCharIterator *iter, michael@0: uint32_t state, michael@0: UErrorCode *pErrorCode) michael@0: { michael@0: if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { michael@0: /* do nothing */ michael@0: } else if(iter==NULL) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: } else if(state==utf8IteratorGetState(iter)) { michael@0: /* setting to the current state: no-op */ michael@0: } else { michael@0: int32_t index=(int32_t)(state>>1); /* UTF-8 index */ michael@0: state&=1; /* 1 if in surrogate pair, must be index>=4 */ michael@0: michael@0: if((state==0 ? index<0 : index<4) || iter->limitstart=index; /* restore UTF-8 byte index */ michael@0: if(index<=1) { michael@0: iter->index=index; michael@0: } else { michael@0: iter->index=-1; /* unknown UTF-16 index */ michael@0: } michael@0: if(state==0) { michael@0: iter->reservedField=0; michael@0: } else { michael@0: /* verified index>=4 above */ michael@0: UChar32 c; michael@0: U8_PREV_OR_FFFD((const uint8_t *)iter->context, 0, index, c); michael@0: if(c<=0xffff) { michael@0: *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; michael@0: } else { michael@0: iter->reservedField=c; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: static const UCharIterator utf8Iterator={ michael@0: 0, 0, 0, 0, 0, 0, michael@0: utf8IteratorGetIndex, michael@0: utf8IteratorMove, michael@0: utf8IteratorHasNext, michael@0: utf8IteratorHasPrevious, michael@0: utf8IteratorCurrent, michael@0: utf8IteratorNext, michael@0: utf8IteratorPrevious, michael@0: NULL, michael@0: utf8IteratorGetState, michael@0: utf8IteratorSetState michael@0: }; michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uiter_setUTF8(UCharIterator *iter, const char *s, int32_t length) { michael@0: if(iter!=0) { michael@0: if(s!=0 && length>=-1) { michael@0: *iter=utf8Iterator; michael@0: iter->context=s; michael@0: if(length>=0) { michael@0: iter->limit=length; michael@0: } else { michael@0: iter->limit=(int32_t)uprv_strlen(s); michael@0: } michael@0: iter->length= iter->limit<=1 ? iter->limit : -1; michael@0: } else { michael@0: *iter=noopIterator; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* Helper functions --------------------------------------------------------- */ michael@0: michael@0: U_CAPI UChar32 U_EXPORT2 michael@0: uiter_current32(UCharIterator *iter) { michael@0: UChar32 c, c2; michael@0: michael@0: c=iter->current(iter); michael@0: if(U16_IS_SURROGATE(c)) { michael@0: if(U16_IS_SURROGATE_LEAD(c)) { michael@0: /* michael@0: * go to the next code unit michael@0: * we know that we are not at the limit because c!=U_SENTINEL michael@0: */ michael@0: iter->move(iter, 1, UITER_CURRENT); michael@0: if(U16_IS_TRAIL(c2=iter->current(iter))) { michael@0: c=U16_GET_SUPPLEMENTARY(c, c2); michael@0: } michael@0: michael@0: /* undo index movement */ michael@0: iter->move(iter, -1, UITER_CURRENT); michael@0: } else { michael@0: if(U16_IS_LEAD(c2=iter->previous(iter))) { michael@0: c=U16_GET_SUPPLEMENTARY(c2, c); michael@0: } michael@0: if(c2>=0) { michael@0: /* undo index movement */ michael@0: iter->move(iter, 1, UITER_CURRENT); michael@0: } michael@0: } michael@0: } michael@0: return c; michael@0: } michael@0: michael@0: U_CAPI UChar32 U_EXPORT2 michael@0: uiter_next32(UCharIterator *iter) { michael@0: UChar32 c, c2; michael@0: michael@0: c=iter->next(iter); michael@0: if(U16_IS_LEAD(c)) { michael@0: if(U16_IS_TRAIL(c2=iter->next(iter))) { michael@0: c=U16_GET_SUPPLEMENTARY(c, c2); michael@0: } else if(c2>=0) { michael@0: /* unmatched first surrogate, undo index movement */ michael@0: iter->move(iter, -1, UITER_CURRENT); michael@0: } michael@0: } michael@0: return c; michael@0: } michael@0: michael@0: U_CAPI UChar32 U_EXPORT2 michael@0: uiter_previous32(UCharIterator *iter) { michael@0: UChar32 c, c2; michael@0: michael@0: c=iter->previous(iter); michael@0: if(U16_IS_TRAIL(c)) { michael@0: if(U16_IS_LEAD(c2=iter->previous(iter))) { michael@0: c=U16_GET_SUPPLEMENTARY(c2, c); michael@0: } else if(c2>=0) { michael@0: /* unmatched second surrogate, undo index movement */ michael@0: iter->move(iter, 1, UITER_CURRENT); michael@0: } michael@0: } michael@0: return c; michael@0: } michael@0: michael@0: U_CAPI uint32_t U_EXPORT2 michael@0: uiter_getState(const UCharIterator *iter) { michael@0: if(iter==NULL || iter->getState==NULL) { michael@0: return UITER_NO_STATE; michael@0: } else { michael@0: return iter->getState(iter); michael@0: } michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: uiter_setState(UCharIterator *iter, uint32_t state, UErrorCode *pErrorCode) { michael@0: if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) { michael@0: /* do nothing */ michael@0: } else if(iter==NULL) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: } else if(iter->setState==NULL) { michael@0: *pErrorCode=U_UNSUPPORTED_ERROR; michael@0: } else { michael@0: iter->setState(iter, state, pErrorCode); michael@0: } michael@0: } michael@0: michael@0: U_CDECL_END