intl/icu/source/common/unicode/utf8.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial