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