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 uint8_t U_EXPORT2
00044 utf8_countTrailBytes[256];
00045 #else
00046 U_CFUNC uint8_t U_IMPORT
00047 utf8_countTrailBytes[256];
00048 #endif
00049
00050
00051
00052
00053
00054
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, 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
00074
00075
00076
00077
00078
00079
00080
00081
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
00087 #define UTF8_NEED_MULTIPLE_UCHAR(c) ((uint32_t)(c)>0x7f)
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
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
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
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
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 \
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 \
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
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 \
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