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