ICU 49.1.1
49.1.1
|
00001 /* 00002 ******************************************************************************* 00003 * 00004 * Copyright (C) 1999-2011, 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 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 /* internal definitions ----------------------------------------------------- */ 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 /* U_IMPORT2? */ /*U_IMPORT*/ 00059 #endif 00060 utf8_countTrailBytes[256]; 00061 00069 #define U8_COUNT_TRAIL_BYTES(leadByte) (utf8_countTrailBytes[(uint8_t)leadByte]) 00070 00078 #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1) 00079 00089 U_STABLE UChar32 U_EXPORT2 00090 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict); 00091 00101 U_STABLE int32_t U_EXPORT2 00102 utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError); 00103 00113 U_STABLE UChar32 U_EXPORT2 00114 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict); 00115 00125 U_STABLE int32_t U_EXPORT2 00126 utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i); 00127 00128 /* single-code point definitions -------------------------------------------- */ 00129 00136 #define U8_IS_SINGLE(c) (((c)&0x80)==0) 00137 00144 #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc0)<0x3e) 00145 00152 #define U8_IS_TRAIL(c) (((c)&0xc0)==0x80) 00153 00161 #define U8_LENGTH(c) \ 00162 ((uint32_t)(c)<=0x7f ? 1 : \ 00163 ((uint32_t)(c)<=0x7ff ? 2 : \ 00164 ((uint32_t)(c)<=0xd7ff ? 3 : \ 00165 ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \ 00166 ((uint32_t)(c)<=0xffff ? 3 : 4)\ 00167 ) \ 00168 ) \ 00169 ) \ 00170 ) 00171 00177 #define U8_MAX_LENGTH 4 00178 00195 #define U8_GET_UNSAFE(s, i, c) { \ 00196 int32_t _u8_get_unsafe_index=(int32_t)(i); \ 00197 U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \ 00198 U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \ 00199 } 00200 00219 #define U8_GET(s, start, i, length, c) { \ 00220 int32_t _u8_get_index=(int32_t)(i); \ 00221 U8_SET_CP_START(s, start, _u8_get_index); \ 00222 U8_NEXT(s, _u8_get_index, length, c); \ 00223 } 00224 00225 /* definitions with forward iteration --------------------------------------- */ 00226 00244 #define U8_NEXT_UNSAFE(s, i, c) { \ 00245 (c)=(uint8_t)(s)[(i)++]; \ 00246 if((uint8_t)((c)-0xc0)<0x35) { \ 00247 uint8_t __count=U8_COUNT_TRAIL_BYTES(c); \ 00248 U8_MASK_LEAD_BYTE(c, __count); \ 00249 switch(__count) { \ 00250 /* each following branch falls through to the next one */ \ 00251 case 3: \ 00252 (c)=((c)<<6)|((s)[(i)++]&0x3f); \ 00253 case 2: \ 00254 (c)=((c)<<6)|((s)[(i)++]&0x3f); \ 00255 case 1: \ 00256 (c)=((c)<<6)|((s)[(i)++]&0x3f); \ 00257 /* no other branches to optimize switch() */ \ 00258 break; \ 00259 } \ 00260 } \ 00261 } 00262 00281 #define U8_NEXT(s, i, length, c) { \ 00282 (c)=(uint8_t)(s)[(i)++]; \ 00283 if((c)>=0x80) { \ 00284 uint8_t __t1, __t2; \ 00285 if( /* handle U+1000..U+CFFF inline */ \ 00286 (0xe0<(c) && (c)<=0xec) && \ 00287 (((i)+1)<(length)) && \ 00288 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \ 00289 (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \ 00290 ) { \ 00291 /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \ 00292 (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \ 00293 (i)+=2; \ 00294 } else if( /* handle U+0080..U+07FF inline */ \ 00295 ((c)<0xe0 && (c)>=0xc2) && \ 00296 ((i)<(length)) && \ 00297 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \ 00298 ) { \ 00299 (c)=(UChar)((((c)&0x1f)<<6)|__t1); \ 00300 ++(i); \ 00301 } else if(U8_IS_LEAD(c)) { \ 00302 /* function call for "complicated" and error cases */ \ 00303 (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (int32_t)(length), c, -1); \ 00304 } else { \ 00305 (c)=U_SENTINEL; \ 00306 } \ 00307 } \ 00308 } 00309 00323 #define U8_APPEND_UNSAFE(s, i, c) { \ 00324 if((uint32_t)(c)<=0x7f) { \ 00325 (s)[(i)++]=(uint8_t)(c); \ 00326 } else { \ 00327 if((uint32_t)(c)<=0x7ff) { \ 00328 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \ 00329 } else { \ 00330 if((uint32_t)(c)<=0xffff) { \ 00331 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \ 00332 } else { \ 00333 (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \ 00334 (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \ 00335 } \ 00336 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \ 00337 } \ 00338 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \ 00339 } \ 00340 } 00341 00359 #define U8_APPEND(s, i, capacity, c, isError) { \ 00360 if((uint32_t)(c)<=0x7f) { \ 00361 (s)[(i)++]=(uint8_t)(c); \ 00362 } else if((uint32_t)(c)<=0x7ff && (i)+1<(capacity)) { \ 00363 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \ 00364 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \ 00365 } else if((uint32_t)(c)<=0xd7ff && (i)+2<(capacity)) { \ 00366 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \ 00367 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \ 00368 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \ 00369 } else { \ 00370 (i)=utf8_appendCharSafeBody(s, (int32_t)(i), (int32_t)(capacity), c, &(isError)); \ 00371 } \ 00372 } 00373 00384 #define U8_FWD_1_UNSAFE(s, i) { \ 00385 (i)+=1+U8_COUNT_TRAIL_BYTES((s)[i]); \ 00386 } 00387 00399 #define U8_FWD_1(s, i, length) { \ 00400 uint8_t __b=(uint8_t)(s)[(i)++]; \ 00401 if(U8_IS_LEAD(__b)) { \ 00402 uint8_t __count=U8_COUNT_TRAIL_BYTES(__b); \ 00403 if((i)+__count>(length)) { \ 00404 __count=(uint8_t)((length)-(i)); \ 00405 } \ 00406 while(__count>0 && U8_IS_TRAIL((s)[i])) { \ 00407 ++(i); \ 00408 --__count; \ 00409 } \ 00410 } \ 00411 } 00412 00425 #define U8_FWD_N_UNSAFE(s, i, n) { \ 00426 int32_t __N=(n); \ 00427 while(__N>0) { \ 00428 U8_FWD_1_UNSAFE(s, i); \ 00429 --__N; \ 00430 } \ 00431 } 00432 00446 #define U8_FWD_N(s, i, length, n) { \ 00447 int32_t __N=(n); \ 00448 while(__N>0 && (i)<(length)) { \ 00449 U8_FWD_1(s, i, length); \ 00450 --__N; \ 00451 } \ 00452 } 00453 00467 #define U8_SET_CP_START_UNSAFE(s, i) { \ 00468 while(U8_IS_TRAIL((s)[i])) { --(i); } \ 00469 } 00470 00485 #define U8_SET_CP_START(s, start, i) { \ 00486 if(U8_IS_TRAIL((s)[(i)])) { \ 00487 (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \ 00488 } \ 00489 } 00490 00491 /* definitions with backward iteration -------------------------------------- */ 00492 00512 #define U8_PREV_UNSAFE(s, i, c) { \ 00513 (c)=(uint8_t)(s)[--(i)]; \ 00514 if(U8_IS_TRAIL(c)) { \ 00515 uint8_t __b, __count=1, __shift=6; \ 00516 \ 00517 /* c is a trail byte */ \ 00518 (c)&=0x3f; \ 00519 for(;;) { \ 00520 __b=(uint8_t)(s)[--(i)]; \ 00521 if(__b>=0xc0) { \ 00522 U8_MASK_LEAD_BYTE(__b, __count); \ 00523 (c)|=(UChar32)__b<<__shift; \ 00524 break; \ 00525 } else { \ 00526 (c)|=(UChar32)(__b&0x3f)<<__shift; \ 00527 ++__count; \ 00528 __shift+=6; \ 00529 } \ 00530 } \ 00531 } \ 00532 } 00533 00554 #define U8_PREV(s, start, i, c) { \ 00555 (c)=(uint8_t)(s)[--(i)]; \ 00556 if((c)>=0x80) { \ 00557 if((c)<=0xbf) { \ 00558 (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \ 00559 } else { \ 00560 (c)=U_SENTINEL; \ 00561 } \ 00562 } \ 00563 } 00564 00576 #define U8_BACK_1_UNSAFE(s, i) { \ 00577 while(U8_IS_TRAIL((s)[--(i)])) {} \ 00578 } 00579 00592 #define U8_BACK_1(s, start, i) { \ 00593 if(U8_IS_TRAIL((s)[--(i)])) { \ 00594 (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \ 00595 } \ 00596 } 00597 00611 #define U8_BACK_N_UNSAFE(s, i, n) { \ 00612 int32_t __N=(n); \ 00613 while(__N>0) { \ 00614 U8_BACK_1_UNSAFE(s, i); \ 00615 --__N; \ 00616 } \ 00617 } 00618 00633 #define U8_BACK_N(s, start, i, n) { \ 00634 int32_t __N=(n); \ 00635 while(__N>0 && (i)>(start)) { \ 00636 U8_BACK_1(s, start, i); \ 00637 --__N; \ 00638 } \ 00639 } 00640 00654 #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \ 00655 U8_BACK_1_UNSAFE(s, i); \ 00656 U8_FWD_1_UNSAFE(s, i); \ 00657 } 00658 00674 #define U8_SET_CP_LIMIT(s, start, i, length) { \ 00675 if((start)<(i) && (i)<(length)) { \ 00676 U8_BACK_1(s, start, i); \ 00677 U8_FWD_1(s, i, length); \ 00678 } \ 00679 } 00680 00681 #endif