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) { \
00352 (c)=(uint8_t)(s)[(i)++]; \
00353 if(!U8_IS_SINGLE(c)) { \
00354 uint8_t __t1, __t2; \
00355 if( \
00356 (0xe0<=(c) && (c)<0xf0) && \
00357 (((i)+1)<(length) || (length)<0) && \
00358 U8_IS_VALID_LEAD3_AND_T1((c), __t1=(s)[i]) && \
00359 (__t2=(s)[(i)+1]-0x80)<=0x3f) { \
00360 (c)=(((c)&0xf)<<12)|((__t1&0x3f)<<6)|__t2; \
00361 (i)+=2; \
00362 } else if( \
00363 ((c)<0xe0 && (c)>=0xc2) && \
00364 ((i)!=(length)) && \
00365 (__t1=(s)[i]-0x80)<=0x3f) { \
00366 (c)=(((c)&0x1f)<<6)|__t1; \
00367 ++(i); \
00368 } else { \
00369 \
00370 (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (length), c, -1); \
00371 } \
00372 } \
00373 }
00374
00399 #define U8_NEXT_OR_FFFD(s, i, length, c) { \
00400 (c)=(uint8_t)(s)[(i)++]; \
00401 if(!U8_IS_SINGLE(c)) { \
00402 uint8_t __t1, __t2; \
00403 if( \
00404 (0xe0<=(c) && (c)<0xf0) && \
00405 (((i)+1)<(length) || (length)<0) && \
00406 U8_IS_VALID_LEAD3_AND_T1((c), __t1=(s)[i]) && \
00407 (__t2=(s)[(i)+1]-0x80)<=0x3f) { \
00408 (c)=(((c)&0xf)<<12)|((__t1&0x3f)<<6)|__t2; \
00409 (i)+=2; \
00410 } else if( \
00411 ((c)<0xe0 && (c)>=0xc2) && \
00412 ((i)!=(length)) && \
00413 (__t1=(s)[i]-0x80)<=0x3f) { \
00414 (c)=(((c)&0x1f)<<6)|__t1; \
00415 ++(i); \
00416 } else { \
00417 \
00418 (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (length), c, -3); \
00419 } \
00420 } \
00421 }
00422
00436 #define U8_APPEND_UNSAFE(s, i, c) { \
00437 if((uint32_t)(c)<=0x7f) { \
00438 (s)[(i)++]=(uint8_t)(c); \
00439 } else { \
00440 if((uint32_t)(c)<=0x7ff) { \
00441 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
00442 } else { \
00443 if((uint32_t)(c)<=0xffff) { \
00444 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
00445 } else { \
00446 (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
00447 (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
00448 } \
00449 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
00450 } \
00451 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
00452 } \
00453 }
00454
00472 #define U8_APPEND(s, i, capacity, c, isError) { \
00473 if((uint32_t)(c)<=0x7f) { \
00474 (s)[(i)++]=(uint8_t)(c); \
00475 } else if((uint32_t)(c)<=0x7ff && (i)+1<(capacity)) { \
00476 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
00477 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
00478 } else if((uint32_t)(c)<=0xd7ff && (i)+2<(capacity)) { \
00479 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
00480 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
00481 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
00482 } else { \
00483 (i)=utf8_appendCharSafeBody(s, (i), (capacity), c, &(isError)); \
00484 } \
00485 }
00486
00497 #define U8_FWD_1_UNSAFE(s, i) { \
00498 (i)+=1+U8_COUNT_TRAIL_BYTES_UNSAFE((s)[i]); \
00499 }
00500
00514 #define U8_FWD_1(s, i, length) { \
00515 uint8_t __b=(s)[(i)++]; \
00516 if(U8_IS_LEAD(__b) && (i)!=(length)) { \
00517 uint8_t __t1=(s)[i]; \
00518 if((0xe0<=__b && __b<0xf0)) { \
00519 if(U8_IS_VALID_LEAD3_AND_T1(__b, __t1) && \
00520 ++(i)!=(length) && U8_IS_TRAIL((s)[i])) { \
00521 ++(i); \
00522 } \
00523 } else if(__b<0xe0) { \
00524 if(U8_IS_TRAIL(__t1)) { \
00525 ++(i); \
00526 } \
00527 } else { \
00528 if(U8_IS_VALID_LEAD4_AND_T1(__b, __t1) && \
00529 ++(i)!=(length) && U8_IS_TRAIL((s)[i]) && \
00530 ++(i)!=(length) && U8_IS_TRAIL((s)[i])) { \
00531 ++(i); \
00532 } \
00533 } \
00534 } \
00535 }
00536
00549 #define U8_FWD_N_UNSAFE(s, i, n) { \
00550 int32_t __N=(n); \
00551 while(__N>0) { \
00552 U8_FWD_1_UNSAFE(s, i); \
00553 --__N; \
00554 } \
00555 }
00556
00572 #define U8_FWD_N(s, i, length, n) { \
00573 int32_t __N=(n); \
00574 while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
00575 U8_FWD_1(s, i, length); \
00576 --__N; \
00577 } \
00578 }
00579
00593 #define U8_SET_CP_START_UNSAFE(s, i) { \
00594 while(U8_IS_TRAIL((s)[i])) { --(i); } \
00595 }
00596
00611 #define U8_SET_CP_START(s, start, i) { \
00612 if(U8_IS_TRAIL((s)[(i)])) { \
00613 (i)=utf8_back1SafeBody(s, start, (i)); \
00614 } \
00615 }
00616
00617
00618
00638 #define U8_PREV_UNSAFE(s, i, c) { \
00639 (c)=(uint8_t)(s)[--(i)]; \
00640 if(U8_IS_TRAIL(c)) { \
00641 uint8_t __b, __count=1, __shift=6; \
00642 \
00643 \
00644 (c)&=0x3f; \
00645 for(;;) { \
00646 __b=(s)[--(i)]; \
00647 if(__b>=0xc0) { \
00648 U8_MASK_LEAD_BYTE(__b, __count); \
00649 (c)|=(UChar32)__b<<__shift; \
00650 break; \
00651 } else { \
00652 (c)|=(UChar32)(__b&0x3f)<<__shift; \
00653 ++__count; \
00654 __shift+=6; \
00655 } \
00656 } \
00657 } \
00658 }
00659
00680 #define U8_PREV(s, start, i, c) { \
00681 (c)=(uint8_t)(s)[--(i)]; \
00682 if(!U8_IS_SINGLE(c)) { \
00683 (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
00684 } \
00685 }
00686
00711 #define U8_PREV_OR_FFFD(s, start, i, c) { \
00712 (c)=(uint8_t)(s)[--(i)]; \
00713 if(!U8_IS_SINGLE(c)) { \
00714 (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -3); \
00715 } \
00716 }
00717
00729 #define U8_BACK_1_UNSAFE(s, i) { \
00730 while(U8_IS_TRAIL((s)[--(i)])) {} \
00731 }
00732
00745 #define U8_BACK_1(s, start, i) { \
00746 if(U8_IS_TRAIL((s)[--(i)])) { \
00747 (i)=utf8_back1SafeBody(s, start, (i)); \
00748 } \
00749 }
00750
00764 #define U8_BACK_N_UNSAFE(s, i, n) { \
00765 int32_t __N=(n); \
00766 while(__N>0) { \
00767 U8_BACK_1_UNSAFE(s, i); \
00768 --__N; \
00769 } \
00770 }
00771
00786 #define U8_BACK_N(s, start, i, n) { \
00787 int32_t __N=(n); \
00788 while(__N>0 && (i)>(start)) { \
00789 U8_BACK_1(s, start, i); \
00790 --__N; \
00791 } \
00792 }
00793
00807 #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \
00808 U8_BACK_1_UNSAFE(s, i); \
00809 U8_FWD_1_UNSAFE(s, i); \
00810 }
00811
00829 #define U8_SET_CP_LIMIT(s, start, i, length) { \
00830 if((start)<(i) && ((i)<(length) || (length)<0)) { \
00831 U8_BACK_1(s, start, i); \
00832 U8_FWD_1(s, i, length); \
00833 } \
00834 }
00835
00836 #endif