]>
git.saurik.com Git - apple/javascriptcore.git/blob - icu/unicode/utf.h
2 *******************************************************************************
4 * Copyright (C) 1999-2010, 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 * The regular "safe" macros require that the initial, passed-in string index
67 * is within bounds. They only check the index when they read more than one
68 * code unit. This is usually done with code similar to the following loop:
69 * <pre>while(i<length) {
70 * U16_NEXT(s, i, length, c);
74 * When it is safe to assume that text is well-formed UTF-16
75 * (does not contain single, unpaired surrogates), then one can use
76 * U16_..._UNSAFE macros.
77 * These do not check for proper code unit sequences or truncated text and may
78 * yield wrong results or even cause a crash if they are used with "malformed"
80 * In practice, U16_..._UNSAFE macros will produce slightly less code but
81 * should not be faster because the processing is only different when a
82 * surrogate code unit is detected, which will be rare.
84 * Similarly for UTF-8, there are "safe" macros without a suffix,
85 * and U8_..._UNSAFE versions.
86 * The performance differences are much larger here because UTF-8 provides so
87 * many opportunities for malformed sequences.
88 * The unsafe UTF-8 macros are entirely implemented inside the macro definitions
89 * and are fast, while the safe UTF-8 macros call functions for all but the
90 * trivial (ASCII) cases.
91 * (ICU 3.6 optimizes U8_NEXT() and U8_APPEND() to handle most other common
92 * characters inline as well.)
94 * Unlike with UTF-16, malformed sequences cannot be expressed with distinct
95 * code point values (0..U+10ffff). They are indicated with negative values instead.
97 * For more information see the ICU User Guide Strings chapter
98 * (http://icu-project.org/userguide/strings.html).
101 * ICU coding guidelines for if() statements should be followed when using these macros.
102 * Compound statements (curly braces {}) must be used for if-else-while...
103 * bodies and all macro statements should be terminated with semicolon.
111 #include "unicode/utypes.h"
112 /* include the utfXX.h after the following definitions */
114 /* single-code point definitions -------------------------------------------- */
117 * This value is intended for sentinel values for APIs that
118 * (take or) return single code points (UChar32).
119 * It is outside of the Unicode code point range 0..0x10ffff.
121 * For example, a "done" or "error" value in a new API
122 * could be indicated with U_SENTINEL.
124 * ICU APIs designed before ICU 2.4 usually define service-specific "done"
125 * values, mostly 0xffff.
126 * Those may need to be distinguished from
127 * actual U+ffff text contents by calling functions like
128 * CharacterIterator::hasNext() or UnicodeString::length().
134 #define U_SENTINEL (-1)
137 * Is this code point a Unicode noncharacter?
138 * @param c 32-bit code point
139 * @return TRUE or FALSE
142 #define U_IS_UNICODE_NONCHAR(c) \
144 ((uint32_t)(c)<=0xfdef || ((c)&0xfffe)==0xfffe) && \
145 (uint32_t)(c)<=0x10ffff)
148 * Is c a Unicode code point value (0..U+10ffff)
149 * that can be assigned a character?
151 * Code points that are not characters include:
152 * - single surrogate code points (U+d800..U+dfff, 2048 code points)
153 * - the last two code points on each plane (U+__fffe and U+__ffff, 34 code points)
154 * - U+fdd0..U+fdef (new with Unicode 3.1, 32 code points)
155 * - the highest Unicode code point value is U+10ffff
157 * This means that all code points below U+d800 are character code points,
158 * and that boundary is tested first for performance.
160 * @param c 32-bit code point
161 * @return TRUE or FALSE
164 #define U_IS_UNICODE_CHAR(c) \
165 ((uint32_t)(c)<0xd800 || \
166 ((uint32_t)(c)>0xdfff && \
167 (uint32_t)(c)<=0x10ffff && \
168 !U_IS_UNICODE_NONCHAR(c)))
171 * Is this code point a BMP code point (U+0000..U+ffff)?
172 * @param c 32-bit code point
173 * @return TRUE or FALSE
176 #define U_IS_BMP(c) ((uint32_t)(c)<=0xffff)
179 * Is this code point a supplementary code point (U+10000..U+10ffff)?
180 * @param c 32-bit code point
181 * @return TRUE or FALSE
184 #define U_IS_SUPPLEMENTARY(c) ((uint32_t)((c)-0x10000)<=0xfffff)
187 * Is this code point a lead surrogate (U+d800..U+dbff)?
188 * @param c 32-bit code point
189 * @return TRUE or FALSE
192 #define U_IS_LEAD(c) (((c)&0xfffffc00)==0xd800)
195 * Is this code point a trail surrogate (U+dc00..U+dfff)?
196 * @param c 32-bit code point
197 * @return TRUE or FALSE
200 #define U_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00)
203 * Is this code point a surrogate (U+d800..U+dfff)?
204 * @param c 32-bit code point
205 * @return TRUE or FALSE
208 #define U_IS_SURROGATE(c) (((c)&0xfffff800)==0xd800)
211 * Assuming c is a surrogate code point (U_IS_SURROGATE(c)),
212 * is it a lead surrogate?
213 * @param c 32-bit code point
214 * @return TRUE or FALSE
217 #define U_IS_SURROGATE_LEAD(c) (((c)&0x400)==0)
220 * Assuming c is a surrogate code point (U_IS_SURROGATE(c)),
221 * is it a trail surrogate?
222 * @param c 32-bit code point
223 * @return TRUE or FALSE
226 #define U_IS_SURROGATE_TRAIL(c) (((c)&0x400)!=0)
228 /* include the utfXX.h ------------------------------------------------------ */
230 #include "unicode/utf8.h"
231 #include "unicode/utf16.h"
233 /* utf_old.h contains deprecated, pre-ICU 2.4 definitions */
234 #include "unicode/utf_old.h"