ICU 4.8.1.1  4.8.1.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
utf8.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 1999-2009, 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 
34 #ifndef __UTF8_H__
35 #define __UTF8_H__
36 
37 /* utf.h must be included first. */
38 #ifndef __UTF_H__
39 # include "unicode/utf.h"
40 #endif
41 
42 /* internal definitions ----------------------------------------------------- */
43 
55 #ifdef U_UTF8_IMPL
56 U_EXPORT const uint8_t
57 #elif defined(U_STATIC_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION)
58 U_CFUNC const uint8_t
59 #else
60 U_CFUNC U_IMPORT const uint8_t /* U_IMPORT2? */ /*U_IMPORT*/
61 #endif
63 
71 #define U8_COUNT_TRAIL_BYTES(leadByte) (utf8_countTrailBytes[(uint8_t)leadByte])
72 
80 #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
81 
92 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict);
93 
103 U_STABLE int32_t U_EXPORT2
104 utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError);
105 
116 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict);
117 
127 U_STABLE int32_t U_EXPORT2
128 utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i);
129 
130 /* single-code point definitions -------------------------------------------- */
131 
138 #define U8_IS_SINGLE(c) (((c)&0x80)==0)
139 
146 #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc0)<0x3e)
147 
154 #define U8_IS_TRAIL(c) (((c)&0xc0)==0x80)
155 
163 #define U8_LENGTH(c) \
164  ((uint32_t)(c)<=0x7f ? 1 : \
165  ((uint32_t)(c)<=0x7ff ? 2 : \
166  ((uint32_t)(c)<=0xd7ff ? 3 : \
167  ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
168  ((uint32_t)(c)<=0xffff ? 3 : 4)\
169  ) \
170  ) \
171  ) \
172  )
173 
179 #define U8_MAX_LENGTH 4
180 
197 #define U8_GET_UNSAFE(s, i, c) { \
198  int32_t _u8_get_unsafe_index=(int32_t)(i); \
199  U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \
200  U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \
201 }
202 
221 #define U8_GET(s, start, i, length, c) { \
222  int32_t _u8_get_index=(int32_t)(i); \
223  U8_SET_CP_START(s, start, _u8_get_index); \
224  U8_NEXT(s, _u8_get_index, length, c); \
225 }
226 
227 /* definitions with forward iteration --------------------------------------- */
228 
246 #define U8_NEXT_UNSAFE(s, i, c) { \
247  (c)=(uint8_t)(s)[(i)++]; \
248  if((uint8_t)((c)-0xc0)<0x35) { \
249  uint8_t __count=U8_COUNT_TRAIL_BYTES(c); \
250  U8_MASK_LEAD_BYTE(c, __count); \
251  switch(__count) { \
252  /* each following branch falls through to the next one */ \
253  case 3: \
254  (c)=((c)<<6)|((s)[(i)++]&0x3f); \
255  case 2: \
256  (c)=((c)<<6)|((s)[(i)++]&0x3f); \
257  case 1: \
258  (c)=((c)<<6)|((s)[(i)++]&0x3f); \
259  /* no other branches to optimize switch() */ \
260  break; \
261  } \
262  } \
263 }
264 
283 #define U8_NEXT(s, i, length, c) { \
284  (c)=(uint8_t)(s)[(i)++]; \
285  if((c)>=0x80) { \
286  uint8_t __t1, __t2; \
287  if( /* handle U+1000..U+CFFF inline */ \
288  (0xe0<(c) && (c)<=0xec) && \
289  (((i)+1)<(length)) && \
290  (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
291  (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
292  ) { \
293  /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
294  (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
295  (i)+=2; \
296  } else if( /* handle U+0080..U+07FF inline */ \
297  ((c)<0xe0 && (c)>=0xc2) && \
298  ((i)<(length)) && \
299  (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
300  ) { \
301  (c)=(UChar)((((c)&0x1f)<<6)|__t1); \
302  ++(i); \
303  } else if(U8_IS_LEAD(c)) { \
304  /* function call for "complicated" and error cases */ \
305  (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (int32_t)(length), c, -1); \
306  } else { \
307  (c)=U_SENTINEL; \
308  } \
309  } \
310 }
311 
325 #define U8_APPEND_UNSAFE(s, i, c) { \
326  if((uint32_t)(c)<=0x7f) { \
327  (s)[(i)++]=(uint8_t)(c); \
328  } else { \
329  if((uint32_t)(c)<=0x7ff) { \
330  (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
331  } else { \
332  if((uint32_t)(c)<=0xffff) { \
333  (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
334  } else { \
335  (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
336  (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
337  } \
338  (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
339  } \
340  (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
341  } \
342 }
343 
361 #define U8_APPEND(s, i, capacity, c, isError) { \
362  if((uint32_t)(c)<=0x7f) { \
363  (s)[(i)++]=(uint8_t)(c); \
364  } else if((uint32_t)(c)<=0x7ff && (i)+1<(capacity)) { \
365  (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
366  (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
367  } else if((uint32_t)(c)<=0xd7ff && (i)+2<(capacity)) { \
368  (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
369  (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
370  (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
371  } else { \
372  (i)=utf8_appendCharSafeBody(s, (int32_t)(i), (int32_t)(capacity), c, &(isError)); \
373  } \
374 }
375 
386 #define U8_FWD_1_UNSAFE(s, i) { \
387  (i)+=1+U8_COUNT_TRAIL_BYTES((s)[i]); \
388 }
389 
401 #define U8_FWD_1(s, i, length) { \
402  uint8_t __b=(uint8_t)(s)[(i)++]; \
403  if(U8_IS_LEAD(__b)) { \
404  uint8_t __count=U8_COUNT_TRAIL_BYTES(__b); \
405  if((i)+__count>(length)) { \
406  __count=(uint8_t)((length)-(i)); \
407  } \
408  while(__count>0 && U8_IS_TRAIL((s)[i])) { \
409  ++(i); \
410  --__count; \
411  } \
412  } \
413 }
414 
427 #define U8_FWD_N_UNSAFE(s, i, n) { \
428  int32_t __N=(n); \
429  while(__N>0) { \
430  U8_FWD_1_UNSAFE(s, i); \
431  --__N; \
432  } \
433 }
434 
448 #define U8_FWD_N(s, i, length, n) { \
449  int32_t __N=(n); \
450  while(__N>0 && (i)<(length)) { \
451  U8_FWD_1(s, i, length); \
452  --__N; \
453  } \
454 }
455 
469 #define U8_SET_CP_START_UNSAFE(s, i) { \
470  while(U8_IS_TRAIL((s)[i])) { --(i); } \
471 }
472 
487 #define U8_SET_CP_START(s, start, i) { \
488  if(U8_IS_TRAIL((s)[(i)])) { \
489  (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
490  } \
491 }
492 
493 /* definitions with backward iteration -------------------------------------- */
494 
514 #define U8_PREV_UNSAFE(s, i, c) { \
515  (c)=(uint8_t)(s)[--(i)]; \
516  if(U8_IS_TRAIL(c)) { \
517  uint8_t __b, __count=1, __shift=6; \
518 \
519  /* c is a trail byte */ \
520  (c)&=0x3f; \
521  for(;;) { \
522  __b=(uint8_t)(s)[--(i)]; \
523  if(__b>=0xc0) { \
524  U8_MASK_LEAD_BYTE(__b, __count); \
525  (c)|=(UChar32)__b<<__shift; \
526  break; \
527  } else { \
528  (c)|=(UChar32)(__b&0x3f)<<__shift; \
529  ++__count; \
530  __shift+=6; \
531  } \
532  } \
533  } \
534 }
535 
556 #define U8_PREV(s, start, i, c) { \
557  (c)=(uint8_t)(s)[--(i)]; \
558  if((c)>=0x80) { \
559  if((c)<=0xbf) { \
560  (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
561  } else { \
562  (c)=U_SENTINEL; \
563  } \
564  } \
565 }
566 
578 #define U8_BACK_1_UNSAFE(s, i) { \
579  while(U8_IS_TRAIL((s)[--(i)])) {} \
580 }
581 
594 #define U8_BACK_1(s, start, i) { \
595  if(U8_IS_TRAIL((s)[--(i)])) { \
596  (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
597  } \
598 }
599 
613 #define U8_BACK_N_UNSAFE(s, i, n) { \
614  int32_t __N=(n); \
615  while(__N>0) { \
616  U8_BACK_1_UNSAFE(s, i); \
617  --__N; \
618  } \
619 }
620 
635 #define U8_BACK_N(s, start, i, n) { \
636  int32_t __N=(n); \
637  while(__N>0 && (i)>(start)) { \
638  U8_BACK_1(s, start, i); \
639  --__N; \
640  } \
641 }
642 
656 #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \
657  U8_BACK_1_UNSAFE(s, i); \
658  U8_FWD_1_UNSAFE(s, i); \
659 }
660 
676 #define U8_SET_CP_LIMIT(s, start, i, length) { \
677  if((start)<(i) && (i)<(length)) { \
678  U8_BACK_1(s, start, i); \
679  U8_FWD_1(s, i, length); \
680  } \
681 }
682 
683 #endif
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:62
#define U_IMPORT
Definition: platform.h:320
UChar32 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict)
Function for handling &quot;next code point&quot; with error-checking.
C API: Code point macros.
#define U_CFUNC
This is used in a declaration of a library private ICU C function.
Definition: umachine.h:109
UChar32 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict)
Function for handling &quot;previous code point&quot; with error-checking.
int32_t utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i)
Function for handling &quot;skip backward one code point&quot; with error-checking.
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition: umachine.h:345
#define U_EXPORT2
Definition: platform.h:314
#define U_EXPORT
Definition: platform.h:303
int32_t utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError)
Function for handling &quot;append code point&quot; with error-checking.
#define U_STABLE
This is used to declare a function as a stable public ICU C API.
Definition: umachine.h:137
int8_t UBool
The ICU boolean type.
Definition: umachine.h:228