00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00029
00030 #ifndef __UTF_H__
00031 # include "unicode/utf.h"
00032 #endif
00033
00034 #ifndef __UTF8_H__
00035 #define __UTF8_H__
00036
00037
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_IMPORT
00044 utf8_countTrailBytes[256];
00045 #endif
00046
00047
00048
00049
00050
00051
00052 #define UTF8_COUNT_TRAIL_BYTES(leadByte) (utf8_countTrailBytes[(uint8_t)leadByte])
00053
00054
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
00071
00072
00073
00074
00075
00076
00077
00078
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
00084 #define UTF8_NEED_MULTIPLE_UCHAR(c) ((uint32_t)(c)>0x7f)
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
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
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
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
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 \
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 \
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
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 \
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