utf16.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. // Copyright (C) 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-2012, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. *******************************************************************************
  10. * file name: utf16.h
  11. * encoding: US-ASCII
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 1999sep09
  16. * created by: Markus W. Scherer
  17. */
  18. /**
  19. * \file
  20. * \brief C API: 16-bit Unicode handling macros
  21. *
  22. * This file defines macros to deal with 16-bit Unicode (UTF-16) code units and strings.
  23. *
  24. * For more information see utf.h and the ICU User Guide Strings chapter
  25. * (http://userguide.icu-project.org/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 __UTF16_H__
  33. #define __UTF16_H__
  34. #include "unicode/umachine.h"
  35. #ifndef __UTF_H__
  36. # include "unicode/utf.h"
  37. #endif
  38. /* single-code point definitions -------------------------------------------- */
  39. /**
  40. * Does this code unit alone encode a code point (BMP, not a surrogate)?
  41. * @param c 16-bit code unit
  42. * @return TRUE or FALSE
  43. * @stable ICU 2.4
  44. */
  45. #define U16_IS_SINGLE(c) !U_IS_SURROGATE(c)
  46. /**
  47. * Is this code unit a lead surrogate (U+d800..U+dbff)?
  48. * @param c 16-bit code unit
  49. * @return TRUE or FALSE
  50. * @stable ICU 2.4
  51. */
  52. #define U16_IS_LEAD(c) (((c)&0xfffffc00)==0xd800)
  53. /**
  54. * Is this code unit a trail surrogate (U+dc00..U+dfff)?
  55. * @param c 16-bit code unit
  56. * @return TRUE or FALSE
  57. * @stable ICU 2.4
  58. */
  59. #define U16_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00)
  60. /**
  61. * Is this code unit a surrogate (U+d800..U+dfff)?
  62. * @param c 16-bit code unit
  63. * @return TRUE or FALSE
  64. * @stable ICU 2.4
  65. */
  66. #define U16_IS_SURROGATE(c) U_IS_SURROGATE(c)
  67. /**
  68. * Assuming c is a surrogate code point (U16_IS_SURROGATE(c)),
  69. * is it a lead surrogate?
  70. * @param c 16-bit code unit
  71. * @return TRUE or FALSE
  72. * @stable ICU 2.4
  73. */
  74. #define U16_IS_SURROGATE_LEAD(c) (((c)&0x400)==0)
  75. /**
  76. * Assuming c is a surrogate code point (U16_IS_SURROGATE(c)),
  77. * is it a trail surrogate?
  78. * @param c 16-bit code unit
  79. * @return TRUE or FALSE
  80. * @stable ICU 4.2
  81. */
  82. #define U16_IS_SURROGATE_TRAIL(c) (((c)&0x400)!=0)
  83. /**
  84. * Helper constant for U16_GET_SUPPLEMENTARY.
  85. * @internal
  86. */
  87. #define U16_SURROGATE_OFFSET ((0xd800<<10UL)+0xdc00-0x10000)
  88. /**
  89. * Get a supplementary code point value (U+10000..U+10ffff)
  90. * from its lead and trail surrogates.
  91. * The result is undefined if the input values are not
  92. * lead and trail surrogates.
  93. *
  94. * @param lead lead surrogate (U+d800..U+dbff)
  95. * @param trail trail surrogate (U+dc00..U+dfff)
  96. * @return supplementary code point (U+10000..U+10ffff)
  97. * @stable ICU 2.4
  98. */
  99. #define U16_GET_SUPPLEMENTARY(lead, trail) \
  100. (((UChar32)(lead)<<10UL)+(UChar32)(trail)-U16_SURROGATE_OFFSET)
  101. /**
  102. * Get the lead surrogate (0xd800..0xdbff) for a
  103. * supplementary code point (0x10000..0x10ffff).
  104. * @param supplementary 32-bit code point (U+10000..U+10ffff)
  105. * @return lead surrogate (U+d800..U+dbff) for supplementary
  106. * @stable ICU 2.4
  107. */
  108. #define U16_LEAD(supplementary) (UChar)(((supplementary)>>10)+0xd7c0)
  109. /**
  110. * Get the trail surrogate (0xdc00..0xdfff) for a
  111. * supplementary code point (0x10000..0x10ffff).
  112. * @param supplementary 32-bit code point (U+10000..U+10ffff)
  113. * @return trail surrogate (U+dc00..U+dfff) for supplementary
  114. * @stable ICU 2.4
  115. */
  116. #define U16_TRAIL(supplementary) (UChar)(((supplementary)&0x3ff)|0xdc00)
  117. /**
  118. * How many 16-bit code units are used to encode this Unicode code point? (1 or 2)
  119. * The result is not defined if c is not a Unicode code point (U+0000..U+10ffff).
  120. * @param c 32-bit code point
  121. * @return 1 or 2
  122. * @stable ICU 2.4
  123. */
  124. #define U16_LENGTH(c) ((uint32_t)(c)<=0xffff ? 1 : 2)
  125. /**
  126. * The maximum number of 16-bit code units per Unicode code point (U+0000..U+10ffff).
  127. * @return 2
  128. * @stable ICU 2.4
  129. */
  130. #define U16_MAX_LENGTH 2
  131. /**
  132. * Get a code point from a string at a random-access offset,
  133. * without changing the offset.
  134. * "Unsafe" macro, assumes well-formed UTF-16.
  135. *
  136. * The offset may point to either the lead or trail surrogate unit
  137. * for a supplementary code point, in which case the macro will read
  138. * the adjacent matching surrogate as well.
  139. * The result is undefined if the offset points to a single, unpaired surrogate.
  140. * Iteration through a string is more efficient with U16_NEXT_UNSAFE or U16_NEXT.
  141. *
  142. * @param s const UChar * string
  143. * @param i string offset
  144. * @param c output UChar32 variable
  145. * @see U16_GET
  146. * @stable ICU 2.4
  147. */
  148. #define U16_GET_UNSAFE(s, i, c) { \
  149. (c)=(s)[i]; \
  150. if(U16_IS_SURROGATE(c)) { \
  151. if(U16_IS_SURROGATE_LEAD(c)) { \
  152. (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)+1]); \
  153. } else { \
  154. (c)=U16_GET_SUPPLEMENTARY((s)[(i)-1], (c)); \
  155. } \
  156. } \
  157. }
  158. /**
  159. * Get a code point from a string at a random-access offset,
  160. * without changing the offset.
  161. * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
  162. *
  163. * The offset may point to either the lead or trail surrogate unit
  164. * for a supplementary code point, in which case the macro will read
  165. * the adjacent matching surrogate as well.
  166. *
  167. * The length can be negative for a NUL-terminated string.
  168. *
  169. * If the offset points to a single, unpaired surrogate, then that itself
  170. * will be returned as the code point.
  171. * Iteration through a string is more efficient with U16_NEXT_UNSAFE or U16_NEXT.
  172. *
  173. * @param s const UChar * string
  174. * @param start starting string offset (usually 0)
  175. * @param i string offset, must be start<=i<length
  176. * @param length string length
  177. * @param c output UChar32 variable
  178. * @see U16_GET_UNSAFE
  179. * @stable ICU 2.4
  180. */
  181. #define U16_GET(s, start, i, length, c) { \
  182. (c)=(s)[i]; \
  183. if(U16_IS_SURROGATE(c)) { \
  184. uint16_t __c2; \
  185. if(U16_IS_SURROGATE_LEAD(c)) { \
  186. if((i)+1!=(length) && U16_IS_TRAIL(__c2=(s)[(i)+1])) { \
  187. (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
  188. } \
  189. } else { \
  190. if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
  191. (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
  192. } \
  193. } \
  194. } \
  195. }
  196. /* definitions with forward iteration --------------------------------------- */
  197. /**
  198. * Get a code point from a string at a code point boundary offset,
  199. * and advance the offset to the next code point boundary.
  200. * (Post-incrementing forward iteration.)
  201. * "Unsafe" macro, assumes well-formed UTF-16.
  202. *
  203. * The offset may point to the lead surrogate unit
  204. * for a supplementary code point, in which case the macro will read
  205. * the following trail surrogate as well.
  206. * If the offset points to a trail surrogate, then that itself
  207. * will be returned as the code point.
  208. * The result is undefined if the offset points to a single, unpaired lead surrogate.
  209. *
  210. * @param s const UChar * string
  211. * @param i string offset
  212. * @param c output UChar32 variable
  213. * @see U16_NEXT
  214. * @stable ICU 2.4
  215. */
  216. #define U16_NEXT_UNSAFE(s, i, c) { \
  217. (c)=(s)[(i)++]; \
  218. if(U16_IS_LEAD(c)) { \
  219. (c)=U16_GET_SUPPLEMENTARY((c), (s)[(i)++]); \
  220. } \
  221. }
  222. /**
  223. * Get a code point from a string at a code point boundary offset,
  224. * and advance the offset to the next code point boundary.
  225. * (Post-incrementing forward iteration.)
  226. * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
  227. *
  228. * The length can be negative for a NUL-terminated string.
  229. *
  230. * The offset may point to the lead surrogate unit
  231. * for a supplementary code point, in which case the macro will read
  232. * the following trail surrogate as well.
  233. * If the offset points to a trail surrogate or
  234. * to a single, unpaired lead surrogate, then that itself
  235. * will be returned as the code point.
  236. *
  237. * @param s const UChar * string
  238. * @param i string offset, must be i<length
  239. * @param length string length
  240. * @param c output UChar32 variable
  241. * @see U16_NEXT_UNSAFE
  242. * @stable ICU 2.4
  243. */
  244. #define U16_NEXT(s, i, length, c) { \
  245. (c)=(s)[(i)++]; \
  246. if(U16_IS_LEAD(c)) { \
  247. uint16_t __c2; \
  248. if((i)!=(length) && U16_IS_TRAIL(__c2=(s)[(i)])) { \
  249. ++(i); \
  250. (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
  251. } \
  252. } \
  253. }
  254. /**
  255. * Append a code point to a string, overwriting 1 or 2 code units.
  256. * The offset points to the current end of the string contents
  257. * and is advanced (post-increment).
  258. * "Unsafe" macro, assumes a valid code point and sufficient space in the string.
  259. * Otherwise, the result is undefined.
  260. *
  261. * @param s const UChar * string buffer
  262. * @param i string offset
  263. * @param c code point to append
  264. * @see U16_APPEND
  265. * @stable ICU 2.4
  266. */
  267. #define U16_APPEND_UNSAFE(s, i, c) { \
  268. if((uint32_t)(c)<=0xffff) { \
  269. (s)[(i)++]=(uint16_t)(c); \
  270. } else { \
  271. (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
  272. (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
  273. } \
  274. }
  275. /**
  276. * Append a code point to a string, overwriting 1 or 2 code units.
  277. * The offset points to the current end of the string contents
  278. * and is advanced (post-increment).
  279. * "Safe" macro, checks for a valid code point.
  280. * If a surrogate pair is written, checks for sufficient space in the string.
  281. * If the code point is not valid or a trail surrogate does not fit,
  282. * then isError is set to TRUE.
  283. *
  284. * @param s const UChar * string buffer
  285. * @param i string offset, must be i<capacity
  286. * @param capacity size of the string buffer
  287. * @param c code point to append
  288. * @param isError output UBool set to TRUE if an error occurs, otherwise not modified
  289. * @see U16_APPEND_UNSAFE
  290. * @stable ICU 2.4
  291. */
  292. #define U16_APPEND(s, i, capacity, c, isError) { \
  293. if((uint32_t)(c)<=0xffff) { \
  294. (s)[(i)++]=(uint16_t)(c); \
  295. } else if((uint32_t)(c)<=0x10ffff && (i)+1<(capacity)) { \
  296. (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
  297. (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
  298. } else /* c>0x10ffff or not enough space */ { \
  299. (isError)=TRUE; \
  300. } \
  301. }
  302. /**
  303. * Advance the string offset from one code point boundary to the next.
  304. * (Post-incrementing iteration.)
  305. * "Unsafe" macro, assumes well-formed UTF-16.
  306. *
  307. * @param s const UChar * string
  308. * @param i string offset
  309. * @see U16_FWD_1
  310. * @stable ICU 2.4
  311. */
  312. #define U16_FWD_1_UNSAFE(s, i) { \
  313. if(U16_IS_LEAD((s)[(i)++])) { \
  314. ++(i); \
  315. } \
  316. }
  317. /**
  318. * Advance the string offset from one code point boundary to the next.
  319. * (Post-incrementing iteration.)
  320. * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
  321. *
  322. * The length can be negative for a NUL-terminated string.
  323. *
  324. * @param s const UChar * string
  325. * @param i string offset, must be i<length
  326. * @param length string length
  327. * @see U16_FWD_1_UNSAFE
  328. * @stable ICU 2.4
  329. */
  330. #define U16_FWD_1(s, i, length) { \
  331. if(U16_IS_LEAD((s)[(i)++]) && (i)!=(length) && U16_IS_TRAIL((s)[i])) { \
  332. ++(i); \
  333. } \
  334. }
  335. /**
  336. * Advance the string offset from one code point boundary to the n-th next one,
  337. * i.e., move forward by n code points.
  338. * (Post-incrementing iteration.)
  339. * "Unsafe" macro, assumes well-formed UTF-16.
  340. *
  341. * @param s const UChar * string
  342. * @param i string offset
  343. * @param n number of code points to skip
  344. * @see U16_FWD_N
  345. * @stable ICU 2.4
  346. */
  347. #define U16_FWD_N_UNSAFE(s, i, n) { \
  348. int32_t __N=(n); \
  349. while(__N>0) { \
  350. U16_FWD_1_UNSAFE(s, i); \
  351. --__N; \
  352. } \
  353. }
  354. /**
  355. * Advance the string offset from one code point boundary to the n-th next one,
  356. * i.e., move forward by n code points.
  357. * (Post-incrementing iteration.)
  358. * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
  359. *
  360. * The length can be negative for a NUL-terminated string.
  361. *
  362. * @param s const UChar * string
  363. * @param i int32_t string offset, must be i<length
  364. * @param length int32_t string length
  365. * @param n number of code points to skip
  366. * @see U16_FWD_N_UNSAFE
  367. * @stable ICU 2.4
  368. */
  369. #define U16_FWD_N(s, i, length, n) { \
  370. int32_t __N=(n); \
  371. while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
  372. U16_FWD_1(s, i, length); \
  373. --__N; \
  374. } \
  375. }
  376. /**
  377. * Adjust a random-access offset to a code point boundary
  378. * at the start of a code point.
  379. * If the offset points to the trail surrogate of a surrogate pair,
  380. * then the offset is decremented.
  381. * Otherwise, it is not modified.
  382. * "Unsafe" macro, assumes well-formed UTF-16.
  383. *
  384. * @param s const UChar * string
  385. * @param i string offset
  386. * @see U16_SET_CP_START
  387. * @stable ICU 2.4
  388. */
  389. #define U16_SET_CP_START_UNSAFE(s, i) { \
  390. if(U16_IS_TRAIL((s)[i])) { \
  391. --(i); \
  392. } \
  393. }
  394. /**
  395. * Adjust a random-access offset to a code point boundary
  396. * at the start of a code point.
  397. * If the offset points to the trail surrogate of a surrogate pair,
  398. * then the offset is decremented.
  399. * Otherwise, it is not modified.
  400. * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
  401. *
  402. * @param s const UChar * string
  403. * @param start starting string offset (usually 0)
  404. * @param i string offset, must be start<=i
  405. * @see U16_SET_CP_START_UNSAFE
  406. * @stable ICU 2.4
  407. */
  408. #define U16_SET_CP_START(s, start, i) { \
  409. if(U16_IS_TRAIL((s)[i]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
  410. --(i); \
  411. } \
  412. }
  413. /* definitions with backward iteration -------------------------------------- */
  414. /**
  415. * Move the string offset from one code point boundary to the previous one
  416. * and get the code point between them.
  417. * (Pre-decrementing backward iteration.)
  418. * "Unsafe" macro, assumes well-formed UTF-16.
  419. *
  420. * The input offset may be the same as the string length.
  421. * If the offset is behind a trail surrogate unit
  422. * for a supplementary code point, then the macro will read
  423. * the preceding lead surrogate as well.
  424. * If the offset is behind a lead surrogate, then that itself
  425. * will be returned as the code point.
  426. * The result is undefined if the offset is behind a single, unpaired trail surrogate.
  427. *
  428. * @param s const UChar * string
  429. * @param i string offset
  430. * @param c output UChar32 variable
  431. * @see U16_PREV
  432. * @stable ICU 2.4
  433. */
  434. #define U16_PREV_UNSAFE(s, i, c) { \
  435. (c)=(s)[--(i)]; \
  436. if(U16_IS_TRAIL(c)) { \
  437. (c)=U16_GET_SUPPLEMENTARY((s)[--(i)], (c)); \
  438. } \
  439. }
  440. /**
  441. * Move the string offset from one code point boundary to the previous one
  442. * and get the code point between them.
  443. * (Pre-decrementing backward iteration.)
  444. * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
  445. *
  446. * The input offset may be the same as the string length.
  447. * If the offset is behind a trail surrogate unit
  448. * for a supplementary code point, then the macro will read
  449. * the preceding lead surrogate as well.
  450. * If the offset is behind a lead surrogate or behind a single, unpaired
  451. * trail surrogate, then that itself
  452. * will be returned as the code point.
  453. *
  454. * @param s const UChar * string
  455. * @param start starting string offset (usually 0)
  456. * @param i string offset, must be start<i
  457. * @param c output UChar32 variable
  458. * @see U16_PREV_UNSAFE
  459. * @stable ICU 2.4
  460. */
  461. #define U16_PREV(s, start, i, c) { \
  462. (c)=(s)[--(i)]; \
  463. if(U16_IS_TRAIL(c)) { \
  464. uint16_t __c2; \
  465. if((i)>(start) && U16_IS_LEAD(__c2=(s)[(i)-1])) { \
  466. --(i); \
  467. (c)=U16_GET_SUPPLEMENTARY(__c2, (c)); \
  468. } \
  469. } \
  470. }
  471. /**
  472. * Move the string offset from one code point boundary to the previous one.
  473. * (Pre-decrementing backward iteration.)
  474. * The input offset may be the same as the string length.
  475. * "Unsafe" macro, assumes well-formed UTF-16.
  476. *
  477. * @param s const UChar * string
  478. * @param i string offset
  479. * @see U16_BACK_1
  480. * @stable ICU 2.4
  481. */
  482. #define U16_BACK_1_UNSAFE(s, i) { \
  483. if(U16_IS_TRAIL((s)[--(i)])) { \
  484. --(i); \
  485. } \
  486. }
  487. /**
  488. * Move the string offset from one code point boundary to the previous one.
  489. * (Pre-decrementing backward iteration.)
  490. * The input offset may be the same as the string length.
  491. * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
  492. *
  493. * @param s const UChar * string
  494. * @param start starting string offset (usually 0)
  495. * @param i string offset, must be start<i
  496. * @see U16_BACK_1_UNSAFE
  497. * @stable ICU 2.4
  498. */
  499. #define U16_BACK_1(s, start, i) { \
  500. if(U16_IS_TRAIL((s)[--(i)]) && (i)>(start) && U16_IS_LEAD((s)[(i)-1])) { \
  501. --(i); \
  502. } \
  503. }
  504. /**
  505. * Move the string offset from one code point boundary to the n-th one before it,
  506. * i.e., move backward by n code points.
  507. * (Pre-decrementing backward iteration.)
  508. * The input offset may be the same as the string length.
  509. * "Unsafe" macro, assumes well-formed UTF-16.
  510. *
  511. * @param s const UChar * string
  512. * @param i string offset
  513. * @param n number of code points to skip
  514. * @see U16_BACK_N
  515. * @stable ICU 2.4
  516. */
  517. #define U16_BACK_N_UNSAFE(s, i, n) { \
  518. int32_t __N=(n); \
  519. while(__N>0) { \
  520. U16_BACK_1_UNSAFE(s, i); \
  521. --__N; \
  522. } \
  523. }
  524. /**
  525. * Move the string offset from one code point boundary to the n-th one before it,
  526. * i.e., move backward by n code points.
  527. * (Pre-decrementing backward iteration.)
  528. * The input offset may be the same as the string length.
  529. * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
  530. *
  531. * @param s const UChar * string
  532. * @param start start of string
  533. * @param i string offset, must be start<i
  534. * @param n number of code points to skip
  535. * @see U16_BACK_N_UNSAFE
  536. * @stable ICU 2.4
  537. */
  538. #define U16_BACK_N(s, start, i, n) { \
  539. int32_t __N=(n); \
  540. while(__N>0 && (i)>(start)) { \
  541. U16_BACK_1(s, start, i); \
  542. --__N; \
  543. } \
  544. }
  545. /**
  546. * Adjust a random-access offset to a code point boundary after a code point.
  547. * If the offset is behind the lead surrogate of a surrogate pair,
  548. * then the offset is incremented.
  549. * Otherwise, it is not modified.
  550. * The input offset may be the same as the string length.
  551. * "Unsafe" macro, assumes well-formed UTF-16.
  552. *
  553. * @param s const UChar * string
  554. * @param i string offset
  555. * @see U16_SET_CP_LIMIT
  556. * @stable ICU 2.4
  557. */
  558. #define U16_SET_CP_LIMIT_UNSAFE(s, i) { \
  559. if(U16_IS_LEAD((s)[(i)-1])) { \
  560. ++(i); \
  561. } \
  562. }
  563. /**
  564. * Adjust a random-access offset to a code point boundary after a code point.
  565. * If the offset is behind the lead surrogate of a surrogate pair,
  566. * then the offset is incremented.
  567. * Otherwise, it is not modified.
  568. * The input offset may be the same as the string length.
  569. * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
  570. *
  571. * The length can be negative for a NUL-terminated string.
  572. *
  573. * @param s const UChar * string
  574. * @param start int32_t starting string offset (usually 0)
  575. * @param i int32_t string offset, start<=i<=length
  576. * @param length int32_t string length
  577. * @see U16_SET_CP_LIMIT_UNSAFE
  578. * @stable ICU 2.4
  579. */
  580. #define U16_SET_CP_LIMIT(s, start, i, length) { \
  581. if((start)<(i) && ((i)<(length) || (length)<0) && U16_IS_LEAD((s)[(i)-1]) && U16_IS_TRAIL((s)[i])) { \
  582. ++(i); \
  583. } \
  584. }
  585. #endif