|
|
|||
File indexing completed on 2025-12-15 10:31:22
0001 // © 2016 and later: Unicode, Inc. and others. 0002 // License & terms of use: http://www.unicode.org/copyright.html 0003 /* 0004 ******************************************************************************* 0005 * 0006 * Copyright (C) 1999-2011, International Business Machines 0007 * Corporation and others. All Rights Reserved. 0008 * 0009 ******************************************************************************* 0010 * file name: utf.h 0011 * encoding: UTF-8 0012 * tab size: 8 (not used) 0013 * indentation:4 0014 * 0015 * created on: 1999sep09 0016 * created by: Markus W. Scherer 0017 */ 0018 0019 /** 0020 * \file 0021 * \brief C API: Code point macros 0022 * 0023 * This file defines macros for checking whether a code point is 0024 * a surrogate or a non-character etc. 0025 * 0026 * If U_NO_DEFAULT_INCLUDE_UTF_HEADERS is 0 then utf.h is included by utypes.h 0027 * and itself includes utf8.h and utf16.h after some 0028 * common definitions. 0029 * If U_NO_DEFAULT_INCLUDE_UTF_HEADERS is 1 then each of these headers must be 0030 * included explicitly if their definitions are used. 0031 * 0032 * utf8.h and utf16.h define macros for efficiently getting code points 0033 * in and out of UTF-8/16 strings. 0034 * utf16.h macros have "U16_" prefixes. 0035 * utf8.h defines similar macros with "U8_" prefixes for UTF-8 string handling. 0036 * 0037 * ICU mostly processes 16-bit Unicode strings. 0038 * Most of the time, such strings are well-formed UTF-16. 0039 * Single, unpaired surrogates must be handled as well, and are treated in ICU 0040 * like regular code points where possible. 0041 * (Pairs of surrogate code points are indistinguishable from supplementary 0042 * code points encoded as pairs of supplementary code units.) 0043 * 0044 * In fact, almost all Unicode code points in normal text (>99%) 0045 * are on the BMP (<=U+ffff) and even <=U+d7ff. 0046 * ICU functions handle supplementary code points (U+10000..U+10ffff) 0047 * but are optimized for the much more frequently occurring BMP code points. 0048 * 0049 * umachine.h defines UChar to be an unsigned 16-bit integer. 0050 * Since ICU 59, ICU uses char16_t in C++, UChar only in C, 0051 * and defines UChar=char16_t by default. See the UChar API docs for details. 0052 * 0053 * UChar32 is defined to be a signed 32-bit integer (int32_t), large enough for a 21-bit 0054 * Unicode code point (Unicode scalar value, 0..0x10ffff) and U_SENTINEL (-1). 0055 * Before ICU 2.4, the definition of UChar32 was similarly platform-dependent as 0056 * the definition of UChar. For details see the documentation for UChar32 itself. 0057 * 0058 * utf.h defines a small number of C macros for single Unicode code points. 0059 * These are simple checks for surrogates and non-characters. 0060 * For actual Unicode character properties see uchar.h. 0061 * 0062 * By default, string operations must be done with error checking in case 0063 * a string is not well-formed UTF-16 or UTF-8. 0064 * 0065 * The U16_ macros detect if a surrogate code unit is unpaired 0066 * (lead unit without trail unit or vice versa) and just return the unit itself 0067 * as the code point. 0068 * 0069 * The U8_ macros detect illegal byte sequences and return a negative value. 0070 * Starting with ICU 60, the observable length of a single illegal byte sequence 0071 * skipped by one of these macros follows the Unicode 6+ recommendation 0072 * which is consistent with the W3C Encoding Standard. 0073 * 0074 * There are ..._OR_FFFD versions of both U16_ and U8_ macros 0075 * that return U+FFFD for illegal code unit sequences. 0076 * 0077 * The regular "safe" macros require that the initial, passed-in string index 0078 * is within bounds. They only check the index when they read more than one 0079 * code unit. This is usually done with code similar to the following loop: 0080 * <pre>while(i<length) { 0081 * U16_NEXT(s, i, length, c); 0082 * // use c 0083 * }</pre> 0084 * 0085 * When it is safe to assume that text is well-formed UTF-16 0086 * (does not contain single, unpaired surrogates), then one can use 0087 * U16_..._UNSAFE macros. 0088 * These do not check for proper code unit sequences or truncated text and may 0089 * yield wrong results or even cause a crash if they are used with "malformed" 0090 * text. 0091 * In practice, U16_..._UNSAFE macros will produce slightly less code but 0092 * should not be faster because the processing is only different when a 0093 * surrogate code unit is detected, which will be rare. 0094 * 0095 * Similarly for UTF-8, there are "safe" macros without a suffix, 0096 * and U8_..._UNSAFE versions. 0097 * The performance differences are much larger here because UTF-8 provides so 0098 * many opportunities for malformed sequences. 0099 * The unsafe UTF-8 macros are entirely implemented inside the macro definitions 0100 * and are fast, while the safe UTF-8 macros call functions for some complicated cases. 0101 * 0102 * Unlike with UTF-16, malformed sequences cannot be expressed with distinct 0103 * code point values (0..U+10ffff). They are indicated with negative values instead. 0104 * 0105 * For more information see the ICU User Guide Strings chapter 0106 * (https://unicode-org.github.io/icu/userguide/strings). 0107 * 0108 * <em>Usage:</em> 0109 * ICU coding guidelines for if() statements should be followed when using these macros. 0110 * Compound statements (curly braces {}) must be used for if-else-while... 0111 * bodies and all macro statements should be terminated with semicolon. 0112 * 0113 * @stable ICU 2.4 0114 */ 0115 0116 #ifndef __UTF_H__ 0117 #define __UTF_H__ 0118 0119 #include "unicode/umachine.h" 0120 /* include the utfXX.h after the following definitions */ 0121 0122 /* single-code point definitions -------------------------------------------- */ 0123 0124 /** 0125 * Is this code point a Unicode noncharacter? 0126 * @param c 32-bit code point 0127 * @return true or false 0128 * @stable ICU 2.4 0129 */ 0130 #define U_IS_UNICODE_NONCHAR(c) \ 0131 ((c)>=0xfdd0 && \ 0132 ((c)<=0xfdef || ((c)&0xfffe)==0xfffe) && (c)<=0x10ffff) 0133 0134 /** 0135 * Is c a Unicode code point value (0..U+10ffff) 0136 * that can be assigned a character? 0137 * 0138 * Code points that are not characters include: 0139 * - single surrogate code points (U+d800..U+dfff, 2048 code points) 0140 * - the last two code points on each plane (U+__fffe and U+__ffff, 34 code points) 0141 * - U+fdd0..U+fdef (new with Unicode 3.1, 32 code points) 0142 * - the highest Unicode code point value is U+10ffff 0143 * 0144 * This means that all code points below U+d800 are character code points, 0145 * and that boundary is tested first for performance. 0146 * 0147 * @param c 32-bit code point 0148 * @return true or false 0149 * @stable ICU 2.4 0150 */ 0151 #define U_IS_UNICODE_CHAR(c) \ 0152 ((uint32_t)(c)<0xd800 || \ 0153 (0xdfff<(c) && (c)<=0x10ffff && !U_IS_UNICODE_NONCHAR(c))) 0154 0155 /** 0156 * Is this code point a BMP code point (U+0000..U+ffff)? 0157 * @param c 32-bit code point 0158 * @return true or false 0159 * @stable ICU 2.8 0160 */ 0161 #define U_IS_BMP(c) ((uint32_t)(c)<=0xffff) 0162 0163 /** 0164 * Is this code point a supplementary code point (U+10000..U+10ffff)? 0165 * @param c 32-bit code point 0166 * @return true or false 0167 * @stable ICU 2.8 0168 */ 0169 #define U_IS_SUPPLEMENTARY(c) ((uint32_t)((c)-0x10000)<=0xfffff) 0170 0171 /** 0172 * Is this code point a lead surrogate (U+d800..U+dbff)? 0173 * @param c 32-bit code point 0174 * @return true or false 0175 * @stable ICU 2.4 0176 */ 0177 #define U_IS_LEAD(c) (((c)&0xfffffc00)==0xd800) 0178 0179 /** 0180 * Is this code point a trail surrogate (U+dc00..U+dfff)? 0181 * @param c 32-bit code point 0182 * @return true or false 0183 * @stable ICU 2.4 0184 */ 0185 #define U_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00) 0186 0187 /** 0188 * Is this code point a surrogate (U+d800..U+dfff)? 0189 * @param c 32-bit code point 0190 * @return true or false 0191 * @stable ICU 2.4 0192 */ 0193 #define U_IS_SURROGATE(c) (((c)&0xfffff800)==0xd800) 0194 0195 /** 0196 * Assuming c is a surrogate code point (U_IS_SURROGATE(c)), 0197 * is it a lead surrogate? 0198 * @param c 32-bit code point 0199 * @return true or false 0200 * @stable ICU 2.4 0201 */ 0202 #define U_IS_SURROGATE_LEAD(c) (((c)&0x400)==0) 0203 0204 /** 0205 * Assuming c is a surrogate code point (U_IS_SURROGATE(c)), 0206 * is it a trail surrogate? 0207 * @param c 32-bit code point 0208 * @return true or false 0209 * @stable ICU 4.2 0210 */ 0211 #define U_IS_SURROGATE_TRAIL(c) (((c)&0x400)!=0) 0212 0213 /* include the utfXX.h ------------------------------------------------------ */ 0214 0215 #if !U_NO_DEFAULT_INCLUDE_UTF_HEADERS 0216 0217 #include "unicode/utf8.h" 0218 #include "unicode/utf16.h" 0219 0220 /* utf_old.h contains deprecated, pre-ICU 2.4 definitions */ 0221 #include "unicode/utf_old.h" 0222 0223 #endif /* !U_NO_DEFAULT_INCLUDE_UTF_HEADERS */ 0224 0225 #endif /* __UTF_H__ */
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|