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

Generated at Fri Dec 15 12:12:39 2000 for ICU 1.7 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000