ICU 50.2 50.2
utf8.h
Go to the documentation of this file.
1/*
2*******************************************************************************
3*
4* Copyright (C) 1999-2012, 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
31
32#ifndef __UTF8_H__
33#define __UTF8_H__
34
35#include "unicode/umachine.h"
36#ifndef __UTF_H__
37# include "unicode/utf.h"
38#endif
39
40/* internal definitions ----------------------------------------------------- */
41
53#ifdef U_UTF8_IMPL
54U_EXPORT const uint8_t
55#elif defined(U_STATIC_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION)
56U_CFUNC const uint8_t
57#else
58U_CFUNC U_IMPORT const uint8_t /* U_IMPORT2? */ /*U_IMPORT*/
59#endif
61
80#define U8_COUNT_TRAIL_BYTES(leadByte) \
81 ((leadByte)<0xf0 ? \
82 ((leadByte)>=0xc0)+((leadByte)>=0xe0) : \
83 (leadByte)<0xfe ? 3+((leadByte)>=0xf8)+((leadByte)>=0xfc) : 0)
84
96#define U8_COUNT_TRAIL_BYTES_UNSAFE(leadByte) \
97 (((leadByte)>=0xc0)+((leadByte)>=0xe0)+((leadByte)>=0xf0))
98
106#define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
107
117U_STABLE UChar32 U_EXPORT2
118utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict);
119
129U_STABLE int32_t U_EXPORT2
130utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError);
131
141U_STABLE UChar32 U_EXPORT2
142utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict);
143
153U_STABLE int32_t U_EXPORT2
154utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i);
155
156/* single-code point definitions -------------------------------------------- */
157
164#define U8_IS_SINGLE(c) (((c)&0x80)==0)
165
172#define U8_IS_LEAD(c) ((uint8_t)((c)-0xc0)<0x3e)
173
180#define U8_IS_TRAIL(c) (((c)&0xc0)==0x80)
181
189#define U8_LENGTH(c) \
190 ((uint32_t)(c)<=0x7f ? 1 : \
191 ((uint32_t)(c)<=0x7ff ? 2 : \
192 ((uint32_t)(c)<=0xd7ff ? 3 : \
193 ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
194 ((uint32_t)(c)<=0xffff ? 3 : 4)\
195 ) \
196 ) \
197 ) \
198 )
199
205#define U8_MAX_LENGTH 4
206
223#define U8_GET_UNSAFE(s, i, c) { \
224 int32_t _u8_get_unsafe_index=(int32_t)(i); \
225 U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \
226 U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \
227}
228
247#define U8_GET(s, start, i, length, c) { \
248 int32_t _u8_get_index=(int32_t)(i); \
249 U8_SET_CP_START(s, start, _u8_get_index); \
250 U8_NEXT(s, _u8_get_index, length, c); \
251}
252
253/* definitions with forward iteration --------------------------------------- */
254
272#define U8_NEXT_UNSAFE(s, i, c) { \
273 (c)=(uint8_t)(s)[(i)++]; \
274 if((c)>=0x80) { \
275 if((c)<0xe0) { \
276 (c)=(((c)&0x1f)<<6)|((s)[(i)++]&0x3f); \
277 } else if((c)<0xf0) { \
278 /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
279 (c)=(UChar)(((c)<<12)|(((s)[i]&0x3f)<<6)|((s)[(i)+1]&0x3f)); \
280 (i)+=2; \
281 } else { \
282 (c)=(((c)&7)<<18)|(((s)[i]&0x3f)<<12)|(((s)[(i)+1]&0x3f)<<6)|((s)[(i)+2]&0x3f); \
283 (i)+=3; \
284 } \
285 } \
286}
287
306#define U8_NEXT(s, i, length, c) { \
307 (c)=(uint8_t)(s)[(i)++]; \
308 if((c)>=0x80) { \
309 uint8_t __t1, __t2; \
310 if( /* handle U+1000..U+CFFF inline */ \
311 (0xe0<(c) && (c)<=0xec) && \
312 (((i)+1)<(length)) && \
313 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
314 (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
315 ) { \
316 /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
317 (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
318 (i)+=2; \
319 } else if( /* handle U+0080..U+07FF inline */ \
320 ((c)<0xe0 && (c)>=0xc2) && \
321 ((i)<(length)) && \
322 (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
323 ) { \
324 (c)=(UChar)((((c)&0x1f)<<6)|__t1); \
325 ++(i); \
326 } else if(U8_IS_LEAD(c)) { \
327 /* function call for "complicated" and error cases */ \
328 (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (int32_t)(length), c, -1); \
329 } else { \
330 (c)=U_SENTINEL; \
331 } \
332 } \
333}
334
348#define U8_APPEND_UNSAFE(s, i, c) { \
349 if((uint32_t)(c)<=0x7f) { \
350 (s)[(i)++]=(uint8_t)(c); \
351 } else { \
352 if((uint32_t)(c)<=0x7ff) { \
353 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
354 } else { \
355 if((uint32_t)(c)<=0xffff) { \
356 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
357 } else { \
358 (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
359 (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
360 } \
361 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
362 } \
363 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
364 } \
365}
366
384#define U8_APPEND(s, i, capacity, c, isError) { \
385 if((uint32_t)(c)<=0x7f) { \
386 (s)[(i)++]=(uint8_t)(c); \
387 } else if((uint32_t)(c)<=0x7ff && (i)+1<(capacity)) { \
388 (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
389 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
390 } else if((uint32_t)(c)<=0xd7ff && (i)+2<(capacity)) { \
391 (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
392 (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
393 (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
394 } else { \
395 (i)=utf8_appendCharSafeBody(s, (int32_t)(i), (int32_t)(capacity), c, &(isError)); \
396 } \
397}
398
409#define U8_FWD_1_UNSAFE(s, i) { \
410 (i)+=1+U8_COUNT_TRAIL_BYTES_UNSAFE((uint8_t)(s)[i]); \
411}
412
424#define U8_FWD_1(s, i, length) { \
425 uint8_t __b=(uint8_t)(s)[(i)++]; \
426 if(U8_IS_LEAD(__b)) { \
427 uint8_t __count=U8_COUNT_TRAIL_BYTES(__b); \
428 if((i)+__count>(length)) { \
429 __count=(uint8_t)((length)-(i)); \
430 } \
431 while(__count>0 && U8_IS_TRAIL((s)[i])) { \
432 ++(i); \
433 --__count; \
434 } \
435 } \
436}
437
450#define U8_FWD_N_UNSAFE(s, i, n) { \
451 int32_t __N=(n); \
452 while(__N>0) { \
453 U8_FWD_1_UNSAFE(s, i); \
454 --__N; \
455 } \
456}
457
471#define U8_FWD_N(s, i, length, n) { \
472 int32_t __N=(n); \
473 while(__N>0 && (i)<(length)) { \
474 U8_FWD_1(s, i, length); \
475 --__N; \
476 } \
477}
478
492#define U8_SET_CP_START_UNSAFE(s, i) { \
493 while(U8_IS_TRAIL((s)[i])) { --(i); } \
494}
495
510#define U8_SET_CP_START(s, start, i) { \
511 if(U8_IS_TRAIL((s)[(i)])) { \
512 (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
513 } \
514}
515
516/* definitions with backward iteration -------------------------------------- */
517
537#define U8_PREV_UNSAFE(s, i, c) { \
538 (c)=(uint8_t)(s)[--(i)]; \
539 if(U8_IS_TRAIL(c)) { \
540 uint8_t __b, __count=1, __shift=6; \
541\
542 /* c is a trail byte */ \
543 (c)&=0x3f; \
544 for(;;) { \
545 __b=(uint8_t)(s)[--(i)]; \
546 if(__b>=0xc0) { \
547 U8_MASK_LEAD_BYTE(__b, __count); \
548 (c)|=(UChar32)__b<<__shift; \
549 break; \
550 } else { \
551 (c)|=(UChar32)(__b&0x3f)<<__shift; \
552 ++__count; \
553 __shift+=6; \
554 } \
555 } \
556 } \
557}
558
579#define U8_PREV(s, start, i, c) { \
580 (c)=(uint8_t)(s)[--(i)]; \
581 if((c)>=0x80) { \
582 if((c)<=0xbf) { \
583 (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
584 } else { \
585 (c)=U_SENTINEL; \
586 } \
587 } \
588}
589
601#define U8_BACK_1_UNSAFE(s, i) { \
602 while(U8_IS_TRAIL((s)[--(i)])) {} \
603}
604
617#define U8_BACK_1(s, start, i) { \
618 if(U8_IS_TRAIL((s)[--(i)])) { \
619 (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
620 } \
621}
622
636#define U8_BACK_N_UNSAFE(s, i, n) { \
637 int32_t __N=(n); \
638 while(__N>0) { \
639 U8_BACK_1_UNSAFE(s, i); \
640 --__N; \
641 } \
642}
643
658#define U8_BACK_N(s, start, i, n) { \
659 int32_t __N=(n); \
660 while(__N>0 && (i)>(start)) { \
661 U8_BACK_1(s, start, i); \
662 --__N; \
663 } \
664}
665
679#define U8_SET_CP_LIMIT_UNSAFE(s, i) { \
680 U8_BACK_1_UNSAFE(s, i); \
681 U8_FWD_1_UNSAFE(s, i); \
682}
683
699#define U8_SET_CP_LIMIT(s, start, i, length) { \
700 if((start)<(i) && (i)<(length)) { \
701 U8_BACK_1(s, start, i); \
702 U8_FWD_1(s, i, length); \
703 } \
704}
705
706#endif
#define U_IMPORT
Definition platform.h:734
#define U_EXPORT
Definition platform.h:716
Basic types and constants for UTF.
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition umachine.h:298
int8_t UBool
The ICU boolean type.
Definition umachine.h:200
#define U_STABLE
This is used to declare a function as a stable public ICU C API.
Definition umachine.h:109
#define U_CFUNC
This is used in a declaration of a library private ICU C function.
Definition umachine.h:81
UChar32 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict)
Function for handling "previous code point" with error-checking.
int32_t utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i)
Function for handling "skip backward one code point" with error-checking.
int32_t utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError)
Function for handling "append code point" with error-checking.
UChar32 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict)
Function for handling "next code point" with error-checking.
U_CFUNC U_IMPORT const uint8_t utf8_countTrailBytes[256]
Internal array with numbers of trail bytes for any given byte used in lead byte position.
Definition utf8.h:60
C API: Code point macros.