00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00034 #ifndef __UTF8_H__
00035 #define __UTF8_H__
00036
00037 #include "unicode/umachine.h"
00038 #ifndef __UTF_H__
00039 # include "unicode/utf.h"
00040 #endif
00041
00042
00043
00055 #define U8_COUNT_TRAIL_BYTES(leadByte) \
00056 (U8_IS_LEAD(leadByte) ? \
00057 ((uint8_t)(leadByte)>=0xe0)+((uint8_t)(leadByte)>=0xf0)+1 : 0)
00058
00070 #define U8_COUNT_TRAIL_BYTES_UNSAFE(leadByte) \
00071 (((uint8_t)(leadByte)>=0xc2)+((uint8_t)(leadByte)>=0xe0)+((uint8_t)(leadByte)>=0xf0))
00072
00080 #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
00081
00090 #define U8_LEAD3_T1_BITS "\x20\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x10\x30\x30"
00091
00097 #define U8_IS_VALID_LEAD3_AND_T1(lead, t1) (U8_LEAD3_T1_BITS[(lead)&0xf]&(1<<((uint8_t)(t1)>>5)))
00098
00107 #define U8_LEAD4_T1_BITS "\x00\x00\x00\x00\x00\x00\x00\x00\x1E\x0F\x0F\x0F\x00\x00\x00\x00"
00108
00114 #define U8_IS_VALID_LEAD4_AND_T1(lead, t1) (U8_LEAD4_T1_BITS[(uint8_t)(t1)>>4]&(1<<((lead)&7)))
00115
00125 U_STABLE UChar32 U_EXPORT2
00126 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict);
00127
00137 U_STABLE int32_t U_EXPORT2
00138 utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError);
00139
00149 U_STABLE UChar32 U_EXPORT2
00150 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict);
00151
00161 U_STABLE int32_t U_EXPORT2
00162 utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i);
00163
00164
00165
00172 #define U8_IS_SINGLE(c) (((c)&0x80)==0)
00173
00180 #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc2)<=0x32)
00181
00182
00189 #define U8_IS_TRAIL(c) ((int8_t)(c)<-0x40)
00190
00198 #define U8_LENGTH(c) \
00199 ((uint32_t)(c)<=0x7f ? 1 : \
00200 ((uint32_t)(c)<=0x7ff ? 2 : \
00201 ((uint32_t)(c)<=0xd7ff ? 3 : \
00202 ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
00203 ((uint32_t)(c)<=0xffff ? 3 : 4)\
00204 ) \
00205 ) \
00206 ) \
00207 )
00208
00214 #define U8_MAX_LENGTH 4
00215
00232 #define U8_GET_UNSAFE(s, i, c) { \
00233 int32_t _u8_get_unsafe_index=(int32_t)(i); \
00234 U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \
00235 U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \
00236 }
00237
00259 #define U8_GET(s, start, i, length, c) { \
00260 int32_t _u8_get_index=(i); \
00261 U8_SET_CP_START(s, start, _u8_get_index); \
00262 U8_NEXT(s, _u8_get_index, length, c); \
00263 }
00264
00290 #define U8_GET_OR_FFFD(s, start, i, length, c) { \
00291 int32_t _u8_get_index=(i); \
00292 U8_SET_CP_START(s, start, _u8_get_index); \
00293 U8_NEXT_OR_FFFD(s, _u8_get_index, length, c); \
00294 }
00295
00296
00297
00315 #define U8_NEXT_UNSAFE(s, i, c) { \
00316 (c)=(uint8_t)(s)[(i)++]; \
00317 if(!U8_IS_SINGLE(c)) { \
00318 if((c)<0xe0) { \
00319 (c)=(((c)&0x1f)<<6)|((s)[(i)++]&0x3f); \
00320 } else if((c)<0xf0) { \
00321 \
00322 (c)=(UChar)(((c)<<12)|(((s)[i]&0x3f)<<6)|((s)[(i)+1]&0x3f)); \
00323 (i)+=2; \
00324 } else { \
00325 (c)=(((c)&7)<<18)|(((s)[i]&0x3f)<<12)|(((s)[(i)+1]&0x3f)<<6)|((s)[(i)+2]&0x3f); \
00326 (i)+=3; \
00327 } \
00328 } \
00329 }
00330
00351 #define U8_NEXT(s, i, length, c) U8_INTERNAL_NEXT_OR_SUB(s, i, length, c, U_SENTINEL)
00352
00377 #define U8_NEXT_OR_FFFD(s, i, length, c) U8_INTERNAL_NEXT_OR_SUB(s, i, length, c, 0xfffd)
00378
00380 #define U8_INTERNAL_NEXT_OR_SUB(s, i, length, c, sub) { \
00381 (c)=(uint8_t)(s)[(i)++]; \
00382 if(!U8_IS_SINGLE(c)) { \
00383 uint8_t __t = 0; \
00384 if((i)!=(length) && \
00385 \
00386 ((c)>=0xe0 ? \
00387 ((c)<0xf0 ? \
00388 U8_LEAD3_T1_BITS[(c)&=0xf]&(1<<((__t=(s)[i])>>5)) && \
00389 (__t&=0x3f, 1) \
00390 : \
00391 ((c)-=0xf0)<=4 && \
00392 U8_LEAD4_T1_BITS[(__t=(s)[i])>>4]&(1<<(c)) && \
00393 ((c)=((c)<<6)|(__t&0x3f), ++(i)!=(length)) && \
00394 (__t=(s)[i]-0x80)<=0x3f) && \
00395 \
00396 ((c)=((c)<<6)|__t, ++(i)!=(length)) \
00397 : \
00398 (c)>=0xc2 && ((c)&=0x1f, 1)) && \
00399 \
00400 (__t=(s)[i]-0x80)<=0x3f && \
00401 ((c)=((c)<<6)|__t, ++(i), 1)) { \
00402 } else { \
00403 (c)=(sub); \
00404 } \
00405 } \
00406 }
00407
00421 #define U8_APPEND_UNSAFE(s, i, c) { \
00422 uint32_t __uc=(c); \
00423 if(__uc<=0x7f) { \
00424 (s)[(i)++]=(uint8_t)__uc; \
00425 } else { \
00426 if(__uc<=0x7ff) { \
00427 (s)[(i)++]=(uint8_t)((__uc>>6)|0xc0); \
00428 } else { \
00429 if(__uc<=0xffff) { \
00430 (s)[(i)++]=(uint8_t)((__uc>>12)|0xe0); \
00431 } else { \
00432 (s)[(i)++]=(uint8_t)((__uc>>18)|0xf0); \
00433 (s)[(i)++]=(uint8_t)(((__uc>>12)&0x3f)|0x80); \
00434 } \
00435 (s)[(i)++]=(uint8_t)(((__uc>>6)&0x3f)|0x80); \
00436 } \
00437 (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \
00438 } \
00439 }
00440
00458 #define U8_APPEND(s, i, capacity, c, isError) { \
00459 uint32_t __uc=(c); \
00460 if(__uc<=0x7f) { \
00461 (s)[(i)++]=(uint8_t)__uc; \
00462 } else if(__uc<=0x7ff && (i)+1<(capacity)) { \
00463 (s)[(i)++]=(uint8_t)((__uc>>6)|0xc0); \
00464 (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \
00465 } else if((__uc<=0xd7ff || (0xe000<=__uc && __uc<=0xffff)) && (i)+2<(capacity)) { \
00466 (s)[(i)++]=(uint8_t)((__uc>>12)|0xe0); \
00467 (s)[(i)++]=(uint8_t)(((__uc>>6)&0x3f)|0x80); \
00468 (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \
00469 } else if(0xffff<__uc && __uc<=0x10ffff && (i)+3<(capacity)) { \
00470 (s)[(i)++]=(uint8_t)((__uc>>18)|0xf0); \
00471 (s)[(i)++]=(uint8_t)(((__uc>>12)&0x3f)|0x80); \
00472 (s)[(i)++]=(uint8_t)(((__uc>>6)&0x3f)|0x80); \
00473 (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \
00474 } else { \
00475 (isError)=TRUE; \
00476 } \
00477 }
00478
00489 #define U8_FWD_1_UNSAFE(s, i) { \
00490 (i)+=1+U8_COUNT_TRAIL_BYTES_UNSAFE((s)[i]); \
00491 }
00492
00506 #define U8_FWD_1(s, i, length) { \
00507 uint8_t __b=(s)[(i)++]; \
00508 if(U8_IS_LEAD(__b) && (i)!=(length)) { \
00509 uint8_t __t1=(s)[i]; \
00510 if((0xe0<=__b && __b<0xf0)) { \
00511 if(U8_IS_VALID_LEAD3_AND_T1(__b, __t1) && \
00512 ++(i)!=(length) && U8_IS_TRAIL((s)[i])) { \
00513 ++(i); \
00514 } \
00515 } else if(__b<0xe0) { \
00516 if(U8_IS_TRAIL(__t1)) { \
00517 ++(i); \
00518 } \
00519 } else { \
00520 if(U8_IS_VALID_LEAD4_AND_T1(__b, __t1) && \
00521 ++(i)!=(length) && U8_IS_TRAIL((s)[i]) && \
00522 ++(i)!=(length) && U8_IS_TRAIL((s)[i])) { \
00523 ++(i); \
00524 } \
00525 } \
00526 } \
00527 }
00528
00541 #define U8_FWD_N_UNSAFE(s, i, n) { \
00542 int32_t __N=(n); \
00543 while(__N>0) { \
00544 U8_FWD_1_UNSAFE(s, i); \
00545 --__N; \
00546 } \
00547 }
00548
00564 #define U8_FWD_N(s, i, length, n) { \
00565 int32_t __N=(n); \
00566 while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
00567 U8_FWD_1(s, i, length); \
00568 --__N; \
00569 } \
00570 }
00571
00585 #define U8_SET_CP_START_UNSAFE(s, i) { \
00586 while(U8_IS_TRAIL((s)[i])) { --(i); } \
00587 }
00588
00606 #define U8_SET_CP_START(s, start, i) { \
00607 if(U8_IS_TRAIL((s)[(i)])) { \
00608 (i)=utf8_back1SafeBody(s, start, (i)); \
00609 } \
00610 }
00611
00612 #ifndef U_HIDE_DRAFT_API
00613
00639 #define U8_TRUNCATE_IF_INCOMPLETE(s, start, length) \
00640 if((length)>(start)) { \
00641 uint8_t __b1=s[(length)-1]; \
00642 if(U8_IS_SINGLE(__b1)) { \
00643 \
00644 } else if(U8_IS_LEAD(__b1)) { \
00645 --(length); \
00646 } else if(U8_IS_TRAIL(__b1) && ((length)-2)>=(start)) { \
00647 uint8_t __b2=s[(length)-2]; \
00648 if(0xe0<=__b2 && __b2<=0xf4) { \
00649 if(__b2<0xf0 ? U8_IS_VALID_LEAD3_AND_T1(__b2, __b1) : \
00650 U8_IS_VALID_LEAD4_AND_T1(__b2, __b1)) { \
00651 (length)-=2; \
00652 } \
00653 } else if(U8_IS_TRAIL(__b2) && ((length)-3)>=(start)) { \
00654 uint8_t __b3=s[(length)-3]; \
00655 if(0xf0<=__b3 && __b3<=0xf4 && U8_IS_VALID_LEAD4_AND_T1(__b3, __b2)) { \
00656 (length)-=3; \
00657 } \
00658 } \
00659 } \
00660 }
00661 #endif // U_HIDE_DRAFT_API
00662
00663
00664
00684 #define U8_PREV_UNSAFE(s, i, c) { \
00685 (c)=(uint8_t)(s)[--(i)]; \
00686 if(U8_IS_TRAIL(c)) { \
00687 uint8_t __b, __count=1, __shift=6; \
00688 \
00689 \
00690 (c)&=0x3f; \
00691 for(;;) { \
00692 __b=(s)[--(i)]; \
00693 if(__b>=0xc0) { \
00694 U8_MASK_LEAD_BYTE(__b, __count); \
00695 (c)|=(UChar32)__b<<__shift; \
00696 break; \
00697 } else { \
00698 (c)|=(UChar32)(__b&0x3f)<<__shift; \
00699 ++__count; \
00700 __shift+=6; \
00701 } \
00702 } \
00703 } \
00704 }
00705
00726 #define U8_PREV(s, start, i, c) { \
00727 (c)=(uint8_t)(s)[--(i)]; \
00728 if(!U8_IS_SINGLE(c)) { \
00729 (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
00730 } \
00731 }
00732
00757 #define U8_PREV_OR_FFFD(s, start, i, c) { \
00758 (c)=(uint8_t)(s)[--(i)]; \
00759 if(!U8_IS_SINGLE(c)) { \
00760 (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -3); \
00761 } \
00762 }
00763
00775 #define U8_BACK_1_UNSAFE(s, i) { \
00776 while(U8_IS_TRAIL((s)[--(i)])) {} \
00777 }
00778
00791 #define U8_BACK_1(s, start, i) { \
00792 if(U8_IS_TRAIL((s)[--(i)])) { \
00793 (i)=utf8_back1SafeBody(s, start, (i)); \
00794 } \
00795 }
00796
00810 #define U8_BACK_N_UNSAFE(s, i, n) { \
00811 int32_t __N=(n); \
00812 while(__N>0) { \
00813 U8_BACK_1_UNSAFE(s, i); \
00814 --__N; \
00815 } \
00816 }
00817
00832 #define U8_BACK_N(s, start, i, n) { \
00833 int32_t __N=(n); \
00834 while(__N>0 && (i)>(start)) { \
00835 U8_BACK_1(s, start, i); \
00836 --__N; \
00837 } \
00838 }
00839
00853 #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \
00854 U8_BACK_1_UNSAFE(s, i); \
00855 U8_FWD_1_UNSAFE(s, i); \
00856 }
00857
00875 #define U8_SET_CP_LIMIT(s, start, i, length) { \
00876 if((start)<(i) && ((i)<(length) || (length)<0)) { \
00877 U8_BACK_1(s, start, i); \
00878 U8_FWD_1(s, i, length); \
00879 } \
00880 }
00881
00882 #endif