ICU 4.8.1.1
4.8.1.1
|
00001 /* 00002 ******************************************************************************* 00003 * 00004 * Copyright (C) 1999-2009, International Business Machines 00005 * Corporation and others. All Rights Reserved. 00006 * 00007 ******************************************************************************* 00008 * file name: utf8.h 00009 * encoding: US-ASCII 00010 * tab size: 8 (not used) 00011 * indentation:4 00012 * 00013 * created on: 1999sep13 00014 * created by: Markus W. Scherer 00015 */ 00016 00034 #ifndef __UTF8_H__ 00035 #define __UTF8_H__ 00036 00037 /* utf.h must be included first. */ 00038 #ifndef __UTF_H__ 00039 # include "unicode/utf.h" 00040 #endif 00041 00042 /* internal definitions ----------------------------------------------------- */ 00043 00055 #ifdef U_UTF8_IMPL 00056 U_EXPORT const uint8_t 00057 #elif defined(U_STATIC_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION) 00058 U_CFUNC const uint8_t 00059 #else 00060 U_CFUNC U_IMPORT const uint8_t /* U_IMPORT2? */ /*U_IMPORT*/ 00061 #endif 00062 utf8_countTrailBytes[256]; 00063 00071 #define U8_COUNT_TRAIL_BYTES(leadByte) (utf8_countTrailBytes[(uint8_t)leadByte]) 00072 00080 #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1) 00081 00091 U_STABLE UChar32 U_EXPORT2 00092 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict); 00093 00103 U_STABLE int32_t U_EXPORT2 00104 utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError); 00105 00115 U_STABLE UChar32 U_EXPORT2 00116 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict); 00117 00127 U_STABLE int32_t U_EXPORT2 00128 utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i); 00129 00130 /* single-code point definitions -------------------------------------------- */ 00131 00138 #define U8_IS_SINGLE(c) (((c)&0x80)==0) 00139 00146 #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc0)<0x3e) 00147 00154 #define U8_IS_TRAIL(c) (((c)&0xc0)==0x80) 00155 00163 #define U8_LENGTH(c) \ 00164 ((uint32_t)(c)<=0x7f ? 1 : \ 00165 ((uint32_t)(c)<=0x7ff ? 2 : \ 00166 ((uint32_t)(c)<=0xd7ff ? 3 : \ 00167 ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \ 00168 ((uint32_t)(c)<=0xffff ? 3 : 4)\ 00169 ) \ 00170 ) \ 00171 ) \ 00172 ) 00173 00179 #define U8_MAX_LENGTH 4 00180 00197 #define U8_GET_UNSAFE(s, i, c) { \ 00198 int32_t _u8_get_unsafe_index=(int32_t)(i); \ 00199 U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \ 00200 U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \ 00201 } 00202 00221 #define U8_GET(s, start, i, length, c) { \ 00222 int32_t _u8_get_index=(int32_t)(i); \ 00223 U8_SET_CP_START(s, start, _u8_get_index); \ 00224 U8_NEXT(s, _u8_get_index, length, c); \ 00225 } 00226 00227 /* definitions with forward iteration --------------------------------------- */ 00228 00246 #define U8_NEXT_UNSAFE(s, i, c) { \ 00247 (c)=(uint8_t)(s)[(i)++]; \ 00248 if((uint8_t)((c)-0xc0)<0x35) { \ 00249 uint8_t __count=U8_COUNT_TRAIL_BYTES(c); \ 00250 U8_MASK_LEAD_BYTE(c, __count); \ 00251 switch(__count) { \ 00252 /* each following branch falls through to the next one */ \ 00253 case 3: \ 00254 (c)=((c)<<6)|((s)[(i)++]&0x3f); \ 00255 case 2: \ 00256 (c)=((c)<<6)|((s)[(i)++]&0x3f); \ 00257 case 1: \ 00258 (c)=((c)<<6)|((s)[(i)++]&0x3f); \ 00259 /* no other branches to optimize switch() */ \ 00260 break; \ 00261 } \ 00262 } \ 00263 } 00264 00283 #define U8_NEXT(s, i, length, c) { \ 00284 (c)=(uint8_t)(s)[(i)++]; \ 00285 if((c)>=0x80) { \ 00286 uint8_t __t1, __t2; \ 00287 if( /* handle U+1000..U+CFFF inline */ \ 00288 (0xe0<(c) && (c)<=0xec) && \ 00289 (((i)+1)<(length)) && \ 00290 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \ 00291 (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \ 00292 ) { \ 00293 /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \ 00294 (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \ 00295 (i)+=2; \ 00296 } else if( /* handle U+0080..U+07FF inline */ \ 00297 ((c)<0xe0 && (c)>=0xc2) && \ 00298 ((i)<(length)) && \ 00299 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \ 00300 ) { \ 00301 (c)=(UChar)((((c)&0x1f)<<6)|__t1); \ 00302 ++(i); \ 00303 } else if(U8_IS_LEAD(c)) { \ 00304 /* function call for "complicated" and error cases */ \ 00305 (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (int32_t)(length), c, -1); \ 00306 } else { \ 00307 (c)=U_SENTINEL; \ 00308 } \ 00309 } \ 00310 } 00311 00325 #define U8_APPEND_UNSAFE(s, i, c) { \ 00326 if((uint32_t)(c)<=0x7f) { \ 00327 (s)[(i)++]=(uint8_t)(c); \ 00328 } else { \ 00329 if((uint32_t)(c)<=0x7ff) { \ 00330 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \ 00331 } else { \ 00332 if((uint32_t)(c)<=0xffff) { \ 00333 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \ 00334 } else { \ 00335 (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \ 00336 (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \ 00337 } \ 00338 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \ 00339 } \ 00340 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \ 00341 } \ 00342 } 00343 00361 #define U8_APPEND(s, i, capacity, c, isError) { \ 00362 if((uint32_t)(c)<=0x7f) { \ 00363 (s)[(i)++]=(uint8_t)(c); \ 00364 } else if((uint32_t)(c)<=0x7ff && (i)+1<(capacity)) { \ 00365 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \ 00366 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \ 00367 } else if((uint32_t)(c)<=0xd7ff && (i)+2<(capacity)) { \ 00368 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \ 00369 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \ 00370 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \ 00371 } else { \ 00372 (i)=utf8_appendCharSafeBody(s, (int32_t)(i), (int32_t)(capacity), c, &(isError)); \ 00373 } \ 00374 } 00375 00386 #define U8_FWD_1_UNSAFE(s, i) { \ 00387 (i)+=1+U8_COUNT_TRAIL_BYTES((s)[i]); \ 00388 } 00389 00401 #define U8_FWD_1(s, i, length) { \ 00402 uint8_t __b=(uint8_t)(s)[(i)++]; \ 00403 if(U8_IS_LEAD(__b)) { \ 00404 uint8_t __count=U8_COUNT_TRAIL_BYTES(__b); \ 00405 if((i)+__count>(length)) { \ 00406 __count=(uint8_t)((length)-(i)); \ 00407 } \ 00408 while(__count>0 && U8_IS_TRAIL((s)[i])) { \ 00409 ++(i); \ 00410 --__count; \ 00411 } \ 00412 } \ 00413 } 00414 00427 #define U8_FWD_N_UNSAFE(s, i, n) { \ 00428 int32_t __N=(n); \ 00429 while(__N>0) { \ 00430 U8_FWD_1_UNSAFE(s, i); \ 00431 --__N; \ 00432 } \ 00433 } 00434 00448 #define U8_FWD_N(s, i, length, n) { \ 00449 int32_t __N=(n); \ 00450 while(__N>0 && (i)<(length)) { \ 00451 U8_FWD_1(s, i, length); \ 00452 --__N; \ 00453 } \ 00454 } 00455 00469 #define U8_SET_CP_START_UNSAFE(s, i) { \ 00470 while(U8_IS_TRAIL((s)[i])) { --(i); } \ 00471 } 00472 00487 #define U8_SET_CP_START(s, start, i) { \ 00488 if(U8_IS_TRAIL((s)[(i)])) { \ 00489 (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \ 00490 } \ 00491 } 00492 00493 /* definitions with backward iteration -------------------------------------- */ 00494 00514 #define U8_PREV_UNSAFE(s, i, c) { \ 00515 (c)=(uint8_t)(s)[--(i)]; \ 00516 if(U8_IS_TRAIL(c)) { \ 00517 uint8_t __b, __count=1, __shift=6; \ 00518 \ 00519 /* c is a trail byte */ \ 00520 (c)&=0x3f; \ 00521 for(;;) { \ 00522 __b=(uint8_t)(s)[--(i)]; \ 00523 if(__b>=0xc0) { \ 00524 U8_MASK_LEAD_BYTE(__b, __count); \ 00525 (c)|=(UChar32)__b<<__shift; \ 00526 break; \ 00527 } else { \ 00528 (c)|=(UChar32)(__b&0x3f)<<__shift; \ 00529 ++__count; \ 00530 __shift+=6; \ 00531 } \ 00532 } \ 00533 } \ 00534 } 00535 00556 #define U8_PREV(s, start, i, c) { \ 00557 (c)=(uint8_t)(s)[--(i)]; \ 00558 if((c)>=0x80) { \ 00559 if((c)<=0xbf) { \ 00560 (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \ 00561 } else { \ 00562 (c)=U_SENTINEL; \ 00563 } \ 00564 } \ 00565 } 00566 00578 #define U8_BACK_1_UNSAFE(s, i) { \ 00579 while(U8_IS_TRAIL((s)[--(i)])) {} \ 00580 } 00581 00594 #define U8_BACK_1(s, start, i) { \ 00595 if(U8_IS_TRAIL((s)[--(i)])) { \ 00596 (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \ 00597 } \ 00598 } 00599 00613 #define U8_BACK_N_UNSAFE(s, i, n) { \ 00614 int32_t __N=(n); \ 00615 while(__N>0) { \ 00616 U8_BACK_1_UNSAFE(s, i); \ 00617 --__N; \ 00618 } \ 00619 } 00620 00635 #define U8_BACK_N(s, start, i, n) { \ 00636 int32_t __N=(n); \ 00637 while(__N>0 && (i)>(start)) { \ 00638 U8_BACK_1(s, start, i); \ 00639 --__N; \ 00640 } \ 00641 } 00642 00656 #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \ 00657 U8_BACK_1_UNSAFE(s, i); \ 00658 U8_FWD_1_UNSAFE(s, i); \ 00659 } 00660 00676 #define U8_SET_CP_LIMIT(s, start, i, length) { \ 00677 if((start)<(i) && (i)<(length)) { \ 00678 U8_BACK_1(s, start, i); \ 00679 U8_FWD_1(s, i, length); \ 00680 } \ 00681 } 00682 00683 #endif