ICU 49.1.1  49.1.1
utf8.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 1999-2011, 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 
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
54 U_EXPORT const uint8_t
55 #elif defined(U_STATIC_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION)
56 U_CFUNC const uint8_t
57 #else
58 U_CFUNC U_IMPORT const uint8_t /* U_IMPORT2? */ /*U_IMPORT*/
59 #endif
61 
69 #define U8_COUNT_TRAIL_BYTES(leadByte) (utf8_countTrailBytes[(uint8_t)leadByte])
70 
78 #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
79 
89 U_STABLE UChar32 U_EXPORT2
90 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict);
91 
101 U_STABLE int32_t U_EXPORT2
102 utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError);
103 
113 U_STABLE UChar32 U_EXPORT2
114 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict);
115 
125 U_STABLE int32_t U_EXPORT2
126 utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i);
127 
128 /* single-code point definitions -------------------------------------------- */
129 
136 #define U8_IS_SINGLE(c) (((c)&0x80)==0)
137 
144 #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc0)<0x3e)
145 
152 #define U8_IS_TRAIL(c) (((c)&0xc0)==0x80)
153 
161 #define U8_LENGTH(c) \
162  ((uint32_t)(c)<=0x7f ? 1 : \
163  ((uint32_t)(c)<=0x7ff ? 2 : \
164  ((uint32_t)(c)<=0xd7ff ? 3 : \
165  ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
166  ((uint32_t)(c)<=0xffff ? 3 : 4)\
167  ) \
168  ) \
169  ) \
170  )
171 
177 #define U8_MAX_LENGTH 4
178 
195 #define U8_GET_UNSAFE(s, i, c) { \
196  int32_t _u8_get_unsafe_index=(int32_t)(i); \
197  U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \
198  U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \
199 }
200 
219 #define U8_GET(s, start, i, length, c) { \
220  int32_t _u8_get_index=(int32_t)(i); \
221  U8_SET_CP_START(s, start, _u8_get_index); \
222  U8_NEXT(s, _u8_get_index, length, c); \
223 }
224 
225 /* definitions with forward iteration --------------------------------------- */
226 
244 #define U8_NEXT_UNSAFE(s, i, c) { \
245  (c)=(uint8_t)(s)[(i)++]; \
246  if((uint8_t)((c)-0xc0)<0x35) { \
247  uint8_t __count=U8_COUNT_TRAIL_BYTES(c); \
248  U8_MASK_LEAD_BYTE(c, __count); \
249  switch(__count) { \
250  /* each following branch falls through to the next one */ \
251  case 3: \
252  (c)=((c)<<6)|((s)[(i)++]&0x3f); \
253  case 2: \
254  (c)=((c)<<6)|((s)[(i)++]&0x3f); \
255  case 1: \
256  (c)=((c)<<6)|((s)[(i)++]&0x3f); \
257  /* no other branches to optimize switch() */ \
258  break; \
259  } \
260  } \
261 }
262 
281 #define U8_NEXT(s, i, length, c) { \
282  (c)=(uint8_t)(s)[(i)++]; \
283  if((c)>=0x80) { \
284  uint8_t __t1, __t2; \
285  if( /* handle U+1000..U+CFFF inline */ \
286  (0xe0<(c) && (c)<=0xec) && \
287  (((i)+1)<(length)) && \
288  (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
289  (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
290  ) { \
291  /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
292  (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
293  (i)+=2; \
294  } else if( /* handle U+0080..U+07FF inline */ \
295  ((c)<0xe0 && (c)>=0xc2) && \
296  ((i)<(length)) && \
297  (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
298  ) { \
299  (c)=(UChar)((((c)&0x1f)<<6)|__t1); \
300  ++(i); \
301  } else if(U8_IS_LEAD(c)) { \
302  /* function call for "complicated" and error cases */ \
303  (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (int32_t)(length), c, -1); \
304  } else { \
305  (c)=U_SENTINEL; \
306  } \
307  } \
308 }
309 
323 #define U8_APPEND_UNSAFE(s, i, c) { \
324  if((uint32_t)(c)<=0x7f) { \
325  (s)[(i)++]=(uint8_t)(c); \
326  } else { \
327  if((uint32_t)(c)<=0x7ff) { \
328  (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
329  } else { \
330  if((uint32_t)(c)<=0xffff) { \
331  (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
332  } else { \
333  (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
334  (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
335  } \
336  (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
337  } \
338  (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
339  } \
340 }
341 
359 #define U8_APPEND(s, i, capacity, c, isError) { \
360  if((uint32_t)(c)<=0x7f) { \
361  (s)[(i)++]=(uint8_t)(c); \
362  } else if((uint32_t)(c)<=0x7ff && (i)+1<(capacity)) { \
363  (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
364  (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
365  } else if((uint32_t)(c)<=0xd7ff && (i)+2<(capacity)) { \
366  (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
367  (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
368  (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
369  } else { \
370  (i)=utf8_appendCharSafeBody(s, (int32_t)(i), (int32_t)(capacity), c, &(isError)); \
371  } \
372 }
373 
384 #define U8_FWD_1_UNSAFE(s, i) { \
385  (i)+=1+U8_COUNT_TRAIL_BYTES((s)[i]); \
386 }
387 
399 #define U8_FWD_1(s, i, length) { \
400  uint8_t __b=(uint8_t)(s)[(i)++]; \
401  if(U8_IS_LEAD(__b)) { \
402  uint8_t __count=U8_COUNT_TRAIL_BYTES(__b); \
403  if((i)+__count>(length)) { \
404  __count=(uint8_t)((length)-(i)); \
405  } \
406  while(__count>0 && U8_IS_TRAIL((s)[i])) { \
407  ++(i); \
408  --__count; \
409  } \
410  } \
411 }
412 
425 #define U8_FWD_N_UNSAFE(s, i, n) { \
426  int32_t __N=(n); \
427  while(__N>0) { \
428  U8_FWD_1_UNSAFE(s, i); \
429  --__N; \
430  } \
431 }
432 
446 #define U8_FWD_N(s, i, length, n) { \
447  int32_t __N=(n); \
448  while(__N>0 && (i)<(length)) { \
449  U8_FWD_1(s, i, length); \
450  --__N; \
451  } \
452 }
453 
467 #define U8_SET_CP_START_UNSAFE(s, i) { \
468  while(U8_IS_TRAIL((s)[i])) { --(i); } \
469 }
470 
485 #define U8_SET_CP_START(s, start, i) { \
486  if(U8_IS_TRAIL((s)[(i)])) { \
487  (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
488  } \
489 }
490 
491 /* definitions with backward iteration -------------------------------------- */
492 
512 #define U8_PREV_UNSAFE(s, i, c) { \
513  (c)=(uint8_t)(s)[--(i)]; \
514  if(U8_IS_TRAIL(c)) { \
515  uint8_t __b, __count=1, __shift=6; \
516 \
517  /* c is a trail byte */ \
518  (c)&=0x3f; \
519  for(;;) { \
520  __b=(uint8_t)(s)[--(i)]; \
521  if(__b>=0xc0) { \
522  U8_MASK_LEAD_BYTE(__b, __count); \
523  (c)|=(UChar32)__b<<__shift; \
524  break; \
525  } else { \
526  (c)|=(UChar32)(__b&0x3f)<<__shift; \
527  ++__count; \
528  __shift+=6; \
529  } \
530  } \
531  } \
532 }
533 
554 #define U8_PREV(s, start, i, c) { \
555  (c)=(uint8_t)(s)[--(i)]; \
556  if((c)>=0x80) { \
557  if((c)<=0xbf) { \
558  (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
559  } else { \
560  (c)=U_SENTINEL; \
561  } \
562  } \
563 }
564 
576 #define U8_BACK_1_UNSAFE(s, i) { \
577  while(U8_IS_TRAIL((s)[--(i)])) {} \
578 }
579 
592 #define U8_BACK_1(s, start, i) { \
593  if(U8_IS_TRAIL((s)[--(i)])) { \
594  (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
595  } \
596 }
597 
611 #define U8_BACK_N_UNSAFE(s, i, n) { \
612  int32_t __N=(n); \
613  while(__N>0) { \
614  U8_BACK_1_UNSAFE(s, i); \
615  --__N; \
616  } \
617 }
618 
633 #define U8_BACK_N(s, start, i, n) { \
634  int32_t __N=(n); \
635  while(__N>0 && (i)>(start)) { \
636  U8_BACK_1(s, start, i); \
637  --__N; \
638  } \
639 }
640 
654 #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \
655  U8_BACK_1_UNSAFE(s, i); \
656  U8_FWD_1_UNSAFE(s, i); \
657 }
658 
674 #define U8_SET_CP_LIMIT(s, start, i, length) { \
675  if((start)<(i) && (i)<(length)) { \
676  U8_BACK_1(s, start, i); \
677  U8_FWD_1(s, i, length); \
678  } \
679 }
680 
681 #endif