Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:58:55

0001 // © 2016 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 /*
0004 *******************************************************************************
0005 *
0006 *   Copyright (C) 1999-2015, International Business Machines
0007 *   Corporation and others.  All Rights Reserved.
0008 *
0009 *******************************************************************************
0010 *   file name:  utf8.h
0011 *   encoding:   UTF-8
0012 *   tab size:   8 (not used)
0013 *   indentation:4
0014 *
0015 *   created on: 1999sep13
0016 *   created by: Markus W. Scherer
0017 */
0018 
0019 /**
0020  * \file
0021  * \brief C API: 8-bit Unicode handling macros
0022  * 
0023  * This file defines macros to deal with 8-bit Unicode (UTF-8) code units (bytes) and strings.
0024  *
0025  * For more information see utf.h and the ICU User Guide Strings chapter
0026  * (http://userguide.icu-project.org/strings).
0027  *
0028  * <em>Usage:</em>
0029  * ICU coding guidelines for if() statements should be followed when using these macros.
0030  * Compound statements (curly braces {}) must be used  for if-else-while... 
0031  * bodies and all macro statements should be terminated with semicolon.
0032  */
0033 
0034 #ifndef __UTF8_H__
0035 #define __UTF8_H__
0036 
0037 #include "unicode/umachine.h"
0038 #ifndef __UTF_H__
0039 #   include "unicode/utf.h"
0040 #endif
0041 
0042 /* internal definitions ----------------------------------------------------- */
0043 
0044 /**
0045  * Counts the trail bytes for a UTF-8 lead byte.
0046  * Returns 0 for 0..0xc1 as well as for 0xf5..0xff.
0047  * leadByte might be evaluated multiple times.
0048  *
0049  * This is internal since it is not meant to be called directly by external clients;
0050  * however it is called by public macros in this file and thus must remain stable.
0051  *
0052  * @param leadByte The first byte of a UTF-8 sequence. Must be 0..0xff.
0053  * @internal
0054  */
0055 #define U8_COUNT_TRAIL_BYTES(leadByte) \
0056     (U8_IS_LEAD(leadByte) ? \
0057         ((uint8_t)(leadByte)>=0xe0)+((uint8_t)(leadByte)>=0xf0)+1 : 0)
0058 
0059 /**
0060  * Counts the trail bytes for a UTF-8 lead byte of a valid UTF-8 sequence.
0061  * Returns 0 for 0..0xc1. Undefined for 0xf5..0xff.
0062  * leadByte might be evaluated multiple times.
0063  *
0064  * This is internal since it is not meant to be called directly by external clients;
0065  * however it is called by public macros in this file and thus must remain stable.
0066  *
0067  * @param leadByte The first byte of a UTF-8 sequence. Must be 0..0xff.
0068  * @internal
0069  */
0070 #define U8_COUNT_TRAIL_BYTES_UNSAFE(leadByte) \
0071     (((uint8_t)(leadByte)>=0xc2)+((uint8_t)(leadByte)>=0xe0)+((uint8_t)(leadByte)>=0xf0))
0072 
0073 /**
0074  * Mask a UTF-8 lead byte, leave only the lower bits that form part of the code point value.
0075  *
0076  * This is internal since it is not meant to be called directly by external clients;
0077  * however it is called by public macros in this file and thus must remain stable.
0078  * @internal
0079  */
0080 #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
0081 
0082 /**
0083  * Internal bit vector for 3-byte UTF-8 validity check, for use in U8_IS_VALID_LEAD3_AND_T1.
0084  * Each bit indicates whether one lead byte + first trail byte pair starts a valid sequence.
0085  * Lead byte E0..EF bits 3..0 are used as byte index,
0086  * first trail byte bits 7..5 are used as bit index into that byte.
0087  * @see U8_IS_VALID_LEAD3_AND_T1
0088  * @internal
0089  */
0090 #define U8_LEAD3_T1_BITS "\x20\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x10\x30\x30"
0091 
0092 /**
0093  * Internal 3-byte UTF-8 validity check.
0094  * Non-zero if lead byte E0..EF and first trail byte 00..FF start a valid sequence.
0095  * @internal
0096  */
0097 #define U8_IS_VALID_LEAD3_AND_T1(lead, t1) (U8_LEAD3_T1_BITS[(lead)&0xf]&(1<<((uint8_t)(t1)>>5)))
0098 
0099 /**
0100  * Internal bit vector for 4-byte UTF-8 validity check, for use in U8_IS_VALID_LEAD4_AND_T1.
0101  * Each bit indicates whether one lead byte + first trail byte pair starts a valid sequence.
0102  * First trail byte bits 7..4 are used as byte index,
0103  * lead byte F0..F4 bits 2..0 are used as bit index into that byte.
0104  * @see U8_IS_VALID_LEAD4_AND_T1
0105  * @internal
0106  */
0107 #define U8_LEAD4_T1_BITS "\x00\x00\x00\x00\x00\x00\x00\x00\x1E\x0F\x0F\x0F\x00\x00\x00\x00"
0108 
0109 /**
0110  * Internal 4-byte UTF-8 validity check.
0111  * Non-zero if lead byte F0..F4 and first trail byte 00..FF start a valid sequence.
0112  * @internal
0113  */
0114 #define U8_IS_VALID_LEAD4_AND_T1(lead, t1) (U8_LEAD4_T1_BITS[(uint8_t)(t1)>>4]&(1<<((lead)&7)))
0115 
0116 /**
0117  * Function for handling "next code point" with error-checking.
0118  *
0119  * This is internal since it is not meant to be called directly by external clients;
0120  * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
0121  * file and thus must remain stable, and should not be hidden when other internal
0122  * functions are hidden (otherwise public macros would fail to compile).
0123  * @internal
0124  */
0125 U_STABLE UChar32 U_EXPORT2
0126 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict);
0127 
0128 /**
0129  * Function for handling "append code point" with error-checking.
0130  *
0131  * This is internal since it is not meant to be called directly by external clients;
0132  * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
0133  * file and thus must remain stable, and should not be hidden when other internal
0134  * functions are hidden (otherwise public macros would fail to compile).
0135  * @internal
0136  */
0137 U_STABLE int32_t U_EXPORT2
0138 utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError);
0139 
0140 /**
0141  * Function for handling "previous code point" with error-checking.
0142  *
0143  * This is internal since it is not meant to be called directly by external clients;
0144  * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
0145  * file and thus must remain stable, and should not be hidden when other internal
0146  * functions are hidden (otherwise public macros would fail to compile).
0147  * @internal
0148  */
0149 U_STABLE UChar32 U_EXPORT2
0150 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict);
0151 
0152 /**
0153  * Function for handling "skip backward one code point" with error-checking.
0154  *
0155  * This is internal since it is not meant to be called directly by external clients;
0156  * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
0157  * file and thus must remain stable, and should not be hidden when other internal
0158  * functions are hidden (otherwise public macros would fail to compile).
0159  * @internal
0160  */
0161 U_STABLE int32_t U_EXPORT2
0162 utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i);
0163 
0164 /* single-code point definitions -------------------------------------------- */
0165 
0166 /**
0167  * Does this code unit (byte) encode a code point by itself (US-ASCII 0..0x7f)?
0168  * @param c 8-bit code unit (byte)
0169  * @return TRUE or FALSE
0170  * @stable ICU 2.4
0171  */
0172 #define U8_IS_SINGLE(c) (((c)&0x80)==0)
0173 
0174 /**
0175  * Is this code unit (byte) a UTF-8 lead byte? (0xC2..0xF4)
0176  * @param c 8-bit code unit (byte)
0177  * @return TRUE or FALSE
0178  * @stable ICU 2.4
0179  */
0180 #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc2)<=0x32)
0181 // 0x32=0xf4-0xc2
0182 
0183 /**
0184  * Is this code unit (byte) a UTF-8 trail byte? (0x80..0xBF)
0185  * @param c 8-bit code unit (byte)
0186  * @return TRUE or FALSE
0187  * @stable ICU 2.4
0188  */
0189 #define U8_IS_TRAIL(c) ((int8_t)(c)<-0x40)
0190 
0191 /**
0192  * How many code units (bytes) are used for the UTF-8 encoding
0193  * of this Unicode code point?
0194  * @param c 32-bit code point
0195  * @return 1..4, or 0 if c is a surrogate or not a Unicode code point
0196  * @stable ICU 2.4
0197  */
0198 #define U8_LENGTH(c) \
0199     ((uint32_t)(c)<=0x7f ? 1 : \
0200         ((uint32_t)(c)<=0x7ff ? 2 : \
0201             ((uint32_t)(c)<=0xd7ff ? 3 : \
0202                 ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
0203                     ((uint32_t)(c)<=0xffff ? 3 : 4)\
0204                 ) \
0205             ) \
0206         ) \
0207     )
0208 
0209 /**
0210  * The maximum number of UTF-8 code units (bytes) per Unicode code point (U+0000..U+10ffff).
0211  * @return 4
0212  * @stable ICU 2.4
0213  */
0214 #define U8_MAX_LENGTH 4
0215 
0216 /**
0217  * Get a code point from a string at a random-access offset,
0218  * without changing the offset.
0219  * The offset may point to either the lead byte or one of the trail bytes
0220  * for a code point, in which case the macro will read all of the bytes
0221  * for the code point.
0222  * The result is undefined if the offset points to an illegal UTF-8
0223  * byte sequence.
0224  * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
0225  *
0226  * @param s const uint8_t * string
0227  * @param i string offset
0228  * @param c output UChar32 variable
0229  * @see U8_GET
0230  * @stable ICU 2.4
0231  */
0232 #define U8_GET_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
0233     int32_t _u8_get_unsafe_index=(int32_t)(i); \
0234     U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \
0235     U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \
0236 } UPRV_BLOCK_MACRO_END
0237 
0238 /**
0239  * Get a code point from a string at a random-access offset,
0240  * without changing the offset.
0241  * The offset may point to either the lead byte or one of the trail bytes
0242  * for a code point, in which case the macro will read all of the bytes
0243  * for the code point.
0244  *
0245  * The length can be negative for a NUL-terminated string.
0246  *
0247  * If the offset points to an illegal UTF-8 byte sequence, then
0248  * c is set to a negative value.
0249  * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
0250  *
0251  * @param s const uint8_t * string
0252  * @param start int32_t starting string offset
0253  * @param i int32_t string offset, must be start<=i<length
0254  * @param length int32_t string length
0255  * @param c output UChar32 variable, set to <0 in case of an error
0256  * @see U8_GET_UNSAFE
0257  * @stable ICU 2.4
0258  */
0259 #define U8_GET(s, start, i, length, c) UPRV_BLOCK_MACRO_BEGIN { \
0260     int32_t _u8_get_index=(i); \
0261     U8_SET_CP_START(s, start, _u8_get_index); \
0262     U8_NEXT(s, _u8_get_index, length, c); \
0263 } UPRV_BLOCK_MACRO_END
0264 
0265 /**
0266  * Get a code point from a string at a random-access offset,
0267  * without changing the offset.
0268  * The offset may point to either the lead byte or one of the trail bytes
0269  * for a code point, in which case the macro will read all of the bytes
0270  * for the code point.
0271  *
0272  * The length can be negative for a NUL-terminated string.
0273  *
0274  * If the offset points to an illegal UTF-8 byte sequence, then
0275  * c is set to U+FFFD.
0276  * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT_OR_FFFD.
0277  *
0278  * This macro does not distinguish between a real U+FFFD in the text
0279  * and U+FFFD returned for an ill-formed sequence.
0280  * Use U8_GET() if that distinction is important.
0281  *
0282  * @param s const uint8_t * string
0283  * @param start int32_t starting string offset
0284  * @param i int32_t string offset, must be start<=i<length
0285  * @param length int32_t string length
0286  * @param c output UChar32 variable, set to U+FFFD in case of an error
0287  * @see U8_GET
0288  * @stable ICU 51
0289  */
0290 #define U8_GET_OR_FFFD(s, start, i, length, c) UPRV_BLOCK_MACRO_BEGIN { \
0291     int32_t _u8_get_index=(i); \
0292     U8_SET_CP_START(s, start, _u8_get_index); \
0293     U8_NEXT_OR_FFFD(s, _u8_get_index, length, c); \
0294 } UPRV_BLOCK_MACRO_END
0295 
0296 /* definitions with forward iteration --------------------------------------- */
0297 
0298 /**
0299  * Get a code point from a string at a code point boundary offset,
0300  * and advance the offset to the next code point boundary.
0301  * (Post-incrementing forward iteration.)
0302  * "Unsafe" macro, assumes well-formed UTF-8.
0303  *
0304  * The offset may point to the lead byte of a multi-byte sequence,
0305  * in which case the macro will read the whole sequence.
0306  * The result is undefined if the offset points to a trail byte
0307  * or an illegal UTF-8 sequence.
0308  *
0309  * @param s const uint8_t * string
0310  * @param i string offset
0311  * @param c output UChar32 variable
0312  * @see U8_NEXT
0313  * @stable ICU 2.4
0314  */
0315 #define U8_NEXT_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
0316     (c)=(uint8_t)(s)[(i)++]; \
0317     if(!U8_IS_SINGLE(c)) { \
0318         if((c)<0xe0) { \
0319             (c)=(((c)&0x1f)<<6)|((s)[(i)++]&0x3f); \
0320         } else if((c)<0xf0) { \
0321             /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
0322             (c)=(UChar)(((c)<<12)|(((s)[i]&0x3f)<<6)|((s)[(i)+1]&0x3f)); \
0323             (i)+=2; \
0324         } else { \
0325             (c)=(((c)&7)<<18)|(((s)[i]&0x3f)<<12)|(((s)[(i)+1]&0x3f)<<6)|((s)[(i)+2]&0x3f); \
0326             (i)+=3; \
0327         } \
0328     } \
0329 } UPRV_BLOCK_MACRO_END
0330 
0331 /**
0332  * Get a code point from a string at a code point boundary offset,
0333  * and advance the offset to the next code point boundary.
0334  * (Post-incrementing forward iteration.)
0335  * "Safe" macro, checks for illegal sequences and for string boundaries.
0336  *
0337  * The length can be negative for a NUL-terminated string.
0338  *
0339  * The offset may point to the lead byte of a multi-byte sequence,
0340  * in which case the macro will read the whole sequence.
0341  * If the offset points to a trail byte or an illegal UTF-8 sequence, then
0342  * c is set to a negative value.
0343  *
0344  * @param s const uint8_t * string
0345  * @param i int32_t string offset, must be i<length
0346  * @param length int32_t string length
0347  * @param c output UChar32 variable, set to <0 in case of an error
0348  * @see U8_NEXT_UNSAFE
0349  * @stable ICU 2.4
0350  */
0351 #define U8_NEXT(s, i, length, c) U8_INTERNAL_NEXT_OR_SUB(s, i, length, c, U_SENTINEL)
0352 
0353 /**
0354  * Get a code point from a string at a code point boundary offset,
0355  * and advance the offset to the next code point boundary.
0356  * (Post-incrementing forward iteration.)
0357  * "Safe" macro, checks for illegal sequences and for string boundaries.
0358  *
0359  * The length can be negative for a NUL-terminated string.
0360  *
0361  * The offset may point to the lead byte of a multi-byte sequence,
0362  * in which case the macro will read the whole sequence.
0363  * If the offset points to a trail byte or an illegal UTF-8 sequence, then
0364  * c is set to U+FFFD.
0365  *
0366  * This macro does not distinguish between a real U+FFFD in the text
0367  * and U+FFFD returned for an ill-formed sequence.
0368  * Use U8_NEXT() if that distinction is important.
0369  *
0370  * @param s const uint8_t * string
0371  * @param i int32_t string offset, must be i<length
0372  * @param length int32_t string length
0373  * @param c output UChar32 variable, set to U+FFFD in case of an error
0374  * @see U8_NEXT
0375  * @stable ICU 51
0376  */
0377 #define U8_NEXT_OR_FFFD(s, i, length, c) U8_INTERNAL_NEXT_OR_SUB(s, i, length, c, 0xfffd)
0378 
0379 /** @internal */
0380 #define U8_INTERNAL_NEXT_OR_SUB(s, i, length, c, sub) UPRV_BLOCK_MACRO_BEGIN { \
0381     (c)=(uint8_t)(s)[(i)++]; \
0382     if(!U8_IS_SINGLE(c)) { \
0383         uint8_t __t = 0; \
0384         if((i)!=(length) && \
0385             /* fetch/validate/assemble all but last trail byte */ \
0386             ((c)>=0xe0 ? \
0387                 ((c)<0xf0 ?  /* U+0800..U+FFFF except surrogates */ \
0388                     U8_LEAD3_T1_BITS[(c)&=0xf]&(1<<((__t=(s)[i])>>5)) && \
0389                     (__t&=0x3f, 1) \
0390                 :  /* U+10000..U+10FFFF */ \
0391                     ((c)-=0xf0)<=4 && \
0392                     U8_LEAD4_T1_BITS[(__t=(s)[i])>>4]&(1<<(c)) && \
0393                     ((c)=((c)<<6)|(__t&0x3f), ++(i)!=(length)) && \
0394                     (__t=(s)[i]-0x80)<=0x3f) && \
0395                 /* valid second-to-last trail byte */ \
0396                 ((c)=((c)<<6)|__t, ++(i)!=(length)) \
0397             :  /* U+0080..U+07FF */ \
0398                 (c)>=0xc2 && ((c)&=0x1f, 1)) && \
0399             /* last trail byte */ \
0400             (__t=(s)[i]-0x80)<=0x3f && \
0401             ((c)=((c)<<6)|__t, ++(i), 1)) { \
0402         } else { \
0403             (c)=(sub);  /* ill-formed*/ \
0404         } \
0405     } \
0406 } UPRV_BLOCK_MACRO_END
0407 
0408 /**
0409  * Append a code point to a string, overwriting 1 to 4 bytes.
0410  * The offset points to the current end of the string contents
0411  * and is advanced (post-increment).
0412  * "Unsafe" macro, assumes a valid code point and sufficient space in the string.
0413  * Otherwise, the result is undefined.
0414  *
0415  * @param s const uint8_t * string buffer
0416  * @param i string offset
0417  * @param c code point to append
0418  * @see U8_APPEND
0419  * @stable ICU 2.4
0420  */
0421 #define U8_APPEND_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
0422     uint32_t __uc=(c); \
0423     if(__uc<=0x7f) { \
0424         (s)[(i)++]=(uint8_t)__uc; \
0425     } else { \
0426         if(__uc<=0x7ff) { \
0427             (s)[(i)++]=(uint8_t)((__uc>>6)|0xc0); \
0428         } else { \
0429             if(__uc<=0xffff) { \
0430                 (s)[(i)++]=(uint8_t)((__uc>>12)|0xe0); \
0431             } else { \
0432                 (s)[(i)++]=(uint8_t)((__uc>>18)|0xf0); \
0433                 (s)[(i)++]=(uint8_t)(((__uc>>12)&0x3f)|0x80); \
0434             } \
0435             (s)[(i)++]=(uint8_t)(((__uc>>6)&0x3f)|0x80); \
0436         } \
0437         (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \
0438     } \
0439 } UPRV_BLOCK_MACRO_END
0440 
0441 /**
0442  * Append a code point to a string, overwriting 1 to 4 bytes.
0443  * The offset points to the current end of the string contents
0444  * and is advanced (post-increment).
0445  * "Safe" macro, checks for a valid code point.
0446  * If a non-ASCII code point is written, checks for sufficient space in the string.
0447  * If the code point is not valid or trail bytes do not fit,
0448  * then isError is set to TRUE.
0449  *
0450  * @param s const uint8_t * string buffer
0451  * @param i int32_t string offset, must be i<capacity
0452  * @param capacity int32_t size of the string buffer
0453  * @param c UChar32 code point to append
0454  * @param isError output UBool set to TRUE if an error occurs, otherwise not modified
0455  * @see U8_APPEND_UNSAFE
0456  * @stable ICU 2.4
0457  */
0458 #define U8_APPEND(s, i, capacity, c, isError) UPRV_BLOCK_MACRO_BEGIN { \
0459     uint32_t __uc=(c); \
0460     if(__uc<=0x7f) { \
0461         (s)[(i)++]=(uint8_t)__uc; \
0462     } else if(__uc<=0x7ff && (i)+1<(capacity)) { \
0463         (s)[(i)++]=(uint8_t)((__uc>>6)|0xc0); \
0464         (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \
0465     } else if((__uc<=0xd7ff || (0xe000<=__uc && __uc<=0xffff)) && (i)+2<(capacity)) { \
0466         (s)[(i)++]=(uint8_t)((__uc>>12)|0xe0); \
0467         (s)[(i)++]=(uint8_t)(((__uc>>6)&0x3f)|0x80); \
0468         (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \
0469     } else if(0xffff<__uc && __uc<=0x10ffff && (i)+3<(capacity)) { \
0470         (s)[(i)++]=(uint8_t)((__uc>>18)|0xf0); \
0471         (s)[(i)++]=(uint8_t)(((__uc>>12)&0x3f)|0x80); \
0472         (s)[(i)++]=(uint8_t)(((__uc>>6)&0x3f)|0x80); \
0473         (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \
0474     } else { \
0475         (isError)=TRUE; \
0476     } \
0477 } UPRV_BLOCK_MACRO_END
0478 
0479 /**
0480  * Advance the string offset from one code point boundary to the next.
0481  * (Post-incrementing iteration.)
0482  * "Unsafe" macro, assumes well-formed UTF-8.
0483  *
0484  * @param s const uint8_t * string
0485  * @param i string offset
0486  * @see U8_FWD_1
0487  * @stable ICU 2.4
0488  */
0489 #define U8_FWD_1_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
0490     (i)+=1+U8_COUNT_TRAIL_BYTES_UNSAFE((s)[i]); \
0491 } UPRV_BLOCK_MACRO_END
0492 
0493 /**
0494  * Advance the string offset from one code point boundary to the next.
0495  * (Post-incrementing iteration.)
0496  * "Safe" macro, checks for illegal sequences and for string boundaries.
0497  *
0498  * The length can be negative for a NUL-terminated string.
0499  *
0500  * @param s const uint8_t * string
0501  * @param i int32_t string offset, must be i<length
0502  * @param length int32_t string length
0503  * @see U8_FWD_1_UNSAFE
0504  * @stable ICU 2.4
0505  */
0506 #define U8_FWD_1(s, i, length) UPRV_BLOCK_MACRO_BEGIN { \
0507     uint8_t __b=(s)[(i)++]; \
0508     if(U8_IS_LEAD(__b) && (i)!=(length)) { \
0509         uint8_t __t1=(s)[i]; \
0510         if((0xe0<=__b && __b<0xf0)) { \
0511             if(U8_IS_VALID_LEAD3_AND_T1(__b, __t1) && \
0512                     ++(i)!=(length) && U8_IS_TRAIL((s)[i])) { \
0513                 ++(i); \
0514             } \
0515         } else if(__b<0xe0) { \
0516             if(U8_IS_TRAIL(__t1)) { \
0517                 ++(i); \
0518             } \
0519         } else /* c>=0xf0 */ { \
0520             if(U8_IS_VALID_LEAD4_AND_T1(__b, __t1) && \
0521                     ++(i)!=(length) && U8_IS_TRAIL((s)[i]) && \
0522                     ++(i)!=(length) && U8_IS_TRAIL((s)[i])) { \
0523                 ++(i); \
0524             } \
0525         } \
0526     } \
0527 } UPRV_BLOCK_MACRO_END
0528 
0529 /**
0530  * Advance the string offset from one code point boundary to the n-th next one,
0531  * i.e., move forward by n code points.
0532  * (Post-incrementing iteration.)
0533  * "Unsafe" macro, assumes well-formed UTF-8.
0534  *
0535  * @param s const uint8_t * string
0536  * @param i string offset
0537  * @param n number of code points to skip
0538  * @see U8_FWD_N
0539  * @stable ICU 2.4
0540  */
0541 #define U8_FWD_N_UNSAFE(s, i, n) UPRV_BLOCK_MACRO_BEGIN { \
0542     int32_t __N=(n); \
0543     while(__N>0) { \
0544         U8_FWD_1_UNSAFE(s, i); \
0545         --__N; \
0546     } \
0547 } UPRV_BLOCK_MACRO_END
0548 
0549 /**
0550  * Advance the string offset from one code point boundary to the n-th next one,
0551  * i.e., move forward by n code points.
0552  * (Post-incrementing iteration.)
0553  * "Safe" macro, checks for illegal sequences and for string boundaries.
0554  *
0555  * The length can be negative for a NUL-terminated string.
0556  *
0557  * @param s const uint8_t * string
0558  * @param i int32_t string offset, must be i<length
0559  * @param length int32_t string length
0560  * @param n number of code points to skip
0561  * @see U8_FWD_N_UNSAFE
0562  * @stable ICU 2.4
0563  */
0564 #define U8_FWD_N(s, i, length, n) UPRV_BLOCK_MACRO_BEGIN { \
0565     int32_t __N=(n); \
0566     while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
0567         U8_FWD_1(s, i, length); \
0568         --__N; \
0569     } \
0570 } UPRV_BLOCK_MACRO_END
0571 
0572 /**
0573  * Adjust a random-access offset to a code point boundary
0574  * at the start of a code point.
0575  * If the offset points to a UTF-8 trail byte,
0576  * then the offset is moved backward to the corresponding lead byte.
0577  * Otherwise, it is not modified.
0578  * "Unsafe" macro, assumes well-formed UTF-8.
0579  *
0580  * @param s const uint8_t * string
0581  * @param i string offset
0582  * @see U8_SET_CP_START
0583  * @stable ICU 2.4
0584  */
0585 #define U8_SET_CP_START_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
0586     while(U8_IS_TRAIL((s)[i])) { --(i); } \
0587 } UPRV_BLOCK_MACRO_END
0588 
0589 /**
0590  * Adjust a random-access offset to a code point boundary
0591  * at the start of a code point.
0592  * If the offset points to a UTF-8 trail byte,
0593  * then the offset is moved backward to the corresponding lead byte.
0594  * Otherwise, it is not modified.
0595  *
0596  * "Safe" macro, checks for illegal sequences and for string boundaries.
0597  * Unlike U8_TRUNCATE_IF_INCOMPLETE(), this macro always reads s[i].
0598  *
0599  * @param s const uint8_t * string
0600  * @param start int32_t starting string offset (usually 0)
0601  * @param i int32_t string offset, must be start<=i
0602  * @see U8_SET_CP_START_UNSAFE
0603  * @see U8_TRUNCATE_IF_INCOMPLETE
0604  * @stable ICU 2.4
0605  */
0606 #define U8_SET_CP_START(s, start, i) UPRV_BLOCK_MACRO_BEGIN { \
0607     if(U8_IS_TRAIL((s)[(i)])) { \
0608         (i)=utf8_back1SafeBody(s, start, (i)); \
0609     } \
0610 } UPRV_BLOCK_MACRO_END
0611 
0612 /**
0613  * If the string ends with a UTF-8 byte sequence that is valid so far
0614  * but incomplete, then reduce the length of the string to end before
0615  * the lead byte of that incomplete sequence.
0616  * For example, if the string ends with E1 80, the length is reduced by 2.
0617  *
0618  * In all other cases (the string ends with a complete sequence, or it is not
0619  * possible for any further trail byte to extend the trailing sequence)
0620  * the length remains unchanged.
0621  *
0622  * Useful for processing text split across multiple buffers
0623  * (save the incomplete sequence for later)
0624  * and for optimizing iteration
0625  * (check for string length only once per character).
0626  *
0627  * "Safe" macro, checks for illegal sequences and for string boundaries.
0628  * Unlike U8_SET_CP_START(), this macro never reads s[length].
0629  *
0630  * (In UTF-16, simply check for U16_IS_LEAD(last code unit).)
0631  *
0632  * @param s const uint8_t * string
0633  * @param start int32_t starting string offset (usually 0)
0634  * @param length int32_t string length (usually start<=length)
0635  * @see U8_SET_CP_START
0636  * @stable ICU 61
0637  */
0638 #define U8_TRUNCATE_IF_INCOMPLETE(s, start, length) UPRV_BLOCK_MACRO_BEGIN { \
0639     if((length)>(start)) { \
0640         uint8_t __b1=s[(length)-1]; \
0641         if(U8_IS_SINGLE(__b1)) { \
0642             /* common ASCII character */ \
0643         } else if(U8_IS_LEAD(__b1)) { \
0644             --(length); \
0645         } else if(U8_IS_TRAIL(__b1) && ((length)-2)>=(start)) { \
0646             uint8_t __b2=s[(length)-2]; \
0647             if(0xe0<=__b2 && __b2<=0xf4) { \
0648                 if(__b2<0xf0 ? U8_IS_VALID_LEAD3_AND_T1(__b2, __b1) : \
0649                         U8_IS_VALID_LEAD4_AND_T1(__b2, __b1)) { \
0650                     (length)-=2; \
0651                 } \
0652             } else if(U8_IS_TRAIL(__b2) && ((length)-3)>=(start)) { \
0653                 uint8_t __b3=s[(length)-3]; \
0654                 if(0xf0<=__b3 && __b3<=0xf4 && U8_IS_VALID_LEAD4_AND_T1(__b3, __b2)) { \
0655                     (length)-=3; \
0656                 } \
0657             } \
0658         } \
0659     } \
0660 } UPRV_BLOCK_MACRO_END
0661 
0662 /* definitions with backward iteration -------------------------------------- */
0663 
0664 /**
0665  * Move the string offset from one code point boundary to the previous one
0666  * and get the code point between them.
0667  * (Pre-decrementing backward iteration.)
0668  * "Unsafe" macro, assumes well-formed UTF-8.
0669  *
0670  * The input offset may be the same as the string length.
0671  * If the offset is behind a multi-byte sequence, then the macro will read
0672  * the whole sequence.
0673  * If the offset is behind a lead byte, then that itself
0674  * will be returned as the code point.
0675  * The result is undefined if the offset is behind an illegal UTF-8 sequence.
0676  *
0677  * @param s const uint8_t * string
0678  * @param i string offset
0679  * @param c output UChar32 variable
0680  * @see U8_PREV
0681  * @stable ICU 2.4
0682  */
0683 #define U8_PREV_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
0684     (c)=(uint8_t)(s)[--(i)]; \
0685     if(U8_IS_TRAIL(c)) { \
0686         uint8_t __b, __count=1, __shift=6; \
0687 \
0688         /* c is a trail byte */ \
0689         (c)&=0x3f; \
0690         for(;;) { \
0691             __b=(s)[--(i)]; \
0692             if(__b>=0xc0) { \
0693                 U8_MASK_LEAD_BYTE(__b, __count); \
0694                 (c)|=(UChar32)__b<<__shift; \
0695                 break; \
0696             } else { \
0697                 (c)|=(UChar32)(__b&0x3f)<<__shift; \
0698                 ++__count; \
0699                 __shift+=6; \
0700             } \
0701         } \
0702     } \
0703 } UPRV_BLOCK_MACRO_END
0704 
0705 /**
0706  * Move the string offset from one code point boundary to the previous one
0707  * and get the code point between them.
0708  * (Pre-decrementing backward iteration.)
0709  * "Safe" macro, checks for illegal sequences and for string boundaries.
0710  *
0711  * The input offset may be the same as the string length.
0712  * If the offset is behind a multi-byte sequence, then the macro will read
0713  * the whole sequence.
0714  * If the offset is behind a lead byte, then that itself
0715  * will be returned as the code point.
0716  * If the offset is behind an illegal UTF-8 sequence, then c is set to a negative value.
0717  *
0718  * @param s const uint8_t * string
0719  * @param start int32_t starting string offset (usually 0)
0720  * @param i int32_t string offset, must be start<i
0721  * @param c output UChar32 variable, set to <0 in case of an error
0722  * @see U8_PREV_UNSAFE
0723  * @stable ICU 2.4
0724  */
0725 #define U8_PREV(s, start, i, c) UPRV_BLOCK_MACRO_BEGIN { \
0726     (c)=(uint8_t)(s)[--(i)]; \
0727     if(!U8_IS_SINGLE(c)) { \
0728         (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
0729     } \
0730 } UPRV_BLOCK_MACRO_END
0731 
0732 /**
0733  * Move the string offset from one code point boundary to the previous one
0734  * and get the code point between them.
0735  * (Pre-decrementing backward iteration.)
0736  * "Safe" macro, checks for illegal sequences and for string boundaries.
0737  *
0738  * The input offset may be the same as the string length.
0739  * If the offset is behind a multi-byte sequence, then the macro will read
0740  * the whole sequence.
0741  * If the offset is behind a lead byte, then that itself
0742  * will be returned as the code point.
0743  * If the offset is behind an illegal UTF-8 sequence, then c is set to U+FFFD.
0744  *
0745  * This macro does not distinguish between a real U+FFFD in the text
0746  * and U+FFFD returned for an ill-formed sequence.
0747  * Use U8_PREV() if that distinction is important.
0748  *
0749  * @param s const uint8_t * string
0750  * @param start int32_t starting string offset (usually 0)
0751  * @param i int32_t string offset, must be start<i
0752  * @param c output UChar32 variable, set to U+FFFD in case of an error
0753  * @see U8_PREV
0754  * @stable ICU 51
0755  */
0756 #define U8_PREV_OR_FFFD(s, start, i, c) UPRV_BLOCK_MACRO_BEGIN { \
0757     (c)=(uint8_t)(s)[--(i)]; \
0758     if(!U8_IS_SINGLE(c)) { \
0759         (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -3); \
0760     } \
0761 } UPRV_BLOCK_MACRO_END
0762 
0763 /**
0764  * Move the string offset from one code point boundary to the previous one.
0765  * (Pre-decrementing backward iteration.)
0766  * The input offset may be the same as the string length.
0767  * "Unsafe" macro, assumes well-formed UTF-8.
0768  *
0769  * @param s const uint8_t * string
0770  * @param i string offset
0771  * @see U8_BACK_1
0772  * @stable ICU 2.4
0773  */
0774 #define U8_BACK_1_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
0775     while(U8_IS_TRAIL((s)[--(i)])) {} \
0776 } UPRV_BLOCK_MACRO_END
0777 
0778 /**
0779  * Move the string offset from one code point boundary to the previous one.
0780  * (Pre-decrementing backward iteration.)
0781  * The input offset may be the same as the string length.
0782  * "Safe" macro, checks for illegal sequences and for string boundaries.
0783  *
0784  * @param s const uint8_t * string
0785  * @param start int32_t starting string offset (usually 0)
0786  * @param i int32_t string offset, must be start<i
0787  * @see U8_BACK_1_UNSAFE
0788  * @stable ICU 2.4
0789  */
0790 #define U8_BACK_1(s, start, i) UPRV_BLOCK_MACRO_BEGIN { \
0791     if(U8_IS_TRAIL((s)[--(i)])) { \
0792         (i)=utf8_back1SafeBody(s, start, (i)); \
0793     } \
0794 } UPRV_BLOCK_MACRO_END
0795 
0796 /**
0797  * Move the string offset from one code point boundary to the n-th one before it,
0798  * i.e., move backward by n code points.
0799  * (Pre-decrementing backward iteration.)
0800  * The input offset may be the same as the string length.
0801  * "Unsafe" macro, assumes well-formed UTF-8.
0802  *
0803  * @param s const uint8_t * string
0804  * @param i string offset
0805  * @param n number of code points to skip
0806  * @see U8_BACK_N
0807  * @stable ICU 2.4
0808  */
0809 #define U8_BACK_N_UNSAFE(s, i, n) UPRV_BLOCK_MACRO_BEGIN { \
0810     int32_t __N=(n); \
0811     while(__N>0) { \
0812         U8_BACK_1_UNSAFE(s, i); \
0813         --__N; \
0814     } \
0815 } UPRV_BLOCK_MACRO_END
0816 
0817 /**
0818  * Move the string offset from one code point boundary to the n-th one before it,
0819  * i.e., move backward by n code points.
0820  * (Pre-decrementing backward iteration.)
0821  * The input offset may be the same as the string length.
0822  * "Safe" macro, checks for illegal sequences and for string boundaries.
0823  *
0824  * @param s const uint8_t * string
0825  * @param start int32_t index of the start of the string
0826  * @param i int32_t string offset, must be start<i
0827  * @param n number of code points to skip
0828  * @see U8_BACK_N_UNSAFE
0829  * @stable ICU 2.4
0830  */
0831 #define U8_BACK_N(s, start, i, n) UPRV_BLOCK_MACRO_BEGIN { \
0832     int32_t __N=(n); \
0833     while(__N>0 && (i)>(start)) { \
0834         U8_BACK_1(s, start, i); \
0835         --__N; \
0836     } \
0837 } UPRV_BLOCK_MACRO_END
0838 
0839 /**
0840  * Adjust a random-access offset to a code point boundary after a code point.
0841  * If the offset is behind a partial multi-byte sequence,
0842  * then the offset is incremented to behind the whole sequence.
0843  * Otherwise, it is not modified.
0844  * The input offset may be the same as the string length.
0845  * "Unsafe" macro, assumes well-formed UTF-8.
0846  *
0847  * @param s const uint8_t * string
0848  * @param i string offset
0849  * @see U8_SET_CP_LIMIT
0850  * @stable ICU 2.4
0851  */
0852 #define U8_SET_CP_LIMIT_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
0853     U8_BACK_1_UNSAFE(s, i); \
0854     U8_FWD_1_UNSAFE(s, i); \
0855 } UPRV_BLOCK_MACRO_END
0856 
0857 /**
0858  * Adjust a random-access offset to a code point boundary after a code point.
0859  * If the offset is behind a partial multi-byte sequence,
0860  * then the offset is incremented to behind the whole sequence.
0861  * Otherwise, it is not modified.
0862  * The input offset may be the same as the string length.
0863  * "Safe" macro, checks for illegal sequences and for string boundaries.
0864  *
0865  * The length can be negative for a NUL-terminated string.
0866  *
0867  * @param s const uint8_t * string
0868  * @param start int32_t starting string offset (usually 0)
0869  * @param i int32_t string offset, must be start<=i<=length
0870  * @param length int32_t string length
0871  * @see U8_SET_CP_LIMIT_UNSAFE
0872  * @stable ICU 2.4
0873  */
0874 #define U8_SET_CP_LIMIT(s, start, i, length) UPRV_BLOCK_MACRO_BEGIN { \
0875     if((start)<(i) && ((i)<(length) || (length)<0)) { \
0876         U8_BACK_1(s, start, i); \
0877         U8_FWD_1(s, i, length); \
0878     } \
0879 } UPRV_BLOCK_MACRO_END
0880 
0881 #endif