ICU 54.1  54.1
utf16.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 1999-2012, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 *******************************************************************************
8 * file name: utf16.h
9 * encoding: US-ASCII
10 * tab size: 8 (not used)
11 * indentation:4
12 *
13 * created on: 1999sep09
14 * created by: Markus W. Scherer
15 */
16 
32 #ifndef __UTF16_H__
33 #define __UTF16_H__
34 
35 #include "unicode/umachine.h"
36 #ifndef __UTF_H__
37 # include "unicode/utf.h"
38 #endif
39 
40 /* single-code point definitions -------------------------------------------- */
41 
48 #define U16_IS_SINGLE(c) !U_IS_SURROGATE(c)
49 
56 #define U16_IS_LEAD(c) (((c)&0xfffffc00)==0xd800)
57 
64 #define U16_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00)
65 
72 #define U16_IS_SURROGATE(c) U_IS_SURROGATE(c)
73 
81 #define U16_IS_SURROGATE_LEAD(c) (((c)&0x400)==0)
82 
90 #define U16_IS_SURROGATE_TRAIL(c) (((c)&0x400)!=0)
91 
96 #define U16_SURROGATE_OFFSET ((0xd800<<10UL)+0xdc00-0x10000)
97 
109 #define U16_GET_SUPPLEMENTARY(lead, trail) \
110  (((UChar32)(lead)<<10UL)+(UChar32)(trail)-U16_SURROGATE_OFFSET)
111 
112 
120 #define U16_LEAD(supplementary) (UChar)(((supplementary)>>10)+0xd7c0)
121 
129 #define U16_TRAIL(supplementary) (UChar)(((supplementary)&0x3ff)|0xdc00)
130 
138 #define U16_LENGTH(c) ((uint32_t)(c)<=0xffff ? 1 : 2)
139 
145 #define U16_MAX_LENGTH 2
146 
164 #define U16_GET_UNSAFE(s, i, c) { \
165  (c)=(s)[i]; \
166  if(U16_IS_SURROGATE(c)) { \
167  if(U16_IS_SURROGATE_LEAD(c)) { \
168  (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)+1]); \
169  } else { \
170  (c)=U16_GET_SUPPLEMENTARY((s)[(i)-1], (c)); \
171  } \
172  } \
173 }
174 
198 #define U16_GET(s, start, i, length, c) { \
199  (c)=(s)[i]; \
200  if(U16_IS_SURROGATE(c)) { \
201  uint16_t __c2; \
202  if(U16_IS_SURROGATE_LEAD(c)) { \
203  if((i)+1!=(length) && U16_IS_TRAIL(__c2=(s)[(i)+1])) { \
204  (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
205  } \
206  } else { \
207  if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
208  (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
209  } \
210  } \
211  } \
212 }
213 
214 /* definitions with forward iteration --------------------------------------- */
215 
235 #define U16_NEXT_UNSAFE(s, i, c) { \
236  (c)=(s)[(i)++]; \
237  if(U16_IS_LEAD(c)) { \
238  (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)++]); \
239  } \
240 }
241 
264 #define U16_NEXT(s, i, length, c) { \
265  (c)=(s)[(i)++]; \
266  if(U16_IS_LEAD(c)) { \
267  uint16_t __c2; \
268  if((i)!=(length) && U16_IS_TRAIL(__c2=(s)[(i)])) { \
269  ++(i); \
270  (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
271  } \
272  } \
273 }
274 
288 #define U16_APPEND_UNSAFE(s, i, c) { \
289  if((uint32_t)(c)<=0xffff) { \
290  (s)[(i)++]=(uint16_t)(c); \
291  } else { \
292  (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
293  (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
294  } \
295 }
296 
314 #define U16_APPEND(s, i, capacity, c, isError) { \
315  if((uint32_t)(c)<=0xffff) { \
316  (s)[(i)++]=(uint16_t)(c); \
317  } else if((uint32_t)(c)<=0x10ffff && (i)+1<(capacity)) { \
318  (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
319  (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
320  } else /* c>0x10ffff or not enough space */ { \
321  (isError)=TRUE; \
322  } \
323 }
324 
335 #define U16_FWD_1_UNSAFE(s, i) { \
336  if(U16_IS_LEAD((s)[(i)++])) { \
337  ++(i); \
338  } \
339 }
340 
354 #define U16_FWD_1(s, i, length) { \
355  if(U16_IS_LEAD((s)[(i)++]) && (i)!=(length) && U16_IS_TRAIL((s)[i])) { \
356  ++(i); \
357  } \
358 }
359 
372 #define U16_FWD_N_UNSAFE(s, i, n) { \
373  int32_t __N=(n); \
374  while(__N>0) { \
375  U16_FWD_1_UNSAFE(s, i); \
376  --__N; \
377  } \
378 }
379 
395 #define U16_FWD_N(s, i, length, n) { \
396  int32_t __N=(n); \
397  while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
398  U16_FWD_1(s, i, length); \
399  --__N; \
400  } \
401 }
402 
416 #define U16_SET_CP_START_UNSAFE(s, i) { \
417  if(U16_IS_TRAIL((s)[i])) { \
418  --(i); \
419  } \
420 }
421 
436 #define U16_SET_CP_START(s, start, i) { \
437  if(U16_IS_TRAIL((s)[i]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
438  --(i); \
439  } \
440 }
441 
442 /* definitions with backward iteration -------------------------------------- */
443 
464 #define U16_PREV_UNSAFE(s, i, c) { \
465  (c)=(s)[--(i)]; \
466  if(U16_IS_TRAIL(c)) { \
467  (c)=U16_GET_SUPPLEMENTARY((s)[--(i)], (c)); \
468  } \
469 }
470 
492 #define U16_PREV(s, start, i, c) { \
493  (c)=(s)[--(i)]; \
494  if(U16_IS_TRAIL(c)) { \
495  uint16_t __c2; \
496  if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
497  --(i); \
498  (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
499  } \
500  } \
501 }
502 
514 #define U16_BACK_1_UNSAFE(s, i) { \
515  if(U16_IS_TRAIL((s)[--(i)])) { \
516  --(i); \
517  } \
518 }
519 
532 #define U16_BACK_1(s, start, i) { \
533  if(U16_IS_TRAIL((s)[--(i)]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
534  --(i); \
535  } \
536 }
537 
551 #define U16_BACK_N_UNSAFE(s, i, n) { \
552  int32_t __N=(n); \
553  while(__N>0) { \
554  U16_BACK_1_UNSAFE(s, i); \
555  --__N; \
556  } \
557 }
558 
573 #define U16_BACK_N(s, start, i, n) { \
574  int32_t __N=(n); \
575  while(__N>0 && (i)>(start)) { \
576  U16_BACK_1(s, start, i); \
577  --__N; \
578  } \
579 }
580 
594 #define U16_SET_CP_LIMIT_UNSAFE(s, i) { \
595  if(U16_IS_LEAD((s)[(i)-1])) { \
596  ++(i); \
597  } \
598 }
599 
617 #define U16_SET_CP_LIMIT(s, start, i, length) { \
618  if((start)<(i) && ((i)<(length) || (length)<0) && U16_IS_LEAD((s)[(i)-1]) && U16_IS_TRAIL((s)[i])) { \
619  ++(i); \
620  } \
621 }
622 
623 #endif
Basic types and constants for UTF.
C API: Code point macros.