00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00087 #ifndef __UTF_H__
00088 #define __UTF_H__
00089
00090
00091
00092
00093
00094 #include <stddef.h>
00095 #include "unicode/umachine.h"
00096
00097
00098
00099 #ifndef UTF_SIZE
00100
00101 # define UTF_SIZE 16
00102 #endif
00103
00105 #define U_SIZEOF_UCHAR (UTF_SIZE>>3)
00106
00111 #ifndef U_HAVE_WCHAR_H
00112 # define U_HAVE_WCHAR_H 1
00113 #endif
00114
00115
00116 #if U_SIZEOF_WCHAR_T==0
00117 # undef U_SIZEOF_WCHAR_T
00118 # define U_SIZEOF_WCHAR_T 4
00119 #endif
00120
00129 #if !defined(U_WCHAR_IS_UTF16) && !defined(U_WCHAR_IS_UTF32)
00130 # ifdef __STDC_ISO_10646__
00131 # if (U_SIZEOF_WCHAR_T==2)
00132 # define U_WCHAR_IS_UTF16
00133 # elif (U_SIZEOF_WCHAR_T==4)
00134 # define U_WCHAR_IS_UTF32
00135 # endif
00136 # elif defined __UCS2__
00137 # if (__OS390__ || __OS400__) && (U_SIZEOF_WCHAR_T==2)
00138 # define U_WCHAR_IS_UTF16
00139 # endif
00140 # elif defined __UCS4__
00141 # if (U_SIZEOF_WCHAR_T==4)
00142 # define U_WCHAR_IS_UTF32
00143 # endif
00144 # elif defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
00145 # define U_WCHAR_IS_UTF16
00146 # endif
00147 #endif
00148
00154 #if U_SIZEOF_WCHAR_T==4
00155 typedef wchar_t UChar32;
00156 #else
00157 typedef uint32_t UChar32;
00158 #endif
00159
00164 typedef int32_t UTextOffset;
00165
00166
00167 #if !defined(UTF_SAFE) && !defined(UTF_STRICT) && !defined(UTF_UNSAFE)
00168
00172 # define UTF_SAFE
00173 #endif
00174
00175
00176
00189 #define UTF8_ERROR_VALUE_1 0x15
00190 #define UTF8_ERROR_VALUE_2 0x9f
00191
00196 #define UTF_ERROR_VALUE 0xffff
00197
00198
00199
00201 #define UTF_IS_SURROGATE(uchar) (((uchar)&0xfffff800)==0xd800)
00202
00206 #define UTF_IS_UNICODE_NONCHAR(c) \
00207 (((((c) & 0xfffe) == 0xfffe) || ((c) >= 0xfdd0 && (c) <= 0xfdef)) && \
00208 ((c) <= 0x10ffff))
00209
00223 #define UTF_IS_UNICODE_CHAR(c) \
00224 ((uint32_t)(c)<0xd800 || \
00225 ((uint32_t)(c)>0xdfff && \
00226 (uint32_t)(c)<=0x10ffff && \
00227 !UTF_IS_UNICODE_NONCHAR(c)))
00228
00233 #define UTF_IS_ERROR(c) \
00234 (((c)&0xfffe)==0xfffe || (c)==UTF8_ERROR_VALUE_1 || (c)==UTF8_ERROR_VALUE_2)
00235
00237 #define UTF_IS_VALID(c) \
00238 (UTF_IS_UNICODE_CHAR(c) && \
00239 (c)!=UTF8_ERROR_VALUE_1 && (c)!=UTF8_ERROR_VALUE_2)
00240
00241
00242
00243 #include "unicode/utf8.h"
00244 #include "unicode/utf16.h"
00245 #include "unicode/utf32.h"
00246
00247
00248
00255 #if UTF_SIZE==8
00256
00257 # error UTF-8 is not implemented, undefine UTF_SIZE or define it to 16
00258
00259
00260
00261
00262
00263 # include <limits.h>
00264
00265
00266 # if CHAR_MAX>=255
00267 typedef char UChar;
00268 # else
00269 typedef uint8_t UChar;
00270 # endif
00271
00272 #elif UTF_SIZE==16
00273
00274
00275 # if U_SIZEOF_WCHAR_T==2
00276 typedef wchar_t UChar;
00277 # else
00278 typedef uint16_t UChar;
00279 # endif
00280
00282 # define UTF_IS_SINGLE(uchar) UTF16_IS_SINGLE(uchar)
00283
00284 # define UTF_IS_LEAD(uchar) UTF16_IS_LEAD(uchar)
00285
00286 # define UTF_IS_TRAIL(uchar) UTF16_IS_TRAIL(uchar)
00287
00289 # define UTF_NEED_MULTIPLE_UCHAR(c) UTF16_NEED_MULTIPLE_UCHAR(c)
00290
00291 # define UTF_CHAR_LENGTH(c) UTF16_CHAR_LENGTH(c)
00292
00293 # define UTF_MAX_CHAR_LENGTH UTF16_MAX_CHAR_LENGTH
00294
00295 # define UTF_ARRAY_SIZE(size) UTF16_ARRAY_SIZE(size)
00296
00298 # define UTF_GET_CHAR_UNSAFE(s, i, c) UTF16_GET_CHAR_UNSAFE(s, i, c)
00299
00300 # define UTF_GET_CHAR_SAFE(s, start, i, length, c, strict) UTF16_GET_CHAR_SAFE(s, start, i, length, c, strict)
00301
00303 # define UTF_NEXT_CHAR_UNSAFE(s, i, c) UTF16_NEXT_CHAR_UNSAFE(s, i, c)
00304
00305 # define UTF_NEXT_CHAR_SAFE(s, i, length, c, strict) UTF16_NEXT_CHAR_SAFE(s, i, length, c, strict)
00306
00308 # define UTF_APPEND_CHAR_UNSAFE(s, i, c) UTF16_APPEND_CHAR_UNSAFE(s, i, c)
00309
00310 # define UTF_APPEND_CHAR_SAFE(s, i, length, c) UTF16_APPEND_CHAR_SAFE(s, i, length, c)
00311
00313 # define UTF_FWD_1_UNSAFE(s, i) UTF16_FWD_1_UNSAFE(s, i)
00314
00315 # define UTF_FWD_1_SAFE(s, i, length) UTF16_FWD_1_SAFE(s, i, length)
00316
00318 # define UTF_FWD_N_UNSAFE(s, i, n) UTF16_FWD_N_UNSAFE(s, i, n)
00319
00320 # define UTF_FWD_N_SAFE(s, i, length, n) UTF16_FWD_N_SAFE(s, i, length, n)
00321
00323 # define UTF_SET_CHAR_START_UNSAFE(s, i) UTF16_SET_CHAR_START_UNSAFE(s, i)
00324
00325 # define UTF_SET_CHAR_START_SAFE(s, start, i) UTF16_SET_CHAR_START_SAFE(s, start, i)
00326
00328 # define UTF_PREV_CHAR_UNSAFE(s, i, c) UTF16_PREV_CHAR_UNSAFE(s, i, c)
00329
00330 # define UTF_PREV_CHAR_SAFE(s, start, i, c, strict) UTF16_PREV_CHAR_SAFE(s, start, i, c, strict)
00331
00333 # define UTF_BACK_1_UNSAFE(s, i) UTF16_BACK_1_UNSAFE(s, i)
00334
00335 # define UTF_BACK_1_SAFE(s, start, i) UTF16_BACK_1_SAFE(s, start, i)
00336
00338 # define UTF_BACK_N_UNSAFE(s, i, n) UTF16_BACK_N_UNSAFE(s, i, n)
00339
00340 # define UTF_BACK_N_SAFE(s, start, i, n) UTF16_BACK_N_SAFE(s, start, i, n)
00341
00343 # define UTF_SET_CHAR_LIMIT_UNSAFE(s, i) UTF16_SET_CHAR_LIMIT_UNSAFE(s, i)
00344
00345 # define UTF_SET_CHAR_LIMIT_SAFE(s, start, i, length) UTF16_SET_CHAR_LIMIT_SAFE(s, start, i, length)
00346
00347 #elif UTF_SIZE==32
00348
00349 # error UTF-32 is not implemented, undefine UTF_SIZE or define it to 16
00350
00351 typedef UChar32 UChar;
00352
00353 #else
00354 # error UTF_SIZE must be undefined or one of { 8, 16, 32 } - only 16 is implemented
00355 #endif
00356
00357
00358
00466 #ifdef UTF_SAFE
00467
00468 # define UTF_GET_CHAR(s, start, i, length, c) UTF_GET_CHAR_SAFE(s, start, i, length, c, FALSE)
00469
00470 # define UTF_NEXT_CHAR(s, i, length, c) UTF_NEXT_CHAR_SAFE(s, i, length, c, FALSE)
00471 # define UTF_APPEND_CHAR(s, i, length, c) UTF_APPEND_CHAR_SAFE(s, i, length, c)
00472 # define UTF_FWD_1(s, i, length) UTF_FWD_1_SAFE(s, i, length)
00473 # define UTF_FWD_N(s, i, length, n) UTF_FWD_N_SAFE(s, i, length, n)
00474 # define UTF_SET_CHAR_START(s, start, i) UTF_SET_CHAR_START_SAFE(s, start, i)
00475
00476 # define UTF_PREV_CHAR(s, start, i, c) UTF_PREV_CHAR_SAFE(s, start, i, c, FALSE)
00477 # define UTF_BACK_1(s, start, i) UTF_BACK_1_SAFE(s, start, i)
00478 # define UTF_BACK_N(s, start, i, n) UTF_BACK_N_SAFE(s, start, i, n)
00479 # define UTF_SET_CHAR_LIMIT(s, start, i, length) UTF_SET_CHAR_LIMIT_SAFE(s, start, i, length)
00480
00481 #elif defined(UTF_STRICT)
00482
00483 # define UTF_GET_CHAR(s, start, i, length, c) UTF_GET_CHAR_SAFE(s, start, i, length, c, TRUE)
00484
00485 # define UTF_NEXT_CHAR(s, i, length, c) UTF_NEXT_CHAR_SAFE(s, i, length, c, TRUE)
00486 # define UTF_APPEND_CHAR(s, i, length, c) UTF_APPEND_CHAR_SAFE(s, i, length, c)
00487 # define UTF_FWD_1(s, i, length) UTF_FWD_1_SAFE(s, i, length)
00488 # define UTF_FWD_N(s, i, length, n) UTF_FWD_N_SAFE(s, i, length, n)
00489 # define UTF_SET_CHAR_START(s, start, i) UTF_SET_CHAR_START_SAFE(s, start, i)
00490
00491 # define UTF_PREV_CHAR(s, start, i, c) UTF_PREV_CHAR_SAFE(s, start, i, c, TRUE)
00492 # define UTF_BACK_1(s, start, i) UTF_BACK_1_SAFE(s, start, i)
00493 # define UTF_BACK_N(s, start, i, n) UTF_BACK_N_SAFE(s, start, i, n)
00494 # define UTF_SET_CHAR_LIMIT(s, start, i, length) UTF_SET_CHAR_LIMIT_SAFE(s, start, i, length)
00495
00496 #else
00497
00498 # define UTF_GET_CHAR(s, start, i, length, c) UTF_GET_CHAR_UNSAFE(s, i, c)
00499
00500 # define UTF_NEXT_CHAR(s, i, length, c) UTF_NEXT_CHAR_UNSAFE(s, i, c)
00501 # define UTF_APPEND_CHAR(s, i, length, c) UTF_APPEND_CHAR_UNSAFE(s, i, c)
00502 # define UTF_FWD_1(s, i, length) UTF_FWD_1_UNSAFE(s, i)
00503 # define UTF_FWD_N(s, i, length, n) UTF_FWD_N_UNSAFE(s, i, n)
00504 # define UTF_SET_CHAR_START(s, start, i) UTF_SET_CHAR_START_UNSAFE(s, i)
00505
00506 # define UTF_PREV_CHAR(s, start, i, c) UTF_PREV_CHAR_UNSAFE(s, i, c)
00507 # define UTF_BACK_1(s, start, i) UTF_BACK_1_UNSAFE(s, i)
00508 # define UTF_BACK_N(s, start, i, n) UTF_BACK_N_UNSAFE(s, i, n)
00509 # define UTF_SET_CHAR_LIMIT(s, start, i, length) UTF_SET_CHAR_LIMIT_UNSAFE(s, i)
00510
00511 #endif
00512
00513 #endif