|
ICU 73.2
73.2
|
00001 // © 2016 and later: Unicode, Inc. and others. 00002 // License & terms of use: http://www.unicode.org/copyright.html 00003 /* 00004 ******************************************************************************* 00005 * 00006 * Copyright (C) 1999-2012, International Business Machines 00007 * Corporation and others. All Rights Reserved. 00008 * 00009 ******************************************************************************* 00010 * file name: utf16.h 00011 * encoding: UTF-8 00012 * tab size: 8 (not used) 00013 * indentation:4 00014 * 00015 * created on: 1999sep09 00016 * created by: Markus W. Scherer 00017 */ 00018 00034 #ifndef __UTF16_H__ 00035 #define __UTF16_H__ 00036 00037 #include <stdbool.h> 00038 #include "unicode/umachine.h" 00039 #ifndef __UTF_H__ 00040 # include "unicode/utf.h" 00041 #endif 00042 00043 /* single-code point definitions -------------------------------------------- */ 00044 00051 #define U16_IS_SINGLE(c) !U_IS_SURROGATE(c) 00052 00059 #define U16_IS_LEAD(c) (((c)&0xfffffc00)==0xd800) 00060 00067 #define U16_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00) 00068 00075 #define U16_IS_SURROGATE(c) U_IS_SURROGATE(c) 00076 00084 #define U16_IS_SURROGATE_LEAD(c) (((c)&0x400)==0) 00085 00093 #define U16_IS_SURROGATE_TRAIL(c) (((c)&0x400)!=0) 00094 00099 #define U16_SURROGATE_OFFSET ((0xd800<<10UL)+0xdc00-0x10000) 00100 00112 #define U16_GET_SUPPLEMENTARY(lead, trail) \ 00113 (((UChar32)(lead)<<10UL)+(UChar32)(trail)-U16_SURROGATE_OFFSET) 00114 00115 00123 #define U16_LEAD(supplementary) (UChar)(((supplementary)>>10)+0xd7c0) 00124 00132 #define U16_TRAIL(supplementary) (UChar)(((supplementary)&0x3ff)|0xdc00) 00133 00141 #define U16_LENGTH(c) ((uint32_t)(c)<=0xffff ? 1 : 2) 00142 00148 #define U16_MAX_LENGTH 2 00149 00167 #define U16_GET_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \ 00168 (c)=(s)[i]; \ 00169 if(U16_IS_SURROGATE(c)) { \ 00170 if(U16_IS_SURROGATE_LEAD(c)) { \ 00171 (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)+1]); \ 00172 } else { \ 00173 (c)=U16_GET_SUPPLEMENTARY((s)[(i)-1], (c)); \ 00174 } \ 00175 } \ 00176 } UPRV_BLOCK_MACRO_END 00177 00201 #define U16_GET(s, start, i, length, c) UPRV_BLOCK_MACRO_BEGIN { \ 00202 (c)=(s)[i]; \ 00203 if(U16_IS_SURROGATE(c)) { \ 00204 uint16_t __c2; \ 00205 if(U16_IS_SURROGATE_LEAD(c)) { \ 00206 if((i)+1!=(length) && U16_IS_TRAIL(__c2=(s)[(i)+1])) { \ 00207 (c)=U16_GET_SUPPLEMENTARY((c), __c2); \ 00208 } \ 00209 } else { \ 00210 if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \ 00211 (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \ 00212 } \ 00213 } \ 00214 } \ 00215 } UPRV_BLOCK_MACRO_END 00216 00240 #define U16_GET_OR_FFFD(s, start, i, length, c) UPRV_BLOCK_MACRO_BEGIN { \ 00241 (c)=(s)[i]; \ 00242 if(U16_IS_SURROGATE(c)) { \ 00243 uint16_t __c2; \ 00244 if(U16_IS_SURROGATE_LEAD(c)) { \ 00245 if((i)+1!=(length) && U16_IS_TRAIL(__c2=(s)[(i)+1])) { \ 00246 (c)=U16_GET_SUPPLEMENTARY((c), __c2); \ 00247 } else { \ 00248 (c)=0xfffd; \ 00249 } \ 00250 } else { \ 00251 if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \ 00252 (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \ 00253 } else { \ 00254 (c)=0xfffd; \ 00255 } \ 00256 } \ 00257 } \ 00258 } UPRV_BLOCK_MACRO_END 00259 00260 /* definitions with forward iteration --------------------------------------- */ 00261 00281 #define U16_NEXT_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \ 00282 (c)=(s)[(i)++]; \ 00283 if(U16_IS_LEAD(c)) { \ 00284 (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)++]); \ 00285 } \ 00286 } UPRV_BLOCK_MACRO_END 00287 00309 #define U16_NEXT(s, i, length, c) UPRV_BLOCK_MACRO_BEGIN { \ 00310 (c)=(s)[(i)++]; \ 00311 if(U16_IS_LEAD(c)) { \ 00312 uint16_t __c2; \ 00313 if((i)!=(length) && U16_IS_TRAIL(__c2=(s)[(i)])) { \ 00314 ++(i); \ 00315 (c)=U16_GET_SUPPLEMENTARY((c), __c2); \ 00316 } \ 00317 } \ 00318 } UPRV_BLOCK_MACRO_END 00319 00341 #define U16_NEXT_OR_FFFD(s, i, length, c) UPRV_BLOCK_MACRO_BEGIN { \ 00342 (c)=(s)[(i)++]; \ 00343 if(U16_IS_SURROGATE(c)) { \ 00344 uint16_t __c2; \ 00345 if(U16_IS_SURROGATE_LEAD(c) && (i)!=(length) && U16_IS_TRAIL(__c2=(s)[(i)])) { \ 00346 ++(i); \ 00347 (c)=U16_GET_SUPPLEMENTARY((c), __c2); \ 00348 } else { \ 00349 (c)=0xfffd; \ 00350 } \ 00351 } \ 00352 } UPRV_BLOCK_MACRO_END 00353 00367 #define U16_APPEND_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \ 00368 if((uint32_t)(c)<=0xffff) { \ 00369 (s)[(i)++]=(uint16_t)(c); \ 00370 } else { \ 00371 (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \ 00372 (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \ 00373 } \ 00374 } UPRV_BLOCK_MACRO_END 00375 00393 #define U16_APPEND(s, i, capacity, c, isError) UPRV_BLOCK_MACRO_BEGIN { \ 00394 if((uint32_t)(c)<=0xffff) { \ 00395 (s)[(i)++]=(uint16_t)(c); \ 00396 } else if((uint32_t)(c)<=0x10ffff && (i)+1<(capacity)) { \ 00397 (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \ 00398 (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \ 00399 } else /* c>0x10ffff or not enough space */ { \ 00400 (isError)=true; \ 00401 } \ 00402 } UPRV_BLOCK_MACRO_END 00403 00414 #define U16_FWD_1_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \ 00415 if(U16_IS_LEAD((s)[(i)++])) { \ 00416 ++(i); \ 00417 } \ 00418 } UPRV_BLOCK_MACRO_END 00419 00433 #define U16_FWD_1(s, i, length) UPRV_BLOCK_MACRO_BEGIN { \ 00434 if(U16_IS_LEAD((s)[(i)++]) && (i)!=(length) && U16_IS_TRAIL((s)[i])) { \ 00435 ++(i); \ 00436 } \ 00437 } UPRV_BLOCK_MACRO_END 00438 00451 #define U16_FWD_N_UNSAFE(s, i, n) UPRV_BLOCK_MACRO_BEGIN { \ 00452 int32_t __N=(n); \ 00453 while(__N>0) { \ 00454 U16_FWD_1_UNSAFE(s, i); \ 00455 --__N; \ 00456 } \ 00457 } UPRV_BLOCK_MACRO_END 00458 00474 #define U16_FWD_N(s, i, length, n) UPRV_BLOCK_MACRO_BEGIN { \ 00475 int32_t __N=(n); \ 00476 while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \ 00477 U16_FWD_1(s, i, length); \ 00478 --__N; \ 00479 } \ 00480 } UPRV_BLOCK_MACRO_END 00481 00495 #define U16_SET_CP_START_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \ 00496 if(U16_IS_TRAIL((s)[i])) { \ 00497 --(i); \ 00498 } \ 00499 } UPRV_BLOCK_MACRO_END 00500 00515 #define U16_SET_CP_START(s, start, i) UPRV_BLOCK_MACRO_BEGIN { \ 00516 if(U16_IS_TRAIL((s)[i]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \ 00517 --(i); \ 00518 } \ 00519 } UPRV_BLOCK_MACRO_END 00520 00521 /* definitions with backward iteration -------------------------------------- */ 00522 00543 #define U16_PREV_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \ 00544 (c)=(s)[--(i)]; \ 00545 if(U16_IS_TRAIL(c)) { \ 00546 (c)=U16_GET_SUPPLEMENTARY((s)[--(i)], (c)); \ 00547 } \ 00548 } UPRV_BLOCK_MACRO_END 00549 00570 #define U16_PREV(s, start, i, c) UPRV_BLOCK_MACRO_BEGIN { \ 00571 (c)=(s)[--(i)]; \ 00572 if(U16_IS_TRAIL(c)) { \ 00573 uint16_t __c2; \ 00574 if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \ 00575 --(i); \ 00576 (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \ 00577 } \ 00578 } \ 00579 } UPRV_BLOCK_MACRO_END 00580 00601 #define U16_PREV_OR_FFFD(s, start, i, c) UPRV_BLOCK_MACRO_BEGIN { \ 00602 (c)=(s)[--(i)]; \ 00603 if(U16_IS_SURROGATE(c)) { \ 00604 uint16_t __c2; \ 00605 if(U16_IS_SURROGATE_TRAIL(c) && (i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \ 00606 --(i); \ 00607 (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \ 00608 } else { \ 00609 (c)=0xfffd; \ 00610 } \ 00611 } \ 00612 } UPRV_BLOCK_MACRO_END 00613 00625 #define U16_BACK_1_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \ 00626 if(U16_IS_TRAIL((s)[--(i)])) { \ 00627 --(i); \ 00628 } \ 00629 } UPRV_BLOCK_MACRO_END 00630 00643 #define U16_BACK_1(s, start, i) UPRV_BLOCK_MACRO_BEGIN { \ 00644 if(U16_IS_TRAIL((s)[--(i)]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \ 00645 --(i); \ 00646 } \ 00647 } UPRV_BLOCK_MACRO_END 00648 00662 #define U16_BACK_N_UNSAFE(s, i, n) UPRV_BLOCK_MACRO_BEGIN { \ 00663 int32_t __N=(n); \ 00664 while(__N>0) { \ 00665 U16_BACK_1_UNSAFE(s, i); \ 00666 --__N; \ 00667 } \ 00668 } UPRV_BLOCK_MACRO_END 00669 00684 #define U16_BACK_N(s, start, i, n) UPRV_BLOCK_MACRO_BEGIN { \ 00685 int32_t __N=(n); \ 00686 while(__N>0 && (i)>(start)) { \ 00687 U16_BACK_1(s, start, i); \ 00688 --__N; \ 00689 } \ 00690 } UPRV_BLOCK_MACRO_END 00691 00705 #define U16_SET_CP_LIMIT_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \ 00706 if(U16_IS_LEAD((s)[(i)-1])) { \ 00707 ++(i); \ 00708 } \ 00709 } UPRV_BLOCK_MACRO_END 00710 00728 #define U16_SET_CP_LIMIT(s, start, i, length) UPRV_BLOCK_MACRO_BEGIN { \ 00729 if((start)<(i) && ((i)<(length) || (length)<0) && U16_IS_LEAD((s)[(i)-1]) && U16_IS_TRAIL((s)[i])) { \ 00730 ++(i); \ 00731 } \ 00732 } UPRV_BLOCK_MACRO_END 00733 00734 #endif
1.7.6.1