ICU 51.2  51.2
utf8.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 1999-2013, 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 
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 
117 U_STABLE UChar32 U_EXPORT2
118 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict);
119 
129 U_STABLE int32_t U_EXPORT2
130 utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError);
131 
141 U_STABLE UChar32 U_EXPORT2
142 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict);
143 
153 U_STABLE int32_t U_EXPORT2
154 utf8_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 
250 #define U8_GET(s, start, i, length, c) { \
251  int32_t _u8_get_index=(i); \
252  U8_SET_CP_START(s, start, _u8_get_index); \
253  U8_NEXT(s, _u8_get_index, length, c); \
254 }
255 
256 #ifndef U_HIDE_DRAFT_API
257 
282 #define U8_GET_OR_FFFD(s, start, i, length, c) { \
283  int32_t _u8_get_index=(i); \
284  U8_SET_CP_START(s, start, _u8_get_index); \
285  U8_NEXT_OR_FFFD(s, _u8_get_index, length, c); \
286 }
287 #endif /* U_HIDE_DRAFT_API */
288 
289 /* definitions with forward iteration --------------------------------------- */
290 
308 #define U8_NEXT_UNSAFE(s, i, c) { \
309  (c)=(uint8_t)(s)[(i)++]; \
310  if((c)>=0x80) { \
311  if((c)<0xe0) { \
312  (c)=(((c)&0x1f)<<6)|((s)[(i)++]&0x3f); \
313  } else if((c)<0xf0) { \
314  /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
315  (c)=(UChar)(((c)<<12)|(((s)[i]&0x3f)<<6)|((s)[(i)+1]&0x3f)); \
316  (i)+=2; \
317  } else { \
318  (c)=(((c)&7)<<18)|(((s)[i]&0x3f)<<12)|(((s)[(i)+1]&0x3f)<<6)|((s)[(i)+2]&0x3f); \
319  (i)+=3; \
320  } \
321  } \
322 }
323 
344 #define U8_NEXT(s, i, length, c) { \
345  (c)=(uint8_t)(s)[(i)++]; \
346  if((c)>=0x80) { \
347  uint8_t __t1, __t2; \
348  if( /* handle U+1000..U+CFFF inline */ \
349  (0xe0<(c) && (c)<=0xec) && \
350  (((i)+1)<(length) || (length)<0) && \
351  (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
352  (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
353  ) { \
354  /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
355  (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
356  (i)+=2; \
357  } else if( /* handle U+0080..U+07FF inline */ \
358  ((c)<0xe0 && (c)>=0xc2) && \
359  ((i)!=(length)) && \
360  (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
361  ) { \
362  (c)=(((c)&0x1f)<<6)|__t1; \
363  ++(i); \
364  } else { \
365  /* function call for "complicated" and error cases */ \
366  (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (length), c, -1); \
367  } \
368  } \
369 }
370 
371 #ifndef U_HIDE_DRAFT_API
372 
396 #define U8_NEXT_OR_FFFD(s, i, length, c) { \
397  (c)=(uint8_t)(s)[(i)++]; \
398  if((c)>=0x80) { \
399  uint8_t __t1, __t2; \
400  if( /* handle U+1000..U+CFFF inline */ \
401  (0xe0<(c) && (c)<=0xec) && \
402  (((i)+1)<(length) || (length)<0) && \
403  (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
404  (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
405  ) { \
406  /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
407  (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
408  (i)+=2; \
409  } else if( /* handle U+0080..U+07FF inline */ \
410  ((c)<0xe0 && (c)>=0xc2) && \
411  ((i)!=(length)) && \
412  (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
413  ) { \
414  (c)=(((c)&0x1f)<<6)|__t1; \
415  ++(i); \
416  } else { \
417  /* function call for "complicated" and error cases */ \
418  (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (length), c, -3); \
419  } \
420  } \
421 }
422 #endif /* U_HIDE_DRAFT_API */
423 
437 #define U8_APPEND_UNSAFE(s, i, c) { \
438  if((uint32_t)(c)<=0x7f) { \
439  (s)[(i)++]=(uint8_t)(c); \
440  } else { \
441  if((uint32_t)(c)<=0x7ff) { \
442  (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
443  } else { \
444  if((uint32_t)(c)<=0xffff) { \
445  (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
446  } else { \
447  (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
448  (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
449  } \
450  (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
451  } \
452  (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
453  } \
454 }
455 
473 #define U8_APPEND(s, i, capacity, c, isError) { \
474  if((uint32_t)(c)<=0x7f) { \
475  (s)[(i)++]=(uint8_t)(c); \
476  } else if((uint32_t)(c)<=0x7ff && (i)+1<(capacity)) { \
477  (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
478  (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
479  } else if((uint32_t)(c)<=0xd7ff && (i)+2<(capacity)) { \
480  (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
481  (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
482  (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
483  } else { \
484  (i)=utf8_appendCharSafeBody(s, (i), (capacity), c, &(isError)); \
485  } \
486 }
487 
498 #define U8_FWD_1_UNSAFE(s, i) { \
499  (i)+=1+U8_COUNT_TRAIL_BYTES_UNSAFE((uint8_t)(s)[i]); \
500 }
501 
515 #define U8_FWD_1(s, i, length) { \
516  uint8_t __b=(uint8_t)(s)[(i)++]; \
517  if(U8_IS_LEAD(__b)) { \
518  uint8_t __count=U8_COUNT_TRAIL_BYTES(__b); \
519  if((i)+__count>(length) && (length)>=0) { \
520  __count=(uint8_t)((length)-(i)); \
521  } \
522  while(__count>0 && U8_IS_TRAIL((s)[i])) { \
523  ++(i); \
524  --__count; \
525  } \
526  } \
527 }
528 
541 #define U8_FWD_N_UNSAFE(s, i, n) { \
542  int32_t __N=(n); \
543  while(__N>0) { \
544  U8_FWD_1_UNSAFE(s, i); \
545  --__N; \
546  } \
547 }
548 
564 #define U8_FWD_N(s, i, length, n) { \
565  int32_t __N=(n); \
566  while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
567  U8_FWD_1(s, i, length); \
568  --__N; \
569  } \
570 }
571 
585 #define U8_SET_CP_START_UNSAFE(s, i) { \
586  while(U8_IS_TRAIL((s)[i])) { --(i); } \
587 }
588 
603 #define U8_SET_CP_START(s, start, i) { \
604  if(U8_IS_TRAIL((s)[(i)])) { \
605  (i)=utf8_back1SafeBody(s, start, (i)); \
606  } \
607 }
608 
609 /* definitions with backward iteration -------------------------------------- */
610 
630 #define U8_PREV_UNSAFE(s, i, c) { \
631  (c)=(uint8_t)(s)[--(i)]; \
632  if(U8_IS_TRAIL(c)) { \
633  uint8_t __b, __count=1, __shift=6; \
634 \
635  /* c is a trail byte */ \
636  (c)&=0x3f; \
637  for(;;) { \
638  __b=(uint8_t)(s)[--(i)]; \
639  if(__b>=0xc0) { \
640  U8_MASK_LEAD_BYTE(__b, __count); \
641  (c)|=(UChar32)__b<<__shift; \
642  break; \
643  } else { \
644  (c)|=(UChar32)(__b&0x3f)<<__shift; \
645  ++__count; \
646  __shift+=6; \
647  } \
648  } \
649  } \
650 }
651 
672 #define U8_PREV(s, start, i, c) { \
673  (c)=(uint8_t)(s)[--(i)]; \
674  if((c)>=0x80) { \
675  (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
676  } \
677 }
678 
679 #ifndef U_HIDE_DRAFT_API
680 
704 #define U8_PREV_OR_FFFD(s, start, i, c) { \
705  (c)=(uint8_t)(s)[--(i)]; \
706  if((c)>=0x80) { \
707  (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -3); \
708  } \
709 }
710 #endif /* U_HIDE_DRAFT_API */
711 
723 #define U8_BACK_1_UNSAFE(s, i) { \
724  while(U8_IS_TRAIL((s)[--(i)])) {} \
725 }
726 
739 #define U8_BACK_1(s, start, i) { \
740  if(U8_IS_TRAIL((s)[--(i)])) { \
741  (i)=utf8_back1SafeBody(s, start, (i)); \
742  } \
743 }
744 
758 #define U8_BACK_N_UNSAFE(s, i, n) { \
759  int32_t __N=(n); \
760  while(__N>0) { \
761  U8_BACK_1_UNSAFE(s, i); \
762  --__N; \
763  } \
764 }
765 
780 #define U8_BACK_N(s, start, i, n) { \
781  int32_t __N=(n); \
782  while(__N>0 && (i)>(start)) { \
783  U8_BACK_1(s, start, i); \
784  --__N; \
785  } \
786 }
787 
801 #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \
802  U8_BACK_1_UNSAFE(s, i); \
803  U8_FWD_1_UNSAFE(s, i); \
804 }
805 
823 #define U8_SET_CP_LIMIT(s, start, i, length) { \
824  if((start)<(i) && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
825  U8_BACK_1(s, start, i); \
826  U8_FWD_1(s, i, length); \
827  } \
828 }
829 
830 #endif