00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00032
00033 #ifndef __UTF_H__
00034 # include "unicode/utf.h"
00035 #endif
00036
00037 #ifndef __UTF8_H__
00038 #define __UTF8_H__
00039
00040
00041
00042 #ifdef U_UTF8_IMPL
00043 U_CAPI const uint8_t
00044 utf8_countTrailBytes[256];
00045 #else
00046 U_CFUNC U_IMPORT const uint8_t
00047 utf8_countTrailBytes[256];
00048 #endif
00049
00055 #define UTF8_COUNT_TRAIL_BYTES(leadByte) (utf8_countTrailBytes[(uint8_t)leadByte])
00056
00057
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, UBool *pIsError);
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
00074
00075
00076
00077
00078
00079
00080
00082 #define UTF8_IS_SINGLE(uchar) (((uchar)&0x80)==0)
00083
00084 #define UTF8_IS_LEAD(uchar) ((uint8_t)((uchar)-0xc0)<0x3e)
00085
00086 #define UTF8_IS_TRAIL(uchar) (((uchar)&0xc0)==0x80)
00087
00089 #define UTF8_NEED_MULTIPLE_UCHAR(c) ((uint32_t)(c)>0x7f)
00090
00102 #if 1
00103 # define UTF8_CHAR_LENGTH(c) \
00104 ((uint32_t)(c)<=0x7f ? 1 : \
00105 ((uint32_t)(c)<=0x7ff ? 2 : \
00106 ((uint32_t)((c)-0x10000)>0xfffff ? 3 : 4) \
00107 ) \
00108 )
00109 #else
00110 # define UTF8_CHAR_LENGTH(c) \
00111 ((uint32_t)(c)<=0x7f ? 1 : \
00112 ((uint32_t)(c)<=0x7ff ? 2 : \
00113 ((uint32_t)(c)<=0xffff ? 3 : \
00114 ((uint32_t)(c)<=0x10ffff ? 4 : \
00115 ((uint32_t)(c)<=0x3ffffff ? 5 : \
00116 ((uint32_t)(c)<=0x7fffffff ? 6 : 3) \
00117 ) \
00118 ) \
00119 ) \
00120 ) \
00121 )
00122 #endif
00123
00125 #define UTF8_MAX_CHAR_LENGTH 4
00126
00128 #define UTF8_ARRAY_SIZE(size) ((5*(size))/2)
00129
00130 #define UTF8_GET_CHAR_UNSAFE(s, i, c) { \
00131 int32_t __I=(int32_t)(i); \
00132 UTF8_SET_CHAR_START_UNSAFE(s, __I); \
00133 UTF8_NEXT_CHAR_UNSAFE(s, __I, c); \
00134 }
00135
00136 #define UTF8_GET_CHAR_SAFE(s, start, i, length, c, strict) { \
00137 int32_t __I=(int32_t)(i); \
00138 UTF8_SET_CHAR_START_SAFE(s, start, __I); \
00139 UTF8_NEXT_CHAR_SAFE(s, __I, length, c, strict); \
00140 }
00141
00142
00143
00154 #define UTF8_NEXT_CHAR_UNSAFE(s, i, c) { \
00155 (c)=(s)[(i)++]; \
00156 if((uint8_t)((c)-0xc0)<0x35) { \
00157 uint8_t __count=UTF8_COUNT_TRAIL_BYTES(c); \
00158 UTF8_MASK_LEAD_BYTE(c, __count); \
00159 switch(__count) { \
00160 \
00161 case 3: \
00162 (c)=((c)<<6)|((s)[(i)++]&0x3f); \
00163 case 2: \
00164 (c)=((c)<<6)|((s)[(i)++]&0x3f); \
00165 case 1: \
00166 (c)=((c)<<6)|((s)[(i)++]&0x3f); \
00167 \
00168 break; \
00169 } \
00170 } \
00171 }
00172
00173 #define UTF8_APPEND_CHAR_UNSAFE(s, i, c) { \
00174 if((uint32_t)(c)<=0x7f) { \
00175 (s)[(i)++]=(uint8_t)(c); \
00176 } else { \
00177 if((uint32_t)(c)<=0x7ff) { \
00178 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
00179 } else { \
00180 if((uint32_t)(c)<=0xffff) { \
00181 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
00182 } else { \
00183 (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
00184 (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
00185 } \
00186 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
00187 } \
00188 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
00189 } \
00190 }
00191
00192 #define UTF8_FWD_1_UNSAFE(s, i) { \
00193 (i)+=1+UTF8_COUNT_TRAIL_BYTES((s)[i]); \
00194 }
00195
00196 #define UTF8_FWD_N_UNSAFE(s, i, n) { \
00197 int32_t __N=(n); \
00198 while(__N>0) { \
00199 UTF8_FWD_1_UNSAFE(s, i); \
00200 --__N; \
00201 } \
00202 }
00203
00204 #define UTF8_SET_CHAR_START_UNSAFE(s, i) { \
00205 while(UTF8_IS_TRAIL((s)[i])) { --(i); } \
00206 }
00207
00208 #define UTF8_NEXT_CHAR_SAFE(s, i, length, c, strict) { \
00209 (c)=(s)[(i)++]; \
00210 if((c)>=0x80) { \
00211 if(UTF8_IS_LEAD(c)) { \
00212 (c)=utf8_nextCharSafeBody(s, &(i), (int32_t)(length), c, strict, NULL); \
00213 } else { \
00214 (c)=UTF8_ERROR_VALUE_1; \
00215 } \
00216 } \
00217 }
00218
00219 #define UTF8_APPEND_CHAR_SAFE(s, i, length, c) { \
00220 if((uint32_t)(c)<=0x7f) { \
00221 (s)[(i)++]=(uint8_t)(c); \
00222 } else { \
00223 (i)=utf8_appendCharSafeBody(s, (int32_t)(i), (int32_t)(length), c); \
00224 } \
00225 }
00226
00227 #define UTF8_FWD_1_SAFE(s, i, length) { \
00228 uint8_t __b=(s)[(i)++]; \
00229 if(UTF8_IS_LEAD(__b)) { \
00230 uint8_t __count=UTF8_COUNT_TRAIL_BYTES(__b); \
00231 if((i)+__count>(length)) { \
00232 __count=(uint8_t)((length)-(i)); \
00233 } \
00234 while(__count>0 && UTF8_IS_TRAIL((s)[i])) { \
00235 ++(i); \
00236 --__count; \
00237 } \
00238 } \
00239 }
00240
00241 #define UTF8_FWD_N_SAFE(s, i, length, n) { \
00242 int32_t __N=(n); \
00243 while(__N>0 && (i)<(length)) { \
00244 UTF8_FWD_1_SAFE(s, i, length); \
00245 --__N; \
00246 } \
00247 }
00248
00249 #define UTF8_SET_CHAR_START_SAFE(s, start, i) { \
00250 if(UTF8_IS_TRAIL((s)[(i)])) { \
00251 (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
00252 } \
00253 }
00254
00255
00256
00257 #define UTF8_PREV_CHAR_UNSAFE(s, i, c) { \
00258 (c)=(s)[--(i)]; \
00259 if(UTF8_IS_TRAIL(c)) { \
00260 uint8_t __b, __count=1, __shift=6; \
00261 \
00262 \
00263 (c)&=0x3f; \
00264 for(;;) { \
00265 __b=(s)[--(i)]; \
00266 if(__b>=0xc0) { \
00267 UTF8_MASK_LEAD_BYTE(__b, __count); \
00268 (c)|=(UChar32)__b<<__shift; \
00269 break; \
00270 } else { \
00271 (c)|=(UChar32)(__b&0x3f)<<__shift; \
00272 ++__count; \
00273 __shift+=6; \
00274 } \
00275 } \
00276 } \
00277 }
00278
00279 #define UTF8_BACK_1_UNSAFE(s, i) { \
00280 while(UTF8_IS_TRAIL((s)[--(i)])) {} \
00281 }
00282
00283 #define UTF8_BACK_N_UNSAFE(s, i, n) { \
00284 int32_t __N=(n); \
00285 while(__N>0) { \
00286 UTF8_BACK_1_UNSAFE(s, i); \
00287 --__N; \
00288 } \
00289 }
00290
00291 #define UTF8_SET_CHAR_LIMIT_UNSAFE(s, i) { \
00292 UTF8_BACK_1_UNSAFE(s, i); \
00293 UTF8_FWD_1_UNSAFE(s, i); \
00294 }
00295
00296 #define UTF8_PREV_CHAR_SAFE(s, start, i, c, strict) { \
00297 (c)=(s)[--(i)]; \
00298 if((c)>=0x80) { \
00299 if((c)<=0xbf) { \
00300 (c)=utf8_prevCharSafeBody(s, start, &(i), c, strict); \
00301 } else { \
00302 (c)=UTF8_ERROR_VALUE_1; \
00303 } \
00304 } \
00305 }
00306
00307 #define UTF8_BACK_1_SAFE(s, start, i) { \
00308 if(UTF8_IS_TRAIL((s)[--(i)])) { \
00309 (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
00310 } \
00311 }
00312
00313 #define UTF8_BACK_N_SAFE(s, start, i, n) { \
00314 int32_t __N=(n); \
00315 while(__N>0 && (i)>(start)) { \
00316 UTF8_BACK_1_SAFE(s, start, i); \
00317 --__N; \
00318 } \
00319 }
00320
00321
00322
00323
00324
00325
00326 #define UTF8_SET_CHAR_LIMIT_SAFE(s, start, i, length) { \
00327 if((start)<(i) && (i)<(length)) { \
00328 UTF8_BACK_1_SAFE(s, start, i); \
00329 UTF8_FWD_1_SAFE(s, i, length); \
00330 } \
00331 }
00332
00333 #endif