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