ICU 50.1.2  50.1.2
utf16.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 1999-2011, 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 
195 #define U16_GET(s, start, i, length, c) { \
196  (c)=(s)[i]; \
197  if(U16_IS_SURROGATE(c)) { \
198  uint16_t __c2; \
199  if(U16_IS_SURROGATE_LEAD(c)) { \
200  if((i)+1<(length) && U16_IS_TRAIL(__c2=(s)[(i)+1])) { \
201  (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
202  } \
203  } else { \
204  if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
205  (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
206  } \
207  } \
208  } \
209 }
210 
211 /* definitions with forward iteration --------------------------------------- */
212 
232 #define U16_NEXT_UNSAFE(s, i, c) { \
233  (c)=(s)[(i)++]; \
234  if(U16_IS_LEAD(c)) { \
235  (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)++]); \
236  } \
237 }
238 
259 #define U16_NEXT(s, i, length, c) { \
260  (c)=(s)[(i)++]; \
261  if(U16_IS_LEAD(c)) { \
262  uint16_t __c2; \
263  if((i)<(length) && U16_IS_TRAIL(__c2=(s)[(i)])) { \
264  ++(i); \
265  (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
266  } \
267  } \
268 }
269 
283 #define U16_APPEND_UNSAFE(s, i, c) { \
284  if((uint32_t)(c)<=0xffff) { \
285  (s)[(i)++]=(uint16_t)(c); \
286  } else { \
287  (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
288  (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
289  } \
290 }
291 
309 #define U16_APPEND(s, i, capacity, c, isError) { \
310  if((uint32_t)(c)<=0xffff) { \
311  (s)[(i)++]=(uint16_t)(c); \
312  } else if((uint32_t)(c)<=0x10ffff && (i)+1<(capacity)) { \
313  (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
314  (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
315  } else /* c>0x10ffff or not enough space */ { \
316  (isError)=TRUE; \
317  } \
318 }
319 
330 #define U16_FWD_1_UNSAFE(s, i) { \
331  if(U16_IS_LEAD((s)[(i)++])) { \
332  ++(i); \
333  } \
334 }
335 
347 #define U16_FWD_1(s, i, length) { \
348  if(U16_IS_LEAD((s)[(i)++]) && (i)<(length) && U16_IS_TRAIL((s)[i])) { \
349  ++(i); \
350  } \
351 }
352 
365 #define U16_FWD_N_UNSAFE(s, i, n) { \
366  int32_t __N=(n); \
367  while(__N>0) { \
368  U16_FWD_1_UNSAFE(s, i); \
369  --__N; \
370  } \
371 }
372 
386 #define U16_FWD_N(s, i, length, n) { \
387  int32_t __N=(n); \
388  while(__N>0 && (i)<(length)) { \
389  U16_FWD_1(s, i, length); \
390  --__N; \
391  } \
392 }
393 
407 #define U16_SET_CP_START_UNSAFE(s, i) { \
408  if(U16_IS_TRAIL((s)[i])) { \
409  --(i); \
410  } \
411 }
412 
427 #define U16_SET_CP_START(s, start, i) { \
428  if(U16_IS_TRAIL((s)[i]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
429  --(i); \
430  } \
431 }
432 
433 /* definitions with backward iteration -------------------------------------- */
434 
455 #define U16_PREV_UNSAFE(s, i, c) { \
456  (c)=(s)[--(i)]; \
457  if(U16_IS_TRAIL(c)) { \
458  (c)=U16_GET_SUPPLEMENTARY((s)[--(i)], (c)); \
459  } \
460 }
461 
483 #define U16_PREV(s, start, i, c) { \
484  (c)=(s)[--(i)]; \
485  if(U16_IS_TRAIL(c)) { \
486  uint16_t __c2; \
487  if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
488  --(i); \
489  (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
490  } \
491  } \
492 }
493 
505 #define U16_BACK_1_UNSAFE(s, i) { \
506  if(U16_IS_TRAIL((s)[--(i)])) { \
507  --(i); \
508  } \
509 }
510 
523 #define U16_BACK_1(s, start, i) { \
524  if(U16_IS_TRAIL((s)[--(i)]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
525  --(i); \
526  } \
527 }
528 
542 #define U16_BACK_N_UNSAFE(s, i, n) { \
543  int32_t __N=(n); \
544  while(__N>0) { \
545  U16_BACK_1_UNSAFE(s, i); \
546  --__N; \
547  } \
548 }
549 
564 #define U16_BACK_N(s, start, i, n) { \
565  int32_t __N=(n); \
566  while(__N>0 && (i)>(start)) { \
567  U16_BACK_1(s, start, i); \
568  --__N; \
569  } \
570 }
571 
585 #define U16_SET_CP_LIMIT_UNSAFE(s, i) { \
586  if(U16_IS_LEAD((s)[(i)-1])) { \
587  ++(i); \
588  } \
589 }
590 
606 #define U16_SET_CP_LIMIT(s, start, i, length) { \
607  if((start)<(i) && (i)<(length) && U16_IS_LEAD((s)[(i)-1]) && U16_IS_TRAIL((s)[i])) { \
608  ++(i); \
609  } \
610 }
611 
612 #endif
Basic types and constants for UTF.
C API: Code point macros.