utf.h 7.8 KB

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