]>
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
189 * c is set to that unpaired surrogate.
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 #ifndef U_HIDE_DRAFT_API
219 * Get a code point from a string at a random-access offset,
220 * without changing the offset.
221 * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
223 * The offset may point to either the lead or trail surrogate unit
224 * for a supplementary code point, in which case the macro will read
225 * the adjacent matching surrogate as well.
227 * The length can be negative for a NUL-terminated string.
229 * If the offset points to a single, unpaired surrogate, then
230 * c is set to U+FFFD.
231 * Iteration through a string is more efficient with U16_NEXT_UNSAFE or U16_NEXT_OR_FFFD.
233 * @param s const UChar * string
234 * @param start starting string offset (usually 0)
235 * @param i string offset, must be start<=i<length
236 * @param length string length
237 * @param c output UChar32 variable
238 * @see U16_GET_UNSAFE
241 #define U16_GET_OR_FFFD(s, start, i, length, c) { \
243 if(U16_IS_SURROGATE(c)) { \
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); \
252 if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
253 (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
261 #endif // U_HIDE_DRAFT_API
263 /* definitions with forward iteration --------------------------------------- */
266 * Get a code point from a string at a code point boundary offset,
267 * and advance the offset to the next code point boundary.
268 * (Post-incrementing forward iteration.)
269 * "Unsafe" macro, assumes well-formed UTF-16.
271 * The offset may point to the lead surrogate unit
272 * for a supplementary code point, in which case the macro will read
273 * the following trail surrogate as well.
274 * If the offset points to a trail surrogate, then that itself
275 * will be returned as the code point.
276 * The result is undefined if the offset points to a single, unpaired lead surrogate.
278 * @param s const UChar * string
279 * @param i string offset
280 * @param c output UChar32 variable
284 #define U16_NEXT_UNSAFE(s, i, c) { \
286 if(U16_IS_LEAD(c)) { \
287 (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)++]); \
292 * Get a code point from a string at a code point boundary offset,
293 * and advance the offset to the next code point boundary.
294 * (Post-incrementing forward iteration.)
295 * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
297 * The length can be negative for a NUL-terminated string.
299 * The offset may point to the lead surrogate unit
300 * for a supplementary code point, in which case the macro will read
301 * the following trail surrogate as well.
302 * If the offset points to a trail surrogate or
303 * to a single, unpaired lead surrogate, then c is set to that unpaired surrogate.
305 * @param s const UChar * string
306 * @param i string offset, must be i<length
307 * @param length string length
308 * @param c output UChar32 variable
309 * @see U16_NEXT_UNSAFE
312 #define U16_NEXT(s, i, length, c) { \
314 if(U16_IS_LEAD(c)) { \
316 if((i)!=(length) && U16_IS_TRAIL(__c2=(s)[(i)])) { \
318 (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
323 #ifndef U_HIDE_DRAFT_API
326 * Get a code point from a string at a code point boundary offset,
327 * and advance the offset to the next code point boundary.
328 * (Post-incrementing forward iteration.)
329 * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
331 * The length can be negative for a NUL-terminated string.
333 * The offset may point to the lead surrogate unit
334 * for a supplementary code point, in which case the macro will read
335 * the following trail surrogate as well.
336 * If the offset points to a trail surrogate or
337 * to a single, unpaired lead surrogate, then c is set to U+FFFD.
339 * @param s const UChar * string
340 * @param i string offset, must be i<length
341 * @param length string length
342 * @param c output UChar32 variable
343 * @see U16_NEXT_UNSAFE
346 #define U16_NEXT_OR_FFFD(s, i, length, c) { \
348 if(U16_IS_SURROGATE(c)) { \
350 if(U16_IS_SURROGATE_LEAD(c) && (i)!=(length) && U16_IS_TRAIL(__c2=(s)[(i)])) { \
352 (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
359 #endif // U_HIDE_DRAFT_API
362 * Append a code point to a string, overwriting 1 or 2 code units.
363 * The offset points to the current end of the string contents
364 * and is advanced (post-increment).
365 * "Unsafe" macro, assumes a valid code point and sufficient space in the string.
366 * Otherwise, the result is undefined.
368 * @param s const UChar * string buffer
369 * @param i string offset
370 * @param c code point to append
374 #define U16_APPEND_UNSAFE(s, i, c) { \
375 if((uint32_t)(c)<=0xffff) { \
376 (s)[(i)++]=(uint16_t)(c); \
378 (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
379 (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
384 * Append a code point to a string, overwriting 1 or 2 code units.
385 * The offset points to the current end of the string contents
386 * and is advanced (post-increment).
387 * "Safe" macro, checks for a valid code point.
388 * If a surrogate pair is written, checks for sufficient space in the string.
389 * If the code point is not valid or a trail surrogate does not fit,
390 * then isError is set to TRUE.
392 * @param s const UChar * string buffer
393 * @param i string offset, must be i<capacity
394 * @param capacity size of the string buffer
395 * @param c code point to append
396 * @param isError output UBool set to TRUE if an error occurs, otherwise not modified
397 * @see U16_APPEND_UNSAFE
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 */ { \
412 * Advance the string offset from one code point boundary to the next.
413 * (Post-incrementing iteration.)
414 * "Unsafe" macro, assumes well-formed UTF-16.
416 * @param s const UChar * string
417 * @param i string offset
421 #define U16_FWD_1_UNSAFE(s, i) { \
422 if(U16_IS_LEAD((s)[(i)++])) { \
428 * Advance the string offset from one code point boundary to the next.
429 * (Post-incrementing iteration.)
430 * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
432 * The length can be negative for a NUL-terminated string.
434 * @param s const UChar * string
435 * @param i string offset, must be i<length
436 * @param length string length
437 * @see U16_FWD_1_UNSAFE
440 #define U16_FWD_1(s, i, length) { \
441 if(U16_IS_LEAD((s)[(i)++]) && (i)!=(length) && U16_IS_TRAIL((s)[i])) { \
447 * Advance the string offset from one code point boundary to the n-th next one,
448 * i.e., move forward by n code points.
449 * (Post-incrementing iteration.)
450 * "Unsafe" macro, assumes well-formed UTF-16.
452 * @param s const UChar * string
453 * @param i string offset
454 * @param n number of code points to skip
458 #define U16_FWD_N_UNSAFE(s, i, n) { \
461 U16_FWD_1_UNSAFE(s, i); \
467 * Advance the string offset from one code point boundary to the n-th next one,
468 * i.e., move forward by n code points.
469 * (Post-incrementing iteration.)
470 * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
472 * The length can be negative for a NUL-terminated string.
474 * @param s const UChar * string
475 * @param i int32_t string offset, must be i<length
476 * @param length int32_t string length
477 * @param n number of code points to skip
478 * @see U16_FWD_N_UNSAFE
481 #define U16_FWD_N(s, i, length, n) { \
483 while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
484 U16_FWD_1(s, i, length); \
490 * Adjust a random-access offset to a code point boundary
491 * at the start of a code point.
492 * If the offset points to the trail surrogate of a surrogate pair,
493 * then the offset is decremented.
494 * Otherwise, it is not modified.
495 * "Unsafe" macro, assumes well-formed UTF-16.
497 * @param s const UChar * string
498 * @param i string offset
499 * @see U16_SET_CP_START
502 #define U16_SET_CP_START_UNSAFE(s, i) { \
503 if(U16_IS_TRAIL((s)[i])) { \
509 * Adjust a random-access offset to a code point boundary
510 * at the start of a code point.
511 * If the offset points to the trail surrogate of a surrogate pair,
512 * then the offset is decremented.
513 * Otherwise, it is not modified.
514 * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
516 * @param s const UChar * string
517 * @param start starting string offset (usually 0)
518 * @param i string offset, must be start<=i
519 * @see U16_SET_CP_START_UNSAFE
522 #define U16_SET_CP_START(s, start, i) { \
523 if(U16_IS_TRAIL((s)[i]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
528 /* definitions with backward iteration -------------------------------------- */
531 * Move the string offset from one code point boundary to the previous one
532 * and get the code point between them.
533 * (Pre-decrementing backward iteration.)
534 * "Unsafe" macro, assumes well-formed UTF-16.
536 * The input offset may be the same as the string length.
537 * If the offset is behind a trail surrogate unit
538 * for a supplementary code point, then the macro will read
539 * the preceding lead surrogate as well.
540 * If the offset is behind a lead surrogate, then that itself
541 * will be returned as the code point.
542 * The result is undefined if the offset is behind a single, unpaired trail surrogate.
544 * @param s const UChar * string
545 * @param i string offset
546 * @param c output UChar32 variable
550 #define U16_PREV_UNSAFE(s, i, c) { \
552 if(U16_IS_TRAIL(c)) { \
553 (c)=U16_GET_SUPPLEMENTARY((s)[--(i)], (c)); \
558 * Move the string offset from one code point boundary to the previous one
559 * and get the code point between them.
560 * (Pre-decrementing backward iteration.)
561 * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
563 * The input offset may be the same as the string length.
564 * If the offset is behind a trail surrogate unit
565 * for a supplementary code point, then the macro will read
566 * the preceding lead surrogate as well.
567 * If the offset is behind a lead surrogate or behind a single, unpaired
568 * trail surrogate, then c is set to that unpaired surrogate.
570 * @param s const UChar * string
571 * @param start starting string offset (usually 0)
572 * @param i string offset, must be start<i
573 * @param c output UChar32 variable
574 * @see U16_PREV_UNSAFE
577 #define U16_PREV(s, start, i, c) { \
579 if(U16_IS_TRAIL(c)) { \
581 if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
583 (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
588 #ifndef U_HIDE_DRAFT_API
591 * Move the string offset from one code point boundary to the previous one
592 * and get the code point between them.
593 * (Pre-decrementing backward iteration.)
594 * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
596 * The input offset may be the same as the string length.
597 * If the offset is behind a trail surrogate unit
598 * for a supplementary code point, then the macro will read
599 * the preceding lead surrogate as well.
600 * If the offset is behind a lead surrogate or behind a single, unpaired
601 * trail surrogate, then c is set to U+FFFD.
603 * @param s const UChar * string
604 * @param start starting string offset (usually 0)
605 * @param i string offset, must be start<i
606 * @param c output UChar32 variable
607 * @see U16_PREV_UNSAFE
610 #define U16_PREV_OR_FFFD(s, start, i, c) { \
612 if(U16_IS_SURROGATE(c)) { \
614 if(U16_IS_SURROGATE_TRAIL(c) && (i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
616 (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
623 #endif // U_HIDE_DRAFT_API
626 * Move the string offset from one code point boundary to the previous one.
627 * (Pre-decrementing backward iteration.)
628 * The input offset may be the same as the string length.
629 * "Unsafe" macro, assumes well-formed UTF-16.
631 * @param s const UChar * string
632 * @param i string offset
636 #define U16_BACK_1_UNSAFE(s, i) { \
637 if(U16_IS_TRAIL((s)[--(i)])) { \
643 * Move the string offset from one code point boundary to the previous one.
644 * (Pre-decrementing backward iteration.)
645 * The input offset may be the same as the string length.
646 * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
648 * @param s const UChar * string
649 * @param start starting string offset (usually 0)
650 * @param i string offset, must be start<i
651 * @see U16_BACK_1_UNSAFE
654 #define U16_BACK_1(s, start, i) { \
655 if(U16_IS_TRAIL((s)[--(i)]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
661 * Move the string offset from one code point boundary to the n-th one before it,
662 * i.e., move backward by n code points.
663 * (Pre-decrementing backward iteration.)
664 * The input offset may be the same as the string length.
665 * "Unsafe" macro, assumes well-formed UTF-16.
667 * @param s const UChar * string
668 * @param i string offset
669 * @param n number of code points to skip
673 #define U16_BACK_N_UNSAFE(s, i, n) { \
676 U16_BACK_1_UNSAFE(s, i); \
682 * Move the string offset from one code point boundary to the n-th one before it,
683 * i.e., move backward by n code points.
684 * (Pre-decrementing backward iteration.)
685 * The input offset may be the same as the string length.
686 * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
688 * @param s const UChar * string
689 * @param start start of string
690 * @param i string offset, must be start<i
691 * @param n number of code points to skip
692 * @see U16_BACK_N_UNSAFE
695 #define U16_BACK_N(s, start, i, n) { \
697 while(__N>0 && (i)>(start)) { \
698 U16_BACK_1(s, start, i); \
704 * Adjust a random-access offset to a code point boundary after a code point.
705 * If the offset is behind the lead surrogate of a surrogate pair,
706 * then the offset is incremented.
707 * Otherwise, it is not modified.
708 * The input offset may be the same as the string length.
709 * "Unsafe" macro, assumes well-formed UTF-16.
711 * @param s const UChar * string
712 * @param i string offset
713 * @see U16_SET_CP_LIMIT
716 #define U16_SET_CP_LIMIT_UNSAFE(s, i) { \
717 if(U16_IS_LEAD((s)[(i)-1])) { \
723 * Adjust a random-access offset to a code point boundary after a code point.
724 * If the offset is behind the lead surrogate of a surrogate pair,
725 * then the offset is incremented.
726 * Otherwise, it is not modified.
727 * The input offset may be the same as the string length.
728 * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
730 * The length can be negative for a NUL-terminated string.
732 * @param s const UChar * string
733 * @param start int32_t starting string offset (usually 0)
734 * @param i int32_t string offset, start<=i<=length
735 * @param length int32_t string length
736 * @see U16_SET_CP_LIMIT_UNSAFE
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])) { \