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 ((leadByte)<0xf0 ? \
00082 ((leadByte)>=0xc0)+((leadByte)>=0xe0) : \
00083 (leadByte)<0xfe ? 3+((leadByte)>=0xf8)+((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
00247 #define U8_GET(s, start, i, length, c) { \
00248 int32_t _u8_get_index=(int32_t)(i); \
00249 U8_SET_CP_START(s, start, _u8_get_index); \
00250 U8_NEXT(s, _u8_get_index, length, c); \
00251 }
00252
00253
00254
00272 #define U8_NEXT_UNSAFE(s, i, c) { \
00273 (c)=(uint8_t)(s)[(i)++]; \
00274 if((c)>=0x80) { \
00275 if((c)<0xe0) { \
00276 (c)=(((c)&0x1f)<<6)|((s)[(i)++]&0x3f); \
00277 } else if((c)<0xf0) { \
00278 \
00279 (c)=(UChar)(((c)<<12)|(((s)[i]&0x3f)<<6)|((s)[(i)+1]&0x3f)); \
00280 (i)+=2; \
00281 } else { \
00282 (c)=(((c)&7)<<18)|(((s)[i]&0x3f)<<12)|(((s)[(i)+1]&0x3f)<<6)|((s)[(i)+2]&0x3f); \
00283 (i)+=3; \
00284 } \
00285 } \
00286 }
00287
00306 #define U8_NEXT(s, i, length, c) { \
00307 (c)=(uint8_t)(s)[(i)++]; \
00308 if((c)>=0x80) { \
00309 uint8_t __t1, __t2; \
00310 if( \
00311 (0xe0<(c) && (c)<=0xec) && \
00312 (((i)+1)<(length)) && \
00313 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
00314 (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
00315 ) { \
00316 \
00317 (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
00318 (i)+=2; \
00319 } else if( \
00320 ((c)<0xe0 && (c)>=0xc2) && \
00321 ((i)<(length)) && \
00322 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
00323 ) { \
00324 (c)=(UChar)((((c)&0x1f)<<6)|__t1); \
00325 ++(i); \
00326 } else if(U8_IS_LEAD(c)) { \
00327 \
00328 (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (int32_t)(length), c, -1); \
00329 } else { \
00330 (c)=U_SENTINEL; \
00331 } \
00332 } \
00333 }
00334
00348 #define U8_APPEND_UNSAFE(s, i, c) { \
00349 if((uint32_t)(c)<=0x7f) { \
00350 (s)[(i)++]=(uint8_t)(c); \
00351 } else { \
00352 if((uint32_t)(c)<=0x7ff) { \
00353 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
00354 } else { \
00355 if((uint32_t)(c)<=0xffff) { \
00356 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
00357 } else { \
00358 (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
00359 (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
00360 } \
00361 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
00362 } \
00363 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
00364 } \
00365 }
00366
00384 #define U8_APPEND(s, i, capacity, c, isError) { \
00385 if((uint32_t)(c)<=0x7f) { \
00386 (s)[(i)++]=(uint8_t)(c); \
00387 } else if((uint32_t)(c)<=0x7ff && (i)+1<(capacity)) { \
00388 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
00389 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
00390 } else if((uint32_t)(c)<=0xd7ff && (i)+2<(capacity)) { \
00391 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
00392 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
00393 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
00394 } else { \
00395 (i)=utf8_appendCharSafeBody(s, (int32_t)(i), (int32_t)(capacity), c, &(isError)); \
00396 } \
00397 }
00398
00409 #define U8_FWD_1_UNSAFE(s, i) { \
00410 (i)+=1+U8_COUNT_TRAIL_BYTES_UNSAFE((uint8_t)(s)[i]); \
00411 }
00412
00424 #define U8_FWD_1(s, i, length) { \
00425 uint8_t __b=(uint8_t)(s)[(i)++]; \
00426 if(U8_IS_LEAD(__b)) { \
00427 uint8_t __count=U8_COUNT_TRAIL_BYTES(__b); \
00428 if((i)+__count>(length)) { \
00429 __count=(uint8_t)((length)-(i)); \
00430 } \
00431 while(__count>0 && U8_IS_TRAIL((s)[i])) { \
00432 ++(i); \
00433 --__count; \
00434 } \
00435 } \
00436 }
00437
00450 #define U8_FWD_N_UNSAFE(s, i, n) { \
00451 int32_t __N=(n); \
00452 while(__N>0) { \
00453 U8_FWD_1_UNSAFE(s, i); \
00454 --__N; \
00455 } \
00456 }
00457
00471 #define U8_FWD_N(s, i, length, n) { \
00472 int32_t __N=(n); \
00473 while(__N>0 && (i)<(length)) { \
00474 U8_FWD_1(s, i, length); \
00475 --__N; \
00476 } \
00477 }
00478
00492 #define U8_SET_CP_START_UNSAFE(s, i) { \
00493 while(U8_IS_TRAIL((s)[i])) { --(i); } \
00494 }
00495
00510 #define U8_SET_CP_START(s, start, i) { \
00511 if(U8_IS_TRAIL((s)[(i)])) { \
00512 (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
00513 } \
00514 }
00515
00516
00517
00537 #define U8_PREV_UNSAFE(s, i, c) { \
00538 (c)=(uint8_t)(s)[--(i)]; \
00539 if(U8_IS_TRAIL(c)) { \
00540 uint8_t __b, __count=1, __shift=6; \
00541 \
00542 \
00543 (c)&=0x3f; \
00544 for(;;) { \
00545 __b=(uint8_t)(s)[--(i)]; \
00546 if(__b>=0xc0) { \
00547 U8_MASK_LEAD_BYTE(__b, __count); \
00548 (c)|=(UChar32)__b<<__shift; \
00549 break; \
00550 } else { \
00551 (c)|=(UChar32)(__b&0x3f)<<__shift; \
00552 ++__count; \
00553 __shift+=6; \
00554 } \
00555 } \
00556 } \
00557 }
00558
00579 #define U8_PREV(s, start, i, c) { \
00580 (c)=(uint8_t)(s)[--(i)]; \
00581 if((c)>=0x80) { \
00582 if((c)<=0xbf) { \
00583 (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
00584 } else { \
00585 (c)=U_SENTINEL; \
00586 } \
00587 } \
00588 }
00589
00601 #define U8_BACK_1_UNSAFE(s, i) { \
00602 while(U8_IS_TRAIL((s)[--(i)])) {} \
00603 }
00604
00617 #define U8_BACK_1(s, start, i) { \
00618 if(U8_IS_TRAIL((s)[--(i)])) { \
00619 (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
00620 } \
00621 }
00622
00636 #define U8_BACK_N_UNSAFE(s, i, n) { \
00637 int32_t __N=(n); \
00638 while(__N>0) { \
00639 U8_BACK_1_UNSAFE(s, i); \
00640 --__N; \
00641 } \
00642 }
00643
00658 #define U8_BACK_N(s, start, i, n) { \
00659 int32_t __N=(n); \
00660 while(__N>0 && (i)>(start)) { \
00661 U8_BACK_1(s, start, i); \
00662 --__N; \
00663 } \
00664 }
00665
00679 #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \
00680 U8_BACK_1_UNSAFE(s, i); \
00681 U8_FWD_1_UNSAFE(s, i); \
00682 }
00683
00699 #define U8_SET_CP_LIMIT(s, start, i, length) { \
00700 if((start)<(i) && (i)<(length)) { \
00701 U8_BACK_1(s, start, i); \
00702 U8_FWD_1(s, i, length); \
00703 } \
00704 }
00705
00706 #endif