while(i 84 * 85 * When it is safe to assume that text is well-formed UTF-16 86 * (does not contain single, unpaired surrogates), then one can use 87 * U16_..._UNSAFE macros. 88 * These do not check for proper code unit sequences or truncated text and may 89 * yield wrong results or even cause a crash if they are used with "malformed" 90 * text. 91 * In practice, U16_..._UNSAFE macros will produce slightly less code but 92 * should not be faster because the processing is only different when a 93 * surrogate code unit is detected, which will be rare. 94 * 95 * Similarly for UTF-8, there are "safe" macros without a suffix, 96 * and U8_..._UNSAFE versions. 97 * The performance differences are much larger here because UTF-8 provides so 98 * many opportunities for malformed sequences. 99 * The unsafe UTF-8 macros are entirely implemented inside the macro definitions 100 * and are fast, while the safe UTF-8 macros call functions for some complicated cases. 101 * 102 * Unlike with UTF-16, malformed sequences cannot be expressed with distinct 103 * code point values (0..U+10ffff). They are indicated with negative values instead. 104 * 105 * For more information see the ICU User Guide Strings chapter 106 * (https://unicode-org.github.io/icu/userguide/strings). 107 * 108 * Usage: 109 * ICU coding guidelines for if() statements should be followed when using these macros. 110 * Compound statements (curly braces {}) must be used for if-else-while... 111 * bodies and all macro statements should be terminated with semicolon. 112 * 113 * @stable ICU 2.4 114 */ 115 116 #ifndef __UTF_H__ 117 #define __UTF_H__ 118 119 #include "unicode/umachine.h" 120 /* include the utfXX.h after the following definitions */ 121 122 /* single-code point definitions -------------------------------------------- */ 123 124 /** 125 * Is this code point a Unicode noncharacter? 126 * @param c 32-bit code point 127 * @return true or false 128 * @stable ICU 2.4 129 */ 130 #define U_IS_UNICODE_NONCHAR(c) \ 131 ((c)>=0xfdd0 && \ 132 ((c)<=0xfdef || ((c)&0xfffe)==0xfffe) && (c)<=0x10ffff) 133 134 /** 135 * Is c a Unicode code point value (0..U+10ffff) 136 * that can be assigned a character? 137 * 138 * Code points that are not characters include: 139 * - single surrogate code points (U+d800..U+dfff, 2048 code points) 140 * - the last two code points on each plane (U+__fffe and U+__ffff, 34 code points) 141 * - U+fdd0..U+fdef (new with Unicode 3.1, 32 code points) 142 * - the highest Unicode code point value is U+10ffff 143 * 144 * This means that all code points below U+d800 are character code points, 145 * and that boundary is tested first for performance. 146 * 147 * @param c 32-bit code point 148 * @return true or false 149 * @stable ICU 2.4 150 */ 151 #define U_IS_UNICODE_CHAR(c) \ 152 ((uint32_t)(c)<0xd800 || \ 153 (0xdfff<(c) && (c)<=0x10ffff && !U_IS_UNICODE_NONCHAR(c))) 154 155 /** 156 * Is this code point a BMP code point (U+0000..U+ffff)? 157 * @param c 32-bit code point 158 * @return true or false 159 * @stable ICU 2.8 160 */ 161 #define U_IS_BMP(c) ((uint32_t)(c)<=0xffff) 162 163 /** 164 * Is this code point a supplementary code point (U+10000..U+10ffff)? 165 * @param c 32-bit code point 166 * @return true or false 167 * @stable ICU 2.8 168 */ 169 #define U_IS_SUPPLEMENTARY(c) ((uint32_t)((c)-0x10000)<=0xfffff) 170 171 /** 172 * Is this code point a lead surrogate (U+d800..U+dbff)? 173 * @param c 32-bit code point 174 * @return true or false 175 * @stable ICU 2.4 176 */ 177 #define U_IS_LEAD(c) (((c)&0xfffffc00)==0xd800) 178 179 /** 180 * Is this code point a trail surrogate (U+dc00..U+dfff)? 181 * @param c 32-bit code point 182 * @return true or false 183 * @stable ICU 2.4 184 */ 185 #define U_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00) 186 187 /** 188 * Is this code point a surrogate (U+d800..U+dfff)? 189 * @param c 32-bit code point 190 * @return true or false 191 * @stable ICU 2.4 192 */ 193 #define U_IS_SURROGATE(c) (((c)&0xfffff800)==0xd800) 194 195 /** 196 * Assuming c is a surrogate code point (U_IS_SURROGATE(c)), 197 * is it a lead surrogate? 198 * @param c 32-bit code point 199 * @return true or false 200 * @stable ICU 2.4 201 */ 202 #define U_IS_SURROGATE_LEAD(c) (((c)&0x400)==0) 203 204 /** 205 * Assuming c is a surrogate code point (U_IS_SURROGATE(c)), 206 * is it a trail surrogate? 207 * @param c 32-bit code point 208 * @return true or false 209 * @stable ICU 4.2 210 */ 211 #define U_IS_SURROGATE_TRAIL(c) (((c)&0x400)!=0) 212 213 /* include the utfXX.h ------------------------------------------------------ */ 214 215 #if !U_NO_DEFAULT_INCLUDE_UTF_HEADERS 216 217 #include "unicode/utf8.h" 218 #include "unicode/utf16.h" 219 220 /* utf_old.h contains deprecated, pre-ICU 2.4 definitions */ 221 #include "unicode/utf_old.h" 222 223 #endif /* !U_NO_DEFAULT_INCLUDE_UTF_HEADERS */ 224 225 #endif /* __UTF_H__ */