/* ******************************************************************************* * * Copyright (C) 1999-2001, International Business Machines * Corporation and others. All Rights Reserved. * ******************************************************************************* * file name: utf8.h * encoding: US-ASCII * tab size: 8 (not used) * indentation:4 * * created on: 1999sep13 * created by: Markus W. Scherer */ /** * \file * \brief C API: UTF-8 macros * * This file defines macros to deal with UTF-8 code units and code points. * Signatures and semantics are the same as for the similarly named macros * in utf16.h. * utf8.h is included by utf.h after unicode/umachine.h * and some common definitions.
*Usage: ICU coding guidelines for if() statements should be followed when using these macros. * Compound statements (curly braces {}) must be used for if-else-while... * bodies and all macro statements should be terminated with semicolon.
*/ /* utf.h must be included first. */ #ifndef __UTF_H__ # include "unicode/utf.h" #endif #ifndef __UTF8_H__ #define __UTF8_H__ /* internal definitions ----------------------------------------------------- */ #ifdef U_UTF8_IMPL U_CAPI const uint8_t utf8_countTrailBytes[256]; #else U_CFUNC const uint8_t /* U_IMPORT2? */ U_IMPORT utf8_countTrailBytes[256]; #endif /* * Count the trail bytes for a lead byte - * this macro should be used so that the assembler code * that is mentioned in utf_impl.c could be used here. */ #define UTF8_COUNT_TRAIL_BYTES(leadByte) (utf8_countTrailBytes[(uint8_t)leadByte]) /* use a macro here, too - there may be a simpler way with some machines */ #define UTF8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1) U_CAPI UChar32 U_EXPORT2 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict); U_CAPI int32_t U_EXPORT2 utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c); U_CAPI UChar32 U_EXPORT2 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict); U_CAPI int32_t U_EXPORT2 utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i); /* * For the semantics of all of these macros, see utf16.h. * The UTF-8 macros favor sequences more the shorter they are. * Sometimes, only the single-byte case is covered by a macro, * while longer sequences are handled by a function call. */ /* single-code point definitions -------------------------------------------- */ /* classes of code unit values */ #define UTF8_IS_SINGLE(uchar) (((uchar)&0x80)==0) #define UTF8_IS_LEAD(uchar) ((uint8_t)((uchar)-0xc0)<0x3e) #define UTF8_IS_TRAIL(uchar) (((uchar)&0xc0)==0x80) /* number of code units per code point */ #define UTF8_NEED_MULTIPLE_UCHAR(c) ((uint32_t)(c)>0x7f) /* * ICU does not deal with code points >0x10ffff * unless necessary for advancing in the byte stream. * * These length macros take into account that for values >0x10ffff * the "safe" append macros would write the error code point 0xffff * with 3 bytes. * Code point comparisons need to be in uint32_t because UChar32 * may be a signed type, and negative values must be recognized. */ #if 1 # define UTF8_CHAR_LENGTH(c) \ ((uint32_t)(c)<=0x7f ? 1 : \ ((uint32_t)(c)<=0x7ff ? 2 : \ ((uint32_t)((c)-0x10000)>0xfffff ? 3 : 4) \ ) \ ) #else # define UTF8_CHAR_LENGTH(c) \ ((uint32_t)(c)<=0x7f ? 1 : \ ((uint32_t)(c)<=0x7ff ? 2 : \ ((uint32_t)(c)<=0xffff ? 3 : \ ((uint32_t)(c)<=0x10ffff ? 4 : \ ((uint32_t)(c)<=0x3ffffff ? 5 : \ ((uint32_t)(c)<=0x7fffffff ? 6 : 3) \ ) \ ) \ ) \ ) \ ) #endif #define UTF8_MAX_CHAR_LENGTH 4 /* average number of code units compared to UTF-16 */ #define UTF8_ARRAY_SIZE(size) ((5*(size))/2) #define UTF8_GET_CHAR_UNSAFE(s, i, c) { \ int32_t __I=(int32_t)(i); \ UTF8_SET_CHAR_START_UNSAFE(s, __I); \ UTF8_NEXT_CHAR_UNSAFE(s, __I, c); \ } #define UTF8_GET_CHAR_SAFE(s, start, i, length, c, strict) { \ int32_t __I=(int32_t)(i); \ UTF8_SET_CHAR_START_SAFE(s, start, __I); \ UTF8_NEXT_CHAR_SAFE(s, __I, length, c, strict); \ } /* definitions with forward iteration --------------------------------------- */ /* * Read a Unicode scalar value from an array of UTF-8 bytes. * Only values <=0x10ffff are accepted, and if an error occurs, * then c will be set such that UTF_IS_ERROR(c). * The _UNSAFE macro is fast and does not check for errors. * The _SAFE macro checks for errors and optionally for * irregular sequences, too, i.e., for sequences that * are longer than necessary, such as