]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/unicode/utf.h
ICU-491.11.1.tar.gz
[apple/icu.git] / icuSources / common / unicode / utf.h
CommitLineData
b75a7d8f
A
1/*
2*******************************************************************************
3*
4388f060 4* Copyright (C) 1999-2011, International Business Machines
b75a7d8f
A
5* Corporation and others. All Rights Reserved.
6*
7*******************************************************************************
8* file name: utf.h
9* encoding: US-ASCII
10* tab size: 8 (not used)
11* indentation:4
12*
13* created on: 1999sep09
14* created by: Markus W. Scherer
15*/
16
17/**
18 * \file
374ca955 19 * \brief C API: Code point macros
b75a7d8f 20 *
374ca955
A
21 * This file defines macros for checking whether a code point is
22 * a surrogate or a non-character etc.
23 *
24 * The UChar and UChar32 data types for Unicode code units and code points
4388f060 25 * are defined in umachine.h because they can be machine-dependent.
b75a7d8f 26 *
4388f060
A
27 * If U_NO_DEFAULT_INCLUDE_UTF_HEADERS is 0 then utf.h is included by utypes.h
28 * and itself includes utf8.h and utf16.h after some
29 * common definitions.
30 * If U_NO_DEFAULT_INCLUDE_UTF_HEADERS is 1 then each of these headers must be
31 * included explicitly if their definitions are used.
32 *
33 * utf8.h and utf16.h define macros for efficiently getting code points
b75a7d8f
A
34 * in and out of UTF-8/16 strings.
35 * utf16.h macros have "U16_" prefixes.
36 * utf8.h defines similar macros with "U8_" prefixes for UTF-8 string handling.
37 *
4388f060 38 * ICU mostly processes 16-bit Unicode strings.
b75a7d8f
A
39 * Most of the time, such strings are well-formed UTF-16.
40 * Single, unpaired surrogates must be handled as well, and are treated in ICU
41 * like regular code points where possible.
42 * (Pairs of surrogate code points are indistinguishable from supplementary
43 * code points encoded as pairs of supplementary code units.)
44 *
45 * In fact, almost all Unicode code points in normal text (>99%)
46 * are on the BMP (<=U+ffff) and even <=U+d7ff.
47 * ICU functions handle supplementary code points (U+10000..U+10ffff)
48 * but are optimized for the much more frequently occurring BMP code points.
49 *
4388f060
A
50 * umachine.h defines UChar to be an unsigned 16-bit integer.
51 * Where available, UChar is defined to be a char16_t
52 * or a wchar_t (if that is an unsigned 16-bit type), otherwise uint16_t.
b75a7d8f
A
53 *
54 * UChar32 is defined to be a signed 32-bit integer (int32_t), large enough for a 21-bit
55 * Unicode code point (Unicode scalar value, 0..0x10ffff).
56 * Before ICU 2.4, the definition of UChar32 was similarly platform-dependent as
57 * the definition of UChar. For details see the documentation for UChar32 itself.
58 *
4388f060 59 * utf.h defines a small number of C macros for single Unicode code points.
b75a7d8f
A
60 * These are simple checks for surrogates and non-characters.
61 * For actual Unicode character properties see uchar.h.
62 *
63 * By default, string operations must be done with error checking in case
64 * a string is not well-formed UTF-16.
65 * The macros will detect if a surrogate code unit is unpaired
66 * (lead unit without trail unit or vice versa) and just return the unit itself
67 * as the code point.
b75a7d8f 68 *
73c04bcf
A
69 * The regular "safe" macros require that the initial, passed-in string index
70 * is within bounds. They only check the index when they read more than one
71 * code unit. This is usually done with code similar to the following loop:
72 * <pre>while(i<length) {
73 * U16_NEXT(s, i, length, c);
74 * // use c
75 * }</pre>
76 *
b75a7d8f
A
77 * When it is safe to assume that text is well-formed UTF-16
78 * (does not contain single, unpaired surrogates), then one can use
79 * U16_..._UNSAFE macros.
80 * These do not check for proper code unit sequences or truncated text and may
81 * yield wrong results or even cause a crash if they are used with "malformed"
82 * text.
83 * In practice, U16_..._UNSAFE macros will produce slightly less code but
84 * should not be faster because the processing is only different when a
85 * surrogate code unit is detected, which will be rare.
86 *
87 * Similarly for UTF-8, there are "safe" macros without a suffix,
88 * and U8_..._UNSAFE versions.
89 * The performance differences are much larger here because UTF-8 provides so
90 * many opportunities for malformed sequences.
91 * The unsafe UTF-8 macros are entirely implemented inside the macro definitions
92 * and are fast, while the safe UTF-8 macros call functions for all but the
93 * trivial (ASCII) cases.
73c04bcf
A
94 * (ICU 3.6 optimizes U8_NEXT() and U8_APPEND() to handle most other common
95 * characters inline as well.)
b75a7d8f
A
96 *
97 * Unlike with UTF-16, malformed sequences cannot be expressed with distinct
98 * code point values (0..U+10ffff). They are indicated with negative values instead.
99 *
100 * For more information see the ICU User Guide Strings chapter
4388f060 101 * (http://userguide.icu-project.org/strings).
b75a7d8f
A
102 *
103 * <em>Usage:</em>
104 * ICU coding guidelines for if() statements should be followed when using these macros.
105 * Compound statements (curly braces {}) must be used for if-else-while...
106 * bodies and all macro statements should be terminated with semicolon.
107 *
374ca955 108 * @stable ICU 2.4
b75a7d8f
A
109 */
110
111#ifndef __UTF_H__
112#define __UTF_H__
113
4388f060 114#include "unicode/umachine.h"
b75a7d8f
A
115/* include the utfXX.h after the following definitions */
116
b75a7d8f
A
117/* single-code point definitions -------------------------------------------- */
118
b75a7d8f
A
119/**
120 * Is this code point a Unicode noncharacter?
121 * @param c 32-bit code point
122 * @return TRUE or FALSE
374ca955 123 * @stable ICU 2.4
b75a7d8f
A
124 */
125#define U_IS_UNICODE_NONCHAR(c) \
126 ((c)>=0xfdd0 && \
127 ((uint32_t)(c)<=0xfdef || ((c)&0xfffe)==0xfffe) && \
128 (uint32_t)(c)<=0x10ffff)
129
130/**
131 * Is c a Unicode code point value (0..U+10ffff)
132 * that can be assigned a character?
133 *
134 * Code points that are not characters include:
135 * - single surrogate code points (U+d800..U+dfff, 2048 code points)
136 * - the last two code points on each plane (U+__fffe and U+__ffff, 34 code points)
137 * - U+fdd0..U+fdef (new with Unicode 3.1, 32 code points)
138 * - the highest Unicode code point value is U+10ffff
139 *
140 * This means that all code points below U+d800 are character code points,
141 * and that boundary is tested first for performance.
142 *
143 * @param c 32-bit code point
144 * @return TRUE or FALSE
374ca955 145 * @stable ICU 2.4
b75a7d8f
A
146 */
147#define U_IS_UNICODE_CHAR(c) \
148 ((uint32_t)(c)<0xd800 || \
149 ((uint32_t)(c)>0xdfff && \
150 (uint32_t)(c)<=0x10ffff && \
151 !U_IS_UNICODE_NONCHAR(c)))
152
374ca955
A
153/**
154 * Is this code point a BMP code point (U+0000..U+ffff)?
155 * @param c 32-bit code point
156 * @return TRUE or FALSE
73c04bcf 157 * @stable ICU 2.8
374ca955
A
158 */
159#define U_IS_BMP(c) ((uint32_t)(c)<=0xffff)
160
161/**
162 * Is this code point a supplementary code point (U+10000..U+10ffff)?
163 * @param c 32-bit code point
164 * @return TRUE or FALSE
73c04bcf 165 * @stable ICU 2.8
374ca955
A
166 */
167#define U_IS_SUPPLEMENTARY(c) ((uint32_t)((c)-0x10000)<=0xfffff)
374ca955 168
b75a7d8f
A
169/**
170 * Is this code point a lead surrogate (U+d800..U+dbff)?
171 * @param c 32-bit code point
172 * @return TRUE or FALSE
374ca955 173 * @stable ICU 2.4
b75a7d8f
A
174 */
175#define U_IS_LEAD(c) (((c)&0xfffffc00)==0xd800)
176
177/**
178 * Is this code point a trail surrogate (U+dc00..U+dfff)?
179 * @param c 32-bit code point
180 * @return TRUE or FALSE
374ca955 181 * @stable ICU 2.4
b75a7d8f
A
182 */
183#define U_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00)
184
185/**
186 * Is this code point a surrogate (U+d800..U+dfff)?
187 * @param c 32-bit code point
188 * @return TRUE or FALSE
374ca955 189 * @stable ICU 2.4
b75a7d8f
A
190 */
191#define U_IS_SURROGATE(c) (((c)&0xfffff800)==0xd800)
192
193/**
194 * Assuming c is a surrogate code point (U_IS_SURROGATE(c)),
195 * is it a lead surrogate?
196 * @param c 32-bit code point
197 * @return TRUE or FALSE
374ca955 198 * @stable ICU 2.4
b75a7d8f
A
199 */
200#define U_IS_SURROGATE_LEAD(c) (((c)&0x400)==0)
201
729e4ab9
A
202/**
203 * Assuming c is a surrogate code point (U_IS_SURROGATE(c)),
204 * is it a trail surrogate?
205 * @param c 32-bit code point
206 * @return TRUE or FALSE
207 * @stable ICU 4.2
208 */
209#define U_IS_SURROGATE_TRAIL(c) (((c)&0x400)!=0)
210
b75a7d8f
A
211/* include the utfXX.h ------------------------------------------------------ */
212
4388f060
A
213#if !U_NO_DEFAULT_INCLUDE_UTF_HEADERS
214
b75a7d8f
A
215#include "unicode/utf8.h"
216#include "unicode/utf16.h"
217
218/* utf_old.h contains deprecated, pre-ICU 2.4 definitions */
219#include "unicode/utf_old.h"
220
4388f060
A
221#endif /* !U_NO_DEFAULT_INCLUDE_UTF_HEADERS */
222
223#endif /* __UTF_H__ */