]>
git.saurik.com Git - apple/javascriptcore.git/blob - icu/unicode/utf.h
2 *******************************************************************************
4 * Copyright (C) 1999-2004, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
10 * tab size: 8 (not used)
13 * created on: 1999sep09
14 * created by: Markus W. Scherer
19 * \brief C API: Code point macros
21 * This file defines macros for checking whether a code point is
22 * a surrogate or a non-character etc.
24 * The UChar and UChar32 data types for Unicode code units and code points
25 * are defined in umachines.h because they can be machine-dependent.
27 * utf.h is included by utypes.h and itself includes utf8.h and utf16.h after some
28 * common definitions. Those files define macros for efficiently getting code points
29 * in and out of UTF-8/16 strings.
30 * utf16.h macros have "U16_" prefixes.
31 * utf8.h defines similar macros with "U8_" prefixes for UTF-8 string handling.
33 * ICU processes 16-bit Unicode strings.
34 * Most of the time, such strings are well-formed UTF-16.
35 * Single, unpaired surrogates must be handled as well, and are treated in ICU
36 * like regular code points where possible.
37 * (Pairs of surrogate code points are indistinguishable from supplementary
38 * code points encoded as pairs of supplementary code units.)
40 * In fact, almost all Unicode code points in normal text (>99%)
41 * are on the BMP (<=U+ffff) and even <=U+d7ff.
42 * ICU functions handle supplementary code points (U+10000..U+10ffff)
43 * but are optimized for the much more frequently occurring BMP code points.
45 * utf.h defines UChar to be an unsigned 16-bit integer. If this matches wchar_t, then
46 * UChar is defined to be exactly wchar_t, otherwise uint16_t.
48 * UChar32 is defined to be a signed 32-bit integer (int32_t), large enough for a 21-bit
49 * Unicode code point (Unicode scalar value, 0..0x10ffff).
50 * Before ICU 2.4, the definition of UChar32 was similarly platform-dependent as
51 * the definition of UChar. For details see the documentation for UChar32 itself.
53 * utf.h also defines a small number of C macros for single Unicode code points.
54 * These are simple checks for surrogates and non-characters.
55 * For actual Unicode character properties see uchar.h.
57 * By default, string operations must be done with error checking in case
58 * a string is not well-formed UTF-16.
59 * The macros will detect if a surrogate code unit is unpaired
60 * (lead unit without trail unit or vice versa) and just return the unit itself
62 * (It is an accidental property of Unicode and UTF-16 that all
63 * malformed sequences can be expressed unambiguously with a distinct subrange
64 * of Unicode code points.)
66 * When it is safe to assume that text is well-formed UTF-16
67 * (does not contain single, unpaired surrogates), then one can use
68 * U16_..._UNSAFE macros.
69 * These do not check for proper code unit sequences or truncated text and may
70 * yield wrong results or even cause a crash if they are used with "malformed"
72 * In practice, U16_..._UNSAFE macros will produce slightly less code but
73 * should not be faster because the processing is only different when a
74 * surrogate code unit is detected, which will be rare.
76 * Similarly for UTF-8, there are "safe" macros without a suffix,
77 * and U8_..._UNSAFE versions.
78 * The performance differences are much larger here because UTF-8 provides so
79 * many opportunities for malformed sequences.
80 * The unsafe UTF-8 macros are entirely implemented inside the macro definitions
81 * and are fast, while the safe UTF-8 macros call functions for all but the
82 * trivial (ASCII) cases.
84 * Unlike with UTF-16, malformed sequences cannot be expressed with distinct
85 * code point values (0..U+10ffff). They are indicated with negative values instead.
87 * For more information see the ICU User Guide Strings chapter
88 * (http://oss.software.ibm.com/icu/userguide/).
91 * ICU coding guidelines for if() statements should be followed when using these macros.
92 * Compound statements (curly braces {}) must be used for if-else-while...
93 * bodies and all macro statements should be terminated with semicolon.
101 #include "unicode/utypes.h"
102 /* include the utfXX.h after the following definitions */
104 /* single-code point definitions -------------------------------------------- */
107 * This value is intended for sentinel values for APIs that
108 * (take or) return single code points (UChar32).
109 * It is outside of the Unicode code point range 0..0x10ffff.
111 * For example, a "done" or "error" value in a new API
112 * could be indicated with U_SENTINEL.
114 * ICU APIs designed before ICU 2.4 usually define service-specific "done"
115 * values, mostly 0xffff.
116 * Those may need to be distinguished from
117 * actual U+ffff text contents by calling functions like
118 * CharacterIterator::hasNext() or UnicodeString::length().
124 #define U_SENTINEL (-1)
127 * Is this code point a Unicode noncharacter?
128 * @param c 32-bit code point
129 * @return TRUE or FALSE
132 #define U_IS_UNICODE_NONCHAR(c) \
134 ((uint32_t)(c)<=0xfdef || ((c)&0xfffe)==0xfffe) && \
135 (uint32_t)(c)<=0x10ffff)
138 * Is c a Unicode code point value (0..U+10ffff)
139 * that can be assigned a character?
141 * Code points that are not characters include:
142 * - single surrogate code points (U+d800..U+dfff, 2048 code points)
143 * - the last two code points on each plane (U+__fffe and U+__ffff, 34 code points)
144 * - U+fdd0..U+fdef (new with Unicode 3.1, 32 code points)
145 * - the highest Unicode code point value is U+10ffff
147 * This means that all code points below U+d800 are character code points,
148 * and that boundary is tested first for performance.
150 * @param c 32-bit code point
151 * @return TRUE or FALSE
154 #define U_IS_UNICODE_CHAR(c) \
155 ((uint32_t)(c)<0xd800 || \
156 ((uint32_t)(c)>0xdfff && \
157 (uint32_t)(c)<=0x10ffff && \
158 !U_IS_UNICODE_NONCHAR(c)))
160 #ifndef U_HIDE_DRAFT_API
163 * Is this code point a BMP code point (U+0000..U+ffff)?
164 * @param c 32-bit code point
165 * @return TRUE or FALSE
168 #define U_IS_BMP(c) ((uint32_t)(c)<=0xffff)
171 * Is this code point a supplementary code point (U+10000..U+10ffff)?
172 * @param c 32-bit code point
173 * @return TRUE or FALSE
176 #define U_IS_SUPPLEMENTARY(c) ((uint32_t)((c)-0x10000)<=0xfffff)
178 #endif /*U_HIDE_DRAFT_API*/
181 * Is this code point a lead surrogate (U+d800..U+dbff)?
182 * @param c 32-bit code point
183 * @return TRUE or FALSE
186 #define U_IS_LEAD(c) (((c)&0xfffffc00)==0xd800)
189 * Is this code point a trail surrogate (U+dc00..U+dfff)?
190 * @param c 32-bit code point
191 * @return TRUE or FALSE
194 #define U_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00)
197 * Is this code point a surrogate (U+d800..U+dfff)?
198 * @param c 32-bit code point
199 * @return TRUE or FALSE
202 #define U_IS_SURROGATE(c) (((c)&0xfffff800)==0xd800)
205 * Assuming c is a surrogate code point (U_IS_SURROGATE(c)),
206 * is it a lead surrogate?
207 * @param c 32-bit code point
208 * @return TRUE or FALSE
211 #define U_IS_SURROGATE_LEAD(c) (((c)&0x400)==0)
213 /* include the utfXX.h ------------------------------------------------------ */
215 #include "unicode/utf8.h"
216 #include "unicode/utf16.h"
218 /* utf_old.h contains deprecated, pre-ICU 2.4 definitions */
219 #include "unicode/utf_old.h"