Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

utf8.h File Reference

C API: UTF-8 macros. More...

#include "unicode/utf.h"

Go to the source code of this file.

Defines

#define UTF8_COUNT_TRAIL_BYTES(leadByte)   (utf8_countTrailBytes[(uint8_t)leadByte])
 Count the trail bytes for a lead byte - this macro should be used so that the assembler code that is mentioned in utf_impl.c could be used here. More...

#define UTF8_MASK_LEAD_BYTE(leadByte, countTrailBytes)   ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
#define UTF8_IS_SINGLE(uchar)   (((uchar)&0x80)==0)
 Is this this code point a single code unit (byte)? More...

#define UTF8_IS_LEAD(uchar)   ((uint8_t)((uchar)-0xc0)<0x3e)
 Is this this code unit the lead code unit (byte) of a code point? More...

#define UTF8_IS_TRAIL(uchar)   (((uchar)&0xc0)==0x80)
 Is this this code unit a trailing code unit (byte) of a code point? More...

#define UTF8_NEED_MULTIPLE_UCHAR(c)   ((uint32_t)(c)>0x7f)
 Does this scalar Unicode value need multiple code units for storage? More...

#define UTF8_CHAR_LENGTH(c)
 Given the lead character, how many bytes are taken by this code point. More...

#define UTF8_MAX_CHAR_LENGTH   4
 The maximum number of bytes per code point. More...

#define UTF8_ARRAY_SIZE(size)   ((5*(size))/2)
 Average number of code units compared to UTF-16. More...

#define UTF8_GET_CHAR_UNSAFE(s, i, c)
#define UTF8_GET_CHAR_SAFE(s, start, i, length, c, strict)
#define UTF8_NEXT_CHAR_UNSAFE(s, i, c)
 Read a Unicode scalar value from an array of UTF-8 bytes. More...

#define UTF8_APPEND_CHAR_UNSAFE(s, i, c)
#define UTF8_FWD_1_UNSAFE(s, i)
#define UTF8_FWD_N_UNSAFE(s, i, n)
#define UTF8_SET_CHAR_START_UNSAFE(s, i)
#define UTF8_NEXT_CHAR_SAFE(s, i, length, c, strict)
#define UTF8_APPEND_CHAR_SAFE(s, i, length, c)
#define UTF8_FWD_1_SAFE(s, i, length)
#define UTF8_FWD_N_SAFE(s, i, length, n)
#define UTF8_SET_CHAR_START_SAFE(s, start, i)
#define UTF8_PREV_CHAR_UNSAFE(s, i, c)
#define UTF8_BACK_1_UNSAFE(s, i)
#define UTF8_BACK_N_UNSAFE(s, i, n)
#define UTF8_SET_CHAR_LIMIT_UNSAFE(s, i)
#define UTF8_PREV_CHAR_SAFE(s, start, i, c, strict)
#define UTF8_BACK_1_SAFE(s, start, i)
#define UTF8_BACK_N_SAFE(s, start, i, n)
#define UTF8_SET_CHAR_LIMIT_SAFE(s, start, i, length)

Functions

U_CAPI UChar32 U_EXPORT2 utf8_nextCharSafeBody (const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict, UBool *pIsError)
U_CAPI int32_t U_EXPORT2 utf8_appendCharSafeBody (uint8_t *s, int32_t i, int32_t length, UChar32 c)
U_CAPI UChar32 U_EXPORT2 utf8_prevCharSafeBody (const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict)
U_CAPI int32_t U_EXPORT2 utf8_back1SafeBody (const uint8_t *s, int32_t start, int32_t i)

Variables

U_CFUNC U_IMPORT const uint8_t utf8_countTrailBytes [256]


Detailed Description

C API: UTF-8 macros.

This file defines macros to deal with UTF-8 code units and code points. Signatures and semantics are the same as for the similarly named macros in utf16.h. utf8.h is included by utf.h after unicode/umachine.h and some common definitions.

Usage: ICU coding guidelines for if() statements should be followed when using these macros. Compound statements (curly braces {}) must be used for if-else-while... bodies and all macro statements should be terminated with semicolon.

Definition in file utf8.h.


Define Documentation

#define UTF8_APPEND_CHAR_SAFE s,
i,
length,
 
 

Value:

{ \
    if((uint32_t)(c)<=0x7f) { \
        (s)[(i)++]=(uint8_t)(c); \
    } else { \
        (i)=utf8_appendCharSafeBody(s, (int32_t)(i), (int32_t)(length), c); \
    } \
}

Definition at line 219 of file utf8.h.

#define UTF8_APPEND_CHAR_UNSAFE s,
i,
 
 

Value:

{ \
    if((uint32_t)(c)<=0x7f) { \
        (s)[(i)++]=(uint8_t)(c); \
    } else { \
        if((uint32_t)(c)<=0x7ff) { \
            (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0); \
        } else { \
            if((uint32_t)(c)<=0xffff) { \
                (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0); \
            } else { \
                (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0); \
                (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80); \
            } \
            (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80); \
        } \
        (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80); \
    } \
}

Definition at line 173 of file utf8.h.

#define UTF8_ARRAY_SIZE size       ((5*(size))/2)
 

Average number of code units compared to UTF-16.

Definition at line 128 of file utf8.h.

#define UTF8_BACK_1_SAFE s,
start,
 
 

Value:

{ \
    if(UTF8_IS_TRAIL((s)[--(i)])) { \
        (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
    } \
}

Definition at line 307 of file utf8.h.

#define UTF8_BACK_1_UNSAFE s,
 
 

Value:

{ \
    while(UTF8_IS_TRAIL((s)[--(i)])) {} \
}

Definition at line 279 of file utf8.h.

#define UTF8_BACK_N_SAFE s,
start,
i,
 
 

Value:

{ \
    int32_t __N=(n); \
    while(__N>0 && (i)>(start)) { \
        UTF8_BACK_1_SAFE(s, start, i); \
        --__N; \
    } \
}

Definition at line 313 of file utf8.h.

#define UTF8_BACK_N_UNSAFE s,
i,
 
 

Value:

{ \
    int32_t __N=(n); \
    while(__N>0) { \
        UTF8_BACK_1_UNSAFE(s, i); \
        --__N; \
    } \
}

Definition at line 283 of file utf8.h.

#define UTF8_CHAR_LENGTH  
 

Value:

((uint32_t)(c)<=0x7f ? 1 : \
            ((uint32_t)(c)<=0x7ff ? 2 : \
                ((uint32_t)((c)-0x10000)>0xfffff ? 3 : 4) \
            ) \
        )
Given the lead character, how many bytes are taken by this code point.

ICU does not deal with code points >0x10ffff unless necessary for advancing in the byte stream.

These length macros take into account that for values >0x10ffff the "safe" append macros would write the error code point 0xffff with 3 bytes. Code point comparisons need to be in uint32_t because UChar32 may be a signed type, and negative values must be recognized.

Definition at line 103 of file utf8.h.

#define UTF8_COUNT_TRAIL_BYTES leadByte       (utf8_countTrailBytes[(uint8_t)leadByte])
 

Count the trail bytes for a lead byte - this macro should be used so that the assembler code that is mentioned in utf_impl.c could be used here.

Definition at line 55 of file utf8.h.

#define UTF8_FWD_1_SAFE s,
i,
length   
 

Value:

{ \
    uint8_t __b=(s)[(i)++]; \
    if(UTF8_IS_LEAD(__b)) { \
        uint8_t __count=UTF8_COUNT_TRAIL_BYTES(__b); \
        if((i)+__count>(length)) { \
            __count=(uint8_t)((length)-(i)); \
        } \
        while(__count>0 && UTF8_IS_TRAIL((s)[i])) { \
            ++(i); \
            --__count; \
        } \
    } \
}

Definition at line 227 of file utf8.h.

#define UTF8_FWD_1_UNSAFE s,
 
 

Value:

{ \
    (i)+=1+UTF8_COUNT_TRAIL_BYTES((s)[i]); \
}

Definition at line 192 of file utf8.h.

#define UTF8_FWD_N_SAFE s,
i,
length,
 
 

Value:

{ \
    int32_t __N=(n); \
    while(__N>0 && (i)<(length)) { \
        UTF8_FWD_1_SAFE(s, i, length); \
        --__N; \
    } \
}

Definition at line 241 of file utf8.h.

#define UTF8_FWD_N_UNSAFE s,
i,
 
 

Value:

{ \
    int32_t __N=(n); \
    while(__N>0) { \
        UTF8_FWD_1_UNSAFE(s, i); \
        --__N; \
    } \
}

Definition at line 196 of file utf8.h.

#define UTF8_GET_CHAR_SAFE s,
start,
i,
length,
c,
strict   
 

Value:

{ \
    int32_t __I=(int32_t)(i); \
    UTF8_SET_CHAR_START_SAFE(s, start, __I); \
    UTF8_NEXT_CHAR_SAFE(s, __I, length, c, strict); \
}

Definition at line 136 of file utf8.h.

#define UTF8_GET_CHAR_UNSAFE s,
i,
 
 

Value:

{ \
    int32_t __I=(int32_t)(i); \
    UTF8_SET_CHAR_START_UNSAFE(s, __I); \
    UTF8_NEXT_CHAR_UNSAFE(s, __I, c); \
}

Definition at line 130 of file utf8.h.

#define UTF8_IS_LEAD uchar       ((uint8_t)((uchar)-0xc0)<0x3e)
 

Is this this code unit the lead code unit (byte) of a code point?

Definition at line 84 of file utf8.h.

#define UTF8_IS_SINGLE uchar       (((uchar)&0x80)==0)
 

Is this this code point a single code unit (byte)?

Definition at line 82 of file utf8.h.

#define UTF8_IS_TRAIL uchar       (((uchar)&0xc0)==0x80)
 

Is this this code unit a trailing code unit (byte) of a code point?

Definition at line 86 of file utf8.h.

#define UTF8_MASK_LEAD_BYTE leadByte,
countTrailBytes       ((leadByte)&=(1<<(6-(countTrailBytes)))-1)
 

Definition at line 58 of file utf8.h.

#define UTF8_MAX_CHAR_LENGTH   4
 

The maximum number of bytes per code point.

Definition at line 125 of file utf8.h.

#define UTF8_NEED_MULTIPLE_UCHAR      ((uint32_t)(c)>0x7f)
 

Does this scalar Unicode value need multiple code units for storage?

Definition at line 89 of file utf8.h.

#define UTF8_NEXT_CHAR_SAFE s,
i,
length,
c,
strict   
 

Value:

{ \
    (c)=(s)[(i)++]; \
    if((c)>=0x80) { \
        if(UTF8_IS_LEAD(c)) { \
            (c)=utf8_nextCharSafeBody(s, &(i), (int32_t)(length), c, strict, NULL); \
        } else { \
            (c)=UTF8_ERROR_VALUE_1; \
        } \
    } \
}

Definition at line 208 of file utf8.h.

#define UTF8_NEXT_CHAR_UNSAFE s,
i,
 
 

Value:

{ \
    (c)=(s)[(i)++]; \
    if((uint8_t)((c)-0xc0)<0x35) { \
        uint8_t __count=UTF8_COUNT_TRAIL_BYTES(c); \
        UTF8_MASK_LEAD_BYTE(c, __count); \
        switch(__count) { \
          \
        case 3: \
            (c)=((c)<<6)|((s)[(i)++]&0x3f); \
        case 2: \
            (c)=((c)<<6)|((s)[(i)++]&0x3f); \
        case 1: \
            (c)=((c)<<6)|((s)[(i)++]&0x3f); \
          \
            break; \
        } \
    } \
}
Read a Unicode scalar value from an array of UTF-8 bytes.

Only values <=0x10ffff are accepted, and if an error occurs, then c will be set such that UTF_IS_ERROR(c). The _UNSAFE macro is fast and does not check for errors. The _SAFE macro checks for errors and optionally for irregular sequences, too, i.e., for sequences that are longer than necessary, such as <c0 80> instead of <0>. The strict checks also check for non-characters.

Definition at line 154 of file utf8.h.

#define UTF8_PREV_CHAR_SAFE s,
start,
i,
c,
strict   
 

Value:

{ \
    (c)=(s)[--(i)]; \
    if((c)>=0x80) { \
        if((c)<=0xbf) { \
            (c)=utf8_prevCharSafeBody(s, start, &(i), c, strict); \
        } else { \
            (c)=UTF8_ERROR_VALUE_1; \
        } \
    } \
}

Definition at line 296 of file utf8.h.

#define UTF8_PREV_CHAR_UNSAFE s,
i,
 
 

Value:

{ \
    (c)=(s)[--(i)]; \
    if(UTF8_IS_TRAIL(c)) { \
        uint8_t __b, __count=1, __shift=6; \
\
          \
        (c)&=0x3f; \
        for(;;) { \
            __b=(s)[--(i)]; \
            if(__b>=0xc0) { \
                UTF8_MASK_LEAD_BYTE(__b, __count); \
                (c)|=(UChar32)__b<<__shift; \
                break; \
            } else { \
                (c)|=(UChar32)(__b&0x3f)<<__shift; \
                ++__count; \
                __shift+=6; \
            } \
        } \
    } \
}

Definition at line 257 of file utf8.h.

#define UTF8_SET_CHAR_LIMIT_SAFE s,
start,
i,
length   
 

Value:

{ \
    if((start)<(i) && (i)<(length)) { \
        UTF8_BACK_1_SAFE(s, start, i); \
        UTF8_FWD_1_SAFE(s, i, length); \
    } \
}

Definition at line 326 of file utf8.h.

#define UTF8_SET_CHAR_LIMIT_UNSAFE s,
 
 

Value:

{ \
    UTF8_BACK_1_UNSAFE(s, i); \
    UTF8_FWD_1_UNSAFE(s, i); \
}

Definition at line 291 of file utf8.h.

#define UTF8_SET_CHAR_START_SAFE s,
start,
 
 

Value:

{ \
    if(UTF8_IS_TRAIL((s)[(i)])) { \
        (i)=utf8_back1SafeBody(s, start, (int32_t)(i)); \
    } \
}

Definition at line 249 of file utf8.h.

#define UTF8_SET_CHAR_START_UNSAFE s,
 
 

Value:

{ \
    while(UTF8_IS_TRAIL((s)[i])) { --(i); } \
}

Definition at line 204 of file utf8.h.


Function Documentation

U_CAPI int32_t U_EXPORT2 utf8_appendCharSafeBody uint8_t   s,
int32_t    i,
int32_t    length,
UChar32    c
 

U_CAPI int32_t U_EXPORT2 utf8_back1SafeBody const uint8_t   s,
int32_t    start,
int32_t    i
 

U_CAPI UChar32 U_EXPORT2 utf8_nextCharSafeBody const uint8_t   s,
int32_t   pi,
int32_t    length,
UChar32    c,
UBool    strict,
UBool   pIsError
 

U_CAPI UChar32 U_EXPORT2 utf8_prevCharSafeBody const uint8_t   s,
int32_t    start,
int32_t   pi,
UChar32    c,
UBool    strict
 


Variable Documentation

U_CFUNC U_IMPORT const uint8_t utf8_countTrailBytes[256]
 

Definition at line 47 of file utf8.h.


Generated on Thu Aug 15 14:13:48 2002 for ICU 2.2 by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001