utf8.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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. * file name: utf8.h
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 1999sep13
  16. * created by: Markus W. Scherer
  17. */
  18. /**
  19. * \file
  20. * \brief C API: 8-bit Unicode handling macros
  21. *
  22. * This file defines macros to deal with 8-bit Unicode (UTF-8) code units (bytes) and strings.
  23. *
  24. * For more information see utf.h and the ICU User Guide Strings chapter
  25. * (https://unicode-org.github.io/icu/userguide/strings).
  26. *
  27. * <em>Usage:</em>
  28. * ICU coding guidelines for if() statements should be followed when using these macros.
  29. * Compound statements (curly braces {}) must be used for if-else-while...
  30. * bodies and all macro statements should be terminated with semicolon.
  31. */
  32. #ifndef __UTF8_H__
  33. #define __UTF8_H__
  34. #include <stdbool.h>
  35. #include "unicode/umachine.h"
  36. #ifndef __UTF_H__
  37. # include "unicode/utf.h"
  38. #endif
  39. /* internal definitions ----------------------------------------------------- */
  40. /**
  41. * Counts the trail bytes for a UTF-8 lead byte.
  42. * Returns 0 for 0..0xc1 as well as for 0xf5..0xff.
  43. * leadByte might be evaluated multiple times.
  44. *
  45. * This is internal since it is not meant to be called directly by external clients;
  46. * however it is called by public macros in this file and thus must remain stable.
  47. *
  48. * @param leadByte The first byte of a UTF-8 sequence. Must be 0..0xff.
  49. * @internal
  50. */
  51. #define U8_COUNT_TRAIL_BYTES(leadByte) \
  52. (U8_IS_LEAD(leadByte) ? \
  53. ((uint8_t)(leadByte)>=0xe0)+((uint8_t)(leadByte)>=0xf0)+1 : 0)
  54. /**
  55. * Counts the trail bytes for a UTF-8 lead byte of a valid UTF-8 sequence.
  56. * Returns 0 for 0..0xc1. Undefined for 0xf5..0xff.
  57. * leadByte might be evaluated multiple times.
  58. *
  59. * This is internal since it is not meant to be called directly by external clients;
  60. * however it is called by public macros in this file and thus must remain stable.
  61. *
  62. * @param leadByte The first byte of a UTF-8 sequence. Must be 0..0xff.
  63. * @internal
  64. */
  65. #define U8_COUNT_TRAIL_BYTES_UNSAFE(leadByte) \
  66. (((uint8_t)(leadByte)>=0xc2)+((uint8_t)(leadByte)>=0xe0)+((uint8_t)(leadByte)>=0xf0))
  67. /**
  68. * Mask a UTF-8 lead byte, leave only the lower bits that form part of the code point value.
  69. *
  70. * This is internal since it is not meant to be called directly by external clients;
  71. * however it is called by public macros in this file and thus must remain stable.
  72. * @internal
  73. */
  74. #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
  75. /**
  76. * Internal bit vector for 3-byte UTF-8 validity check, for use in U8_IS_VALID_LEAD3_AND_T1.
  77. * Each bit indicates whether one lead byte + first trail byte pair starts a valid sequence.
  78. * Lead byte E0..EF bits 3..0 are used as byte index,
  79. * first trail byte bits 7..5 are used as bit index into that byte.
  80. * @see U8_IS_VALID_LEAD3_AND_T1
  81. * @internal
  82. */
  83. #define U8_LEAD3_T1_BITS "\x20\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x10\x30\x30"
  84. /**
  85. * Internal 3-byte UTF-8 validity check.
  86. * Non-zero if lead byte E0..EF and first trail byte 00..FF start a valid sequence.
  87. * @internal
  88. */
  89. #define U8_IS_VALID_LEAD3_AND_T1(lead, t1) (U8_LEAD3_T1_BITS[(lead)&0xf]&(1<<((uint8_t)(t1)>>5)))
  90. /**
  91. * Internal bit vector for 4-byte UTF-8 validity check, for use in U8_IS_VALID_LEAD4_AND_T1.
  92. * Each bit indicates whether one lead byte + first trail byte pair starts a valid sequence.
  93. * First trail byte bits 7..4 are used as byte index,
  94. * lead byte F0..F4 bits 2..0 are used as bit index into that byte.
  95. * @see U8_IS_VALID_LEAD4_AND_T1
  96. * @internal
  97. */
  98. #define U8_LEAD4_T1_BITS "\x00\x00\x00\x00\x00\x00\x00\x00\x1E\x0F\x0F\x0F\x00\x00\x00\x00"
  99. /**
  100. * Internal 4-byte UTF-8 validity check.
  101. * Non-zero if lead byte F0..F4 and first trail byte 00..FF start a valid sequence.
  102. * @internal
  103. */
  104. #define U8_IS_VALID_LEAD4_AND_T1(lead, t1) (U8_LEAD4_T1_BITS[(uint8_t)(t1)>>4]&(1<<((lead)&7)))
  105. /**
  106. * Function for handling "next code point" with error-checking.
  107. *
  108. * This is internal since it is not meant to be called directly by external clients;
  109. * however it is called by public macros in this
  110. * file and thus must remain stable, and should not be hidden when other internal
  111. * functions are hidden (otherwise public macros would fail to compile).
  112. * @internal
  113. */
  114. U_CAPI UChar32 U_EXPORT2
  115. utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict);
  116. /**
  117. * Function for handling "append code point" with error-checking.
  118. *
  119. * This is internal since it is not meant to be called directly by external clients;
  120. * however it is called by public macros in this
  121. * file and thus must remain stable, and should not be hidden when other internal
  122. * functions are hidden (otherwise public macros would fail to compile).
  123. * @internal
  124. */
  125. U_CAPI int32_t U_EXPORT2
  126. utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError);
  127. /**
  128. * Function for handling "previous code point" with error-checking.
  129. *
  130. * This is internal since it is not meant to be called directly by external clients;
  131. * however it is called by public macros in this
  132. * file and thus must remain stable, and should not be hidden when other internal
  133. * functions are hidden (otherwise public macros would fail to compile).
  134. * @internal
  135. */
  136. U_CAPI UChar32 U_EXPORT2
  137. utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict);
  138. /**
  139. * Function for handling "skip backward one code point" with error-checking.
  140. *
  141. * This is internal since it is not meant to be called directly by external clients;
  142. * however it is called by public macros in this
  143. * file and thus must remain stable, and should not be hidden when other internal
  144. * functions are hidden (otherwise public macros would fail to compile).
  145. * @internal
  146. */
  147. U_CAPI int32_t U_EXPORT2
  148. utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i);
  149. /* single-code point definitions -------------------------------------------- */
  150. /**
  151. * Does this code unit (byte) encode a code point by itself (US-ASCII 0..0x7f)?
  152. * @param c 8-bit code unit (byte)
  153. * @return true or false
  154. * @stable ICU 2.4
  155. */
  156. #define U8_IS_SINGLE(c) (((c)&0x80)==0)
  157. /**
  158. * Is this code unit (byte) a UTF-8 lead byte? (0xC2..0xF4)
  159. * @param c 8-bit code unit (byte)
  160. * @return true or false
  161. * @stable ICU 2.4
  162. */
  163. #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc2)<=0x32)
  164. // 0x32=0xf4-0xc2
  165. /**
  166. * Is this code unit (byte) a UTF-8 trail byte? (0x80..0xBF)
  167. * @param c 8-bit code unit (byte)
  168. * @return true or false
  169. * @stable ICU 2.4
  170. */
  171. #define U8_IS_TRAIL(c) ((int8_t)(c)<-0x40)
  172. /**
  173. * How many code units (bytes) are used for the UTF-8 encoding
  174. * of this Unicode code point?
  175. * @param c 32-bit code point
  176. * @return 1..4, or 0 if c is a surrogate or not a Unicode code point
  177. * @stable ICU 2.4
  178. */
  179. #define U8_LENGTH(c) \
  180. ((uint32_t)(c)<=0x7f ? 1 : \
  181. ((uint32_t)(c)<=0x7ff ? 2 : \
  182. ((uint32_t)(c)<=0xd7ff ? 3 : \
  183. ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
  184. ((uint32_t)(c)<=0xffff ? 3 : 4)\
  185. ) \
  186. ) \
  187. ) \
  188. )
  189. /**
  190. * The maximum number of UTF-8 code units (bytes) per Unicode code point (U+0000..U+10ffff).
  191. * @return 4
  192. * @stable ICU 2.4
  193. */
  194. #define U8_MAX_LENGTH 4
  195. /**
  196. * Get a code point from a string at a random-access offset,
  197. * without changing the offset.
  198. * The offset may point to either the lead byte or one of the trail bytes
  199. * for a code point, in which case the macro will read all of the bytes
  200. * for the code point.
  201. * The result is undefined if the offset points to an illegal UTF-8
  202. * byte sequence.
  203. * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
  204. *
  205. * @param s const uint8_t * string
  206. * @param i string offset
  207. * @param c output UChar32 variable
  208. * @see U8_GET
  209. * @stable ICU 2.4
  210. */
  211. #define U8_GET_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
  212. int32_t _u8_get_unsafe_index=(int32_t)(i); \
  213. U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \
  214. U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \
  215. } UPRV_BLOCK_MACRO_END
  216. /**
  217. * Get a code point from a string at a random-access offset,
  218. * without changing the offset.
  219. * The offset may point to either the lead byte or one of the trail bytes
  220. * for a code point, in which case the macro will read all of the bytes
  221. * for the code point.
  222. *
  223. * The length can be negative for a NUL-terminated string.
  224. *
  225. * If the offset points to an illegal UTF-8 byte sequence, then
  226. * c is set to a negative value.
  227. * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
  228. *
  229. * @param s const uint8_t * string
  230. * @param start int32_t starting string offset
  231. * @param i int32_t string offset, must be start<=i<length
  232. * @param length int32_t string length
  233. * @param c output UChar32 variable, set to <0 in case of an error
  234. * @see U8_GET_UNSAFE
  235. * @stable ICU 2.4
  236. */
  237. #define U8_GET(s, start, i, length, c) UPRV_BLOCK_MACRO_BEGIN { \
  238. int32_t _u8_get_index=(i); \
  239. U8_SET_CP_START(s, start, _u8_get_index); \
  240. U8_NEXT(s, _u8_get_index, length, c); \
  241. } UPRV_BLOCK_MACRO_END
  242. /**
  243. * Get a code point from a string at a random-access offset,
  244. * without changing the offset.
  245. * The offset may point to either the lead byte or one of the trail bytes
  246. * for a code point, in which case the macro will read all of the bytes
  247. * for the code point.
  248. *
  249. * The length can be negative for a NUL-terminated string.
  250. *
  251. * If the offset points to an illegal UTF-8 byte sequence, then
  252. * c is set to U+FFFD.
  253. * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT_OR_FFFD.
  254. *
  255. * This macro does not distinguish between a real U+FFFD in the text
  256. * and U+FFFD returned for an ill-formed sequence.
  257. * Use U8_GET() if that distinction is important.
  258. *
  259. * @param s const uint8_t * string
  260. * @param start int32_t starting string offset
  261. * @param i int32_t string offset, must be start<=i<length
  262. * @param length int32_t string length
  263. * @param c output UChar32 variable, set to U+FFFD in case of an error
  264. * @see U8_GET
  265. * @stable ICU 51
  266. */
  267. #define U8_GET_OR_FFFD(s, start, i, length, c) UPRV_BLOCK_MACRO_BEGIN { \
  268. int32_t _u8_get_index=(i); \
  269. U8_SET_CP_START(s, start, _u8_get_index); \
  270. U8_NEXT_OR_FFFD(s, _u8_get_index, length, c); \
  271. } UPRV_BLOCK_MACRO_END
  272. /* definitions with forward iteration --------------------------------------- */
  273. /**
  274. * Get a code point from a string at a code point boundary offset,
  275. * and advance the offset to the next code point boundary.
  276. * (Post-incrementing forward iteration.)
  277. * "Unsafe" macro, assumes well-formed UTF-8.
  278. *
  279. * The offset may point to the lead byte of a multi-byte sequence,
  280. * in which case the macro will read the whole sequence.
  281. * The result is undefined if the offset points to a trail byte
  282. * or an illegal UTF-8 sequence.
  283. *
  284. * @param s const uint8_t * string
  285. * @param i string offset
  286. * @param c output UChar32 variable
  287. * @see U8_NEXT
  288. * @stable ICU 2.4
  289. */
  290. #define U8_NEXT_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
  291. (c)=(uint8_t)(s)[(i)++]; \
  292. if(!U8_IS_SINGLE(c)) { \
  293. if((c)<0xe0) { \
  294. (c)=(((c)&0x1f)<<6)|((s)[(i)++]&0x3f); \
  295. } else if((c)<0xf0) { \
  296. /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
  297. (c)=(UChar)(((c)<<12)|(((s)[i]&0x3f)<<6)|((s)[(i)+1]&0x3f)); \
  298. (i)+=2; \
  299. } else { \
  300. (c)=(((c)&7)<<18)|(((s)[i]&0x3f)<<12)|(((s)[(i)+1]&0x3f)<<6)|((s)[(i)+2]&0x3f); \
  301. (i)+=3; \
  302. } \
  303. } \
  304. } UPRV_BLOCK_MACRO_END
  305. /**
  306. * Get a code point from a string at a code point boundary offset,
  307. * and advance the offset to the next code point boundary.
  308. * (Post-incrementing forward iteration.)
  309. * "Safe" macro, checks for illegal sequences and for string boundaries.
  310. *
  311. * The length can be negative for a NUL-terminated string.
  312. *
  313. * The offset may point to the lead byte of a multi-byte sequence,
  314. * in which case the macro will read the whole sequence.
  315. * If the offset points to a trail byte or an illegal UTF-8 sequence, then
  316. * c is set to a negative value.
  317. *
  318. * @param s const uint8_t * string
  319. * @param i int32_t string offset, must be i<length
  320. * @param length int32_t string length
  321. * @param c output UChar32 variable, set to <0 in case of an error
  322. * @see U8_NEXT_UNSAFE
  323. * @stable ICU 2.4
  324. */
  325. #define U8_NEXT(s, i, length, c) U8_INTERNAL_NEXT_OR_SUB(s, i, length, c, U_SENTINEL)
  326. /**
  327. * Get a code point from a string at a code point boundary offset,
  328. * and advance the offset to the next code point boundary.
  329. * (Post-incrementing forward iteration.)
  330. * "Safe" macro, checks for illegal sequences and for string boundaries.
  331. *
  332. * The length can be negative for a NUL-terminated string.
  333. *
  334. * The offset may point to the lead byte of a multi-byte sequence,
  335. * in which case the macro will read the whole sequence.
  336. * If the offset points to a trail byte or an illegal UTF-8 sequence, then
  337. * c is set to U+FFFD.
  338. *
  339. * This macro does not distinguish between a real U+FFFD in the text
  340. * and U+FFFD returned for an ill-formed sequence.
  341. * Use U8_NEXT() if that distinction is important.
  342. *
  343. * @param s const uint8_t * string
  344. * @param i int32_t string offset, must be i<length
  345. * @param length int32_t string length
  346. * @param c output UChar32 variable, set to U+FFFD in case of an error
  347. * @see U8_NEXT
  348. * @stable ICU 51
  349. */
  350. #define U8_NEXT_OR_FFFD(s, i, length, c) U8_INTERNAL_NEXT_OR_SUB(s, i, length, c, 0xfffd)
  351. /** @internal */
  352. #define U8_INTERNAL_NEXT_OR_SUB(s, i, length, c, sub) UPRV_BLOCK_MACRO_BEGIN { \
  353. (c)=(uint8_t)(s)[(i)++]; \
  354. if(!U8_IS_SINGLE(c)) { \
  355. uint8_t __t = 0; \
  356. if((i)!=(length) && \
  357. /* fetch/validate/assemble all but last trail byte */ \
  358. ((c)>=0xe0 ? \
  359. ((c)<0xf0 ? /* U+0800..U+FFFF except surrogates */ \
  360. U8_LEAD3_T1_BITS[(c)&=0xf]&(1<<((__t=(s)[i])>>5)) && \
  361. (__t&=0x3f, 1) \
  362. : /* U+10000..U+10FFFF */ \
  363. ((c)-=0xf0)<=4 && \
  364. U8_LEAD4_T1_BITS[(__t=(s)[i])>>4]&(1<<(c)) && \
  365. ((c)=((c)<<6)|(__t&0x3f), ++(i)!=(length)) && \
  366. (__t=(s)[i]-0x80)<=0x3f) && \
  367. /* valid second-to-last trail byte */ \
  368. ((c)=((c)<<6)|__t, ++(i)!=(length)) \
  369. : /* U+0080..U+07FF */ \
  370. (c)>=0xc2 && ((c)&=0x1f, 1)) && \
  371. /* last trail byte */ \
  372. (__t=(s)[i]-0x80)<=0x3f && \
  373. ((c)=((c)<<6)|__t, ++(i), 1)) { \
  374. } else { \
  375. (c)=(sub); /* ill-formed*/ \
  376. } \
  377. } \
  378. } UPRV_BLOCK_MACRO_END
  379. /**
  380. * Append a code point to a string, overwriting 1 to 4 bytes.
  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 uint8_t * string buffer
  387. * @param i string offset
  388. * @param c code point to append
  389. * @see U8_APPEND
  390. * @stable ICU 2.4
  391. */
  392. #define U8_APPEND_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
  393. uint32_t __uc=(c); \
  394. if(__uc<=0x7f) { \
  395. (s)[(i)++]=(uint8_t)__uc; \
  396. } else { \
  397. if(__uc<=0x7ff) { \
  398. (s)[(i)++]=(uint8_t)((__uc>>6)|0xc0); \
  399. } else { \
  400. if(__uc<=0xffff) { \
  401. (s)[(i)++]=(uint8_t)((__uc>>12)|0xe0); \
  402. } else { \
  403. (s)[(i)++]=(uint8_t)((__uc>>18)|0xf0); \
  404. (s)[(i)++]=(uint8_t)(((__uc>>12)&0x3f)|0x80); \
  405. } \
  406. (s)[(i)++]=(uint8_t)(((__uc>>6)&0x3f)|0x80); \
  407. } \
  408. (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \
  409. } \
  410. } UPRV_BLOCK_MACRO_END
  411. /**
  412. * Append a code point to a string, overwriting 1 to 4 bytes.
  413. * The offset points to the current end of the string contents
  414. * and is advanced (post-increment).
  415. * "Safe" macro, checks for a valid code point.
  416. * If a non-ASCII code point is written, checks for sufficient space in the string.
  417. * If the code point is not valid or trail bytes do not fit,
  418. * then isError is set to true.
  419. *
  420. * @param s const uint8_t * string buffer
  421. * @param i int32_t string offset, must be i<capacity
  422. * @param capacity int32_t size of the string buffer
  423. * @param c UChar32 code point to append
  424. * @param isError output UBool set to true if an error occurs, otherwise not modified
  425. * @see U8_APPEND_UNSAFE
  426. * @stable ICU 2.4
  427. */
  428. #define U8_APPEND(s, i, capacity, c, isError) UPRV_BLOCK_MACRO_BEGIN { \
  429. uint32_t __uc=(c); \
  430. if(__uc<=0x7f) { \
  431. (s)[(i)++]=(uint8_t)__uc; \
  432. } else if(__uc<=0x7ff && (i)+1<(capacity)) { \
  433. (s)[(i)++]=(uint8_t)((__uc>>6)|0xc0); \
  434. (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \
  435. } else if((__uc<=0xd7ff || (0xe000<=__uc && __uc<=0xffff)) && (i)+2<(capacity)) { \
  436. (s)[(i)++]=(uint8_t)((__uc>>12)|0xe0); \
  437. (s)[(i)++]=(uint8_t)(((__uc>>6)&0x3f)|0x80); \
  438. (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \
  439. } else if(0xffff<__uc && __uc<=0x10ffff && (i)+3<(capacity)) { \
  440. (s)[(i)++]=(uint8_t)((__uc>>18)|0xf0); \
  441. (s)[(i)++]=(uint8_t)(((__uc>>12)&0x3f)|0x80); \
  442. (s)[(i)++]=(uint8_t)(((__uc>>6)&0x3f)|0x80); \
  443. (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \
  444. } else { \
  445. (isError)=true; \
  446. } \
  447. } UPRV_BLOCK_MACRO_END
  448. /**
  449. * Advance the string offset from one code point boundary to the next.
  450. * (Post-incrementing iteration.)
  451. * "Unsafe" macro, assumes well-formed UTF-8.
  452. *
  453. * @param s const uint8_t * string
  454. * @param i string offset
  455. * @see U8_FWD_1
  456. * @stable ICU 2.4
  457. */
  458. #define U8_FWD_1_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
  459. (i)+=1+U8_COUNT_TRAIL_BYTES_UNSAFE((s)[i]); \
  460. } UPRV_BLOCK_MACRO_END
  461. /**
  462. * Advance the string offset from one code point boundary to the next.
  463. * (Post-incrementing iteration.)
  464. * "Safe" macro, checks for illegal sequences and for string boundaries.
  465. *
  466. * The length can be negative for a NUL-terminated string.
  467. *
  468. * @param s const uint8_t * string
  469. * @param i int32_t string offset, must be i<length
  470. * @param length int32_t string length
  471. * @see U8_FWD_1_UNSAFE
  472. * @stable ICU 2.4
  473. */
  474. #define U8_FWD_1(s, i, length) UPRV_BLOCK_MACRO_BEGIN { \
  475. uint8_t __b=(s)[(i)++]; \
  476. if(U8_IS_LEAD(__b) && (i)!=(length)) { \
  477. uint8_t __t1=(s)[i]; \
  478. if((0xe0<=__b && __b<0xf0)) { \
  479. if(U8_IS_VALID_LEAD3_AND_T1(__b, __t1) && \
  480. ++(i)!=(length) && U8_IS_TRAIL((s)[i])) { \
  481. ++(i); \
  482. } \
  483. } else if(__b<0xe0) { \
  484. if(U8_IS_TRAIL(__t1)) { \
  485. ++(i); \
  486. } \
  487. } else /* c>=0xf0 */ { \
  488. if(U8_IS_VALID_LEAD4_AND_T1(__b, __t1) && \
  489. ++(i)!=(length) && U8_IS_TRAIL((s)[i]) && \
  490. ++(i)!=(length) && U8_IS_TRAIL((s)[i])) { \
  491. ++(i); \
  492. } \
  493. } \
  494. } \
  495. } UPRV_BLOCK_MACRO_END
  496. /**
  497. * Advance the string offset from one code point boundary to the n-th next one,
  498. * i.e., move forward by n code points.
  499. * (Post-incrementing iteration.)
  500. * "Unsafe" macro, assumes well-formed UTF-8.
  501. *
  502. * @param s const uint8_t * string
  503. * @param i string offset
  504. * @param n number of code points to skip
  505. * @see U8_FWD_N
  506. * @stable ICU 2.4
  507. */
  508. #define U8_FWD_N_UNSAFE(s, i, n) UPRV_BLOCK_MACRO_BEGIN { \
  509. int32_t __N=(n); \
  510. while(__N>0) { \
  511. U8_FWD_1_UNSAFE(s, i); \
  512. --__N; \
  513. } \
  514. } UPRV_BLOCK_MACRO_END
  515. /**
  516. * Advance the string offset from one code point boundary to the n-th next one,
  517. * i.e., move forward by n code points.
  518. * (Post-incrementing iteration.)
  519. * "Safe" macro, checks for illegal sequences and for string boundaries.
  520. *
  521. * The length can be negative for a NUL-terminated string.
  522. *
  523. * @param s const uint8_t * string
  524. * @param i int32_t string offset, must be i<length
  525. * @param length int32_t string length
  526. * @param n number of code points to skip
  527. * @see U8_FWD_N_UNSAFE
  528. * @stable ICU 2.4
  529. */
  530. #define U8_FWD_N(s, i, length, n) UPRV_BLOCK_MACRO_BEGIN { \
  531. int32_t __N=(n); \
  532. while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
  533. U8_FWD_1(s, i, length); \
  534. --__N; \
  535. } \
  536. } UPRV_BLOCK_MACRO_END
  537. /**
  538. * Adjust a random-access offset to a code point boundary
  539. * at the start of a code point.
  540. * If the offset points to a UTF-8 trail byte,
  541. * then the offset is moved backward to the corresponding lead byte.
  542. * Otherwise, it is not modified.
  543. * "Unsafe" macro, assumes well-formed UTF-8.
  544. *
  545. * @param s const uint8_t * string
  546. * @param i string offset
  547. * @see U8_SET_CP_START
  548. * @stable ICU 2.4
  549. */
  550. #define U8_SET_CP_START_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
  551. while(U8_IS_TRAIL((s)[i])) { --(i); } \
  552. } UPRV_BLOCK_MACRO_END
  553. /**
  554. * Adjust a random-access offset to a code point boundary
  555. * at the start of a code point.
  556. * If the offset points to a UTF-8 trail byte,
  557. * then the offset is moved backward to the corresponding lead byte.
  558. * Otherwise, it is not modified.
  559. *
  560. * "Safe" macro, checks for illegal sequences and for string boundaries.
  561. * Unlike U8_TRUNCATE_IF_INCOMPLETE(), this macro always reads s[i].
  562. *
  563. * @param s const uint8_t * string
  564. * @param start int32_t starting string offset (usually 0)
  565. * @param i int32_t string offset, must be start<=i
  566. * @see U8_SET_CP_START_UNSAFE
  567. * @see U8_TRUNCATE_IF_INCOMPLETE
  568. * @stable ICU 2.4
  569. */
  570. #define U8_SET_CP_START(s, start, i) UPRV_BLOCK_MACRO_BEGIN { \
  571. if(U8_IS_TRAIL((s)[(i)])) { \
  572. (i)=utf8_back1SafeBody(s, start, (i)); \
  573. } \
  574. } UPRV_BLOCK_MACRO_END
  575. /**
  576. * If the string ends with a UTF-8 byte sequence that is valid so far
  577. * but incomplete, then reduce the length of the string to end before
  578. * the lead byte of that incomplete sequence.
  579. * For example, if the string ends with E1 80, the length is reduced by 2.
  580. *
  581. * In all other cases (the string ends with a complete sequence, or it is not
  582. * possible for any further trail byte to extend the trailing sequence)
  583. * the length remains unchanged.
  584. *
  585. * Useful for processing text split across multiple buffers
  586. * (save the incomplete sequence for later)
  587. * and for optimizing iteration
  588. * (check for string length only once per character).
  589. *
  590. * "Safe" macro, checks for illegal sequences and for string boundaries.
  591. * Unlike U8_SET_CP_START(), this macro never reads s[length].
  592. *
  593. * (In UTF-16, simply check for U16_IS_LEAD(last code unit).)
  594. *
  595. * @param s const uint8_t * string
  596. * @param start int32_t starting string offset (usually 0)
  597. * @param length int32_t string length (usually start<=length)
  598. * @see U8_SET_CP_START
  599. * @stable ICU 61
  600. */
  601. #define U8_TRUNCATE_IF_INCOMPLETE(s, start, length) UPRV_BLOCK_MACRO_BEGIN { \
  602. if((length)>(start)) { \
  603. uint8_t __b1=s[(length)-1]; \
  604. if(U8_IS_SINGLE(__b1)) { \
  605. /* common ASCII character */ \
  606. } else if(U8_IS_LEAD(__b1)) { \
  607. --(length); \
  608. } else if(U8_IS_TRAIL(__b1) && ((length)-2)>=(start)) { \
  609. uint8_t __b2=s[(length)-2]; \
  610. if(0xe0<=__b2 && __b2<=0xf4) { \
  611. if(__b2<0xf0 ? U8_IS_VALID_LEAD3_AND_T1(__b2, __b1) : \
  612. U8_IS_VALID_LEAD4_AND_T1(__b2, __b1)) { \
  613. (length)-=2; \
  614. } \
  615. } else if(U8_IS_TRAIL(__b2) && ((length)-3)>=(start)) { \
  616. uint8_t __b3=s[(length)-3]; \
  617. if(0xf0<=__b3 && __b3<=0xf4 && U8_IS_VALID_LEAD4_AND_T1(__b3, __b2)) { \
  618. (length)-=3; \
  619. } \
  620. } \
  621. } \
  622. } \
  623. } UPRV_BLOCK_MACRO_END
  624. /* definitions with backward iteration -------------------------------------- */
  625. /**
  626. * Move the string offset from one code point boundary to the previous one
  627. * and get the code point between them.
  628. * (Pre-decrementing backward iteration.)
  629. * "Unsafe" macro, assumes well-formed UTF-8.
  630. *
  631. * The input offset may be the same as the string length.
  632. * If the offset is behind a multi-byte sequence, then the macro will read
  633. * the whole sequence.
  634. * If the offset is behind a lead byte, then that itself
  635. * will be returned as the code point.
  636. * The result is undefined if the offset is behind an illegal UTF-8 sequence.
  637. *
  638. * @param s const uint8_t * string
  639. * @param i string offset
  640. * @param c output UChar32 variable
  641. * @see U8_PREV
  642. * @stable ICU 2.4
  643. */
  644. #define U8_PREV_UNSAFE(s, i, c) UPRV_BLOCK_MACRO_BEGIN { \
  645. (c)=(uint8_t)(s)[--(i)]; \
  646. if(U8_IS_TRAIL(c)) { \
  647. uint8_t __b, __count=1, __shift=6; \
  648. \
  649. /* c is a trail byte */ \
  650. (c)&=0x3f; \
  651. for(;;) { \
  652. __b=(s)[--(i)]; \
  653. if(__b>=0xc0) { \
  654. U8_MASK_LEAD_BYTE(__b, __count); \
  655. (c)|=(UChar32)__b<<__shift; \
  656. break; \
  657. } else { \
  658. (c)|=(UChar32)(__b&0x3f)<<__shift; \
  659. ++__count; \
  660. __shift+=6; \
  661. } \
  662. } \
  663. } \
  664. } UPRV_BLOCK_MACRO_END
  665. /**
  666. * Move the string offset from one code point boundary to the previous one
  667. * and get the code point between them.
  668. * (Pre-decrementing backward iteration.)
  669. * "Safe" macro, checks for illegal sequences and for string boundaries.
  670. *
  671. * The input offset may be the same as the string length.
  672. * If the offset is behind a multi-byte sequence, then the macro will read
  673. * the whole sequence.
  674. * If the offset is behind a lead byte, then that itself
  675. * will be returned as the code point.
  676. * If the offset is behind an illegal UTF-8 sequence, then c is set to a negative value.
  677. *
  678. * @param s const uint8_t * string
  679. * @param start int32_t starting string offset (usually 0)
  680. * @param i int32_t string offset, must be start<i
  681. * @param c output UChar32 variable, set to <0 in case of an error
  682. * @see U8_PREV_UNSAFE
  683. * @stable ICU 2.4
  684. */
  685. #define U8_PREV(s, start, i, c) UPRV_BLOCK_MACRO_BEGIN { \
  686. (c)=(uint8_t)(s)[--(i)]; \
  687. if(!U8_IS_SINGLE(c)) { \
  688. (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
  689. } \
  690. } UPRV_BLOCK_MACRO_END
  691. /**
  692. * Move the string offset from one code point boundary to the previous one
  693. * and get the code point between them.
  694. * (Pre-decrementing backward iteration.)
  695. * "Safe" macro, checks for illegal sequences and for string boundaries.
  696. *
  697. * The input offset may be the same as the string length.
  698. * If the offset is behind a multi-byte sequence, then the macro will read
  699. * the whole sequence.
  700. * If the offset is behind a lead byte, then that itself
  701. * will be returned as the code point.
  702. * If the offset is behind an illegal UTF-8 sequence, then c is set to U+FFFD.
  703. *
  704. * This macro does not distinguish between a real U+FFFD in the text
  705. * and U+FFFD returned for an ill-formed sequence.
  706. * Use U8_PREV() if that distinction is important.
  707. *
  708. * @param s const uint8_t * string
  709. * @param start int32_t starting string offset (usually 0)
  710. * @param i int32_t string offset, must be start<i
  711. * @param c output UChar32 variable, set to U+FFFD in case of an error
  712. * @see U8_PREV
  713. * @stable ICU 51
  714. */
  715. #define U8_PREV_OR_FFFD(s, start, i, c) UPRV_BLOCK_MACRO_BEGIN { \
  716. (c)=(uint8_t)(s)[--(i)]; \
  717. if(!U8_IS_SINGLE(c)) { \
  718. (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -3); \
  719. } \
  720. } UPRV_BLOCK_MACRO_END
  721. /**
  722. * Move the string offset from one code point boundary to the previous one.
  723. * (Pre-decrementing backward iteration.)
  724. * The input offset may be the same as the string length.
  725. * "Unsafe" macro, assumes well-formed UTF-8.
  726. *
  727. * @param s const uint8_t * string
  728. * @param i string offset
  729. * @see U8_BACK_1
  730. * @stable ICU 2.4
  731. */
  732. #define U8_BACK_1_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
  733. while(U8_IS_TRAIL((s)[--(i)])) {} \
  734. } UPRV_BLOCK_MACRO_END
  735. /**
  736. * Move the string offset from one code point boundary to the previous one.
  737. * (Pre-decrementing backward iteration.)
  738. * The input offset may be the same as the string length.
  739. * "Safe" macro, checks for illegal sequences and for string boundaries.
  740. *
  741. * @param s const uint8_t * string
  742. * @param start int32_t starting string offset (usually 0)
  743. * @param i int32_t string offset, must be start<i
  744. * @see U8_BACK_1_UNSAFE
  745. * @stable ICU 2.4
  746. */
  747. #define U8_BACK_1(s, start, i) UPRV_BLOCK_MACRO_BEGIN { \
  748. if(U8_IS_TRAIL((s)[--(i)])) { \
  749. (i)=utf8_back1SafeBody(s, start, (i)); \
  750. } \
  751. } UPRV_BLOCK_MACRO_END
  752. /**
  753. * Move the string offset from one code point boundary to the n-th one before it,
  754. * i.e., move backward by n code points.
  755. * (Pre-decrementing backward iteration.)
  756. * The input offset may be the same as the string length.
  757. * "Unsafe" macro, assumes well-formed UTF-8.
  758. *
  759. * @param s const uint8_t * string
  760. * @param i string offset
  761. * @param n number of code points to skip
  762. * @see U8_BACK_N
  763. * @stable ICU 2.4
  764. */
  765. #define U8_BACK_N_UNSAFE(s, i, n) UPRV_BLOCK_MACRO_BEGIN { \
  766. int32_t __N=(n); \
  767. while(__N>0) { \
  768. U8_BACK_1_UNSAFE(s, i); \
  769. --__N; \
  770. } \
  771. } UPRV_BLOCK_MACRO_END
  772. /**
  773. * Move the string offset from one code point boundary to the n-th one before it,
  774. * i.e., move backward by n code points.
  775. * (Pre-decrementing backward iteration.)
  776. * The input offset may be the same as the string length.
  777. * "Safe" macro, checks for illegal sequences and for string boundaries.
  778. *
  779. * @param s const uint8_t * string
  780. * @param start int32_t index of the start of the string
  781. * @param i int32_t string offset, must be start<i
  782. * @param n number of code points to skip
  783. * @see U8_BACK_N_UNSAFE
  784. * @stable ICU 2.4
  785. */
  786. #define U8_BACK_N(s, start, i, n) UPRV_BLOCK_MACRO_BEGIN { \
  787. int32_t __N=(n); \
  788. while(__N>0 && (i)>(start)) { \
  789. U8_BACK_1(s, start, i); \
  790. --__N; \
  791. } \
  792. } UPRV_BLOCK_MACRO_END
  793. /**
  794. * Adjust a random-access offset to a code point boundary after a code point.
  795. * If the offset is behind a partial multi-byte sequence,
  796. * then the offset is incremented to behind the whole sequence.
  797. * Otherwise, it is not modified.
  798. * The input offset may be the same as the string length.
  799. * "Unsafe" macro, assumes well-formed UTF-8.
  800. *
  801. * @param s const uint8_t * string
  802. * @param i string offset
  803. * @see U8_SET_CP_LIMIT
  804. * @stable ICU 2.4
  805. */
  806. #define U8_SET_CP_LIMIT_UNSAFE(s, i) UPRV_BLOCK_MACRO_BEGIN { \
  807. U8_BACK_1_UNSAFE(s, i); \
  808. U8_FWD_1_UNSAFE(s, i); \
  809. } UPRV_BLOCK_MACRO_END
  810. /**
  811. * Adjust a random-access offset to a code point boundary after a code point.
  812. * If the offset is behind a partial multi-byte sequence,
  813. * then the offset is incremented to behind the whole sequence.
  814. * Otherwise, it is not modified.
  815. * The input offset may be the same as the string length.
  816. * "Safe" macro, checks for illegal sequences and for string boundaries.
  817. *
  818. * The length can be negative for a NUL-terminated string.
  819. *
  820. * @param s const uint8_t * string
  821. * @param start int32_t starting string offset (usually 0)
  822. * @param i int32_t string offset, must be start<=i<=length
  823. * @param length int32_t string length
  824. * @see U8_SET_CP_LIMIT_UNSAFE
  825. * @stable ICU 2.4
  826. */
  827. #define U8_SET_CP_LIMIT(s, start, i, length) UPRV_BLOCK_MACRO_BEGIN { \
  828. if((start)<(i) && ((i)<(length) || (length)<0)) { \
  829. U8_BACK_1(s, start, i); \
  830. U8_FWD_1(s, i, length); \
  831. } \
  832. } UPRV_BLOCK_MACRO_END
  833. #endif