utf8.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /*
  2. *******************************************************************************
  3. *
  4. * Copyright (C) 1999-2009, 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. * utf8.h is included by utf.h after unicode/umachine.h
  22. * and some common definitions.
  23. *
  24. * For more information see utf.h and the ICU User Guide Strings chapter
  25. * (http://icu-project.org/userguide/strings.html).
  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. /* utf.h must be included first. */
  35. #ifndef __UTF_H__
  36. # include "unicode/utf.h"
  37. #endif
  38. /* internal definitions ----------------------------------------------------- */
  39. /**
  40. * \var utf8_countTrailBytes
  41. * Internal array with numbers of trail bytes for any given byte used in
  42. * lead byte position.
  43. *
  44. * This is internal since it is not meant to be called directly by external clients;
  45. * however it is called by public macros in this file and thus must remain stable,
  46. * and should not be hidden when other internal functions are hidden (otherwise
  47. * public macros would fail to compile).
  48. * @internal
  49. */
  50. #ifdef U_UTF8_IMPL
  51. U_EXPORT const uint8_t
  52. #elif defined(U_STATIC_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION)
  53. U_CFUNC const uint8_t
  54. #else
  55. U_CFUNC U_IMPORT const uint8_t /* U_IMPORT2? */ /*U_IMPORT*/
  56. #endif
  57. utf8_countTrailBytes[256];
  58. /**
  59. * Count the trail bytes for a UTF-8 lead byte.
  60. *
  61. * This is internal since it is not meant to be called directly by external clients;
  62. * however it is called by public macros in this file and thus must remain stable.
  63. * @internal
  64. */
  65. #define U8_COUNT_TRAIL_BYTES(leadByte) (utf8_countTrailBytes[(uint8_t)leadByte])
  66. /**
  67. * Mask a UTF-8 lead byte, leave only the lower bits that form part of the code point value.
  68. *
  69. * This is internal since it is not meant to be called directly by external clients;
  70. * however it is called by public macros in this file and thus must remain stable.
  71. * @internal
  72. */
  73. #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
  74. /**
  75. * Function for handling "next code point" with error-checking.
  76. *
  77. * This is internal since it is not meant to be called directly by external clients;
  78. * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
  79. * file and thus must remain stable, and should not be hidden when other internal
  80. * functions are hidden (otherwise public macros would fail to compile).
  81. * @internal
  82. */
  83. U_STABLE UChar32 U_EXPORT2
  84. utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict);
  85. /**
  86. * Function for handling "append code point" with error-checking.
  87. *
  88. * This is internal since it is not meant to be called directly by external clients;
  89. * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
  90. * file and thus must remain stable, and should not be hidden when other internal
  91. * functions are hidden (otherwise public macros would fail to compile).
  92. * @internal
  93. */
  94. U_STABLE int32_t U_EXPORT2
  95. utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError);
  96. /**
  97. * Function for handling "previous code point" with error-checking.
  98. *
  99. * This is internal since it is not meant to be called directly by external clients;
  100. * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
  101. * file and thus must remain stable, and should not be hidden when other internal
  102. * functions are hidden (otherwise public macros would fail to compile).
  103. * @internal
  104. */
  105. U_STABLE UChar32 U_EXPORT2
  106. utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict);
  107. /**
  108. * Function for handling "skip backward one code point" with error-checking.
  109. *
  110. * This is internal since it is not meant to be called directly by external clients;
  111. * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
  112. * file and thus must remain stable, and should not be hidden when other internal
  113. * functions are hidden (otherwise public macros would fail to compile).
  114. * @internal
  115. */
  116. U_STABLE int32_t U_EXPORT2
  117. utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i);
  118. /* single-code point definitions -------------------------------------------- */
  119. /**
  120. * Does this code unit (byte) encode a code point by itself (US-ASCII 0..0x7f)?
  121. * @param c 8-bit code unit (byte)
  122. * @return TRUE or FALSE
  123. * @stable ICU 2.4
  124. */
  125. #define U8_IS_SINGLE(c) (((c)&0x80)==0)
  126. /**
  127. * Is this code unit (byte) a UTF-8 lead byte?
  128. * @param c 8-bit code unit (byte)
  129. * @return TRUE or FALSE
  130. * @stable ICU 2.4
  131. */
  132. #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc0)<0x3e)
  133. /**
  134. * Is this code unit (byte) a UTF-8 trail byte?
  135. * @param c 8-bit code unit (byte)
  136. * @return TRUE or FALSE
  137. * @stable ICU 2.4
  138. */
  139. #define U8_IS_TRAIL(c) (((c)&0xc0)==0x80)
  140. /**
  141. * How many code units (bytes) are used for the UTF-8 encoding
  142. * of this Unicode code point?
  143. * @param c 32-bit code point
  144. * @return 1..4, or 0 if c is a surrogate or not a Unicode code point
  145. * @stable ICU 2.4
  146. */
  147. #define U8_LENGTH(c) \
  148. ((uint32_t)(c)<=0x7f ? 1 : \
  149. ((uint32_t)(c)<=0x7ff ? 2 : \
  150. ((uint32_t)(c)<=0xd7ff ? 3 : \
  151. ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
  152. ((uint32_t)(c)<=0xffff ? 3 : 4)\
  153. ) \
  154. ) \
  155. ) \
  156. )
  157. /**
  158. * The maximum number of UTF-8 code units (bytes) per Unicode code point (U+0000..U+10ffff).
  159. * @return 4
  160. * @stable ICU 2.4
  161. */
  162. #define U8_MAX_LENGTH 4
  163. /**
  164. * Get a code point from a string at a random-access offset,
  165. * without changing the offset.
  166. * The offset may point to either the lead byte or one of the trail bytes
  167. * for a code point, in which case the macro will read all of the bytes
  168. * for the code point.
  169. * The result is undefined if the offset points to an illegal UTF-8
  170. * byte sequence.
  171. * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
  172. *
  173. * @param s const uint8_t * string
  174. * @param i string offset
  175. * @param c output UChar32 variable
  176. * @see U8_GET
  177. * @stable ICU 2.4
  178. */
  179. #define U8_GET_UNSAFE(s, i, c) { \
  180. int32_t _u8_get_unsafe_index=(int32_t)(i); \
  181. U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \
  182. U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \
  183. }
  184. /**
  185. * Get a code point from a string at a random-access offset,
  186. * without changing the offset.
  187. * The offset may point to either the lead byte or one of the trail bytes
  188. * for a code point, in which case the macro will read all of the bytes
  189. * for the code point.
  190. * If the offset points to an illegal UTF-8 byte sequence, then
  191. * c is set to a negative value.
  192. * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
  193. *
  194. * @param s const uint8_t * string
  195. * @param start starting string offset
  196. * @param i string offset, must be start<=i<length
  197. * @param length string length
  198. * @param c output UChar32 variable, set to <0 in case of an error
  199. * @see U8_GET_UNSAFE
  200. * @stable ICU 2.4
  201. */
  202. #define U8_GET(s, start, i, length, c) { \
  203. int32_t _u8_get_index=(int32_t)(i); \
  204. U8_SET_CP_START(s, start, _u8_get_index); \
  205. U8_NEXT(s, _u8_get_index, length, c); \
  206. }
  207. /* definitions with forward iteration --------------------------------------- */
  208. /**
  209. * Get a code point from a string at a code point boundary offset,
  210. * and advance the offset to the next code point boundary.
  211. * (Post-incrementing forward iteration.)
  212. * "Unsafe" macro, assumes well-formed UTF-8.
  213. *
  214. * The offset may point to the lead byte of a multi-byte sequence,
  215. * in which case the macro will read the whole sequence.
  216. * The result is undefined if the offset points to a trail byte
  217. * or an illegal UTF-8 sequence.
  218. *
  219. * @param s const uint8_t * string
  220. * @param i string offset
  221. * @param c output UChar32 variable
  222. * @see U8_NEXT
  223. * @stable ICU 2.4
  224. */
  225. #define U8_NEXT_UNSAFE(s, i, c) { \
  226. (c)=(uint8_t)(s)[(i)++]; \
  227. if((uint8_t)((c)-0xc0)<0x35) { \
  228. uint8_t __count=U8_COUNT_TRAIL_BYTES(c); \
  229. U8_MASK_LEAD_BYTE(c, __count); \
  230. switch(__count) { \
  231. /* each following branch falls through to the next one */ \
  232. case 3: \
  233. (c)=((c)<<6)|((s)[(i)++]&0x3f); \
  234. case 2: \
  235. (c)=((c)<<6)|((s)[(i)++]&0x3f); \
  236. case 1: \
  237. (c)=((c)<<6)|((s)[(i)++]&0x3f); \
  238. /* no other branches to optimize switch() */ \
  239. break; \
  240. } \
  241. } \
  242. }
  243. /**
  244. * Get a code point from a string at a code point boundary offset,
  245. * and advance the offset to the next code point boundary.
  246. * (Post-incrementing forward iteration.)
  247. * "Safe" macro, checks for illegal sequences and for string boundaries.
  248. *
  249. * The offset may point to the lead byte of a multi-byte sequence,
  250. * in which case the macro will read the whole sequence.
  251. * If the offset points to a trail byte or an illegal UTF-8 sequence, then
  252. * c is set to a negative value.
  253. *
  254. * @param s const uint8_t * string
  255. * @param i string offset, must be i<length
  256. * @param length string length
  257. * @param c output UChar32 variable, set to <0 in case of an error
  258. * @see U8_NEXT_UNSAFE
  259. * @stable ICU 2.4
  260. */
  261. #define U8_NEXT(s, i, length, c) { \
  262. (c)=(uint8_t)(s)[(i)++]; \
  263. if((c)>=0x80) { \
  264. uint8_t __t1, __t2; \
  265. if( /* handle U+1000..U+CFFF inline */ \
  266. (0xe0<(c) && (c)<=0xec) && \
  267. (((i)+1)<(length)) && \
  268. (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
  269. (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
  270. ) { \
  271. /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
  272. (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
  273. (i)+=2; \
  274. } else if( /* handle U+0080..U+07FF inline */ \
  275. ((c)<0xe0 && (c)>=0xc2) && \
  276. ((i)<(length)) && \
  277. (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
  278. ) { \
  279. (c)=(UChar)((((c)&0x1f)<<6)|__t1); \
  280. ++(i); \
  281. } else if(U8_IS_LEAD(c)) { \
  282. /* function call for "complicated" and error cases */ \
  283. (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (int32_t)(length), c, -1); \
  284. } else { \
  285. (c)=U_SENTINEL; \
  286. } \
  287. } \
  288. }
  289. /**
  290. * Append a code point to a string, overwriting 1 to 4 bytes.
  291. * The offset points to the current end of the string contents
  292. * and is advanced (post-increment).
  293. * "Unsafe" macro, assumes a valid code point and sufficient space in the string.
  294. * Otherwise, the result is undefined.
  295. *
  296. * @param s const uint8_t * string buffer
  297. * @param i string offset
  298. * @param c code point to append
  299. * @see U8_APPEND
  300. * @stable ICU 2.4
  301. */
  302. #define U8_APPEND_UNSAFE(s, i, c) { \
  303. if((uint32_t)(c)<=0x7f) { \
  304. (s)[(i)++]=(uint8_t)(c); \
  305. } else { \
  306. if((uint32_t)(c)<=0x7ff) { \
  307. (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
  308. } else { \
  309. if((uint32_t)(c)<=0xffff) { \
  310. (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
  311. } else { \
  312. (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
  313. (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
  314. } \
  315. (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
  316. } \
  317. (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
  318. } \
  319. }
  320. /**
  321. * Append a code point to a string, overwriting 1 to 4 bytes.
  322. * The offset points to the current end of the string contents
  323. * and is advanced (post-increment).
  324. * "Safe" macro, checks for a valid code point.
  325. * If a non-ASCII code point is written, checks for sufficient space in the string.
  326. * If the code point is not valid or trail bytes do not fit,
  327. * then isError is set to TRUE.
  328. *
  329. * @param s const uint8_t * string buffer
  330. * @param i string offset, must be i<capacity
  331. * @param capacity size of the string buffer
  332. * @param c code point to append
  333. * @param isError output UBool set to TRUE if an error occurs, otherwise not modified
  334. * @see U8_APPEND_UNSAFE
  335. * @stable ICU 2.4
  336. */
  337. #define U8_APPEND(s, i, capacity, c, isError) { \
  338. if((uint32_t)(c)<=0x7f) { \
  339. (s)[(i)++]=(uint8_t)(c); \
  340. } else if((uint32_t)(c)<=0x7ff && (i)+1<(capacity)) { \
  341. (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
  342. (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
  343. } else if((uint32_t)(c)<=0xd7ff && (i)+2<(capacity)) { \
  344. (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
  345. (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
  346. (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
  347. } else { \
  348. (i)=utf8_appendCharSafeBody(s, (int32_t)(i), (int32_t)(capacity), c, &(isError)); \
  349. } \
  350. }
  351. /**
  352. * Advance the string offset from one code point boundary to the next.
  353. * (Post-incrementing iteration.)
  354. * "Unsafe" macro, assumes well-formed UTF-8.
  355. *
  356. * @param s const uint8_t * string
  357. * @param i string offset
  358. * @see U8_FWD_1
  359. * @stable ICU 2.4
  360. */
  361. #define U8_FWD_1_UNSAFE(s, i) { \
  362. (i)+=1+U8_COUNT_TRAIL_BYTES((s)[i]); \
  363. }
  364. /**
  365. * Advance the string offset from one code point boundary to the next.
  366. * (Post-incrementing iteration.)
  367. * "Safe" macro, checks for illegal sequences and for string boundaries.
  368. *
  369. * @param s const uint8_t * string
  370. * @param i string offset, must be i<length
  371. * @param length string length
  372. * @see U8_FWD_1_UNSAFE
  373. * @stable ICU 2.4
  374. */
  375. #define U8_FWD_1(s, i, length) { \
  376. uint8_t __b=(uint8_t)(s)[(i)++]; \
  377. if(U8_IS_LEAD(__b)) { \
  378. uint8_t __count=U8_COUNT_TRAIL_BYTES(__b); \
  379. if((i)+__count>(length)) { \
  380. __count=(uint8_t)((length)-(i)); \
  381. } \
  382. while(__count>0 && U8_IS_TRAIL((s)[i])) { \
  383. ++(i); \
  384. --__count; \
  385. } \
  386. } \
  387. }
  388. /**
  389. * Advance the string offset from one code point boundary to the n-th next one,
  390. * i.e., move forward by n code points.
  391. * (Post-incrementing iteration.)
  392. * "Unsafe" macro, assumes well-formed UTF-8.
  393. *
  394. * @param s const uint8_t * string
  395. * @param i string offset
  396. * @param n number of code points to skip
  397. * @see U8_FWD_N
  398. * @stable ICU 2.4
  399. */
  400. #define U8_FWD_N_UNSAFE(s, i, n) { \
  401. int32_t __N=(n); \
  402. while(__N>0) { \
  403. U8_FWD_1_UNSAFE(s, i); \
  404. --__N; \
  405. } \
  406. }
  407. /**
  408. * Advance the string offset from one code point boundary to the n-th next one,
  409. * i.e., move forward by n code points.
  410. * (Post-incrementing iteration.)
  411. * "Safe" macro, checks for illegal sequences and for string boundaries.
  412. *
  413. * @param s const uint8_t * string
  414. * @param i string offset, must be i<length
  415. * @param length string length
  416. * @param n number of code points to skip
  417. * @see U8_FWD_N_UNSAFE
  418. * @stable ICU 2.4
  419. */
  420. #define U8_FWD_N(s, i, length, n) { \
  421. int32_t __N=(n); \
  422. while(__N>0 && (i)<(length)) { \
  423. U8_FWD_1(s, i, length); \
  424. --__N; \
  425. } \
  426. }
  427. /**
  428. * Adjust a random-access offset to a code point boundary
  429. * at the start of a code point.
  430. * If the offset points to a UTF-8 trail byte,
  431. * then the offset is moved backward to the corresponding lead byte.
  432. * Otherwise, it is not modified.
  433. * "Unsafe" macro, assumes well-formed UTF-8.
  434. *
  435. * @param s const uint8_t * string
  436. * @param i string offset
  437. * @see U8_SET_CP_START
  438. * @stable ICU 2.4
  439. */
  440. #define U8_SET_CP_START_UNSAFE(s, i) { \
  441. while(U8_IS_TRAIL((s)[i])) { --(i); } \
  442. }
  443. /**
  444. * Adjust a random-access offset to a code point boundary
  445. * at the start of a code point.
  446. * If the offset points to a UTF-8 trail byte,
  447. * then the offset is moved backward to the corresponding lead byte.
  448. * Otherwise, it is not modified.
  449. * "Safe" macro, checks for illegal sequences and for string boundaries.
  450. *
  451. * @param s const uint8_t * string
  452. * @param start starting string offset (usually 0)
  453. * @param i string offset, must be start<=i
  454. * @see U8_SET_CP_START_UNSAFE
  455. * @stable ICU 2.4
  456. */
  457. #define U8_SET_CP_START(s, start, i) { \
  458. if(U8_IS_TRAIL((s)[(i)])) { \
  459. (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
  460. } \
  461. }
  462. /* definitions with backward iteration -------------------------------------- */
  463. /**
  464. * Move the string offset from one code point boundary to the previous one
  465. * and get the code point between them.
  466. * (Pre-decrementing backward iteration.)
  467. * "Unsafe" macro, assumes well-formed UTF-8.
  468. *
  469. * The input offset may be the same as the string length.
  470. * If the offset is behind a multi-byte sequence, then the macro will read
  471. * the whole sequence.
  472. * If the offset is behind a lead byte, then that itself
  473. * will be returned as the code point.
  474. * The result is undefined if the offset is behind an illegal UTF-8 sequence.
  475. *
  476. * @param s const uint8_t * string
  477. * @param i string offset
  478. * @param c output UChar32 variable
  479. * @see U8_PREV
  480. * @stable ICU 2.4
  481. */
  482. #define U8_PREV_UNSAFE(s, i, c) { \
  483. (c)=(uint8_t)(s)[--(i)]; \
  484. if(U8_IS_TRAIL(c)) { \
  485. uint8_t __b, __count=1, __shift=6; \
  486. \
  487. /* c is a trail byte */ \
  488. (c)&=0x3f; \
  489. for(;;) { \
  490. __b=(uint8_t)(s)[--(i)]; \
  491. if(__b>=0xc0) { \
  492. U8_MASK_LEAD_BYTE(__b, __count); \
  493. (c)|=(UChar32)__b<<__shift; \
  494. break; \
  495. } else { \
  496. (c)|=(UChar32)(__b&0x3f)<<__shift; \
  497. ++__count; \
  498. __shift+=6; \
  499. } \
  500. } \
  501. } \
  502. }
  503. /**
  504. * Move the string offset from one code point boundary to the previous one
  505. * and get the code point between them.
  506. * (Pre-decrementing backward iteration.)
  507. * "Safe" macro, checks for illegal sequences and for string boundaries.
  508. *
  509. * The input offset may be the same as the string length.
  510. * If the offset is behind a multi-byte sequence, then the macro will read
  511. * the whole sequence.
  512. * If the offset is behind a lead byte, then that itself
  513. * will be returned as the code point.
  514. * If the offset is behind an illegal UTF-8 sequence, then c is set to a negative value.
  515. *
  516. * @param s const uint8_t * string
  517. * @param start starting string offset (usually 0)
  518. * @param i string offset, must be start<i
  519. * @param c output UChar32 variable, set to <0 in case of an error
  520. * @see U8_PREV_UNSAFE
  521. * @stable ICU 2.4
  522. */
  523. #define U8_PREV(s, start, i, c) { \
  524. (c)=(uint8_t)(s)[--(i)]; \
  525. if((c)>=0x80) { \
  526. if((c)<=0xbf) { \
  527. (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
  528. } else { \
  529. (c)=U_SENTINEL; \
  530. } \
  531. } \
  532. }
  533. /**
  534. * Move the string offset from one code point boundary to the previous one.
  535. * (Pre-decrementing backward iteration.)
  536. * The input offset may be the same as the string length.
  537. * "Unsafe" macro, assumes well-formed UTF-8.
  538. *
  539. * @param s const uint8_t * string
  540. * @param i string offset
  541. * @see U8_BACK_1
  542. * @stable ICU 2.4
  543. */
  544. #define U8_BACK_1_UNSAFE(s, i) { \
  545. while(U8_IS_TRAIL((s)[--(i)])) {} \
  546. }
  547. /**
  548. * Move the string offset from one code point boundary to the previous one.
  549. * (Pre-decrementing backward iteration.)
  550. * The input offset may be the same as the string length.
  551. * "Safe" macro, checks for illegal sequences and for string boundaries.
  552. *
  553. * @param s const uint8_t * string
  554. * @param start starting string offset (usually 0)
  555. * @param i string offset, must be start<i
  556. * @see U8_BACK_1_UNSAFE
  557. * @stable ICU 2.4
  558. */
  559. #define U8_BACK_1(s, start, i) { \
  560. if(U8_IS_TRAIL((s)[--(i)])) { \
  561. (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
  562. } \
  563. }
  564. /**
  565. * Move the string offset from one code point boundary to the n-th one before it,
  566. * i.e., move backward by n code points.
  567. * (Pre-decrementing backward iteration.)
  568. * The input offset may be the same as the string length.
  569. * "Unsafe" macro, assumes well-formed UTF-8.
  570. *
  571. * @param s const uint8_t * string
  572. * @param i string offset
  573. * @param n number of code points to skip
  574. * @see U8_BACK_N
  575. * @stable ICU 2.4
  576. */
  577. #define U8_BACK_N_UNSAFE(s, i, n) { \
  578. int32_t __N=(n); \
  579. while(__N>0) { \
  580. U8_BACK_1_UNSAFE(s, i); \
  581. --__N; \
  582. } \
  583. }
  584. /**
  585. * Move the string offset from one code point boundary to the n-th one before it,
  586. * i.e., move backward by n code points.
  587. * (Pre-decrementing backward iteration.)
  588. * The input offset may be the same as the string length.
  589. * "Safe" macro, checks for illegal sequences and for string boundaries.
  590. *
  591. * @param s const uint8_t * string
  592. * @param start index of the start of the string
  593. * @param i string offset, must be start<i
  594. * @param n number of code points to skip
  595. * @see U8_BACK_N_UNSAFE
  596. * @stable ICU 2.4
  597. */
  598. #define U8_BACK_N(s, start, i, n) { \
  599. int32_t __N=(n); \
  600. while(__N>0 && (i)>(start)) { \
  601. U8_BACK_1(s, start, i); \
  602. --__N; \
  603. } \
  604. }
  605. /**
  606. * Adjust a random-access offset to a code point boundary after a code point.
  607. * If the offset is behind a partial multi-byte sequence,
  608. * then the offset is incremented to behind the whole sequence.
  609. * Otherwise, it is not modified.
  610. * The input offset may be the same as the string length.
  611. * "Unsafe" macro, assumes well-formed UTF-8.
  612. *
  613. * @param s const uint8_t * string
  614. * @param i string offset
  615. * @see U8_SET_CP_LIMIT
  616. * @stable ICU 2.4
  617. */
  618. #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \
  619. U8_BACK_1_UNSAFE(s, i); \
  620. U8_FWD_1_UNSAFE(s, i); \
  621. }
  622. /**
  623. * Adjust a random-access offset to a code point boundary after a code point.
  624. * If the offset is behind a partial multi-byte sequence,
  625. * then the offset is incremented to behind the whole sequence.
  626. * Otherwise, it is not modified.
  627. * The input offset may be the same as the string length.
  628. * "Safe" macro, checks for illegal sequences and for string boundaries.
  629. *
  630. * @param s const uint8_t * string
  631. * @param start starting string offset (usually 0)
  632. * @param i string offset, must be start<=i<=length
  633. * @param length string length
  634. * @see U8_SET_CP_LIMIT_UNSAFE
  635. * @stable ICU 2.4
  636. */
  637. #define U8_SET_CP_LIMIT(s, start, i, length) { \
  638. if((start)<(i) && (i)<(length)) { \
  639. U8_BACK_1(s, start, i); \
  640. U8_FWD_1(s, i, length); \
  641. } \
  642. }
  643. #endif