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