00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00034 #ifndef __UTF8_H__
00035 #define __UTF8_H__
00036
00037
00038 #ifndef __UTF_H__
00039 # include "unicode/utf.h"
00040 #endif
00041
00042
00043
00050 #ifdef U_UTF8_IMPL
00051 U_EXPORT const uint8_t
00052 #elif defined(U_STATIC_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION)
00053 U_CFUNC const uint8_t
00054 #else
00055 U_CFUNC U_IMPORT const uint8_t
00056 #endif
00057 utf8_countTrailBytes[256];
00058
00063 #define U8_COUNT_TRAIL_BYTES(leadByte) (utf8_countTrailBytes[(uint8_t)leadByte])
00064
00069 #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
00070
00075 U_INTERNAL UChar32 U_EXPORT2
00076 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict);
00077
00082 U_INTERNAL int32_t U_EXPORT2
00083 utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError);
00084
00089 U_INTERNAL UChar32 U_EXPORT2
00090 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict);
00091
00096 U_INTERNAL int32_t U_EXPORT2
00097 utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i);
00098
00099
00100
00107 #define U8_IS_SINGLE(c) (((c)&0x80)==0)
00108
00115 #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc0)<0x3e)
00116
00123 #define U8_IS_TRAIL(c) (((c)&0xc0)==0x80)
00124
00132 #define U8_LENGTH(c) \
00133 ((uint32_t)(c)<=0x7f ? 1 : \
00134 ((uint32_t)(c)<=0x7ff ? 2 : \
00135 ((uint32_t)(c)<=0xd7ff ? 3 : \
00136 ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
00137 ((uint32_t)(c)<=0xffff ? 3 : 4)\
00138 ) \
00139 ) \
00140 ) \
00141 )
00142
00148 #define U8_MAX_LENGTH 4
00149
00166 #define U8_GET_UNSAFE(s, i, c) { \
00167 int32_t _u8_get_unsafe_index=(int32_t)(i); \
00168 U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \
00169 U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \
00170 }
00171
00190 #define U8_GET(s, start, i, length, c) { \
00191 int32_t _u8_get_index=(int32_t)(i); \
00192 U8_SET_CP_START(s, start, _u8_get_index); \
00193 U8_NEXT(s, _u8_get_index, length, c); \
00194 }
00195
00196
00197
00215 #define U8_NEXT_UNSAFE(s, i, c) { \
00216 (c)=(uint8_t)(s)[(i)++]; \
00217 if((uint8_t)((c)-0xc0)<0x35) { \
00218 uint8_t __count=U8_COUNT_TRAIL_BYTES(c); \
00219 U8_MASK_LEAD_BYTE(c, __count); \
00220 switch(__count) { \
00221 \
00222 case 3: \
00223 (c)=((c)<<6)|((s)[(i)++]&0x3f); \
00224 case 2: \
00225 (c)=((c)<<6)|((s)[(i)++]&0x3f); \
00226 case 1: \
00227 (c)=((c)<<6)|((s)[(i)++]&0x3f); \
00228 \
00229 break; \
00230 } \
00231 } \
00232 }
00233
00252 #define U8_NEXT(s, i, length, c) { \
00253 (c)=(uint8_t)(s)[(i)++]; \
00254 if((c)>=0x80) { \
00255 uint8_t __t1, __t2; \
00256 if( \
00257 (0xe0<(c) && (c)<=0xec) && \
00258 (((i)+1)<(length)) && \
00259 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
00260 (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
00261 ) { \
00262 \
00263 (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
00264 (i)+=2; \
00265 } else if( \
00266 ((c)<0xe0 && (c)>=0xc2) && \
00267 ((i)<(length)) && \
00268 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
00269 ) { \
00270 (c)=(UChar)((((c)&0x1f)<<6)|__t1); \
00271 ++(i); \
00272 } else if(U8_IS_LEAD(c)) { \
00273 \
00274 (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (int32_t)(length), c, -1); \
00275 } else { \
00276 (c)=U_SENTINEL; \
00277 } \
00278 } \
00279 }
00280
00294 #define U8_APPEND_UNSAFE(s, i, c) { \
00295 if((uint32_t)(c)<=0x7f) { \
00296 (s)[(i)++]=(uint8_t)(c); \
00297 } else { \
00298 if((uint32_t)(c)<=0x7ff) { \
00299 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
00300 } else { \
00301 if((uint32_t)(c)<=0xffff) { \
00302 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
00303 } else { \
00304 (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
00305 (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
00306 } \
00307 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
00308 } \
00309 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
00310 } \
00311 }
00312
00330 #define U8_APPEND(s, i, capacity, c, isError) { \
00331 if((uint32_t)(c)<=0x7f) { \
00332 (s)[(i)++]=(uint8_t)(c); \
00333 } else if((uint32_t)(c)<=0x7ff && (i)+1<(capacity)) { \
00334 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
00335 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
00336 } else if((uint32_t)(c)<=0xd7ff && (i)+2<(capacity)) { \
00337 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
00338 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
00339 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
00340 } else { \
00341 (i)=utf8_appendCharSafeBody(s, (int32_t)(i), (int32_t)(capacity), c, &(isError)); \
00342 } \
00343 }
00344
00355 #define U8_FWD_1_UNSAFE(s, i) { \
00356 (i)+=1+U8_COUNT_TRAIL_BYTES((s)[i]); \
00357 }
00358
00370 #define U8_FWD_1(s, i, length) { \
00371 uint8_t __b=(uint8_t)(s)[(i)++]; \
00372 if(U8_IS_LEAD(__b)) { \
00373 uint8_t __count=U8_COUNT_TRAIL_BYTES(__b); \
00374 if((i)+__count>(length)) { \
00375 __count=(uint8_t)((length)-(i)); \
00376 } \
00377 while(__count>0 && U8_IS_TRAIL((s)[i])) { \
00378 ++(i); \
00379 --__count; \
00380 } \
00381 } \
00382 }
00383
00396 #define U8_FWD_N_UNSAFE(s, i, n) { \
00397 int32_t __N=(n); \
00398 while(__N>0) { \
00399 U8_FWD_1_UNSAFE(s, i); \
00400 --__N; \
00401 } \
00402 }
00403
00417 #define U8_FWD_N(s, i, length, n) { \
00418 int32_t __N=(n); \
00419 while(__N>0 && (i)<(length)) { \
00420 U8_FWD_1(s, i, length); \
00421 --__N; \
00422 } \
00423 }
00424
00438 #define U8_SET_CP_START_UNSAFE(s, i) { \
00439 while(U8_IS_TRAIL((s)[i])) { --(i); } \
00440 }
00441
00456 #define U8_SET_CP_START(s, start, i) { \
00457 if(U8_IS_TRAIL((s)[(i)])) { \
00458 (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
00459 } \
00460 }
00461
00462
00463
00483 #define U8_PREV_UNSAFE(s, i, c) { \
00484 (c)=(uint8_t)(s)[--(i)]; \
00485 if(U8_IS_TRAIL(c)) { \
00486 uint8_t __b, __count=1, __shift=6; \
00487 \
00488 \
00489 (c)&=0x3f; \
00490 for(;;) { \
00491 __b=(uint8_t)(s)[--(i)]; \
00492 if(__b>=0xc0) { \
00493 U8_MASK_LEAD_BYTE(__b, __count); \
00494 (c)|=(UChar32)__b<<__shift; \
00495 break; \
00496 } else { \
00497 (c)|=(UChar32)(__b&0x3f)<<__shift; \
00498 ++__count; \
00499 __shift+=6; \
00500 } \
00501 } \
00502 } \
00503 }
00504
00525 #define U8_PREV(s, start, i, c) { \
00526 (c)=(uint8_t)(s)[--(i)]; \
00527 if((c)>=0x80) { \
00528 if((c)<=0xbf) { \
00529 (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
00530 } else { \
00531 (c)=U_SENTINEL; \
00532 } \
00533 } \
00534 }
00535
00547 #define U8_BACK_1_UNSAFE(s, i) { \
00548 while(U8_IS_TRAIL((s)[--(i)])) {} \
00549 }
00550
00563 #define U8_BACK_1(s, start, i) { \
00564 if(U8_IS_TRAIL((s)[--(i)])) { \
00565 (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
00566 } \
00567 }
00568
00582 #define U8_BACK_N_UNSAFE(s, i, n) { \
00583 int32_t __N=(n); \
00584 while(__N>0) { \
00585 U8_BACK_1_UNSAFE(s, i); \
00586 --__N; \
00587 } \
00588 }
00589
00604 #define U8_BACK_N(s, start, i, n) { \
00605 int32_t __N=(n); \
00606 while(__N>0 && (i)>(start)) { \
00607 U8_BACK_1(s, start, i); \
00608 --__N; \
00609 } \
00610 }
00611
00625 #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \
00626 U8_BACK_1_UNSAFE(s, i); \
00627 U8_FWD_1_UNSAFE(s, i); \
00628 }
00629
00645 #define U8_SET_CP_LIMIT(s, start, i, length) { \
00646 if((start)<(i) && (i)<(length)) { \
00647 U8_BACK_1(s, start, i); \
00648 U8_FWD_1(s, i, length); \
00649 } \
00650 }
00651
00652 #endif