utf16.h 19 KB

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