2000-02-03 19:54:49 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999-2000, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
* file name: utf8.h
|
|
|
|
* encoding: US-ASCII
|
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* created on: 1999sep13
|
|
|
|
* created by: Markus W. Scherer
|
2000-12-08 18:43:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
2000-12-12 20:31:47 +00:00
|
|
|
* \brief C API: UTF-8 macros
|
2000-12-08 18:43:57 +00:00
|
|
|
*
|
2000-02-03 19:54:49 +00:00
|
|
|
* This file defines macros to deal with UTF-8 code units and code points.
|
|
|
|
* Signatures and semantics are the same as for the similarly named macros
|
|
|
|
* in utf16.h.
|
|
|
|
* utf8.h is included by utf.h after unicode/umachine.h
|
|
|
|
* and some common definitions.
|
|
|
|
*/
|
|
|
|
|
2000-06-03 04:18:06 +00:00
|
|
|
|
|
|
|
/* utf.h must be included first. */
|
|
|
|
#ifndef __UTF_H__
|
|
|
|
# include "unicode/utf.h"
|
|
|
|
#endif
|
|
|
|
|
2000-02-03 19:54:49 +00:00
|
|
|
#ifndef __UTF8_H__
|
|
|
|
#define __UTF8_H__
|
|
|
|
|
|
|
|
/* internal definitions ----------------------------------------------------- */
|
|
|
|
|
2000-07-26 20:37:53 +00:00
|
|
|
#ifdef U_UTF8_IMPL
|
2000-07-26 02:00:22 +00:00
|
|
|
U_CAPI uint8_t U_EXPORT2
|
2000-02-03 19:54:49 +00:00
|
|
|
utf8_countTrailBytes[256];
|
2000-07-26 02:00:22 +00:00
|
|
|
#else
|
2000-07-26 20:37:53 +00:00
|
|
|
U_CFUNC uint8_t /* U_IMPORT2? */ U_IMPORT
|
2000-07-26 02:00:22 +00:00
|
|
|
utf8_countTrailBytes[256];
|
|
|
|
#endif
|
2000-02-03 19:54:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Count the trail bytes for a lead byte -
|
|
|
|
* this macro should be used so that the assembler code
|
|
|
|
* that is mentioned in utf_impl.c could be used here.
|
|
|
|
*/
|
|
|
|
#define UTF8_COUNT_TRAIL_BYTES(leadByte) (utf8_countTrailBytes[(uint8_t)leadByte])
|
|
|
|
|
|
|
|
/* use a macro here, too - there may be a simpler way with some machines */
|
|
|
|
#define UTF8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
|
|
|
|
|
|
|
|
U_CAPI UChar32 U_EXPORT2
|
2000-05-18 22:08:39 +00:00
|
|
|
utf8_nextCharSafeBody(const uint8_t *s, UTextOffset *pi, UTextOffset length, UChar32 c, UBool strict);
|
2000-02-03 19:54:49 +00:00
|
|
|
|
|
|
|
U_CAPI UTextOffset U_EXPORT2
|
|
|
|
utf8_appendCharSafeBody(uint8_t *s, UTextOffset i, UTextOffset length, UChar32 c);
|
|
|
|
|
|
|
|
U_CAPI UChar32 U_EXPORT2
|
2000-05-18 22:08:39 +00:00
|
|
|
utf8_prevCharSafeBody(const uint8_t *s, UTextOffset start, UTextOffset *pi, UChar32 c, UBool strict);
|
2000-02-03 19:54:49 +00:00
|
|
|
|
|
|
|
U_CAPI UTextOffset U_EXPORT2
|
2000-04-12 19:36:07 +00:00
|
|
|
utf8_back1SafeBody(const uint8_t *s, UTextOffset start, UTextOffset i);
|
2000-02-03 19:54:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For the semantics of all of these macros, see utf16.h.
|
|
|
|
* The UTF-8 macros favor sequences more the shorter they are.
|
|
|
|
* Sometimes, only the single-byte case is covered by a macro,
|
|
|
|
* while longer sequences are handled by a function call.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* single-code point definitions -------------------------------------------- */
|
|
|
|
|
|
|
|
/* classes of code unit values */
|
2000-07-25 00:46:25 +00:00
|
|
|
#define UTF8_IS_SINGLE(uchar) (((uchar)&0x80)==0)
|
2000-04-03 23:53:59 +00:00
|
|
|
#define UTF8_IS_LEAD(uchar) ((uint8_t)((uchar)-0xc0)<0x3e)
|
2000-07-25 00:46:25 +00:00
|
|
|
#define UTF8_IS_TRAIL(uchar) (((uchar)&0xc0)==0x80)
|
2000-02-03 19:54:49 +00:00
|
|
|
|
|
|
|
/* number of code units per code point */
|
|
|
|
#define UTF8_NEED_MULTIPLE_UCHAR(c) ((uint32_t)(c)>0x7f)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ICU does not deal with code points >0x10ffff
|
|
|
|
* unless necessary for advancing in the byte stream.
|
2000-04-03 23:53:59 +00:00
|
|
|
*
|
|
|
|
* These length macros take into account that for values >0x10ffff
|
|
|
|
* the "safe" append macros would write the error code point 0xffff
|
|
|
|
* with 3 bytes.
|
|
|
|
* Code point comparisons need to be in uint32_t because UChar32
|
|
|
|
* may be a signed type, and negative values must be recognized.
|
2000-02-03 19:54:49 +00:00
|
|
|
*/
|
|
|
|
#if 1
|
|
|
|
# define UTF8_CHAR_LENGTH(c) \
|
|
|
|
((uint32_t)(c)<=0x7f ? 1 : \
|
|
|
|
((uint32_t)(c)<=0x7ff ? 2 : \
|
2000-04-03 23:53:59 +00:00
|
|
|
((uint32_t)((c)-0x10000)>0xfffff ? 3 : 4) \
|
2000-02-03 19:54:49 +00:00
|
|
|
) \
|
|
|
|
)
|
|
|
|
#else
|
|
|
|
# define UTF8_CHAR_LENGTH(c) \
|
|
|
|
((uint32_t)(c)<=0x7f ? 1 : \
|
|
|
|
((uint32_t)(c)<=0x7ff ? 2 : \
|
|
|
|
((uint32_t)(c)<=0xffff ? 3 : \
|
|
|
|
((uint32_t)(c)<=0x10ffff ? 4 : \
|
|
|
|
((uint32_t)(c)<=0x3ffffff ? 5 : \
|
2000-04-03 23:53:59 +00:00
|
|
|
((uint32_t)(c)<=0x7fffffff ? 6 : 3) \
|
2000-02-03 19:54:49 +00:00
|
|
|
) \
|
|
|
|
) \
|
|
|
|
) \
|
|
|
|
) \
|
|
|
|
)
|
|
|
|
#endif
|
|
|
|
|
2000-02-09 19:42:15 +00:00
|
|
|
#define UTF8_MAX_CHAR_LENGTH 4
|
|
|
|
|
|
|
|
/* average number of code units compared to UTF-16 */
|
|
|
|
#define UTF8_ARRAY_SIZE(size) ((5*(size))/2)
|
|
|
|
|
2000-02-03 19:54:49 +00:00
|
|
|
#define UTF8_GET_CHAR_UNSAFE(s, i, c) { \
|
2000-04-03 23:53:59 +00:00
|
|
|
UTextOffset __I=(UTextOffset)(i); \
|
|
|
|
UTF8_SET_CHAR_START_UNSAFE(s, __I); \
|
|
|
|
UTF8_NEXT_CHAR_UNSAFE(s, __I, c); \
|
2000-02-03 19:54:49 +00:00
|
|
|
}
|
|
|
|
|
2000-04-12 19:36:07 +00:00
|
|
|
#define UTF8_GET_CHAR_SAFE(s, start, i, length, c, strict) { \
|
2000-04-03 23:53:59 +00:00
|
|
|
UTextOffset __I=(UTextOffset)(i); \
|
2000-04-12 19:36:07 +00:00
|
|
|
UTF8_SET_CHAR_START_SAFE(s, start, __I); \
|
2000-04-03 23:53:59 +00:00
|
|
|
UTF8_NEXT_CHAR_SAFE(s, __I, length, c, strict); \
|
2000-02-03 19:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* definitions with forward iteration --------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a Unicode scalar value from an array of UTF-8 bytes.
|
|
|
|
* Only values <=0x10ffff are accepted, and if an error occurs,
|
|
|
|
* then c will be set such that UTF_IS_ERROR(c).
|
|
|
|
* The _UNSAFE macro is fast and does not check for errors.
|
|
|
|
* The _SAFE macro checks for errors and optionally for
|
|
|
|
* irregular sequences, too, i.e., for sequences that
|
|
|
|
* are longer than necessary, such as <c0 80> instead of <0>.
|
|
|
|
* The strict checks also check for surrogates and
|
|
|
|
* for 0xXXXXfffe and 0xXXXXffff.
|
|
|
|
*/
|
|
|
|
#define UTF8_NEXT_CHAR_UNSAFE(s, i, c) { \
|
|
|
|
(c)=(s)[(i)++]; \
|
2000-04-12 19:36:07 +00:00
|
|
|
if((uint8_t)((c)-0xc0)<0x35) { \
|
2000-04-03 23:53:59 +00:00
|
|
|
uint8_t __count=UTF8_COUNT_TRAIL_BYTES(c); \
|
|
|
|
UTF8_MASK_LEAD_BYTE(c, __count); \
|
|
|
|
switch(__count) { \
|
2000-02-03 19:54:49 +00:00
|
|
|
/* each following branch falls through to the next one */ \
|
|
|
|
case 3: \
|
|
|
|
(c)=((c)<<6)|((s)[(i)++]&0x3f); \
|
|
|
|
case 2: \
|
|
|
|
(c)=((c)<<6)|((s)[(i)++]&0x3f); \
|
|
|
|
case 1: \
|
|
|
|
(c)=((c)<<6)|((s)[(i)++]&0x3f); \
|
|
|
|
/* no other branches to optimize switch() */ \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define UTF8_APPEND_CHAR_UNSAFE(s, i, c) { \
|
|
|
|
if((uint32_t)(c)<=0x7f) { \
|
|
|
|
(s)[(i)++]=(uint8_t)(c); \
|
|
|
|
} else { \
|
|
|
|
if((uint32_t)(c)<=0x7ff) { \
|
2000-08-11 00:02:59 +00:00
|
|
|
(s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
|
2000-02-03 19:54:49 +00:00
|
|
|
} else { \
|
|
|
|
if((uint32_t)(c)<=0xffff) { \
|
2000-08-11 00:02:59 +00:00
|
|
|
(s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
|
2000-02-03 19:54:49 +00:00
|
|
|
} else { \
|
2000-08-11 00:02:59 +00:00
|
|
|
(s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
|
2001-02-07 19:35:52 +00:00
|
|
|
(s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
|
2000-02-03 19:54:49 +00:00
|
|
|
} \
|
2001-02-07 19:35:52 +00:00
|
|
|
(s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
|
2000-02-03 19:54:49 +00:00
|
|
|
} \
|
2001-02-07 19:35:52 +00:00
|
|
|
(s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
|
2000-02-03 19:54:49 +00:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define UTF8_FWD_1_UNSAFE(s, i) { \
|
|
|
|
(i)+=1+UTF8_COUNT_TRAIL_BYTES((s)[i]); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define UTF8_FWD_N_UNSAFE(s, i, n) { \
|
2000-04-03 23:53:59 +00:00
|
|
|
UTextOffset __N=(n); \
|
|
|
|
while(__N>0) { \
|
2000-02-03 19:54:49 +00:00
|
|
|
UTF8_FWD_1_UNSAFE(s, i); \
|
2000-04-03 23:53:59 +00:00
|
|
|
--__N; \
|
2000-02-03 19:54:49 +00:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define UTF8_SET_CHAR_START_UNSAFE(s, i) { \
|
|
|
|
while(UTF8_IS_TRAIL((s)[i])) { --(i); } \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define UTF8_NEXT_CHAR_SAFE(s, i, length, c, strict) { \
|
|
|
|
(c)=(s)[(i)++]; \
|
2000-04-12 19:36:07 +00:00
|
|
|
if(UTF8_IS_LEAD(c)) { \
|
2000-02-03 19:54:49 +00:00
|
|
|
(c)=utf8_nextCharSafeBody(s, &(i), (UTextOffset)(length), c, strict); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define UTF8_APPEND_CHAR_SAFE(s, i, length, c) { \
|
|
|
|
if((uint32_t)(c)<=0x7f) { \
|
|
|
|
(s)[(i)++]=(uint8_t)(c); \
|
|
|
|
} else { \
|
|
|
|
(i)=utf8_appendCharSafeBody(s, (UTextOffset)(i), (UTextOffset)(length), c); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define UTF8_FWD_1_SAFE(s, i, length) { \
|
2000-04-12 19:36:07 +00:00
|
|
|
uint8_t __b=(s)[(i)++]; \
|
|
|
|
if(UTF8_IS_LEAD(__b)) { \
|
|
|
|
uint8_t __count=UTF8_COUNT_TRAIL_BYTES(__b); \
|
|
|
|
if((i)+__count>(length)) { \
|
2000-08-30 16:30:55 +00:00
|
|
|
__count=(uint8_t)((length)-(i)); \
|
2000-04-12 19:36:07 +00:00
|
|
|
} \
|
|
|
|
while(__count>0 && UTF8_IS_TRAIL((s)[i])) { \
|
|
|
|
++(i); \
|
|
|
|
--__count; \
|
2000-02-03 19:54:49 +00:00
|
|
|
} \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define UTF8_FWD_N_SAFE(s, i, length, n) { \
|
2000-04-03 23:53:59 +00:00
|
|
|
UTextOffset __N=(n); \
|
|
|
|
while(__N>0 && (i)<(length)) { \
|
2000-02-03 19:54:49 +00:00
|
|
|
UTF8_FWD_1_SAFE(s, i, length); \
|
2000-04-03 23:53:59 +00:00
|
|
|
--__N; \
|
2000-02-03 19:54:49 +00:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2000-04-12 19:36:07 +00:00
|
|
|
#define UTF8_SET_CHAR_START_SAFE(s, start, i) { \
|
|
|
|
if(UTF8_IS_TRAIL((s)[(i)])) { \
|
|
|
|
(i)=utf8_back1SafeBody(s, start, (UTextOffset)(i)); \
|
2000-02-03 19:54:49 +00:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
/* definitions with backward iteration -------------------------------------- */
|
|
|
|
|
|
|
|
#define UTF8_PREV_CHAR_UNSAFE(s, i, c) { \
|
|
|
|
(c)=(s)[--(i)]; \
|
2000-04-12 19:36:07 +00:00
|
|
|
if(UTF8_IS_TRAIL(c)) { \
|
2000-04-03 23:53:59 +00:00
|
|
|
uint8_t __b, __count=1, __shift=6; \
|
2000-02-03 19:54:49 +00:00
|
|
|
\
|
2000-04-12 19:36:07 +00:00
|
|
|
/* c is a trail byte */ \
|
2000-02-03 19:54:49 +00:00
|
|
|
(c)&=0x3f; \
|
|
|
|
for(;;) { \
|
2000-04-03 23:53:59 +00:00
|
|
|
__b=(s)[--(i)]; \
|
|
|
|
if(__b>=0xc0) { \
|
|
|
|
UTF8_MASK_LEAD_BYTE(__b, __count); \
|
|
|
|
(c)|=(UChar32)__b<<__shift; \
|
2000-02-03 19:54:49 +00:00
|
|
|
break; \
|
|
|
|
} else { \
|
2000-04-03 23:53:59 +00:00
|
|
|
(c)|=(UChar32)(__b&0x3f)<<__shift; \
|
|
|
|
++__count; \
|
|
|
|
__shift+=6; \
|
2000-02-03 19:54:49 +00:00
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define UTF8_BACK_1_UNSAFE(s, i) { \
|
|
|
|
while(UTF8_IS_TRAIL((s)[--(i)])) {} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define UTF8_BACK_N_UNSAFE(s, i, n) { \
|
2000-04-03 23:53:59 +00:00
|
|
|
UTextOffset __N=(n); \
|
|
|
|
while(__N>0) { \
|
2000-02-03 19:54:49 +00:00
|
|
|
UTF8_BACK_1_UNSAFE(s, i); \
|
2000-04-03 23:53:59 +00:00
|
|
|
--__N; \
|
2000-02-03 19:54:49 +00:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define UTF8_SET_CHAR_LIMIT_UNSAFE(s, i) { \
|
|
|
|
UTF8_BACK_1_UNSAFE(s, i); \
|
|
|
|
UTF8_FWD_1_UNSAFE(s, i); \
|
|
|
|
}
|
|
|
|
|
2000-04-12 19:36:07 +00:00
|
|
|
#define UTF8_PREV_CHAR_SAFE(s, start, i, c, strict) { \
|
2000-02-03 19:54:49 +00:00
|
|
|
(c)=(s)[--(i)]; \
|
2000-04-12 19:36:07 +00:00
|
|
|
if(UTF8_IS_TRAIL((c))) { \
|
|
|
|
(c)=utf8_prevCharSafeBody(s, start, &(i), c, strict); \
|
2000-02-03 19:54:49 +00:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2000-04-12 19:36:07 +00:00
|
|
|
#define UTF8_BACK_1_SAFE(s, start, i) { \
|
|
|
|
if(UTF8_IS_TRAIL((s)[--(i)])) { \
|
|
|
|
(i)=utf8_back1SafeBody(s, start, (UTextOffset)(i)); \
|
2000-02-03 19:54:49 +00:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2000-04-12 19:36:07 +00:00
|
|
|
#define UTF8_BACK_N_SAFE(s, start, i, n) { \
|
2000-04-03 23:53:59 +00:00
|
|
|
UTextOffset __N=(n); \
|
2000-04-12 19:36:07 +00:00
|
|
|
while(__N>0 && (i)>(start)) { \
|
|
|
|
UTF8_BACK_1_SAFE(s, start, i); \
|
2000-04-03 23:53:59 +00:00
|
|
|
--__N; \
|
2000-02-03 19:54:49 +00:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2000-04-12 19:36:07 +00:00
|
|
|
#define UTF8_SET_CHAR_LIMIT_SAFE(s, start, i, length) { \
|
|
|
|
if((start)<(i) && (i)<(length)) { \
|
|
|
|
UTF8_BACK_1_SAFE(s, start, i); \
|
|
|
|
(i)+=1+UTF8_COUNT_TRAIL_BYTES((s)[i]); \
|
|
|
|
if((i)>(length)) { \
|
|
|
|
(i)=(length); \
|
|
|
|
} \
|
|
|
|
} \
|
2000-02-03 19:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|