Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

utf8.h

Go to the documentation of this file.
00001 /*
00002 *******************************************************************************
00003 *
00004 *   Copyright (C) 1999-2001, 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 /* utf.h must be included first. */
00033 #ifndef __UTF_H__
00034 # include "unicode/utf.h"
00035 #endif
00036 
00037 #ifndef __UTF8_H__
00038 #define __UTF8_H__
00039 
00040 /* internal definitions ----------------------------------------------------- */
00041 
00042 #ifdef U_UTF8_IMPL
00043 U_CAPI const uint8_t 
00044 utf8_countTrailBytes[256];
00045 #else
00046 U_CFUNC const uint8_t /* U_IMPORT2? */ U_IMPORT
00047 utf8_countTrailBytes[256];
00048 #endif
00049 
00050 /*
00051  * Count the trail bytes for a lead byte -
00052  * this macro should be used so that the assembler code
00053  * that is mentioned in utf_impl.c could be used here.
00054  */
00055 #define UTF8_COUNT_TRAIL_BYTES(leadByte) (utf8_countTrailBytes[(uint8_t)leadByte])
00056 
00057 /* use a macro here, too - there may be a simpler way with some machines */
00058 #define UTF8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
00059 
00060 U_CAPI UChar32 U_EXPORT2
00061 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict);
00062 
00063 U_CAPI int32_t U_EXPORT2
00064 utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c);
00065 
00066 U_CAPI UChar32 U_EXPORT2
00067 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict);
00068 
00069 U_CAPI int32_t U_EXPORT2
00070 utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i);
00071 
00072 /*
00073  * For the semantics of all of these macros, see utf16.h.
00074  * The UTF-8 macros favor sequences more the shorter they are.
00075  * Sometimes, only the single-byte case is covered by a macro,
00076  * while longer sequences are handled by a function call.
00077  */
00078 
00079 /* single-code point definitions -------------------------------------------- */
00080 
00081 /* classes of code unit values */
00082 #define UTF8_IS_SINGLE(uchar) (((uchar)&0x80)==0)
00083 #define UTF8_IS_LEAD(uchar) ((uint8_t)((uchar)-0xc0)<0x3e)
00084 #define UTF8_IS_TRAIL(uchar) (((uchar)&0xc0)==0x80)
00085 
00086 /* number of code units per code point */
00087 #define UTF8_NEED_MULTIPLE_UCHAR(c) ((uint32_t)(c)>0x7f)
00088 
00089 /*
00090  * ICU does not deal with code points >0x10ffff
00091  * unless necessary for advancing in the byte stream.
00092  *
00093  * These length macros take into account that for values >0x10ffff
00094  * the "safe" append macros would write the error code point 0xffff
00095  * with 3 bytes.
00096  * Code point comparisons need to be in uint32_t because UChar32
00097  * may be a signed type, and negative values must be recognized.
00098  */
00099 #if 1
00100 #   define UTF8_CHAR_LENGTH(c) \
00101         ((uint32_t)(c)<=0x7f ? 1 : \
00102             ((uint32_t)(c)<=0x7ff ? 2 : \
00103                 ((uint32_t)((c)-0x10000)>0xfffff ? 3 : 4) \
00104             ) \
00105         )
00106 #else
00107 #   define UTF8_CHAR_LENGTH(c) \
00108         ((uint32_t)(c)<=0x7f ? 1 : \
00109             ((uint32_t)(c)<=0x7ff ? 2 : \
00110                 ((uint32_t)(c)<=0xffff ? 3 : \
00111                     ((uint32_t)(c)<=0x10ffff ? 4 : \
00112                         ((uint32_t)(c)<=0x3ffffff ? 5 : \
00113                             ((uint32_t)(c)<=0x7fffffff ? 6 : 3) \
00114                         ) \
00115                     ) \
00116                 ) \
00117             ) \
00118         )
00119 #endif
00120 
00121 #define UTF8_MAX_CHAR_LENGTH 4
00122 
00123 /* average number of code units compared to UTF-16 */
00124 #define UTF8_ARRAY_SIZE(size) ((5*(size))/2)
00125 
00126 #define UTF8_GET_CHAR_UNSAFE(s, i, c) { \
00127     int32_t __I=(int32_t)(i); \
00128     UTF8_SET_CHAR_START_UNSAFE(s, __I); \
00129     UTF8_NEXT_CHAR_UNSAFE(s, __I, c); \
00130 }
00131 
00132 #define UTF8_GET_CHAR_SAFE(s, start, i, length, c, strict) { \
00133     int32_t __I=(int32_t)(i); \
00134     UTF8_SET_CHAR_START_SAFE(s, start, __I); \
00135     UTF8_NEXT_CHAR_SAFE(s, __I, length, c, strict); \
00136 }
00137 
00138 /* definitions with forward iteration --------------------------------------- */
00139 
00140 /*
00141  * Read a Unicode scalar value from an array of UTF-8 bytes.
00142  * Only values <=0x10ffff are accepted, and if an error occurs,
00143  * then c will be set such that UTF_IS_ERROR(c).
00144  * The _UNSAFE macro is fast and does not check for errors.
00145  * The _SAFE macro checks for errors and optionally for
00146  * irregular sequences, too, i.e., for sequences that
00147  * are longer than necessary, such as <c0 80> instead of <0>.
00148  * The strict checks also check for non-characters.
00149  */
00150 #define UTF8_NEXT_CHAR_UNSAFE(s, i, c) { \
00151     (c)=(s)[(i)++]; \
00152     if((uint8_t)((c)-0xc0)<0x35) { \
00153         uint8_t __count=UTF8_COUNT_TRAIL_BYTES(c); \
00154         UTF8_MASK_LEAD_BYTE(c, __count); \
00155         switch(__count) { \
00156         /* each following branch falls through to the next one */ \
00157         case 3: \
00158             (c)=((c)<<6)|((s)[(i)++]&0x3f); \
00159         case 2: \
00160             (c)=((c)<<6)|((s)[(i)++]&0x3f); \
00161         case 1: \
00162             (c)=((c)<<6)|((s)[(i)++]&0x3f); \
00163         /* no other branches to optimize switch() */ \
00164             break; \
00165         } \
00166     } \
00167 }
00168 
00169 #define UTF8_APPEND_CHAR_UNSAFE(s, i, c) { \
00170     if((uint32_t)(c)<=0x7f) { \
00171         (s)[(i)++]=(uint8_t)(c); \
00172     } else { \
00173         if((uint32_t)(c)<=0x7ff) { \
00174             (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
00175         } else { \
00176             if((uint32_t)(c)<=0xffff) { \
00177                 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
00178             } else { \
00179                 (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
00180                 (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
00181             } \
00182             (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
00183         } \
00184         (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
00185     } \
00186 }
00187 
00188 #define UTF8_FWD_1_UNSAFE(s, i) { \
00189     (i)+=1+UTF8_COUNT_TRAIL_BYTES((s)[i]); \
00190 }
00191 
00192 #define UTF8_FWD_N_UNSAFE(s, i, n) { \
00193     int32_t __N=(n); \
00194     while(__N>0) { \
00195         UTF8_FWD_1_UNSAFE(s, i); \
00196         --__N; \
00197     } \
00198 }
00199 
00200 #define UTF8_SET_CHAR_START_UNSAFE(s, i) { \
00201     while(UTF8_IS_TRAIL((s)[i])) { --(i); } \
00202 }
00203 
00204 #define UTF8_NEXT_CHAR_SAFE(s, i, length, c, strict) { \
00205     (c)=(s)[(i)++]; \
00206     if((c)>=0x80) { \
00207         if(UTF8_IS_LEAD(c)) { \
00208             (c)=utf8_nextCharSafeBody(s, &(i), (int32_t)(length), c, strict); \
00209         } else { \
00210             (c)=UTF8_ERROR_VALUE_1; \
00211         } \
00212     } \
00213 }
00214 
00215 #define UTF8_APPEND_CHAR_SAFE(s, i, length, c) { \
00216     if((uint32_t)(c)<=0x7f) { \
00217         (s)[(i)++]=(uint8_t)(c); \
00218     } else { \
00219         (i)=utf8_appendCharSafeBody(s, (int32_t)(i), (int32_t)(length), c); \
00220     } \
00221 }
00222 
00223 #define UTF8_FWD_1_SAFE(s, i, length) { \
00224     uint8_t __b=(s)[(i)++]; \
00225     if(UTF8_IS_LEAD(__b)) { \
00226         uint8_t __count=UTF8_COUNT_TRAIL_BYTES(__b); \
00227         if((i)+__count>(length)) { \
00228             __count=(uint8_t)((length)-(i)); \
00229         } \
00230         while(__count>0 && UTF8_IS_TRAIL((s)[i])) { \
00231             ++(i); \
00232             --__count; \
00233         } \
00234     } \
00235 }
00236 
00237 #define UTF8_FWD_N_SAFE(s, i, length, n) { \
00238     int32_t __N=(n); \
00239     while(__N>0 && (i)<(length)) { \
00240         UTF8_FWD_1_SAFE(s, i, length); \
00241         --__N; \
00242     } \
00243 }
00244 
00245 #define UTF8_SET_CHAR_START_SAFE(s, start, i) { \
00246     if(UTF8_IS_TRAIL((s)[(i)])) { \
00247         (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
00248     } \
00249 }
00250 
00251 /* definitions with backward iteration -------------------------------------- */
00252 
00253 #define UTF8_PREV_CHAR_UNSAFE(s, i, c) { \
00254     (c)=(s)[--(i)]; \
00255     if(UTF8_IS_TRAIL(c)) { \
00256         uint8_t __b, __count=1, __shift=6; \
00257 \
00258         /* c is a trail byte */ \
00259         (c)&=0x3f; \
00260         for(;;) { \
00261             __b=(s)[--(i)]; \
00262             if(__b>=0xc0) { \
00263                 UTF8_MASK_LEAD_BYTE(__b, __count); \
00264                 (c)|=(UChar32)__b<<__shift; \
00265                 break; \
00266             } else { \
00267                 (c)|=(UChar32)(__b&0x3f)<<__shift; \
00268                 ++__count; \
00269                 __shift+=6; \
00270             } \
00271         } \
00272     } \
00273 }
00274 
00275 #define UTF8_BACK_1_UNSAFE(s, i) { \
00276     while(UTF8_IS_TRAIL((s)[--(i)])) {} \
00277 }
00278 
00279 #define UTF8_BACK_N_UNSAFE(s, i, n) { \
00280     int32_t __N=(n); \
00281     while(__N>0) { \
00282         UTF8_BACK_1_UNSAFE(s, i); \
00283         --__N; \
00284     } \
00285 }
00286 
00287 #define UTF8_SET_CHAR_LIMIT_UNSAFE(s, i) { \
00288     UTF8_BACK_1_UNSAFE(s, i); \
00289     UTF8_FWD_1_UNSAFE(s, i); \
00290 }
00291 
00292 #define UTF8_PREV_CHAR_SAFE(s, start, i, c, strict) { \
00293     (c)=(s)[--(i)]; \
00294     if((c)>=0x80) { \
00295         if((c)<=0xbf) { \
00296             (c)=utf8_prevCharSafeBody(s, start, &(i), c, strict); \
00297         } else { \
00298             (c)=UTF8_ERROR_VALUE_1; \
00299         } \
00300     } \
00301 }
00302 
00303 #define UTF8_BACK_1_SAFE(s, start, i) { \
00304     if(UTF8_IS_TRAIL((s)[--(i)])) { \
00305         (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
00306     } \
00307 }
00308 
00309 #define UTF8_BACK_N_SAFE(s, start, i, n) { \
00310     int32_t __N=(n); \
00311     while(__N>0 && (i)>(start)) { \
00312         UTF8_BACK_1_SAFE(s, start, i); \
00313         --__N; \
00314     } \
00315 }
00316 
00317 /*
00318  * Need to use UTF8_FWD_1_SAFE() because UTF8_BACK_1_SAFE()
00319  * may have started from the middle of the sequence and not checked
00320  * all trail bytes.
00321  */
00322 #define UTF8_SET_CHAR_LIMIT_SAFE(s, start, i, length) { \
00323     if((start)<(i) && (i)<(length)) { \
00324         UTF8_BACK_1_SAFE(s, start, i); \
00325         UTF8_FWD_1_SAFE(s, i, length); \
00326     } \
00327 }
00328 
00329 #endif

Generated on Sun May 22 18:49:35 2005 for ICU 2.1 by  doxygen 1.4.2