2 *******************************************************************************
4 * Copyright (C) 1999-2013, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
10 * tab size: 8 (not used)
13 * created on: 1999sep13
14 * created by: Markus W. Scherer
19 * \brief C API: 8-bit Unicode handling macros
21 * This file defines macros to deal with 8-bit Unicode (UTF-8) code units (bytes) and strings.
23 * For more information see utf.h and the ICU User Guide Strings chapter
24 * (http://userguide.icu-project.org/strings).
27 * ICU coding guidelines for if() statements should be followed when using these macros.
28 * Compound statements (curly braces {}) must be used for if-else-while...
29 * bodies and all macro statements should be terminated with semicolon.
35 #include "unicode/umachine.h"
37 # include "unicode/utf.h"
40 /* internal definitions ----------------------------------------------------- */
43 * \var utf8_countTrailBytes
44 * Internal array with numbers of trail bytes for any given byte used in
47 * This is internal since it is not meant to be called directly by external clients;
48 * however it is called by public macros in this file and thus must remain stable,
49 * and should not be hidden when other internal functions are hidden (otherwise
50 * public macros would fail to compile).
54 U_EXPORT
const uint8_t
55 #elif defined(U_STATIC_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION)
58 U_CFUNC U_IMPORT
const uint8_t /* U_IMPORT2? */ /*U_IMPORT*/
60 utf8_countTrailBytes
[256];
63 * Counts the trail bytes for a UTF-8 lead byte.
64 * Returns 0 for 0..0xbf as well as for 0xfe and 0xff.
66 * This is internal since it is not meant to be called directly by external clients;
67 * however it is called by public macros in this file and thus must remain stable.
69 * Note: Beginning with ICU 50, the implementation uses a multi-condition expression
70 * which was shown in 2012 (on x86-64) to compile to fast, branch-free code.
71 * leadByte is evaluated multiple times.
73 * The pre-ICU 50 implementation used the exported array utf8_countTrailBytes:
74 * #define U8_COUNT_TRAIL_BYTES(leadByte) (utf8_countTrailBytes[leadByte])
75 * leadByte was evaluated exactly once.
77 * @param leadByte The first byte of a UTF-8 sequence. Must be 0..0xff.
80 #define U8_COUNT_TRAIL_BYTES(leadByte) \
82 ((leadByte)>=0xc0)+((leadByte)>=0xe0) : \
83 (leadByte)<0xfe ? 3+((leadByte)>=0xf8)+((leadByte)>=0xfc) : 0)
86 * Counts the trail bytes for a UTF-8 lead byte of a valid UTF-8 sequence.
87 * The maximum supported lead byte is 0xf4 corresponding to U+10FFFF.
88 * leadByte might be evaluated multiple times.
90 * This is internal since it is not meant to be called directly by external clients;
91 * however it is called by public macros in this file and thus must remain stable.
93 * @param leadByte The first byte of a UTF-8 sequence. Must be 0..0xff.
96 #define U8_COUNT_TRAIL_BYTES_UNSAFE(leadByte) \
97 (((leadByte)>=0xc0)+((leadByte)>=0xe0)+((leadByte)>=0xf0))
100 * Mask a UTF-8 lead byte, leave only the lower bits that form part of the code point value.
102 * This is internal since it is not meant to be called directly by external clients;
103 * however it is called by public macros in this file and thus must remain stable.
106 #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
109 * Function for handling "next code point" with error-checking.
111 * This is internal since it is not meant to be called directly by external clients;
112 * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
113 * file and thus must remain stable, and should not be hidden when other internal
114 * functions are hidden (otherwise public macros would fail to compile).
117 U_STABLE UChar32 U_EXPORT2
118 utf8_nextCharSafeBody(const uint8_t *s
, int32_t *pi
, int32_t length
, UChar32 c
, UBool strict
);
121 * Function for handling "append code point" with error-checking.
123 * This is internal since it is not meant to be called directly by external clients;
124 * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
125 * file and thus must remain stable, and should not be hidden when other internal
126 * functions are hidden (otherwise public macros would fail to compile).
129 U_STABLE
int32_t U_EXPORT2
130 utf8_appendCharSafeBody(uint8_t *s
, int32_t i
, int32_t length
, UChar32 c
, UBool
*pIsError
);
133 * Function for handling "previous code point" with error-checking.
135 * This is internal since it is not meant to be called directly by external clients;
136 * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
137 * file and thus must remain stable, and should not be hidden when other internal
138 * functions are hidden (otherwise public macros would fail to compile).
141 U_STABLE UChar32 U_EXPORT2
142 utf8_prevCharSafeBody(const uint8_t *s
, int32_t start
, int32_t *pi
, UChar32 c
, UBool strict
);
145 * Function for handling "skip backward one code point" with error-checking.
147 * This is internal since it is not meant to be called directly by external clients;
148 * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
149 * file and thus must remain stable, and should not be hidden when other internal
150 * functions are hidden (otherwise public macros would fail to compile).
153 U_STABLE
int32_t U_EXPORT2
154 utf8_back1SafeBody(const uint8_t *s
, int32_t start
, int32_t i
);
156 /* single-code point definitions -------------------------------------------- */
159 * Does this code unit (byte) encode a code point by itself (US-ASCII 0..0x7f)?
160 * @param c 8-bit code unit (byte)
161 * @return TRUE or FALSE
164 #define U8_IS_SINGLE(c) (((c)&0x80)==0)
167 * Is this code unit (byte) a UTF-8 lead byte?
168 * @param c 8-bit code unit (byte)
169 * @return TRUE or FALSE
172 #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc0)<0x3e)
175 * Is this code unit (byte) a UTF-8 trail byte?
176 * @param c 8-bit code unit (byte)
177 * @return TRUE or FALSE
180 #define U8_IS_TRAIL(c) (((c)&0xc0)==0x80)
183 * How many code units (bytes) are used for the UTF-8 encoding
184 * of this Unicode code point?
185 * @param c 32-bit code point
186 * @return 1..4, or 0 if c is a surrogate or not a Unicode code point
189 #define U8_LENGTH(c) \
190 ((uint32_t)(c)<=0x7f ? 1 : \
191 ((uint32_t)(c)<=0x7ff ? 2 : \
192 ((uint32_t)(c)<=0xd7ff ? 3 : \
193 ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
194 ((uint32_t)(c)<=0xffff ? 3 : 4)\
201 * The maximum number of UTF-8 code units (bytes) per Unicode code point (U+0000..U+10ffff).
205 #define U8_MAX_LENGTH 4
208 * Get a code point from a string at a random-access offset,
209 * without changing the offset.
210 * The offset may point to either the lead byte or one of the trail bytes
211 * for a code point, in which case the macro will read all of the bytes
212 * for the code point.
213 * The result is undefined if the offset points to an illegal UTF-8
215 * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
217 * @param s const uint8_t * string
218 * @param i string offset
219 * @param c output UChar32 variable
223 #define U8_GET_UNSAFE(s, i, c) { \
224 int32_t _u8_get_unsafe_index=(int32_t)(i); \
225 U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \
226 U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \
230 * Get a code point from a string at a random-access offset,
231 * without changing the offset.
232 * The offset may point to either the lead byte or one of the trail bytes
233 * for a code point, in which case the macro will read all of the bytes
234 * for the code point.
236 * The length can be negative for a NUL-terminated string.
238 * If the offset points to an illegal UTF-8 byte sequence, then
239 * c is set to a negative value.
240 * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
242 * @param s const uint8_t * string
243 * @param start int32_t starting string offset
244 * @param i int32_t string offset, must be start<=i<length
245 * @param length int32_t string length
246 * @param c output UChar32 variable, set to <0 in case of an error
250 #define U8_GET(s, start, i, length, c) { \
251 int32_t _u8_get_index=(i); \
252 U8_SET_CP_START(s, start, _u8_get_index); \
253 U8_NEXT(s, _u8_get_index, length, c); \
256 #ifndef U_HIDE_DRAFT_API
258 * Get a code point from a string at a random-access offset,
259 * without changing the offset.
260 * The offset may point to either the lead byte or one of the trail bytes
261 * for a code point, in which case the macro will read all of the bytes
262 * for the code point.
264 * The length can be negative for a NUL-terminated string.
266 * If the offset points to an illegal UTF-8 byte sequence, then
267 * c is set to U+FFFD.
268 * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT_OR_FFFD.
270 * This macro does not distinguish between a real U+FFFD in the text
271 * and U+FFFD returned for an ill-formed sequence.
272 * Use U8_GET() if that distinction is important.
274 * @param s const uint8_t * string
275 * @param start int32_t starting string offset
276 * @param i int32_t string offset, must be start<=i<length
277 * @param length int32_t string length
278 * @param c output UChar32 variable, set to U+FFFD in case of an error
282 #define U8_GET_OR_FFFD(s, start, i, length, c) { \
283 int32_t _u8_get_index=(i); \
284 U8_SET_CP_START(s, start, _u8_get_index); \
285 U8_NEXT_OR_FFFD(s, _u8_get_index, length, c); \
287 #endif /* U_HIDE_DRAFT_API */
289 /* definitions with forward iteration --------------------------------------- */
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 * "Unsafe" macro, assumes well-formed UTF-8.
297 * The offset may point to the lead byte of a multi-byte sequence,
298 * in which case the macro will read the whole sequence.
299 * The result is undefined if the offset points to a trail byte
300 * or an illegal UTF-8 sequence.
302 * @param s const uint8_t * string
303 * @param i string offset
304 * @param c output UChar32 variable
308 #define U8_NEXT_UNSAFE(s, i, c) { \
309 (c)=(uint8_t)(s)[(i)++]; \
312 (c)=(((c)&0x1f)<<6)|((s)[(i)++]&0x3f); \
313 } else if((c)<0xf0) { \
314 /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
315 (c)=(UChar)(((c)<<12)|(((s)[i]&0x3f)<<6)|((s)[(i)+1]&0x3f)); \
318 (c)=(((c)&7)<<18)|(((s)[i]&0x3f)<<12)|(((s)[(i)+1]&0x3f)<<6)|((s)[(i)+2]&0x3f); \
325 * Get a code point from a string at a code point boundary offset,
326 * and advance the offset to the next code point boundary.
327 * (Post-incrementing forward iteration.)
328 * "Safe" macro, checks for illegal sequences and for string boundaries.
330 * The length can be negative for a NUL-terminated string.
332 * The offset may point to the lead byte of a multi-byte sequence,
333 * in which case the macro will read the whole sequence.
334 * If the offset points to a trail byte or an illegal UTF-8 sequence, then
335 * c is set to a negative value.
337 * @param s const uint8_t * string
338 * @param i int32_t string offset, must be i<length
339 * @param length int32_t string length
340 * @param c output UChar32 variable, set to <0 in case of an error
341 * @see U8_NEXT_UNSAFE
344 #define U8_NEXT(s, i, length, c) { \
345 (c)=(uint8_t)(s)[(i)++]; \
347 uint8_t __t1, __t2; \
348 if( /* handle U+1000..U+CFFF inline */ \
349 (0xe0<(c) && (c)<=0xec) && \
350 (((i)+1)<(length) || (length)<0) && \
351 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
352 (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
354 /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
355 (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
357 } else if( /* handle U+0080..U+07FF inline */ \
358 ((c)<0xe0 && (c)>=0xc2) && \
360 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
362 (c)=(((c)&0x1f)<<6)|__t1; \
365 /* function call for "complicated" and error cases */ \
366 (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (length), c, -1); \
371 #ifndef U_HIDE_DRAFT_API
373 * Get a code point from a string at a code point boundary offset,
374 * and advance the offset to the next code point boundary.
375 * (Post-incrementing forward iteration.)
376 * "Safe" macro, checks for illegal sequences and for string boundaries.
378 * The length can be negative for a NUL-terminated string.
380 * The offset may point to the lead byte of a multi-byte sequence,
381 * in which case the macro will read the whole sequence.
382 * If the offset points to a trail byte or an illegal UTF-8 sequence, then
383 * c is set to U+FFFD.
385 * This macro does not distinguish between a real U+FFFD in the text
386 * and U+FFFD returned for an ill-formed sequence.
387 * Use U8_NEXT() if that distinction is important.
389 * @param s const uint8_t * string
390 * @param i int32_t string offset, must be i<length
391 * @param length int32_t string length
392 * @param c output UChar32 variable, set to U+FFFD in case of an error
396 #define U8_NEXT_OR_FFFD(s, i, length, c) { \
397 (c)=(uint8_t)(s)[(i)++]; \
399 uint8_t __t1, __t2; \
400 if( /* handle U+1000..U+CFFF inline */ \
401 (0xe0<(c) && (c)<=0xec) && \
402 (((i)+1)<(length) || (length)<0) && \
403 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
404 (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
406 /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
407 (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
409 } else if( /* handle U+0080..U+07FF inline */ \
410 ((c)<0xe0 && (c)>=0xc2) && \
412 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
414 (c)=(((c)&0x1f)<<6)|__t1; \
417 /* function call for "complicated" and error cases */ \
418 (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (length), c, -3); \
422 #endif /* U_HIDE_DRAFT_API */
425 * Append a code point to a string, overwriting 1 to 4 bytes.
426 * The offset points to the current end of the string contents
427 * and is advanced (post-increment).
428 * "Unsafe" macro, assumes a valid code point and sufficient space in the string.
429 * Otherwise, the result is undefined.
431 * @param s const uint8_t * string buffer
432 * @param i string offset
433 * @param c code point to append
437 #define U8_APPEND_UNSAFE(s, i, c) { \
438 if((uint32_t)(c)<=0x7f) { \
439 (s)[(i)++]=(uint8_t)(c); \
441 if((uint32_t)(c)<=0x7ff) { \
442 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
444 if((uint32_t)(c)<=0xffff) { \
445 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
447 (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
448 (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
450 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
452 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
457 * Append a code point to a string, overwriting 1 to 4 bytes.
458 * The offset points to the current end of the string contents
459 * and is advanced (post-increment).
460 * "Safe" macro, checks for a valid code point.
461 * If a non-ASCII code point is written, checks for sufficient space in the string.
462 * If the code point is not valid or trail bytes do not fit,
463 * then isError is set to TRUE.
465 * @param s const uint8_t * string buffer
466 * @param i int32_t string offset, must be i<capacity
467 * @param capacity int32_t size of the string buffer
468 * @param c UChar32 code point to append
469 * @param isError output UBool set to TRUE if an error occurs, otherwise not modified
470 * @see U8_APPEND_UNSAFE
473 #define U8_APPEND(s, i, capacity, c, isError) { \
474 if((uint32_t)(c)<=0x7f) { \
475 (s)[(i)++]=(uint8_t)(c); \
476 } else if((uint32_t)(c)<=0x7ff && (i)+1<(capacity)) { \
477 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
478 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
479 } else if((uint32_t)(c)<=0xd7ff && (i)+2<(capacity)) { \
480 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
481 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
482 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
484 (i)=utf8_appendCharSafeBody(s, (i), (capacity), c, &(isError)); \
489 * Advance the string offset from one code point boundary to the next.
490 * (Post-incrementing iteration.)
491 * "Unsafe" macro, assumes well-formed UTF-8.
493 * @param s const uint8_t * string
494 * @param i string offset
498 #define U8_FWD_1_UNSAFE(s, i) { \
499 (i)+=1+U8_COUNT_TRAIL_BYTES_UNSAFE((uint8_t)(s)[i]); \
503 * Advance the string offset from one code point boundary to the next.
504 * (Post-incrementing iteration.)
505 * "Safe" macro, checks for illegal sequences and for string boundaries.
507 * The length can be negative for a NUL-terminated string.
509 * @param s const uint8_t * string
510 * @param i int32_t string offset, must be i<length
511 * @param length int32_t string length
512 * @see U8_FWD_1_UNSAFE
515 #define U8_FWD_1(s, i, length) { \
516 uint8_t __b=(uint8_t)(s)[(i)++]; \
517 if(U8_IS_LEAD(__b)) { \
518 uint8_t __count=U8_COUNT_TRAIL_BYTES(__b); \
519 if((i)+__count>(length) && (length)>=0) { \
520 __count=(uint8_t)((length)-(i)); \
522 while(__count>0 && U8_IS_TRAIL((s)[i])) { \
530 * Advance the string offset from one code point boundary to the n-th next one,
531 * i.e., move forward by n code points.
532 * (Post-incrementing iteration.)
533 * "Unsafe" macro, assumes well-formed UTF-8.
535 * @param s const uint8_t * string
536 * @param i string offset
537 * @param n number of code points to skip
541 #define U8_FWD_N_UNSAFE(s, i, n) { \
544 U8_FWD_1_UNSAFE(s, i); \
550 * Advance the string offset from one code point boundary to the n-th next one,
551 * i.e., move forward by n code points.
552 * (Post-incrementing iteration.)
553 * "Safe" macro, checks for illegal sequences and for string boundaries.
555 * The length can be negative for a NUL-terminated string.
557 * @param s const uint8_t * string
558 * @param i int32_t string offset, must be i<length
559 * @param length int32_t string length
560 * @param n number of code points to skip
561 * @see U8_FWD_N_UNSAFE
564 #define U8_FWD_N(s, i, length, n) { \
566 while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
567 U8_FWD_1(s, i, length); \
573 * Adjust a random-access offset to a code point boundary
574 * at the start of a code point.
575 * If the offset points to a UTF-8 trail byte,
576 * then the offset is moved backward to the corresponding lead byte.
577 * Otherwise, it is not modified.
578 * "Unsafe" macro, assumes well-formed UTF-8.
580 * @param s const uint8_t * string
581 * @param i string offset
582 * @see U8_SET_CP_START
585 #define U8_SET_CP_START_UNSAFE(s, i) { \
586 while(U8_IS_TRAIL((s)[i])) { --(i); } \
590 * Adjust a random-access offset to a code point boundary
591 * at the start of a code point.
592 * If the offset points to a UTF-8 trail byte,
593 * then the offset is moved backward to the corresponding lead byte.
594 * Otherwise, it is not modified.
595 * "Safe" macro, checks for illegal sequences and for string boundaries.
597 * @param s const uint8_t * string
598 * @param start int32_t starting string offset (usually 0)
599 * @param i int32_t string offset, must be start<=i
600 * @see U8_SET_CP_START_UNSAFE
603 #define U8_SET_CP_START(s, start, i) { \
604 if(U8_IS_TRAIL((s)[(i)])) { \
605 (i)=utf8_back1SafeBody(s, start, (i)); \
609 /* definitions with backward iteration -------------------------------------- */
612 * Move the string offset from one code point boundary to the previous one
613 * and get the code point between them.
614 * (Pre-decrementing backward iteration.)
615 * "Unsafe" macro, assumes well-formed UTF-8.
617 * The input offset may be the same as the string length.
618 * If the offset is behind a multi-byte sequence, then the macro will read
619 * the whole sequence.
620 * If the offset is behind a lead byte, then that itself
621 * will be returned as the code point.
622 * The result is undefined if the offset is behind an illegal UTF-8 sequence.
624 * @param s const uint8_t * string
625 * @param i string offset
626 * @param c output UChar32 variable
630 #define U8_PREV_UNSAFE(s, i, c) { \
631 (c)=(uint8_t)(s)[--(i)]; \
632 if(U8_IS_TRAIL(c)) { \
633 uint8_t __b, __count=1, __shift=6; \
635 /* c is a trail byte */ \
638 __b=(uint8_t)(s)[--(i)]; \
640 U8_MASK_LEAD_BYTE(__b, __count); \
641 (c)|=(UChar32)__b<<__shift; \
644 (c)|=(UChar32)(__b&0x3f)<<__shift; \
653 * Move the string offset from one code point boundary to the previous one
654 * and get the code point between them.
655 * (Pre-decrementing backward iteration.)
656 * "Safe" macro, checks for illegal sequences and for string boundaries.
658 * The input offset may be the same as the string length.
659 * If the offset is behind a multi-byte sequence, then the macro will read
660 * the whole sequence.
661 * If the offset is behind a lead byte, then that itself
662 * will be returned as the code point.
663 * If the offset is behind an illegal UTF-8 sequence, then c is set to a negative value.
665 * @param s const uint8_t * string
666 * @param start int32_t starting string offset (usually 0)
667 * @param i int32_t string offset, must be start<i
668 * @param c output UChar32 variable, set to <0 in case of an error
669 * @see U8_PREV_UNSAFE
672 #define U8_PREV(s, start, i, c) { \
673 (c)=(uint8_t)(s)[--(i)]; \
675 (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
679 #ifndef U_HIDE_DRAFT_API
681 * Move the string offset from one code point boundary to the previous one
682 * and get the code point between them.
683 * (Pre-decrementing backward iteration.)
684 * "Safe" macro, checks for illegal sequences and for string boundaries.
686 * The input offset may be the same as the string length.
687 * If the offset is behind a multi-byte sequence, then the macro will read
688 * the whole sequence.
689 * If the offset is behind a lead byte, then that itself
690 * will be returned as the code point.
691 * If the offset is behind an illegal UTF-8 sequence, then c is set to U+FFFD.
693 * This macro does not distinguish between a real U+FFFD in the text
694 * and U+FFFD returned for an ill-formed sequence.
695 * Use U8_PREV() if that distinction is important.
697 * @param s const uint8_t * string
698 * @param start int32_t starting string offset (usually 0)
699 * @param i int32_t string offset, must be start<i
700 * @param c output UChar32 variable, set to U+FFFD in case of an error
704 #define U8_PREV_OR_FFFD(s, start, i, c) { \
705 (c)=(uint8_t)(s)[--(i)]; \
707 (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -3); \
710 #endif /* U_HIDE_DRAFT_API */
713 * Move the string offset from one code point boundary to the previous one.
714 * (Pre-decrementing backward iteration.)
715 * The input offset may be the same as the string length.
716 * "Unsafe" macro, assumes well-formed UTF-8.
718 * @param s const uint8_t * string
719 * @param i string offset
723 #define U8_BACK_1_UNSAFE(s, i) { \
724 while(U8_IS_TRAIL((s)[--(i)])) {} \
728 * Move the string offset from one code point boundary to the previous one.
729 * (Pre-decrementing backward iteration.)
730 * The input offset may be the same as the string length.
731 * "Safe" macro, checks for illegal sequences and for string boundaries.
733 * @param s const uint8_t * string
734 * @param start int32_t starting string offset (usually 0)
735 * @param i int32_t string offset, must be start<i
736 * @see U8_BACK_1_UNSAFE
739 #define U8_BACK_1(s, start, i) { \
740 if(U8_IS_TRAIL((s)[--(i)])) { \
741 (i)=utf8_back1SafeBody(s, start, (i)); \
746 * Move the string offset from one code point boundary to the n-th one before it,
747 * i.e., move backward by n code points.
748 * (Pre-decrementing backward iteration.)
749 * The input offset may be the same as the string length.
750 * "Unsafe" macro, assumes well-formed UTF-8.
752 * @param s const uint8_t * string
753 * @param i string offset
754 * @param n number of code points to skip
758 #define U8_BACK_N_UNSAFE(s, i, n) { \
761 U8_BACK_1_UNSAFE(s, i); \
767 * Move the string offset from one code point boundary to the n-th one before it,
768 * i.e., move backward by n code points.
769 * (Pre-decrementing backward iteration.)
770 * The input offset may be the same as the string length.
771 * "Safe" macro, checks for illegal sequences and for string boundaries.
773 * @param s const uint8_t * string
774 * @param start int32_t index of the start of the string
775 * @param i int32_t string offset, must be start<i
776 * @param n number of code points to skip
777 * @see U8_BACK_N_UNSAFE
780 #define U8_BACK_N(s, start, i, n) { \
782 while(__N>0 && (i)>(start)) { \
783 U8_BACK_1(s, start, i); \
789 * Adjust a random-access offset to a code point boundary after a code point.
790 * If the offset is behind a partial multi-byte sequence,
791 * then the offset is incremented to behind the whole sequence.
792 * Otherwise, it is not modified.
793 * The input offset may be the same as the string length.
794 * "Unsafe" macro, assumes well-formed UTF-8.
796 * @param s const uint8_t * string
797 * @param i string offset
798 * @see U8_SET_CP_LIMIT
801 #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \
802 U8_BACK_1_UNSAFE(s, i); \
803 U8_FWD_1_UNSAFE(s, i); \
807 * Adjust a random-access offset to a code point boundary after a code point.
808 * If the offset is behind a partial multi-byte sequence,
809 * then the offset is incremented to behind the whole sequence.
810 * Otherwise, it is not modified.
811 * The input offset may be the same as the string length.
812 * "Safe" macro, checks for illegal sequences and for string boundaries.
814 * The length can be negative for a NUL-terminated string.
816 * @param s const uint8_t * string
817 * @param start int32_t starting string offset (usually 0)
818 * @param i int32_t string offset, must be start<=i<=length
819 * @param length int32_t string length
820 * @see U8_SET_CP_LIMIT_UNSAFE
823 #define U8_SET_CP_LIMIT(s, start, i, length) { \
824 if((start)<(i) && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
825 U8_BACK_1(s, start, i); \
826 U8_FWD_1(s, i, length); \