1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 1999-2015, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
12 * tab size: 8 (not used)
15 * created on: 1999sep13
16 * created by: Markus W. Scherer
21 * \brief C API: 8-bit Unicode handling macros
23 * This file defines macros to deal with 8-bit Unicode (UTF-8) code units (bytes) 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 /* internal definitions ----------------------------------------------------- */
45 * \var utf8_countTrailBytes
46 * Internal array with numbers of trail bytes for any given byte used in
49 * This is internal since it is not meant to be called directly by external clients;
50 * however it is called by public macros in this file and thus must remain stable,
51 * and should not be hidden when other internal functions are hidden (otherwise
52 * public macros would fail to compile).
56 U_EXPORT const uint8_t
57 #elif defined(U_STATIC_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION)
60 U_CFUNC U_IMPORT const uint8_t /* U_IMPORT2? */ /*U_IMPORT*/
62 utf8_countTrailBytes[256];
65 * Counts the trail bytes for a UTF-8 lead byte.
66 * Returns 0 for 0..0xbf as well as for 0xfe and 0xff.
68 * This is internal since it is not meant to be called directly by external clients;
69 * however it is called by public macros in this file and thus must remain stable.
71 * Note: Beginning with ICU 50, the implementation uses a multi-condition expression
72 * which was shown in 2012 (on x86-64) to compile to fast, branch-free code.
73 * leadByte is evaluated multiple times.
75 * The pre-ICU 50 implementation used the exported array utf8_countTrailBytes:
76 * #define U8_COUNT_TRAIL_BYTES(leadByte) (utf8_countTrailBytes[leadByte])
77 * leadByte was evaluated exactly once.
79 * @param leadByte The first byte of a UTF-8 sequence. Must be 0..0xff.
82 #define U8_COUNT_TRAIL_BYTES(leadByte) \
83 ((uint8_t)(leadByte)<0xf0 ? \
84 ((uint8_t)(leadByte)>=0xc0)+((uint8_t)(leadByte)>=0xe0) : \
85 (uint8_t)(leadByte)<0xfe ? 3+((uint8_t)(leadByte)>=0xf8)+((uint8_t)(leadByte)>=0xfc) : 0)
88 * Counts the trail bytes for a UTF-8 lead byte of a valid UTF-8 sequence.
89 * The maximum supported lead byte is 0xf4 corresponding to U+10FFFF.
90 * leadByte might be evaluated multiple times.
92 * This is internal since it is not meant to be called directly by external clients;
93 * however it is called by public macros in this file and thus must remain stable.
95 * @param leadByte The first byte of a UTF-8 sequence. Must be 0..0xff.
98 #define U8_COUNT_TRAIL_BYTES_UNSAFE(leadByte) \
99 (((leadByte)>=0xc0)+((leadByte)>=0xe0)+((leadByte)>=0xf0))
102 * Mask a UTF-8 lead byte, leave only the lower bits that form part of the code point value.
104 * This is internal since it is not meant to be called directly by external clients;
105 * however it is called by public macros in this file and thus must remain stable.
108 #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
111 * Function for handling "next code point" with error-checking.
113 * This is internal since it is not meant to be called directly by external clients;
114 * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
115 * file and thus must remain stable, and should not be hidden when other internal
116 * functions are hidden (otherwise public macros would fail to compile).
119 U_STABLE UChar32 U_EXPORT2
120 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict);
123 * Function for handling "append code point" with error-checking.
125 * This is internal since it is not meant to be called directly by external clients;
126 * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
127 * file and thus must remain stable, and should not be hidden when other internal
128 * functions are hidden (otherwise public macros would fail to compile).
131 U_STABLE int32_t U_EXPORT2
132 utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError);
135 * Function for handling "previous code point" with error-checking.
137 * This is internal since it is not meant to be called directly by external clients;
138 * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
139 * file and thus must remain stable, and should not be hidden when other internal
140 * functions are hidden (otherwise public macros would fail to compile).
143 U_STABLE UChar32 U_EXPORT2
144 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict);
147 * Function for handling "skip backward one code point" with error-checking.
149 * This is internal since it is not meant to be called directly by external clients;
150 * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
151 * file and thus must remain stable, and should not be hidden when other internal
152 * functions are hidden (otherwise public macros would fail to compile).
155 U_STABLE int32_t U_EXPORT2
156 utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i);
158 /* single-code point definitions -------------------------------------------- */
161 * Does this code unit (byte) encode a code point by itself (US-ASCII 0..0x7f)?
162 * @param c 8-bit code unit (byte)
163 * @return TRUE or FALSE
166 #define U8_IS_SINGLE(c) (((c)&0x80)==0)
169 * Is this code unit (byte) a UTF-8 lead byte?
170 * @param c 8-bit code unit (byte)
171 * @return TRUE or FALSE
174 #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc0)<0x3e)
177 * Is this code unit (byte) a UTF-8 trail byte?
178 * @param c 8-bit code unit (byte)
179 * @return TRUE or FALSE
182 #define U8_IS_TRAIL(c) (((c)&0xc0)==0x80)
185 * How many code units (bytes) are used for the UTF-8 encoding
186 * of this Unicode code point?
187 * @param c 32-bit code point
188 * @return 1..4, or 0 if c is a surrogate or not a Unicode code point
191 #define U8_LENGTH(c) \
192 ((uint32_t)(c)<=0x7f ? 1 : \
193 ((uint32_t)(c)<=0x7ff ? 2 : \
194 ((uint32_t)(c)<=0xd7ff ? 3 : \
195 ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
196 ((uint32_t)(c)<=0xffff ? 3 : 4)\
203 * The maximum number of UTF-8 code units (bytes) per Unicode code point (U+0000..U+10ffff).
207 #define U8_MAX_LENGTH 4
210 * Get a code point from a string at a random-access offset,
211 * without changing the offset.
212 * The offset may point to either the lead byte or one of the trail bytes
213 * for a code point, in which case the macro will read all of the bytes
214 * for the code point.
215 * The result is undefined if the offset points to an illegal UTF-8
217 * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
219 * @param s const uint8_t * string
220 * @param i string offset
221 * @param c output UChar32 variable
225 #define U8_GET_UNSAFE(s, i, c) { \
226 int32_t _u8_get_unsafe_index=(int32_t)(i); \
227 U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \
228 U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \
232 * Get a code point from a string at a random-access offset,
233 * without changing the offset.
234 * The offset may point to either the lead byte or one of the trail bytes
235 * for a code point, in which case the macro will read all of the bytes
236 * for the code point.
238 * The length can be negative for a NUL-terminated string.
240 * If the offset points to an illegal UTF-8 byte sequence, then
241 * c is set to a negative value.
242 * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
244 * @param s const uint8_t * string
245 * @param start int32_t starting string offset
246 * @param i int32_t string offset, must be start<=i<length
247 * @param length int32_t string length
248 * @param c output UChar32 variable, set to <0 in case of an error
252 #define U8_GET(s, start, i, length, c) { \
253 int32_t _u8_get_index=(i); \
254 U8_SET_CP_START(s, start, _u8_get_index); \
255 U8_NEXT(s, _u8_get_index, length, c); \
259 * Get a code point from a string at a random-access offset,
260 * without changing the offset.
261 * The offset may point to either the lead byte or one of the trail bytes
262 * for a code point, in which case the macro will read all of the bytes
263 * for the code point.
265 * The length can be negative for a NUL-terminated string.
267 * If the offset points to an illegal UTF-8 byte sequence, then
268 * c is set to U+FFFD.
269 * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT_OR_FFFD.
271 * This macro does not distinguish between a real U+FFFD in the text
272 * and U+FFFD returned for an ill-formed sequence.
273 * Use U8_GET() if that distinction is important.
275 * @param s const uint8_t * string
276 * @param start int32_t starting string offset
277 * @param i int32_t string offset, must be start<=i<length
278 * @param length int32_t string length
279 * @param c output UChar32 variable, set to U+FFFD in case of an error
283 #define U8_GET_OR_FFFD(s, start, i, length, c) { \
284 int32_t _u8_get_index=(i); \
285 U8_SET_CP_START(s, start, _u8_get_index); \
286 U8_NEXT_OR_FFFD(s, _u8_get_index, length, c); \
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); \
372 * Get a code point from a string at a code point boundary offset,
373 * and advance the offset to the next code point boundary.
374 * (Post-incrementing forward iteration.)
375 * "Safe" macro, checks for illegal sequences and for string boundaries.
377 * The length can be negative for a NUL-terminated string.
379 * The offset may point to the lead byte of a multi-byte sequence,
380 * in which case the macro will read the whole sequence.
381 * If the offset points to a trail byte or an illegal UTF-8 sequence, then
382 * c is set to U+FFFD.
384 * This macro does not distinguish between a real U+FFFD in the text
385 * and U+FFFD returned for an ill-formed sequence.
386 * Use U8_NEXT() if that distinction is important.
388 * @param s const uint8_t * string
389 * @param i int32_t string offset, must be i<length
390 * @param length int32_t string length
391 * @param c output UChar32 variable, set to U+FFFD in case of an error
395 #define U8_NEXT_OR_FFFD(s, i, length, c) { \
396 (c)=(uint8_t)(s)[(i)++]; \
398 uint8_t __t1, __t2; \
399 if( /* handle U+1000..U+CFFF inline */ \
400 (0xe0<(c) && (c)<=0xec) && \
401 (((i)+1)<(length) || (length)<0) && \
402 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
403 (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
405 /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
406 (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
408 } else if( /* handle U+0080..U+07FF inline */ \
409 ((c)<0xe0 && (c)>=0xc2) && \
411 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
413 (c)=(((c)&0x1f)<<6)|__t1; \
416 /* function call for "complicated" and error cases */ \
417 (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (length), c, -3); \
423 * Append a code point to a string, overwriting 1 to 4 bytes.
424 * The offset points to the current end of the string contents
425 * and is advanced (post-increment).
426 * "Unsafe" macro, assumes a valid code point and sufficient space in the string.
427 * Otherwise, the result is undefined.
429 * @param s const uint8_t * string buffer
430 * @param i string offset
431 * @param c code point to append
435 #define U8_APPEND_UNSAFE(s, i, c) { \
436 if((uint32_t)(c)<=0x7f) { \
437 (s)[(i)++]=(uint8_t)(c); \
439 if((uint32_t)(c)<=0x7ff) { \
440 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
442 if((uint32_t)(c)<=0xffff) { \
443 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
445 (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
446 (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
448 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
450 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
455 * Append a code point to a string, overwriting 1 to 4 bytes.
456 * The offset points to the current end of the string contents
457 * and is advanced (post-increment).
458 * "Safe" macro, checks for a valid code point.
459 * If a non-ASCII code point is written, checks for sufficient space in the string.
460 * If the code point is not valid or trail bytes do not fit,
461 * then isError is set to TRUE.
463 * @param s const uint8_t * string buffer
464 * @param i int32_t string offset, must be i<capacity
465 * @param capacity int32_t size of the string buffer
466 * @param c UChar32 code point to append
467 * @param isError output UBool set to TRUE if an error occurs, otherwise not modified
468 * @see U8_APPEND_UNSAFE
471 #define U8_APPEND(s, i, capacity, c, isError) { \
472 if((uint32_t)(c)<=0x7f) { \
473 (s)[(i)++]=(uint8_t)(c); \
474 } else if((uint32_t)(c)<=0x7ff && (i)+1<(capacity)) { \
475 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
476 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
477 } else if((uint32_t)(c)<=0xd7ff && (i)+2<(capacity)) { \
478 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
479 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
480 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
482 (i)=utf8_appendCharSafeBody(s, (i), (capacity), c, &(isError)); \
487 * Advance the string offset from one code point boundary to the next.
488 * (Post-incrementing iteration.)
489 * "Unsafe" macro, assumes well-formed UTF-8.
491 * @param s const uint8_t * string
492 * @param i string offset
496 #define U8_FWD_1_UNSAFE(s, i) { \
497 (i)+=1+U8_COUNT_TRAIL_BYTES_UNSAFE((uint8_t)(s)[i]); \
501 * Advance the string offset from one code point boundary to the next.
502 * (Post-incrementing iteration.)
503 * "Safe" macro, checks for illegal sequences and for string boundaries.
505 * The length can be negative for a NUL-terminated string.
507 * @param s const uint8_t * string
508 * @param i int32_t string offset, must be i<length
509 * @param length int32_t string length
510 * @see U8_FWD_1_UNSAFE
513 #define U8_FWD_1(s, i, length) { \
514 uint8_t __b=(uint8_t)(s)[(i)++]; \
515 if(U8_IS_LEAD(__b)) { \
516 uint8_t __count=U8_COUNT_TRAIL_BYTES(__b); \
517 if((i)+__count>(length) && (length)>=0) { \
518 __count=(uint8_t)((length)-(i)); \
520 while(__count>0 && U8_IS_TRAIL((s)[i])) { \
528 * Advance the string offset from one code point boundary to the n-th next one,
529 * i.e., move forward by n code points.
530 * (Post-incrementing iteration.)
531 * "Unsafe" macro, assumes well-formed UTF-8.
533 * @param s const uint8_t * string
534 * @param i string offset
535 * @param n number of code points to skip
539 #define U8_FWD_N_UNSAFE(s, i, n) { \
542 U8_FWD_1_UNSAFE(s, i); \
548 * Advance the string offset from one code point boundary to the n-th next one,
549 * i.e., move forward by n code points.
550 * (Post-incrementing iteration.)
551 * "Safe" macro, checks for illegal sequences and for string boundaries.
553 * The length can be negative for a NUL-terminated string.
555 * @param s const uint8_t * string
556 * @param i int32_t string offset, must be i<length
557 * @param length int32_t string length
558 * @param n number of code points to skip
559 * @see U8_FWD_N_UNSAFE
562 #define U8_FWD_N(s, i, length, n) { \
564 while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
565 U8_FWD_1(s, i, length); \
571 * Adjust a random-access offset to a code point boundary
572 * at the start of a code point.
573 * If the offset points to a UTF-8 trail byte,
574 * then the offset is moved backward to the corresponding lead byte.
575 * Otherwise, it is not modified.
576 * "Unsafe" macro, assumes well-formed UTF-8.
578 * @param s const uint8_t * string
579 * @param i string offset
580 * @see U8_SET_CP_START
583 #define U8_SET_CP_START_UNSAFE(s, i) { \
584 while(U8_IS_TRAIL((s)[i])) { --(i); } \
588 * Adjust a random-access offset to a code point boundary
589 * at the start of a code point.
590 * If the offset points to a UTF-8 trail byte,
591 * then the offset is moved backward to the corresponding lead byte.
592 * Otherwise, it is not modified.
593 * "Safe" macro, checks for illegal sequences and for string boundaries.
595 * @param s const uint8_t * string
596 * @param start int32_t starting string offset (usually 0)
597 * @param i int32_t string offset, must be start<=i
598 * @see U8_SET_CP_START_UNSAFE
601 #define U8_SET_CP_START(s, start, i) { \
602 if(U8_IS_TRAIL((s)[(i)])) { \
603 (i)=utf8_back1SafeBody(s, start, (i)); \
607 /* definitions with backward iteration -------------------------------------- */
610 * Move the string offset from one code point boundary to the previous one
611 * and get the code point between them.
612 * (Pre-decrementing backward iteration.)
613 * "Unsafe" macro, assumes well-formed UTF-8.
615 * The input offset may be the same as the string length.
616 * If the offset is behind a multi-byte sequence, then the macro will read
617 * the whole sequence.
618 * If the offset is behind a lead byte, then that itself
619 * will be returned as the code point.
620 * The result is undefined if the offset is behind an illegal UTF-8 sequence.
622 * @param s const uint8_t * string
623 * @param i string offset
624 * @param c output UChar32 variable
628 #define U8_PREV_UNSAFE(s, i, c) { \
629 (c)=(uint8_t)(s)[--(i)]; \
630 if(U8_IS_TRAIL(c)) { \
631 uint8_t __b, __count=1, __shift=6; \
633 /* c is a trail byte */ \
636 __b=(uint8_t)(s)[--(i)]; \
638 U8_MASK_LEAD_BYTE(__b, __count); \
639 (c)|=(UChar32)__b<<__shift; \
642 (c)|=(UChar32)(__b&0x3f)<<__shift; \
651 * Move the string offset from one code point boundary to the previous one
652 * and get the code point between them.
653 * (Pre-decrementing backward iteration.)
654 * "Safe" macro, checks for illegal sequences and for string boundaries.
656 * The input offset may be the same as the string length.
657 * If the offset is behind a multi-byte sequence, then the macro will read
658 * the whole sequence.
659 * If the offset is behind a lead byte, then that itself
660 * will be returned as the code point.
661 * If the offset is behind an illegal UTF-8 sequence, then c is set to a negative value.
663 * @param s const uint8_t * string
664 * @param start int32_t starting string offset (usually 0)
665 * @param i int32_t string offset, must be start<i
666 * @param c output UChar32 variable, set to <0 in case of an error
667 * @see U8_PREV_UNSAFE
670 #define U8_PREV(s, start, i, c) { \
671 (c)=(uint8_t)(s)[--(i)]; \
673 (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
678 * Move the string offset from one code point boundary to the previous one
679 * and get the code point between them.
680 * (Pre-decrementing backward iteration.)
681 * "Safe" macro, checks for illegal sequences and for string boundaries.
683 * The input offset may be the same as the string length.
684 * If the offset is behind a multi-byte sequence, then the macro will read
685 * the whole sequence.
686 * If the offset is behind a lead byte, then that itself
687 * will be returned as the code point.
688 * If the offset is behind an illegal UTF-8 sequence, then c is set to U+FFFD.
690 * This macro does not distinguish between a real U+FFFD in the text
691 * and U+FFFD returned for an ill-formed sequence.
692 * Use U8_PREV() if that distinction is important.
694 * @param s const uint8_t * string
695 * @param start int32_t starting string offset (usually 0)
696 * @param i int32_t string offset, must be start<i
697 * @param c output UChar32 variable, set to U+FFFD in case of an error
701 #define U8_PREV_OR_FFFD(s, start, i, c) { \
702 (c)=(uint8_t)(s)[--(i)]; \
704 (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -3); \
709 * Move the string offset from one code point boundary to the previous one.
710 * (Pre-decrementing backward iteration.)
711 * The input offset may be the same as the string length.
712 * "Unsafe" macro, assumes well-formed UTF-8.
714 * @param s const uint8_t * string
715 * @param i string offset
719 #define U8_BACK_1_UNSAFE(s, i) { \
720 while(U8_IS_TRAIL((s)[--(i)])) {} \
724 * Move the string offset from one code point boundary to the previous one.
725 * (Pre-decrementing backward iteration.)
726 * The input offset may be the same as the string length.
727 * "Safe" macro, checks for illegal sequences and for string boundaries.
729 * @param s const uint8_t * string
730 * @param start int32_t starting string offset (usually 0)
731 * @param i int32_t string offset, must be start<i
732 * @see U8_BACK_1_UNSAFE
735 #define U8_BACK_1(s, start, i) { \
736 if(U8_IS_TRAIL((s)[--(i)])) { \
737 (i)=utf8_back1SafeBody(s, start, (i)); \
742 * Move the string offset from one code point boundary to the n-th one before it,
743 * i.e., move backward by n code points.
744 * (Pre-decrementing backward iteration.)
745 * The input offset may be the same as the string length.
746 * "Unsafe" macro, assumes well-formed UTF-8.
748 * @param s const uint8_t * string
749 * @param i string offset
750 * @param n number of code points to skip
754 #define U8_BACK_N_UNSAFE(s, i, n) { \
757 U8_BACK_1_UNSAFE(s, i); \
763 * Move the string offset from one code point boundary to the n-th one before it,
764 * i.e., move backward by n code points.
765 * (Pre-decrementing backward iteration.)
766 * The input offset may be the same as the string length.
767 * "Safe" macro, checks for illegal sequences and for string boundaries.
769 * @param s const uint8_t * string
770 * @param start int32_t index of the start of the string
771 * @param i int32_t string offset, must be start<i
772 * @param n number of code points to skip
773 * @see U8_BACK_N_UNSAFE
776 #define U8_BACK_N(s, start, i, n) { \
778 while(__N>0 && (i)>(start)) { \
779 U8_BACK_1(s, start, i); \
785 * Adjust a random-access offset to a code point boundary after a code point.
786 * If the offset is behind a partial multi-byte sequence,
787 * then the offset is incremented to behind the whole sequence.
788 * Otherwise, it is not modified.
789 * The input offset may be the same as the string length.
790 * "Unsafe" macro, assumes well-formed UTF-8.
792 * @param s const uint8_t * string
793 * @param i string offset
794 * @see U8_SET_CP_LIMIT
797 #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \
798 U8_BACK_1_UNSAFE(s, i); \
799 U8_FWD_1_UNSAFE(s, i); \
803 * Adjust a random-access offset to a code point boundary after a code point.
804 * If the offset is behind a partial multi-byte sequence,
805 * then the offset is incremented to behind the whole sequence.
806 * Otherwise, it is not modified.
807 * The input offset may be the same as the string length.
808 * "Safe" macro, checks for illegal sequences and for string boundaries.
810 * The length can be negative for a NUL-terminated string.
812 * @param s const uint8_t * string
813 * @param start int32_t starting string offset (usually 0)
814 * @param i int32_t string offset, must be start<=i<=length
815 * @param length int32_t string length
816 * @see U8_SET_CP_LIMIT_UNSAFE
819 #define U8_SET_CP_LIMIT(s, start, i, length) { \
820 if((start)<(i) && ((i)<(length) || (length)<0)) { \
821 U8_BACK_1(s, start, i); \
822 U8_FWD_1(s, i, length); \