annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/include/unicode/utf8.h @ 69:33d812a61356

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