ICU 51.2  51.2
utf8.h
Go to the documentation of this file.
00001 /*
00002 *******************************************************************************
00003 *
00004 *   Copyright (C) 1999-2013, 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 
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 /* single-code point definitions -------------------------------------------- */
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 
00250 #define U8_GET(s, start, i, length, c) { \
00251     int32_t _u8_get_index=(i); \
00252     U8_SET_CP_START(s, start, _u8_get_index); \
00253     U8_NEXT(s, _u8_get_index, length, c); \
00254 }
00255 
00256 #ifndef U_HIDE_DRAFT_API
00257 
00282 #define U8_GET_OR_FFFD(s, start, i, length, c) { \
00283     int32_t _u8_get_index=(i); \
00284     U8_SET_CP_START(s, start, _u8_get_index); \
00285     U8_NEXT_OR_FFFD(s, _u8_get_index, length, c); \
00286 }
00287 #endif /* U_HIDE_DRAFT_API */
00288 
00289 /* definitions with forward iteration --------------------------------------- */
00290 
00308 #define U8_NEXT_UNSAFE(s, i, c) { \
00309     (c)=(uint8_t)(s)[(i)++]; \
00310     if((c)>=0x80) { \
00311         if((c)<0xe0) { \
00312             (c)=(((c)&0x1f)<<6)|((s)[(i)++]&0x3f); \
00313         } else if((c)<0xf0) { \
00314             /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
00315             (c)=(UChar)(((c)<<12)|(((s)[i]&0x3f)<<6)|((s)[(i)+1]&0x3f)); \
00316             (i)+=2; \
00317         } else { \
00318             (c)=(((c)&7)<<18)|(((s)[i]&0x3f)<<12)|(((s)[(i)+1]&0x3f)<<6)|((s)[(i)+2]&0x3f); \
00319             (i)+=3; \
00320         } \
00321     } \
00322 }
00323 
00344 #define U8_NEXT(s, i, length, c) { \
00345     (c)=(uint8_t)(s)[(i)++]; \
00346     if((c)>=0x80) { \
00347         uint8_t __t1, __t2; \
00348         if( /* handle U+1000..U+CFFF inline */ \
00349             (0xe0<(c) && (c)<=0xec) && \
00350             (((i)+1)<(length) || (length)<0) && \
00351             (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
00352             (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
00353         ) { \
00354             /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
00355             (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
00356             (i)+=2; \
00357         } else if( /* handle U+0080..U+07FF inline */ \
00358             ((c)<0xe0 && (c)>=0xc2) && \
00359             ((i)!=(length)) && \
00360             (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
00361         ) { \
00362             (c)=(((c)&0x1f)<<6)|__t1; \
00363             ++(i); \
00364         } else { \
00365             /* function call for "complicated" and error cases */ \
00366             (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (length), c, -1); \
00367         } \
00368     } \
00369 }
00370 
00371 #ifndef U_HIDE_DRAFT_API
00372 
00396 #define U8_NEXT_OR_FFFD(s, i, length, c) { \
00397     (c)=(uint8_t)(s)[(i)++]; \
00398     if((c)>=0x80) { \
00399         uint8_t __t1, __t2; \
00400         if( /* handle U+1000..U+CFFF inline */ \
00401             (0xe0<(c) && (c)<=0xec) && \
00402             (((i)+1)<(length) || (length)<0) && \
00403             (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
00404             (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
00405         ) { \
00406             /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
00407             (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
00408             (i)+=2; \
00409         } else if( /* handle U+0080..U+07FF inline */ \
00410             ((c)<0xe0 && (c)>=0xc2) && \
00411             ((i)!=(length)) && \
00412             (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
00413         ) { \
00414             (c)=(((c)&0x1f)<<6)|__t1; \
00415             ++(i); \
00416         } else { \
00417             /* function call for "complicated" and error cases */ \
00418             (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (length), c, -3); \
00419         } \
00420     } \
00421 }
00422 #endif /* U_HIDE_DRAFT_API */
00423 
00437 #define U8_APPEND_UNSAFE(s, i, c) { \
00438     if((uint32_t)(c)<=0x7f) { \
00439         (s)[(i)++]=(uint8_t)(c); \
00440     } else { \
00441         if((uint32_t)(c)<=0x7ff) { \
00442             (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
00443         } else { \
00444             if((uint32_t)(c)<=0xffff) { \
00445                 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
00446             } else { \
00447                 (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
00448                 (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
00449             } \
00450             (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
00451         } \
00452         (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
00453     } \
00454 }
00455 
00473 #define U8_APPEND(s, i, capacity, c, isError) { \
00474     if((uint32_t)(c)<=0x7f) { \
00475         (s)[(i)++]=(uint8_t)(c); \
00476     } else if((uint32_t)(c)<=0x7ff && (i)+1<(capacity)) { \
00477         (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
00478         (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
00479     } else if((uint32_t)(c)<=0xd7ff && (i)+2<(capacity)) { \
00480         (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
00481         (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
00482         (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
00483     } else { \
00484         (i)=utf8_appendCharSafeBody(s, (i), (capacity), c, &(isError)); \
00485     } \
00486 }
00487 
00498 #define U8_FWD_1_UNSAFE(s, i) { \
00499     (i)+=1+U8_COUNT_TRAIL_BYTES_UNSAFE((uint8_t)(s)[i]); \
00500 }
00501 
00515 #define U8_FWD_1(s, i, length) { \
00516     uint8_t __b=(uint8_t)(s)[(i)++]; \
00517     if(U8_IS_LEAD(__b)) { \
00518         uint8_t __count=U8_COUNT_TRAIL_BYTES(__b); \
00519         if((i)+__count>(length) && (length)>=0) { \
00520             __count=(uint8_t)((length)-(i)); \
00521         } \
00522         while(__count>0 && U8_IS_TRAIL((s)[i])) { \
00523             ++(i); \
00524             --__count; \
00525         } \
00526     } \
00527 }
00528 
00541 #define U8_FWD_N_UNSAFE(s, i, n) { \
00542     int32_t __N=(n); \
00543     while(__N>0) { \
00544         U8_FWD_1_UNSAFE(s, i); \
00545         --__N; \
00546     } \
00547 }
00548 
00564 #define U8_FWD_N(s, i, length, n) { \
00565     int32_t __N=(n); \
00566     while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
00567         U8_FWD_1(s, i, length); \
00568         --__N; \
00569     } \
00570 }
00571 
00585 #define U8_SET_CP_START_UNSAFE(s, i) { \
00586     while(U8_IS_TRAIL((s)[i])) { --(i); } \
00587 }
00588 
00603 #define U8_SET_CP_START(s, start, i) { \
00604     if(U8_IS_TRAIL((s)[(i)])) { \
00605         (i)=utf8_back1SafeBody(s, start, (i)); \
00606     } \
00607 }
00608 
00609 /* definitions with backward iteration -------------------------------------- */
00610 
00630 #define U8_PREV_UNSAFE(s, i, c) { \
00631     (c)=(uint8_t)(s)[--(i)]; \
00632     if(U8_IS_TRAIL(c)) { \
00633         uint8_t __b, __count=1, __shift=6; \
00634 \
00635         /* c is a trail byte */ \
00636         (c)&=0x3f; \
00637         for(;;) { \
00638             __b=(uint8_t)(s)[--(i)]; \
00639             if(__b>=0xc0) { \
00640                 U8_MASK_LEAD_BYTE(__b, __count); \
00641                 (c)|=(UChar32)__b<<__shift; \
00642                 break; \
00643             } else { \
00644                 (c)|=(UChar32)(__b&0x3f)<<__shift; \
00645                 ++__count; \
00646                 __shift+=6; \
00647             } \
00648         } \
00649     } \
00650 }
00651 
00672 #define U8_PREV(s, start, i, c) { \
00673     (c)=(uint8_t)(s)[--(i)]; \
00674     if((c)>=0x80) { \
00675         (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
00676     } \
00677 }
00678 
00679 #ifndef U_HIDE_DRAFT_API
00680 
00704 #define U8_PREV_OR_FFFD(s, start, i, c) { \
00705     (c)=(uint8_t)(s)[--(i)]; \
00706     if((c)>=0x80) { \
00707         (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -3); \
00708     } \
00709 }
00710 #endif /* U_HIDE_DRAFT_API */
00711 
00723 #define U8_BACK_1_UNSAFE(s, i) { \
00724     while(U8_IS_TRAIL((s)[--(i)])) {} \
00725 }
00726 
00739 #define U8_BACK_1(s, start, i) { \
00740     if(U8_IS_TRAIL((s)[--(i)])) { \
00741         (i)=utf8_back1SafeBody(s, start, (i)); \
00742     } \
00743 }
00744 
00758 #define U8_BACK_N_UNSAFE(s, i, n) { \
00759     int32_t __N=(n); \
00760     while(__N>0) { \
00761         U8_BACK_1_UNSAFE(s, i); \
00762         --__N; \
00763     } \
00764 }
00765 
00780 #define U8_BACK_N(s, start, i, n) { \
00781     int32_t __N=(n); \
00782     while(__N>0 && (i)>(start)) { \
00783         U8_BACK_1(s, start, i); \
00784         --__N; \
00785     } \
00786 }
00787 
00801 #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \
00802     U8_BACK_1_UNSAFE(s, i); \
00803     U8_FWD_1_UNSAFE(s, i); \
00804 }
00805 
00823 #define U8_SET_CP_LIMIT(s, start, i, length) { \
00824     if((start)<(i) && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
00825         U8_BACK_1(s, start, i); \
00826         U8_FWD_1(s, i, length); \
00827     } \
00828 }
00829 
00830 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines