jpayne@69: // © 2016 and later: Unicode, Inc. and others.
jpayne@69: // License & terms of use: http://www.unicode.org/copyright.html
jpayne@69: /*
jpayne@69: *******************************************************************************
jpayne@69: *
jpayne@69: * Copyright (C) 1999-2015, International Business Machines
jpayne@69: * Corporation and others. All Rights Reserved.
jpayne@69: *
jpayne@69: *******************************************************************************
jpayne@69: * file name: utf8.h
jpayne@69: * encoding: UTF-8
jpayne@69: * tab size: 8 (not used)
jpayne@69: * indentation:4
jpayne@69: *
jpayne@69: * created on: 1999sep13
jpayne@69: * created by: Markus W. Scherer
jpayne@69: */
jpayne@69:
jpayne@69: /**
jpayne@69: * \file
jpayne@69: * \brief C API: 8-bit Unicode handling macros
jpayne@69: *
jpayne@69: * This file defines macros to deal with 8-bit Unicode (UTF-8) code units (bytes) and strings.
jpayne@69: *
jpayne@69: * For more information see utf.h and the ICU User Guide Strings chapter
jpayne@69: * (http://userguide.icu-project.org/strings).
jpayne@69: *
jpayne@69: * Usage:
jpayne@69: * ICU coding guidelines for if() statements should be followed when using these macros.
jpayne@69: * Compound statements (curly braces {}) must be used for if-else-while...
jpayne@69: * bodies and all macro statements should be terminated with semicolon.
jpayne@69: */
jpayne@69:
jpayne@69: #ifndef __UTF8_H__
jpayne@69: #define __UTF8_H__
jpayne@69:
jpayne@69: #include "unicode/umachine.h"
jpayne@69: #ifndef __UTF_H__
jpayne@69: # include "unicode/utf.h"
jpayne@69: #endif
jpayne@69:
jpayne@69: /* internal definitions ----------------------------------------------------- */
jpayne@69:
jpayne@69: /**
jpayne@69: * Counts the trail bytes for a UTF-8 lead byte.
jpayne@69: * Returns 0 for 0..0xc1 as well as for 0xf5..0xff.
jpayne@69: * leadByte might be evaluated multiple times.
jpayne@69: *
jpayne@69: * This is internal since it is not meant to be called directly by external clients;
jpayne@69: * however it is called by public macros in this file and thus must remain stable.
jpayne@69: *
jpayne@69: * @param leadByte The first byte of a UTF-8 sequence. Must be 0..0xff.
jpayne@69: * @internal
jpayne@69: */
jpayne@69: #define U8_COUNT_TRAIL_BYTES(leadByte) \
jpayne@69: (U8_IS_LEAD(leadByte) ? \
jpayne@69: ((uint8_t)(leadByte)>=0xe0)+((uint8_t)(leadByte)>=0xf0)+1 : 0)
jpayne@69:
jpayne@69: /**
jpayne@69: * Counts the trail bytes for a UTF-8 lead byte of a valid UTF-8 sequence.
jpayne@69: * Returns 0 for 0..0xc1. Undefined for 0xf5..0xff.
jpayne@69: * leadByte might be evaluated multiple times.
jpayne@69: *
jpayne@69: * This is internal since it is not meant to be called directly by external clients;
jpayne@69: * however it is called by public macros in this file and thus must remain stable.
jpayne@69: *
jpayne@69: * @param leadByte The first byte of a UTF-8 sequence. Must be 0..0xff.
jpayne@69: * @internal
jpayne@69: */
jpayne@69: #define U8_COUNT_TRAIL_BYTES_UNSAFE(leadByte) \
jpayne@69: (((uint8_t)(leadByte)>=0xc2)+((uint8_t)(leadByte)>=0xe0)+((uint8_t)(leadByte)>=0xf0))
jpayne@69:
jpayne@69: /**
jpayne@69: * Mask a UTF-8 lead byte, leave only the lower bits that form part of the code point value.
jpayne@69: *
jpayne@69: * This is internal since it is not meant to be called directly by external clients;
jpayne@69: * however it is called by public macros in this file and thus must remain stable.
jpayne@69: * @internal
jpayne@69: */
jpayne@69: #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
jpayne@69:
jpayne@69: /**
jpayne@69: * Internal bit vector for 3-byte UTF-8 validity check, for use in U8_IS_VALID_LEAD3_AND_T1.
jpayne@69: * Each bit indicates whether one lead byte + first trail byte pair starts a valid sequence.
jpayne@69: * Lead byte E0..EF bits 3..0 are used as byte index,
jpayne@69: * first trail byte bits 7..5 are used as bit index into that byte.
jpayne@69: * @see U8_IS_VALID_LEAD3_AND_T1
jpayne@69: * @internal
jpayne@69: */
jpayne@69: #define U8_LEAD3_T1_BITS "\x20\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x10\x30\x30"
jpayne@69:
jpayne@69: /**
jpayne@69: * Internal 3-byte UTF-8 validity check.
jpayne@69: * Non-zero if lead byte E0..EF and first trail byte 00..FF start a valid sequence.
jpayne@69: * @internal
jpayne@69: */
jpayne@69: #define U8_IS_VALID_LEAD3_AND_T1(lead, t1) (U8_LEAD3_T1_BITS[(lead)&0xf]&(1<<((uint8_t)(t1)>>5)))
jpayne@69:
jpayne@69: /**
jpayne@69: * Internal bit vector for 4-byte UTF-8 validity check, for use in U8_IS_VALID_LEAD4_AND_T1.
jpayne@69: * Each bit indicates whether one lead byte + first trail byte pair starts a valid sequence.
jpayne@69: * First trail byte bits 7..4 are used as byte index,
jpayne@69: * lead byte F0..F4 bits 2..0 are used as bit index into that byte.
jpayne@69: * @see U8_IS_VALID_LEAD4_AND_T1
jpayne@69: * @internal
jpayne@69: */
jpayne@69: #define U8_LEAD4_T1_BITS "\x00\x00\x00\x00\x00\x00\x00\x00\x1E\x0F\x0F\x0F\x00\x00\x00\x00"
jpayne@69:
jpayne@69: /**
jpayne@69: * Internal 4-byte UTF-8 validity check.
jpayne@69: * Non-zero if lead byte F0..F4 and first trail byte 00..FF start a valid sequence.
jpayne@69: * @internal
jpayne@69: */
jpayne@69: #define U8_IS_VALID_LEAD4_AND_T1(lead, t1) (U8_LEAD4_T1_BITS[(uint8_t)(t1)>>4]&(1<<((lead)&7)))
jpayne@69:
jpayne@69: /**
jpayne@69: * Function for handling "next code point" with error-checking.
jpayne@69: *
jpayne@69: * This is internal since it is not meant to be called directly by external clients;
jpayne@69: * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
jpayne@69: * file and thus must remain stable, and should not be hidden when other internal
jpayne@69: * functions are hidden (otherwise public macros would fail to compile).
jpayne@69: * @internal
jpayne@69: */
jpayne@69: U_STABLE UChar32 U_EXPORT2
jpayne@69: utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict);
jpayne@69:
jpayne@69: /**
jpayne@69: * Function for handling "append code point" with error-checking.
jpayne@69: *
jpayne@69: * This is internal since it is not meant to be called directly by external clients;
jpayne@69: * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
jpayne@69: * file and thus must remain stable, and should not be hidden when other internal
jpayne@69: * functions are hidden (otherwise public macros would fail to compile).
jpayne@69: * @internal
jpayne@69: */
jpayne@69: U_STABLE int32_t U_EXPORT2
jpayne@69: utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError);
jpayne@69:
jpayne@69: /**
jpayne@69: * Function for handling "previous code point" with error-checking.
jpayne@69: *
jpayne@69: * This is internal since it is not meant to be called directly by external clients;
jpayne@69: * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
jpayne@69: * file and thus must remain stable, and should not be hidden when other internal
jpayne@69: * functions are hidden (otherwise public macros would fail to compile).
jpayne@69: * @internal
jpayne@69: */
jpayne@69: U_STABLE UChar32 U_EXPORT2
jpayne@69: utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict);
jpayne@69:
jpayne@69: /**
jpayne@69: * Function for handling "skip backward one code point" with error-checking.
jpayne@69: *
jpayne@69: * This is internal since it is not meant to be called directly by external clients;
jpayne@69: * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
jpayne@69: * file and thus must remain stable, and should not be hidden when other internal
jpayne@69: * functions are hidden (otherwise public macros would fail to compile).
jpayne@69: * @internal
jpayne@69: */
jpayne@69: U_STABLE int32_t U_EXPORT2
jpayne@69: utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i);
jpayne@69:
jpayne@69: /* single-code point definitions -------------------------------------------- */
jpayne@69:
jpayne@69: /**
jpayne@69: * Does this code unit (byte) encode a code point by itself (US-ASCII 0..0x7f)?
jpayne@69: * @param c 8-bit code unit (byte)
jpayne@69: * @return TRUE or FALSE
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: #define U8_IS_SINGLE(c) (((c)&0x80)==0)
jpayne@69:
jpayne@69: /**
jpayne@69: * Is this code unit (byte) a UTF-8 lead byte? (0xC2..0xF4)
jpayne@69: * @param c 8-bit code unit (byte)
jpayne@69: * @return TRUE or FALSE
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc2)<=0x32)
jpayne@69: // 0x32=0xf4-0xc2
jpayne@69:
jpayne@69: /**
jpayne@69: * Is this code unit (byte) a UTF-8 trail byte? (0x80..0xBF)
jpayne@69: * @param c 8-bit code unit (byte)
jpayne@69: * @return TRUE or FALSE
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: #define U8_IS_TRAIL(c) ((int8_t)(c)<-0x40)
jpayne@69:
jpayne@69: /**
jpayne@69: * How many code units (bytes) are used for the UTF-8 encoding
jpayne@69: * of this Unicode code point?
jpayne@69: * @param c 32-bit code point
jpayne@69: * @return 1..4, or 0 if c is a surrogate or not a Unicode code point
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: #define U8_LENGTH(c) \
jpayne@69: ((uint32_t)(c)<=0x7f ? 1 : \
jpayne@69: ((uint32_t)(c)<=0x7ff ? 2 : \
jpayne@69: ((uint32_t)(c)<=0xd7ff ? 3 : \
jpayne@69: ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
jpayne@69: ((uint32_t)(c)<=0xffff ? 3 : 4)\
jpayne@69: ) \
jpayne@69: ) \
jpayne@69: ) \
jpayne@69: )
jpayne@69:
jpayne@69: /**
jpayne@69: * The maximum number of UTF-8 code units (bytes) per Unicode code point (U+0000..U+10ffff).
jpayne@69: * @return 4
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: #define U8_MAX_LENGTH 4
jpayne@69:
jpayne@69: /**
jpayne@69: * Get a code point from a string at a random-access offset,
jpayne@69: * without changing the offset.
jpayne@69: * The offset may point to either the lead byte or one of the trail bytes
jpayne@69: * for a code point, in which case the macro will read all of the bytes
jpayne@69: * for the code point.
jpayne@69: * The result is undefined if the offset points to an illegal UTF-8
jpayne@69: * byte sequence.
jpayne@69: * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
jpayne@69: *
jpayne@69: * @param s const uint8_t * string
jpayne@69: * @param i string offset
jpayne@69: * @param c output UChar32 variable
jpayne@69: * @see U8_GET
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: #define U8_GET_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
jpayne@69: int32_t _u8_get_unsafe_index=(int32_t)(i); \
jpayne@69: U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \
jpayne@69: U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \
jpayne@69: } UPRV_BLOCK_MACRO_END
jpayne@69:
jpayne@69: /**
jpayne@69: * Get a code point from a string at a random-access offset,
jpayne@69: * without changing the offset.
jpayne@69: * The offset may point to either the lead byte or one of the trail bytes
jpayne@69: * for a code point, in which case the macro will read all of the bytes
jpayne@69: * for the code point.
jpayne@69: *
jpayne@69: * The length can be negative for a NUL-terminated string.
jpayne@69: *
jpayne@69: * If the offset points to an illegal UTF-8 byte sequence, then
jpayne@69: * c is set to a negative value.
jpayne@69: * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
jpayne@69: *
jpayne@69: * @param s const uint8_t * string
jpayne@69: * @param start int32_t starting string offset
jpayne@69: * @param i int32_t string offset, must be start<=i=0xe0 ? \
jpayne@69: ((c)<0xf0 ? /* U+0800..U+FFFF except surrogates */ \
jpayne@69: U8_LEAD3_T1_BITS[(c)&=0xf]&(1<<((__t=(s)[i])>>5)) && \
jpayne@69: (__t&=0x3f, 1) \
jpayne@69: : /* U+10000..U+10FFFF */ \
jpayne@69: ((c)-=0xf0)<=4 && \
jpayne@69: U8_LEAD4_T1_BITS[(__t=(s)[i])>>4]&(1<<(c)) && \
jpayne@69: ((c)=((c)<<6)|(__t&0x3f), ++(i)!=(length)) && \
jpayne@69: (__t=(s)[i]-0x80)<=0x3f) && \
jpayne@69: /* valid second-to-last trail byte */ \
jpayne@69: ((c)=((c)<<6)|__t, ++(i)!=(length)) \
jpayne@69: : /* U+0080..U+07FF */ \
jpayne@69: (c)>=0xc2 && ((c)&=0x1f, 1)) && \
jpayne@69: /* last trail byte */ \
jpayne@69: (__t=(s)[i]-0x80)<=0x3f && \
jpayne@69: ((c)=((c)<<6)|__t, ++(i), 1)) { \
jpayne@69: } else { \
jpayne@69: (c)=(sub); /* ill-formed*/ \
jpayne@69: } \
jpayne@69: } \
jpayne@69: } UPRV_BLOCK_MACRO_END
jpayne@69:
jpayne@69: /**
jpayne@69: * Append a code point to a string, overwriting 1 to 4 bytes.
jpayne@69: * The offset points to the current end of the string contents
jpayne@69: * and is advanced (post-increment).
jpayne@69: * "Unsafe" macro, assumes a valid code point and sufficient space in the string.
jpayne@69: * Otherwise, the result is undefined.
jpayne@69: *
jpayne@69: * @param s const uint8_t * string buffer
jpayne@69: * @param i string offset
jpayne@69: * @param c code point to append
jpayne@69: * @see U8_APPEND
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: #define U8_APPEND_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
jpayne@69: uint32_t __uc=(c); \
jpayne@69: if(__uc<=0x7f) { \
jpayne@69: (s)[(i)++]=(uint8_t)__uc; \
jpayne@69: } else { \
jpayne@69: if(__uc<=0x7ff) { \
jpayne@69: (s)[(i)++]=(uint8_t)((__uc>>6)|0xc0); \
jpayne@69: } else { \
jpayne@69: if(__uc<=0xffff) { \
jpayne@69: (s)[(i)++]=(uint8_t)((__uc>>12)|0xe0); \
jpayne@69: } else { \
jpayne@69: (s)[(i)++]=(uint8_t)((__uc>>18)|0xf0); \
jpayne@69: (s)[(i)++]=(uint8_t)(((__uc>>12)&0x3f)|0x80); \
jpayne@69: } \
jpayne@69: (s)[(i)++]=(uint8_t)(((__uc>>6)&0x3f)|0x80); \
jpayne@69: } \
jpayne@69: (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \
jpayne@69: } \
jpayne@69: } UPRV_BLOCK_MACRO_END
jpayne@69:
jpayne@69: /**
jpayne@69: * Append a code point to a string, overwriting 1 to 4 bytes.
jpayne@69: * The offset points to the current end of the string contents
jpayne@69: * and is advanced (post-increment).
jpayne@69: * "Safe" macro, checks for a valid code point.
jpayne@69: * If a non-ASCII code point is written, checks for sufficient space in the string.
jpayne@69: * If the code point is not valid or trail bytes do not fit,
jpayne@69: * then isError is set to TRUE.
jpayne@69: *
jpayne@69: * @param s const uint8_t * string buffer
jpayne@69: * @param i int32_t string offset, must be i>6)|0xc0); \
jpayne@69: (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \
jpayne@69: } else if((__uc<=0xd7ff || (0xe000<=__uc && __uc<=0xffff)) && (i)+2<(capacity)) { \
jpayne@69: (s)[(i)++]=(uint8_t)((__uc>>12)|0xe0); \
jpayne@69: (s)[(i)++]=(uint8_t)(((__uc>>6)&0x3f)|0x80); \
jpayne@69: (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \
jpayne@69: } else if(0xffff<__uc && __uc<=0x10ffff && (i)+3<(capacity)) { \
jpayne@69: (s)[(i)++]=(uint8_t)((__uc>>18)|0xf0); \
jpayne@69: (s)[(i)++]=(uint8_t)(((__uc>>12)&0x3f)|0x80); \
jpayne@69: (s)[(i)++]=(uint8_t)(((__uc>>6)&0x3f)|0x80); \
jpayne@69: (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \
jpayne@69: } else { \
jpayne@69: (isError)=TRUE; \
jpayne@69: } \
jpayne@69: } UPRV_BLOCK_MACRO_END
jpayne@69:
jpayne@69: /**
jpayne@69: * Advance the string offset from one code point boundary to the next.
jpayne@69: * (Post-incrementing iteration.)
jpayne@69: * "Unsafe" macro, assumes well-formed UTF-8.
jpayne@69: *
jpayne@69: * @param s const uint8_t * string
jpayne@69: * @param i string offset
jpayne@69: * @see U8_FWD_1
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: #define U8_FWD_1_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
jpayne@69: (i)+=1+U8_COUNT_TRAIL_BYTES_UNSAFE((s)[i]); \
jpayne@69: } UPRV_BLOCK_MACRO_END
jpayne@69:
jpayne@69: /**
jpayne@69: * Advance the string offset from one code point boundary to the next.
jpayne@69: * (Post-incrementing iteration.)
jpayne@69: * "Safe" macro, checks for illegal sequences and for string boundaries.
jpayne@69: *
jpayne@69: * The length can be negative for a NUL-terminated string.
jpayne@69: *
jpayne@69: * @param s const uint8_t * string
jpayne@69: * @param i int32_t string offset, must be i=0xf0 */ { \
jpayne@69: if(U8_IS_VALID_LEAD4_AND_T1(__b, __t1) && \
jpayne@69: ++(i)!=(length) && U8_IS_TRAIL((s)[i]) && \
jpayne@69: ++(i)!=(length) && U8_IS_TRAIL((s)[i])) { \
jpayne@69: ++(i); \
jpayne@69: } \
jpayne@69: } \
jpayne@69: } \
jpayne@69: } UPRV_BLOCK_MACRO_END
jpayne@69:
jpayne@69: /**
jpayne@69: * Advance the string offset from one code point boundary to the n-th next one,
jpayne@69: * i.e., move forward by n code points.
jpayne@69: * (Post-incrementing iteration.)
jpayne@69: * "Unsafe" macro, assumes well-formed UTF-8.
jpayne@69: *
jpayne@69: * @param s const uint8_t * string
jpayne@69: * @param i string offset
jpayne@69: * @param n number of code points to skip
jpayne@69: * @see U8_FWD_N
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: #define U8_FWD_N_UNSAFE(s, i, n) UPRV_BLOCK_MACRO_BEGIN { \
jpayne@69: int32_t __N=(n); \
jpayne@69: while(__N>0) { \
jpayne@69: U8_FWD_1_UNSAFE(s, i); \
jpayne@69: --__N; \
jpayne@69: } \
jpayne@69: } UPRV_BLOCK_MACRO_END
jpayne@69:
jpayne@69: /**
jpayne@69: * Advance the string offset from one code point boundary to the n-th next one,
jpayne@69: * i.e., move forward by n code points.
jpayne@69: * (Post-incrementing iteration.)
jpayne@69: * "Safe" macro, checks for illegal sequences and for string boundaries.
jpayne@69: *
jpayne@69: * The length can be negative for a NUL-terminated string.
jpayne@69: *
jpayne@69: * @param s const uint8_t * string
jpayne@69: * @param i int32_t string offset, must be i0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
jpayne@69: U8_FWD_1(s, i, length); \
jpayne@69: --__N; \
jpayne@69: } \
jpayne@69: } UPRV_BLOCK_MACRO_END
jpayne@69:
jpayne@69: /**
jpayne@69: * Adjust a random-access offset to a code point boundary
jpayne@69: * at the start of a code point.
jpayne@69: * If the offset points to a UTF-8 trail byte,
jpayne@69: * then the offset is moved backward to the corresponding lead byte.
jpayne@69: * Otherwise, it is not modified.
jpayne@69: * "Unsafe" macro, assumes well-formed UTF-8.
jpayne@69: *
jpayne@69: * @param s const uint8_t * string
jpayne@69: * @param i string offset
jpayne@69: * @see U8_SET_CP_START
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: #define U8_SET_CP_START_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
jpayne@69: while(U8_IS_TRAIL((s)[i])) { --(i); } \
jpayne@69: } UPRV_BLOCK_MACRO_END
jpayne@69:
jpayne@69: /**
jpayne@69: * Adjust a random-access offset to a code point boundary
jpayne@69: * at the start of a code point.
jpayne@69: * If the offset points to a UTF-8 trail byte,
jpayne@69: * then the offset is moved backward to the corresponding lead byte.
jpayne@69: * Otherwise, it is not modified.
jpayne@69: *
jpayne@69: * "Safe" macro, checks for illegal sequences and for string boundaries.
jpayne@69: * Unlike U8_TRUNCATE_IF_INCOMPLETE(), this macro always reads s[i].
jpayne@69: *
jpayne@69: * @param s const uint8_t * string
jpayne@69: * @param start int32_t starting string offset (usually 0)
jpayne@69: * @param i int32_t string offset, must be start<=i
jpayne@69: * @see U8_SET_CP_START_UNSAFE
jpayne@69: * @see U8_TRUNCATE_IF_INCOMPLETE
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: #define U8_SET_CP_START(s, start, i) UPRV_BLOCK_MACRO_BEGIN { \
jpayne@69: if(U8_IS_TRAIL((s)[(i)])) { \
jpayne@69: (i)=utf8_back1SafeBody(s, start, (i)); \
jpayne@69: } \
jpayne@69: } UPRV_BLOCK_MACRO_END
jpayne@69:
jpayne@69: /**
jpayne@69: * If the string ends with a UTF-8 byte sequence that is valid so far
jpayne@69: * but incomplete, then reduce the length of the string to end before
jpayne@69: * the lead byte of that incomplete sequence.
jpayne@69: * For example, if the string ends with E1 80, the length is reduced by 2.
jpayne@69: *
jpayne@69: * In all other cases (the string ends with a complete sequence, or it is not
jpayne@69: * possible for any further trail byte to extend the trailing sequence)
jpayne@69: * the length remains unchanged.
jpayne@69: *
jpayne@69: * Useful for processing text split across multiple buffers
jpayne@69: * (save the incomplete sequence for later)
jpayne@69: * and for optimizing iteration
jpayne@69: * (check for string length only once per character).
jpayne@69: *
jpayne@69: * "Safe" macro, checks for illegal sequences and for string boundaries.
jpayne@69: * Unlike U8_SET_CP_START(), this macro never reads s[length].
jpayne@69: *
jpayne@69: * (In UTF-16, simply check for U16_IS_LEAD(last code unit).)
jpayne@69: *
jpayne@69: * @param s const uint8_t * string
jpayne@69: * @param start int32_t starting string offset (usually 0)
jpayne@69: * @param length int32_t string length (usually start<=length)
jpayne@69: * @see U8_SET_CP_START
jpayne@69: * @stable ICU 61
jpayne@69: */
jpayne@69: #define U8_TRUNCATE_IF_INCOMPLETE(s, start, length) UPRV_BLOCK_MACRO_BEGIN { \
jpayne@69: if((length)>(start)) { \
jpayne@69: uint8_t __b1=s[(length)-1]; \
jpayne@69: if(U8_IS_SINGLE(__b1)) { \
jpayne@69: /* common ASCII character */ \
jpayne@69: } else if(U8_IS_LEAD(__b1)) { \
jpayne@69: --(length); \
jpayne@69: } else if(U8_IS_TRAIL(__b1) && ((length)-2)>=(start)) { \
jpayne@69: uint8_t __b2=s[(length)-2]; \
jpayne@69: if(0xe0<=__b2 && __b2<=0xf4) { \
jpayne@69: if(__b2<0xf0 ? U8_IS_VALID_LEAD3_AND_T1(__b2, __b1) : \
jpayne@69: U8_IS_VALID_LEAD4_AND_T1(__b2, __b1)) { \
jpayne@69: (length)-=2; \
jpayne@69: } \
jpayne@69: } else if(U8_IS_TRAIL(__b2) && ((length)-3)>=(start)) { \
jpayne@69: uint8_t __b3=s[(length)-3]; \
jpayne@69: if(0xf0<=__b3 && __b3<=0xf4 && U8_IS_VALID_LEAD4_AND_T1(__b3, __b2)) { \
jpayne@69: (length)-=3; \
jpayne@69: } \
jpayne@69: } \
jpayne@69: } \
jpayne@69: } \
jpayne@69: } UPRV_BLOCK_MACRO_END
jpayne@69:
jpayne@69: /* definitions with backward iteration -------------------------------------- */
jpayne@69:
jpayne@69: /**
jpayne@69: * Move the string offset from one code point boundary to the previous one
jpayne@69: * and get the code point between them.
jpayne@69: * (Pre-decrementing backward iteration.)
jpayne@69: * "Unsafe" macro, assumes well-formed UTF-8.
jpayne@69: *
jpayne@69: * The input offset may be the same as the string length.
jpayne@69: * If the offset is behind a multi-byte sequence, then the macro will read
jpayne@69: * the whole sequence.
jpayne@69: * If the offset is behind a lead byte, then that itself
jpayne@69: * will be returned as the code point.
jpayne@69: * The result is undefined if the offset is behind an illegal UTF-8 sequence.
jpayne@69: *
jpayne@69: * @param s const uint8_t * string
jpayne@69: * @param i string offset
jpayne@69: * @param c output UChar32 variable
jpayne@69: * @see U8_PREV
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: #define U8_PREV_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
jpayne@69: (c)=(uint8_t)(s)[--(i)]; \
jpayne@69: if(U8_IS_TRAIL(c)) { \
jpayne@69: uint8_t __b, __count=1, __shift=6; \
jpayne@69: \
jpayne@69: /* c is a trail byte */ \
jpayne@69: (c)&=0x3f; \
jpayne@69: for(;;) { \
jpayne@69: __b=(s)[--(i)]; \
jpayne@69: if(__b>=0xc0) { \
jpayne@69: U8_MASK_LEAD_BYTE(__b, __count); \
jpayne@69: (c)|=(UChar32)__b<<__shift; \
jpayne@69: break; \
jpayne@69: } else { \
jpayne@69: (c)|=(UChar32)(__b&0x3f)<<__shift; \
jpayne@69: ++__count; \
jpayne@69: __shift+=6; \
jpayne@69: } \
jpayne@69: } \
jpayne@69: } \
jpayne@69: } UPRV_BLOCK_MACRO_END
jpayne@69:
jpayne@69: /**
jpayne@69: * Move the string offset from one code point boundary to the previous one
jpayne@69: * and get the code point between them.
jpayne@69: * (Pre-decrementing backward iteration.)
jpayne@69: * "Safe" macro, checks for illegal sequences and for string boundaries.
jpayne@69: *
jpayne@69: * The input offset may be the same as the string length.
jpayne@69: * If the offset is behind a multi-byte sequence, then the macro will read
jpayne@69: * the whole sequence.
jpayne@69: * If the offset is behind a lead byte, then that itself
jpayne@69: * will be returned as the code point.
jpayne@69: * If the offset is behind an illegal UTF-8 sequence, then c is set to a negative value.
jpayne@69: *
jpayne@69: * @param s const uint8_t * string
jpayne@69: * @param start int32_t starting string offset (usually 0)
jpayne@69: * @param i int32_t string offset, must be start0) { \
jpayne@69: U8_BACK_1_UNSAFE(s, i); \
jpayne@69: --__N; \
jpayne@69: } \
jpayne@69: } UPRV_BLOCK_MACRO_END
jpayne@69:
jpayne@69: /**
jpayne@69: * Move the string offset from one code point boundary to the n-th one before it,
jpayne@69: * i.e., move backward by n code points.
jpayne@69: * (Pre-decrementing backward iteration.)
jpayne@69: * The input offset may be the same as the string length.
jpayne@69: * "Safe" macro, checks for illegal sequences and for string boundaries.
jpayne@69: *
jpayne@69: * @param s const uint8_t * string
jpayne@69: * @param start int32_t index of the start of the string
jpayne@69: * @param i int32_t string offset, must be start0 && (i)>(start)) { \
jpayne@69: U8_BACK_1(s, start, i); \
jpayne@69: --__N; \
jpayne@69: } \
jpayne@69: } UPRV_BLOCK_MACRO_END
jpayne@69:
jpayne@69: /**
jpayne@69: * Adjust a random-access offset to a code point boundary after a code point.
jpayne@69: * If the offset is behind a partial multi-byte sequence,
jpayne@69: * then the offset is incremented to behind the whole sequence.
jpayne@69: * Otherwise, it is not modified.
jpayne@69: * The input offset may be the same as the string length.
jpayne@69: * "Unsafe" macro, assumes well-formed UTF-8.
jpayne@69: *
jpayne@69: * @param s const uint8_t * string
jpayne@69: * @param i string offset
jpayne@69: * @see U8_SET_CP_LIMIT
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: #define U8_SET_CP_LIMIT_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
jpayne@69: U8_BACK_1_UNSAFE(s, i); \
jpayne@69: U8_FWD_1_UNSAFE(s, i); \
jpayne@69: } UPRV_BLOCK_MACRO_END
jpayne@69:
jpayne@69: /**
jpayne@69: * Adjust a random-access offset to a code point boundary after a code point.
jpayne@69: * If the offset is behind a partial multi-byte sequence,
jpayne@69: * then the offset is incremented to behind the whole sequence.
jpayne@69: * Otherwise, it is not modified.
jpayne@69: * The input offset may be the same as the string length.
jpayne@69: * "Safe" macro, checks for illegal sequences and for string boundaries.
jpayne@69: *
jpayne@69: * The length can be negative for a NUL-terminated string.
jpayne@69: *
jpayne@69: * @param s const uint8_t * string
jpayne@69: * @param start int32_t starting string offset (usually 0)
jpayne@69: * @param i int32_t string offset, must be start<=i<=length
jpayne@69: * @param length int32_t string length
jpayne@69: * @see U8_SET_CP_LIMIT_UNSAFE
jpayne@69: * @stable ICU 2.4
jpayne@69: */
jpayne@69: #define U8_SET_CP_LIMIT(s, start, i, length) UPRV_BLOCK_MACRO_BEGIN { \
jpayne@69: if((start)<(i) && ((i)<(length) || (length)<0)) { \
jpayne@69: U8_BACK_1(s, start, i); \
jpayne@69: U8_FWD_1(s, i, length); \
jpayne@69: } \
jpayne@69: } UPRV_BLOCK_MACRO_END
jpayne@69:
jpayne@69: #endif