Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

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  uint8_t U_EXPORT2 
00044 utf8_countTrailBytes[256];
00045 #else
00046 U_CFUNC 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, UTextOffset *pi, UTextOffset length, UChar32 c, UBool strict);
00062 
00063 U_CAPI UTextOffset U_EXPORT2
00064 utf8_appendCharSafeBody(uint8_t *s, UTextOffset i, UTextOffset length, UChar32 c);
00065 
00066 U_CAPI UChar32 U_EXPORT2
00067 utf8_prevCharSafeBody(const uint8_t *s, UTextOffset start, UTextOffset *pi, UChar32 c, UBool strict);
00068 
00069 U_CAPI UTextOffset U_EXPORT2
00070 utf8_back1SafeBody(const uint8_t *s, UTextOffset start, UTextOffset 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     UTextOffset __I=(UTextOffset)(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     UTextOffset __I=(UTextOffset)(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 surrogates and
00149  * for 0xXXXXfffe and 0xXXXXffff.
00150  */
00151 #define UTF8_NEXT_CHAR_UNSAFE(s, i, c) { \
00152     (c)=(s)[(i)++]; \
00153     if((uint8_t)((c)-0xc0)<0x35) { \
00154         uint8_t __count=UTF8_COUNT_TRAIL_BYTES(c); \
00155         UTF8_MASK_LEAD_BYTE(c, __count); \
00156         switch(__count) { \
00157         /* each following branch falls through to the next one */ \
00158         case 3: \
00159             (c)=((c)<<6)|((s)[(i)++]&0x3f); \
00160         case 2: \
00161             (c)=((c)<<6)|((s)[(i)++]&0x3f); \
00162         case 1: \
00163             (c)=((c)<<6)|((s)[(i)++]&0x3f); \
00164         /* no other branches to optimize switch() */ \
00165             break; \
00166         } \
00167     } \
00168 }
00169 
00170 #define UTF8_APPEND_CHAR_UNSAFE(s, i, c) { \
00171     if((uint32_t)(c)<=0x7f) { \
00172         (s)[(i)++]=(uint8_t)(c); \
00173     } else { \
00174         if((uint32_t)(c)<=0x7ff) { \
00175             (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
00176         } else { \
00177             if((uint32_t)(c)<=0xffff) { \
00178                 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
00179             } else { \
00180                 (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
00181                 (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
00182             } \
00183             (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
00184         } \
00185         (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
00186     } \
00187 }
00188 
00189 #define UTF8_FWD_1_UNSAFE(s, i) { \
00190     (i)+=1+UTF8_COUNT_TRAIL_BYTES((s)[i]); \
00191 }
00192 
00193 #define UTF8_FWD_N_UNSAFE(s, i, n) { \
00194     UTextOffset __N=(n); \
00195     while(__N>0) { \
00196         UTF8_FWD_1_UNSAFE(s, i); \
00197         --__N; \
00198     } \
00199 }
00200 
00201 #define UTF8_SET_CHAR_START_UNSAFE(s, i) { \
00202     while(UTF8_IS_TRAIL((s)[i])) { --(i); } \
00203 }
00204 
00205 #define UTF8_NEXT_CHAR_SAFE(s, i, length, c, strict) { \
00206     (c)=(s)[(i)++]; \
00207     if(UTF8_IS_LEAD(c)) { \
00208         (c)=utf8_nextCharSafeBody(s, &(i), (UTextOffset)(length), c, strict); \
00209     } \
00210 }
00211 
00212 #define UTF8_APPEND_CHAR_SAFE(s, i, length, c) { \
00213     if((uint32_t)(c)<=0x7f) { \
00214         (s)[(i)++]=(uint8_t)(c); \
00215     } else { \
00216         (i)=utf8_appendCharSafeBody(s, (UTextOffset)(i), (UTextOffset)(length), c); \
00217     } \
00218 }
00219 
00220 #define UTF8_FWD_1_SAFE(s, i, length) { \
00221     uint8_t __b=(s)[(i)++]; \
00222     if(UTF8_IS_LEAD(__b)) { \
00223         uint8_t __count=UTF8_COUNT_TRAIL_BYTES(__b); \
00224         if((i)+__count>(length)) { \
00225             __count=(uint8_t)((length)-(i)); \
00226         } \
00227         while(__count>0 && UTF8_IS_TRAIL((s)[i])) { \
00228             ++(i); \
00229             --__count; \
00230         } \
00231     } \
00232 }
00233 
00234 #define UTF8_FWD_N_SAFE(s, i, length, n) { \
00235     UTextOffset __N=(n); \
00236     while(__N>0 && (i)<(length)) { \
00237         UTF8_FWD_1_SAFE(s, i, length); \
00238         --__N; \
00239     } \
00240 }
00241 
00242 #define UTF8_SET_CHAR_START_SAFE(s, start, i) { \
00243     if(UTF8_IS_TRAIL((s)[(i)])) { \
00244         (i)=utf8_back1SafeBody(s, start, (UTextOffset)(i)); \
00245     } \
00246 }
00247 
00248 /* definitions with backward iteration -------------------------------------- */
00249 
00250 #define UTF8_PREV_CHAR_UNSAFE(s, i, c) { \
00251     (c)=(s)[--(i)]; \
00252     if(UTF8_IS_TRAIL(c)) { \
00253         uint8_t __b, __count=1, __shift=6; \
00254 \
00255         /* c is a trail byte */ \
00256         (c)&=0x3f; \
00257         for(;;) { \
00258             __b=(s)[--(i)]; \
00259             if(__b>=0xc0) { \
00260                 UTF8_MASK_LEAD_BYTE(__b, __count); \
00261                 (c)|=(UChar32)__b<<__shift; \
00262                 break; \
00263             } else { \
00264                 (c)|=(UChar32)(__b&0x3f)<<__shift; \
00265                 ++__count; \
00266                 __shift+=6; \
00267             } \
00268         } \
00269     } \
00270 }
00271 
00272 #define UTF8_BACK_1_UNSAFE(s, i) { \
00273     while(UTF8_IS_TRAIL((s)[--(i)])) {} \
00274 }
00275 
00276 #define UTF8_BACK_N_UNSAFE(s, i, n) { \
00277     UTextOffset __N=(n); \
00278     while(__N>0) { \
00279         UTF8_BACK_1_UNSAFE(s, i); \
00280         --__N; \
00281     } \
00282 }
00283 
00284 #define UTF8_SET_CHAR_LIMIT_UNSAFE(s, i) { \
00285     UTF8_BACK_1_UNSAFE(s, i); \
00286     UTF8_FWD_1_UNSAFE(s, i); \
00287 }
00288 
00289 #define UTF8_PREV_CHAR_SAFE(s, start, i, c, strict) { \
00290     (c)=(s)[--(i)]; \
00291     if(UTF8_IS_TRAIL((c))) { \
00292         (c)=utf8_prevCharSafeBody(s, start, &(i), c, strict); \
00293     } \
00294 }
00295 
00296 #define UTF8_BACK_1_SAFE(s, start, i) { \
00297     if(UTF8_IS_TRAIL((s)[--(i)])) { \
00298         (i)=utf8_back1SafeBody(s, start, (UTextOffset)(i)); \
00299     } \
00300 }
00301 
00302 #define UTF8_BACK_N_SAFE(s, start, i, n) { \
00303     UTextOffset __N=(n); \
00304     while(__N>0 && (i)>(start)) { \
00305         UTF8_BACK_1_SAFE(s, start, i); \
00306         --__N; \
00307     } \
00308 }
00309 
00310 #define UTF8_SET_CHAR_LIMIT_SAFE(s, start, i, length) { \
00311     if((start)<(i) && (i)<(length)) { \
00312         UTF8_BACK_1_SAFE(s, start, i); \
00313         (i)+=1+UTF8_COUNT_TRAIL_BYTES((s)[i]); \
00314         if((i)>(length)) { \
00315             (i)=(length); \
00316         } \
00317     } \
00318 }
00319 
00320 #endif

Generated at Thu Mar 22 16:12:42 2001 for ICU 1.8 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000