00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00032 #ifndef __UTF8_H__
00033 #define __UTF8_H__
00034
00035 #include "unicode/umachine.h"
00036 #ifndef __UTF_H__
00037 # include "unicode/utf.h"
00038 #endif
00039
00040
00041
00053 #ifdef U_UTF8_IMPL
00054 U_EXPORT const uint8_t
00055 #elif defined(U_STATIC_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION)
00056 U_CFUNC const uint8_t
00057 #else
00058 U_CFUNC U_IMPORT const uint8_t
00059 #endif
00060 utf8_countTrailBytes[256];
00061
00080 #define U8_COUNT_TRAIL_BYTES(leadByte) \
00081 ((uint8_t)(leadByte)<0xf0 ? \
00082 ((uint8_t)(leadByte)>=0xc0)+((uint8_t)(leadByte)>=0xe0) : \
00083 (uint8_t)(leadByte)<0xfe ? 3+((uint8_t)(leadByte)>=0xf8)+((uint8_t)(leadByte)>=0xfc) : 0)
00084
00096 #define U8_COUNT_TRAIL_BYTES_UNSAFE(leadByte) \
00097 (((leadByte)>=0xc0)+((leadByte)>=0xe0)+((leadByte)>=0xf0))
00098
00106 #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
00107
00117 U_STABLE UChar32 U_EXPORT2
00118 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict);
00119
00129 U_STABLE int32_t U_EXPORT2
00130 utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError);
00131
00141 U_STABLE UChar32 U_EXPORT2
00142 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict);
00143
00153 U_STABLE int32_t U_EXPORT2
00154 utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i);
00155
00156
00157
00164 #define U8_IS_SINGLE(c) (((c)&0x80)==0)
00165
00172 #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc0)<0x3e)
00173
00180 #define U8_IS_TRAIL(c) (((c)&0xc0)==0x80)
00181
00189 #define U8_LENGTH(c) \
00190 ((uint32_t)(c)<=0x7f ? 1 : \
00191 ((uint32_t)(c)<=0x7ff ? 2 : \
00192 ((uint32_t)(c)<=0xd7ff ? 3 : \
00193 ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
00194 ((uint32_t)(c)<=0xffff ? 3 : 4)\
00195 ) \
00196 ) \
00197 ) \
00198 )
00199
00205 #define U8_MAX_LENGTH 4
00206
00223 #define U8_GET_UNSAFE(s, i, c) { \
00224 int32_t _u8_get_unsafe_index=(int32_t)(i); \
00225 U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \
00226 U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \
00227 }
00228
00250 #define U8_GET(s, start, i, length, c) { \
00251 int32_t _u8_get_index=(i); \
00252 U8_SET_CP_START(s, start, _u8_get_index); \
00253 U8_NEXT(s, _u8_get_index, length, c); \
00254 }
00255
00281 #define U8_GET_OR_FFFD(s, start, i, length, c) { \
00282 int32_t _u8_get_index=(i); \
00283 U8_SET_CP_START(s, start, _u8_get_index); \
00284 U8_NEXT_OR_FFFD(s, _u8_get_index, length, c); \
00285 }
00286
00287
00288
00306 #define U8_NEXT_UNSAFE(s, i, c) { \
00307 (c)=(uint8_t)(s)[(i)++]; \
00308 if((c)>=0x80) { \
00309 if((c)<0xe0) { \
00310 (c)=(((c)&0x1f)<<6)|((s)[(i)++]&0x3f); \
00311 } else if((c)<0xf0) { \
00312 \
00313 (c)=(UChar)(((c)<<12)|(((s)[i]&0x3f)<<6)|((s)[(i)+1]&0x3f)); \
00314 (i)+=2; \
00315 } else { \
00316 (c)=(((c)&7)<<18)|(((s)[i]&0x3f)<<12)|(((s)[(i)+1]&0x3f)<<6)|((s)[(i)+2]&0x3f); \
00317 (i)+=3; \
00318 } \
00319 } \
00320 }
00321
00342 #define U8_NEXT(s, i, length, c) { \
00343 (c)=(uint8_t)(s)[(i)++]; \
00344 if((c)>=0x80) { \
00345 uint8_t __t1, __t2; \
00346 if( \
00347 (0xe0<(c) && (c)<=0xec) && \
00348 (((i)+1)<(length) || (length)<0) && \
00349 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
00350 (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
00351 ) { \
00352 \
00353 (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
00354 (i)+=2; \
00355 } else if( \
00356 ((c)<0xe0 && (c)>=0xc2) && \
00357 ((i)!=(length)) && \
00358 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
00359 ) { \
00360 (c)=(((c)&0x1f)<<6)|__t1; \
00361 ++(i); \
00362 } else { \
00363 \
00364 (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (length), c, -1); \
00365 } \
00366 } \
00367 }
00368
00393 #define U8_NEXT_OR_FFFD(s, i, length, c) { \
00394 (c)=(uint8_t)(s)[(i)++]; \
00395 if((c)>=0x80) { \
00396 uint8_t __t1, __t2; \
00397 if( \
00398 (0xe0<(c) && (c)<=0xec) && \
00399 (((i)+1)<(length) || (length)<0) && \
00400 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
00401 (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
00402 ) { \
00403 \
00404 (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
00405 (i)+=2; \
00406 } else if( \
00407 ((c)<0xe0 && (c)>=0xc2) && \
00408 ((i)!=(length)) && \
00409 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
00410 ) { \
00411 (c)=(((c)&0x1f)<<6)|__t1; \
00412 ++(i); \
00413 } else { \
00414 \
00415 (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (length), c, -3); \
00416 } \
00417 } \
00418 }
00419
00433 #define U8_APPEND_UNSAFE(s, i, c) { \
00434 if((uint32_t)(c)<=0x7f) { \
00435 (s)[(i)++]=(uint8_t)(c); \
00436 } else { \
00437 if((uint32_t)(c)<=0x7ff) { \
00438 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
00439 } else { \
00440 if((uint32_t)(c)<=0xffff) { \
00441 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
00442 } else { \
00443 (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
00444 (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
00445 } \
00446 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
00447 } \
00448 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
00449 } \
00450 }
00451
00469 #define U8_APPEND(s, i, capacity, c, isError) { \
00470 if((uint32_t)(c)<=0x7f) { \
00471 (s)[(i)++]=(uint8_t)(c); \
00472 } else if((uint32_t)(c)<=0x7ff && (i)+1<(capacity)) { \
00473 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
00474 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
00475 } else if((uint32_t)(c)<=0xd7ff && (i)+2<(capacity)) { \
00476 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
00477 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
00478 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
00479 } else { \
00480 (i)=utf8_appendCharSafeBody(s, (i), (capacity), c, &(isError)); \
00481 } \
00482 }
00483
00494 #define U8_FWD_1_UNSAFE(s, i) { \
00495 (i)+=1+U8_COUNT_TRAIL_BYTES_UNSAFE((uint8_t)(s)[i]); \
00496 }
00497
00511 #define U8_FWD_1(s, i, length) { \
00512 uint8_t __b=(uint8_t)(s)[(i)++]; \
00513 if(U8_IS_LEAD(__b)) { \
00514 uint8_t __count=U8_COUNT_TRAIL_BYTES(__b); \
00515 if((i)+__count>(length) && (length)>=0) { \
00516 __count=(uint8_t)((length)-(i)); \
00517 } \
00518 while(__count>0 && U8_IS_TRAIL((s)[i])) { \
00519 ++(i); \
00520 --__count; \
00521 } \
00522 } \
00523 }
00524
00537 #define U8_FWD_N_UNSAFE(s, i, n) { \
00538 int32_t __N=(n); \
00539 while(__N>0) { \
00540 U8_FWD_1_UNSAFE(s, i); \
00541 --__N; \
00542 } \
00543 }
00544
00560 #define U8_FWD_N(s, i, length, n) { \
00561 int32_t __N=(n); \
00562 while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
00563 U8_FWD_1(s, i, length); \
00564 --__N; \
00565 } \
00566 }
00567
00581 #define U8_SET_CP_START_UNSAFE(s, i) { \
00582 while(U8_IS_TRAIL((s)[i])) { --(i); } \
00583 }
00584
00599 #define U8_SET_CP_START(s, start, i) { \
00600 if(U8_IS_TRAIL((s)[(i)])) { \
00601 (i)=utf8_back1SafeBody(s, start, (i)); \
00602 } \
00603 }
00604
00605
00606
00626 #define U8_PREV_UNSAFE(s, i, c) { \
00627 (c)=(uint8_t)(s)[--(i)]; \
00628 if(U8_IS_TRAIL(c)) { \
00629 uint8_t __b, __count=1, __shift=6; \
00630 \
00631 \
00632 (c)&=0x3f; \
00633 for(;;) { \
00634 __b=(uint8_t)(s)[--(i)]; \
00635 if(__b>=0xc0) { \
00636 U8_MASK_LEAD_BYTE(__b, __count); \
00637 (c)|=(UChar32)__b<<__shift; \
00638 break; \
00639 } else { \
00640 (c)|=(UChar32)(__b&0x3f)<<__shift; \
00641 ++__count; \
00642 __shift+=6; \
00643 } \
00644 } \
00645 } \
00646 }
00647
00668 #define U8_PREV(s, start, i, c) { \
00669 (c)=(uint8_t)(s)[--(i)]; \
00670 if((c)>=0x80) { \
00671 (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
00672 } \
00673 }
00674
00699 #define U8_PREV_OR_FFFD(s, start, i, c) { \
00700 (c)=(uint8_t)(s)[--(i)]; \
00701 if((c)>=0x80) { \
00702 (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -3); \
00703 } \
00704 }
00705
00717 #define U8_BACK_1_UNSAFE(s, i) { \
00718 while(U8_IS_TRAIL((s)[--(i)])) {} \
00719 }
00720
00733 #define U8_BACK_1(s, start, i) { \
00734 if(U8_IS_TRAIL((s)[--(i)])) { \
00735 (i)=utf8_back1SafeBody(s, start, (i)); \
00736 } \
00737 }
00738
00752 #define U8_BACK_N_UNSAFE(s, i, n) { \
00753 int32_t __N=(n); \
00754 while(__N>0) { \
00755 U8_BACK_1_UNSAFE(s, i); \
00756 --__N; \
00757 } \
00758 }
00759
00774 #define U8_BACK_N(s, start, i, n) { \
00775 int32_t __N=(n); \
00776 while(__N>0 && (i)>(start)) { \
00777 U8_BACK_1(s, start, i); \
00778 --__N; \
00779 } \
00780 }
00781
00795 #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \
00796 U8_BACK_1_UNSAFE(s, i); \
00797 U8_FWD_1_UNSAFE(s, i); \
00798 }
00799
00817 #define U8_SET_CP_LIMIT(s, start, i, length) { \
00818 if((start)<(i) && ((i)<(length) || (length)<0)) { \
00819 U8_BACK_1(s, start, i); \
00820 U8_FWD_1(s, i, length); \
00821 } \
00822 }
00823
00824 #endif