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