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