sl@0: /* sl@0: ******************************************************************************* sl@0: * sl@0: * Copyright (C) 1999-2005, International Business Machines sl@0: * Corporation and others. All Rights Reserved. sl@0: * sl@0: ******************************************************************************* sl@0: * file name: utf.h sl@0: * encoding: US-ASCII sl@0: * tab size: 8 (not used) sl@0: * indentation:4 sl@0: * sl@0: * created on: 1999sep09 sl@0: * created by: Markus W. Scherer sl@0: */ sl@0: sl@0: /** sl@0: * \file sl@0: * \brief C API: Code point macros sl@0: * sl@0: * This file defines macros for checking whether a code point is sl@0: * a surrogate or a non-character etc. sl@0: * sl@0: * The UChar and UChar32 data types for Unicode code units and code points sl@0: * are defined in umachines.h because they can be machine-dependent. sl@0: * sl@0: * utf.h is included by utypes.h and itself includes utf8.h and utf16.h after some sl@0: * common definitions. Those files define macros for efficiently getting code points sl@0: * in and out of UTF-8/16 strings. sl@0: * utf16.h macros have "U16_" prefixes. sl@0: * utf8.h defines similar macros with "U8_" prefixes for UTF-8 string handling. sl@0: * sl@0: * ICU processes 16-bit Unicode strings. sl@0: * Most of the time, such strings are well-formed UTF-16. sl@0: * Single, unpaired surrogates must be handled as well, and are treated in ICU sl@0: * like regular code points where possible. sl@0: * (Pairs of surrogate code points are indistinguishable from supplementary sl@0: * code points encoded as pairs of supplementary code units.) sl@0: * sl@0: * In fact, almost all Unicode code points in normal text (>99%) sl@0: * are on the BMP (<=U+ffff) and even <=U+d7ff. sl@0: * ICU functions handle supplementary code points (U+10000..U+10ffff) sl@0: * but are optimized for the much more frequently occurring BMP code points. sl@0: * sl@0: * utf.h defines UChar to be an unsigned 16-bit integer. If this matches wchar_t, then sl@0: * UChar is defined to be exactly wchar_t, otherwise uint16_t. sl@0: * sl@0: * UChar32 is defined to be a signed 32-bit integer (int32_t), large enough for a 21-bit sl@0: * Unicode code point (Unicode scalar value, 0..0x10ffff). sl@0: * Before ICU 2.4, the definition of UChar32 was similarly platform-dependent as sl@0: * the definition of UChar. For details see the documentation for UChar32 itself. sl@0: * sl@0: * utf.h also defines a small number of C macros for single Unicode code points. sl@0: * These are simple checks for surrogates and non-characters. sl@0: * For actual Unicode character properties see uchar.h. sl@0: * sl@0: * By default, string operations must be done with error checking in case sl@0: * a string is not well-formed UTF-16. sl@0: * The macros will detect if a surrogate code unit is unpaired sl@0: * (lead unit without trail unit or vice versa) and just return the unit itself sl@0: * as the code point. sl@0: * (It is an accidental property of Unicode and UTF-16 that all sl@0: * malformed sequences can be expressed unambiguously with a distinct subrange sl@0: * of Unicode code points.) sl@0: * sl@0: * When it is safe to assume that text is well-formed UTF-16 sl@0: * (does not contain single, unpaired surrogates), then one can use sl@0: * U16_..._UNSAFE macros. sl@0: * These do not check for proper code unit sequences or truncated text and may sl@0: * yield wrong results or even cause a crash if they are used with "malformed" sl@0: * text. sl@0: * In practice, U16_..._UNSAFE macros will produce slightly less code but sl@0: * should not be faster because the processing is only different when a sl@0: * surrogate code unit is detected, which will be rare. sl@0: * sl@0: * Similarly for UTF-8, there are "safe" macros without a suffix, sl@0: * and U8_..._UNSAFE versions. sl@0: * The performance differences are much larger here because UTF-8 provides so sl@0: * many opportunities for malformed sequences. sl@0: * The unsafe UTF-8 macros are entirely implemented inside the macro definitions sl@0: * and are fast, while the safe UTF-8 macros call functions for all but the sl@0: * trivial (ASCII) cases. sl@0: * sl@0: * Unlike with UTF-16, malformed sequences cannot be expressed with distinct sl@0: * code point values (0..U+10ffff). They are indicated with negative values instead. sl@0: * sl@0: * For more information see the ICU User Guide Strings chapter sl@0: * (http://icu.sourceforge.net/userguide/strings.html). sl@0: * sl@0: * Usage: sl@0: * ICU coding guidelines for if() statements should be followed when using these macros. sl@0: * Compound statements (curly braces {}) must be used for if-else-while... sl@0: * bodies and all macro statements should be terminated with semicolon. sl@0: * sl@0: * @stable ICU 2.4 sl@0: */ sl@0: sl@0: #ifndef __UTF_H__ sl@0: #define __UTF_H__ sl@0: sl@0: #include "unicode/utypes.h" sl@0: /* include the utfXX.h after the following definitions */ sl@0: sl@0: /* single-code point definitions -------------------------------------------- */ sl@0: sl@0: /** sl@0: * This value is intended for sentinel values for APIs that sl@0: * (take or) return single code points (UChar32). sl@0: * It is outside of the Unicode code point range 0..0x10ffff. sl@0: * sl@0: * For example, a "done" or "error" value in a new API sl@0: * could be indicated with U_SENTINEL. sl@0: * sl@0: * ICU APIs designed before ICU 2.4 usually define service-specific "done" sl@0: * values, mostly 0xffff. sl@0: * Those may need to be distinguished from sl@0: * actual U+ffff text contents by calling functions like sl@0: * CharacterIterator::hasNext() or UnicodeString::length(). sl@0: * sl@0: * @return -1 sl@0: * @see UChar32 sl@0: * @stable ICU 2.4 sl@0: */ sl@0: #define U_SENTINEL (-1) sl@0: sl@0: /** sl@0: * Is this code point a Unicode noncharacter? sl@0: * @param c 32-bit code point sl@0: * @return TRUE or FALSE sl@0: * @stable ICU 2.4 sl@0: */ sl@0: #define U_IS_UNICODE_NONCHAR(c) \ sl@0: ((c)>=0xfdd0 && \ sl@0: ((uint32_t)(c)<=0xfdef || ((c)&0xfffe)==0xfffe) && \ sl@0: (uint32_t)(c)<=0x10ffff) sl@0: sl@0: /** sl@0: * Is c a Unicode code point value (0..U+10ffff) sl@0: * that can be assigned a character? sl@0: * sl@0: * Code points that are not characters include: sl@0: * - single surrogate code points (U+d800..U+dfff, 2048 code points) sl@0: * - the last two code points on each plane (U+__fffe and U+__ffff, 34 code points) sl@0: * - U+fdd0..U+fdef (new with Unicode 3.1, 32 code points) sl@0: * - the highest Unicode code point value is U+10ffff sl@0: * sl@0: * This means that all code points below U+d800 are character code points, sl@0: * and that boundary is tested first for performance. sl@0: * sl@0: * @param c 32-bit code point sl@0: * @return TRUE or FALSE sl@0: * @stable ICU 2.4 sl@0: */ sl@0: #define U_IS_UNICODE_CHAR(c) \ sl@0: ((uint32_t)(c)<0xd800 || \ sl@0: ((uint32_t)(c)>0xdfff && \ sl@0: (uint32_t)(c)<=0x10ffff && \ sl@0: !U_IS_UNICODE_NONCHAR(c))) sl@0: sl@0: #ifndef U_HIDE_DRAFT_API sl@0: sl@0: /** sl@0: * Is this code point a BMP code point (U+0000..U+ffff)? sl@0: * @param c 32-bit code point sl@0: * @return TRUE or FALSE sl@0: * @stable ICU 2.8 sl@0: */ sl@0: #define U_IS_BMP(c) ((uint32_t)(c)<=0xffff) sl@0: sl@0: /** sl@0: * Is this code point a supplementary code point (U+10000..U+10ffff)? sl@0: * @param c 32-bit code point sl@0: * @return TRUE or FALSE sl@0: * @stable ICU 2.8 sl@0: */ sl@0: #define U_IS_SUPPLEMENTARY(c) ((uint32_t)((c)-0x10000)<=0xfffff) sl@0: sl@0: #endif /*U_HIDE_DRAFT_API*/ sl@0: sl@0: /** sl@0: * Is this code point a lead surrogate (U+d800..U+dbff)? sl@0: * @param c 32-bit code point sl@0: * @return TRUE or FALSE sl@0: * @stable ICU 2.4 sl@0: */ sl@0: #define U_IS_LEAD(c) (((c)&0xfffffc00)==0xd800) sl@0: sl@0: /** sl@0: * Is this code point a trail surrogate (U+dc00..U+dfff)? sl@0: * @param c 32-bit code point sl@0: * @return TRUE or FALSE sl@0: * @stable ICU 2.4 sl@0: */ sl@0: #define U_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00) sl@0: sl@0: /** sl@0: * Is this code point a surrogate (U+d800..U+dfff)? sl@0: * @param c 32-bit code point sl@0: * @return TRUE or FALSE sl@0: * @stable ICU 2.4 sl@0: */ sl@0: #define U_IS_SURROGATE(c) (((c)&0xfffff800)==0xd800) sl@0: sl@0: /** sl@0: * Assuming c is a surrogate code point (U_IS_SURROGATE(c)), sl@0: * is it a lead surrogate? sl@0: * @param c 32-bit code point sl@0: * @return TRUE or FALSE sl@0: * @stable ICU 2.4 sl@0: */ sl@0: #define U_IS_SURROGATE_LEAD(c) (((c)&0x400)==0) sl@0: sl@0: /* include the utfXX.h ------------------------------------------------------ */ sl@0: sl@0: #include "unicode/utf8.h" sl@0: #include "unicode/utf16.h" sl@0: sl@0: /* utf_old.h contains deprecated, pre-ICU 2.4 definitions */ sl@0: #include "unicode/utf_old.h" sl@0: sl@0: #endif