1*0e209d39SAndroid Build Coastguard Worker // © 2016 and later: Unicode, Inc. and others. 2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html 3*0e209d39SAndroid Build Coastguard Worker /* 4*0e209d39SAndroid Build Coastguard Worker ******************************************************************************* 5*0e209d39SAndroid Build Coastguard Worker * 6*0e209d39SAndroid Build Coastguard Worker * Copyright (C) 1999-2015, International Business Machines 7*0e209d39SAndroid Build Coastguard Worker * Corporation and others. All Rights Reserved. 8*0e209d39SAndroid Build Coastguard Worker * 9*0e209d39SAndroid Build Coastguard Worker ******************************************************************************* 10*0e209d39SAndroid Build Coastguard Worker * file name: utf8.h 11*0e209d39SAndroid Build Coastguard Worker * encoding: UTF-8 12*0e209d39SAndroid Build Coastguard Worker * tab size: 8 (not used) 13*0e209d39SAndroid Build Coastguard Worker * indentation:4 14*0e209d39SAndroid Build Coastguard Worker * 15*0e209d39SAndroid Build Coastguard Worker * created on: 1999sep13 16*0e209d39SAndroid Build Coastguard Worker * created by: Markus W. Scherer 17*0e209d39SAndroid Build Coastguard Worker */ 18*0e209d39SAndroid Build Coastguard Worker 19*0e209d39SAndroid Build Coastguard Worker /** 20*0e209d39SAndroid Build Coastguard Worker * \file 21*0e209d39SAndroid Build Coastguard Worker * \brief C API: 8-bit Unicode handling macros 22*0e209d39SAndroid Build Coastguard Worker * 23*0e209d39SAndroid Build Coastguard Worker * This file defines macros to deal with 8-bit Unicode (UTF-8) code units (bytes) and strings. 24*0e209d39SAndroid Build Coastguard Worker * 25*0e209d39SAndroid Build Coastguard Worker * For more information see utf.h and the ICU User Guide Strings chapter 26*0e209d39SAndroid Build Coastguard Worker * (https://unicode-org.github.io/icu/userguide/strings). 27*0e209d39SAndroid Build Coastguard Worker * 28*0e209d39SAndroid Build Coastguard Worker * <em>Usage:</em> 29*0e209d39SAndroid Build Coastguard Worker * ICU coding guidelines for if() statements should be followed when using these macros. 30*0e209d39SAndroid Build Coastguard Worker * Compound statements (curly braces {}) must be used for if-else-while... 31*0e209d39SAndroid Build Coastguard Worker * bodies and all macro statements should be terminated with semicolon. 32*0e209d39SAndroid Build Coastguard Worker */ 33*0e209d39SAndroid Build Coastguard Worker 34*0e209d39SAndroid Build Coastguard Worker #ifndef __UTF8_H__ 35*0e209d39SAndroid Build Coastguard Worker #define __UTF8_H__ 36*0e209d39SAndroid Build Coastguard Worker 37*0e209d39SAndroid Build Coastguard Worker #include <stdbool.h> 38*0e209d39SAndroid Build Coastguard Worker #include "unicode/umachine.h" 39*0e209d39SAndroid Build Coastguard Worker #ifndef __UTF_H__ 40*0e209d39SAndroid Build Coastguard Worker # include "unicode/utf.h" 41*0e209d39SAndroid Build Coastguard Worker #endif 42*0e209d39SAndroid Build Coastguard Worker 43*0e209d39SAndroid Build Coastguard Worker /* internal definitions ----------------------------------------------------- */ 44*0e209d39SAndroid Build Coastguard Worker 45*0e209d39SAndroid Build Coastguard Worker /** 46*0e209d39SAndroid Build Coastguard Worker * Counts the trail bytes for a UTF-8 lead byte. 47*0e209d39SAndroid Build Coastguard Worker * Returns 0 for 0..0xc1 as well as for 0xf5..0xff. 48*0e209d39SAndroid Build Coastguard Worker * leadByte might be evaluated multiple times. 49*0e209d39SAndroid Build Coastguard Worker * 50*0e209d39SAndroid Build Coastguard Worker * This is internal since it is not meant to be called directly by external clients; 51*0e209d39SAndroid Build Coastguard Worker * however it is called by public macros in this file and thus must remain stable. 52*0e209d39SAndroid Build Coastguard Worker * 53*0e209d39SAndroid Build Coastguard Worker * @param leadByte The first byte of a UTF-8 sequence. Must be 0..0xff. 54*0e209d39SAndroid Build Coastguard Worker * @internal 55*0e209d39SAndroid Build Coastguard Worker */ 56*0e209d39SAndroid Build Coastguard Worker #define U8_COUNT_TRAIL_BYTES(leadByte) \ 57*0e209d39SAndroid Build Coastguard Worker (U8_IS_LEAD(leadByte) ? \ 58*0e209d39SAndroid Build Coastguard Worker ((uint8_t)(leadByte)>=0xe0)+((uint8_t)(leadByte)>=0xf0)+1 : 0) 59*0e209d39SAndroid Build Coastguard Worker 60*0e209d39SAndroid Build Coastguard Worker /** 61*0e209d39SAndroid Build Coastguard Worker * Counts the trail bytes for a UTF-8 lead byte of a valid UTF-8 sequence. 62*0e209d39SAndroid Build Coastguard Worker * Returns 0 for 0..0xc1. Undefined for 0xf5..0xff. 63*0e209d39SAndroid Build Coastguard Worker * leadByte might be evaluated multiple times. 64*0e209d39SAndroid Build Coastguard Worker * 65*0e209d39SAndroid Build Coastguard Worker * This is internal since it is not meant to be called directly by external clients; 66*0e209d39SAndroid Build Coastguard Worker * however it is called by public macros in this file and thus must remain stable. 67*0e209d39SAndroid Build Coastguard Worker * 68*0e209d39SAndroid Build Coastguard Worker * @param leadByte The first byte of a UTF-8 sequence. Must be 0..0xff. 69*0e209d39SAndroid Build Coastguard Worker * @internal 70*0e209d39SAndroid Build Coastguard Worker */ 71*0e209d39SAndroid Build Coastguard Worker #define U8_COUNT_TRAIL_BYTES_UNSAFE(leadByte) \ 72*0e209d39SAndroid Build Coastguard Worker (((uint8_t)(leadByte)>=0xc2)+((uint8_t)(leadByte)>=0xe0)+((uint8_t)(leadByte)>=0xf0)) 73*0e209d39SAndroid Build Coastguard Worker 74*0e209d39SAndroid Build Coastguard Worker /** 75*0e209d39SAndroid Build Coastguard Worker * Mask a UTF-8 lead byte, leave only the lower bits that form part of the code point value. 76*0e209d39SAndroid Build Coastguard Worker * 77*0e209d39SAndroid Build Coastguard Worker * This is internal since it is not meant to be called directly by external clients; 78*0e209d39SAndroid Build Coastguard Worker * however it is called by public macros in this file and thus must remain stable. 79*0e209d39SAndroid Build Coastguard Worker * @internal 80*0e209d39SAndroid Build Coastguard Worker */ 81*0e209d39SAndroid Build Coastguard Worker #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1) 82*0e209d39SAndroid Build Coastguard Worker 83*0e209d39SAndroid Build Coastguard Worker /** 84*0e209d39SAndroid Build Coastguard Worker * Internal bit vector for 3-byte UTF-8 validity check, for use in U8_IS_VALID_LEAD3_AND_T1. 85*0e209d39SAndroid Build Coastguard Worker * Each bit indicates whether one lead byte + first trail byte pair starts a valid sequence. 86*0e209d39SAndroid Build Coastguard Worker * Lead byte E0..EF bits 3..0 are used as byte index, 87*0e209d39SAndroid Build Coastguard Worker * first trail byte bits 7..5 are used as bit index into that byte. 88*0e209d39SAndroid Build Coastguard Worker * @see U8_IS_VALID_LEAD3_AND_T1 89*0e209d39SAndroid Build Coastguard Worker * @internal 90*0e209d39SAndroid Build Coastguard Worker */ 91*0e209d39SAndroid Build Coastguard Worker #define U8_LEAD3_T1_BITS "\x20\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x10\x30\x30" 92*0e209d39SAndroid Build Coastguard Worker 93*0e209d39SAndroid Build Coastguard Worker /** 94*0e209d39SAndroid Build Coastguard Worker * Internal 3-byte UTF-8 validity check. 95*0e209d39SAndroid Build Coastguard Worker * Non-zero if lead byte E0..EF and first trail byte 00..FF start a valid sequence. 96*0e209d39SAndroid Build Coastguard Worker * @internal 97*0e209d39SAndroid Build Coastguard Worker */ 98*0e209d39SAndroid Build Coastguard Worker #define U8_IS_VALID_LEAD3_AND_T1(lead, t1) (U8_LEAD3_T1_BITS[(lead)&0xf]&(1<<((uint8_t)(t1)>>5))) 99*0e209d39SAndroid Build Coastguard Worker 100*0e209d39SAndroid Build Coastguard Worker /** 101*0e209d39SAndroid Build Coastguard Worker * Internal bit vector for 4-byte UTF-8 validity check, for use in U8_IS_VALID_LEAD4_AND_T1. 102*0e209d39SAndroid Build Coastguard Worker * Each bit indicates whether one lead byte + first trail byte pair starts a valid sequence. 103*0e209d39SAndroid Build Coastguard Worker * First trail byte bits 7..4 are used as byte index, 104*0e209d39SAndroid Build Coastguard Worker * lead byte F0..F4 bits 2..0 are used as bit index into that byte. 105*0e209d39SAndroid Build Coastguard Worker * @see U8_IS_VALID_LEAD4_AND_T1 106*0e209d39SAndroid Build Coastguard Worker * @internal 107*0e209d39SAndroid Build Coastguard Worker */ 108*0e209d39SAndroid Build Coastguard Worker #define U8_LEAD4_T1_BITS "\x00\x00\x00\x00\x00\x00\x00\x00\x1E\x0F\x0F\x0F\x00\x00\x00\x00" 109*0e209d39SAndroid Build Coastguard Worker 110*0e209d39SAndroid Build Coastguard Worker /** 111*0e209d39SAndroid Build Coastguard Worker * Internal 4-byte UTF-8 validity check. 112*0e209d39SAndroid Build Coastguard Worker * Non-zero if lead byte F0..F4 and first trail byte 00..FF start a valid sequence. 113*0e209d39SAndroid Build Coastguard Worker * @internal 114*0e209d39SAndroid Build Coastguard Worker */ 115*0e209d39SAndroid Build Coastguard Worker #define U8_IS_VALID_LEAD4_AND_T1(lead, t1) (U8_LEAD4_T1_BITS[(uint8_t)(t1)>>4]&(1<<((lead)&7))) 116*0e209d39SAndroid Build Coastguard Worker 117*0e209d39SAndroid Build Coastguard Worker /** 118*0e209d39SAndroid Build Coastguard Worker * Function for handling "next code point" with error-checking. 119*0e209d39SAndroid Build Coastguard Worker * 120*0e209d39SAndroid Build Coastguard Worker * This is internal since it is not meant to be called directly by external clients; 121*0e209d39SAndroid Build Coastguard Worker * however it is called by public macros in this 122*0e209d39SAndroid Build Coastguard Worker * file and thus must remain stable, and should not be hidden when other internal 123*0e209d39SAndroid Build Coastguard Worker * functions are hidden (otherwise public macros would fail to compile). 124*0e209d39SAndroid Build Coastguard Worker * @internal 125*0e209d39SAndroid Build Coastguard Worker */ 126*0e209d39SAndroid Build Coastguard Worker U_CAPI UChar32 U_EXPORT2 127*0e209d39SAndroid Build Coastguard Worker utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict); 128*0e209d39SAndroid Build Coastguard Worker 129*0e209d39SAndroid Build Coastguard Worker /** 130*0e209d39SAndroid Build Coastguard Worker * Function for handling "append code point" with error-checking. 131*0e209d39SAndroid Build Coastguard Worker * 132*0e209d39SAndroid Build Coastguard Worker * This is internal since it is not meant to be called directly by external clients; 133*0e209d39SAndroid Build Coastguard Worker * however it is called by public macros in this 134*0e209d39SAndroid Build Coastguard Worker * file and thus must remain stable, and should not be hidden when other internal 135*0e209d39SAndroid Build Coastguard Worker * functions are hidden (otherwise public macros would fail to compile). 136*0e209d39SAndroid Build Coastguard Worker * @internal 137*0e209d39SAndroid Build Coastguard Worker */ 138*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2 139*0e209d39SAndroid Build Coastguard Worker utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError); 140*0e209d39SAndroid Build Coastguard Worker 141*0e209d39SAndroid Build Coastguard Worker /** 142*0e209d39SAndroid Build Coastguard Worker * Function for handling "previous code point" with error-checking. 143*0e209d39SAndroid Build Coastguard Worker * 144*0e209d39SAndroid Build Coastguard Worker * This is internal since it is not meant to be called directly by external clients; 145*0e209d39SAndroid Build Coastguard Worker * however it is called by public macros in this 146*0e209d39SAndroid Build Coastguard Worker * file and thus must remain stable, and should not be hidden when other internal 147*0e209d39SAndroid Build Coastguard Worker * functions are hidden (otherwise public macros would fail to compile). 148*0e209d39SAndroid Build Coastguard Worker * @internal 149*0e209d39SAndroid Build Coastguard Worker */ 150*0e209d39SAndroid Build Coastguard Worker U_CAPI UChar32 U_EXPORT2 151*0e209d39SAndroid Build Coastguard Worker utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict); 152*0e209d39SAndroid Build Coastguard Worker 153*0e209d39SAndroid Build Coastguard Worker /** 154*0e209d39SAndroid Build Coastguard Worker * Function for handling "skip backward one code point" with error-checking. 155*0e209d39SAndroid Build Coastguard Worker * 156*0e209d39SAndroid Build Coastguard Worker * This is internal since it is not meant to be called directly by external clients; 157*0e209d39SAndroid Build Coastguard Worker * however it is called by public macros in this 158*0e209d39SAndroid Build Coastguard Worker * file and thus must remain stable, and should not be hidden when other internal 159*0e209d39SAndroid Build Coastguard Worker * functions are hidden (otherwise public macros would fail to compile). 160*0e209d39SAndroid Build Coastguard Worker * @internal 161*0e209d39SAndroid Build Coastguard Worker */ 162*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2 163*0e209d39SAndroid Build Coastguard Worker utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i); 164*0e209d39SAndroid Build Coastguard Worker 165*0e209d39SAndroid Build Coastguard Worker /* single-code point definitions -------------------------------------------- */ 166*0e209d39SAndroid Build Coastguard Worker 167*0e209d39SAndroid Build Coastguard Worker /** 168*0e209d39SAndroid Build Coastguard Worker * Does this code unit (byte) encode a code point by itself (US-ASCII 0..0x7f)? 169*0e209d39SAndroid Build Coastguard Worker * @param c 8-bit code unit (byte) 170*0e209d39SAndroid Build Coastguard Worker * @return true or false 171*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 172*0e209d39SAndroid Build Coastguard Worker */ 173*0e209d39SAndroid Build Coastguard Worker #define U8_IS_SINGLE(c) (((c)&0x80)==0) 174*0e209d39SAndroid Build Coastguard Worker 175*0e209d39SAndroid Build Coastguard Worker /** 176*0e209d39SAndroid Build Coastguard Worker * Is this code unit (byte) a UTF-8 lead byte? (0xC2..0xF4) 177*0e209d39SAndroid Build Coastguard Worker * @param c 8-bit code unit (byte) 178*0e209d39SAndroid Build Coastguard Worker * @return true or false 179*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 180*0e209d39SAndroid Build Coastguard Worker */ 181*0e209d39SAndroid Build Coastguard Worker #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc2)<=0x32) 182*0e209d39SAndroid Build Coastguard Worker // 0x32=0xf4-0xc2 183*0e209d39SAndroid Build Coastguard Worker 184*0e209d39SAndroid Build Coastguard Worker /** 185*0e209d39SAndroid Build Coastguard Worker * Is this code unit (byte) a UTF-8 trail byte? (0x80..0xBF) 186*0e209d39SAndroid Build Coastguard Worker * @param c 8-bit code unit (byte) 187*0e209d39SAndroid Build Coastguard Worker * @return true or false 188*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 189*0e209d39SAndroid Build Coastguard Worker */ 190*0e209d39SAndroid Build Coastguard Worker #define U8_IS_TRAIL(c) ((int8_t)(c)<-0x40) 191*0e209d39SAndroid Build Coastguard Worker 192*0e209d39SAndroid Build Coastguard Worker /** 193*0e209d39SAndroid Build Coastguard Worker * How many code units (bytes) are used for the UTF-8 encoding 194*0e209d39SAndroid Build Coastguard Worker * of this Unicode code point? 195*0e209d39SAndroid Build Coastguard Worker * @param c 32-bit code point 196*0e209d39SAndroid Build Coastguard Worker * @return 1..4, or 0 if c is a surrogate or not a Unicode code point 197*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 198*0e209d39SAndroid Build Coastguard Worker */ 199*0e209d39SAndroid Build Coastguard Worker #define U8_LENGTH(c) \ 200*0e209d39SAndroid Build Coastguard Worker ((uint32_t)(c)<=0x7f ? 1 : \ 201*0e209d39SAndroid Build Coastguard Worker ((uint32_t)(c)<=0x7ff ? 2 : \ 202*0e209d39SAndroid Build Coastguard Worker ((uint32_t)(c)<=0xd7ff ? 3 : \ 203*0e209d39SAndroid Build Coastguard Worker ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \ 204*0e209d39SAndroid Build Coastguard Worker ((uint32_t)(c)<=0xffff ? 3 : 4)\ 205*0e209d39SAndroid Build Coastguard Worker ) \ 206*0e209d39SAndroid Build Coastguard Worker ) \ 207*0e209d39SAndroid Build Coastguard Worker ) \ 208*0e209d39SAndroid Build Coastguard Worker ) 209*0e209d39SAndroid Build Coastguard Worker 210*0e209d39SAndroid Build Coastguard Worker /** 211*0e209d39SAndroid Build Coastguard Worker * The maximum number of UTF-8 code units (bytes) per Unicode code point (U+0000..U+10ffff). 212*0e209d39SAndroid Build Coastguard Worker * @return 4 213*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 214*0e209d39SAndroid Build Coastguard Worker */ 215*0e209d39SAndroid Build Coastguard Worker #define U8_MAX_LENGTH 4 216*0e209d39SAndroid Build Coastguard Worker 217*0e209d39SAndroid Build Coastguard Worker /** 218*0e209d39SAndroid Build Coastguard Worker * Get a code point from a string at a random-access offset, 219*0e209d39SAndroid Build Coastguard Worker * without changing the offset. 220*0e209d39SAndroid Build Coastguard Worker * The offset may point to either the lead byte or one of the trail bytes 221*0e209d39SAndroid Build Coastguard Worker * for a code point, in which case the macro will read all of the bytes 222*0e209d39SAndroid Build Coastguard Worker * for the code point. 223*0e209d39SAndroid Build Coastguard Worker * The result is undefined if the offset points to an illegal UTF-8 224*0e209d39SAndroid Build Coastguard Worker * byte sequence. 225*0e209d39SAndroid Build Coastguard Worker * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT. 226*0e209d39SAndroid Build Coastguard Worker * 227*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 228*0e209d39SAndroid Build Coastguard Worker * @param i string offset 229*0e209d39SAndroid Build Coastguard Worker * @param c output UChar32 variable 230*0e209d39SAndroid Build Coastguard Worker * @see U8_GET 231*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 232*0e209d39SAndroid Build Coastguard Worker */ 233*0e209d39SAndroid Build Coastguard Worker #define U8_GET_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \ 234*0e209d39SAndroid Build Coastguard Worker int32_t _u8_get_unsafe_index=(int32_t)(i); \ 235*0e209d39SAndroid Build Coastguard Worker U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \ 236*0e209d39SAndroid Build Coastguard Worker U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \ 237*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 238*0e209d39SAndroid Build Coastguard Worker 239*0e209d39SAndroid Build Coastguard Worker /** 240*0e209d39SAndroid Build Coastguard Worker * Get a code point from a string at a random-access offset, 241*0e209d39SAndroid Build Coastguard Worker * without changing the offset. 242*0e209d39SAndroid Build Coastguard Worker * The offset may point to either the lead byte or one of the trail bytes 243*0e209d39SAndroid Build Coastguard Worker * for a code point, in which case the macro will read all of the bytes 244*0e209d39SAndroid Build Coastguard Worker * for the code point. 245*0e209d39SAndroid Build Coastguard Worker * 246*0e209d39SAndroid Build Coastguard Worker * The length can be negative for a NUL-terminated string. 247*0e209d39SAndroid Build Coastguard Worker * 248*0e209d39SAndroid Build Coastguard Worker * If the offset points to an illegal UTF-8 byte sequence, then 249*0e209d39SAndroid Build Coastguard Worker * c is set to a negative value. 250*0e209d39SAndroid Build Coastguard Worker * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT. 251*0e209d39SAndroid Build Coastguard Worker * 252*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 253*0e209d39SAndroid Build Coastguard Worker * @param start int32_t starting string offset 254*0e209d39SAndroid Build Coastguard Worker * @param i int32_t string offset, must be start<=i<length 255*0e209d39SAndroid Build Coastguard Worker * @param length int32_t string length 256*0e209d39SAndroid Build Coastguard Worker * @param c output UChar32 variable, set to <0 in case of an error 257*0e209d39SAndroid Build Coastguard Worker * @see U8_GET_UNSAFE 258*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 259*0e209d39SAndroid Build Coastguard Worker */ 260*0e209d39SAndroid Build Coastguard Worker #define U8_GET(s, start, i, length, c) UPRV_BLOCK_MACRO_BEGIN { \ 261*0e209d39SAndroid Build Coastguard Worker int32_t _u8_get_index=(i); \ 262*0e209d39SAndroid Build Coastguard Worker U8_SET_CP_START(s, start, _u8_get_index); \ 263*0e209d39SAndroid Build Coastguard Worker U8_NEXT(s, _u8_get_index, length, c); \ 264*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 265*0e209d39SAndroid Build Coastguard Worker 266*0e209d39SAndroid Build Coastguard Worker /** 267*0e209d39SAndroid Build Coastguard Worker * Get a code point from a string at a random-access offset, 268*0e209d39SAndroid Build Coastguard Worker * without changing the offset. 269*0e209d39SAndroid Build Coastguard Worker * The offset may point to either the lead byte or one of the trail bytes 270*0e209d39SAndroid Build Coastguard Worker * for a code point, in which case the macro will read all of the bytes 271*0e209d39SAndroid Build Coastguard Worker * for the code point. 272*0e209d39SAndroid Build Coastguard Worker * 273*0e209d39SAndroid Build Coastguard Worker * The length can be negative for a NUL-terminated string. 274*0e209d39SAndroid Build Coastguard Worker * 275*0e209d39SAndroid Build Coastguard Worker * If the offset points to an illegal UTF-8 byte sequence, then 276*0e209d39SAndroid Build Coastguard Worker * c is set to U+FFFD. 277*0e209d39SAndroid Build Coastguard Worker * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT_OR_FFFD. 278*0e209d39SAndroid Build Coastguard Worker * 279*0e209d39SAndroid Build Coastguard Worker * This macro does not distinguish between a real U+FFFD in the text 280*0e209d39SAndroid Build Coastguard Worker * and U+FFFD returned for an ill-formed sequence. 281*0e209d39SAndroid Build Coastguard Worker * Use U8_GET() if that distinction is important. 282*0e209d39SAndroid Build Coastguard Worker * 283*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 284*0e209d39SAndroid Build Coastguard Worker * @param start int32_t starting string offset 285*0e209d39SAndroid Build Coastguard Worker * @param i int32_t string offset, must be start<=i<length 286*0e209d39SAndroid Build Coastguard Worker * @param length int32_t string length 287*0e209d39SAndroid Build Coastguard Worker * @param c output UChar32 variable, set to U+FFFD in case of an error 288*0e209d39SAndroid Build Coastguard Worker * @see U8_GET 289*0e209d39SAndroid Build Coastguard Worker * @stable ICU 51 290*0e209d39SAndroid Build Coastguard Worker */ 291*0e209d39SAndroid Build Coastguard Worker #define U8_GET_OR_FFFD(s, start, i, length, c) UPRV_BLOCK_MACRO_BEGIN { \ 292*0e209d39SAndroid Build Coastguard Worker int32_t _u8_get_index=(i); \ 293*0e209d39SAndroid Build Coastguard Worker U8_SET_CP_START(s, start, _u8_get_index); \ 294*0e209d39SAndroid Build Coastguard Worker U8_NEXT_OR_FFFD(s, _u8_get_index, length, c); \ 295*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 296*0e209d39SAndroid Build Coastguard Worker 297*0e209d39SAndroid Build Coastguard Worker /* definitions with forward iteration --------------------------------------- */ 298*0e209d39SAndroid Build Coastguard Worker 299*0e209d39SAndroid Build Coastguard Worker /** 300*0e209d39SAndroid Build Coastguard Worker * Get a code point from a string at a code point boundary offset, 301*0e209d39SAndroid Build Coastguard Worker * and advance the offset to the next code point boundary. 302*0e209d39SAndroid Build Coastguard Worker * (Post-incrementing forward iteration.) 303*0e209d39SAndroid Build Coastguard Worker * "Unsafe" macro, assumes well-formed UTF-8. 304*0e209d39SAndroid Build Coastguard Worker * 305*0e209d39SAndroid Build Coastguard Worker * The offset may point to the lead byte of a multi-byte sequence, 306*0e209d39SAndroid Build Coastguard Worker * in which case the macro will read the whole sequence. 307*0e209d39SAndroid Build Coastguard Worker * The result is undefined if the offset points to a trail byte 308*0e209d39SAndroid Build Coastguard Worker * or an illegal UTF-8 sequence. 309*0e209d39SAndroid Build Coastguard Worker * 310*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 311*0e209d39SAndroid Build Coastguard Worker * @param i string offset 312*0e209d39SAndroid Build Coastguard Worker * @param c output UChar32 variable 313*0e209d39SAndroid Build Coastguard Worker * @see U8_NEXT 314*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 315*0e209d39SAndroid Build Coastguard Worker */ 316*0e209d39SAndroid Build Coastguard Worker #define U8_NEXT_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \ 317*0e209d39SAndroid Build Coastguard Worker (c)=(uint8_t)(s)[(i)++]; \ 318*0e209d39SAndroid Build Coastguard Worker if(!U8_IS_SINGLE(c)) { \ 319*0e209d39SAndroid Build Coastguard Worker if((c)<0xe0) { \ 320*0e209d39SAndroid Build Coastguard Worker (c)=(((c)&0x1f)<<6)|((s)[(i)++]&0x3f); \ 321*0e209d39SAndroid Build Coastguard Worker } else if((c)<0xf0) { \ 322*0e209d39SAndroid Build Coastguard Worker /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \ 323*0e209d39SAndroid Build Coastguard Worker (c)=(UChar)(((c)<<12)|(((s)[i]&0x3f)<<6)|((s)[(i)+1]&0x3f)); \ 324*0e209d39SAndroid Build Coastguard Worker (i)+=2; \ 325*0e209d39SAndroid Build Coastguard Worker } else { \ 326*0e209d39SAndroid Build Coastguard Worker (c)=(((c)&7)<<18)|(((s)[i]&0x3f)<<12)|(((s)[(i)+1]&0x3f)<<6)|((s)[(i)+2]&0x3f); \ 327*0e209d39SAndroid Build Coastguard Worker (i)+=3; \ 328*0e209d39SAndroid Build Coastguard Worker } \ 329*0e209d39SAndroid Build Coastguard Worker } \ 330*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 331*0e209d39SAndroid Build Coastguard Worker 332*0e209d39SAndroid Build Coastguard Worker /** 333*0e209d39SAndroid Build Coastguard Worker * Get a code point from a string at a code point boundary offset, 334*0e209d39SAndroid Build Coastguard Worker * and advance the offset to the next code point boundary. 335*0e209d39SAndroid Build Coastguard Worker * (Post-incrementing forward iteration.) 336*0e209d39SAndroid Build Coastguard Worker * "Safe" macro, checks for illegal sequences and for string boundaries. 337*0e209d39SAndroid Build Coastguard Worker * 338*0e209d39SAndroid Build Coastguard Worker * The length can be negative for a NUL-terminated string. 339*0e209d39SAndroid Build Coastguard Worker * 340*0e209d39SAndroid Build Coastguard Worker * The offset may point to the lead byte of a multi-byte sequence, 341*0e209d39SAndroid Build Coastguard Worker * in which case the macro will read the whole sequence. 342*0e209d39SAndroid Build Coastguard Worker * If the offset points to a trail byte or an illegal UTF-8 sequence, then 343*0e209d39SAndroid Build Coastguard Worker * c is set to a negative value. 344*0e209d39SAndroid Build Coastguard Worker * 345*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 346*0e209d39SAndroid Build Coastguard Worker * @param i int32_t string offset, must be i<length 347*0e209d39SAndroid Build Coastguard Worker * @param length int32_t string length 348*0e209d39SAndroid Build Coastguard Worker * @param c output UChar32 variable, set to <0 in case of an error 349*0e209d39SAndroid Build Coastguard Worker * @see U8_NEXT_UNSAFE 350*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 351*0e209d39SAndroid Build Coastguard Worker */ 352*0e209d39SAndroid Build Coastguard Worker #define U8_NEXT(s, i, length, c) U8_INTERNAL_NEXT_OR_SUB(s, i, length, c, U_SENTINEL) 353*0e209d39SAndroid Build Coastguard Worker 354*0e209d39SAndroid Build Coastguard Worker /** 355*0e209d39SAndroid Build Coastguard Worker * Get a code point from a string at a code point boundary offset, 356*0e209d39SAndroid Build Coastguard Worker * and advance the offset to the next code point boundary. 357*0e209d39SAndroid Build Coastguard Worker * (Post-incrementing forward iteration.) 358*0e209d39SAndroid Build Coastguard Worker * "Safe" macro, checks for illegal sequences and for string boundaries. 359*0e209d39SAndroid Build Coastguard Worker * 360*0e209d39SAndroid Build Coastguard Worker * The length can be negative for a NUL-terminated string. 361*0e209d39SAndroid Build Coastguard Worker * 362*0e209d39SAndroid Build Coastguard Worker * The offset may point to the lead byte of a multi-byte sequence, 363*0e209d39SAndroid Build Coastguard Worker * in which case the macro will read the whole sequence. 364*0e209d39SAndroid Build Coastguard Worker * If the offset points to a trail byte or an illegal UTF-8 sequence, then 365*0e209d39SAndroid Build Coastguard Worker * c is set to U+FFFD. 366*0e209d39SAndroid Build Coastguard Worker * 367*0e209d39SAndroid Build Coastguard Worker * This macro does not distinguish between a real U+FFFD in the text 368*0e209d39SAndroid Build Coastguard Worker * and U+FFFD returned for an ill-formed sequence. 369*0e209d39SAndroid Build Coastguard Worker * Use U8_NEXT() if that distinction is important. 370*0e209d39SAndroid Build Coastguard Worker * 371*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 372*0e209d39SAndroid Build Coastguard Worker * @param i int32_t string offset, must be i<length 373*0e209d39SAndroid Build Coastguard Worker * @param length int32_t string length 374*0e209d39SAndroid Build Coastguard Worker * @param c output UChar32 variable, set to U+FFFD in case of an error 375*0e209d39SAndroid Build Coastguard Worker * @see U8_NEXT 376*0e209d39SAndroid Build Coastguard Worker * @stable ICU 51 377*0e209d39SAndroid Build Coastguard Worker */ 378*0e209d39SAndroid Build Coastguard Worker #define U8_NEXT_OR_FFFD(s, i, length, c) U8_INTERNAL_NEXT_OR_SUB(s, i, length, c, 0xfffd) 379*0e209d39SAndroid Build Coastguard Worker 380*0e209d39SAndroid Build Coastguard Worker /** @internal */ 381*0e209d39SAndroid Build Coastguard Worker #define U8_INTERNAL_NEXT_OR_SUB(s, i, length, c, sub) UPRV_BLOCK_MACRO_BEGIN { \ 382*0e209d39SAndroid Build Coastguard Worker (c)=(uint8_t)(s)[(i)++]; \ 383*0e209d39SAndroid Build Coastguard Worker if(!U8_IS_SINGLE(c)) { \ 384*0e209d39SAndroid Build Coastguard Worker uint8_t __t = 0; \ 385*0e209d39SAndroid Build Coastguard Worker if((i)!=(length) && \ 386*0e209d39SAndroid Build Coastguard Worker /* fetch/validate/assemble all but last trail byte */ \ 387*0e209d39SAndroid Build Coastguard Worker ((c)>=0xe0 ? \ 388*0e209d39SAndroid Build Coastguard Worker ((c)<0xf0 ? /* U+0800..U+FFFF except surrogates */ \ 389*0e209d39SAndroid Build Coastguard Worker U8_LEAD3_T1_BITS[(c)&=0xf]&(1<<((__t=(s)[i])>>5)) && \ 390*0e209d39SAndroid Build Coastguard Worker (__t&=0x3f, 1) \ 391*0e209d39SAndroid Build Coastguard Worker : /* U+10000..U+10FFFF */ \ 392*0e209d39SAndroid Build Coastguard Worker ((c)-=0xf0)<=4 && \ 393*0e209d39SAndroid Build Coastguard Worker U8_LEAD4_T1_BITS[(__t=(s)[i])>>4]&(1<<(c)) && \ 394*0e209d39SAndroid Build Coastguard Worker ((c)=((c)<<6)|(__t&0x3f), ++(i)!=(length)) && \ 395*0e209d39SAndroid Build Coastguard Worker (__t=(s)[i]-0x80)<=0x3f) && \ 396*0e209d39SAndroid Build Coastguard Worker /* valid second-to-last trail byte */ \ 397*0e209d39SAndroid Build Coastguard Worker ((c)=((c)<<6)|__t, ++(i)!=(length)) \ 398*0e209d39SAndroid Build Coastguard Worker : /* U+0080..U+07FF */ \ 399*0e209d39SAndroid Build Coastguard Worker (c)>=0xc2 && ((c)&=0x1f, 1)) && \ 400*0e209d39SAndroid Build Coastguard Worker /* last trail byte */ \ 401*0e209d39SAndroid Build Coastguard Worker (__t=(s)[i]-0x80)<=0x3f && \ 402*0e209d39SAndroid Build Coastguard Worker ((c)=((c)<<6)|__t, ++(i), 1)) { \ 403*0e209d39SAndroid Build Coastguard Worker } else { \ 404*0e209d39SAndroid Build Coastguard Worker (c)=(sub); /* ill-formed*/ \ 405*0e209d39SAndroid Build Coastguard Worker } \ 406*0e209d39SAndroid Build Coastguard Worker } \ 407*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 408*0e209d39SAndroid Build Coastguard Worker 409*0e209d39SAndroid Build Coastguard Worker /** 410*0e209d39SAndroid Build Coastguard Worker * Append a code point to a string, overwriting 1 to 4 bytes. 411*0e209d39SAndroid Build Coastguard Worker * The offset points to the current end of the string contents 412*0e209d39SAndroid Build Coastguard Worker * and is advanced (post-increment). 413*0e209d39SAndroid Build Coastguard Worker * "Unsafe" macro, assumes a valid code point and sufficient space in the string. 414*0e209d39SAndroid Build Coastguard Worker * Otherwise, the result is undefined. 415*0e209d39SAndroid Build Coastguard Worker * 416*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string buffer 417*0e209d39SAndroid Build Coastguard Worker * @param i string offset 418*0e209d39SAndroid Build Coastguard Worker * @param c code point to append 419*0e209d39SAndroid Build Coastguard Worker * @see U8_APPEND 420*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 421*0e209d39SAndroid Build Coastguard Worker */ 422*0e209d39SAndroid Build Coastguard Worker #define U8_APPEND_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \ 423*0e209d39SAndroid Build Coastguard Worker uint32_t __uc=(c); \ 424*0e209d39SAndroid Build Coastguard Worker if(__uc<=0x7f) { \ 425*0e209d39SAndroid Build Coastguard Worker (s)[(i)++]=(uint8_t)__uc; \ 426*0e209d39SAndroid Build Coastguard Worker } else { \ 427*0e209d39SAndroid Build Coastguard Worker if(__uc<=0x7ff) { \ 428*0e209d39SAndroid Build Coastguard Worker (s)[(i)++]=(uint8_t)((__uc>>6)|0xc0); \ 429*0e209d39SAndroid Build Coastguard Worker } else { \ 430*0e209d39SAndroid Build Coastguard Worker if(__uc<=0xffff) { \ 431*0e209d39SAndroid Build Coastguard Worker (s)[(i)++]=(uint8_t)((__uc>>12)|0xe0); \ 432*0e209d39SAndroid Build Coastguard Worker } else { \ 433*0e209d39SAndroid Build Coastguard Worker (s)[(i)++]=(uint8_t)((__uc>>18)|0xf0); \ 434*0e209d39SAndroid Build Coastguard Worker (s)[(i)++]=(uint8_t)(((__uc>>12)&0x3f)|0x80); \ 435*0e209d39SAndroid Build Coastguard Worker } \ 436*0e209d39SAndroid Build Coastguard Worker (s)[(i)++]=(uint8_t)(((__uc>>6)&0x3f)|0x80); \ 437*0e209d39SAndroid Build Coastguard Worker } \ 438*0e209d39SAndroid Build Coastguard Worker (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \ 439*0e209d39SAndroid Build Coastguard Worker } \ 440*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 441*0e209d39SAndroid Build Coastguard Worker 442*0e209d39SAndroid Build Coastguard Worker /** 443*0e209d39SAndroid Build Coastguard Worker * Append a code point to a string, overwriting 1 to 4 bytes. 444*0e209d39SAndroid Build Coastguard Worker * The offset points to the current end of the string contents 445*0e209d39SAndroid Build Coastguard Worker * and is advanced (post-increment). 446*0e209d39SAndroid Build Coastguard Worker * "Safe" macro, checks for a valid code point. 447*0e209d39SAndroid Build Coastguard Worker * If a non-ASCII code point is written, checks for sufficient space in the string. 448*0e209d39SAndroid Build Coastguard Worker * If the code point is not valid or trail bytes do not fit, 449*0e209d39SAndroid Build Coastguard Worker * then isError is set to true. 450*0e209d39SAndroid Build Coastguard Worker * 451*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string buffer 452*0e209d39SAndroid Build Coastguard Worker * @param i int32_t string offset, must be i<capacity 453*0e209d39SAndroid Build Coastguard Worker * @param capacity int32_t size of the string buffer 454*0e209d39SAndroid Build Coastguard Worker * @param c UChar32 code point to append 455*0e209d39SAndroid Build Coastguard Worker * @param isError output UBool set to true if an error occurs, otherwise not modified 456*0e209d39SAndroid Build Coastguard Worker * @see U8_APPEND_UNSAFE 457*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 458*0e209d39SAndroid Build Coastguard Worker */ 459*0e209d39SAndroid Build Coastguard Worker #define U8_APPEND(s, i, capacity, c, isError) UPRV_BLOCK_MACRO_BEGIN { \ 460*0e209d39SAndroid Build Coastguard Worker uint32_t __uc=(c); \ 461*0e209d39SAndroid Build Coastguard Worker if(__uc<=0x7f) { \ 462*0e209d39SAndroid Build Coastguard Worker (s)[(i)++]=(uint8_t)__uc; \ 463*0e209d39SAndroid Build Coastguard Worker } else if(__uc<=0x7ff && (i)+1<(capacity)) { \ 464*0e209d39SAndroid Build Coastguard Worker (s)[(i)++]=(uint8_t)((__uc>>6)|0xc0); \ 465*0e209d39SAndroid Build Coastguard Worker (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \ 466*0e209d39SAndroid Build Coastguard Worker } else if((__uc<=0xd7ff || (0xe000<=__uc && __uc<=0xffff)) && (i)+2<(capacity)) { \ 467*0e209d39SAndroid Build Coastguard Worker (s)[(i)++]=(uint8_t)((__uc>>12)|0xe0); \ 468*0e209d39SAndroid Build Coastguard Worker (s)[(i)++]=(uint8_t)(((__uc>>6)&0x3f)|0x80); \ 469*0e209d39SAndroid Build Coastguard Worker (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \ 470*0e209d39SAndroid Build Coastguard Worker } else if(0xffff<__uc && __uc<=0x10ffff && (i)+3<(capacity)) { \ 471*0e209d39SAndroid Build Coastguard Worker (s)[(i)++]=(uint8_t)((__uc>>18)|0xf0); \ 472*0e209d39SAndroid Build Coastguard Worker (s)[(i)++]=(uint8_t)(((__uc>>12)&0x3f)|0x80); \ 473*0e209d39SAndroid Build Coastguard Worker (s)[(i)++]=(uint8_t)(((__uc>>6)&0x3f)|0x80); \ 474*0e209d39SAndroid Build Coastguard Worker (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \ 475*0e209d39SAndroid Build Coastguard Worker } else { \ 476*0e209d39SAndroid Build Coastguard Worker (isError)=true; \ 477*0e209d39SAndroid Build Coastguard Worker } \ 478*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 479*0e209d39SAndroid Build Coastguard Worker 480*0e209d39SAndroid Build Coastguard Worker /** 481*0e209d39SAndroid Build Coastguard Worker * Advance the string offset from one code point boundary to the next. 482*0e209d39SAndroid Build Coastguard Worker * (Post-incrementing iteration.) 483*0e209d39SAndroid Build Coastguard Worker * "Unsafe" macro, assumes well-formed UTF-8. 484*0e209d39SAndroid Build Coastguard Worker * 485*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 486*0e209d39SAndroid Build Coastguard Worker * @param i string offset 487*0e209d39SAndroid Build Coastguard Worker * @see U8_FWD_1 488*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 489*0e209d39SAndroid Build Coastguard Worker */ 490*0e209d39SAndroid Build Coastguard Worker #define U8_FWD_1_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \ 491*0e209d39SAndroid Build Coastguard Worker (i)+=1+U8_COUNT_TRAIL_BYTES_UNSAFE((s)[i]); \ 492*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 493*0e209d39SAndroid Build Coastguard Worker 494*0e209d39SAndroid Build Coastguard Worker /** 495*0e209d39SAndroid Build Coastguard Worker * Advance the string offset from one code point boundary to the next. 496*0e209d39SAndroid Build Coastguard Worker * (Post-incrementing iteration.) 497*0e209d39SAndroid Build Coastguard Worker * "Safe" macro, checks for illegal sequences and for string boundaries. 498*0e209d39SAndroid Build Coastguard Worker * 499*0e209d39SAndroid Build Coastguard Worker * The length can be negative for a NUL-terminated string. 500*0e209d39SAndroid Build Coastguard Worker * 501*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 502*0e209d39SAndroid Build Coastguard Worker * @param i int32_t string offset, must be i<length 503*0e209d39SAndroid Build Coastguard Worker * @param length int32_t string length 504*0e209d39SAndroid Build Coastguard Worker * @see U8_FWD_1_UNSAFE 505*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 506*0e209d39SAndroid Build Coastguard Worker */ 507*0e209d39SAndroid Build Coastguard Worker #define U8_FWD_1(s, i, length) UPRV_BLOCK_MACRO_BEGIN { \ 508*0e209d39SAndroid Build Coastguard Worker uint8_t __b=(s)[(i)++]; \ 509*0e209d39SAndroid Build Coastguard Worker if(U8_IS_LEAD(__b) && (i)!=(length)) { \ 510*0e209d39SAndroid Build Coastguard Worker uint8_t __t1=(s)[i]; \ 511*0e209d39SAndroid Build Coastguard Worker if((0xe0<=__b && __b<0xf0)) { \ 512*0e209d39SAndroid Build Coastguard Worker if(U8_IS_VALID_LEAD3_AND_T1(__b, __t1) && \ 513*0e209d39SAndroid Build Coastguard Worker ++(i)!=(length) && U8_IS_TRAIL((s)[i])) { \ 514*0e209d39SAndroid Build Coastguard Worker ++(i); \ 515*0e209d39SAndroid Build Coastguard Worker } \ 516*0e209d39SAndroid Build Coastguard Worker } else if(__b<0xe0) { \ 517*0e209d39SAndroid Build Coastguard Worker if(U8_IS_TRAIL(__t1)) { \ 518*0e209d39SAndroid Build Coastguard Worker ++(i); \ 519*0e209d39SAndroid Build Coastguard Worker } \ 520*0e209d39SAndroid Build Coastguard Worker } else /* c>=0xf0 */ { \ 521*0e209d39SAndroid Build Coastguard Worker if(U8_IS_VALID_LEAD4_AND_T1(__b, __t1) && \ 522*0e209d39SAndroid Build Coastguard Worker ++(i)!=(length) && U8_IS_TRAIL((s)[i]) && \ 523*0e209d39SAndroid Build Coastguard Worker ++(i)!=(length) && U8_IS_TRAIL((s)[i])) { \ 524*0e209d39SAndroid Build Coastguard Worker ++(i); \ 525*0e209d39SAndroid Build Coastguard Worker } \ 526*0e209d39SAndroid Build Coastguard Worker } \ 527*0e209d39SAndroid Build Coastguard Worker } \ 528*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 529*0e209d39SAndroid Build Coastguard Worker 530*0e209d39SAndroid Build Coastguard Worker /** 531*0e209d39SAndroid Build Coastguard Worker * Advance the string offset from one code point boundary to the n-th next one, 532*0e209d39SAndroid Build Coastguard Worker * i.e., move forward by n code points. 533*0e209d39SAndroid Build Coastguard Worker * (Post-incrementing iteration.) 534*0e209d39SAndroid Build Coastguard Worker * "Unsafe" macro, assumes well-formed UTF-8. 535*0e209d39SAndroid Build Coastguard Worker * 536*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 537*0e209d39SAndroid Build Coastguard Worker * @param i string offset 538*0e209d39SAndroid Build Coastguard Worker * @param n number of code points to skip 539*0e209d39SAndroid Build Coastguard Worker * @see U8_FWD_N 540*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 541*0e209d39SAndroid Build Coastguard Worker */ 542*0e209d39SAndroid Build Coastguard Worker #define U8_FWD_N_UNSAFE(s, i, n) UPRV_BLOCK_MACRO_BEGIN { \ 543*0e209d39SAndroid Build Coastguard Worker int32_t __N=(n); \ 544*0e209d39SAndroid Build Coastguard Worker while(__N>0) { \ 545*0e209d39SAndroid Build Coastguard Worker U8_FWD_1_UNSAFE(s, i); \ 546*0e209d39SAndroid Build Coastguard Worker --__N; \ 547*0e209d39SAndroid Build Coastguard Worker } \ 548*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 549*0e209d39SAndroid Build Coastguard Worker 550*0e209d39SAndroid Build Coastguard Worker /** 551*0e209d39SAndroid Build Coastguard Worker * Advance the string offset from one code point boundary to the n-th next one, 552*0e209d39SAndroid Build Coastguard Worker * i.e., move forward by n code points. 553*0e209d39SAndroid Build Coastguard Worker * (Post-incrementing iteration.) 554*0e209d39SAndroid Build Coastguard Worker * "Safe" macro, checks for illegal sequences and for string boundaries. 555*0e209d39SAndroid Build Coastguard Worker * 556*0e209d39SAndroid Build Coastguard Worker * The length can be negative for a NUL-terminated string. 557*0e209d39SAndroid Build Coastguard Worker * 558*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 559*0e209d39SAndroid Build Coastguard Worker * @param i int32_t string offset, must be i<length 560*0e209d39SAndroid Build Coastguard Worker * @param length int32_t string length 561*0e209d39SAndroid Build Coastguard Worker * @param n number of code points to skip 562*0e209d39SAndroid Build Coastguard Worker * @see U8_FWD_N_UNSAFE 563*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 564*0e209d39SAndroid Build Coastguard Worker */ 565*0e209d39SAndroid Build Coastguard Worker #define U8_FWD_N(s, i, length, n) UPRV_BLOCK_MACRO_BEGIN { \ 566*0e209d39SAndroid Build Coastguard Worker int32_t __N=(n); \ 567*0e209d39SAndroid Build Coastguard Worker while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \ 568*0e209d39SAndroid Build Coastguard Worker U8_FWD_1(s, i, length); \ 569*0e209d39SAndroid Build Coastguard Worker --__N; \ 570*0e209d39SAndroid Build Coastguard Worker } \ 571*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 572*0e209d39SAndroid Build Coastguard Worker 573*0e209d39SAndroid Build Coastguard Worker /** 574*0e209d39SAndroid Build Coastguard Worker * Adjust a random-access offset to a code point boundary 575*0e209d39SAndroid Build Coastguard Worker * at the start of a code point. 576*0e209d39SAndroid Build Coastguard Worker * If the offset points to a UTF-8 trail byte, 577*0e209d39SAndroid Build Coastguard Worker * then the offset is moved backward to the corresponding lead byte. 578*0e209d39SAndroid Build Coastguard Worker * Otherwise, it is not modified. 579*0e209d39SAndroid Build Coastguard Worker * "Unsafe" macro, assumes well-formed UTF-8. 580*0e209d39SAndroid Build Coastguard Worker * 581*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 582*0e209d39SAndroid Build Coastguard Worker * @param i string offset 583*0e209d39SAndroid Build Coastguard Worker * @see U8_SET_CP_START 584*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 585*0e209d39SAndroid Build Coastguard Worker */ 586*0e209d39SAndroid Build Coastguard Worker #define U8_SET_CP_START_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \ 587*0e209d39SAndroid Build Coastguard Worker while(U8_IS_TRAIL((s)[i])) { --(i); } \ 588*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 589*0e209d39SAndroid Build Coastguard Worker 590*0e209d39SAndroid Build Coastguard Worker /** 591*0e209d39SAndroid Build Coastguard Worker * Adjust a random-access offset to a code point boundary 592*0e209d39SAndroid Build Coastguard Worker * at the start of a code point. 593*0e209d39SAndroid Build Coastguard Worker * If the offset points to a UTF-8 trail byte, 594*0e209d39SAndroid Build Coastguard Worker * then the offset is moved backward to the corresponding lead byte. 595*0e209d39SAndroid Build Coastguard Worker * Otherwise, it is not modified. 596*0e209d39SAndroid Build Coastguard Worker * 597*0e209d39SAndroid Build Coastguard Worker * "Safe" macro, checks for illegal sequences and for string boundaries. 598*0e209d39SAndroid Build Coastguard Worker * Unlike U8_TRUNCATE_IF_INCOMPLETE(), this macro always reads s[i]. 599*0e209d39SAndroid Build Coastguard Worker * 600*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 601*0e209d39SAndroid Build Coastguard Worker * @param start int32_t starting string offset (usually 0) 602*0e209d39SAndroid Build Coastguard Worker * @param i int32_t string offset, must be start<=i 603*0e209d39SAndroid Build Coastguard Worker * @see U8_SET_CP_START_UNSAFE 604*0e209d39SAndroid Build Coastguard Worker * @see U8_TRUNCATE_IF_INCOMPLETE 605*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 606*0e209d39SAndroid Build Coastguard Worker */ 607*0e209d39SAndroid Build Coastguard Worker #define U8_SET_CP_START(s, start, i) UPRV_BLOCK_MACRO_BEGIN { \ 608*0e209d39SAndroid Build Coastguard Worker if(U8_IS_TRAIL((s)[(i)])) { \ 609*0e209d39SAndroid Build Coastguard Worker (i)=utf8_back1SafeBody(s, start, (i)); \ 610*0e209d39SAndroid Build Coastguard Worker } \ 611*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 612*0e209d39SAndroid Build Coastguard Worker 613*0e209d39SAndroid Build Coastguard Worker /** 614*0e209d39SAndroid Build Coastguard Worker * If the string ends with a UTF-8 byte sequence that is valid so far 615*0e209d39SAndroid Build Coastguard Worker * but incomplete, then reduce the length of the string to end before 616*0e209d39SAndroid Build Coastguard Worker * the lead byte of that incomplete sequence. 617*0e209d39SAndroid Build Coastguard Worker * For example, if the string ends with E1 80, the length is reduced by 2. 618*0e209d39SAndroid Build Coastguard Worker * 619*0e209d39SAndroid Build Coastguard Worker * In all other cases (the string ends with a complete sequence, or it is not 620*0e209d39SAndroid Build Coastguard Worker * possible for any further trail byte to extend the trailing sequence) 621*0e209d39SAndroid Build Coastguard Worker * the length remains unchanged. 622*0e209d39SAndroid Build Coastguard Worker * 623*0e209d39SAndroid Build Coastguard Worker * Useful for processing text split across multiple buffers 624*0e209d39SAndroid Build Coastguard Worker * (save the incomplete sequence for later) 625*0e209d39SAndroid Build Coastguard Worker * and for optimizing iteration 626*0e209d39SAndroid Build Coastguard Worker * (check for string length only once per character). 627*0e209d39SAndroid Build Coastguard Worker * 628*0e209d39SAndroid Build Coastguard Worker * "Safe" macro, checks for illegal sequences and for string boundaries. 629*0e209d39SAndroid Build Coastguard Worker * Unlike U8_SET_CP_START(), this macro never reads s[length]. 630*0e209d39SAndroid Build Coastguard Worker * 631*0e209d39SAndroid Build Coastguard Worker * (In UTF-16, simply check for U16_IS_LEAD(last code unit).) 632*0e209d39SAndroid Build Coastguard Worker * 633*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 634*0e209d39SAndroid Build Coastguard Worker * @param start int32_t starting string offset (usually 0) 635*0e209d39SAndroid Build Coastguard Worker * @param length int32_t string length (usually start<=length) 636*0e209d39SAndroid Build Coastguard Worker * @see U8_SET_CP_START 637*0e209d39SAndroid Build Coastguard Worker * @stable ICU 61 638*0e209d39SAndroid Build Coastguard Worker */ 639*0e209d39SAndroid Build Coastguard Worker #define U8_TRUNCATE_IF_INCOMPLETE(s, start, length) UPRV_BLOCK_MACRO_BEGIN { \ 640*0e209d39SAndroid Build Coastguard Worker if((length)>(start)) { \ 641*0e209d39SAndroid Build Coastguard Worker uint8_t __b1=s[(length)-1]; \ 642*0e209d39SAndroid Build Coastguard Worker if(U8_IS_SINGLE(__b1)) { \ 643*0e209d39SAndroid Build Coastguard Worker /* common ASCII character */ \ 644*0e209d39SAndroid Build Coastguard Worker } else if(U8_IS_LEAD(__b1)) { \ 645*0e209d39SAndroid Build Coastguard Worker --(length); \ 646*0e209d39SAndroid Build Coastguard Worker } else if(U8_IS_TRAIL(__b1) && ((length)-2)>=(start)) { \ 647*0e209d39SAndroid Build Coastguard Worker uint8_t __b2=s[(length)-2]; \ 648*0e209d39SAndroid Build Coastguard Worker if(0xe0<=__b2 && __b2<=0xf4) { \ 649*0e209d39SAndroid Build Coastguard Worker if(__b2<0xf0 ? U8_IS_VALID_LEAD3_AND_T1(__b2, __b1) : \ 650*0e209d39SAndroid Build Coastguard Worker U8_IS_VALID_LEAD4_AND_T1(__b2, __b1)) { \ 651*0e209d39SAndroid Build Coastguard Worker (length)-=2; \ 652*0e209d39SAndroid Build Coastguard Worker } \ 653*0e209d39SAndroid Build Coastguard Worker } else if(U8_IS_TRAIL(__b2) && ((length)-3)>=(start)) { \ 654*0e209d39SAndroid Build Coastguard Worker uint8_t __b3=s[(length)-3]; \ 655*0e209d39SAndroid Build Coastguard Worker if(0xf0<=__b3 && __b3<=0xf4 && U8_IS_VALID_LEAD4_AND_T1(__b3, __b2)) { \ 656*0e209d39SAndroid Build Coastguard Worker (length)-=3; \ 657*0e209d39SAndroid Build Coastguard Worker } \ 658*0e209d39SAndroid Build Coastguard Worker } \ 659*0e209d39SAndroid Build Coastguard Worker } \ 660*0e209d39SAndroid Build Coastguard Worker } \ 661*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 662*0e209d39SAndroid Build Coastguard Worker 663*0e209d39SAndroid Build Coastguard Worker /* definitions with backward iteration -------------------------------------- */ 664*0e209d39SAndroid Build Coastguard Worker 665*0e209d39SAndroid Build Coastguard Worker /** 666*0e209d39SAndroid Build Coastguard Worker * Move the string offset from one code point boundary to the previous one 667*0e209d39SAndroid Build Coastguard Worker * and get the code point between them. 668*0e209d39SAndroid Build Coastguard Worker * (Pre-decrementing backward iteration.) 669*0e209d39SAndroid Build Coastguard Worker * "Unsafe" macro, assumes well-formed UTF-8. 670*0e209d39SAndroid Build Coastguard Worker * 671*0e209d39SAndroid Build Coastguard Worker * The input offset may be the same as the string length. 672*0e209d39SAndroid Build Coastguard Worker * If the offset is behind a multi-byte sequence, then the macro will read 673*0e209d39SAndroid Build Coastguard Worker * the whole sequence. 674*0e209d39SAndroid Build Coastguard Worker * If the offset is behind a lead byte, then that itself 675*0e209d39SAndroid Build Coastguard Worker * will be returned as the code point. 676*0e209d39SAndroid Build Coastguard Worker * The result is undefined if the offset is behind an illegal UTF-8 sequence. 677*0e209d39SAndroid Build Coastguard Worker * 678*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 679*0e209d39SAndroid Build Coastguard Worker * @param i string offset 680*0e209d39SAndroid Build Coastguard Worker * @param c output UChar32 variable 681*0e209d39SAndroid Build Coastguard Worker * @see U8_PREV 682*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 683*0e209d39SAndroid Build Coastguard Worker */ 684*0e209d39SAndroid Build Coastguard Worker #define U8_PREV_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \ 685*0e209d39SAndroid Build Coastguard Worker (c)=(uint8_t)(s)[--(i)]; \ 686*0e209d39SAndroid Build Coastguard Worker if(U8_IS_TRAIL(c)) { \ 687*0e209d39SAndroid Build Coastguard Worker uint8_t __b, __count=1, __shift=6; \ 688*0e209d39SAndroid Build Coastguard Worker \ 689*0e209d39SAndroid Build Coastguard Worker /* c is a trail byte */ \ 690*0e209d39SAndroid Build Coastguard Worker (c)&=0x3f; \ 691*0e209d39SAndroid Build Coastguard Worker for(;;) { \ 692*0e209d39SAndroid Build Coastguard Worker __b=(s)[--(i)]; \ 693*0e209d39SAndroid Build Coastguard Worker if(__b>=0xc0) { \ 694*0e209d39SAndroid Build Coastguard Worker U8_MASK_LEAD_BYTE(__b, __count); \ 695*0e209d39SAndroid Build Coastguard Worker (c)|=(UChar32)__b<<__shift; \ 696*0e209d39SAndroid Build Coastguard Worker break; \ 697*0e209d39SAndroid Build Coastguard Worker } else { \ 698*0e209d39SAndroid Build Coastguard Worker (c)|=(UChar32)(__b&0x3f)<<__shift; \ 699*0e209d39SAndroid Build Coastguard Worker ++__count; \ 700*0e209d39SAndroid Build Coastguard Worker __shift+=6; \ 701*0e209d39SAndroid Build Coastguard Worker } \ 702*0e209d39SAndroid Build Coastguard Worker } \ 703*0e209d39SAndroid Build Coastguard Worker } \ 704*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 705*0e209d39SAndroid Build Coastguard Worker 706*0e209d39SAndroid Build Coastguard Worker /** 707*0e209d39SAndroid Build Coastguard Worker * Move the string offset from one code point boundary to the previous one 708*0e209d39SAndroid Build Coastguard Worker * and get the code point between them. 709*0e209d39SAndroid Build Coastguard Worker * (Pre-decrementing backward iteration.) 710*0e209d39SAndroid Build Coastguard Worker * "Safe" macro, checks for illegal sequences and for string boundaries. 711*0e209d39SAndroid Build Coastguard Worker * 712*0e209d39SAndroid Build Coastguard Worker * The input offset may be the same as the string length. 713*0e209d39SAndroid Build Coastguard Worker * If the offset is behind a multi-byte sequence, then the macro will read 714*0e209d39SAndroid Build Coastguard Worker * the whole sequence. 715*0e209d39SAndroid Build Coastguard Worker * If the offset is behind a lead byte, then that itself 716*0e209d39SAndroid Build Coastguard Worker * will be returned as the code point. 717*0e209d39SAndroid Build Coastguard Worker * If the offset is behind an illegal UTF-8 sequence, then c is set to a negative value. 718*0e209d39SAndroid Build Coastguard Worker * 719*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 720*0e209d39SAndroid Build Coastguard Worker * @param start int32_t starting string offset (usually 0) 721*0e209d39SAndroid Build Coastguard Worker * @param i int32_t string offset, must be start<i 722*0e209d39SAndroid Build Coastguard Worker * @param c output UChar32 variable, set to <0 in case of an error 723*0e209d39SAndroid Build Coastguard Worker * @see U8_PREV_UNSAFE 724*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 725*0e209d39SAndroid Build Coastguard Worker */ 726*0e209d39SAndroid Build Coastguard Worker #define U8_PREV(s, start, i, c) UPRV_BLOCK_MACRO_BEGIN { \ 727*0e209d39SAndroid Build Coastguard Worker (c)=(uint8_t)(s)[--(i)]; \ 728*0e209d39SAndroid Build Coastguard Worker if(!U8_IS_SINGLE(c)) { \ 729*0e209d39SAndroid Build Coastguard Worker (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \ 730*0e209d39SAndroid Build Coastguard Worker } \ 731*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 732*0e209d39SAndroid Build Coastguard Worker 733*0e209d39SAndroid Build Coastguard Worker /** 734*0e209d39SAndroid Build Coastguard Worker * Move the string offset from one code point boundary to the previous one 735*0e209d39SAndroid Build Coastguard Worker * and get the code point between them. 736*0e209d39SAndroid Build Coastguard Worker * (Pre-decrementing backward iteration.) 737*0e209d39SAndroid Build Coastguard Worker * "Safe" macro, checks for illegal sequences and for string boundaries. 738*0e209d39SAndroid Build Coastguard Worker * 739*0e209d39SAndroid Build Coastguard Worker * The input offset may be the same as the string length. 740*0e209d39SAndroid Build Coastguard Worker * If the offset is behind a multi-byte sequence, then the macro will read 741*0e209d39SAndroid Build Coastguard Worker * the whole sequence. 742*0e209d39SAndroid Build Coastguard Worker * If the offset is behind a lead byte, then that itself 743*0e209d39SAndroid Build Coastguard Worker * will be returned as the code point. 744*0e209d39SAndroid Build Coastguard Worker * If the offset is behind an illegal UTF-8 sequence, then c is set to U+FFFD. 745*0e209d39SAndroid Build Coastguard Worker * 746*0e209d39SAndroid Build Coastguard Worker * This macro does not distinguish between a real U+FFFD in the text 747*0e209d39SAndroid Build Coastguard Worker * and U+FFFD returned for an ill-formed sequence. 748*0e209d39SAndroid Build Coastguard Worker * Use U8_PREV() if that distinction is important. 749*0e209d39SAndroid Build Coastguard Worker * 750*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 751*0e209d39SAndroid Build Coastguard Worker * @param start int32_t starting string offset (usually 0) 752*0e209d39SAndroid Build Coastguard Worker * @param i int32_t string offset, must be start<i 753*0e209d39SAndroid Build Coastguard Worker * @param c output UChar32 variable, set to U+FFFD in case of an error 754*0e209d39SAndroid Build Coastguard Worker * @see U8_PREV 755*0e209d39SAndroid Build Coastguard Worker * @stable ICU 51 756*0e209d39SAndroid Build Coastguard Worker */ 757*0e209d39SAndroid Build Coastguard Worker #define U8_PREV_OR_FFFD(s, start, i, c) UPRV_BLOCK_MACRO_BEGIN { \ 758*0e209d39SAndroid Build Coastguard Worker (c)=(uint8_t)(s)[--(i)]; \ 759*0e209d39SAndroid Build Coastguard Worker if(!U8_IS_SINGLE(c)) { \ 760*0e209d39SAndroid Build Coastguard Worker (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -3); \ 761*0e209d39SAndroid Build Coastguard Worker } \ 762*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 763*0e209d39SAndroid Build Coastguard Worker 764*0e209d39SAndroid Build Coastguard Worker /** 765*0e209d39SAndroid Build Coastguard Worker * Move the string offset from one code point boundary to the previous one. 766*0e209d39SAndroid Build Coastguard Worker * (Pre-decrementing backward iteration.) 767*0e209d39SAndroid Build Coastguard Worker * The input offset may be the same as the string length. 768*0e209d39SAndroid Build Coastguard Worker * "Unsafe" macro, assumes well-formed UTF-8. 769*0e209d39SAndroid Build Coastguard Worker * 770*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 771*0e209d39SAndroid Build Coastguard Worker * @param i string offset 772*0e209d39SAndroid Build Coastguard Worker * @see U8_BACK_1 773*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 774*0e209d39SAndroid Build Coastguard Worker */ 775*0e209d39SAndroid Build Coastguard Worker #define U8_BACK_1_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \ 776*0e209d39SAndroid Build Coastguard Worker while(U8_IS_TRAIL((s)[--(i)])) {} \ 777*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 778*0e209d39SAndroid Build Coastguard Worker 779*0e209d39SAndroid Build Coastguard Worker /** 780*0e209d39SAndroid Build Coastguard Worker * Move the string offset from one code point boundary to the previous one. 781*0e209d39SAndroid Build Coastguard Worker * (Pre-decrementing backward iteration.) 782*0e209d39SAndroid Build Coastguard Worker * The input offset may be the same as the string length. 783*0e209d39SAndroid Build Coastguard Worker * "Safe" macro, checks for illegal sequences and for string boundaries. 784*0e209d39SAndroid Build Coastguard Worker * 785*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 786*0e209d39SAndroid Build Coastguard Worker * @param start int32_t starting string offset (usually 0) 787*0e209d39SAndroid Build Coastguard Worker * @param i int32_t string offset, must be start<i 788*0e209d39SAndroid Build Coastguard Worker * @see U8_BACK_1_UNSAFE 789*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 790*0e209d39SAndroid Build Coastguard Worker */ 791*0e209d39SAndroid Build Coastguard Worker #define U8_BACK_1(s, start, i) UPRV_BLOCK_MACRO_BEGIN { \ 792*0e209d39SAndroid Build Coastguard Worker if(U8_IS_TRAIL((s)[--(i)])) { \ 793*0e209d39SAndroid Build Coastguard Worker (i)=utf8_back1SafeBody(s, start, (i)); \ 794*0e209d39SAndroid Build Coastguard Worker } \ 795*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 796*0e209d39SAndroid Build Coastguard Worker 797*0e209d39SAndroid Build Coastguard Worker /** 798*0e209d39SAndroid Build Coastguard Worker * Move the string offset from one code point boundary to the n-th one before it, 799*0e209d39SAndroid Build Coastguard Worker * i.e., move backward by n code points. 800*0e209d39SAndroid Build Coastguard Worker * (Pre-decrementing backward iteration.) 801*0e209d39SAndroid Build Coastguard Worker * The input offset may be the same as the string length. 802*0e209d39SAndroid Build Coastguard Worker * "Unsafe" macro, assumes well-formed UTF-8. 803*0e209d39SAndroid Build Coastguard Worker * 804*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 805*0e209d39SAndroid Build Coastguard Worker * @param i string offset 806*0e209d39SAndroid Build Coastguard Worker * @param n number of code points to skip 807*0e209d39SAndroid Build Coastguard Worker * @see U8_BACK_N 808*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 809*0e209d39SAndroid Build Coastguard Worker */ 810*0e209d39SAndroid Build Coastguard Worker #define U8_BACK_N_UNSAFE(s, i, n) UPRV_BLOCK_MACRO_BEGIN { \ 811*0e209d39SAndroid Build Coastguard Worker int32_t __N=(n); \ 812*0e209d39SAndroid Build Coastguard Worker while(__N>0) { \ 813*0e209d39SAndroid Build Coastguard Worker U8_BACK_1_UNSAFE(s, i); \ 814*0e209d39SAndroid Build Coastguard Worker --__N; \ 815*0e209d39SAndroid Build Coastguard Worker } \ 816*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 817*0e209d39SAndroid Build Coastguard Worker 818*0e209d39SAndroid Build Coastguard Worker /** 819*0e209d39SAndroid Build Coastguard Worker * Move the string offset from one code point boundary to the n-th one before it, 820*0e209d39SAndroid Build Coastguard Worker * i.e., move backward by n code points. 821*0e209d39SAndroid Build Coastguard Worker * (Pre-decrementing backward iteration.) 822*0e209d39SAndroid Build Coastguard Worker * The input offset may be the same as the string length. 823*0e209d39SAndroid Build Coastguard Worker * "Safe" macro, checks for illegal sequences and for string boundaries. 824*0e209d39SAndroid Build Coastguard Worker * 825*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 826*0e209d39SAndroid Build Coastguard Worker * @param start int32_t index of the start of the string 827*0e209d39SAndroid Build Coastguard Worker * @param i int32_t string offset, must be start<i 828*0e209d39SAndroid Build Coastguard Worker * @param n number of code points to skip 829*0e209d39SAndroid Build Coastguard Worker * @see U8_BACK_N_UNSAFE 830*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 831*0e209d39SAndroid Build Coastguard Worker */ 832*0e209d39SAndroid Build Coastguard Worker #define U8_BACK_N(s, start, i, n) UPRV_BLOCK_MACRO_BEGIN { \ 833*0e209d39SAndroid Build Coastguard Worker int32_t __N=(n); \ 834*0e209d39SAndroid Build Coastguard Worker while(__N>0 && (i)>(start)) { \ 835*0e209d39SAndroid Build Coastguard Worker U8_BACK_1(s, start, i); \ 836*0e209d39SAndroid Build Coastguard Worker --__N; \ 837*0e209d39SAndroid Build Coastguard Worker } \ 838*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 839*0e209d39SAndroid Build Coastguard Worker 840*0e209d39SAndroid Build Coastguard Worker /** 841*0e209d39SAndroid Build Coastguard Worker * Adjust a random-access offset to a code point boundary after a code point. 842*0e209d39SAndroid Build Coastguard Worker * If the offset is behind a partial multi-byte sequence, 843*0e209d39SAndroid Build Coastguard Worker * then the offset is incremented to behind the whole sequence. 844*0e209d39SAndroid Build Coastguard Worker * Otherwise, it is not modified. 845*0e209d39SAndroid Build Coastguard Worker * The input offset may be the same as the string length. 846*0e209d39SAndroid Build Coastguard Worker * "Unsafe" macro, assumes well-formed UTF-8. 847*0e209d39SAndroid Build Coastguard Worker * 848*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 849*0e209d39SAndroid Build Coastguard Worker * @param i string offset 850*0e209d39SAndroid Build Coastguard Worker * @see U8_SET_CP_LIMIT 851*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 852*0e209d39SAndroid Build Coastguard Worker */ 853*0e209d39SAndroid Build Coastguard Worker #define U8_SET_CP_LIMIT_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \ 854*0e209d39SAndroid Build Coastguard Worker U8_BACK_1_UNSAFE(s, i); \ 855*0e209d39SAndroid Build Coastguard Worker U8_FWD_1_UNSAFE(s, i); \ 856*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 857*0e209d39SAndroid Build Coastguard Worker 858*0e209d39SAndroid Build Coastguard Worker /** 859*0e209d39SAndroid Build Coastguard Worker * Adjust a random-access offset to a code point boundary after a code point. 860*0e209d39SAndroid Build Coastguard Worker * If the offset is behind a partial multi-byte sequence, 861*0e209d39SAndroid Build Coastguard Worker * then the offset is incremented to behind the whole sequence. 862*0e209d39SAndroid Build Coastguard Worker * Otherwise, it is not modified. 863*0e209d39SAndroid Build Coastguard Worker * The input offset may be the same as the string length. 864*0e209d39SAndroid Build Coastguard Worker * "Safe" macro, checks for illegal sequences and for string boundaries. 865*0e209d39SAndroid Build Coastguard Worker * 866*0e209d39SAndroid Build Coastguard Worker * The length can be negative for a NUL-terminated string. 867*0e209d39SAndroid Build Coastguard Worker * 868*0e209d39SAndroid Build Coastguard Worker * @param s const uint8_t * string 869*0e209d39SAndroid Build Coastguard Worker * @param start int32_t starting string offset (usually 0) 870*0e209d39SAndroid Build Coastguard Worker * @param i int32_t string offset, must be start<=i<=length 871*0e209d39SAndroid Build Coastguard Worker * @param length int32_t string length 872*0e209d39SAndroid Build Coastguard Worker * @see U8_SET_CP_LIMIT_UNSAFE 873*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.4 874*0e209d39SAndroid Build Coastguard Worker */ 875*0e209d39SAndroid Build Coastguard Worker #define U8_SET_CP_LIMIT(s, start, i, length) UPRV_BLOCK_MACRO_BEGIN { \ 876*0e209d39SAndroid Build Coastguard Worker if((start)<(i) && ((i)<(length) || (length)<0)) { \ 877*0e209d39SAndroid Build Coastguard Worker U8_BACK_1(s, start, i); \ 878*0e209d39SAndroid Build Coastguard Worker U8_FWD_1(s, i, length); \ 879*0e209d39SAndroid Build Coastguard Worker } \ 880*0e209d39SAndroid Build Coastguard Worker } UPRV_BLOCK_MACRO_END 881*0e209d39SAndroid Build Coastguard Worker 882*0e209d39SAndroid Build Coastguard Worker #endif 883