jpayne@69: // © 2016 and later: Unicode, Inc. and others. jpayne@69: // License & terms of use: http://www.unicode.org/copyright.html jpayne@69: /* jpayne@69: ******************************************************************************* jpayne@69: * jpayne@69: * Copyright (C) 1999-2011, International Business Machines jpayne@69: * Corporation and others. All Rights Reserved. jpayne@69: * jpayne@69: ******************************************************************************* jpayne@69: * file name: utf.h jpayne@69: * encoding: UTF-8 jpayne@69: * tab size: 8 (not used) jpayne@69: * indentation:4 jpayne@69: * jpayne@69: * created on: 1999sep09 jpayne@69: * created by: Markus W. Scherer jpayne@69: */ jpayne@69: jpayne@69: /** jpayne@69: * \file jpayne@69: * \brief C API: Code point macros jpayne@69: * jpayne@69: * This file defines macros for checking whether a code point is jpayne@69: * a surrogate or a non-character etc. jpayne@69: * jpayne@69: * If U_NO_DEFAULT_INCLUDE_UTF_HEADERS is 0 then utf.h is included by utypes.h jpayne@69: * and itself includes utf8.h and utf16.h after some jpayne@69: * common definitions. jpayne@69: * If U_NO_DEFAULT_INCLUDE_UTF_HEADERS is 1 then each of these headers must be jpayne@69: * included explicitly if their definitions are used. jpayne@69: * jpayne@69: * utf8.h and utf16.h define macros for efficiently getting code points jpayne@69: * in and out of UTF-8/16 strings. jpayne@69: * utf16.h macros have "U16_" prefixes. jpayne@69: * utf8.h defines similar macros with "U8_" prefixes for UTF-8 string handling. jpayne@69: * jpayne@69: * ICU mostly processes 16-bit Unicode strings. jpayne@69: * Most of the time, such strings are well-formed UTF-16. jpayne@69: * Single, unpaired surrogates must be handled as well, and are treated in ICU jpayne@69: * like regular code points where possible. jpayne@69: * (Pairs of surrogate code points are indistinguishable from supplementary jpayne@69: * code points encoded as pairs of supplementary code units.) jpayne@69: * jpayne@69: * In fact, almost all Unicode code points in normal text (>99%) jpayne@69: * are on the BMP (<=U+ffff) and even <=U+d7ff. jpayne@69: * ICU functions handle supplementary code points (U+10000..U+10ffff) jpayne@69: * but are optimized for the much more frequently occurring BMP code points. jpayne@69: * jpayne@69: * umachine.h defines UChar to be an unsigned 16-bit integer. jpayne@69: * Since ICU 59, ICU uses char16_t in C++, UChar only in C, jpayne@69: * and defines UChar=char16_t by default. See the UChar API docs for details. jpayne@69: * jpayne@69: * UChar32 is defined to be a signed 32-bit integer (int32_t), large enough for a 21-bit jpayne@69: * Unicode code point (Unicode scalar value, 0..0x10ffff) and U_SENTINEL (-1). jpayne@69: * Before ICU 2.4, the definition of UChar32 was similarly platform-dependent as jpayne@69: * the definition of UChar. For details see the documentation for UChar32 itself. jpayne@69: * jpayne@69: * utf.h defines a small number of C macros for single Unicode code points. jpayne@69: * These are simple checks for surrogates and non-characters. jpayne@69: * For actual Unicode character properties see uchar.h. jpayne@69: * jpayne@69: * By default, string operations must be done with error checking in case jpayne@69: * a string is not well-formed UTF-16 or UTF-8. jpayne@69: * jpayne@69: * The U16_ macros detect if a surrogate code unit is unpaired jpayne@69: * (lead unit without trail unit or vice versa) and just return the unit itself jpayne@69: * as the code point. jpayne@69: * jpayne@69: * The U8_ macros detect illegal byte sequences and return a negative value. jpayne@69: * Starting with ICU 60, the observable length of a single illegal byte sequence jpayne@69: * skipped by one of these macros follows the Unicode 6+ recommendation jpayne@69: * which is consistent with the W3C Encoding Standard. jpayne@69: * jpayne@69: * There are ..._OR_FFFD versions of both U16_ and U8_ macros jpayne@69: * that return U+FFFD for illegal code unit sequences. jpayne@69: * jpayne@69: * The regular "safe" macros require that the initial, passed-in string index jpayne@69: * is within bounds. They only check the index when they read more than one jpayne@69: * code unit. This is usually done with code similar to the following loop: jpayne@69: *
while(ijpayne@69: * jpayne@69: * When it is safe to assume that text is well-formed UTF-16 jpayne@69: * (does not contain single, unpaired surrogates), then one can use jpayne@69: * U16_..._UNSAFE macros. jpayne@69: * These do not check for proper code unit sequences or truncated text and may jpayne@69: * yield wrong results or even cause a crash if they are used with "malformed" jpayne@69: * text. jpayne@69: * In practice, U16_..._UNSAFE macros will produce slightly less code but jpayne@69: * should not be faster because the processing is only different when a jpayne@69: * surrogate code unit is detected, which will be rare. jpayne@69: * jpayne@69: * Similarly for UTF-8, there are "safe" macros without a suffix, jpayne@69: * and U8_..._UNSAFE versions. jpayne@69: * The performance differences are much larger here because UTF-8 provides so jpayne@69: * many opportunities for malformed sequences. jpayne@69: * The unsafe UTF-8 macros are entirely implemented inside the macro definitions jpayne@69: * and are fast, while the safe UTF-8 macros call functions for some complicated cases. jpayne@69: * jpayne@69: * Unlike with UTF-16, malformed sequences cannot be expressed with distinct jpayne@69: * code point values (0..U+10ffff). They are indicated with negative values instead. jpayne@69: * jpayne@69: * For more information see the ICU User Guide Strings chapter jpayne@69: * (http://userguide.icu-project.org/strings). jpayne@69: * jpayne@69: * Usage: jpayne@69: * ICU coding guidelines for if() statements should be followed when using these macros. jpayne@69: * Compound statements (curly braces {}) must be used for if-else-while... jpayne@69: * bodies and all macro statements should be terminated with semicolon. jpayne@69: * jpayne@69: * @stable ICU 2.4 jpayne@69: */ jpayne@69: jpayne@69: #ifndef __UTF_H__ jpayne@69: #define __UTF_H__ jpayne@69: jpayne@69: #include "unicode/umachine.h" jpayne@69: /* include the utfXX.h after the following definitions */ jpayne@69: jpayne@69: /* single-code point definitions -------------------------------------------- */ jpayne@69: jpayne@69: /** jpayne@69: * Is this code point a Unicode noncharacter? jpayne@69: * @param c 32-bit code point jpayne@69: * @return TRUE or FALSE jpayne@69: * @stable ICU 2.4 jpayne@69: */ jpayne@69: #define U_IS_UNICODE_NONCHAR(c) \ jpayne@69: ((c)>=0xfdd0 && \ jpayne@69: ((c)<=0xfdef || ((c)&0xfffe)==0xfffe) && (c)<=0x10ffff) jpayne@69: jpayne@69: /** jpayne@69: * Is c a Unicode code point value (0..U+10ffff) jpayne@69: * that can be assigned a character? jpayne@69: * jpayne@69: * Code points that are not characters include: jpayne@69: * - single surrogate code points (U+d800..U+dfff, 2048 code points) jpayne@69: * - the last two code points on each plane (U+__fffe and U+__ffff, 34 code points) jpayne@69: * - U+fdd0..U+fdef (new with Unicode 3.1, 32 code points) jpayne@69: * - the highest Unicode code point value is U+10ffff jpayne@69: * jpayne@69: * This means that all code points below U+d800 are character code points, jpayne@69: * and that boundary is tested first for performance. jpayne@69: * jpayne@69: * @param c 32-bit code point jpayne@69: * @return TRUE or FALSE jpayne@69: * @stable ICU 2.4 jpayne@69: */ jpayne@69: #define U_IS_UNICODE_CHAR(c) \ jpayne@69: ((uint32_t)(c)<0xd800 || \ jpayne@69: (0xdfff<(c) && (c)<=0x10ffff && !U_IS_UNICODE_NONCHAR(c))) jpayne@69: jpayne@69: /** jpayne@69: * Is this code point a BMP code point (U+0000..U+ffff)? jpayne@69: * @param c 32-bit code point jpayne@69: * @return TRUE or FALSE jpayne@69: * @stable ICU 2.8 jpayne@69: */ jpayne@69: #define U_IS_BMP(c) ((uint32_t)(c)<=0xffff) jpayne@69: jpayne@69: /** jpayne@69: * Is this code point a supplementary code point (U+10000..U+10ffff)? jpayne@69: * @param c 32-bit code point jpayne@69: * @return TRUE or FALSE jpayne@69: * @stable ICU 2.8 jpayne@69: */ jpayne@69: #define U_IS_SUPPLEMENTARY(c) ((uint32_t)((c)-0x10000)<=0xfffff) jpayne@69: jpayne@69: /** jpayne@69: * Is this code point a lead surrogate (U+d800..U+dbff)? jpayne@69: * @param c 32-bit code point jpayne@69: * @return TRUE or FALSE jpayne@69: * @stable ICU 2.4 jpayne@69: */ jpayne@69: #define U_IS_LEAD(c) (((c)&0xfffffc00)==0xd800) jpayne@69: jpayne@69: /** jpayne@69: * Is this code point a trail surrogate (U+dc00..U+dfff)? jpayne@69: * @param c 32-bit code point jpayne@69: * @return TRUE or FALSE jpayne@69: * @stable ICU 2.4 jpayne@69: */ jpayne@69: #define U_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00) jpayne@69: jpayne@69: /** jpayne@69: * Is this code point a surrogate (U+d800..U+dfff)? jpayne@69: * @param c 32-bit code point jpayne@69: * @return TRUE or FALSE jpayne@69: * @stable ICU 2.4 jpayne@69: */ jpayne@69: #define U_IS_SURROGATE(c) (((c)&0xfffff800)==0xd800) jpayne@69: jpayne@69: /** jpayne@69: * Assuming c is a surrogate code point (U_IS_SURROGATE(c)), jpayne@69: * is it a lead surrogate? jpayne@69: * @param c 32-bit code point jpayne@69: * @return TRUE or FALSE jpayne@69: * @stable ICU 2.4 jpayne@69: */ jpayne@69: #define U_IS_SURROGATE_LEAD(c) (((c)&0x400)==0) jpayne@69: jpayne@69: /** jpayne@69: * Assuming c is a surrogate code point (U_IS_SURROGATE(c)), jpayne@69: * is it a trail surrogate? jpayne@69: * @param c 32-bit code point jpayne@69: * @return TRUE or FALSE jpayne@69: * @stable ICU 4.2 jpayne@69: */ jpayne@69: #define U_IS_SURROGATE_TRAIL(c) (((c)&0x400)!=0) jpayne@69: jpayne@69: /* include the utfXX.h ------------------------------------------------------ */ jpayne@69: jpayne@69: #if !U_NO_DEFAULT_INCLUDE_UTF_HEADERS jpayne@69: jpayne@69: #include "unicode/utf8.h" jpayne@69: #include "unicode/utf16.h" jpayne@69: jpayne@69: /* utf_old.h contains deprecated, pre-ICU 2.4 definitions */ jpayne@69: #include "unicode/utf_old.h" jpayne@69: jpayne@69: #endif /* !U_NO_DEFAULT_INCLUDE_UTF_HEADERS */ jpayne@69: jpayne@69: #endif /* __UTF_H__ */