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