ICU 62.1 62.1
utf16.h
Go to the documentation of this file.
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3/*
4*******************************************************************************
5*
6* Copyright (C) 1999-2012, International Business Machines
7* Corporation and others. All Rights Reserved.
8*
9*******************************************************************************
10* file name: utf16.h
11* encoding: UTF-8
12* tab size: 8 (not used)
13* indentation:4
14*
15* created on: 1999sep09
16* created by: Markus W. Scherer
17*/
18
34#ifndef __UTF16_H__
35#define __UTF16_H__
36
37#include "unicode/umachine.h"
38#ifndef __UTF_H__
39# include "unicode/utf.h"
40#endif
41
42/* single-code point definitions -------------------------------------------- */
43
50#define U16_IS_SINGLE(c) !U_IS_SURROGATE(c)
51
58#define U16_IS_LEAD(c) (((c)&0xfffffc00)==0xd800)
59
66#define U16_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00)
67
74#define U16_IS_SURROGATE(c) U_IS_SURROGATE(c)
75
83#define U16_IS_SURROGATE_LEAD(c) (((c)&0x400)==0)
84
92#define U16_IS_SURROGATE_TRAIL(c) (((c)&0x400)!=0)
93
98#define U16_SURROGATE_OFFSET ((0xd800<<10UL)+0xdc00-0x10000)
99
111#define U16_GET_SUPPLEMENTARY(lead, trail) \
112 (((UChar32)(lead)<<10UL)+(UChar32)(trail)-U16_SURROGATE_OFFSET)
113
114
122#define U16_LEAD(supplementary) (UChar)(((supplementary)>>10)+0xd7c0)
123
131#define U16_TRAIL(supplementary) (UChar)(((supplementary)&0x3ff)|0xdc00)
132
140#define U16_LENGTH(c) ((uint32_t)(c)<=0xffff ? 1 : 2)
141
147#define U16_MAX_LENGTH 2
148
166#define U16_GET_UNSAFE(s, i, c) { \
167 (c)=(s)[i]; \
168 if(U16_IS_SURROGATE(c)) { \
169 if(U16_IS_SURROGATE_LEAD(c)) { \
170 (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)+1]); \
171 } else { \
172 (c)=U16_GET_SUPPLEMENTARY((s)[(i)-1], (c)); \
173 } \
174 } \
175}
176
200#define U16_GET(s, start, i, length, c) { \
201 (c)=(s)[i]; \
202 if(U16_IS_SURROGATE(c)) { \
203 uint16_t __c2; \
204 if(U16_IS_SURROGATE_LEAD(c)) { \
205 if((i)+1!=(length) && U16_IS_TRAIL(__c2=(s)[(i)+1])) { \
206 (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
207 } \
208 } else { \
209 if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
210 (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
211 } \
212 } \
213 } \
214}
215
216#ifndef U_HIDE_DRAFT_API
217
241#define U16_GET_OR_FFFD(s, start, i, length, c) { \
242 (c)=(s)[i]; \
243 if(U16_IS_SURROGATE(c)) { \
244 uint16_t __c2; \
245 if(U16_IS_SURROGATE_LEAD(c)) { \
246 if((i)+1!=(length) && U16_IS_TRAIL(__c2=(s)[(i)+1])) { \
247 (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
248 } else { \
249 (c)=0xfffd; \
250 } \
251 } else { \
252 if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
253 (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
254 } else { \
255 (c)=0xfffd; \
256 } \
257 } \
258 } \
259}
260
261#endif // U_HIDE_DRAFT_API
262
263/* definitions with forward iteration --------------------------------------- */
264
284#define U16_NEXT_UNSAFE(s, i, c) { \
285 (c)=(s)[(i)++]; \
286 if(U16_IS_LEAD(c)) { \
287 (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)++]); \
288 } \
289}
290
312#define U16_NEXT(s, i, length, c) { \
313 (c)=(s)[(i)++]; \
314 if(U16_IS_LEAD(c)) { \
315 uint16_t __c2; \
316 if((i)!=(length) && U16_IS_TRAIL(__c2=(s)[(i)])) { \
317 ++(i); \
318 (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
319 } \
320 } \
321}
322
323#ifndef U_HIDE_DRAFT_API
324
346#define U16_NEXT_OR_FFFD(s, i, length, c) { \
347 (c)=(s)[(i)++]; \
348 if(U16_IS_SURROGATE(c)) { \
349 uint16_t __c2; \
350 if(U16_IS_SURROGATE_LEAD(c) && (i)!=(length) && U16_IS_TRAIL(__c2=(s)[(i)])) { \
351 ++(i); \
352 (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
353 } else { \
354 (c)=0xfffd; \
355 } \
356 } \
357}
358
359#endif // U_HIDE_DRAFT_API
360
374#define U16_APPEND_UNSAFE(s, i, c) { \
375 if((uint32_t)(c)<=0xffff) { \
376 (s)[(i)++]=(uint16_t)(c); \
377 } else { \
378 (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
379 (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
380 } \
381}
382
400#define U16_APPEND(s, i, capacity, c, isError) { \
401 if((uint32_t)(c)<=0xffff) { \
402 (s)[(i)++]=(uint16_t)(c); \
403 } else if((uint32_t)(c)<=0x10ffff && (i)+1<(capacity)) { \
404 (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
405 (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
406 } else /* c>0x10ffff or not enough space */ { \
407 (isError)=TRUE; \
408 } \
409}
410
421#define U16_FWD_1_UNSAFE(s, i) { \
422 if(U16_IS_LEAD((s)[(i)++])) { \
423 ++(i); \
424 } \
425}
426
440#define U16_FWD_1(s, i, length) { \
441 if(U16_IS_LEAD((s)[(i)++]) && (i)!=(length) && U16_IS_TRAIL((s)[i])) { \
442 ++(i); \
443 } \
444}
445
458#define U16_FWD_N_UNSAFE(s, i, n) { \
459 int32_t __N=(n); \
460 while(__N>0) { \
461 U16_FWD_1_UNSAFE(s, i); \
462 --__N; \
463 } \
464}
465
481#define U16_FWD_N(s, i, length, n) { \
482 int32_t __N=(n); \
483 while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
484 U16_FWD_1(s, i, length); \
485 --__N; \
486 } \
487}
488
502#define U16_SET_CP_START_UNSAFE(s, i) { \
503 if(U16_IS_TRAIL((s)[i])) { \
504 --(i); \
505 } \
506}
507
522#define U16_SET_CP_START(s, start, i) { \
523 if(U16_IS_TRAIL((s)[i]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
524 --(i); \
525 } \
526}
527
528/* definitions with backward iteration -------------------------------------- */
529
550#define U16_PREV_UNSAFE(s, i, c) { \
551 (c)=(s)[--(i)]; \
552 if(U16_IS_TRAIL(c)) { \
553 (c)=U16_GET_SUPPLEMENTARY((s)[--(i)], (c)); \
554 } \
555}
556
577#define U16_PREV(s, start, i, c) { \
578 (c)=(s)[--(i)]; \
579 if(U16_IS_TRAIL(c)) { \
580 uint16_t __c2; \
581 if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
582 --(i); \
583 (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
584 } \
585 } \
586}
587
588#ifndef U_HIDE_DRAFT_API
589
610#define U16_PREV_OR_FFFD(s, start, i, c) { \
611 (c)=(s)[--(i)]; \
612 if(U16_IS_SURROGATE(c)) { \
613 uint16_t __c2; \
614 if(U16_IS_SURROGATE_TRAIL(c) && (i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
615 --(i); \
616 (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
617 } else { \
618 (c)=0xfffd; \
619 } \
620 } \
621}
622
623#endif // U_HIDE_DRAFT_API
624
636#define U16_BACK_1_UNSAFE(s, i) { \
637 if(U16_IS_TRAIL((s)[--(i)])) { \
638 --(i); \
639 } \
640}
641
654#define U16_BACK_1(s, start, i) { \
655 if(U16_IS_TRAIL((s)[--(i)]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
656 --(i); \
657 } \
658}
659
673#define U16_BACK_N_UNSAFE(s, i, n) { \
674 int32_t __N=(n); \
675 while(__N>0) { \
676 U16_BACK_1_UNSAFE(s, i); \
677 --__N; \
678 } \
679}
680
695#define U16_BACK_N(s, start, i, n) { \
696 int32_t __N=(n); \
697 while(__N>0 && (i)>(start)) { \
698 U16_BACK_1(s, start, i); \
699 --__N; \
700 } \
701}
702
716#define U16_SET_CP_LIMIT_UNSAFE(s, i) { \
717 if(U16_IS_LEAD((s)[(i)-1])) { \
718 ++(i); \
719 } \
720}
721
739#define U16_SET_CP_LIMIT(s, start, i, length) { \
740 if((start)<(i) && ((i)<(length) || (length)<0) && U16_IS_LEAD((s)[(i)-1]) && U16_IS_TRAIL((s)[i])) { \
741 ++(i); \
742 } \
743}
744
745#endif
Basic types and constants for UTF.
C API: Code point macros.