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 <stdbool.h>
00038 #include "unicode/umachine.h"
00039 #ifndef __UTF_H__
00040 # include "unicode/utf.h"
00041 #endif
00042
00043
00044
00056 #define U8_COUNT_TRAIL_BYTES(leadByte) \
00057 (U8_IS_LEAD(leadByte) ? \
00058 ((uint8_t)(leadByte)>=0xe0)+((uint8_t)(leadByte)>=0xf0)+1 : 0)
00059
00071 #define U8_COUNT_TRAIL_BYTES_UNSAFE(leadByte) \
00072 (((uint8_t)(leadByte)>=0xc2)+((uint8_t)(leadByte)>=0xe0)+((uint8_t)(leadByte)>=0xf0))
00073
00081 #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
00082
00091 #define U8_LEAD3_T1_BITS "\x20\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x10\x30\x30"
00092
00098 #define U8_IS_VALID_LEAD3_AND_T1(lead, t1) (U8_LEAD3_T1_BITS[(lead)&0xf]&(1<<((uint8_t)(t1)>>5)))
00099
00108 #define U8_LEAD4_T1_BITS "\x00\x00\x00\x00\x00\x00\x00\x00\x1E\x0F\x0F\x0F\x00\x00\x00\x00"
00109
00115 #define U8_IS_VALID_LEAD4_AND_T1(lead, t1) (U8_LEAD4_T1_BITS[(uint8_t)(t1)>>4]&(1<<((lead)&7)))
00116
00126 U_CAPI UChar32 U_EXPORT2
00127 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict);
00128
00138 U_CAPI int32_t U_EXPORT2
00139 utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError);
00140
00150 U_CAPI UChar32 U_EXPORT2
00151 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict);
00152
00162 U_CAPI int32_t U_EXPORT2
00163 utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i);
00164
00165
00166
00173 #define U8_IS_SINGLE(c) (((c)&0x80)==0)
00174
00181 #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc2)<=0x32)
00182
00183
00190 #define U8_IS_TRAIL(c) ((int8_t)(c)<-0x40)
00191
00199 #define U8_LENGTH(c) \
00200 ((uint32_t)(c)<=0x7f ? 1 : \
00201 ((uint32_t)(c)<=0x7ff ? 2 : \
00202 ((uint32_t)(c)<=0xd7ff ? 3 : \
00203 ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
00204 ((uint32_t)(c)<=0xffff ? 3 : 4)\
00205 ) \
00206 ) \
00207 ) \
00208 )
00209
00215 #define U8_MAX_LENGTH 4
00216
00233 #define U8_GET_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
00234 int32_t _u8_get_unsafe_index=(int32_t)(i); \
00235 U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \
00236 U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \
00237 } UPRV_BLOCK_MACRO_END
00238
00260 #define U8_GET(s, start, i, length, c) UPRV_BLOCK_MACRO_BEGIN { \
00261 int32_t _u8_get_index=(i); \
00262 U8_SET_CP_START(s, start, _u8_get_index); \
00263 U8_NEXT(s, _u8_get_index, length, c); \
00264 } UPRV_BLOCK_MACRO_END
00265
00291 #define U8_GET_OR_FFFD(s, start, i, length, c) UPRV_BLOCK_MACRO_BEGIN { \
00292 int32_t _u8_get_index=(i); \
00293 U8_SET_CP_START(s, start, _u8_get_index); \
00294 U8_NEXT_OR_FFFD(s, _u8_get_index, length, c); \
00295 } UPRV_BLOCK_MACRO_END
00296
00297
00298
00316 #define U8_NEXT_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
00317 (c)=(uint8_t)(s)[(i)++]; \
00318 if(!U8_IS_SINGLE(c)) { \
00319 if((c)<0xe0) { \
00320 (c)=(((c)&0x1f)<<6)|((s)[(i)++]&0x3f); \
00321 } else if((c)<0xf0) { \
00322 \
00323 (c)=(UChar)(((c)<<12)|(((s)[i]&0x3f)<<6)|((s)[(i)+1]&0x3f)); \
00324 (i)+=2; \
00325 } else { \
00326 (c)=(((c)&7)<<18)|(((s)[i]&0x3f)<<12)|(((s)[(i)+1]&0x3f)<<6)|((s)[(i)+2]&0x3f); \
00327 (i)+=3; \
00328 } \
00329 } \
00330 } UPRV_BLOCK_MACRO_END
00331
00352 #define U8_NEXT(s, i, length, c) U8_INTERNAL_NEXT_OR_SUB(s, i, length, c, U_SENTINEL)
00353
00378 #define U8_NEXT_OR_FFFD(s, i, length, c) U8_INTERNAL_NEXT_OR_SUB(s, i, length, c, 0xfffd)
00379
00381 #define U8_INTERNAL_NEXT_OR_SUB(s, i, length, c, sub) UPRV_BLOCK_MACRO_BEGIN { \
00382 (c)=(uint8_t)(s)[(i)++]; \
00383 if(!U8_IS_SINGLE(c)) { \
00384 uint8_t __t = 0; \
00385 if((i)!=(length) && \
00386 \
00387 ((c)>=0xe0 ? \
00388 ((c)<0xf0 ? \
00389 U8_LEAD3_T1_BITS[(c)&=0xf]&(1<<((__t=(s)[i])>>5)) && \
00390 (__t&=0x3f, 1) \
00391 : \
00392 ((c)-=0xf0)<=4 && \
00393 U8_LEAD4_T1_BITS[(__t=(s)[i])>>4]&(1<<(c)) && \
00394 ((c)=((c)<<6)|(__t&0x3f), ++(i)!=(length)) && \
00395 (__t=(s)[i]-0x80)<=0x3f) && \
00396 \
00397 ((c)=((c)<<6)|__t, ++(i)!=(length)) \
00398 : \
00399 (c)>=0xc2 && ((c)&=0x1f, 1)) && \
00400 \
00401 (__t=(s)[i]-0x80)<=0x3f && \
00402 ((c)=((c)<<6)|__t, ++(i), 1)) { \
00403 } else { \
00404 (c)=(sub); \
00405 } \
00406 } \
00407 } UPRV_BLOCK_MACRO_END
00408
00422 #define U8_APPEND_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
00423 uint32_t __uc=(c); \
00424 if(__uc<=0x7f) { \
00425 (s)[(i)++]=(uint8_t)__uc; \
00426 } else { \
00427 if(__uc<=0x7ff) { \
00428 (s)[(i)++]=(uint8_t)((__uc>>6)|0xc0); \
00429 } else { \
00430 if(__uc<=0xffff) { \
00431 (s)[(i)++]=(uint8_t)((__uc>>12)|0xe0); \
00432 } else { \
00433 (s)[(i)++]=(uint8_t)((__uc>>18)|0xf0); \
00434 (s)[(i)++]=(uint8_t)(((__uc>>12)&0x3f)|0x80); \
00435 } \
00436 (s)[(i)++]=(uint8_t)(((__uc>>6)&0x3f)|0x80); \
00437 } \
00438 (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \
00439 } \
00440 } UPRV_BLOCK_MACRO_END
00441
00459 #define U8_APPEND(s, i, capacity, c, isError) UPRV_BLOCK_MACRO_BEGIN { \
00460 uint32_t __uc=(c); \
00461 if(__uc<=0x7f) { \
00462 (s)[(i)++]=(uint8_t)__uc; \
00463 } else if(__uc<=0x7ff && (i)+1<(capacity)) { \
00464 (s)[(i)++]=(uint8_t)((__uc>>6)|0xc0); \
00465 (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \
00466 } else if((__uc<=0xd7ff || (0xe000<=__uc && __uc<=0xffff)) && (i)+2<(capacity)) { \
00467 (s)[(i)++]=(uint8_t)((__uc>>12)|0xe0); \
00468 (s)[(i)++]=(uint8_t)(((__uc>>6)&0x3f)|0x80); \
00469 (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \
00470 } else if(0xffff<__uc && __uc<=0x10ffff && (i)+3<(capacity)) { \
00471 (s)[(i)++]=(uint8_t)((__uc>>18)|0xf0); \
00472 (s)[(i)++]=(uint8_t)(((__uc>>12)&0x3f)|0x80); \
00473 (s)[(i)++]=(uint8_t)(((__uc>>6)&0x3f)|0x80); \
00474 (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \
00475 } else { \
00476 (isError)=true; \
00477 } \
00478 } UPRV_BLOCK_MACRO_END
00479
00490 #define U8_FWD_1_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
00491 (i)+=1+U8_COUNT_TRAIL_BYTES_UNSAFE((s)[i]); \
00492 } UPRV_BLOCK_MACRO_END
00493
00507 #define U8_FWD_1(s, i, length) UPRV_BLOCK_MACRO_BEGIN { \
00508 uint8_t __b=(s)[(i)++]; \
00509 if(U8_IS_LEAD(__b) && (i)!=(length)) { \
00510 uint8_t __t1=(s)[i]; \
00511 if((0xe0<=__b && __b<0xf0)) { \
00512 if(U8_IS_VALID_LEAD3_AND_T1(__b, __t1) && \
00513 ++(i)!=(length) && U8_IS_TRAIL((s)[i])) { \
00514 ++(i); \
00515 } \
00516 } else if(__b<0xe0) { \
00517 if(U8_IS_TRAIL(__t1)) { \
00518 ++(i); \
00519 } \
00520 } else { \
00521 if(U8_IS_VALID_LEAD4_AND_T1(__b, __t1) && \
00522 ++(i)!=(length) && U8_IS_TRAIL((s)[i]) && \
00523 ++(i)!=(length) && U8_IS_TRAIL((s)[i])) { \
00524 ++(i); \
00525 } \
00526 } \
00527 } \
00528 } UPRV_BLOCK_MACRO_END
00529
00542 #define U8_FWD_N_UNSAFE(s, i, n) UPRV_BLOCK_MACRO_BEGIN { \
00543 int32_t __N=(n); \
00544 while(__N>0) { \
00545 U8_FWD_1_UNSAFE(s, i); \
00546 --__N; \
00547 } \
00548 } UPRV_BLOCK_MACRO_END
00549
00565 #define U8_FWD_N(s, i, length, n) UPRV_BLOCK_MACRO_BEGIN { \
00566 int32_t __N=(n); \
00567 while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
00568 U8_FWD_1(s, i, length); \
00569 --__N; \
00570 } \
00571 } UPRV_BLOCK_MACRO_END
00572
00586 #define U8_SET_CP_START_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
00587 while(U8_IS_TRAIL((s)[i])) { --(i); } \
00588 } UPRV_BLOCK_MACRO_END
00589
00607 #define U8_SET_CP_START(s, start, i) UPRV_BLOCK_MACRO_BEGIN { \
00608 if(U8_IS_TRAIL((s)[(i)])) { \
00609 (i)=utf8_back1SafeBody(s, start, (i)); \
00610 } \
00611 } UPRV_BLOCK_MACRO_END
00612
00639 #define U8_TRUNCATE_IF_INCOMPLETE(s, start, length) UPRV_BLOCK_MACRO_BEGIN { \
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 } UPRV_BLOCK_MACRO_END
00662
00663
00664
00684 #define U8_PREV_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
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 } UPRV_BLOCK_MACRO_END
00705
00726 #define U8_PREV(s, start, i, c) UPRV_BLOCK_MACRO_BEGIN { \
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 } UPRV_BLOCK_MACRO_END
00732
00757 #define U8_PREV_OR_FFFD(s, start, i, c) UPRV_BLOCK_MACRO_BEGIN { \
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 } UPRV_BLOCK_MACRO_END
00763
00775 #define U8_BACK_1_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
00776 while(U8_IS_TRAIL((s)[--(i)])) {} \
00777 } UPRV_BLOCK_MACRO_END
00778
00791 #define U8_BACK_1(s, start, i) UPRV_BLOCK_MACRO_BEGIN { \
00792 if(U8_IS_TRAIL((s)[--(i)])) { \
00793 (i)=utf8_back1SafeBody(s, start, (i)); \
00794 } \
00795 } UPRV_BLOCK_MACRO_END
00796
00810 #define U8_BACK_N_UNSAFE(s, i, n) UPRV_BLOCK_MACRO_BEGIN { \
00811 int32_t __N=(n); \
00812 while(__N>0) { \
00813 U8_BACK_1_UNSAFE(s, i); \
00814 --__N; \
00815 } \
00816 } UPRV_BLOCK_MACRO_END
00817
00832 #define U8_BACK_N(s, start, i, n) UPRV_BLOCK_MACRO_BEGIN { \
00833 int32_t __N=(n); \
00834 while(__N>0 && (i)>(start)) { \
00835 U8_BACK_1(s, start, i); \
00836 --__N; \
00837 } \
00838 } UPRV_BLOCK_MACRO_END
00839
00853 #define U8_SET_CP_LIMIT_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
00854 U8_BACK_1_UNSAFE(s, i); \
00855 U8_FWD_1_UNSAFE(s, i); \
00856 } UPRV_BLOCK_MACRO_END
00857
00875 #define U8_SET_CP_LIMIT(s, start, i, length) UPRV_BLOCK_MACRO_BEGIN { \
00876 if((start)<(i) && ((i)<(length) || (length)<0)) { \
00877 U8_BACK_1(s, start, i); \
00878 U8_FWD_1(s, i, length); \
00879 } \
00880 } UPRV_BLOCK_MACRO_END
00881
00882 #endif