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_INTERNAL const uint8_t
00052 #elif defined(U_STATIC_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)=(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)=(s)[(i)++]; \
00254 if(((uint8_t)(c))>=0x80) { \
00255 if(U8_IS_LEAD(c)) { \
00256 (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (int32_t)(length), c, -1); \
00257 } else { \
00258 (c)=U_SENTINEL; \
00259 } \
00260 } \
00261 }
00262
00276 #define U8_APPEND_UNSAFE(s, i, c) { \
00277 if((uint32_t)(c)<=0x7f) { \
00278 (s)[(i)++]=(uint8_t)(c); \
00279 } else { \
00280 if((uint32_t)(c)<=0x7ff) { \
00281 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
00282 } else { \
00283 if((uint32_t)(c)<=0xffff) { \
00284 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
00285 } else { \
00286 (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
00287 (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
00288 } \
00289 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
00290 } \
00291 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
00292 } \
00293 }
00294
00312 #define U8_APPEND(s, i, length, c, isError) { \
00313 if((uint32_t)(c)<=0x7f) { \
00314 (s)[(i)++]=(uint8_t)(c); \
00315 } else { \
00316 (i)=utf8_appendCharSafeBody(s, (int32_t)(i), (int32_t)(length), c, &(isError)); \
00317 } \
00318 }
00319
00330 #define U8_FWD_1_UNSAFE(s, i) { \
00331 (i)+=1+U8_COUNT_TRAIL_BYTES((s)[i]); \
00332 }
00333
00345 #define U8_FWD_1(s, i, length) { \
00346 uint8_t __b=(s)[(i)++]; \
00347 if(U8_IS_LEAD(__b)) { \
00348 uint8_t __count=U8_COUNT_TRAIL_BYTES(__b); \
00349 if((i)+__count>(length)) { \
00350 __count=(uint8_t)((length)-(i)); \
00351 } \
00352 while(__count>0 && U8_IS_TRAIL((s)[i])) { \
00353 ++(i); \
00354 --__count; \
00355 } \
00356 } \
00357 }
00358
00371 #define U8_FWD_N_UNSAFE(s, i, n) { \
00372 int32_t __N=(n); \
00373 while(__N>0) { \
00374 U8_FWD_1_UNSAFE(s, i); \
00375 --__N; \
00376 } \
00377 }
00378
00392 #define U8_FWD_N(s, i, length, n) { \
00393 int32_t __N=(n); \
00394 while(__N>0 && (i)<(length)) { \
00395 U8_FWD_1(s, i, length); \
00396 --__N; \
00397 } \
00398 }
00399
00413 #define U8_SET_CP_START_UNSAFE(s, i) { \
00414 while(U8_IS_TRAIL((s)[i])) { --(i); } \
00415 }
00416
00431 #define U8_SET_CP_START(s, start, i) { \
00432 if(U8_IS_TRAIL((s)[(i)])) { \
00433 (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
00434 } \
00435 }
00436
00437
00438
00458 #define U8_PREV_UNSAFE(s, i, c) { \
00459 (c)=(s)[--(i)]; \
00460 if(U8_IS_TRAIL(c)) { \
00461 uint8_t __b, __count=1, __shift=6; \
00462 \
00463 \
00464 (c)&=0x3f; \
00465 for(;;) { \
00466 __b=(s)[--(i)]; \
00467 if(__b>=0xc0) { \
00468 U8_MASK_LEAD_BYTE(__b, __count); \
00469 (c)|=(UChar32)__b<<__shift; \
00470 break; \
00471 } else { \
00472 (c)|=(UChar32)(__b&0x3f)<<__shift; \
00473 ++__count; \
00474 __shift+=6; \
00475 } \
00476 } \
00477 } \
00478 }
00479
00500 #define U8_PREV(s, start, i, c) { \
00501 (c)=(s)[--(i)]; \
00502 if((c)>=0x80) { \
00503 if((c)<=0xbf) { \
00504 (c)=utf8_prevCharSafeBody(s, start, &(i), c, -1); \
00505 } else { \
00506 (c)=U_SENTINEL; \
00507 } \
00508 } \
00509 }
00510
00522 #define U8_BACK_1_UNSAFE(s, i) { \
00523 while(U8_IS_TRAIL((s)[--(i)])) {} \
00524 }
00525
00538 #define U8_BACK_1(s, start, i) { \
00539 if(U8_IS_TRAIL((s)[--(i)])) { \
00540 (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
00541 } \
00542 }
00543
00557 #define U8_BACK_N_UNSAFE(s, i, n) { \
00558 int32_t __N=(n); \
00559 while(__N>0) { \
00560 U8_BACK_1_UNSAFE(s, i); \
00561 --__N; \
00562 } \
00563 }
00564
00579 #define U8_BACK_N(s, start, i, n) { \
00580 int32_t __N=(n); \
00581 while(__N>0 && (i)>(start)) { \
00582 U8_BACK_1(s, start, i); \
00583 --__N; \
00584 } \
00585 }
00586
00600 #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \
00601 U8_BACK_1_UNSAFE(s, i); \
00602 U8_FWD_1_UNSAFE(s, i); \
00603 }
00604
00620 #define U8_SET_CP_LIMIT(s, start, i, length) { \
00621 if((start)<(i) && (i)<(length)) { \
00622 U8_BACK_1(s, start, i); \
00623 U8_FWD_1(s, i, length); \
00624 } \
00625 }
00626
00627 #endif