utf.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // © 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: UTF-8
  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. * If U_NO_DEFAULT_INCLUDE_UTF_HEADERS is 0 then utf.h is included by utypes.h
  26. * and itself includes utf8.h and utf16.h after some
  27. * common definitions.
  28. * If U_NO_DEFAULT_INCLUDE_UTF_HEADERS is 1 then each of these headers must be
  29. * included explicitly if their definitions are used.
  30. *
  31. * utf8.h and utf16.h define macros for efficiently getting code points
  32. * in and out of UTF-8/16 strings.
  33. * utf16.h macros have "U16_" prefixes.
  34. * utf8.h defines similar macros with "U8_" prefixes for UTF-8 string handling.
  35. *
  36. * ICU mostly processes 16-bit Unicode strings.
  37. * Most of the time, such strings are well-formed UTF-16.
  38. * Single, unpaired surrogates must be handled as well, and are treated in ICU
  39. * like regular code points where possible.
  40. * (Pairs of surrogate code points are indistinguishable from supplementary
  41. * code points encoded as pairs of supplementary code units.)
  42. *
  43. * In fact, almost all Unicode code points in normal text (>99%)
  44. * are on the BMP (<=U+ffff) and even <=U+d7ff.
  45. * ICU functions handle supplementary code points (U+10000..U+10ffff)
  46. * but are optimized for the much more frequently occurring BMP code points.
  47. *
  48. * umachine.h defines UChar to be an unsigned 16-bit integer.
  49. * Since ICU 59, ICU uses char16_t in C++, UChar only in C,
  50. * and defines UChar=char16_t by default. See the UChar API docs for details.
  51. *
  52. * UChar32 is defined to be a signed 32-bit integer (int32_t), large enough for a 21-bit
  53. * Unicode code point (Unicode scalar value, 0..0x10ffff) and U_SENTINEL (-1).
  54. * Before ICU 2.4, the definition of UChar32 was similarly platform-dependent as
  55. * the definition of UChar. For details see the documentation for UChar32 itself.
  56. *
  57. * utf.h defines a small number of C macros for single Unicode code points.
  58. * These are simple checks for surrogates and non-characters.
  59. * For actual Unicode character properties see uchar.h.
  60. *
  61. * By default, string operations must be done with error checking in case
  62. * a string is not well-formed UTF-16 or UTF-8.
  63. *
  64. * The U16_ macros 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 U8_ macros detect illegal byte sequences and return a negative value.
  69. * Starting with ICU 60, the observable length of a single illegal byte sequence
  70. * skipped by one of these macros follows the Unicode 6+ recommendation
  71. * which is consistent with the W3C Encoding Standard.
  72. *
  73. * There are ..._OR_FFFD versions of both U16_ and U8_ macros
  74. * that return U+FFFD for illegal code unit sequences.
  75. *
  76. * The regular "safe" macros require that the initial, passed-in string index
  77. * is within bounds. They only check the index when they read more than one
  78. * code unit. This is usually done with code similar to the following loop:
  79. * <pre>while(i<length) {
  80. * U16_NEXT(s, i, length, c);
  81. * // use c
  82. * }</pre>
  83. *
  84. * When it is safe to assume that text is well-formed UTF-16
  85. * (does not contain single, unpaired surrogates), then one can use
  86. * U16_..._UNSAFE macros.
  87. * These do not check for proper code unit sequences or truncated text and may
  88. * yield wrong results or even cause a crash if they are used with "malformed"
  89. * text.
  90. * In practice, U16_..._UNSAFE macros will produce slightly less code but
  91. * should not be faster because the processing is only different when a
  92. * surrogate code unit is detected, which will be rare.
  93. *
  94. * Similarly for UTF-8, there are "safe" macros without a suffix,
  95. * and U8_..._UNSAFE versions.
  96. * The performance differences are much larger here because UTF-8 provides so
  97. * many opportunities for malformed sequences.
  98. * The unsafe UTF-8 macros are entirely implemented inside the macro definitions
  99. * and are fast, while the safe UTF-8 macros call functions for some complicated cases.
  100. *
  101. * Unlike with UTF-16, malformed sequences cannot be expressed with distinct
  102. * code point values (0..U+10ffff). They are indicated with negative values instead.
  103. *
  104. * For more information see the ICU User Guide Strings chapter
  105. * (https://unicode-org.github.io/icu/userguide/strings).
  106. *
  107. * <em>Usage:</em>
  108. * ICU coding guidelines for if() statements should be followed when using these macros.
  109. * Compound statements (curly braces {}) must be used for if-else-while...
  110. * bodies and all macro statements should be terminated with semicolon.
  111. *
  112. * @stable ICU 2.4
  113. */
  114. #ifndef __UTF_H__
  115. #define __UTF_H__
  116. #include "unicode/umachine.h"
  117. /* include the utfXX.h after the following definitions */
  118. /* single-code point definitions -------------------------------------------- */
  119. /**
  120. * Is this code point a Unicode noncharacter?
  121. * @param c 32-bit code point
  122. * @return true or false
  123. * @stable ICU 2.4
  124. */
  125. #define U_IS_UNICODE_NONCHAR(c) \
  126. ((c)>=0xfdd0 && \
  127. ((c)<=0xfdef || ((c)&0xfffe)==0xfffe) && (c)<=0x10ffff)
  128. /**
  129. * Is c a Unicode code point value (0..U+10ffff)
  130. * that can be assigned a character?
  131. *
  132. * Code points that are not characters include:
  133. * - single surrogate code points (U+d800..U+dfff, 2048 code points)
  134. * - the last two code points on each plane (U+__fffe and U+__ffff, 34 code points)
  135. * - U+fdd0..U+fdef (new with Unicode 3.1, 32 code points)
  136. * - the highest Unicode code point value is U+10ffff
  137. *
  138. * This means that all code points below U+d800 are character code points,
  139. * and that boundary is tested first for performance.
  140. *
  141. * @param c 32-bit code point
  142. * @return true or false
  143. * @stable ICU 2.4
  144. */
  145. #define U_IS_UNICODE_CHAR(c) \
  146. ((uint32_t)(c)<0xd800 || \
  147. (0xdfff<(c) && (c)<=0x10ffff && !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__ */