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