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