• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *******************************************************************************
3 *
4 *   Copyright (C) 1999-2013, International Business Machines
5 *   Corporation and others.  All Rights Reserved.
6 *
7 *******************************************************************************
8 *   file name:  utf8.h
9 *   encoding:   US-ASCII
10 *   tab size:   8 (not used)
11 *   indentation:4
12 *
13 *   created on: 1999sep13
14 *   created by: Markus W. Scherer
15 */
16 
17 /**
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.
22  *
23  * For more information see utf.h and the ICU User Guide Strings chapter
24  * (http://userguide.icu-project.org/strings).
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 
32 #ifndef __UTF8_H__
33 #define __UTF8_H__
34 
35 #include "unicode/umachine.h"
36 #ifndef __UTF_H__
37 #   include "unicode/utf.h"
38 #endif
39 
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.
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).
51  * @internal
52  */
53 #ifdef U_UTF8_IMPL
54 U_EXPORT const uint8_t
55 #elif defined(U_STATIC_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION)
56 U_CFUNC const uint8_t
57 #else
58 U_CFUNC U_IMPORT const uint8_t /* U_IMPORT2? */ /*U_IMPORT*/
59 #endif
60 utf8_countTrailBytes[256];
61 
62 /**
63  * Counts the trail bytes for a UTF-8 lead byte.
64  * Returns 0 for 0..0xbf as well as for 0xfe and 0xff.
65  *
66  * This is internal since it is not meant to be called directly by external clients;
67  * however it is called by public macros in this file and thus must remain stable.
68  *
69  * Note: Beginning with ICU 50, the implementation uses a multi-condition expression
70  * which was shown in 2012 (on x86-64) to compile to fast, branch-free code.
71  * leadByte is evaluated multiple times.
72  *
73  * The pre-ICU 50 implementation used the exported array utf8_countTrailBytes:
74  * #define U8_COUNT_TRAIL_BYTES(leadByte) (utf8_countTrailBytes[leadByte])
75  * leadByte was evaluated exactly once.
76  *
77  * @param leadByte The first byte of a UTF-8 sequence. Must be 0..0xff.
78  * @internal
79  */
80 #define U8_COUNT_TRAIL_BYTES(leadByte) \
81     ((leadByte)<0xf0 ? \
82         ((leadByte)>=0xc0)+((leadByte)>=0xe0) : \
83         (leadByte)<0xfe ? 3+((leadByte)>=0xf8)+((leadByte)>=0xfc) : 0)
84 
85 /**
86  * Counts the trail bytes for a UTF-8 lead byte of a valid UTF-8 sequence.
87  * The maximum supported lead byte is 0xf4 corresponding to U+10FFFF.
88  * leadByte might be evaluated multiple times.
89  *
90  * This is internal since it is not meant to be called directly by external clients;
91  * however it is called by public macros in this file and thus must remain stable.
92  *
93  * @param leadByte The first byte of a UTF-8 sequence. Must be 0..0xff.
94  * @internal
95  */
96 #define U8_COUNT_TRAIL_BYTES_UNSAFE(leadByte) \
97     (((leadByte)>=0xc0)+((leadByte)>=0xe0)+((leadByte)>=0xf0))
98 
99 /**
100  * Mask a UTF-8 lead byte, leave only the lower bits that form part of the code point value.
101  *
102  * This is internal since it is not meant to be called directly by external clients;
103  * however it is called by public macros in this file and thus must remain stable.
104  * @internal
105  */
106 #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
107 
108 /**
109  * Function for handling "next code point" with error-checking.
110  *
111  * This is internal since it is not meant to be called directly by external clients;
112  * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
113  * file and thus must remain stable, and should not be hidden when other internal
114  * functions are hidden (otherwise public macros would fail to compile).
115  * @internal
116  */
117 U_STABLE UChar32 U_EXPORT2
118 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict);
119 
120 /**
121  * Function for handling "append code point" with error-checking.
122  *
123  * This is internal since it is not meant to be called directly by external clients;
124  * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
125  * file and thus must remain stable, and should not be hidden when other internal
126  * functions are hidden (otherwise public macros would fail to compile).
127  * @internal
128  */
129 U_STABLE int32_t U_EXPORT2
130 utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError);
131 
132 /**
133  * Function for handling "previous code point" with error-checking.
134  *
135  * This is internal since it is not meant to be called directly by external clients;
136  * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
137  * file and thus must remain stable, and should not be hidden when other internal
138  * functions are hidden (otherwise public macros would fail to compile).
139  * @internal
140  */
141 U_STABLE UChar32 U_EXPORT2
142 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict);
143 
144 /**
145  * Function for handling "skip backward one code point" with error-checking.
146  *
147  * This is internal since it is not meant to be called directly by external clients;
148  * however it is U_STABLE (not U_INTERNAL) since it is called by public macros in this
149  * file and thus must remain stable, and should not be hidden when other internal
150  * functions are hidden (otherwise public macros would fail to compile).
151  * @internal
152  */
153 U_STABLE int32_t U_EXPORT2
154 utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i);
155 
156 /* single-code point definitions -------------------------------------------- */
157 
158 /**
159  * Does this code unit (byte) encode a code point by itself (US-ASCII 0..0x7f)?
160  * @param c 8-bit code unit (byte)
161  * @return TRUE or FALSE
162  * @stable ICU 2.4
163  */
164 #define U8_IS_SINGLE(c) (((c)&0x80)==0)
165 
166 /**
167  * Is this code unit (byte) a UTF-8 lead byte?
168  * @param c 8-bit code unit (byte)
169  * @return TRUE or FALSE
170  * @stable ICU 2.4
171  */
172 #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc0)<0x3e)
173 
174 /**
175  * Is this code unit (byte) a UTF-8 trail byte?
176  * @param c 8-bit code unit (byte)
177  * @return TRUE or FALSE
178  * @stable ICU 2.4
179  */
180 #define U8_IS_TRAIL(c) (((c)&0xc0)==0x80)
181 
182 /**
183  * How many code units (bytes) are used for the UTF-8 encoding
184  * of this Unicode code point?
185  * @param c 32-bit code point
186  * @return 1..4, or 0 if c is a surrogate or not a Unicode code point
187  * @stable ICU 2.4
188  */
189 #define U8_LENGTH(c) \
190     ((uint32_t)(c)<=0x7f ? 1 : \
191         ((uint32_t)(c)<=0x7ff ? 2 : \
192             ((uint32_t)(c)<=0xd7ff ? 3 : \
193                 ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
194                     ((uint32_t)(c)<=0xffff ? 3 : 4)\
195                 ) \
196             ) \
197         ) \
198     )
199 
200 /**
201  * The maximum number of UTF-8 code units (bytes) per Unicode code point (U+0000..U+10ffff).
202  * @return 4
203  * @stable ICU 2.4
204  */
205 #define U8_MAX_LENGTH 4
206 
207 /**
208  * Get a code point from a string at a random-access offset,
209  * without changing the offset.
210  * The offset may point to either the lead byte or one of the trail bytes
211  * for a code point, in which case the macro will read all of the bytes
212  * for the code point.
213  * The result is undefined if the offset points to an illegal UTF-8
214  * byte sequence.
215  * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
216  *
217  * @param s const uint8_t * string
218  * @param i string offset
219  * @param c output UChar32 variable
220  * @see U8_GET
221  * @stable ICU 2.4
222  */
223 #define U8_GET_UNSAFE(s, i, c) { \
224     int32_t _u8_get_unsafe_index=(int32_t)(i); \
225     U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \
226     U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \
227 }
228 
229 /**
230  * Get a code point from a string at a random-access offset,
231  * without changing the offset.
232  * The offset may point to either the lead byte or one of the trail bytes
233  * for a code point, in which case the macro will read all of the bytes
234  * for the code point.
235  *
236  * The length can be negative for a NUL-terminated string.
237  *
238  * If the offset points to an illegal UTF-8 byte sequence, then
239  * c is set to a negative value.
240  * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT.
241  *
242  * @param s const uint8_t * string
243  * @param start int32_t starting string offset
244  * @param i int32_t string offset, must be start<=i<length
245  * @param length int32_t string length
246  * @param c output UChar32 variable, set to <0 in case of an error
247  * @see U8_GET_UNSAFE
248  * @stable ICU 2.4
249  */
250 #define U8_GET(s, start, i, length, c) { \
251     int32_t _u8_get_index=(i); \
252     U8_SET_CP_START(s, start, _u8_get_index); \
253     U8_NEXT(s, _u8_get_index, length, c); \
254 }
255 
256 #ifndef U_HIDE_DRAFT_API
257 /**
258  * Get a code point from a string at a random-access offset,
259  * without changing the offset.
260  * The offset may point to either the lead byte or one of the trail bytes
261  * for a code point, in which case the macro will read all of the bytes
262  * for the code point.
263  *
264  * The length can be negative for a NUL-terminated string.
265  *
266  * If the offset points to an illegal UTF-8 byte sequence, then
267  * c is set to U+FFFD.
268  * Iteration through a string is more efficient with U8_NEXT_UNSAFE or U8_NEXT_OR_FFFD.
269  *
270  * This macro does not distinguish between a real U+FFFD in the text
271  * and U+FFFD returned for an ill-formed sequence.
272  * Use U8_GET() if that distinction is important.
273  *
274  * @param s const uint8_t * string
275  * @param start int32_t starting string offset
276  * @param i int32_t string offset, must be start<=i<length
277  * @param length int32_t string length
278  * @param c output UChar32 variable, set to U+FFFD in case of an error
279  * @see U8_GET
280  * @draft ICU 51
281  */
282 #define U8_GET_OR_FFFD(s, start, i, length, c) { \
283     int32_t _u8_get_index=(i); \
284     U8_SET_CP_START(s, start, _u8_get_index); \
285     U8_NEXT_OR_FFFD(s, _u8_get_index, length, c); \
286 }
287 #endif /* U_HIDE_DRAFT_API */
288 
289 /* definitions with forward iteration --------------------------------------- */
290 
291 /**
292  * Get a code point from a string at a code point boundary offset,
293  * and advance the offset to the next code point boundary.
294  * (Post-incrementing forward iteration.)
295  * "Unsafe" macro, assumes well-formed UTF-8.
296  *
297  * The offset may point to the lead byte of a multi-byte sequence,
298  * in which case the macro will read the whole sequence.
299  * The result is undefined if the offset points to a trail byte
300  * or an illegal UTF-8 sequence.
301  *
302  * @param s const uint8_t * string
303  * @param i string offset
304  * @param c output UChar32 variable
305  * @see U8_NEXT
306  * @stable ICU 2.4
307  */
308 #define U8_NEXT_UNSAFE(s, i, c) { \
309     (c)=(uint8_t)(s)[(i)++]; \
310     if((c)>=0x80) { \
311         if((c)<0xe0) { \
312             (c)=(((c)&0x1f)<<6)|((s)[(i)++]&0x3f); \
313         } else if((c)<0xf0) { \
314             /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
315             (c)=(UChar)(((c)<<12)|(((s)[i]&0x3f)<<6)|((s)[(i)+1]&0x3f)); \
316             (i)+=2; \
317         } else { \
318             (c)=(((c)&7)<<18)|(((s)[i]&0x3f)<<12)|(((s)[(i)+1]&0x3f)<<6)|((s)[(i)+2]&0x3f); \
319             (i)+=3; \
320         } \
321     } \
322 }
323 
324 /**
325  * Get a code point from a string at a code point boundary offset,
326  * and advance the offset to the next code point boundary.
327  * (Post-incrementing forward iteration.)
328  * "Safe" macro, checks for illegal sequences and for string boundaries.
329  *
330  * The length can be negative for a NUL-terminated string.
331  *
332  * The offset may point to the lead byte of a multi-byte sequence,
333  * in which case the macro will read the whole sequence.
334  * If the offset points to a trail byte or an illegal UTF-8 sequence, then
335  * c is set to a negative value.
336  *
337  * @param s const uint8_t * string
338  * @param i int32_t string offset, must be i<length
339  * @param length int32_t string length
340  * @param c output UChar32 variable, set to <0 in case of an error
341  * @see U8_NEXT_UNSAFE
342  * @stable ICU 2.4
343  */
344 #define U8_NEXT(s, i, length, c) { \
345     (c)=(uint8_t)(s)[(i)++]; \
346     if((c)>=0x80) { \
347         uint8_t __t1, __t2; \
348         if( /* handle U+1000..U+CFFF inline */ \
349             (0xe0<(c) && (c)<=0xec) && \
350             (((i)+1)<(length) || (length)<0) && \
351             (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
352             (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
353         ) { \
354             /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
355             (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
356             (i)+=2; \
357         } else if( /* handle U+0080..U+07FF inline */ \
358             ((c)<0xe0 && (c)>=0xc2) && \
359             ((i)!=(length)) && \
360             (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
361         ) { \
362             (c)=(((c)&0x1f)<<6)|__t1; \
363             ++(i); \
364         } else { \
365             /* function call for "complicated" and error cases */ \
366             (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (length), c, -1); \
367         } \
368     } \
369 }
370 
371 #ifndef U_HIDE_DRAFT_API
372 /**
373  * Get a code point from a string at a code point boundary offset,
374  * and advance the offset to the next code point boundary.
375  * (Post-incrementing forward iteration.)
376  * "Safe" macro, checks for illegal sequences and for string boundaries.
377  *
378  * The length can be negative for a NUL-terminated string.
379  *
380  * The offset may point to the lead byte of a multi-byte sequence,
381  * in which case the macro will read the whole sequence.
382  * If the offset points to a trail byte or an illegal UTF-8 sequence, then
383  * c is set to U+FFFD.
384  *
385  * This macro does not distinguish between a real U+FFFD in the text
386  * and U+FFFD returned for an ill-formed sequence.
387  * Use U8_NEXT() if that distinction is important.
388  *
389  * @param s const uint8_t * string
390  * @param i int32_t string offset, must be i<length
391  * @param length int32_t string length
392  * @param c output UChar32 variable, set to U+FFFD in case of an error
393  * @see U8_NEXT
394  * @draft ICU 51
395  */
396 #define U8_NEXT_OR_FFFD(s, i, length, c) { \
397     (c)=(uint8_t)(s)[(i)++]; \
398     if((c)>=0x80) { \
399         uint8_t __t1, __t2; \
400         if( /* handle U+1000..U+CFFF inline */ \
401             (0xe0<(c) && (c)<=0xec) && \
402             (((i)+1)<(length) || (length)<0) && \
403             (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
404             (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
405         ) { \
406             /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
407             (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
408             (i)+=2; \
409         } else if( /* handle U+0080..U+07FF inline */ \
410             ((c)<0xe0 && (c)>=0xc2) && \
411             ((i)!=(length)) && \
412             (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
413         ) { \
414             (c)=(((c)&0x1f)<<6)|__t1; \
415             ++(i); \
416         } else { \
417             /* function call for "complicated" and error cases */ \
418             (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (length), c, -3); \
419         } \
420     } \
421 }
422 #endif /* U_HIDE_DRAFT_API */
423 
424 /**
425  * Append a code point to a string, overwriting 1 to 4 bytes.
426  * The offset points to the current end of the string contents
427  * and is advanced (post-increment).
428  * "Unsafe" macro, assumes a valid code point and sufficient space in the string.
429  * Otherwise, the result is undefined.
430  *
431  * @param s const uint8_t * string buffer
432  * @param i string offset
433  * @param c code point to append
434  * @see U8_APPEND
435  * @stable ICU 2.4
436  */
437 #define U8_APPEND_UNSAFE(s, i, c) { \
438     if((uint32_t)(c)<=0x7f) { \
439         (s)[(i)++]=(uint8_t)(c); \
440     } else { \
441         if((uint32_t)(c)<=0x7ff) { \
442             (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
443         } else { \
444             if((uint32_t)(c)<=0xffff) { \
445                 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
446             } else { \
447                 (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
448                 (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
449             } \
450             (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
451         } \
452         (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
453     } \
454 }
455 
456 /**
457  * Append a code point to a string, overwriting 1 to 4 bytes.
458  * The offset points to the current end of the string contents
459  * and is advanced (post-increment).
460  * "Safe" macro, checks for a valid code point.
461  * If a non-ASCII code point is written, checks for sufficient space in the string.
462  * If the code point is not valid or trail bytes do not fit,
463  * then isError is set to TRUE.
464  *
465  * @param s const uint8_t * string buffer
466  * @param i int32_t string offset, must be i<capacity
467  * @param capacity int32_t size of the string buffer
468  * @param c UChar32 code point to append
469  * @param isError output UBool set to TRUE if an error occurs, otherwise not modified
470  * @see U8_APPEND_UNSAFE
471  * @stable ICU 2.4
472  */
473 #define U8_APPEND(s, i, capacity, c, isError) { \
474     if((uint32_t)(c)<=0x7f) { \
475         (s)[(i)++]=(uint8_t)(c); \
476     } else if((uint32_t)(c)<=0x7ff && (i)+1<(capacity)) { \
477         (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
478         (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
479     } else if((uint32_t)(c)<=0xd7ff && (i)+2<(capacity)) { \
480         (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
481         (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
482         (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
483     } else { \
484         (i)=utf8_appendCharSafeBody(s, (i), (capacity), c, &(isError)); \
485     } \
486 }
487 
488 /**
489  * Advance the string offset from one code point boundary to the next.
490  * (Post-incrementing iteration.)
491  * "Unsafe" macro, assumes well-formed UTF-8.
492  *
493  * @param s const uint8_t * string
494  * @param i string offset
495  * @see U8_FWD_1
496  * @stable ICU 2.4
497  */
498 #define U8_FWD_1_UNSAFE(s, i) { \
499     (i)+=1+U8_COUNT_TRAIL_BYTES_UNSAFE((uint8_t)(s)[i]); \
500 }
501 
502 /**
503  * Advance the string offset from one code point boundary to the next.
504  * (Post-incrementing iteration.)
505  * "Safe" macro, checks for illegal sequences and for string boundaries.
506  *
507  * The length can be negative for a NUL-terminated string.
508  *
509  * @param s const uint8_t * string
510  * @param i int32_t string offset, must be i<length
511  * @param length int32_t string length
512  * @see U8_FWD_1_UNSAFE
513  * @stable ICU 2.4
514  */
515 #define U8_FWD_1(s, i, length) { \
516     uint8_t __b=(uint8_t)(s)[(i)++]; \
517     if(U8_IS_LEAD(__b)) { \
518         uint8_t __count=U8_COUNT_TRAIL_BYTES(__b); \
519         if((i)+__count>(length) && (length)>=0) { \
520             __count=(uint8_t)((length)-(i)); \
521         } \
522         while(__count>0 && U8_IS_TRAIL((s)[i])) { \
523             ++(i); \
524             --__count; \
525         } \
526     } \
527 }
528 
529 /**
530  * Advance the string offset from one code point boundary to the n-th next one,
531  * i.e., move forward by n code points.
532  * (Post-incrementing iteration.)
533  * "Unsafe" macro, assumes well-formed UTF-8.
534  *
535  * @param s const uint8_t * string
536  * @param i string offset
537  * @param n number of code points to skip
538  * @see U8_FWD_N
539  * @stable ICU 2.4
540  */
541 #define U8_FWD_N_UNSAFE(s, i, n) { \
542     int32_t __N=(n); \
543     while(__N>0) { \
544         U8_FWD_1_UNSAFE(s, i); \
545         --__N; \
546     } \
547 }
548 
549 /**
550  * Advance the string offset from one code point boundary to the n-th next one,
551  * i.e., move forward by n code points.
552  * (Post-incrementing iteration.)
553  * "Safe" macro, checks for illegal sequences and for string boundaries.
554  *
555  * The length can be negative for a NUL-terminated string.
556  *
557  * @param s const uint8_t * string
558  * @param i int32_t string offset, must be i<length
559  * @param length int32_t string length
560  * @param n number of code points to skip
561  * @see U8_FWD_N_UNSAFE
562  * @stable ICU 2.4
563  */
564 #define U8_FWD_N(s, i, length, n) { \
565     int32_t __N=(n); \
566     while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
567         U8_FWD_1(s, i, length); \
568         --__N; \
569     } \
570 }
571 
572 /**
573  * Adjust a random-access offset to a code point boundary
574  * at the start of a code point.
575  * If the offset points to a UTF-8 trail byte,
576  * then the offset is moved backward to the corresponding lead byte.
577  * Otherwise, it is not modified.
578  * "Unsafe" macro, assumes well-formed UTF-8.
579  *
580  * @param s const uint8_t * string
581  * @param i string offset
582  * @see U8_SET_CP_START
583  * @stable ICU 2.4
584  */
585 #define U8_SET_CP_START_UNSAFE(s, i) { \
586     while(U8_IS_TRAIL((s)[i])) { --(i); } \
587 }
588 
589 /**
590  * Adjust a random-access offset to a code point boundary
591  * at the start of a code point.
592  * If the offset points to a UTF-8 trail byte,
593  * then the offset is moved backward to the corresponding lead byte.
594  * Otherwise, it is not modified.
595  * "Safe" macro, checks for illegal sequences and for string boundaries.
596  *
597  * @param s const uint8_t * string
598  * @param start int32_t starting string offset (usually 0)
599  * @param i int32_t string offset, must be start<=i
600  * @see U8_SET_CP_START_UNSAFE
601  * @stable ICU 2.4
602  */
603 #define U8_SET_CP_START(s, start, i) { \
604     if(U8_IS_TRAIL((s)[(i)])) { \
605         (i)=utf8_back1SafeBody(s, start, (i)); \
606     } \
607 }
608 
609 /* definitions with backward iteration -------------------------------------- */
610 
611 /**
612  * Move the string offset from one code point boundary to the previous one
613  * and get the code point between them.
614  * (Pre-decrementing backward iteration.)
615  * "Unsafe" macro, assumes well-formed UTF-8.
616  *
617  * The input offset may be the same as the string length.
618  * If the offset is behind a multi-byte sequence, then the macro will read
619  * the whole sequence.
620  * If the offset is behind a lead byte, then that itself
621  * will be returned as the code point.
622  * The result is undefined if the offset is behind an illegal UTF-8 sequence.
623  *
624  * @param s const uint8_t * string
625  * @param i string offset
626  * @param c output UChar32 variable
627  * @see U8_PREV
628  * @stable ICU 2.4
629  */
630 #define U8_PREV_UNSAFE(s, i, c) { \
631     (c)=(uint8_t)(s)[--(i)]; \
632     if(U8_IS_TRAIL(c)) { \
633         uint8_t __b, __count=1, __shift=6; \
634 \
635         /* c is a trail byte */ \
636         (c)&=0x3f; \
637         for(;;) { \
638             __b=(uint8_t)(s)[--(i)]; \
639             if(__b>=0xc0) { \
640                 U8_MASK_LEAD_BYTE(__b, __count); \
641                 (c)|=(UChar32)__b<<__shift; \
642                 break; \
643             } else { \
644                 (c)|=(UChar32)(__b&0x3f)<<__shift; \
645                 ++__count; \
646                 __shift+=6; \
647             } \
648         } \
649     } \
650 }
651 
652 /**
653  * Move the string offset from one code point boundary to the previous one
654  * and get the code point between them.
655  * (Pre-decrementing backward iteration.)
656  * "Safe" macro, checks for illegal sequences and for string boundaries.
657  *
658  * The input offset may be the same as the string length.
659  * If the offset is behind a multi-byte sequence, then the macro will read
660  * the whole sequence.
661  * If the offset is behind a lead byte, then that itself
662  * will be returned as the code point.
663  * If the offset is behind an illegal UTF-8 sequence, then c is set to a negative value.
664  *
665  * @param s const uint8_t * string
666  * @param start int32_t starting string offset (usually 0)
667  * @param i int32_t string offset, must be start<i
668  * @param c output UChar32 variable, set to <0 in case of an error
669  * @see U8_PREV_UNSAFE
670  * @stable ICU 2.4
671  */
672 #define U8_PREV(s, start, i, c) { \
673     (c)=(uint8_t)(s)[--(i)]; \
674     if((c)>=0x80) { \
675         (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
676     } \
677 }
678 
679 #ifndef U_HIDE_DRAFT_API
680 /**
681  * Move the string offset from one code point boundary to the previous one
682  * and get the code point between them.
683  * (Pre-decrementing backward iteration.)
684  * "Safe" macro, checks for illegal sequences and for string boundaries.
685  *
686  * The input offset may be the same as the string length.
687  * If the offset is behind a multi-byte sequence, then the macro will read
688  * the whole sequence.
689  * If the offset is behind a lead byte, then that itself
690  * will be returned as the code point.
691  * If the offset is behind an illegal UTF-8 sequence, then c is set to U+FFFD.
692  *
693  * This macro does not distinguish between a real U+FFFD in the text
694  * and U+FFFD returned for an ill-formed sequence.
695  * Use U8_PREV() if that distinction is important.
696  *
697  * @param s const uint8_t * string
698  * @param start int32_t starting string offset (usually 0)
699  * @param i int32_t string offset, must be start<i
700  * @param c output UChar32 variable, set to U+FFFD in case of an error
701  * @see U8_PREV
702  * @draft ICU 51
703  */
704 #define U8_PREV_OR_FFFD(s, start, i, c) { \
705     (c)=(uint8_t)(s)[--(i)]; \
706     if((c)>=0x80) { \
707         (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -3); \
708     } \
709 }
710 #endif /* U_HIDE_DRAFT_API */
711 
712 /**
713  * Move the string offset from one code point boundary to the previous one.
714  * (Pre-decrementing backward iteration.)
715  * The input offset may be the same as the string length.
716  * "Unsafe" macro, assumes well-formed UTF-8.
717  *
718  * @param s const uint8_t * string
719  * @param i string offset
720  * @see U8_BACK_1
721  * @stable ICU 2.4
722  */
723 #define U8_BACK_1_UNSAFE(s, i) { \
724     while(U8_IS_TRAIL((s)[--(i)])) {} \
725 }
726 
727 /**
728  * Move the string offset from one code point boundary to the previous one.
729  * (Pre-decrementing backward iteration.)
730  * The input offset may be the same as the string length.
731  * "Safe" macro, checks for illegal sequences and for string boundaries.
732  *
733  * @param s const uint8_t * string
734  * @param start int32_t starting string offset (usually 0)
735  * @param i int32_t string offset, must be start<i
736  * @see U8_BACK_1_UNSAFE
737  * @stable ICU 2.4
738  */
739 #define U8_BACK_1(s, start, i) { \
740     if(U8_IS_TRAIL((s)[--(i)])) { \
741         (i)=utf8_back1SafeBody(s, start, (i)); \
742     } \
743 }
744 
745 /**
746  * Move the string offset from one code point boundary to the n-th one before it,
747  * i.e., move backward by n code points.
748  * (Pre-decrementing backward iteration.)
749  * The input offset may be the same as the string length.
750  * "Unsafe" macro, assumes well-formed UTF-8.
751  *
752  * @param s const uint8_t * string
753  * @param i string offset
754  * @param n number of code points to skip
755  * @see U8_BACK_N
756  * @stable ICU 2.4
757  */
758 #define U8_BACK_N_UNSAFE(s, i, n) { \
759     int32_t __N=(n); \
760     while(__N>0) { \
761         U8_BACK_1_UNSAFE(s, i); \
762         --__N; \
763     } \
764 }
765 
766 /**
767  * Move the string offset from one code point boundary to the n-th one before it,
768  * i.e., move backward by n code points.
769  * (Pre-decrementing backward iteration.)
770  * The input offset may be the same as the string length.
771  * "Safe" macro, checks for illegal sequences and for string boundaries.
772  *
773  * @param s const uint8_t * string
774  * @param start int32_t index of the start of the string
775  * @param i int32_t string offset, must be start<i
776  * @param n number of code points to skip
777  * @see U8_BACK_N_UNSAFE
778  * @stable ICU 2.4
779  */
780 #define U8_BACK_N(s, start, i, n) { \
781     int32_t __N=(n); \
782     while(__N>0 && (i)>(start)) { \
783         U8_BACK_1(s, start, i); \
784         --__N; \
785     } \
786 }
787 
788 /**
789  * Adjust a random-access offset to a code point boundary after a code point.
790  * If the offset is behind a partial multi-byte sequence,
791  * then the offset is incremented to behind the whole sequence.
792  * Otherwise, it is not modified.
793  * The input offset may be the same as the string length.
794  * "Unsafe" macro, assumes well-formed UTF-8.
795  *
796  * @param s const uint8_t * string
797  * @param i string offset
798  * @see U8_SET_CP_LIMIT
799  * @stable ICU 2.4
800  */
801 #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \
802     U8_BACK_1_UNSAFE(s, i); \
803     U8_FWD_1_UNSAFE(s, i); \
804 }
805 
806 /**
807  * Adjust a random-access offset to a code point boundary after a code point.
808  * If the offset is behind a partial multi-byte sequence,
809  * then the offset is incremented to behind the whole sequence.
810  * Otherwise, it is not modified.
811  * The input offset may be the same as the string length.
812  * "Safe" macro, checks for illegal sequences and for string boundaries.
813  *
814  * The length can be negative for a NUL-terminated string.
815  *
816  * @param s const uint8_t * string
817  * @param start int32_t starting string offset (usually 0)
818  * @param i int32_t string offset, must be start<=i<=length
819  * @param length int32_t string length
820  * @see U8_SET_CP_LIMIT_UNSAFE
821  * @stable ICU 2.4
822  */
823 #define U8_SET_CP_LIMIT(s, start, i, length) { \
824     if((start)<(i) && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
825         U8_BACK_1(s, start, i); \
826         U8_FWD_1(s, i, length); \
827     } \
828 }
829 
830 #endif
831