ICU 58.2  58.2
utf8.h
Go to the documentation of this file.
1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *
6 * Copyright (C) 1999-2015, International Business Machines
7 * Corporation and others. All Rights Reserved.
8 *
9 *******************************************************************************
10 * file name: utf8.h
11 * encoding: US-ASCII
12 * tab size: 8 (not used)
13 * indentation:4
14 *
15 * created on: 1999sep13
16 * created by: Markus W. Scherer
17 */
18 
34 #ifndef __UTF8_H__
35 #define __UTF8_H__
36 
37 #include "unicode/umachine.h"
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 
82 #define U8_COUNT_TRAIL_BYTES(leadByte) \
83  ((uint8_t)(leadByte)<0xf0 ? \
84  ((uint8_t)(leadByte)>=0xc0)+((uint8_t)(leadByte)>=0xe0) : \
85  (uint8_t)(leadByte)<0xfe ? 3+((uint8_t)(leadByte)>=0xf8)+((uint8_t)(leadByte)>=0xfc) : 0)
86 
98 #define U8_COUNT_TRAIL_BYTES_UNSAFE(leadByte) \
99  (((leadByte)>=0xc0)+((leadByte)>=0xe0)+((leadByte)>=0xf0))
100 
108 #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
109 
119 U_STABLE UChar32 U_EXPORT2
120 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict);
121 
131 U_STABLE int32_t U_EXPORT2
132 utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError);
133 
143 U_STABLE UChar32 U_EXPORT2
144 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict);
145 
155 U_STABLE int32_t U_EXPORT2
156 utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i);
157 
158 /* single-code point definitions -------------------------------------------- */
159 
166 #define U8_IS_SINGLE(c) (((c)&0x80)==0)
167 
174 #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc0)<0x3e)
175 
182 #define U8_IS_TRAIL(c) (((c)&0xc0)==0x80)
183 
191 #define U8_LENGTH(c) \
192  ((uint32_t)(c)<=0x7f ? 1 : \
193  ((uint32_t)(c)<=0x7ff ? 2 : \
194  ((uint32_t)(c)<=0xd7ff ? 3 : \
195  ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
196  ((uint32_t)(c)<=0xffff ? 3 : 4)\
197  ) \
198  ) \
199  ) \
200  )
201 
207 #define U8_MAX_LENGTH 4
208 
225 #define U8_GET_UNSAFE(s, i, c) { \
226  int32_t _u8_get_unsafe_index=(int32_t)(i); \
227  U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \
228  U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \
229 }
230 
252 #define U8_GET(s, start, i, length, c) { \
253  int32_t _u8_get_index=(i); \
254  U8_SET_CP_START(s, start, _u8_get_index); \
255  U8_NEXT(s, _u8_get_index, length, c); \
256 }
257 
283 #define U8_GET_OR_FFFD(s, start, i, length, c) { \
284  int32_t _u8_get_index=(i); \
285  U8_SET_CP_START(s, start, _u8_get_index); \
286  U8_NEXT_OR_FFFD(s, _u8_get_index, length, c); \
287 }
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 
395 #define U8_NEXT_OR_FFFD(s, i, length, c) { \
396  (c)=(uint8_t)(s)[(i)++]; \
397  if((c)>=0x80) { \
398  uint8_t __t1, __t2; \
399  if( /* handle U+1000..U+CFFF inline */ \
400  (0xe0<(c) && (c)<=0xec) && \
401  (((i)+1)<(length) || (length)<0) && \
402  (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
403  (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
404  ) { \
405  /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
406  (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
407  (i)+=2; \
408  } else if( /* handle U+0080..U+07FF inline */ \
409  ((c)<0xe0 && (c)>=0xc2) && \
410  ((i)!=(length)) && \
411  (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
412  ) { \
413  (c)=(((c)&0x1f)<<6)|__t1; \
414  ++(i); \
415  } else { \
416  /* function call for "complicated" and error cases */ \
417  (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (length), c, -3); \
418  } \
419  } \
420 }
421 
435 #define U8_APPEND_UNSAFE(s, i, c) { \
436  if((uint32_t)(c)<=0x7f) { \
437  (s)[(i)++]=(uint8_t)(c); \
438  } else { \
439  if((uint32_t)(c)<=0x7ff) { \
440  (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
441  } else { \
442  if((uint32_t)(c)<=0xffff) { \
443  (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
444  } else { \
445  (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
446  (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
447  } \
448  (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
449  } \
450  (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
451  } \
452 }
453 
471 #define U8_APPEND(s, i, capacity, c, isError) { \
472  if((uint32_t)(c)<=0x7f) { \
473  (s)[(i)++]=(uint8_t)(c); \
474  } else if((uint32_t)(c)<=0x7ff && (i)+1<(capacity)) { \
475  (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
476  (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
477  } else if((uint32_t)(c)<=0xd7ff && (i)+2<(capacity)) { \
478  (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
479  (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
480  (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
481  } else { \
482  (i)=utf8_appendCharSafeBody(s, (i), (capacity), c, &(isError)); \
483  } \
484 }
485 
496 #define U8_FWD_1_UNSAFE(s, i) { \
497  (i)+=1+U8_COUNT_TRAIL_BYTES_UNSAFE((uint8_t)(s)[i]); \
498 }
499 
513 #define U8_FWD_1(s, i, length) { \
514  uint8_t __b=(uint8_t)(s)[(i)++]; \
515  if(U8_IS_LEAD(__b)) { \
516  uint8_t __count=U8_COUNT_TRAIL_BYTES(__b); \
517  if((i)+__count>(length) && (length)>=0) { \
518  __count=(uint8_t)((length)-(i)); \
519  } \
520  while(__count>0 && U8_IS_TRAIL((s)[i])) { \
521  ++(i); \
522  --__count; \
523  } \
524  } \
525 }
526 
539 #define U8_FWD_N_UNSAFE(s, i, n) { \
540  int32_t __N=(n); \
541  while(__N>0) { \
542  U8_FWD_1_UNSAFE(s, i); \
543  --__N; \
544  } \
545 }
546 
562 #define U8_FWD_N(s, i, length, n) { \
563  int32_t __N=(n); \
564  while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
565  U8_FWD_1(s, i, length); \
566  --__N; \
567  } \
568 }
569 
583 #define U8_SET_CP_START_UNSAFE(s, i) { \
584  while(U8_IS_TRAIL((s)[i])) { --(i); } \
585 }
586 
601 #define U8_SET_CP_START(s, start, i) { \
602  if(U8_IS_TRAIL((s)[(i)])) { \
603  (i)=utf8_back1SafeBody(s, start, (i)); \
604  } \
605 }
606 
607 /* definitions with backward iteration -------------------------------------- */
608 
628 #define U8_PREV_UNSAFE(s, i, c) { \
629  (c)=(uint8_t)(s)[--(i)]; \
630  if(U8_IS_TRAIL(c)) { \
631  uint8_t __b, __count=1, __shift=6; \
632 \
633  /* c is a trail byte */ \
634  (c)&=0x3f; \
635  for(;;) { \
636  __b=(uint8_t)(s)[--(i)]; \
637  if(__b>=0xc0) { \
638  U8_MASK_LEAD_BYTE(__b, __count); \
639  (c)|=(UChar32)__b<<__shift; \
640  break; \
641  } else { \
642  (c)|=(UChar32)(__b&0x3f)<<__shift; \
643  ++__count; \
644  __shift+=6; \
645  } \
646  } \
647  } \
648 }
649 
670 #define U8_PREV(s, start, i, c) { \
671  (c)=(uint8_t)(s)[--(i)]; \
672  if((c)>=0x80) { \
673  (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
674  } \
675 }
676 
701 #define U8_PREV_OR_FFFD(s, start, i, c) { \
702  (c)=(uint8_t)(s)[--(i)]; \
703  if((c)>=0x80) { \
704  (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -3); \
705  } \
706 }
707 
719 #define U8_BACK_1_UNSAFE(s, i) { \
720  while(U8_IS_TRAIL((s)[--(i)])) {} \
721 }
722 
735 #define U8_BACK_1(s, start, i) { \
736  if(U8_IS_TRAIL((s)[--(i)])) { \
737  (i)=utf8_back1SafeBody(s, start, (i)); \
738  } \
739 }
740 
754 #define U8_BACK_N_UNSAFE(s, i, n) { \
755  int32_t __N=(n); \
756  while(__N>0) { \
757  U8_BACK_1_UNSAFE(s, i); \
758  --__N; \
759  } \
760 }
761 
776 #define U8_BACK_N(s, start, i, n) { \
777  int32_t __N=(n); \
778  while(__N>0 && (i)>(start)) { \
779  U8_BACK_1(s, start, i); \
780  --__N; \
781  } \
782 }
783 
797 #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \
798  U8_BACK_1_UNSAFE(s, i); \
799  U8_FWD_1_UNSAFE(s, i); \
800 }
801 
819 #define U8_SET_CP_LIMIT(s, start, i, length) { \
820  if((start)<(i) && ((i)<(length) || (length)<0)) { \
821  U8_BACK_1(s, start, i); \
822  U8_FWD_1(s, i, length); \
823  } \
824 }
825 
826 #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:834
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.
Basic types and constants for UTF.
C API: Code point macros.
#define U_CFUNC
This is used in a declaration of a library private ICU C function.
Definition: umachine.h:106
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 UChar32
Define UChar32 as a type for single Unicode code points.
Definition: umachine.h:357
#define U_EXPORT
Definition: platform.h:816
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.
#define U_STABLE
This is used to declare a function as a stable public ICU C API.
Definition: umachine.h:134
int8_t UBool
The ICU boolean type.
Definition: umachine.h:259