2 *******************************************************************************
4 * Copyright (C) 1999-2004, 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.
22 * utf8.h is included by utf.h after unicode/umachine.h
23 * and some common definitions.
25 * For more information see utf.h and the ICU User Guide Strings chapter
26 * (http://oss.software.ibm.com/icu/userguide/).
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 /* utf.h must be included first. */
42 /* internal definitions ----------------------------------------------------- */
45 * \var utf8_countTrailBytes
46 * Internal array with numbers of trail bytes for any given byte used in
51 U_INTERNAL
const uint8_t
52 #elif defined(U_STATIC_IMPLEMENTATION)
55 U_CFUNC U_IMPORT
const uint8_t /* U_IMPORT2? */ /*U_IMPORT*/
57 utf8_countTrailBytes
[256];
60 * Count the trail bytes for a UTF-8 lead byte.
63 #define U8_COUNT_TRAIL_BYTES(leadByte) (utf8_countTrailBytes[(uint8_t)leadByte])
66 * Mask a UTF-8 lead byte, leave only the lower bits that form part of the code point value.
69 #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
72 * Function for handling "next code point" with error-checking.
75 U_INTERNAL UChar32 U_EXPORT2
76 utf8_nextCharSafeBody(const uint8_t *s
, int32_t *pi
, int32_t length
, UChar32 c
, UBool strict
);
79 * Function for handling "append code point" with error-checking.
82 U_INTERNAL
int32_t U_EXPORT2
83 utf8_appendCharSafeBody(uint8_t *s
, int32_t i
, int32_t length
, UChar32 c
, UBool
*pIsError
);
86 * Function for handling "previous code point" with error-checking.
89 U_INTERNAL UChar32 U_EXPORT2
90 utf8_prevCharSafeBody(const uint8_t *s
, int32_t start
, int32_t *pi
, UChar32 c
, UBool strict
);
93 * Function for handling "skip backward one code point" with error-checking.
96 U_INTERNAL
int32_t U_EXPORT2
97 utf8_back1SafeBody(const uint8_t *s
, int32_t start
, int32_t i
);
99 /* single-code point definitions -------------------------------------------- */
102 * Does this code unit (byte) encode a code point by itself (US-ASCII 0..0x7f)?
103 * @param c 8-bit code unit (byte)
104 * @return TRUE or FALSE
107 #define U8_IS_SINGLE(c) (((c)&0x80)==0)
110 * Is this code unit (byte) a UTF-8 lead byte?
111 * @param c 8-bit code unit (byte)
112 * @return TRUE or FALSE
115 #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc0)<0x3e)
118 * Is this code unit (byte) a UTF-8 trail byte?
119 * @param c 8-bit code unit (byte)
120 * @return TRUE or FALSE
123 #define U8_IS_TRAIL(c) (((c)&0xc0)==0x80)
126 * How many code units (bytes) are used for the UTF-8 encoding
127 * of this Unicode code point?
128 * @param c 32-bit code point
129 * @return 1..4, or 0 if c is a surrogate or not a Unicode code point
132 #define U8_LENGTH(c) \
133 ((uint32_t)(c)<=0x7f ? 1 : \
134 ((uint32_t)(c)<=0x7ff ? 2 : \
135 ((uint32_t)(c)<=0xd7ff ? 3 : \
136 ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
137 ((uint32_t)(c)<=0xffff ? 3 : 4)\
144 * The maximum number of UTF-8 code units (bytes) per Unicode code point (U+0000..U+10ffff).
148 #define U8_MAX_LENGTH 4
151 * Get a code point from a string at a random-access offset,
152 * without changing the offset.
153 * The offset may point to either the lead byte or one of the trail bytes
154 * for a code point, in which case the macro will read all of the bytes
155 * for the code point.
156 * The result is undefined if the offset points to an illegal UTF-8
158 * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
160 * @param s const uint8_t * string
161 * @param i string offset
162 * @param c output UChar32 variable
166 #define U8_GET_UNSAFE(s, i, c) { \
167 int32_t _u8_get_unsafe_index=(int32_t)(i); \
168 U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \
169 U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \
173 * Get a code point from a string at a random-access offset,
174 * without changing the offset.
175 * The offset may point to either the lead byte or one of the trail bytes
176 * for a code point, in which case the macro will read all of the bytes
177 * for the code point.
178 * If the offset points to an illegal UTF-8 byte sequence, then
179 * c is set to a negative value.
180 * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
182 * @param s const uint8_t * string
183 * @param start starting string offset
184 * @param i string offset, start<=i<length
185 * @param length string length
186 * @param c output UChar32 variable, set to <0 in case of an error
190 #define U8_GET(s, start, i, length, c) { \
191 int32_t _u8_get_index=(int32_t)(i); \
192 U8_SET_CP_START(s, start, _u8_get_index); \
193 U8_NEXT(s, _u8_get_index, length, c); \
196 /* definitions with forward iteration --------------------------------------- */
199 * Get a code point from a string at a code point boundary offset,
200 * and advance the offset to the next code point boundary.
201 * (Post-incrementing forward iteration.)
202 * "Unsafe" macro, assumes well-formed UTF-8.
204 * The offset may point to the lead byte of a multi-byte sequence,
205 * in which case the macro will read the whole sequence.
206 * The result is undefined if the offset points to a trail byte
207 * or an illegal UTF-8 sequence.
209 * @param s const uint8_t * string
210 * @param i string offset
211 * @param c output UChar32 variable
215 #define U8_NEXT_UNSAFE(s, i, c) { \
217 if((uint8_t)((c)-0xc0)<0x35) { \
218 uint8_t __count=U8_COUNT_TRAIL_BYTES(c); \
219 U8_MASK_LEAD_BYTE(c, __count); \
221 /* each following branch falls through to the next one */ \
223 (c)=((c)<<6)|((s)[(i)++]&0x3f); \
225 (c)=((c)<<6)|((s)[(i)++]&0x3f); \
227 (c)=((c)<<6)|((s)[(i)++]&0x3f); \
228 /* no other branches to optimize switch() */ \
235 * Get a code point from a string at a code point boundary offset,
236 * and advance the offset to the next code point boundary.
237 * (Post-incrementing forward iteration.)
238 * "Safe" macro, checks for illegal sequences and for string boundaries.
240 * The offset may point to the lead byte of a multi-byte sequence,
241 * in which case the macro will read the whole sequence.
242 * If the offset points to a trail byte or an illegal UTF-8 sequence, then
243 * c is set to a negative value.
245 * @param s const uint8_t * string
246 * @param i string offset, i<length
247 * @param length string length
248 * @param c output UChar32 variable, set to <0 in case of an error
249 * @see U8_NEXT_UNSAFE
252 #define U8_NEXT(s, i, length, c) { \
254 if(((uint8_t)(c))>=0x80) { \
255 if(U8_IS_LEAD(c)) { \
256 (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (int32_t)(length), c, -1); \
264 * Append a code point to a string, overwriting 1 to 4 bytes.
265 * The offset points to the current end of the string contents
266 * and is advanced (post-increment).
267 * "Unsafe" macro, assumes a valid code point and sufficient space in the string.
268 * Otherwise, the result is undefined.
270 * @param s const uint8_t * string buffer
271 * @param i string offset
272 * @param c code point to append
276 #define U8_APPEND_UNSAFE(s, i, c) { \
277 if((uint32_t)(c)<=0x7f) { \
278 (s)[(i)++]=(uint8_t)(c); \
280 if((uint32_t)(c)<=0x7ff) { \
281 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
283 if((uint32_t)(c)<=0xffff) { \
284 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
286 (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
287 (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
289 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
291 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
296 * Append a code point to a string, overwriting 1 or 2 code units.
297 * The offset points to the current end of the string contents
298 * and is advanced (post-increment).
299 * "Safe" macro, checks for a valid code point.
300 * If a non-ASCII code point is written, checks for sufficient space in the string.
301 * If the code point is not valid or trail bytes do not fit,
302 * then isError is set to TRUE.
304 * @param s const uint8_t * string buffer
305 * @param i string offset, i<length
306 * @param length size of the string buffer
307 * @param c code point to append
308 * @param isError output UBool set to TRUE if an error occurs, otherwise not modified
309 * @see U8_APPEND_UNSAFE
312 #define U8_APPEND(s, i, length, c, isError) { \
313 if((uint32_t)(c)<=0x7f) { \
314 (s)[(i)++]=(uint8_t)(c); \
316 (i)=utf8_appendCharSafeBody(s, (int32_t)(i), (int32_t)(length), c, &(isError)); \
321 * Advance the string offset from one code point boundary to the next.
322 * (Post-incrementing iteration.)
323 * "Unsafe" macro, assumes well-formed UTF-8.
325 * @param s const uint8_t * string
326 * @param i string offset
330 #define U8_FWD_1_UNSAFE(s, i) { \
331 (i)+=1+U8_COUNT_TRAIL_BYTES((s)[i]); \
335 * Advance the string offset from one code point boundary to the next.
336 * (Post-incrementing iteration.)
337 * "Safe" macro, checks for illegal sequences and for string boundaries.
339 * @param s const uint8_t * string
340 * @param i string offset, i<length
341 * @param length string length
342 * @see U8_FWD_1_UNSAFE
345 #define U8_FWD_1(s, i, length) { \
346 uint8_t __b=(s)[(i)++]; \
347 if(U8_IS_LEAD(__b)) { \
348 uint8_t __count=U8_COUNT_TRAIL_BYTES(__b); \
349 if((i)+__count>(length)) { \
350 __count=(uint8_t)((length)-(i)); \
352 while(__count>0 && U8_IS_TRAIL((s)[i])) { \
360 * Advance the string offset from one code point boundary to the n-th next one,
361 * i.e., move forward by n code points.
362 * (Post-incrementing iteration.)
363 * "Unsafe" macro, assumes well-formed UTF-8.
365 * @param s const uint8_t * string
366 * @param i string offset
367 * @param n number of code points to skip
371 #define U8_FWD_N_UNSAFE(s, i, n) { \
374 U8_FWD_1_UNSAFE(s, i); \
380 * Advance the string offset from one code point boundary to the n-th next one,
381 * i.e., move forward by n code points.
382 * (Post-incrementing iteration.)
383 * "Safe" macro, checks for illegal sequences and for string boundaries.
385 * @param s const uint8_t * string
386 * @param i string offset, i<length
387 * @param length string length
388 * @param n number of code points to skip
389 * @see U8_FWD_N_UNSAFE
392 #define U8_FWD_N(s, i, length, n) { \
394 while(__N>0 && (i)<(length)) { \
395 U8_FWD_1(s, i, length); \
401 * Adjust a random-access offset to a code point boundary
402 * at the start of a code point.
403 * If the offset points to a UTF-8 trail byte,
404 * then the offset is moved backward to the corresponding lead byte.
405 * Otherwise, it is not modified.
406 * "Unsafe" macro, assumes well-formed UTF-8.
408 * @param s const uint8_t * string
409 * @param i string offset
410 * @see U8_SET_CP_START
413 #define U8_SET_CP_START_UNSAFE(s, i) { \
414 while(U8_IS_TRAIL((s)[i])) { --(i); } \
418 * Adjust a random-access offset to a code point boundary
419 * at the start of a code point.
420 * If the offset points to a UTF-8 trail byte,
421 * then the offset is moved backward to the corresponding lead byte.
422 * Otherwise, it is not modified.
423 * "Safe" macro, checks for illegal sequences and for string boundaries.
425 * @param s const uint8_t * string
426 * @param start starting string offset (usually 0)
427 * @param i string offset, start<=i
428 * @see U8_SET_CP_START_UNSAFE
431 #define U8_SET_CP_START(s, start, i) { \
432 if(U8_IS_TRAIL((s)[(i)])) { \
433 (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
437 /* definitions with backward iteration -------------------------------------- */
440 * Move the string offset from one code point boundary to the previous one
441 * and get the code point between them.
442 * (Pre-decrementing backward iteration.)
443 * "Unsafe" macro, assumes well-formed UTF-8.
445 * The input offset may be the same as the string length.
446 * If the offset is behind a multi-byte sequence, then the macro will read
447 * the whole sequence.
448 * If the offset is behind a lead byte, then that itself
449 * will be returned as the code point.
450 * The result is undefined if the offset is behind an illegal UTF-8 sequence.
452 * @param s const uint8_t * string
453 * @param i string offset
454 * @param c output UChar32 variable
458 #define U8_PREV_UNSAFE(s, i, c) { \
460 if(U8_IS_TRAIL(c)) { \
461 uint8_t __b, __count=1, __shift=6; \
463 /* c is a trail byte */ \
468 U8_MASK_LEAD_BYTE(__b, __count); \
469 (c)|=(UChar32)__b<<__shift; \
472 (c)|=(UChar32)(__b&0x3f)<<__shift; \
481 * Move the string offset from one code point boundary to the previous one
482 * and get the code point between them.
483 * (Pre-decrementing backward iteration.)
484 * "Safe" macro, checks for illegal sequences and for string boundaries.
486 * The input offset may be the same as the string length.
487 * If the offset is behind a multi-byte sequence, then the macro will read
488 * the whole sequence.
489 * If the offset is behind a lead byte, then that itself
490 * will be returned as the code point.
491 * If the offset is behind an illegal UTF-8 sequence, then c is set to a negative value.
493 * @param s const uint8_t * string
494 * @param start starting string offset (usually 0)
495 * @param i string offset, start<=i
496 * @param c output UChar32 variable, set to <0 in case of an error
497 * @see U8_PREV_UNSAFE
500 #define U8_PREV(s, start, i, c) { \
504 (c)=utf8_prevCharSafeBody(s, start, &(i), c, -1); \
512 * Move the string offset from one code point boundary to the previous one.
513 * (Pre-decrementing backward iteration.)
514 * The input offset may be the same as the string length.
515 * "Unsafe" macro, assumes well-formed UTF-8.
517 * @param s const uint8_t * string
518 * @param i string offset
522 #define U8_BACK_1_UNSAFE(s, i) { \
523 while(U8_IS_TRAIL((s)[--(i)])) {} \
527 * Move the string offset from one code point boundary to the previous one.
528 * (Pre-decrementing backward iteration.)
529 * The input offset may be the same as the string length.
530 * "Safe" macro, checks for illegal sequences and for string boundaries.
532 * @param s const uint8_t * string
533 * @param start starting string offset (usually 0)
534 * @param i string offset, start<=i
535 * @see U8_BACK_1_UNSAFE
538 #define U8_BACK_1(s, start, i) { \
539 if(U8_IS_TRAIL((s)[--(i)])) { \
540 (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
545 * Move the string offset from one code point boundary to the n-th one before it,
546 * i.e., move backward by n code points.
547 * (Pre-decrementing backward iteration.)
548 * The input offset may be the same as the string length.
549 * "Unsafe" macro, assumes well-formed UTF-8.
551 * @param s const uint8_t * string
552 * @param i string offset
553 * @param n number of code points to skip
557 #define U8_BACK_N_UNSAFE(s, i, n) { \
560 U8_BACK_1_UNSAFE(s, i); \
566 * Move the string offset from one code point boundary to the n-th one before it,
567 * i.e., move backward by n code points.
568 * (Pre-decrementing backward iteration.)
569 * The input offset may be the same as the string length.
570 * "Safe" macro, checks for illegal sequences and for string boundaries.
572 * @param s const uint8_t * string
573 * @param start index of the start of the string
574 * @param i string offset, i<length
575 * @param n number of code points to skip
576 * @see U8_BACK_N_UNSAFE
579 #define U8_BACK_N(s, start, i, n) { \
581 while(__N>0 && (i)>(start)) { \
582 U8_BACK_1(s, start, i); \
588 * Adjust a random-access offset to a code point boundary after a code point.
589 * If the offset is behind a partial multi-byte sequence,
590 * then the offset is incremented to behind the whole sequence.
591 * Otherwise, it is not modified.
592 * The input offset may be the same as the string length.
593 * "Unsafe" macro, assumes well-formed UTF-8.
595 * @param s const uint8_t * string
596 * @param i string offset
597 * @see U8_SET_CP_LIMIT
600 #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \
601 U8_BACK_1_UNSAFE(s, i); \
602 U8_FWD_1_UNSAFE(s, i); \
606 * Adjust a random-access offset to a code point boundary after a code point.
607 * If the offset is behind a partial multi-byte sequence,
608 * then the offset is incremented to behind the whole sequence.
609 * Otherwise, it is not modified.
610 * The input offset may be the same as the string length.
611 * "Safe" macro, checks for illegal sequences and for string boundaries.
613 * @param s const uint8_t * string
614 * @param start starting string offset (usually 0)
615 * @param i string offset, start<=i<=length
616 * @param length string length
617 * @see U8_SET_CP_LIMIT_UNSAFE
620 #define U8_SET_CP_LIMIT(s, start, i, length) { \
621 if((start)<(i) && (i)<(length)) { \
622 U8_BACK_1(s, start, i); \
623 U8_FWD_1(s, i, length); \