2 *******************************************************************************
4 * Copyright (C) 1999-2011, 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 * Count the trail bytes for a UTF-8 lead byte.
65 * This is internal since it is not meant to be called directly by external clients;
66 * however it is called by public macros in this file and thus must remain stable.
69 #define U8_COUNT_TRAIL_BYTES(leadByte) (utf8_countTrailBytes[(uint8_t)leadByte])
72 * Mask a UTF-8 lead byte, leave only the lower bits that form part of the code point value.
74 * This is internal since it is not meant to be called directly by external clients;
75 * however it is called by public macros in this file and thus must remain stable.
78 #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
81 * Function for handling "next code point" with error-checking.
83 * This is internal since it is not meant to be called directly by external clients;
84 * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
85 * file and thus must remain stable, and should not be hidden when other internal
86 * functions are hidden (otherwise public macros would fail to compile).
89 U_STABLE UChar32 U_EXPORT2
90 utf8_nextCharSafeBody(const uint8_t *s
, int32_t *pi
, int32_t length
, UChar32 c
, UBool strict
);
93 * Function for handling "append code point" with error-checking.
95 * This is internal since it is not meant to be called directly by external clients;
96 * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
97 * file and thus must remain stable, and should not be hidden when other internal
98 * functions are hidden (otherwise public macros would fail to compile).
101 U_STABLE
int32_t U_EXPORT2
102 utf8_appendCharSafeBody(uint8_t *s
, int32_t i
, int32_t length
, UChar32 c
, UBool
*pIsError
);
105 * Function for handling "previous code point" with error-checking.
107 * This is internal since it is not meant to be called directly by external clients;
108 * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
109 * file and thus must remain stable, and should not be hidden when other internal
110 * functions are hidden (otherwise public macros would fail to compile).
113 U_STABLE UChar32 U_EXPORT2
114 utf8_prevCharSafeBody(const uint8_t *s
, int32_t start
, int32_t *pi
, UChar32 c
, UBool strict
);
117 * Function for handling "skip backward one code point" with error-checking.
119 * This is internal since it is not meant to be called directly by external clients;
120 * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
121 * file and thus must remain stable, and should not be hidden when other internal
122 * functions are hidden (otherwise public macros would fail to compile).
125 U_STABLE
int32_t U_EXPORT2
126 utf8_back1SafeBody(const uint8_t *s
, int32_t start
, int32_t i
);
128 /* single-code point definitions -------------------------------------------- */
131 * Does this code unit (byte) encode a code point by itself (US-ASCII 0..0x7f)?
132 * @param c 8-bit code unit (byte)
133 * @return TRUE or FALSE
136 #define U8_IS_SINGLE(c) (((c)&0x80)==0)
139 * Is this code unit (byte) a UTF-8 lead byte?
140 * @param c 8-bit code unit (byte)
141 * @return TRUE or FALSE
144 #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc0)<0x3e)
147 * Is this code unit (byte) a UTF-8 trail byte?
148 * @param c 8-bit code unit (byte)
149 * @return TRUE or FALSE
152 #define U8_IS_TRAIL(c) (((c)&0xc0)==0x80)
155 * How many code units (bytes) are used for the UTF-8 encoding
156 * of this Unicode code point?
157 * @param c 32-bit code point
158 * @return 1..4, or 0 if c is a surrogate or not a Unicode code point
161 #define U8_LENGTH(c) \
162 ((uint32_t)(c)<=0x7f ? 1 : \
163 ((uint32_t)(c)<=0x7ff ? 2 : \
164 ((uint32_t)(c)<=0xd7ff ? 3 : \
165 ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
166 ((uint32_t)(c)<=0xffff ? 3 : 4)\
173 * The maximum number of UTF-8 code units (bytes) per Unicode code point (U+0000..U+10ffff).
177 #define U8_MAX_LENGTH 4
180 * Get a code point from a string at a random-access offset,
181 * without changing the offset.
182 * The offset may point to either the lead byte or one of the trail bytes
183 * for a code point, in which case the macro will read all of the bytes
184 * for the code point.
185 * The result is undefined if the offset points to an illegal UTF-8
187 * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
189 * @param s const uint8_t * string
190 * @param i string offset
191 * @param c output UChar32 variable
195 #define U8_GET_UNSAFE(s, i, c) { \
196 int32_t _u8_get_unsafe_index=(int32_t)(i); \
197 U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \
198 U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \
202 * Get a code point from a string at a random-access offset,
203 * without changing the offset.
204 * The offset may point to either the lead byte or one of the trail bytes
205 * for a code point, in which case the macro will read all of the bytes
206 * for the code point.
207 * If the offset points to an illegal UTF-8 byte sequence, then
208 * c is set to a negative value.
209 * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
211 * @param s const uint8_t * string
212 * @param start starting string offset
213 * @param i string offset, must be start<=i<length
214 * @param length string length
215 * @param c output UChar32 variable, set to <0 in case of an error
219 #define U8_GET(s, start, i, length, c) { \
220 int32_t _u8_get_index=(int32_t)(i); \
221 U8_SET_CP_START(s, start, _u8_get_index); \
222 U8_NEXT(s, _u8_get_index, length, c); \
225 /* definitions with forward iteration --------------------------------------- */
228 * Get a code point from a string at a code point boundary offset,
229 * and advance the offset to the next code point boundary.
230 * (Post-incrementing forward iteration.)
231 * "Unsafe" macro, assumes well-formed UTF-8.
233 * The offset may point to the lead byte of a multi-byte sequence,
234 * in which case the macro will read the whole sequence.
235 * The result is undefined if the offset points to a trail byte
236 * or an illegal UTF-8 sequence.
238 * @param s const uint8_t * string
239 * @param i string offset
240 * @param c output UChar32 variable
244 #define U8_NEXT_UNSAFE(s, i, c) { \
245 (c)=(uint8_t)(s)[(i)++]; \
246 if((uint8_t)((c)-0xc0)<0x35) { \
247 uint8_t __count=U8_COUNT_TRAIL_BYTES(c); \
248 U8_MASK_LEAD_BYTE(c, __count); \
250 /* each following branch falls through to the next one */ \
252 (c)=((c)<<6)|((s)[(i)++]&0x3f); \
254 (c)=((c)<<6)|((s)[(i)++]&0x3f); \
256 (c)=((c)<<6)|((s)[(i)++]&0x3f); \
257 /* no other branches to optimize switch() */ \
264 * Get a code point from a string at a code point boundary offset,
265 * and advance the offset to the next code point boundary.
266 * (Post-incrementing forward iteration.)
267 * "Safe" macro, checks for illegal sequences and for string boundaries.
269 * The offset may point to the lead byte of a multi-byte sequence,
270 * in which case the macro will read the whole sequence.
271 * If the offset points to a trail byte or an illegal UTF-8 sequence, then
272 * c is set to a negative value.
274 * @param s const uint8_t * string
275 * @param i string offset, must be i<length
276 * @param length string length
277 * @param c output UChar32 variable, set to <0 in case of an error
278 * @see U8_NEXT_UNSAFE
281 #define U8_NEXT(s, i, length, c) { \
282 (c)=(uint8_t)(s)[(i)++]; \
284 uint8_t __t1, __t2; \
285 if( /* handle U+1000..U+CFFF inline */ \
286 (0xe0<(c) && (c)<=0xec) && \
287 (((i)+1)<(length)) && \
288 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
289 (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
291 /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
292 (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
294 } else if( /* handle U+0080..U+07FF inline */ \
295 ((c)<0xe0 && (c)>=0xc2) && \
297 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
299 (c)=(UChar)((((c)&0x1f)<<6)|__t1); \
301 } else if(U8_IS_LEAD(c)) { \
302 /* function call for "complicated" and error cases */ \
303 (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (int32_t)(length), c, -1); \
311 * Append a code point to a string, overwriting 1 to 4 bytes.
312 * The offset points to the current end of the string contents
313 * and is advanced (post-increment).
314 * "Unsafe" macro, assumes a valid code point and sufficient space in the string.
315 * Otherwise, the result is undefined.
317 * @param s const uint8_t * string buffer
318 * @param i string offset
319 * @param c code point to append
323 #define U8_APPEND_UNSAFE(s, i, c) { \
324 if((uint32_t)(c)<=0x7f) { \
325 (s)[(i)++]=(uint8_t)(c); \
327 if((uint32_t)(c)<=0x7ff) { \
328 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
330 if((uint32_t)(c)<=0xffff) { \
331 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
333 (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
334 (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
336 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
338 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
343 * Append a code point to a string, overwriting 1 to 4 bytes.
344 * The offset points to the current end of the string contents
345 * and is advanced (post-increment).
346 * "Safe" macro, checks for a valid code point.
347 * If a non-ASCII code point is written, checks for sufficient space in the string.
348 * If the code point is not valid or trail bytes do not fit,
349 * then isError is set to TRUE.
351 * @param s const uint8_t * string buffer
352 * @param i string offset, must be i<capacity
353 * @param capacity size of the string buffer
354 * @param c code point to append
355 * @param isError output UBool set to TRUE if an error occurs, otherwise not modified
356 * @see U8_APPEND_UNSAFE
359 #define U8_APPEND(s, i, capacity, c, isError) { \
360 if((uint32_t)(c)<=0x7f) { \
361 (s)[(i)++]=(uint8_t)(c); \
362 } else if((uint32_t)(c)<=0x7ff && (i)+1<(capacity)) { \
363 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
364 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
365 } else if((uint32_t)(c)<=0xd7ff && (i)+2<(capacity)) { \
366 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
367 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
368 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
370 (i)=utf8_appendCharSafeBody(s, (int32_t)(i), (int32_t)(capacity), c, &(isError)); \
375 * Advance the string offset from one code point boundary to the next.
376 * (Post-incrementing iteration.)
377 * "Unsafe" macro, assumes well-formed UTF-8.
379 * @param s const uint8_t * string
380 * @param i string offset
384 #define U8_FWD_1_UNSAFE(s, i) { \
385 (i)+=1+U8_COUNT_TRAIL_BYTES((s)[i]); \
389 * Advance the string offset from one code point boundary to the next.
390 * (Post-incrementing iteration.)
391 * "Safe" macro, checks for illegal sequences and for string boundaries.
393 * @param s const uint8_t * string
394 * @param i string offset, must be i<length
395 * @param length string length
396 * @see U8_FWD_1_UNSAFE
399 #define U8_FWD_1(s, i, length) { \
400 uint8_t __b=(uint8_t)(s)[(i)++]; \
401 if(U8_IS_LEAD(__b)) { \
402 uint8_t __count=U8_COUNT_TRAIL_BYTES(__b); \
403 if((i)+__count>(length)) { \
404 __count=(uint8_t)((length)-(i)); \
406 while(__count>0 && U8_IS_TRAIL((s)[i])) { \
414 * Advance the string offset from one code point boundary to the n-th next one,
415 * i.e., move forward by n code points.
416 * (Post-incrementing iteration.)
417 * "Unsafe" macro, assumes well-formed UTF-8.
419 * @param s const uint8_t * string
420 * @param i string offset
421 * @param n number of code points to skip
425 #define U8_FWD_N_UNSAFE(s, i, n) { \
428 U8_FWD_1_UNSAFE(s, i); \
434 * Advance the string offset from one code point boundary to the n-th next one,
435 * i.e., move forward by n code points.
436 * (Post-incrementing iteration.)
437 * "Safe" macro, checks for illegal sequences and for string boundaries.
439 * @param s const uint8_t * string
440 * @param i string offset, must be i<length
441 * @param length string length
442 * @param n number of code points to skip
443 * @see U8_FWD_N_UNSAFE
446 #define U8_FWD_N(s, i, length, n) { \
448 while(__N>0 && (i)<(length)) { \
449 U8_FWD_1(s, i, length); \
455 * Adjust a random-access offset to a code point boundary
456 * at the start of a code point.
457 * If the offset points to a UTF-8 trail byte,
458 * then the offset is moved backward to the corresponding lead byte.
459 * Otherwise, it is not modified.
460 * "Unsafe" macro, assumes well-formed UTF-8.
462 * @param s const uint8_t * string
463 * @param i string offset
464 * @see U8_SET_CP_START
467 #define U8_SET_CP_START_UNSAFE(s, i) { \
468 while(U8_IS_TRAIL((s)[i])) { --(i); } \
472 * Adjust a random-access offset to a code point boundary
473 * at the start of a code point.
474 * If the offset points to a UTF-8 trail byte,
475 * then the offset is moved backward to the corresponding lead byte.
476 * Otherwise, it is not modified.
477 * "Safe" macro, checks for illegal sequences and for string boundaries.
479 * @param s const uint8_t * string
480 * @param start starting string offset (usually 0)
481 * @param i string offset, must be start<=i
482 * @see U8_SET_CP_START_UNSAFE
485 #define U8_SET_CP_START(s, start, i) { \
486 if(U8_IS_TRAIL((s)[(i)])) { \
487 (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
491 /* definitions with backward iteration -------------------------------------- */
494 * Move the string offset from one code point boundary to the previous one
495 * and get the code point between them.
496 * (Pre-decrementing backward iteration.)
497 * "Unsafe" macro, assumes well-formed UTF-8.
499 * The input offset may be the same as the string length.
500 * If the offset is behind a multi-byte sequence, then the macro will read
501 * the whole sequence.
502 * If the offset is behind a lead byte, then that itself
503 * will be returned as the code point.
504 * The result is undefined if the offset is behind an illegal UTF-8 sequence.
506 * @param s const uint8_t * string
507 * @param i string offset
508 * @param c output UChar32 variable
512 #define U8_PREV_UNSAFE(s, i, c) { \
513 (c)=(uint8_t)(s)[--(i)]; \
514 if(U8_IS_TRAIL(c)) { \
515 uint8_t __b, __count=1, __shift=6; \
517 /* c is a trail byte */ \
520 __b=(uint8_t)(s)[--(i)]; \
522 U8_MASK_LEAD_BYTE(__b, __count); \
523 (c)|=(UChar32)__b<<__shift; \
526 (c)|=(UChar32)(__b&0x3f)<<__shift; \
535 * Move the string offset from one code point boundary to the previous one
536 * and get the code point between them.
537 * (Pre-decrementing backward iteration.)
538 * "Safe" macro, checks for illegal sequences and for string boundaries.
540 * The input offset may be the same as the string length.
541 * If the offset is behind a multi-byte sequence, then the macro will read
542 * the whole sequence.
543 * If the offset is behind a lead byte, then that itself
544 * will be returned as the code point.
545 * If the offset is behind an illegal UTF-8 sequence, then c is set to a negative value.
547 * @param s const uint8_t * string
548 * @param start starting string offset (usually 0)
549 * @param i string offset, must be start<i
550 * @param c output UChar32 variable, set to <0 in case of an error
551 * @see U8_PREV_UNSAFE
554 #define U8_PREV(s, start, i, c) { \
555 (c)=(uint8_t)(s)[--(i)]; \
558 (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
566 * Move the string offset from one code point boundary to the previous one.
567 * (Pre-decrementing backward iteration.)
568 * The input offset may be the same as the string length.
569 * "Unsafe" macro, assumes well-formed UTF-8.
571 * @param s const uint8_t * string
572 * @param i string offset
576 #define U8_BACK_1_UNSAFE(s, i) { \
577 while(U8_IS_TRAIL((s)[--(i)])) {} \
581 * Move the string offset from one code point boundary to the previous one.
582 * (Pre-decrementing backward iteration.)
583 * The input offset may be the same as the string length.
584 * "Safe" macro, checks for illegal sequences and for string boundaries.
586 * @param s const uint8_t * string
587 * @param start starting string offset (usually 0)
588 * @param i string offset, must be start<i
589 * @see U8_BACK_1_UNSAFE
592 #define U8_BACK_1(s, start, i) { \
593 if(U8_IS_TRAIL((s)[--(i)])) { \
594 (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
599 * Move the string offset from one code point boundary to the n-th one before it,
600 * i.e., move backward by n code points.
601 * (Pre-decrementing backward iteration.)
602 * The input offset may be the same as the string length.
603 * "Unsafe" macro, assumes well-formed UTF-8.
605 * @param s const uint8_t * string
606 * @param i string offset
607 * @param n number of code points to skip
611 #define U8_BACK_N_UNSAFE(s, i, n) { \
614 U8_BACK_1_UNSAFE(s, i); \
620 * Move the string offset from one code point boundary to the n-th one before it,
621 * i.e., move backward by n code points.
622 * (Pre-decrementing backward iteration.)
623 * The input offset may be the same as the string length.
624 * "Safe" macro, checks for illegal sequences and for string boundaries.
626 * @param s const uint8_t * string
627 * @param start index of the start of the string
628 * @param i string offset, must be start<i
629 * @param n number of code points to skip
630 * @see U8_BACK_N_UNSAFE
633 #define U8_BACK_N(s, start, i, n) { \
635 while(__N>0 && (i)>(start)) { \
636 U8_BACK_1(s, start, i); \
642 * Adjust a random-access offset to a code point boundary after a code point.
643 * If the offset is behind a partial multi-byte sequence,
644 * then the offset is incremented to behind the whole sequence.
645 * Otherwise, it is not modified.
646 * The input offset may be the same as the string length.
647 * "Unsafe" macro, assumes well-formed UTF-8.
649 * @param s const uint8_t * string
650 * @param i string offset
651 * @see U8_SET_CP_LIMIT
654 #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \
655 U8_BACK_1_UNSAFE(s, i); \
656 U8_FWD_1_UNSAFE(s, i); \
660 * Adjust a random-access offset to a code point boundary after a code point.
661 * If the offset is behind a partial multi-byte sequence,
662 * then the offset is incremented to behind the whole sequence.
663 * Otherwise, it is not modified.
664 * The input offset may be the same as the string length.
665 * "Safe" macro, checks for illegal sequences and for string boundaries.
667 * @param s const uint8_t * string
668 * @param start starting string offset (usually 0)
669 * @param i string offset, must be start<=i<=length
670 * @param length string length
671 * @see U8_SET_CP_LIMIT_UNSAFE
674 #define U8_SET_CP_LIMIT(s, start, i, length) { \
675 if((start)<(i) && (i)<(length)) { \
676 U8_BACK_1(s, start, i); \
677 U8_FWD_1(s, i, length); \