]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/unicode/utf16.h
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 1999-2012, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
12 * tab size: 8 (not used)
15 * created on: 1999sep09
16 * created by: Markus W. Scherer
21 * \brief C API: 16-bit Unicode handling macros
23 * This file defines macros to deal with 16-bit Unicode (UTF-16) code units and strings.
25 * For more information see utf.h and the ICU User Guide Strings chapter
26 * (http://userguide.icu-project.org/strings).
29 * ICU coding guidelines for if() statements should be followed when using these macros.
30 * Compound statements (curly braces {}) must be used for if-else-while...
31 * bodies and all macro statements should be terminated with semicolon.
37 #include "unicode/umachine.h"
39 # include "unicode/utf.h"
42 /* single-code point definitions -------------------------------------------- */
45 * Does this code unit alone encode a code point (BMP, not a surrogate)?
46 * @param c 16-bit code unit
47 * @return TRUE or FALSE
50 #define U16_IS_SINGLE(c) !U_IS_SURROGATE(c)
53 * Is this code unit a lead surrogate (U+d800..U+dbff)?
54 * @param c 16-bit code unit
55 * @return TRUE or FALSE
58 #define U16_IS_LEAD(c) (((c)&0xfffffc00)==0xd800)
61 * Is this code unit a trail surrogate (U+dc00..U+dfff)?
62 * @param c 16-bit code unit
63 * @return TRUE or FALSE
66 #define U16_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00)
69 * Is this code unit a surrogate (U+d800..U+dfff)?
70 * @param c 16-bit code unit
71 * @return TRUE or FALSE
74 #define U16_IS_SURROGATE(c) U_IS_SURROGATE(c)
77 * Assuming c is a surrogate code point (U16_IS_SURROGATE(c)),
78 * is it a lead surrogate?
79 * @param c 16-bit code unit
80 * @return TRUE or FALSE
83 #define U16_IS_SURROGATE_LEAD(c) (((c)&0x400)==0)
86 * Assuming c is a surrogate code point (U16_IS_SURROGATE(c)),
87 * is it a trail surrogate?
88 * @param c 16-bit code unit
89 * @return TRUE or FALSE
92 #define U16_IS_SURROGATE_TRAIL(c) (((c)&0x400)!=0)
95 * Helper constant for U16_GET_SUPPLEMENTARY.
98 #define U16_SURROGATE_OFFSET ((0xd800<<10UL)+0xdc00-0x10000)
101 * Get a supplementary code point value (U+10000..U+10ffff)
102 * from its lead and trail surrogates.
103 * The result is undefined if the input values are not
104 * lead and trail surrogates.
106 * @param lead lead surrogate (U+d800..U+dbff)
107 * @param trail trail surrogate (U+dc00..U+dfff)
108 * @return supplementary code point (U+10000..U+10ffff)
111 #define U16_GET_SUPPLEMENTARY(lead, trail) \
112 (((UChar32)(lead)<<10UL)+(UChar32)(trail)-U16_SURROGATE_OFFSET)
116 * Get the lead surrogate (0xd800..0xdbff) for a
117 * supplementary code point (0x10000..0x10ffff).
118 * @param supplementary 32-bit code point (U+10000..U+10ffff)
119 * @return lead surrogate (U+d800..U+dbff) for supplementary
122 #define U16_LEAD(supplementary) (UChar)(((supplementary)>>10)+0xd7c0)
125 * Get the trail surrogate (0xdc00..0xdfff) for a
126 * supplementary code point (0x10000..0x10ffff).
127 * @param supplementary 32-bit code point (U+10000..U+10ffff)
128 * @return trail surrogate (U+dc00..U+dfff) for supplementary
131 #define U16_TRAIL(supplementary) (UChar)(((supplementary)&0x3ff)|0xdc00)
134 * How many 16-bit code units are used to encode this Unicode code point? (1 or 2)
135 * The result is not defined if c is not a Unicode code point (U+0000..U+10ffff).
136 * @param c 32-bit code point
140 #define U16_LENGTH(c) ((uint32_t)(c)<=0xffff ? 1 : 2)
143 * The maximum number of 16-bit code units per Unicode code point (U+0000..U+10ffff).
147 #define U16_MAX_LENGTH 2
150 * Get a code point from a string at a random-access offset,
151 * without changing the offset.
152 * "Unsafe" macro, assumes well-formed UTF-16.
154 * The offset may point to either the lead or trail surrogate unit
155 * for a supplementary code point, in which case the macro will read
156 * the adjacent matching surrogate as well.
157 * The result is undefined if the offset points to a single, unpaired surrogate.
158 * Iteration through a string is more efficient with U16_NEXT_UNSAFE or U16_NEXT.
160 * @param s const UChar * string
161 * @param i string offset
162 * @param c output UChar32 variable
166 #define U16_GET_UNSAFE(s, i, c) { \
168 if(U16_IS_SURROGATE(c)) { \
169 if(U16_IS_SURROGATE_LEAD(c)) { \
170 (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)+1]); \
172 (c)=U16_GET_SUPPLEMENTARY((s)[(i)-1], (c)); \
178 * Get a code point from a string at a random-access offset,
179 * without changing the offset.
180 * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
182 * The offset may point to either the lead or trail surrogate unit
183 * for a supplementary code point, in which case the macro will read
184 * the adjacent matching surrogate as well.
186 * The length can be negative for a NUL-terminated string.
188 * If the offset points to a single, unpaired surrogate, then that itself
189 * will be returned as the code point.
190 * Iteration through a string is more efficient with U16_NEXT_UNSAFE or U16_NEXT.
192 * @param s const UChar * string
193 * @param start starting string offset (usually 0)
194 * @param i string offset, must be start<=i<length
195 * @param length string length
196 * @param c output UChar32 variable
197 * @see U16_GET_UNSAFE
200 #define U16_GET(s, start, i, length, c) { \
202 if(U16_IS_SURROGATE(c)) { \
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); \
209 if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
210 (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
216 /* definitions with forward iteration --------------------------------------- */
219 * Get a code point from a string at a code point boundary offset,
220 * and advance the offset to the next code point boundary.
221 * (Post-incrementing forward iteration.)
222 * "Unsafe" macro, assumes well-formed UTF-16.
224 * The offset may point to the lead surrogate unit
225 * for a supplementary code point, in which case the macro will read
226 * the following trail surrogate as well.
227 * If the offset points to a trail surrogate, then that itself
228 * will be returned as the code point.
229 * The result is undefined if the offset points to a single, unpaired lead surrogate.
231 * @param s const UChar * string
232 * @param i string offset
233 * @param c output UChar32 variable
237 #define U16_NEXT_UNSAFE(s, i, c) { \
239 if(U16_IS_LEAD(c)) { \
240 (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)++]); \
245 * Get a code point from a string at a code point boundary offset,
246 * and advance the offset to the next code point boundary.
247 * (Post-incrementing forward iteration.)
248 * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
250 * The length can be negative for a NUL-terminated string.
252 * The offset may point to the lead surrogate unit
253 * for a supplementary code point, in which case the macro will read
254 * the following trail surrogate as well.
255 * If the offset points to a trail surrogate or
256 * to a single, unpaired lead surrogate, then that itself
257 * will be returned as the code point.
259 * @param s const UChar * string
260 * @param i string offset, must be i<length
261 * @param length string length
262 * @param c output UChar32 variable
263 * @see U16_NEXT_UNSAFE
266 #define U16_NEXT(s, i, length, c) { \
268 if(U16_IS_LEAD(c)) { \
270 if((i)!=(length) && U16_IS_TRAIL(__c2=(s)[(i)])) { \
272 (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
278 * Append a code point to a string, overwriting 1 or 2 code units.
279 * The offset points to the current end of the string contents
280 * and is advanced (post-increment).
281 * "Unsafe" macro, assumes a valid code point and sufficient space in the string.
282 * Otherwise, the result is undefined.
284 * @param s const UChar * string buffer
285 * @param i string offset
286 * @param c code point to append
290 #define U16_APPEND_UNSAFE(s, i, c) { \
291 if((uint32_t)(c)<=0xffff) { \
292 (s)[(i)++]=(uint16_t)(c); \
294 (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
295 (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
300 * Append a code point to a string, overwriting 1 or 2 code units.
301 * The offset points to the current end of the string contents
302 * and is advanced (post-increment).
303 * "Safe" macro, checks for a valid code point.
304 * If a surrogate pair is written, checks for sufficient space in the string.
305 * If the code point is not valid or a trail surrogate does not fit,
306 * then isError is set to TRUE.
308 * @param s const UChar * string buffer
309 * @param i string offset, must be i<capacity
310 * @param capacity size of the string buffer
311 * @param c code point to append
312 * @param isError output UBool set to TRUE if an error occurs, otherwise not modified
313 * @see U16_APPEND_UNSAFE
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 */ { \
328 * Advance the string offset from one code point boundary to the next.
329 * (Post-incrementing iteration.)
330 * "Unsafe" macro, assumes well-formed UTF-16.
332 * @param s const UChar * string
333 * @param i string offset
337 #define U16_FWD_1_UNSAFE(s, i) { \
338 if(U16_IS_LEAD((s)[(i)++])) { \
344 * Advance the string offset from one code point boundary to the next.
345 * (Post-incrementing iteration.)
346 * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
348 * The length can be negative for a NUL-terminated string.
350 * @param s const UChar * string
351 * @param i string offset, must be i<length
352 * @param length string length
353 * @see U16_FWD_1_UNSAFE
356 #define U16_FWD_1(s, i, length) { \
357 if(U16_IS_LEAD((s)[(i)++]) && (i)!=(length) && U16_IS_TRAIL((s)[i])) { \
363 * Advance the string offset from one code point boundary to the n-th next one,
364 * i.e., move forward by n code points.
365 * (Post-incrementing iteration.)
366 * "Unsafe" macro, assumes well-formed UTF-16.
368 * @param s const UChar * string
369 * @param i string offset
370 * @param n number of code points to skip
374 #define U16_FWD_N_UNSAFE(s, i, n) { \
377 U16_FWD_1_UNSAFE(s, i); \
383 * Advance the string offset from one code point boundary to the n-th next one,
384 * i.e., move forward by n code points.
385 * (Post-incrementing iteration.)
386 * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
388 * The length can be negative for a NUL-terminated string.
390 * @param s const UChar * string
391 * @param i int32_t string offset, must be i<length
392 * @param length int32_t string length
393 * @param n number of code points to skip
394 * @see U16_FWD_N_UNSAFE
397 #define U16_FWD_N(s, i, length, n) { \
399 while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
400 U16_FWD_1(s, i, length); \
406 * Adjust a random-access offset to a code point boundary
407 * at the start of a code point.
408 * If the offset points to the trail surrogate of a surrogate pair,
409 * then the offset is decremented.
410 * Otherwise, it is not modified.
411 * "Unsafe" macro, assumes well-formed UTF-16.
413 * @param s const UChar * string
414 * @param i string offset
415 * @see U16_SET_CP_START
418 #define U16_SET_CP_START_UNSAFE(s, i) { \
419 if(U16_IS_TRAIL((s)[i])) { \
425 * Adjust a random-access offset to a code point boundary
426 * at the start of a code point.
427 * If the offset points to the trail surrogate of a surrogate pair,
428 * then the offset is decremented.
429 * Otherwise, it is not modified.
430 * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
432 * @param s const UChar * string
433 * @param start starting string offset (usually 0)
434 * @param i string offset, must be start<=i
435 * @see U16_SET_CP_START_UNSAFE
438 #define U16_SET_CP_START(s, start, i) { \
439 if(U16_IS_TRAIL((s)[i]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
444 /* definitions with backward iteration -------------------------------------- */
447 * Move the string offset from one code point boundary to the previous one
448 * and get the code point between them.
449 * (Pre-decrementing backward iteration.)
450 * "Unsafe" macro, assumes well-formed UTF-16.
452 * The input offset may be the same as the string length.
453 * If the offset is behind a trail surrogate unit
454 * for a supplementary code point, then the macro will read
455 * the preceding lead surrogate as well.
456 * If the offset is behind a lead surrogate, then that itself
457 * will be returned as the code point.
458 * The result is undefined if the offset is behind a single, unpaired trail surrogate.
460 * @param s const UChar * string
461 * @param i string offset
462 * @param c output UChar32 variable
466 #define U16_PREV_UNSAFE(s, i, c) { \
468 if(U16_IS_TRAIL(c)) { \
469 (c)=U16_GET_SUPPLEMENTARY((s)[--(i)], (c)); \
474 * Move the string offset from one code point boundary to the previous one
475 * and get the code point between them.
476 * (Pre-decrementing backward iteration.)
477 * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
479 * The input offset may be the same as the string length.
480 * If the offset is behind a trail surrogate unit
481 * for a supplementary code point, then the macro will read
482 * the preceding lead surrogate as well.
483 * If the offset is behind a lead surrogate or behind a single, unpaired
484 * trail surrogate, then that itself
485 * will be returned as the code point.
487 * @param s const UChar * string
488 * @param start starting string offset (usually 0)
489 * @param i string offset, must be start<i
490 * @param c output UChar32 variable
491 * @see U16_PREV_UNSAFE
494 #define U16_PREV(s, start, i, c) { \
496 if(U16_IS_TRAIL(c)) { \
498 if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
500 (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
506 * Move the string offset from one code point boundary to the previous one.
507 * (Pre-decrementing backward iteration.)
508 * The input offset may be the same as the string length.
509 * "Unsafe" macro, assumes well-formed UTF-16.
511 * @param s const UChar * string
512 * @param i string offset
516 #define U16_BACK_1_UNSAFE(s, i) { \
517 if(U16_IS_TRAIL((s)[--(i)])) { \
523 * Move the string offset from one code point boundary to the previous one.
524 * (Pre-decrementing backward iteration.)
525 * The input offset may be the same as the string length.
526 * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
528 * @param s const UChar * string
529 * @param start starting string offset (usually 0)
530 * @param i string offset, must be start<i
531 * @see U16_BACK_1_UNSAFE
534 #define U16_BACK_1(s, start, i) { \
535 if(U16_IS_TRAIL((s)[--(i)]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
541 * Move the string offset from one code point boundary to the n-th one before it,
542 * i.e., move backward by n code points.
543 * (Pre-decrementing backward iteration.)
544 * The input offset may be the same as the string length.
545 * "Unsafe" macro, assumes well-formed UTF-16.
547 * @param s const UChar * string
548 * @param i string offset
549 * @param n number of code points to skip
553 #define U16_BACK_N_UNSAFE(s, i, n) { \
556 U16_BACK_1_UNSAFE(s, i); \
562 * Move the string offset from one code point boundary to the n-th one before it,
563 * i.e., move backward by n code points.
564 * (Pre-decrementing backward iteration.)
565 * The input offset may be the same as the string length.
566 * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
568 * @param s const UChar * string
569 * @param start start of string
570 * @param i string offset, must be start<i
571 * @param n number of code points to skip
572 * @see U16_BACK_N_UNSAFE
575 #define U16_BACK_N(s, start, i, n) { \
577 while(__N>0 && (i)>(start)) { \
578 U16_BACK_1(s, start, i); \
584 * Adjust a random-access offset to a code point boundary after a code point.
585 * If the offset is behind the lead surrogate of a surrogate pair,
586 * then the offset is incremented.
587 * Otherwise, it is not modified.
588 * The input offset may be the same as the string length.
589 * "Unsafe" macro, assumes well-formed UTF-16.
591 * @param s const UChar * string
592 * @param i string offset
593 * @see U16_SET_CP_LIMIT
596 #define U16_SET_CP_LIMIT_UNSAFE(s, i) { \
597 if(U16_IS_LEAD((s)[(i)-1])) { \
603 * Adjust a random-access offset to a code point boundary after a code point.
604 * If the offset is behind the lead surrogate of a surrogate pair,
605 * then the offset is incremented.
606 * Otherwise, it is not modified.
607 * The input offset may be the same as the string length.
608 * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
610 * The length can be negative for a NUL-terminated string.
612 * @param s const UChar * string
613 * @param start int32_t starting string offset (usually 0)
614 * @param i int32_t string offset, start<=i<=length
615 * @param length int32_t string length
616 * @see U16_SET_CP_LIMIT_UNSAFE
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])) { \