icu_utf.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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-2015, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. */
  11. #ifndef BASE_THIRD_PARTY_ICU_ICU_UTF_H_
  12. #define BASE_THIRD_PARTY_ICU_ICU_UTF_H_
  13. #include <stdint.h>
  14. namespace base_icu {
  15. // source/common/unicode/umachine.h
  16. /** The ICU boolean type @stable ICU 2.0 */
  17. typedef int8_t UBool;
  18. /**
  19. * Define UChar32 as a type for single Unicode code points.
  20. * UChar32 is a signed 32-bit integer (same as int32_t).
  21. *
  22. * The Unicode code point range is 0..0x10ffff.
  23. * All other values (negative or >=0x110000) are illegal as Unicode code points.
  24. * They may be used as sentinel values to indicate "done", "error"
  25. * or similar non-code point conditions.
  26. *
  27. * Before ICU 2.4 (Jitterbug 2146), UChar32 was defined
  28. * to be wchar_t if that is 32 bits wide (wchar_t may be signed or unsigned)
  29. * or else to be uint32_t.
  30. * That is, the definition of UChar32 was platform-dependent.
  31. *
  32. * @see U_SENTINEL
  33. * @stable ICU 2.4
  34. */
  35. typedef int32_t UChar32;
  36. /**
  37. * This value is intended for sentinel values for APIs that
  38. * (take or) return single code points (UChar32).
  39. * It is outside of the Unicode code point range 0..0x10ffff.
  40. *
  41. * For example, a "done" or "error" value in a new API
  42. * could be indicated with U_SENTINEL.
  43. *
  44. * ICU APIs designed before ICU 2.4 usually define service-specific "done"
  45. * values, mostly 0xffff.
  46. * Those may need to be distinguished from
  47. * actual U+ffff text contents by calling functions like
  48. * CharacterIterator::hasNext() or UnicodeString::length().
  49. *
  50. * @return -1
  51. * @see UChar32
  52. * @stable ICU 2.4
  53. */
  54. #define CBU_SENTINEL (-1)
  55. // source/common/unicode/utf.h
  56. /**
  57. * Is this code point a Unicode noncharacter?
  58. * @param c 32-bit code point
  59. * @return TRUE or FALSE
  60. * @stable ICU 2.4
  61. */
  62. #define CBU_IS_UNICODE_NONCHAR(c) \
  63. ((c)>=0xfdd0 && \
  64. ((c)<=0xfdef || ((c)&0xfffe)==0xfffe) && (c)<=0x10ffff)
  65. /**
  66. * Is c a Unicode code point value (0..U+10ffff)
  67. * that can be assigned a character?
  68. *
  69. * Code points that are not characters include:
  70. * - single surrogate code points (U+d800..U+dfff, 2048 code points)
  71. * - the last two code points on each plane (U+__fffe and U+__ffff, 34 code points)
  72. * - U+fdd0..U+fdef (new with Unicode 3.1, 32 code points)
  73. * - the highest Unicode code point value is U+10ffff
  74. *
  75. * This means that all code points below U+d800 are character code points,
  76. * and that boundary is tested first for performance.
  77. *
  78. * @param c 32-bit code point
  79. * @return TRUE or FALSE
  80. * @stable ICU 2.4
  81. */
  82. #define CBU_IS_UNICODE_CHAR(c) \
  83. ((uint32_t)(c)<0xd800 || \
  84. (0xdfff<(c) && (c)<=0x10ffff && !CBU_IS_UNICODE_NONCHAR(c)))
  85. /**
  86. * Is this code point a surrogate (U+d800..U+dfff)?
  87. * @param c 32-bit code point
  88. * @return TRUE or FALSE
  89. * @stable ICU 2.4
  90. */
  91. #define CBU_IS_SURROGATE(c) (((c)&0xfffff800)==0xd800)
  92. /**
  93. * Assuming c is a surrogate code point (U_IS_SURROGATE(c)),
  94. * is it a lead surrogate?
  95. * @param c 32-bit code point
  96. * @return TRUE or FALSE
  97. * @stable ICU 2.4
  98. */
  99. #define CBU_IS_SURROGATE_LEAD(c) (((c)&0x400)==0)
  100. // source/common/unicode/utf8.h
  101. /**
  102. * Internal bit vector for 3-byte UTF-8 validity check, for use in U8_IS_VALID_LEAD3_AND_T1.
  103. * Each bit indicates whether one lead byte + first trail byte pair starts a valid sequence.
  104. * Lead byte E0..EF bits 3..0 are used as byte index,
  105. * first trail byte bits 7..5 are used as bit index into that byte.
  106. * @see U8_IS_VALID_LEAD3_AND_T1
  107. * @internal
  108. */
  109. #define CBU8_LEAD3_T1_BITS "\x20\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x10\x30\x30"
  110. /**
  111. * Internal 3-byte UTF-8 validity check.
  112. * Non-zero if lead byte E0..EF and first trail byte 00..FF start a valid sequence.
  113. * @internal
  114. */
  115. #define CBU8_IS_VALID_LEAD3_AND_T1(lead, t1) (CBU8_LEAD3_T1_BITS[(lead)&0xf]&(1<<((uint8_t)(t1)>>5)))
  116. /**
  117. * Internal bit vector for 4-byte UTF-8 validity check, for use in U8_IS_VALID_LEAD4_AND_T1.
  118. * Each bit indicates whether one lead byte + first trail byte pair starts a valid sequence.
  119. * First trail byte bits 7..4 are used as byte index,
  120. * lead byte F0..F4 bits 2..0 are used as bit index into that byte.
  121. * @see U8_IS_VALID_LEAD4_AND_T1
  122. * @internal
  123. */
  124. #define CBU8_LEAD4_T1_BITS "\x00\x00\x00\x00\x00\x00\x00\x00\x1E\x0F\x0F\x0F\x00\x00\x00\x00"
  125. /**
  126. * Internal 4-byte UTF-8 validity check.
  127. * Non-zero if lead byte F0..F4 and first trail byte 00..FF start a valid sequence.
  128. * @internal
  129. */
  130. #define CBU8_IS_VALID_LEAD4_AND_T1(lead, t1) (CBU8_LEAD4_T1_BITS[(uint8_t)(t1)>>4]&(1<<((lead)&7)))
  131. /**
  132. * Function for handling "next code point" with error-checking.
  133. *
  134. * This is internal since it is not meant to be called directly by external clie
  135. nts;
  136. * however it is U_STABLE (not U_INTERNAL) since it is called by public macros i
  137. n this
  138. * file and thus must remain stable, and should not be hidden when other interna
  139. l
  140. * functions are hidden (otherwise public macros would fail to compile).
  141. * @internal
  142. */
  143. UChar32
  144. utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, ::base_icu::UChar32 c, ::base_icu::UBool strict);
  145. /**
  146. * Does this code unit (byte) encode a code point by itself (US-ASCII 0..0x7f)?
  147. * @param c 8-bit code unit (byte)
  148. * @return TRUE or FALSE
  149. * @stable ICU 2.4
  150. */
  151. #define CBU8_IS_SINGLE(c) (((c)&0x80)==0)
  152. /**
  153. * Is this code unit (byte) a UTF-8 lead byte? (0xC2..0xF4)
  154. * @param c 8-bit code unit (byte)
  155. * @return TRUE or FALSE
  156. * @stable ICU 2.4
  157. */
  158. #define CBU8_IS_LEAD(c) ((uint8_t)((c)-0xc2)<=0x32)
  159. /**
  160. * Is this code unit (byte) a UTF-8 trail byte? (0x80..0xBF)
  161. * @param c 8-bit code unit (byte)
  162. * @return TRUE or FALSE
  163. * @stable ICU 2.4
  164. */
  165. #define CBU8_IS_TRAIL(c) ((int8_t)(c)<-0x40)
  166. /**
  167. * How many code units (bytes) are used for the UTF-8 encoding
  168. * of this Unicode code point?
  169. * @param c 32-bit code point
  170. * @return 1..4, or 0 if c is a surrogate or not a Unicode code point
  171. * @stable ICU 2.4
  172. */
  173. #define CBU8_LENGTH(c) \
  174. ((uint32_t)(c)<=0x7f ? 1 : \
  175. ((uint32_t)(c)<=0x7ff ? 2 : \
  176. ((uint32_t)(c)<=0xd7ff ? 3 : \
  177. ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
  178. ((uint32_t)(c)<=0xffff ? 3 : 4)\
  179. ) \
  180. ) \
  181. ) \
  182. )
  183. /**
  184. * The maximum number of UTF-8 code units (bytes) per Unicode code point (U+0000..U+10ffff).
  185. * @return 4
  186. * @stable ICU 2.4
  187. */
  188. #define CBU8_MAX_LENGTH 4
  189. /**
  190. * Get a code point from a string at a code point boundary offset,
  191. * and advance the offset to the next code point boundary.
  192. * (Post-incrementing forward iteration.)
  193. * "Safe" macro, checks for illegal sequences and for string boundaries.
  194. *
  195. * The length can be negative for a NUL-terminated string.
  196. *
  197. * The offset may point to the lead byte of a multi-byte sequence,
  198. * in which case the macro will read the whole sequence.
  199. * If the offset points to a trail byte or an illegal UTF-8 sequence, then
  200. * c is set to a negative value.
  201. *
  202. * @param s const uint8_t * string
  203. * @param i int32_t string offset, must be i<length
  204. * @param length int32_t string length
  205. * @param c output UChar32 variable, set to <0 in case of an error
  206. * @see U8_NEXT_UNSAFE
  207. * @stable ICU 2.4
  208. */
  209. #define CBU8_NEXT(s, i, length, c) { \
  210. (c)=(uint8_t)(s)[(i)++]; \
  211. if(!CBU8_IS_SINGLE(c)) { \
  212. uint8_t __t1, __t2; \
  213. if( /* handle U+0800..U+FFFF inline */ \
  214. (0xe0<=(c) && (c)<0xf0) && \
  215. (((i)+1)<(length) || (length)<0) && \
  216. CBU8_IS_VALID_LEAD3_AND_T1((c), __t1=(s)[i]) && \
  217. (__t2=(s)[(i)+1]-0x80)<=0x3f) { \
  218. (c)=(((c)&0xf)<<12)|((__t1&0x3f)<<6)|__t2; \
  219. (i)+=2; \
  220. } else if( /* handle U+0080..U+07FF inline */ \
  221. ((c)<0xe0 && (c)>=0xc2) && \
  222. ((i)!=(length)) && \
  223. (__t1=(s)[i]-0x80)<=0x3f) { \
  224. (c)=(((c)&0x1f)<<6)|__t1; \
  225. ++(i); \
  226. } else { \
  227. /* function call for "complicated" and error cases */ \
  228. (c)=::base_icu::utf8_nextCharSafeBody((const uint8_t *)s, &(i), (length), c, -1); \
  229. } \
  230. } \
  231. }
  232. /**
  233. * Append a code point to a string, overwriting 1 to 4 bytes.
  234. * The offset points to the current end of the string contents
  235. * and is advanced (post-increment).
  236. * "Unsafe" macro, assumes a valid code point and sufficient space in the string.
  237. * Otherwise, the result is undefined.
  238. *
  239. * @param s const uint8_t * string buffer
  240. * @param i string offset
  241. * @param c code point to append
  242. * @see U8_APPEND
  243. * @stable ICU 2.4
  244. */
  245. #define CBU8_APPEND_UNSAFE(s, i, c) { \
  246. if((uint32_t)(c)<=0x7f) { \
  247. (s)[(i)++]=(uint8_t)(c); \
  248. } else { \
  249. if((uint32_t)(c)<=0x7ff) { \
  250. (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
  251. } else { \
  252. if((uint32_t)(c)<=0xffff) { \
  253. (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
  254. } else { \
  255. (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
  256. (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
  257. } \
  258. (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
  259. } \
  260. (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
  261. } \
  262. }
  263. // source/common/unicode/utf16.h
  264. /**
  265. * Does this code unit alone encode a code point (BMP, not a surrogate)?
  266. * @param c 16-bit code unit
  267. * @return TRUE or FALSE
  268. * @stable ICU 2.4
  269. */
  270. #define CBU16_IS_SINGLE(c) !CBU_IS_SURROGATE(c)
  271. /**
  272. * Is this code unit a lead surrogate (U+d800..U+dbff)?
  273. * @param c 16-bit code unit
  274. * @return TRUE or FALSE
  275. * @stable ICU 2.4
  276. */
  277. #define CBU16_IS_LEAD(c) (((c)&0xfffffc00)==0xd800)
  278. /**
  279. * Is this code unit a trail surrogate (U+dc00..U+dfff)?
  280. * @param c 16-bit code unit
  281. * @return TRUE or FALSE
  282. * @stable ICU 2.4
  283. */
  284. #define CBU16_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00)
  285. /**
  286. * Is this code unit a surrogate (U+d800..U+dfff)?
  287. * @param c 16-bit code unit
  288. * @return TRUE or FALSE
  289. * @stable ICU 2.4
  290. */
  291. #define CBU16_IS_SURROGATE(c) CBU_IS_SURROGATE(c)
  292. /**
  293. * Assuming c is a surrogate code point (U16_IS_SURROGATE(c)),
  294. * is it a lead surrogate?
  295. * @param c 16-bit code unit
  296. * @return TRUE or FALSE
  297. * @stable ICU 2.4
  298. */
  299. #define CBU16_IS_SURROGATE_LEAD(c) (((c)&0x400)==0)
  300. /**
  301. * Helper constant for U16_GET_SUPPLEMENTARY.
  302. * @internal
  303. */
  304. #define CBU16_SURROGATE_OFFSET ((0xd800<<10UL)+0xdc00-0x10000)
  305. /**
  306. * Get a supplementary code point value (U+10000..U+10ffff)
  307. * from its lead and trail surrogates.
  308. * The result is undefined if the input values are not
  309. * lead and trail surrogates.
  310. *
  311. * @param lead lead surrogate (U+d800..U+dbff)
  312. * @param trail trail surrogate (U+dc00..U+dfff)
  313. * @return supplementary code point (U+10000..U+10ffff)
  314. * @stable ICU 2.4
  315. */
  316. #define CBU16_GET_SUPPLEMENTARY(lead, trail) \
  317. (((::base_icu::UChar32)(lead)<<10UL)+(::base_icu::UChar32)(trail)-CBU16_SURROGATE_OFFSET)
  318. /**
  319. * Get the lead surrogate (0xd800..0xdbff) for a
  320. * supplementary code point (0x10000..0x10ffff).
  321. * @param supplementary 32-bit code point (U+10000..U+10ffff)
  322. * @return lead surrogate (U+d800..U+dbff) for supplementary
  323. * @stable ICU 2.4
  324. */
  325. #define CBU16_LEAD(supplementary) (::base_icu::UChar)(((supplementary)>>10)+0xd7c0)
  326. /**
  327. * Get the trail surrogate (0xdc00..0xdfff) for a
  328. * supplementary code point (0x10000..0x10ffff).
  329. * @param supplementary 32-bit code point (U+10000..U+10ffff)
  330. * @return trail surrogate (U+dc00..U+dfff) for supplementary
  331. * @stable ICU 2.4
  332. */
  333. #define CBU16_TRAIL(supplementary) (::base_icu::UChar)(((supplementary)&0x3ff)|0xdc00)
  334. /**
  335. * How many 16-bit code units are used to encode this Unicode code point? (1 or 2)
  336. * The result is not defined if c is not a Unicode code point (U+0000..U+10ffff).
  337. * @param c 32-bit code point
  338. * @return 1 or 2
  339. * @stable ICU 2.4
  340. */
  341. #define CBU16_LENGTH(c) ((uint32_t)(c)<=0xffff ? 1 : 2)
  342. /**
  343. * The maximum number of 16-bit code units per Unicode code point (U+0000..U+10ffff).
  344. * @return 2
  345. * @stable ICU 2.4
  346. */
  347. #define CBU16_MAX_LENGTH 2
  348. /**
  349. * Get a code point from a string at a code point boundary offset,
  350. * and advance the offset to the next code point boundary.
  351. * (Post-incrementing forward iteration.)
  352. * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
  353. *
  354. * The length can be negative for a NUL-terminated string.
  355. *
  356. * The offset may point to the lead surrogate unit
  357. * for a supplementary code point, in which case the macro will read
  358. * the following trail surrogate as well.
  359. * If the offset points to a trail surrogate or
  360. * to a single, unpaired lead surrogate, then c is set to that unpaired surrogate.
  361. *
  362. * @param s const UChar * string
  363. * @param i string offset, must be i<length
  364. * @param length string length
  365. * @param c output UChar32 variable
  366. * @see U16_NEXT_UNSAFE
  367. * @stable ICU 2.4
  368. */
  369. #define CBU16_NEXT(s, i, length, c) { \
  370. (c)=(s)[(i)++]; \
  371. if(CBU16_IS_LEAD(c)) { \
  372. uint16_t __c2; \
  373. if((i)!=(length) && CBU16_IS_TRAIL(__c2=(s)[(i)])) { \
  374. ++(i); \
  375. (c)=CBU16_GET_SUPPLEMENTARY((c), __c2); \
  376. } \
  377. } \
  378. }
  379. /**
  380. * Append a code point to a string, overwriting 1 or 2 code units.
  381. * The offset points to the current end of the string contents
  382. * and is advanced (post-increment).
  383. * "Unsafe" macro, assumes a valid code point and sufficient space in the string.
  384. * Otherwise, the result is undefined.
  385. *
  386. * @param s const UChar * string buffer
  387. * @param i string offset
  388. * @param c code point to append
  389. * @see U16_APPEND
  390. * @stable ICU 2.4
  391. */
  392. #define CBU16_APPEND_UNSAFE(s, i, c) { \
  393. if((uint32_t)(c)<=0xffff) { \
  394. (s)[(i)++]=(uint16_t)(c); \
  395. } else { \
  396. (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
  397. (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
  398. } \
  399. }
  400. } // namesapce base_icu
  401. #endif // BASE_THIRD_PARTY_ICU_ICU_UTF_H_