示例#1
0
文件: uiter.cpp 项目: Botyto/Core
U_CAPI void U_EXPORT2
uiter_setUTF16BE(UCharIterator * iter, const char * s, int32_t length)
{
	if (iter != NULL)
	{
		/* allow only even-length strings (the input length counts bytes) */
		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;

			if (U_IS_BIG_ENDIAN && IS_POINTER_EVEN(s))
			{
				/* big-endian machine and 2-aligned UTF-16BE string: use normal UChar iterator */
				uiter_setString(iter, (const UChar *)s, length);
				return;
			}

			*iter = utf16BEIterator;
			iter->context = s;
			if (length >= 0)
			{
				iter->length = length;
			}
			else
			{
				iter->length = utf16BE_strlen(s);
			}
			iter->limit = iter->length;
		}
		else
		{
示例#2
0
/*
 * 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) {
    if(IS_POINTER_EVEN(s)) {
        /*
         * 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);
    }
}