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