utf8.h 28 KB

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