ICU 4.2.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-2007, 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 
50 #ifdef U_UTF8_IMPL
51 U_EXPORT const uint8_t
52 #elif defined(U_STATIC_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION)
53 U_CFUNC const uint8_t
54 #else
55 U_CFUNC U_IMPORT const uint8_t /* U_IMPORT2? */ /*U_IMPORT*/
56 #endif
58 
63 #define U8_COUNT_TRAIL_BYTES(leadByte) (utf8_countTrailBytes[(uint8_t)leadByte])
64 
69 #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
70 
76 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict);
77 
83 utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError);
84 
90 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict);
91 
97 utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i);
98 
99 /* single-code point definitions -------------------------------------------- */
100 
107 #define U8_IS_SINGLE(c) (((c)&0x80)==0)
108 
115 #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc0)<0x3e)
116 
123 #define U8_IS_TRAIL(c) (((c)&0xc0)==0x80)
124 
132 #define U8_LENGTH(c) \
133  ((uint32_t)(c)<=0x7f ? 1 : \
134  ((uint32_t)(c)<=0x7ff ? 2 : \
135  ((uint32_t)(c)<=0xd7ff ? 3 : \
136  ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
137  ((uint32_t)(c)<=0xffff ? 3 : 4)\
138  ) \
139  ) \
140  ) \
141  )
142 
148 #define U8_MAX_LENGTH 4
149 
166 #define U8_GET_UNSAFE(s, i, c) { \
167  int32_t _u8_get_unsafe_index=(int32_t)(i); \
168  U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \
169  U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \
170 }
171 
190 #define U8_GET(s, start, i, length, c) { \
191  int32_t _u8_get_index=(int32_t)(i); \
192  U8_SET_CP_START(s, start, _u8_get_index); \
193  U8_NEXT(s, _u8_get_index, length, c); \
194 }
195 
196 /* definitions with forward iteration --------------------------------------- */
197 
215 #define U8_NEXT_UNSAFE(s, i, c) { \
216  (c)=(uint8_t)(s)[(i)++]; \
217  if((uint8_t)((c)-0xc0)<0x35) { \
218  uint8_t __count=U8_COUNT_TRAIL_BYTES(c); \
219  U8_MASK_LEAD_BYTE(c, __count); \
220  switch(__count) { \
221  /* each following branch falls through to the next one */ \
222  case 3: \
223  (c)=((c)<<6)|((s)[(i)++]&0x3f); \
224  case 2: \
225  (c)=((c)<<6)|((s)[(i)++]&0x3f); \
226  case 1: \
227  (c)=((c)<<6)|((s)[(i)++]&0x3f); \
228  /* no other branches to optimize switch() */ \
229  break; \
230  } \
231  } \
232 }
233 
252 #define U8_NEXT(s, i, length, c) { \
253  (c)=(uint8_t)(s)[(i)++]; \
254  if((c)>=0x80) { \
255  uint8_t __t1, __t2; \
256  if( /* handle U+1000..U+CFFF inline */ \
257  (0xe0<(c) && (c)<=0xec) && \
258  (((i)+1)<(length)) && \
259  (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
260  (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
261  ) { \
262  /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
263  (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
264  (i)+=2; \
265  } else if( /* handle U+0080..U+07FF inline */ \
266  ((c)<0xe0 && (c)>=0xc2) && \
267  ((i)<(length)) && \
268  (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
269  ) { \
270  (c)=(UChar)((((c)&0x1f)<<6)|__t1); \
271  ++(i); \
272  } else if(U8_IS_LEAD(c)) { \
273  /* function call for "complicated" and error cases */ \
274  (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (int32_t)(length), c, -1); \
275  } else { \
276  (c)=U_SENTINEL; \
277  } \
278  } \
279 }
280 
294 #define U8_APPEND_UNSAFE(s, i, c) { \
295  if((uint32_t)(c)<=0x7f) { \
296  (s)[(i)++]=(uint8_t)(c); \
297  } else { \
298  if((uint32_t)(c)<=0x7ff) { \
299  (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
300  } else { \
301  if((uint32_t)(c)<=0xffff) { \
302  (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
303  } else { \
304  (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
305  (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
306  } \
307  (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
308  } \
309  (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
310  } \
311 }
312 
330 #define U8_APPEND(s, i, capacity, c, isError) { \
331  if((uint32_t)(c)<=0x7f) { \
332  (s)[(i)++]=(uint8_t)(c); \
333  } else if((uint32_t)(c)<=0x7ff && (i)+1<(capacity)) { \
334  (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
335  (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
336  } else if((uint32_t)(c)<=0xd7ff && (i)+2<(capacity)) { \
337  (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
338  (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
339  (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
340  } else { \
341  (i)=utf8_appendCharSafeBody(s, (int32_t)(i), (int32_t)(capacity), c, &(isError)); \
342  } \
343 }
344 
355 #define U8_FWD_1_UNSAFE(s, i) { \
356  (i)+=1+U8_COUNT_TRAIL_BYTES((s)[i]); \
357 }
358 
370 #define U8_FWD_1(s, i, length) { \
371  uint8_t __b=(uint8_t)(s)[(i)++]; \
372  if(U8_IS_LEAD(__b)) { \
373  uint8_t __count=U8_COUNT_TRAIL_BYTES(__b); \
374  if((i)+__count>(length)) { \
375  __count=(uint8_t)((length)-(i)); \
376  } \
377  while(__count>0 && U8_IS_TRAIL((s)[i])) { \
378  ++(i); \
379  --__count; \
380  } \
381  } \
382 }
383 
396 #define U8_FWD_N_UNSAFE(s, i, n) { \
397  int32_t __N=(n); \
398  while(__N>0) { \
399  U8_FWD_1_UNSAFE(s, i); \
400  --__N; \
401  } \
402 }
403 
417 #define U8_FWD_N(s, i, length, n) { \
418  int32_t __N=(n); \
419  while(__N>0 && (i)<(length)) { \
420  U8_FWD_1(s, i, length); \
421  --__N; \
422  } \
423 }
424 
438 #define U8_SET_CP_START_UNSAFE(s, i) { \
439  while(U8_IS_TRAIL((s)[i])) { --(i); } \
440 }
441 
456 #define U8_SET_CP_START(s, start, i) { \
457  if(U8_IS_TRAIL((s)[(i)])) { \
458  (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
459  } \
460 }
461 
462 /* definitions with backward iteration -------------------------------------- */
463 
483 #define U8_PREV_UNSAFE(s, i, c) { \
484  (c)=(uint8_t)(s)[--(i)]; \
485  if(U8_IS_TRAIL(c)) { \
486  uint8_t __b, __count=1, __shift=6; \
487 \
488  /* c is a trail byte */ \
489  (c)&=0x3f; \
490  for(;;) { \
491  __b=(uint8_t)(s)[--(i)]; \
492  if(__b>=0xc0) { \
493  U8_MASK_LEAD_BYTE(__b, __count); \
494  (c)|=(UChar32)__b<<__shift; \
495  break; \
496  } else { \
497  (c)|=(UChar32)(__b&0x3f)<<__shift; \
498  ++__count; \
499  __shift+=6; \
500  } \
501  } \
502  } \
503 }
504 
525 #define U8_PREV(s, start, i, c) { \
526  (c)=(uint8_t)(s)[--(i)]; \
527  if((c)>=0x80) { \
528  if((c)<=0xbf) { \
529  (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
530  } else { \
531  (c)=U_SENTINEL; \
532  } \
533  } \
534 }
535 
547 #define U8_BACK_1_UNSAFE(s, i) { \
548  while(U8_IS_TRAIL((s)[--(i)])) {} \
549 }
550 
563 #define U8_BACK_1(s, start, i) { \
564  if(U8_IS_TRAIL((s)[--(i)])) { \
565  (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
566  } \
567 }
568 
582 #define U8_BACK_N_UNSAFE(s, i, n) { \
583  int32_t __N=(n); \
584  while(__N>0) { \
585  U8_BACK_1_UNSAFE(s, i); \
586  --__N; \
587  } \
588 }
589 
604 #define U8_BACK_N(s, start, i, n) { \
605  int32_t __N=(n); \
606  while(__N>0 && (i)>(start)) { \
607  U8_BACK_1(s, start, i); \
608  --__N; \
609  } \
610 }
611 
625 #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \
626  U8_BACK_1_UNSAFE(s, i); \
627  U8_FWD_1_UNSAFE(s, i); \
628 }
629 
645 #define U8_SET_CP_LIMIT(s, start, i, length) { \
646  if((start)<(i) && (i)<(length)) { \
647  U8_BACK_1(s, start, i); \
648  U8_FWD_1(s, i, length); \
649  } \
650 }
651 
652 #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:57
#define U_IMPORT
Definition: platform.h:344
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.
#define U_INTERNAL
This is used to declare a function as an internal ICU C API.
Definition: umachine.h:125
unsigned char uint8_t
Define 64 bit limits.
Definition: pwin32.h:131
C API: Code point macros.
#define U_CFUNC
This is used in a declaration of a library private ICU C function.
Definition: umachine.h:100
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:319
#define U_EXPORT2
Definition: platform.h:338
#define U_EXPORT
Definition: platform.h:327
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.
signed int int32_t
Define 64 bit limits.
Definition: pwin32.h:143
int8_t UBool
The ICU boolean type.
Definition: umachine.h:208