00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00080 #ifndef __UTF_H__
00081 #define __UTF_H__
00082
00083
00084
00085
00086
00087
00088 #include <stddef.h>
00089 #include <limits.h>
00090 #include "unicode/umachine.h"
00091
00092
00093
00094 #ifndef UTF_SIZE
00095
00096 # define UTF_SIZE 16
00097 #endif
00098
00100 #define U_SIZEOF_UCHAR (UTF_SIZE>>3)
00101
00106 #ifndef U_HAVE_WCHAR_H
00107 # define U_HAVE_WCHAR_H 1
00108 #endif
00109
00110
00111 #if U_SIZEOF_WCHAR_T==0
00112 # undef U_SIZEOF_WCHAR_T
00113
00114 # define U_SIZEOF_WCHAR_T 4
00115 #endif
00116
00122 #if U_SIZEOF_WCHAR_T==4
00123 typedef wchar_t UChar32;
00124 #else
00125 typedef uint32_t UChar32;
00126 #endif
00127
00132 typedef int32_t UTextOffset;
00133
00134
00135 #if !defined(UTF_SAFE) && !defined(UTF_STRICT) && !defined(UTF_UNSAFE)
00136
00140 # define UTF_SAFE
00141 #endif
00142
00143
00144
00157 #define UTF8_ERROR_VALUE_1 0x15
00158 #define UTF8_ERROR_VALUE_2 0x9f
00159
00164 #define UTF_ERROR_VALUE 0xffff
00165
00166
00167
00169 #define UTF_IS_SURROGATE(uchar) (((uchar)&0xfffff800)==0xd800)
00170
00175 #define UTF_IS_UNICODE_CHAR(c) \
00176 ((uint32_t)(c)<=0x10ffff && \
00177 !UTF_IS_SURROGATE(c) && ((c)&0xfffe)!=0xfffe)
00178
00183 #define UTF_IS_ERROR(c) \
00184 (((c)&0xfffe)==0xfffe || (c)==UTF8_ERROR_VALUE_1 || (c)==UTF8_ERROR_VALUE_2)
00185
00187 #define UTF_IS_VALID(c) \
00188 ((uint32_t)(c)<=0x10ffff && \
00189 !UTF_IS_SURROGATE(c) && \
00190 ((c)&0xfffe)!=0xfffe && \
00191 (c)!=UTF8_ERROR_VALUE_1 && (c)!=UTF8_ERROR_VALUE_2)
00192
00193
00194
00195 #include "unicode/utf8.h"
00196 #include "unicode/utf16.h"
00197 #include "unicode/utf32.h"
00198
00199
00200
00207 #if UTF_SIZE==8
00208
00209 # error UTF-8 is not implemented, undefine UTF_SIZE or define it to 16
00210
00211
00212 # if CHAR_MAX>=255
00213 typedef char UChar;
00214 # else
00215 typedef uint8_t UChar;
00216 # endif
00217
00218 #elif UTF_SIZE==16
00219
00220
00221 # if U_SIZEOF_WCHAR_T==2
00222 typedef wchar_t UChar;
00223 # else
00224 typedef uint16_t UChar;
00225 # endif
00226
00228 # define UTF_IS_SINGLE(uchar) UTF16_IS_SINGLE(uchar)
00229
00230 # define UTF_IS_LEAD(uchar) UTF16_IS_LEAD(uchar)
00231
00232 # define UTF_IS_TRAIL(uchar) UTF16_IS_TRAIL(uchar)
00233
00235 # define UTF_NEED_MULTIPLE_UCHAR(c) UTF16_NEED_MULTIPLE_UCHAR(c)
00236
00237 # define UTF_CHAR_LENGTH(c) UTF16_CHAR_LENGTH(c)
00238
00239 # define UTF_MAX_CHAR_LENGTH UTF16_MAX_CHAR_LENGTH
00240
00241 # define UTF_ARRAY_SIZE(size) UTF16_ARRAY_SIZE(size)
00242
00244 # define UTF_GET_CHAR_UNSAFE(s, i, c) UTF16_GET_CHAR_UNSAFE(s, i, c)
00245
00246 # define UTF_GET_CHAR_SAFE(s, start, i, length, c, strict) UTF16_GET_CHAR_SAFE(s, start, i, length, c, strict)
00247
00249 # define UTF_NEXT_CHAR_UNSAFE(s, i, c) UTF16_NEXT_CHAR_UNSAFE(s, i, c)
00250
00251 # define UTF_NEXT_CHAR_SAFE(s, i, length, c, strict) UTF16_NEXT_CHAR_SAFE(s, i, length, c, strict)
00252
00254 # define UTF_APPEND_CHAR_UNSAFE(s, i, c) UTF16_APPEND_CHAR_UNSAFE(s, i, c)
00255
00256 # define UTF_APPEND_CHAR_SAFE(s, i, length, c) UTF16_APPEND_CHAR_SAFE(s, i, length, c)
00257
00259 # define UTF_FWD_1_UNSAFE(s, i) UTF16_FWD_1_UNSAFE(s, i)
00260
00261 # define UTF_FWD_1_SAFE(s, i, length) UTF16_FWD_1_SAFE(s, i, length)
00262
00264 # define UTF_FWD_N_UNSAFE(s, i, n) UTF16_FWD_N_UNSAFE(s, i, n)
00265
00266 # define UTF_FWD_N_SAFE(s, i, length, n) UTF16_FWD_N_SAFE(s, i, length, n)
00267
00269 # define UTF_SET_CHAR_START_UNSAFE(s, i) UTF16_SET_CHAR_START_UNSAFE(s, i)
00270
00271 # define UTF_SET_CHAR_START_SAFE(s, start, i) UTF16_SET_CHAR_START_SAFE(s, start, i)
00272
00274 # define UTF_PREV_CHAR_UNSAFE(s, i, c) UTF16_PREV_CHAR_UNSAFE(s, i, c)
00275
00276 # define UTF_PREV_CHAR_SAFE(s, start, i, c, strict) UTF16_PREV_CHAR_SAFE(s, start, i, c, strict)
00277
00279 # define UTF_BACK_1_UNSAFE(s, i) UTF16_BACK_1_UNSAFE(s, i)
00280
00281 # define UTF_BACK_1_SAFE(s, start, i) UTF16_BACK_1_SAFE(s, start, i)
00282
00284 # define UTF_BACK_N_UNSAFE(s, i, n) UTF16_BACK_N_UNSAFE(s, i, n)
00285
00286 # define UTF_BACK_N_SAFE(s, start, i, n) UTF16_BACK_N_SAFE(s, start, i, n)
00287
00289 # define UTF_SET_CHAR_LIMIT_UNSAFE(s, i) UTF16_SET_CHAR_LIMIT_UNSAFE(s, i)
00290
00291 # define UTF_SET_CHAR_LIMIT_SAFE(s, start, i, length) UTF16_SET_CHAR_LIMIT_SAFE(s, start, i, length)
00292
00293 #elif UTF_SIZE==32
00294
00295 # error UTF-32 is not implemented, undefine UTF_SIZE or define it to 16
00296
00297 typedef UChar32 UChar;
00298
00299 #else
00300 # error UTF_SIZE must be undefined or one of { 8, 16, 32 } - only 16 is implemented
00301 #endif
00302
00303
00304
00412 #ifdef UTF_SAFE
00413
00414 # define UTF_GET_CHAR(s, start, i, length, c) UTF_GET_CHAR_SAFE(s, start, i, length, c, FALSE)
00415
00416 # define UTF_NEXT_CHAR(s, i, length, c) UTF_NEXT_CHAR_SAFE(s, i, length, c, FALSE)
00417 # define UTF_APPEND_CHAR(s, i, length, c) UTF_APPEND_CHAR_SAFE(s, i, length, c)
00418 # define UTF_FWD_1(s, i, length) UTF_FWD_1_SAFE(s, i, length)
00419 # define UTF_FWD_N(s, i, length, n) UTF_FWD_N_SAFE(s, i, length, n)
00420 # define UTF_SET_CHAR_START(s, start, i) UTF_SET_CHAR_START_SAFE(s, start, i)
00421
00422 # define UTF_PREV_CHAR(s, start, i, c) UTF_PREV_CHAR_SAFE(s, start, i, c, FALSE)
00423 # define UTF_BACK_1(s, start, i) UTF_BACK_1_SAFE(s, start, i)
00424 # define UTF_BACK_N(s, start, i, n) UTF_BACK_N_SAFE(s, start, i, n)
00425 # define UTF_SET_CHAR_LIMIT(s, start, i, length) UTF_SET_CHAR_LIMIT_SAFE(s, start, i, length)
00426
00427 #elif defined(UTF_STRICT)
00428
00429 # define UTF_GET_CHAR(s, start, i, length, c) UTF_GET_CHAR_SAFE(s, start, i, length, c, TRUE)
00430
00431 # define UTF_NEXT_CHAR(s, i, length, c) UTF_NEXT_CHAR_SAFE(s, i, length, c, TRUE)
00432 # define UTF_APPEND_CHAR(s, i, length, c) UTF_APPEND_CHAR_SAFE(s, i, length, c)
00433 # define UTF_FWD_1(s, i, length) UTF_FWD_1_SAFE(s, i, length)
00434 # define UTF_FWD_N(s, i, length, n) UTF_FWD_N_SAFE(s, i, length, n)
00435 # define UTF_SET_CHAR_START(s, start, i) UTF_SET_CHAR_START_SAFE(s, start, i)
00436
00437 # define UTF_PREV_CHAR(s, start, i, c) UTF_PREV_CHAR_SAFE(s, start, i, c, TRUE)
00438 # define UTF_BACK_1(s, start, i) UTF_BACK_1_SAFE(s, start, i)
00439 # define UTF_BACK_N(s, start, i, n) UTF_BACK_N_SAFE(s, start, i, n)
00440 # define UTF_SET_CHAR_LIMIT(s, start, i, length) UTF_SET_CHAR_LIMIT_SAFE(s, start, i, length)
00441
00442 #else
00443
00444 # define UTF_GET_CHAR(s, start, i, length, c) UTF_GET_CHAR_UNSAFE(s, i, c)
00445
00446 # define UTF_NEXT_CHAR(s, i, length, c) UTF_NEXT_CHAR_UNSAFE(s, i, c)
00447 # define UTF_APPEND_CHAR(s, i, length, c) UTF_APPEND_CHAR_UNSAFE(s, i, c)
00448 # define UTF_FWD_1(s, i, length) UTF_FWD_1_UNSAFE(s, i)
00449 # define UTF_FWD_N(s, i, length, n) UTF_FWD_N_UNSAFE(s, i, n)
00450 # define UTF_SET_CHAR_START(s, start, i) UTF_SET_CHAR_START_UNSAFE(s, i)
00451
00452 # define UTF_PREV_CHAR(s, start, i, c) UTF_PREV_CHAR_UNSAFE(s, i, c)
00453 # define UTF_BACK_1(s, start, i) UTF_BACK_1_UNSAFE(s, i)
00454 # define UTF_BACK_N(s, start, i, n) UTF_BACK_N_UNSAFE(s, i, n)
00455 # define UTF_SET_CHAR_LIMIT(s, start, i, length) UTF_SET_CHAR_LIMIT_UNSAFE(s, i)
00456
00457 #endif
00458
00459 #endif