ICU 58.2  58.2
utf16.h
Go to the documentation of this file.
1 // Copyright (C) 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: US-ASCII
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 /* definitions with forward iteration --------------------------------------- */
217 
237 #define U16_NEXT_UNSAFE(s, i, c) { \
238  (c)=(s)[(i)++]; \
239  if(U16_IS_LEAD(c)) { \
240  (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)++]); \
241  } \
242 }
243 
266 #define U16_NEXT(s, i, length, c) { \
267  (c)=(s)[(i)++]; \
268  if(U16_IS_LEAD(c)) { \
269  uint16_t __c2; \
270  if((i)!=(length) && U16_IS_TRAIL(__c2=(s)[(i)])) { \
271  ++(i); \
272  (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
273  } \
274  } \
275 }
276 
290 #define U16_APPEND_UNSAFE(s, i, c) { \
291  if((uint32_t)(c)<=0xffff) { \
292  (s)[(i)++]=(uint16_t)(c); \
293  } else { \
294  (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
295  (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
296  } \
297 }
298 
316 #define U16_APPEND(s, i, capacity, c, isError) { \
317  if((uint32_t)(c)<=0xffff) { \
318  (s)[(i)++]=(uint16_t)(c); \
319  } else if((uint32_t)(c)<=0x10ffff && (i)+1<(capacity)) { \
320  (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
321  (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
322  } else /* c>0x10ffff or not enough space */ { \
323  (isError)=TRUE; \
324  } \
325 }
326 
337 #define U16_FWD_1_UNSAFE(s, i) { \
338  if(U16_IS_LEAD((s)[(i)++])) { \
339  ++(i); \
340  } \
341 }
342 
356 #define U16_FWD_1(s, i, length) { \
357  if(U16_IS_LEAD((s)[(i)++]) && (i)!=(length) && U16_IS_TRAIL((s)[i])) { \
358  ++(i); \
359  } \
360 }
361 
374 #define U16_FWD_N_UNSAFE(s, i, n) { \
375  int32_t __N=(n); \
376  while(__N>0) { \
377  U16_FWD_1_UNSAFE(s, i); \
378  --__N; \
379  } \
380 }
381 
397 #define U16_FWD_N(s, i, length, n) { \
398  int32_t __N=(n); \
399  while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
400  U16_FWD_1(s, i, length); \
401  --__N; \
402  } \
403 }
404 
418 #define U16_SET_CP_START_UNSAFE(s, i) { \
419  if(U16_IS_TRAIL((s)[i])) { \
420  --(i); \
421  } \
422 }
423 
438 #define U16_SET_CP_START(s, start, i) { \
439  if(U16_IS_TRAIL((s)[i]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
440  --(i); \
441  } \
442 }
443 
444 /* definitions with backward iteration -------------------------------------- */
445 
466 #define U16_PREV_UNSAFE(s, i, c) { \
467  (c)=(s)[--(i)]; \
468  if(U16_IS_TRAIL(c)) { \
469  (c)=U16_GET_SUPPLEMENTARY((s)[--(i)], (c)); \
470  } \
471 }
472 
494 #define U16_PREV(s, start, i, c) { \
495  (c)=(s)[--(i)]; \
496  if(U16_IS_TRAIL(c)) { \
497  uint16_t __c2; \
498  if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
499  --(i); \
500  (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
501  } \
502  } \
503 }
504 
516 #define U16_BACK_1_UNSAFE(s, i) { \
517  if(U16_IS_TRAIL((s)[--(i)])) { \
518  --(i); \
519  } \
520 }
521 
534 #define U16_BACK_1(s, start, i) { \
535  if(U16_IS_TRAIL((s)[--(i)]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
536  --(i); \
537  } \
538 }
539 
553 #define U16_BACK_N_UNSAFE(s, i, n) { \
554  int32_t __N=(n); \
555  while(__N>0) { \
556  U16_BACK_1_UNSAFE(s, i); \
557  --__N; \
558  } \
559 }
560 
575 #define U16_BACK_N(s, start, i, n) { \
576  int32_t __N=(n); \
577  while(__N>0 && (i)>(start)) { \
578  U16_BACK_1(s, start, i); \
579  --__N; \
580  } \
581 }
582 
596 #define U16_SET_CP_LIMIT_UNSAFE(s, i) { \
597  if(U16_IS_LEAD((s)[(i)-1])) { \
598  ++(i); \
599  } \
600 }
601 
619 #define U16_SET_CP_LIMIT(s, start, i, length) { \
620  if((start)<(i) && ((i)<(length) || (length)<0) && U16_IS_LEAD((s)[(i)-1]) && U16_IS_TRAIL((s)[i])) { \
621  ++(i); \
622  } \
623 }
624 
625 #endif
Basic types and constants for UTF.
C API: Code point macros.