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