ICU 59.1  59.1
utf8.h
Go to the documentation of this file.
1 // © 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: UTF-8
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 
44 
45 
64 #define U8_COUNT_TRAIL_BYTES(leadByte) \
65  ((uint8_t)(leadByte)<0xf0 ? \
66  ((uint8_t)(leadByte)>=0xc0)+((uint8_t)(leadByte)>=0xe0) : \
67  (uint8_t)(leadByte)<0xfe ? 3+((uint8_t)(leadByte)>=0xf8)+((uint8_t)(leadByte)>=0xfc) : 0)
68 
80 #define U8_COUNT_TRAIL_BYTES_UNSAFE(leadByte) \
81  (((leadByte)>=0xc0)+((leadByte)>=0xe0)+((leadByte)>=0xf0))
82 
90 #define U8_MASK_LEAD_BYTE(leadByte, countTrailBytes) ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
91 
101 U_STABLE UChar32 U_EXPORT2
102 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict);
103 
113 U_STABLE int32_t U_EXPORT2
114 utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError);
115 
125 U_STABLE UChar32 U_EXPORT2
126 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict);
127 
137 U_STABLE int32_t U_EXPORT2
138 utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i);
139 
140 /* single-code point definitions -------------------------------------------- */
141 
148 #define U8_IS_SINGLE(c) (((c)&0x80)==0)
149 
156 #define U8_IS_LEAD(c) ((uint8_t)((c)-0xc0)<0x3e)
157 
164 #define U8_IS_TRAIL(c) (((c)&0xc0)==0x80)
165 
173 #define U8_LENGTH(c) \
174  ((uint32_t)(c)<=0x7f ? 1 : \
175  ((uint32_t)(c)<=0x7ff ? 2 : \
176  ((uint32_t)(c)<=0xd7ff ? 3 : \
177  ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
178  ((uint32_t)(c)<=0xffff ? 3 : 4)\
179  ) \
180  ) \
181  ) \
182  )
183 
189 #define U8_MAX_LENGTH 4
190 
207 #define U8_GET_UNSAFE(s, i, c) { \
208  int32_t _u8_get_unsafe_index=(int32_t)(i); \
209  U8_SET_CP_START_UNSAFE(s, _u8_get_unsafe_index); \
210  U8_NEXT_UNSAFE(s, _u8_get_unsafe_index, c); \
211 }
212 
234 #define U8_GET(s, start, i, length, c) { \
235  int32_t _u8_get_index=(i); \
236  U8_SET_CP_START(s, start, _u8_get_index); \
237  U8_NEXT(s, _u8_get_index, length, c); \
238 }
239 
265 #define U8_GET_OR_FFFD(s, start, i, length, c) { \
266  int32_t _u8_get_index=(i); \
267  U8_SET_CP_START(s, start, _u8_get_index); \
268  U8_NEXT_OR_FFFD(s, _u8_get_index, length, c); \
269 }
270 
271 /* definitions with forward iteration --------------------------------------- */
272 
290 #define U8_NEXT_UNSAFE(s, i, c) { \
291  (c)=(uint8_t)(s)[(i)++]; \
292  if((c)>=0x80) { \
293  if((c)<0xe0) { \
294  (c)=(((c)&0x1f)<<6)|((s)[(i)++]&0x3f); \
295  } else if((c)<0xf0) { \
296  /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
297  (c)=(UChar)(((c)<<12)|(((s)[i]&0x3f)<<6)|((s)[(i)+1]&0x3f)); \
298  (i)+=2; \
299  } else { \
300  (c)=(((c)&7)<<18)|(((s)[i]&0x3f)<<12)|(((s)[(i)+1]&0x3f)<<6)|((s)[(i)+2]&0x3f); \
301  (i)+=3; \
302  } \
303  } \
304 }
305 
326 #define U8_NEXT(s, i, length, c) { \
327  (c)=(uint8_t)(s)[(i)++]; \
328  if((c)>=0x80) { \
329  uint8_t __t1, __t2; \
330  if( /* handle U+1000..U+CFFF inline */ \
331  (0xe0<(c) && (c)<=0xec) && \
332  (((i)+1)<(length) || (length)<0) && \
333  (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
334  (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
335  ) { \
336  /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
337  (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
338  (i)+=2; \
339  } else if( /* handle U+0080..U+07FF inline */ \
340  ((c)<0xe0 && (c)>=0xc2) && \
341  ((i)!=(length)) && \
342  (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
343  ) { \
344  (c)=(((c)&0x1f)<<6)|__t1; \
345  ++(i); \
346  } else { \
347  /* function call for "complicated" and error cases */ \
348  (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (length), c, -1); \
349  } \
350  } \
351 }
352 
377 #define U8_NEXT_OR_FFFD(s, i, length, c) { \
378  (c)=(uint8_t)(s)[(i)++]; \
379  if((c)>=0x80) { \
380  uint8_t __t1, __t2; \
381  if( /* handle U+1000..U+CFFF inline */ \
382  (0xe0<(c) && (c)<=0xec) && \
383  (((i)+1)<(length) || (length)<0) && \
384  (__t1=(uint8_t)((s)[i]-0x80))<=0x3f && \
385  (__t2=(uint8_t)((s)[(i)+1]-0x80))<= 0x3f \
386  ) { \
387  /* no need for (c&0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */ \
388  (c)=(UChar)(((c)<<12)|(__t1<<6)|__t2); \
389  (i)+=2; \
390  } else if( /* handle U+0080..U+07FF inline */ \
391  ((c)<0xe0 && (c)>=0xc2) && \
392  ((i)!=(length)) && \
393  (__t1=(uint8_t)((s)[i]-0x80))<=0x3f \
394  ) { \
395  (c)=(((c)&0x1f)<<6)|__t1; \
396  ++(i); \
397  } else { \
398  /* function call for "complicated" and error cases */ \
399  (c)=utf8_nextCharSafeBody((const uint8_t *)s, &(i), (length), c, -3); \
400  } \
401  } \
402 }
403 
417 #define U8_APPEND_UNSAFE(s, i, c) { \
418  if((uint32_t)(c)<=0x7f) { \
419  (s)[(i)++]=(uint8_t)(c); \
420  } else { \
421  if((uint32_t)(c)<=0x7ff) { \
422  (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
423  } else { \
424  if((uint32_t)(c)<=0xffff) { \
425  (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
426  } else { \
427  (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
428  (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
429  } \
430  (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
431  } \
432  (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
433  } \
434 }
435 
453 #define U8_APPEND(s, i, capacity, c, isError) { \
454  if((uint32_t)(c)<=0x7f) { \
455  (s)[(i)++]=(uint8_t)(c); \
456  } else if((uint32_t)(c)<=0x7ff && (i)+1<(capacity)) { \
457  (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
458  (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
459  } else if((uint32_t)(c)<=0xd7ff && (i)+2<(capacity)) { \
460  (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
461  (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
462  (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
463  } else { \
464  (i)=utf8_appendCharSafeBody(s, (i), (capacity), c, &(isError)); \
465  } \
466 }
467 
478 #define U8_FWD_1_UNSAFE(s, i) { \
479  (i)+=1+U8_COUNT_TRAIL_BYTES_UNSAFE((uint8_t)(s)[i]); \
480 }
481 
495 #define U8_FWD_1(s, i, length) { \
496  uint8_t __b=(uint8_t)(s)[(i)++]; \
497  if(U8_IS_LEAD(__b)) { \
498  uint8_t __count=U8_COUNT_TRAIL_BYTES(__b); \
499  if((i)+__count>(length) && (length)>=0) { \
500  __count=(uint8_t)((length)-(i)); \
501  } \
502  while(__count>0 && U8_IS_TRAIL((s)[i])) { \
503  ++(i); \
504  --__count; \
505  } \
506  } \
507 }
508 
521 #define U8_FWD_N_UNSAFE(s, i, n) { \
522  int32_t __N=(n); \
523  while(__N>0) { \
524  U8_FWD_1_UNSAFE(s, i); \
525  --__N; \
526  } \
527 }
528 
544 #define U8_FWD_N(s, i, length, n) { \
545  int32_t __N=(n); \
546  while(__N>0 && ((i)<(length) || ((length)<0 && (s)[i]!=0))) { \
547  U8_FWD_1(s, i, length); \
548  --__N; \
549  } \
550 }
551 
565 #define U8_SET_CP_START_UNSAFE(s, i) { \
566  while(U8_IS_TRAIL((s)[i])) { --(i); } \
567 }
568 
583 #define U8_SET_CP_START(s, start, i) { \
584  if(U8_IS_TRAIL((s)[(i)])) { \
585  (i)=utf8_back1SafeBody(s, start, (i)); \
586  } \
587 }
588 
589 /* definitions with backward iteration -------------------------------------- */
590 
610 #define U8_PREV_UNSAFE(s, i, c) { \
611  (c)=(uint8_t)(s)[--(i)]; \
612  if(U8_IS_TRAIL(c)) { \
613  uint8_t __b, __count=1, __shift=6; \
614 \
615  /* c is a trail byte */ \
616  (c)&=0x3f; \
617  for(;;) { \
618  __b=(uint8_t)(s)[--(i)]; \
619  if(__b>=0xc0) { \
620  U8_MASK_LEAD_BYTE(__b, __count); \
621  (c)|=(UChar32)__b<<__shift; \
622  break; \
623  } else { \
624  (c)|=(UChar32)(__b&0x3f)<<__shift; \
625  ++__count; \
626  __shift+=6; \
627  } \
628  } \
629  } \
630 }
631 
652 #define U8_PREV(s, start, i, c) { \
653  (c)=(uint8_t)(s)[--(i)]; \
654  if((c)>=0x80) { \
655  (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -1); \
656  } \
657 }
658 
683 #define U8_PREV_OR_FFFD(s, start, i, c) { \
684  (c)=(uint8_t)(s)[--(i)]; \
685  if((c)>=0x80) { \
686  (c)=utf8_prevCharSafeBody((const uint8_t *)s, start, &(i), c, -3); \
687  } \
688 }
689 
701 #define U8_BACK_1_UNSAFE(s, i) { \
702  while(U8_IS_TRAIL((s)[--(i)])) {} \
703 }
704 
717 #define U8_BACK_1(s, start, i) { \
718  if(U8_IS_TRAIL((s)[--(i)])) { \
719  (i)=utf8_back1SafeBody(s, start, (i)); \
720  } \
721 }
722 
736 #define U8_BACK_N_UNSAFE(s, i, n) { \
737  int32_t __N=(n); \
738  while(__N>0) { \
739  U8_BACK_1_UNSAFE(s, i); \
740  --__N; \
741  } \
742 }
743 
758 #define U8_BACK_N(s, start, i, n) { \
759  int32_t __N=(n); \
760  while(__N>0 && (i)>(start)) { \
761  U8_BACK_1(s, start, i); \
762  --__N; \
763  } \
764 }
765 
779 #define U8_SET_CP_LIMIT_UNSAFE(s, i) { \
780  U8_BACK_1_UNSAFE(s, i); \
781  U8_FWD_1_UNSAFE(s, i); \
782 }
783 
801 #define U8_SET_CP_LIMIT(s, start, i, length) { \
802  if((start)<(i) && ((i)<(length) || (length)<0)) { \
803  U8_BACK_1(s, start, i); \
804  U8_FWD_1(s, i, length); \
805  } \
806 }
807 
808 #endif
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.
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:396
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:111
int8_t UBool
The ICU boolean type.
Definition: umachine.h:236