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