michael@0: /*
michael@0: *******************************************************************************
michael@0: *
michael@0: * Copyright (C) 1999-2013, International Business Machines
michael@0: * Corporation and others. All Rights Reserved.
michael@0: *
michael@0: *******************************************************************************
michael@0: * file name: utf8.h
michael@0: * encoding: US-ASCII
michael@0: * tab size: 8 (not used)
michael@0: * indentation:4
michael@0: *
michael@0: * created on: 1999sep13
michael@0: * created by: Markus W. Scherer
michael@0: */
michael@0:
michael@0: /**
michael@0: * \file
michael@0: * \brief C API: 8-bit Unicode handling macros
michael@0: *
michael@0: * This file defines macros to deal with 8-bit Unicode (UTF-8) code units (bytes) and strings.
michael@0: *
michael@0: * For more information see utf.h and the ICU User Guide Strings chapter
michael@0: * (http://userguide.icu-project.org/strings).
michael@0: *
michael@0: * Usage:
michael@0: * ICU coding guidelines for if() statements should be followed when using these macros.
michael@0: * Compound statements (curly braces {}) must be used for if-else-while...
michael@0: * bodies and all macro statements should be terminated with semicolon.
michael@0: */
michael@0:
michael@0: #ifndef __UTF8_H__
michael@0: #define __UTF8_H__
michael@0:
michael@0: #include "unicode/umachine.h"
michael@0: #ifndef __UTF_H__
michael@0: # include "unicode/utf.h"
michael@0: #endif
michael@0:
michael@0: /* internal definitions ----------------------------------------------------- */
michael@0:
michael@0: /**
michael@0: * \var utf8_countTrailBytes
michael@0: * Internal array with numbers of trail bytes for any given byte used in
michael@0: * lead byte position.
michael@0: *
michael@0: * This is internal since it is not meant to be called directly by external clients;
michael@0: * however it is called by public macros in this file and thus must remain stable,
michael@0: * and should not be hidden when other internal functions are hidden (otherwise
michael@0: * public macros would fail to compile).
michael@0: * @internal
michael@0: */
michael@0: #ifdef U_UTF8_IMPL
michael@0: U_EXPORT const uint8_t
michael@0: #elif defined(U_STATIC_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION)
michael@0: U_CFUNC const uint8_t
michael@0: #else
michael@0: U_CFUNC U_IMPORT const uint8_t /* U_IMPORT2? */ /*U_IMPORT*/
michael@0: #endif
michael@0: utf8_countTrailBytes[256];
michael@0:
michael@0: /**
michael@0: * Counts the trail bytes for a UTF-8 lead byte.
michael@0: * Returns 0 for 0..0xbf as well as for 0xfe and 0xff.
michael@0: *
michael@0: * This is internal since it is not meant to be called directly by external clients;
michael@0: * however it is called by public macros in this file and thus must remain stable.
michael@0: *
michael@0: * Note: Beginning with ICU 50, the implementation uses a multi-condition expression
michael@0: * which was shown in 2012 (on x86-64) to compile to fast, branch-free code.
michael@0: * leadByte is evaluated multiple times.
michael@0: *
michael@0: * The pre-ICU 50 implementation used the exported array utf8_countTrailBytes:
michael@0: * #define U8_COUNT_TRAIL_BYTES(leadByte) (utf8_countTrailBytes[leadByte])
michael@0: * leadByte was evaluated exactly once.
michael@0: *
michael@0: * @param leadByte The first byte of a UTF-8 sequence. Must be 0..0xff.
michael@0: * @internal
michael@0: */
michael@0: #define U8_COUNT_TRAIL_BYTES(leadByte) \
michael@0: ((leadByte)<0xf0 ? \
michael@0: ((leadByte)>=0xc0)+((leadByte)>=0xe0) : \
michael@0: (leadByte)<0xfe ? 3+((leadByte)>=0xf8)+((leadByte)>=0xfc) : 0)
michael@0:
michael@0: /**
michael@0: * Counts the trail bytes for a UTF-8 lead byte of a valid UTF-8 sequence.
michael@0: * The maximum supported lead byte is 0xf4 corresponding to U+10FFFF.
michael@0: * leadByte might be evaluated multiple times.
michael@0: *
michael@0: * This is internal since it is not meant to be called directly by external clients;
michael@0: * however it is called by public macros in this file and thus must remain stable.
michael@0: *
michael@0: * @param leadByte The first byte of a UTF-8 sequence. Must be 0..0xff.
michael@0: * @internal
michael@0: */
michael@0: #define U8_COUNT_TRAIL_BYTES_UNSAFE(leadByte) \
michael@0: (((leadByte)>=0xc0)+((leadByte)>=0xe0)+((leadByte)>=0xf0))
michael@0:
michael@0: /**
michael@0: * Mask a UTF-8 lead byte, leave only the lower bits that form part of the code point value.
michael@0: *
michael@0: * This is internal since it is not meant to be called directly by external clients;
michael@0: * however it is called by public macros in this file and thus must remain stable.
michael@0: * @internal
michael@0: */
michael@0: #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
michael@0:
michael@0: /**
michael@0: * Function for handling "next code point" with error-checking.
michael@0: *
michael@0: * This is internal since it is not meant to be called directly by external clients;
michael@0: * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
michael@0: * file and thus must remain stable, and should not be hidden when other internal
michael@0: * functions are hidden (otherwise public macros would fail to compile).
michael@0: * @internal
michael@0: */
michael@0: U_STABLE UChar32 U_EXPORT2
michael@0: utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict);
michael@0:
michael@0: /**
michael@0: * Function for handling "append code point" with error-checking.
michael@0: *
michael@0: * This is internal since it is not meant to be called directly by external clients;
michael@0: * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
michael@0: * file and thus must remain stable, and should not be hidden when other internal
michael@0: * functions are hidden (otherwise public macros would fail to compile).
michael@0: * @internal
michael@0: */
michael@0: U_STABLE int32_t U_EXPORT2
michael@0: utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError);
michael@0:
michael@0: /**
michael@0: * Function for handling "previous code point" with error-checking.
michael@0: *
michael@0: * This is internal since it is not meant to be called directly by external clients;
michael@0: * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
michael@0: * file and thus must remain stable, and should not be hidden when other internal
michael@0: * functions are hidden (otherwise public macros would fail to compile).
michael@0: * @internal
michael@0: */
michael@0: U_STABLE UChar32 U_EXPORT2
michael@0: utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict);
michael@0:
michael@0: /**
michael@0: * Function for handling "skip backward one code point" with error-checking.
michael@0: *
michael@0: * This is internal since it is not meant to be called directly by external clients;
michael@0: * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
michael@0: * file and thus must remain stable, and should not be hidden when other internal
michael@0: * functions are hidden (otherwise public macros would fail to compile).
michael@0: * @internal
michael@0: */
michael@0: U_STABLE int32_t U_EXPORT2
michael@0: utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i);
michael@0:
michael@0: /* single-code point definitions -------------------------------------------- */
michael@0:
michael@0: /**
michael@0: * Does this code unit (byte) encode a code point by itself (US-ASCII 0..0x7f)?
michael@0: * @param c 8-bit code unit (byte)
michael@0: * @return TRUE or FALSE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U8_IS_SINGLE(c) (((c)&0x80)==0)
michael@0:
michael@0: /**
michael@0: * Is this code unit (byte) a UTF-8 lead byte?
michael@0: * @param c 8-bit code unit (byte)
michael@0: * @return TRUE or FALSE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc0)<0x3e)
michael@0:
michael@0: /**
michael@0: * Is this code unit (byte) a UTF-8 trail byte?
michael@0: * @param c 8-bit code unit (byte)
michael@0: * @return TRUE or FALSE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U8_IS_TRAIL(c) (((c)&0xc0)==0x80)
michael@0:
michael@0: /**
michael@0: * How many code units (bytes) are used for the UTF-8 encoding
michael@0: * of this Unicode code point?
michael@0: * @param c 32-bit code point
michael@0: * @return 1..4, or 0 if c is a surrogate or not a Unicode code point
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U8_LENGTH(c) \
michael@0: ((uint32_t)(c)<=0x7f ? 1 : \
michael@0: ((uint32_t)(c)<=0x7ff ? 2 : \
michael@0: ((uint32_t)(c)<=0xd7ff ? 3 : \
michael@0: ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
michael@0: ((uint32_t)(c)<=0xffff ? 3 : 4)\
michael@0: ) \
michael@0: ) \
michael@0: ) \
michael@0: )
michael@0:
michael@0: /**
michael@0: * The maximum number of UTF-8 code units (bytes) per Unicode code point (U+0000..U+10ffff).
michael@0: * @return 4
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U8_MAX_LENGTH 4
michael@0:
michael@0: /**
michael@0: * Get a code point from a string at a random-access offset,
michael@0: * without changing the offset.
michael@0: * The offset may point to either the lead byte or one of the trail bytes
michael@0: * for a code point, in which case the macro will read all of the bytes
michael@0: * for the code point.
michael@0: * The result is undefined if the offset points to an illegal UTF-8
michael@0: * byte sequence.
michael@0: * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
michael@0: *
michael@0: * @param s const uint8_t * string
michael@0: * @param i string offset
michael@0: * @param c output UChar32 variable
michael@0: * @see U8_GET
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U8_GET_UNSAFE(s, i, c) { \
michael@0: int32_t _u8_get_unsafe_index=(int32_t)(i); \
michael@0: U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \
michael@0: U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Get a code point from a string at a random-access offset,
michael@0: * without changing the offset.
michael@0: * The offset may point to either the lead byte or one of the trail bytes
michael@0: * for a code point, in which case the macro will read all of the bytes
michael@0: * for the code point.
michael@0: *
michael@0: * The length can be negative for a NUL-terminated string.
michael@0: *
michael@0: * If the offset points to an illegal UTF-8 byte sequence, then
michael@0: * c is set to a negative value.
michael@0: * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
michael@0: *
michael@0: * @param s const uint8_t * string
michael@0: * @param start int32_t starting string offset
michael@0: * @param i int32_t string offset, must be start<=i=0x80) { \
michael@0: if((c)<0xe0) { \
michael@0: (c)=(((c)&0x1f)<<6)|((s)[(i)++]&0x3f); \
michael@0: } else if((c)<0xf0) { \
michael@0: /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
michael@0: (c)=(UChar)(((c)<<12)|(((s)[i]&0x3f)<<6)|((s)[(i)+1]&0x3f)); \
michael@0: (i)+=2; \
michael@0: } else { \
michael@0: (c)=(((c)&7)<<18)|(((s)[i]&0x3f)<<12)|(((s)[(i)+1]&0x3f)<<6)|((s)[(i)+2]&0x3f); \
michael@0: (i)+=3; \
michael@0: } \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Get a code point from a string at a code point boundary offset,
michael@0: * and advance the offset to the next code point boundary.
michael@0: * (Post-incrementing forward iteration.)
michael@0: * "Safe" macro, checks for illegal sequences and for string boundaries.
michael@0: *
michael@0: * The length can be negative for a NUL-terminated string.
michael@0: *
michael@0: * The offset may point to the lead byte of a multi-byte sequence,
michael@0: * in which case the macro will read the whole sequence.
michael@0: * If the offset points to a trail byte or an illegal UTF-8 sequence, then
michael@0: * c is set to a negative value.
michael@0: *
michael@0: * @param s const uint8_t * string
michael@0: * @param i int32_t string offset, must be i=0x80) { \
michael@0: uint8_t __t1, __t2; \
michael@0: if( /* handle U+1000..U+CFFF inline */ \
michael@0: (0xe0<(c) && (c)<=0xec) && \
michael@0: (((i)+1)<(length) || (length)<0) && \
michael@0: (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
michael@0: (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
michael@0: ) { \
michael@0: /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
michael@0: (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
michael@0: (i)+=2; \
michael@0: } else if( /* handle U+0080..U+07FF inline */ \
michael@0: ((c)<0xe0 && (c)>=0xc2) && \
michael@0: ((i)!=(length)) && \
michael@0: (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
michael@0: ) { \
michael@0: (c)=(((c)&0x1f)<<6)|__t1; \
michael@0: ++(i); \
michael@0: } else { \
michael@0: /* function call for "complicated" and error cases */ \
michael@0: (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (length), c, -1); \
michael@0: } \
michael@0: } \
michael@0: }
michael@0:
michael@0: #ifndef U_HIDE_DRAFT_API
michael@0: /**
michael@0: * Get a code point from a string at a code point boundary offset,
michael@0: * and advance the offset to the next code point boundary.
michael@0: * (Post-incrementing forward iteration.)
michael@0: * "Safe" macro, checks for illegal sequences and for string boundaries.
michael@0: *
michael@0: * The length can be negative for a NUL-terminated string.
michael@0: *
michael@0: * The offset may point to the lead byte of a multi-byte sequence,
michael@0: * in which case the macro will read the whole sequence.
michael@0: * If the offset points to a trail byte or an illegal UTF-8 sequence, then
michael@0: * c is set to U+FFFD.
michael@0: *
michael@0: * This macro does not distinguish between a real U+FFFD in the text
michael@0: * and U+FFFD returned for an ill-formed sequence.
michael@0: * Use U8_NEXT() if that distinction is important.
michael@0: *
michael@0: * @param s const uint8_t * string
michael@0: * @param i int32_t string offset, must be i=0x80) { \
michael@0: uint8_t __t1, __t2; \
michael@0: if( /* handle U+1000..U+CFFF inline */ \
michael@0: (0xe0<(c) && (c)<=0xec) && \
michael@0: (((i)+1)<(length) || (length)<0) && \
michael@0: (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
michael@0: (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
michael@0: ) { \
michael@0: /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
michael@0: (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
michael@0: (i)+=2; \
michael@0: } else if( /* handle U+0080..U+07FF inline */ \
michael@0: ((c)<0xe0 && (c)>=0xc2) && \
michael@0: ((i)!=(length)) && \
michael@0: (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
michael@0: ) { \
michael@0: (c)=(((c)&0x1f)<<6)|__t1; \
michael@0: ++(i); \
michael@0: } else { \
michael@0: /* function call for "complicated" and error cases */ \
michael@0: (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (length), c, -3); \
michael@0: } \
michael@0: } \
michael@0: }
michael@0: #endif /* U_HIDE_DRAFT_API */
michael@0:
michael@0: /**
michael@0: * Append a code point to a string, overwriting 1 to 4 bytes.
michael@0: * The offset points to the current end of the string contents
michael@0: * and is advanced (post-increment).
michael@0: * "Unsafe" macro, assumes a valid code point and sufficient space in the string.
michael@0: * Otherwise, the result is undefined.
michael@0: *
michael@0: * @param s const uint8_t * string buffer
michael@0: * @param i string offset
michael@0: * @param c code point to append
michael@0: * @see U8_APPEND
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U8_APPEND_UNSAFE(s, i, c) { \
michael@0: if((uint32_t)(c)<=0x7f) { \
michael@0: (s)[(i)++]=(uint8_t)(c); \
michael@0: } else { \
michael@0: if((uint32_t)(c)<=0x7ff) { \
michael@0: (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
michael@0: } else { \
michael@0: if((uint32_t)(c)<=0xffff) { \
michael@0: (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
michael@0: } else { \
michael@0: (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
michael@0: (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
michael@0: } \
michael@0: (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
michael@0: } \
michael@0: (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Append a code point to a string, overwriting 1 to 4 bytes.
michael@0: * The offset points to the current end of the string contents
michael@0: * and is advanced (post-increment).
michael@0: * "Safe" macro, checks for a valid code point.
michael@0: * If a non-ASCII code point is written, checks for sufficient space in the string.
michael@0: * If the code point is not valid or trail bytes do not fit,
michael@0: * then isError is set to TRUE.
michael@0: *
michael@0: * @param s const uint8_t * string buffer
michael@0: * @param i int32_t string offset, must be i>6)|0xc0); \
michael@0: (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
michael@0: } else if((uint32_t)(c)<=0xd7ff && (i)+2<(capacity)) { \
michael@0: (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
michael@0: (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
michael@0: (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
michael@0: } else { \
michael@0: (i)=utf8_appendCharSafeBody(s, (i), (capacity), c, &(isError)); \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Advance the string offset from one code point boundary to the next.
michael@0: * (Post-incrementing iteration.)
michael@0: * "Unsafe" macro, assumes well-formed UTF-8.
michael@0: *
michael@0: * @param s const uint8_t * string
michael@0: * @param i string offset
michael@0: * @see U8_FWD_1
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U8_FWD_1_UNSAFE(s, i) { \
michael@0: (i)+=1+U8_COUNT_TRAIL_BYTES_UNSAFE((uint8_t)(s)[i]); \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Advance the string offset from one code point boundary to the next.
michael@0: * (Post-incrementing iteration.)
michael@0: * "Safe" macro, checks for illegal sequences and for string boundaries.
michael@0: *
michael@0: * The length can be negative for a NUL-terminated string.
michael@0: *
michael@0: * @param s const uint8_t * string
michael@0: * @param i int32_t string offset, must be i(length) && (length)>=0) { \
michael@0: __count=(uint8_t)((length)-(i)); \
michael@0: } \
michael@0: while(__count>0 && U8_IS_TRAIL((s)[i])) { \
michael@0: ++(i); \
michael@0: --__count; \
michael@0: } \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Advance the string offset from one code point boundary to the n-th next one,
michael@0: * i.e., move forward by n code points.
michael@0: * (Post-incrementing iteration.)
michael@0: * "Unsafe" macro, assumes well-formed UTF-8.
michael@0: *
michael@0: * @param s const uint8_t * string
michael@0: * @param i string offset
michael@0: * @param n number of code points to skip
michael@0: * @see U8_FWD_N
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U8_FWD_N_UNSAFE(s, i, n) { \
michael@0: int32_t __N=(n); \
michael@0: while(__N>0) { \
michael@0: U8_FWD_1_UNSAFE(s, i); \
michael@0: --__N; \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Advance the string offset from one code point boundary to the n-th next one,
michael@0: * i.e., move forward by n code points.
michael@0: * (Post-incrementing iteration.)
michael@0: * "Safe" macro, checks for illegal sequences and for string boundaries.
michael@0: *
michael@0: * The length can be negative for a NUL-terminated string.
michael@0: *
michael@0: * @param s const uint8_t * string
michael@0: * @param i int32_t string offset, must be i0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
michael@0: U8_FWD_1(s, i, length); \
michael@0: --__N; \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Adjust a random-access offset to a code point boundary
michael@0: * at the start of a code point.
michael@0: * If the offset points to a UTF-8 trail byte,
michael@0: * then the offset is moved backward to the corresponding lead byte.
michael@0: * Otherwise, it is not modified.
michael@0: * "Unsafe" macro, assumes well-formed UTF-8.
michael@0: *
michael@0: * @param s const uint8_t * string
michael@0: * @param i string offset
michael@0: * @see U8_SET_CP_START
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U8_SET_CP_START_UNSAFE(s, i) { \
michael@0: while(U8_IS_TRAIL((s)[i])) { --(i); } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Adjust a random-access offset to a code point boundary
michael@0: * at the start of a code point.
michael@0: * If the offset points to a UTF-8 trail byte,
michael@0: * then the offset is moved backward to the corresponding lead byte.
michael@0: * Otherwise, it is not modified.
michael@0: * "Safe" macro, checks for illegal sequences and for string boundaries.
michael@0: *
michael@0: * @param s const uint8_t * string
michael@0: * @param start int32_t starting string offset (usually 0)
michael@0: * @param i int32_t string offset, must be start<=i
michael@0: * @see U8_SET_CP_START_UNSAFE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U8_SET_CP_START(s, start, i) { \
michael@0: if(U8_IS_TRAIL((s)[(i)])) { \
michael@0: (i)=utf8_back1SafeBody(s, start, (i)); \
michael@0: } \
michael@0: }
michael@0:
michael@0: /* definitions with backward iteration -------------------------------------- */
michael@0:
michael@0: /**
michael@0: * Move the string offset from one code point boundary to the previous one
michael@0: * and get the code point between them.
michael@0: * (Pre-decrementing backward iteration.)
michael@0: * "Unsafe" macro, assumes well-formed UTF-8.
michael@0: *
michael@0: * The input offset may be the same as the string length.
michael@0: * If the offset is behind a multi-byte sequence, then the macro will read
michael@0: * the whole sequence.
michael@0: * If the offset is behind a lead byte, then that itself
michael@0: * will be returned as the code point.
michael@0: * The result is undefined if the offset is behind an illegal UTF-8 sequence.
michael@0: *
michael@0: * @param s const uint8_t * string
michael@0: * @param i string offset
michael@0: * @param c output UChar32 variable
michael@0: * @see U8_PREV
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U8_PREV_UNSAFE(s, i, c) { \
michael@0: (c)=(uint8_t)(s)[--(i)]; \
michael@0: if(U8_IS_TRAIL(c)) { \
michael@0: uint8_t __b, __count=1, __shift=6; \
michael@0: \
michael@0: /* c is a trail byte */ \
michael@0: (c)&=0x3f; \
michael@0: for(;;) { \
michael@0: __b=(uint8_t)(s)[--(i)]; \
michael@0: if(__b>=0xc0) { \
michael@0: U8_MASK_LEAD_BYTE(__b, __count); \
michael@0: (c)|=(UChar32)__b<<__shift; \
michael@0: break; \
michael@0: } else { \
michael@0: (c)|=(UChar32)(__b&0x3f)<<__shift; \
michael@0: ++__count; \
michael@0: __shift+=6; \
michael@0: } \
michael@0: } \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Move the string offset from one code point boundary to the previous one
michael@0: * and get the code point between them.
michael@0: * (Pre-decrementing backward iteration.)
michael@0: * "Safe" macro, checks for illegal sequences and for string boundaries.
michael@0: *
michael@0: * The input offset may be the same as the string length.
michael@0: * If the offset is behind a multi-byte sequence, then the macro will read
michael@0: * the whole sequence.
michael@0: * If the offset is behind a lead byte, then that itself
michael@0: * will be returned as the code point.
michael@0: * If the offset is behind an illegal UTF-8 sequence, then c is set to a negative value.
michael@0: *
michael@0: * @param s const uint8_t * string
michael@0: * @param start int32_t starting string offset (usually 0)
michael@0: * @param i int32_t string offset, must be start=0x80) { \
michael@0: (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
michael@0: } \
michael@0: }
michael@0:
michael@0: #ifndef U_HIDE_DRAFT_API
michael@0: /**
michael@0: * Move the string offset from one code point boundary to the previous one
michael@0: * and get the code point between them.
michael@0: * (Pre-decrementing backward iteration.)
michael@0: * "Safe" macro, checks for illegal sequences and for string boundaries.
michael@0: *
michael@0: * The input offset may be the same as the string length.
michael@0: * If the offset is behind a multi-byte sequence, then the macro will read
michael@0: * the whole sequence.
michael@0: * If the offset is behind a lead byte, then that itself
michael@0: * will be returned as the code point.
michael@0: * If the offset is behind an illegal UTF-8 sequence, then c is set to U+FFFD.
michael@0: *
michael@0: * This macro does not distinguish between a real U+FFFD in the text
michael@0: * and U+FFFD returned for an ill-formed sequence.
michael@0: * Use U8_PREV() if that distinction is important.
michael@0: *
michael@0: * @param s const uint8_t * string
michael@0: * @param start int32_t starting string offset (usually 0)
michael@0: * @param i int32_t string offset, must be start=0x80) { \
michael@0: (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -3); \
michael@0: } \
michael@0: }
michael@0: #endif /* U_HIDE_DRAFT_API */
michael@0:
michael@0: /**
michael@0: * Move the string offset from one code point boundary to the previous one.
michael@0: * (Pre-decrementing backward iteration.)
michael@0: * The input offset may be the same as the string length.
michael@0: * "Unsafe" macro, assumes well-formed UTF-8.
michael@0: *
michael@0: * @param s const uint8_t * string
michael@0: * @param i string offset
michael@0: * @see U8_BACK_1
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U8_BACK_1_UNSAFE(s, i) { \
michael@0: while(U8_IS_TRAIL((s)[--(i)])) {} \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Move the string offset from one code point boundary to the previous one.
michael@0: * (Pre-decrementing backward iteration.)
michael@0: * The input offset may be the same as the string length.
michael@0: * "Safe" macro, checks for illegal sequences and for string boundaries.
michael@0: *
michael@0: * @param s const uint8_t * string
michael@0: * @param start int32_t starting string offset (usually 0)
michael@0: * @param i int32_t string offset, must be start0) { \
michael@0: U8_BACK_1_UNSAFE(s, i); \
michael@0: --__N; \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Move the string offset from one code point boundary to the n-th one before it,
michael@0: * i.e., move backward by n code points.
michael@0: * (Pre-decrementing backward iteration.)
michael@0: * The input offset may be the same as the string length.
michael@0: * "Safe" macro, checks for illegal sequences and for string boundaries.
michael@0: *
michael@0: * @param s const uint8_t * string
michael@0: * @param start int32_t index of the start of the string
michael@0: * @param i int32_t string offset, must be start0 && (i)>(start)) { \
michael@0: U8_BACK_1(s, start, i); \
michael@0: --__N; \
michael@0: } \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Adjust a random-access offset to a code point boundary after a code point.
michael@0: * If the offset is behind a partial multi-byte sequence,
michael@0: * then the offset is incremented to behind the whole sequence.
michael@0: * Otherwise, it is not modified.
michael@0: * The input offset may be the same as the string length.
michael@0: * "Unsafe" macro, assumes well-formed UTF-8.
michael@0: *
michael@0: * @param s const uint8_t * string
michael@0: * @param i string offset
michael@0: * @see U8_SET_CP_LIMIT
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \
michael@0: U8_BACK_1_UNSAFE(s, i); \
michael@0: U8_FWD_1_UNSAFE(s, i); \
michael@0: }
michael@0:
michael@0: /**
michael@0: * Adjust a random-access offset to a code point boundary after a code point.
michael@0: * If the offset is behind a partial multi-byte sequence,
michael@0: * then the offset is incremented to behind the whole sequence.
michael@0: * Otherwise, it is not modified.
michael@0: * The input offset may be the same as the string length.
michael@0: * "Safe" macro, checks for illegal sequences and for string boundaries.
michael@0: *
michael@0: * The length can be negative for a NUL-terminated string.
michael@0: *
michael@0: * @param s const uint8_t * string
michael@0: * @param start int32_t starting string offset (usually 0)
michael@0: * @param i int32_t string offset, must be start<=i<=length
michael@0: * @param length int32_t string length
michael@0: * @see U8_SET_CP_LIMIT_UNSAFE
michael@0: * @stable ICU 2.4
michael@0: */
michael@0: #define U8_SET_CP_LIMIT(s, start, i, length) { \
michael@0: if((start)<(i) && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
michael@0: U8_BACK_1(s, start, i); \
michael@0: U8_FWD_1(s, i, length); \
michael@0: } \
michael@0: }
michael@0:
michael@0: #endif