utf.h 7.6 KB

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